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

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

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

Powered by Google App Engine
This is Rietveld