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 29379647: Issue 4948 - add possibility to not send data depending on connection properties (Closed)
Patch Set: updated to 4931 code review patch set 3 and later Created March 21, 2017, 10:28 a.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
1 /* 1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-2016 Eyeo GmbH 3 * Copyright (C) 2006-2016 Eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 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 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 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/>. 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 17
18 package org.adblockplus.libadblockplus.android.settings; 18 package org.adblockplus.libadblockplus.android.settings;
19 19
20 import android.app.Activity; 20 import android.app.Activity;
21 import android.os.Bundle; 21 import android.os.Bundle;
22 import android.preference.ListPreference;
22 import android.preference.MultiSelectListPreference; 23 import android.preference.MultiSelectListPreference;
23 import android.preference.Preference; 24 import android.preference.Preference;
24 import android.preference.SwitchPreference; 25 import android.preference.SwitchPreference;
25 import android.util.Log; 26 import android.util.Log;
26 27
27 import org.adblockplus.libadblockplus.android.Subscription; 28 import org.adblockplus.libadblockplus.android.Subscription;
28 29
29 import java.util.HashSet; 30 import java.util.HashSet;
30 import java.util.LinkedList; 31 import java.util.LinkedList;
31 import java.util.List; 32 import java.util.List;
32 import java.util.Set; 33 import java.util.Set;
33 34
34 /** 35 /**
35 * General Adblock settings fragment. 36 * General Adblock settings fragment.
36 * Use the {@link GeneralSettingsFragment#newInstance} factory method to 37 * Use the {@link GeneralSettingsFragment#newInstance} factory method to
37 * create an instance of this fragment. 38 * create an instance of this fragment.
38 */ 39 */
39 public class GeneralSettingsFragment 40 public class GeneralSettingsFragment
40 extends BaseSettingsFragment<GeneralSettingsFragment.Listener> 41 extends BaseSettingsFragment<GeneralSettingsFragment.Listener>
41 implements Preference.OnPreferenceChangeListener, Preference.OnPreferenceClick Listener 42 implements Preference.OnPreferenceChangeListener, Preference.OnPreferenceClick Listener
42 { 43 {
43 private String SETTINGS_ENABLED_KEY; 44 private String SETTINGS_ENABLED_KEY;
44 private String SETTINGS_FILTER_LISTS_KEY; 45 private String SETTINGS_FILTER_LISTS_KEY;
45 private String SETTINGS_AA_ENABLED_KEY; 46 private String SETTINGS_AA_ENABLED_KEY;
46 private String SETTINGS_WL_DOMAINS_KEY; 47 private String SETTINGS_WL_DOMAINS_KEY;
48 private String SETTINGS_ALLOWED_CONNECTION_TYPE_KEY;
47 49
48 private SwitchPreference adblockEnabled; 50 private SwitchPreference adblockEnabled;
49 private MultiSelectListPreference filterLists; 51 private MultiSelectListPreference filterLists;
50 private SwitchPreference acceptableAdsEnabled; 52 private SwitchPreference acceptableAdsEnabled;
51 private Preference whitelistedDomains; 53 private Preference whitelistedDomains;
54 private ListPreference allowedConnectionType;
52 55
53 /** 56 /**
54 * Listener with additional `onWhitelistedDomainsClicked` event 57 * Listener with additional `onWhitelistedDomainsClicked` event
55 */ 58 */
56 public interface Listener extends BaseSettingsFragment.Listener 59 public interface Listener extends BaseSettingsFragment.Listener
57 { 60 {
58 void onWhitelistedDomainsClicked(GeneralSettingsFragment fragment); 61 void onWhitelistedDomainsClicked(GeneralSettingsFragment fragment);
59 } 62 }
60 63
61 /** 64 /**
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
96 super.onResume(); 99 super.onResume();
97 initPreferences(); 100 initPreferences();
98 } 101 }
99 102
100 private void readKeys() 103 private void readKeys()
101 { 104 {
102 SETTINGS_ENABLED_KEY = getString(R.string.fragment_adblock_settings_enabled_ key); 105 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); 106 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); 107 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); 108 SETTINGS_WL_DOMAINS_KEY = getString(R.string.fragment_adblock_settings_wl_ke y);
109 SETTINGS_ALLOWED_CONNECTION_TYPE_KEY = getString(R.string.fragment_adblock_s ettings_allowed_connection_type_key);
106 } 110 }
107 111
108 private void bindPreferences() 112 private void bindPreferences()
109 { 113 {
110 adblockEnabled = (SwitchPreference) findPreference(SETTINGS_ENABLED_KEY); 114 adblockEnabled = (SwitchPreference) findPreference(SETTINGS_ENABLED_KEY);
111 filterLists = (MultiSelectListPreference) findPreference(SETTINGS_FILTER_LIS TS_KEY); 115 filterLists = (MultiSelectListPreference) findPreference(SETTINGS_FILTER_LIS TS_KEY);
112 acceptableAdsEnabled = (SwitchPreference) findPreference(SETTINGS_AA_ENABLED _KEY); 116 acceptableAdsEnabled = (SwitchPreference) findPreference(SETTINGS_AA_ENABLED _KEY);
113 whitelistedDomains = findPreference(SETTINGS_WL_DOMAINS_KEY); 117 whitelistedDomains = findPreference(SETTINGS_WL_DOMAINS_KEY);
118 allowedConnectionType = (ListPreference) findPreference(SETTINGS_ALLOWED_CON NECTION_TYPE_KEY);
114 } 119 }
115 120
116 private void initPreferences() 121 private void initPreferences()
117 { 122 {
118 initEnabled(); 123 initEnabled();
119 initFilterLists(); 124 initFilterLists();
120 initAcceptableAdsEnabled(); 125 initAcceptableAdsEnabled();
121 initWhitelistedDomains(); 126 initWhitelistedDomains();
127 initUpdatesConnection();
128 }
129
130 private void initUpdatesConnection()
131 {
132 CharSequence[] values =
133 {
134 ConnectionType.WIFI_NON_METERED.getValue(),
135 ConnectionType.WIFI.getValue(),
136 ConnectionType.ANY.getValue()
137 };
138
139 CharSequence[] titles =
140 {
141 getString(R.string.fragment_adblock_settings_allowed_connection_type_wifi_ non_metered),
142 getString(R.string.fragment_adblock_settings_allowed_connection_type_wifi) ,
143 getString(R.string.fragment_adblock_settings_allowed_connection_type_all),
144 };
145
146 allowedConnectionType.setEntryValues(values);
147 allowedConnectionType.setEntries(titles);
148
149 // selected value
150 ConnectionType connectionType = settings.getAllowedConnectionType();
151 if (connectionType == null)
152 {
153 connectionType = ConnectionType.ANY;
154 }
155 allowedConnectionType.setValue(connectionType.getValue());
156 allowedConnectionType.setOnPreferenceChangeListener(this);
122 } 157 }
123 158
124 private void initWhitelistedDomains() 159 private void initWhitelistedDomains()
125 { 160 {
126 whitelistedDomains.setOnPreferenceClickListener(this); 161 whitelistedDomains.setOnPreferenceClickListener(this);
127 } 162 }
128 163
129 private void initAcceptableAdsEnabled() 164 private void initAcceptableAdsEnabled()
130 { 165 {
131 acceptableAdsEnabled.setChecked(settings.isAcceptableAdsEnabled()); 166 acceptableAdsEnabled.setChecked(settings.isAcceptableAdsEnabled());
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
174 handleEnabledChanged((Boolean)newValue); 209 handleEnabledChanged((Boolean)newValue);
175 } 210 }
176 else if (preference.getKey().equals(SETTINGS_FILTER_LISTS_KEY)) 211 else if (preference.getKey().equals(SETTINGS_FILTER_LISTS_KEY))
177 { 212 {
178 handleFilterListsChanged((Set<String>) newValue); 213 handleFilterListsChanged((Set<String>) newValue);
179 } 214 }
180 else if (preference.getKey().equals(SETTINGS_AA_ENABLED_KEY)) 215 else if (preference.getKey().equals(SETTINGS_AA_ENABLED_KEY))
181 { 216 {
182 handleAcceptableAdsEnabledChanged((Boolean) newValue); 217 handleAcceptableAdsEnabledChanged((Boolean) newValue);
183 } 218 }
219 else if (preference.getKey().equals(SETTINGS_ALLOWED_CONNECTION_TYPE_KEY))
220 {
221 handleAllowedConnectionTypeChanged((String) newValue);
222 }
184 else 223 else
185 { 224 {
186 // handle other values if changed 225 // handle other values if changed
187 // `false` for NOT update preference view state 226 // `false` for NOT update preference view state
188 return false; 227 return false;
189 } 228 }
190 229
191 // `true` for update preference view state 230 // `true` for update preference view state
192 return true; 231 return true;
193 } 232 }
194 233
234 private void handleAllowedConnectionTypeChanged(String value)
235 {
236 // update and save settings
237 settings.setAllowedConnectionType(ConnectionType.findByValue(value));
238 provider.getAdblockSettingsStorage().save(settings);
239
240 // apply settings
241 allowedConnectionType.setValue(value);
242 provider.getAdblockEngine().getFilterEngine().setAllowedConnectionType(value );
243
244 // signal event
245 listener.onAdblockSettingsChanged(this);
246
247 }
248
195 private void handleAcceptableAdsEnabledChanged(Boolean newValue) 249 private void handleAcceptableAdsEnabledChanged(Boolean newValue)
196 { 250 {
197 boolean enabledValue = newValue; 251 boolean enabledValue = newValue;
198 252
199 // update and save settings 253 // update and save settings
200 settings.setAcceptableAdsEnabled(enabledValue); 254 settings.setAcceptableAdsEnabled(enabledValue);
201 provider.getAdblockSettingsStorage().save(settings); 255 provider.getAdblockSettingsStorage().save(settings);
202 256
203 // apply settings 257 // apply settings
204 provider.getAdblockEngine().setAcceptableAdsEnabled(enabledValue); 258 provider.getAdblockEngine().setAcceptableAdsEnabled(enabledValue);
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
247 301
248 // all other settings are meaningless if adblocking is disabled 302 // all other settings are meaningless if adblocking is disabled
249 applyAdblockEnabled(newValue); 303 applyAdblockEnabled(newValue);
250 } 304 }
251 305
252 private void applyAdblockEnabled(boolean enabledValue) 306 private void applyAdblockEnabled(boolean enabledValue)
253 { 307 {
254 filterLists.setEnabled(enabledValue); 308 filterLists.setEnabled(enabledValue);
255 acceptableAdsEnabled.setEnabled(enabledValue); 309 acceptableAdsEnabled.setEnabled(enabledValue);
256 whitelistedDomains.setEnabled(enabledValue); 310 whitelistedDomains.setEnabled(enabledValue);
311 allowedConnectionType.setEnabled(enabledValue);
257 } 312 }
258 313
259 @Override 314 @Override
260 public boolean onPreferenceClick(Preference preference) 315 public boolean onPreferenceClick(Preference preference)
261 { 316 {
262 if (preference.getKey().equals(SETTINGS_WL_DOMAINS_KEY)) 317 if (preference.getKey().equals(SETTINGS_WL_DOMAINS_KEY))
263 { 318 {
264 listener.onWhitelistedDomainsClicked(this); 319 listener.onWhitelistedDomainsClicked(this);
265 } 320 }
266 else 321 else
267 { 322 {
268 // should not be invoked as only 'wl' preference is subscribed for callbac k 323 // should not be invoked as only 'wl' preference is subscribed for callbac k
269 return false; 324 return false;
270 } 325 }
271 326
272 return true; 327 return true;
273 } 328 }
274 } 329 }
OLDNEW

Powered by Google App Engine
This is Rietveld