Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: adblockplussbrowser/src/main/java/org/adblockplus/sbrowser/contentblocker/util/SubscriptionUtils.java

Issue 29603697: Issue 5931 - Create tests for util package (Closed)
Patch Set: Created Nov. 10, 2017, 2:23 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-present eyeo GmbH
4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation.
8 *
9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 package org.adblockplus.sbrowser.contentblocker.util;
19
20 import java.util.List;
21 import java.util.Locale;
22
23 import org.adblockplus.sbrowser.contentblocker.engine.DefaultSubscriptionInfo;
24 import org.adblockplus.sbrowser.contentblocker.engine.Engine;
25
26 import android.content.res.Resources;
27
28 public class SubscriptionUtils
29 {
30
31 private static final String INDONESIAN_OLD = "in";
32 private static final String INDONESIAN_NEW = "id";
33 private static final String HEBREW_OLD = "iw";
34 private static final String HEBREW_NEW = "he";
35 private static final String YIDDISH_OLD = "ji";
36 private static final String YIDDISH_NEW = "yi";
37
38 /**
39 * @param defaultSubscriptions
40 * @return The URL of the filter list which fits best for the device language.
41 * If no match was found, return EASYLIST as default.
42 */
43 public static String chooseDefaultSubscriptionUrl(List<DefaultSubscriptionInfo > defaultSubscriptions)
44 {
45 for (final DefaultSubscriptionInfo info : defaultSubscriptions)
46 {
47 String lang = getDeviceLanguageCode();
48 if (info != null && info.getPrefixes().contains(lang) && info.isComplete() )
49 {
50 return info.getUrl();
51 }
52 }
53 return Engine.EASYLIST_URL;
54 }
55
56 @SuppressWarnings("deprecation")
57 private static String getDeviceLanguageCode()
58 {
59 Locale locale;
60 if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N)
61 {
62 locale = Resources.getSystem().getConfiguration().getLocales().get(0);
63 }
64 else
65 {
66 locale = Resources.getSystem().getConfiguration().locale;
67 }
68
69 return convertOldISOCodes(locale.getLanguage());
70 }
71
72 /*
73 * ISO 639 is not a stable standard; some of the language codes it defines
74 * (specifically "iw", "ji", and "in") have changed. Android returns the old c odes
75 * ("iw", "ji", and "in") but in subscriptions.xml we use the new codes ("he", "yi", and "id").
76 * To match the device language to a subscription, we need to convert the old codes.
77 */
78 private static String convertOldISOCodes(String language)
79 {
80 switch (language)
81 {
82 case HEBREW_OLD:
83 return HEBREW_NEW;
84 case YIDDISH_OLD:
85 return YIDDISH_NEW;
86 case INDONESIAN_OLD:
87 return INDONESIAN_NEW;
88 default:
89 return language;
90 }
91 }
92 }
OLDNEW

Powered by Google App Engine
This is Rietveld