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