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

Side by Side Diff: adblockplussbrowser/src/main/java/org/adblockplus/sbrowser/contentblocker/ListedSubscriptionsPreferenceCategory.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;
19
20 import java.util.Collections;
21 import java.util.HashMap;
22 import java.util.List;
23 import java.util.Locale;
24
25 import org.adblockplus.sbrowser.contentblocker.engine.DefaultSubscriptionInfo;
26 import org.adblockplus.sbrowser.contentblocker.engine.Engine;
27 import org.adblockplus.sbrowser.contentblocker.engine.EngineService;
28 import org.adblockplus.sbrowser.contentblocker.engine.SubscriptionInfo;
29 import org.adblockplus.sbrowser.contentblocker.preferences.MultilinePreferenceCa tegory;
30 import org.adblockplus.adblockplussbrowser.R;
31
32 import android.annotation.SuppressLint;
33 import android.content.Context;
34 import android.preference.CheckBoxPreference;
35 import android.preference.Preference;
36 import android.preference.Preference.OnPreferenceChangeListener;
37 import android.text.format.DateUtils;
38 import android.util.AttributeSet;
39
40 @SuppressLint("DefaultLocale")
41 public class ListedSubscriptionsPreferenceCategory extends MultilinePreferenceCa tegory implements
42 EngineService.OnEngineCreatedCallback, OnPreferenceChangeListener
43 {
44 private Engine engine = null;
45 private boolean isEnabledView = false;
46
47 private static final String[] LANGUAGE_TRANSLATIONS =
48 {
49 "id", "Bahasa Indonesia",
50 "he", "עברית"
51 };
52
53 private static final HashMap<String, String> LANGUAGE_TRANSLATION_MAP = new Ha shMap<>();
54
55 static
56 {
57 for (int i = 0; i < LANGUAGE_TRANSLATIONS.length; i += 2)
58 {
59 LANGUAGE_TRANSLATION_MAP.put(LANGUAGE_TRANSLATIONS[i], LANGUAGE_TRANSLATIO NS[i + 1]);
60 }
61 }
62
63 public ListedSubscriptionsPreferenceCategory(final Context context)
64 {
65 super(context);
66 }
67
68 public ListedSubscriptionsPreferenceCategory(final Context context, final Attr ibuteSet attrs)
69 {
70 super(context, attrs);
71 }
72
73 @Override
74 protected void onAttachedToActivity()
75 {
76 EngineService.startService(this.getContext().getApplicationContext(), this);
77 super.onAttachedToActivity();
78 }
79
80 @Override
81 public void onEngineCreated(final Engine engine, final boolean success)
82 {
83 this.engine = engine;
84 this.isEnabledView = this.getTitleRes() == R.string.enabled_subscriptions;
85
86 final HashMap<String, Locale> localeMap = new HashMap<>();
87 for (final Locale l : Locale.getAvailableLocales())
88 {
89 final String lang = l.getLanguage();
90 if (!lang.isEmpty())
91 {
92 localeMap.put(lang.toLowerCase(), l);
93 }
94 }
95
96 if (success)
97 {
98 final List<SubscriptionInfo> subs = engine.getListedSubscriptions();
99 Collections.sort(subs);
100 this.removeAll();
101
102 for (final SubscriptionInfo sub : subs)
103 {
104 if (sub.isEnabled() == this.isEnabledView)
105 {
106 switch (sub.getType())
107 {
108 case ADS:
109 final DefaultSubscriptionInfo info = engine.getDefaultSubscription InfoForUrl(
110 sub.getUrl());
111 if (info != null && !info.getPrefixes().isEmpty() && info.isComple te())
112 {
113 final CheckBoxPreference cbp = new CheckBoxPreference(this.getCo ntext());
114 if (this.isEnabledView)
115 {
116 final StringBuilder sb = new StringBuilder();
117 sb.append(this.getContext().getString(R.string.last_update));
118 sb.append(' ');
119 final long timestamp = sub.getLastUpdateTime();
120 if (timestamp > 0)
121 {
122 sb.append(DateUtils.formatDateTime(this.getContext(), timest amp,
123 DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_TIME) );
124 }
125 else
126 {
127 sb.append(this.getContext().getString(R.string.last_update_n ever));
128 }
129 cbp.setSummary(sb.toString());
130 }
131
132 cbp.setTitle(sub.getTitle());
133 final String[] prefixes = info.getPrefixes().split(",");
134 final StringBuilder sb = new StringBuilder();
135 for (String p : prefixes)
136 {
137 final Locale loc = localeMap.get(p.trim().toLowerCase());
138 if (loc != null)
139 {
140 if (sb.length() > 0)
141 {
142 sb.append(", ");
143 }
144 sb.append(loc.getDisplayLanguage(loc));
145 }
146 else
147 {
148 final String name = LANGUAGE_TRANSLATION_MAP.get(p.trim().to LowerCase());
149 {
150 if (name != null)
151 {
152 if (sb.length() > 0)
153 {
154 sb.append(", ");
155 }
156 sb.append(name);
157 }
158 }
159 }
160 }
161
162 if (sb.length() > 0)
163 {
164 cbp.setTitle(sb.toString());
165 }
166
167 cbp.setChecked(sub.isEnabled());
168 cbp.setPersistent(false);
169 cbp.setKey(sub.getId());
170 cbp.setOnPreferenceChangeListener(this);
171 this.addPreference(cbp);
172 }
173 break;
174 default:
175 break;
176 }
177 }
178 }
179 }
180 }
181
182 @Override
183 public boolean onPreferenceChange(final Preference preference, final Object ne wValue)
184 {
185 final String id = preference.getKey();
186 final boolean enabled = (Boolean) newValue;
187
188 this.engine.changeSubscriptionState(id, enabled);
189
190 return true;
191 }
192 }
OLDNEW

Powered by Google App Engine
This is Rietveld