Left: | ||
Right: |
OLD | NEW |
---|---|
(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 AdblockGeneralSettingsFragment#newInstance} factory method to | |
37 * create an instance of this fragment. | |
38 */ | |
39 public class AdblockGeneralSettingsFragment | |
40 extends AdblockSettingsFragment<AdblockGeneralSettingsFragment.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 /** | |
49 * Listener with additional `onWhitelistedDomainsClicked` event | |
50 */ | |
51 public interface Listener extends AdblockSettingsFragment.Listener | |
52 { | |
53 void onWhitelistedDomainsClicked(AdblockGeneralSettingsFragment fragment); | |
54 } | |
55 | |
56 /** | |
57 * Use this factory method to create a new instance of | |
58 * this fragment using the provided parameters. | |
59 * @return A new instance of fragment AdblockGeneralSettingsFragment. | |
60 */ | |
61 public static AdblockGeneralSettingsFragment newInstance() | |
62 { | |
63 return new AdblockGeneralSettingsFragment(); | |
64 } | |
65 | |
66 public AdblockGeneralSettingsFragment() | |
67 { | |
68 // required empty public constructor | |
69 } | |
70 | |
71 @Override | |
72 public void onAttach(Activity activity) | |
73 { | |
74 super.onAttach(activity); | |
75 listener = castOrThrow(activity, Listener.class); | |
76 } | |
77 | |
78 @Override | |
79 public void onCreate(Bundle savedInstanceState) | |
80 { | |
81 super.onCreate(savedInstanceState); | |
82 readKeys(); | |
83 | |
84 addPreferencesFromResource(R.xml.preference_adblock_general); | |
85 bindPreferences(); | |
86 } | |
87 | |
88 @Override | |
89 public void onResume() | |
90 { | |
91 super.onResume(); | |
92 initPreferences(); | |
93 } | |
94 | |
95 private void readKeys() | |
96 { | |
97 SETTINGS_ENABLED_KEY = getString(R.string.fragment_adblock_settings_enabled_ key); | |
98 SETTINGS_FILTER_LISTS_KEY = getString(R.string.fragment_adblock_settings_fil ter_lists_key); | |
99 SETTINGS_AA_ENABLED_KEY = getString(R.string.fragment_adblock_settings_aa_en abled_key); | |
100 SETTINGS_WL_DOMAINS_KEY = getString(R.string.fragment_adblock_settings_wl_ke y); | |
101 } | |
102 | |
103 private SwitchPreference adblockEnabled; | |
104 private MultiSelectListPreference filterLists; | |
105 private SwitchPreference acceptableAdsEnabled; | |
106 private Preference whitelistedDomains; | |
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>(); | |
diegocarloslima
2016/11/29 17:47:40
I wouldn't insist, but I would advise to use the d
anton
2016/11/30 06:34:40
Diamond operator is supported starting Java 7 and
| |
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>(); | |
diegocarloslima
2016/11/29 17:47:41
I wouldn't insist, but I would advise to use the d
anton
2016/11/30 06:34:41
Same here (see above)
| |
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 } | |
OLD | NEW |