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

Side by Side Diff: libadblockplus-android-settings/src/org/adblockplus/libadblockplus/android/settings/AdblockGeneralSettingsFragment.java

Issue 29361445: Issue 4399 - Add WebView inheritor with ad blocking (Closed)
Patch Set: Created Nov. 1, 2016, 12:14 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-2016 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.libadblockplus.android.settings;
19
20 import android.app.Activity;
21 import android.os.Bundle;
22 import android.preference.CheckBoxPreference;
diegocarloslima 2016/11/08 16:30:41 This import is never used
anton 2016/11/09 12:30:32 Acknowledged.
23 import android.preference.MultiSelectListPreference;
24 import android.preference.Preference;
25 import android.preference.SwitchPreference;
26 import android.util.Log;
27
28 import org.adblockplus.libadblockplus.android.Subscription;
29
30 import java.util.HashSet;
31 import java.util.LinkedList;
32 import java.util.List;
33 import java.util.Set;
34
35 /**
36 * General Adblock settings fragment.
37 * Use the {@link AdblockGeneralSettingsFragment#newInstance} factory method to
38 * create an instance of this fragment.
39 */
40 public class AdblockGeneralSettingsFragment
41 extends AdblockSettingsFragment<AdblockGeneralSettingsFragment.Listener>
42 implements Preference.OnPreferenceChangeListener, Preference.OnPreferenceClick Listener
43 {
44 private String SETTINGS_ENABLED_KEY;
45 private String SETTINGS_FILTER_LISTS_KEY;
46 private String SETTINGS_AA_ENABLED_KEY;
47 private String SETTINGS_WL_DOMAINS_KEY;
diegocarloslima 2016/11/08 16:30:41 This naming convention should be applied only for
anton 2016/11/09 12:30:31 they can be considered as `final`. The only reason
48
49 /**
50 * Listener with additional `onWhitelistedDomainsClicked` event
51 */
52 public interface Listener extends AdblockSettingsFragment.Listener
53 {
54 void onWhitelistedDomainsClicked(AdblockGeneralSettingsFragment fragment);
55 }
56
57 /**
58 * Use this factory method to create a new instance of
59 * this fragment using the provided parameters.
60 * @return A new instance of fragment AdblockGeneralSettingsFragment.
61 */
62 public static AdblockGeneralSettingsFragment newInstance()
63 {
64 return new AdblockGeneralSettingsFragment();
65 }
66
67 public AdblockGeneralSettingsFragment()
68 {
69 // required empty public constructor
70 }
71
72 @Override
73 public void onAttach(Activity activity)
74 {
75 super.onAttach(activity);
76 listener = castOrThrow(activity, Listener.class);
77 }
78
79 @Override
80 public void onCreate(Bundle savedInstanceState)
81 {
82 super.onCreate(savedInstanceState);
83 readKeys();
84
85 addPreferencesFromResource(R.xml.preference_adblock_general);
86 bindPreferences();
87 }
88
89 @Override
90 public void onResume()
91 {
92 super.onResume();
93 initPreferences();
94 }
95
96 private void readKeys()
97 {
98 SETTINGS_ENABLED_KEY = getString(R.string.fragment_adblock_settings_enabled_ key);
99 SETTINGS_FILTER_LISTS_KEY = getString(R.string.fragment_adblock_settings_fil ter_lists_key);
100 SETTINGS_AA_ENABLED_KEY = getString(R.string.fragment_adblock_settings_aa_en abled_key);
101 SETTINGS_WL_DOMAINS_KEY = getString(R.string.fragment_adblock_settings_wl_ke y);
102 }
103
104 private SwitchPreference adblockEnabled;
105 private MultiSelectListPreference filterLists;
106 private SwitchPreference acceptableAdsEnabled;
107 private Preference whitelistedDomains;
108
109 private void bindPreferences()
110 {
111 adblockEnabled = (SwitchPreference) findPreference(SETTINGS_ENABLED_KEY);
112 filterLists = (MultiSelectListPreference) findPreference(SETTINGS_FILTER_LIS TS_KEY);
113 acceptableAdsEnabled = (SwitchPreference) findPreference(SETTINGS_AA_ENABLED _KEY);
114 whitelistedDomains = findPreference(SETTINGS_WL_DOMAINS_KEY);
115 }
116
117 private void initPreferences()
118 {
119 initEnabled();
120 initFilterLists();
121 initAcceptableAdsEnabled();
122 initWhitelistedDomains();
123 }
124
125 private void initWhitelistedDomains()
126 {
127 whitelistedDomains.setOnPreferenceClickListener(this);
128 }
129
130 private void initAcceptableAdsEnabled()
131 {
132 acceptableAdsEnabled.setChecked(settings.isAcceptableAdsEnabled());
133 acceptableAdsEnabled.setOnPreferenceChangeListener(this);
134 }
135
136 private void initFilterLists()
137 {
138 // all available values
139 Subscription[] availableSubscriptions = provider.getAdblockEngine().getRecom mendedSubscriptions();
140 CharSequence[] availableSubscriptionsTitles = new CharSequence[availableSubs criptions.length];
141 CharSequence[] availableSubscriptionsValues = new CharSequence[availableSubs criptions.length];
142 for (int i = 0; i < availableSubscriptions.length; i++)
143 {
144 availableSubscriptionsTitles[i] = availableSubscriptions[i].title;
145 availableSubscriptionsValues[i] = availableSubscriptions[i].url;
146 }
147 filterLists.setEntries(availableSubscriptionsTitles);
148 filterLists.setEntryValues(availableSubscriptionsValues);
149
150 // selected values
151 Set<String> selectedSubscriptionValues = new HashSet<String>();
152 for (Subscription eachSubscription : settings.getSubscriptions())
153 {
154 selectedSubscriptionValues.add(eachSubscription.url);
155 }
156 filterLists.setValues(selectedSubscriptionValues);
157 filterLists.setOnPreferenceChangeListener(this);
158 }
159
160 private void initEnabled()
161 {
162 boolean enabled = settings.isAdblockEnabled();
163 adblockEnabled.setChecked(enabled);
164 adblockEnabled.setOnPreferenceChangeListener(this);
165 applyAdblockEnabled(enabled);
166 }
167
168 @Override
169 public boolean onPreferenceChange(Preference preference, Object newValue)
170 {
171 Log.d(TAG, "\"" + preference.getTitle() + "\" new value is " + newValue);
172
173 if (preference.getKey().equals(SETTINGS_ENABLED_KEY))
174 {
175 handleEnabledChanged((Boolean)newValue);
176 }
177 else if (preference.getKey().equals(SETTINGS_FILTER_LISTS_KEY))
178 {
179 handleFilterListsChanged((Set<String>) newValue);
180 }
181 else if (preference.getKey().equals(SETTINGS_AA_ENABLED_KEY))
182 {
183 handleAcceptableAdsEnabledChanged((Boolean) newValue);
184 }
185 else
186 {
187 // handle other values if changed
188 // `false` for NOT update preference view state
189 return false;
190 }
191
192 // `true` for update preference view state
193 return true;
194 }
195
196 private void handleAcceptableAdsEnabledChanged(Boolean newValue)
197 {
198 boolean enabledValue = newValue;
199
200 // update and save settings
201 settings.setAcceptableAdsEnabled(enabledValue);
202 provider.getAdblockSettingsStorage().save(settings);
203
204 // apply settings
205 provider.getAdblockEngine().setAcceptableAdsEnabled(enabledValue);
206
207 // signal event
208 listener.onAdblockSettingsChanged(this);
209 }
210
211 private void handleFilterListsChanged(Set<String> newValue)
212 {
213 Set<String> selectedValues = newValue;
diegocarloslima 2016/11/08 16:30:41 Why don't we just use the newValue parameter inste
anton 2016/11/09 12:30:31 Yes, we can and we should. It's just because of An
214 List<Subscription> selectedSubscriptions = new LinkedList<Subscription>();
215
216 for (Subscription eachSubscription : provider.getAdblockEngine().getRecommen dedSubscriptions())
217 {
218 if (selectedValues.contains(eachSubscription.url))
219 {
220 selectedSubscriptions.add(eachSubscription);
221 }
222 }
223
224 // update and save settings
225 settings.setSubscriptions(selectedSubscriptions);
226 provider.getAdblockSettingsStorage().save(settings);
227
228 // apply settings
229 provider.getAdblockEngine().setSubscriptions(selectedValues);
230
231 // since 'aa enabled' setting affects subscriptions list, we need to set it again
232 provider.getAdblockEngine().setAcceptableAdsEnabled(settings.isAcceptableAds Enabled());
233
234 // signal event
235 listener.onAdblockSettingsChanged(this);
236 }
237
238 private void handleEnabledChanged(boolean newValue)
239 {
240 // update and save settings
241 settings.setAdblockEnabled(newValue);
242 provider.getAdblockSettingsStorage().save(settings);
243
244 // apply settings
245 provider.getAdblockEngine().setEnabled(newValue);
246
247 // signal event
248 listener.onAdblockSettingsChanged(this);
249
250 // all other settings are meaningless if adblocking is disabled
251 applyAdblockEnabled(newValue);
252 }
253
254 private void applyAdblockEnabled(boolean enabledValue)
255 {
256 filterLists.setEnabled(enabledValue);
257 acceptableAdsEnabled.setEnabled(enabledValue);
258 whitelistedDomains.setEnabled(enabledValue);
259 }
260
261 @Override
262 public boolean onPreferenceClick(Preference preference)
263 {
264 if (preference.getKey().equals(SETTINGS_WL_DOMAINS_KEY))
265 {
266 listener.onWhitelistedDomainsClicked(this);
267 }
268 else
269 {
270 // should not be invoked as only 'wl' preference is subscribed for callbac k
271 return false;
272 }
273
274 return true;
275 }
276 }
OLDNEW

Powered by Google App Engine
This is Rietveld