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

Side by Side Diff: adblockplussbrowser/src/org/adblockplus/sbrowser/contentblocker/MainPreferences.java

Issue 29449601: Issue 5235 - Refactoring on SharedPrefs (Closed)
Patch Set: Fixing unregisterOnSharedPreferenceChangeListener using wrapper Created May 31, 2017, 2:33 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
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.sbrowser.contentblocker; 18 package org.adblockplus.sbrowser.contentblocker;
19 19
20 import org.adblockplus.sbrowser.contentblocker.engine.Engine; 20 import org.adblockplus.sbrowser.contentblocker.engine.Engine;
21 import org.adblockplus.sbrowser.contentblocker.engine.EngineService; 21 import org.adblockplus.sbrowser.contentblocker.engine.EngineService;
22 import org.adblockplus.adblockplussbrowser.R; 22 import org.adblockplus.adblockplussbrowser.R;
23 import org.adblockplus.sbrowser.contentblocker.util.SharedPrefsUtils;
23 24
24 import android.app.AlertDialog; 25 import android.app.AlertDialog;
25 import android.app.ProgressDialog; 26 import android.app.ProgressDialog;
26 import android.content.DialogInterface; 27 import android.content.DialogInterface;
27 import android.content.DialogInterface.OnClickListener; 28 import android.content.DialogInterface.OnClickListener;
28 import android.content.Intent; 29 import android.content.Intent;
29 import android.content.SharedPreferences;
30 import android.net.Uri; 30 import android.net.Uri;
31 import android.os.Bundle; 31 import android.os.Bundle;
32 import android.preference.PreferenceActivity; 32 import android.preference.PreferenceActivity;
33 import android.preference.PreferenceManager; 33 import android.preference.PreferenceManager;
34 import android.text.Html; 34 import android.text.Html;
35 import android.util.Log; 35 import android.util.Log;
36 import android.view.Gravity; 36 import android.view.Gravity;
37 import android.widget.Button; 37 import android.widget.Button;
38 import android.widget.LinearLayout; 38 import android.widget.LinearLayout;
39 39
40 public class MainPreferences extends PreferenceActivity implements 40 public class MainPreferences extends PreferenceActivity implements
41 EngineService.OnEngineCreatedCallback, SharedPreferences.OnSharedPreferenceC hangeListener 41 EngineService.OnEngineCreatedCallback, SharedPrefsUtils.OnSharedPreferenceCh angeListener
42 { 42 {
43 private static final String TAG = MainPreferences.class.getSimpleName(); 43 private static final String TAG = MainPreferences.class.getSimpleName();
44 private Engine engine = null; 44 private Engine engine = null;
45 private AlertDialog dialog; 45 private AlertDialog dialog;
46 private int dialogTitleResId; 46 private int dialogTitleResId;
47 47
48 private SharedPreferences getSharedPreferences()
49 {
50 return PreferenceManager.getDefaultSharedPreferences(this.getApplicationCont ext());
51 }
52
53 @Override 48 @Override
54 public void onCreate(Bundle savedInstanceState) 49 public void onCreate(Bundle savedInstanceState)
55 { 50 {
56 super.onCreate(savedInstanceState); 51 super.onCreate(savedInstanceState);
57 PreferenceManager.setDefaultValues(this, R.xml.preferences_main, false); 52 PreferenceManager.setDefaultValues(this, R.xml.preferences_main, false);
58 53
59 this.getFragmentManager() 54 this.getFragmentManager()
60 .beginTransaction() 55 .beginTransaction()
61 .replace(android.R.id.content, new Preferences()) 56 .replace(android.R.id.content, new Preferences())
62 .commit(); 57 .commit();
63
64 // This try/catch block is a workaround for a preference mismatch
65 // issue. We check for a type mismatch in one particular key and,
66 // if there's a mismatch, clean sweep the preferences.
67 // See: https://issues.adblockplus.org/ticket/3931
68 try
69 {
70 this.getSharedPreferences().getBoolean(
71 this.getString(R.string.key_application_activated),
72 false);
73 }
74 catch(final Throwable t)
75 {
76 this.getSharedPreferences()
77 .edit()
78 .clear()
79 .commit();
80 }
81 } 58 }
82 59
83 @Override 60 @Override
84 protected void onStart() 61 protected void onStart()
85 { 62 {
86 this.dialogTitleResId = R.string.initialization_title; 63 this.dialogTitleResId = R.string.initialization_title;
87 this.dialog = ProgressDialog.show(this, 64 this.dialog = ProgressDialog.show(this,
88 this.getString(this.dialogTitleResId), 65 this.getString(this.dialogTitleResId),
89 this.getString(R.string.initialization_message)); 66 this.getString(R.string.initialization_message));
90 super.onStart(); 67 super.onStart();
91 this.getSharedPreferences().registerOnSharedPreferenceChangeListener(this); 68 SharedPrefsUtils.registerOnSharedPreferenceChangeListener(this, this);
92 EngineService.startService(this.getApplicationContext(), this); 69 EngineService.startService(this.getApplicationContext(), this);
93 } 70 }
94 71
95 @Override 72 @Override
96 protected void onStop() 73 protected void onStop()
97 { 74 {
98 super.onStop(); 75 super.onStop();
99 this.getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this) ; 76 SharedPrefsUtils.unregisterOnSharedPreferenceChangeListener(this, this);
100 this.dismissDialog(); 77 this.dismissDialog();
101 } 78 }
102 79
103 private void dismissDialog() 80 private void dismissDialog()
104 { 81 {
105 if (this.dialog != null) 82 if (this.dialog != null)
106 { 83 {
107 this.dialogTitleResId = 0; 84 this.dialogTitleResId = 0;
108 this.dialog.dismiss(); 85 this.dialog.dismiss();
109 this.dialog = null; 86 this.dialog = null;
(...skipping 29 matching lines...) Expand all
139 this.dialog.show(); 116 this.dialog.show();
140 } 117 }
141 else 118 else
142 { 119 {
143 this.checkAAStatusAndProceed(); 120 this.checkAAStatusAndProceed();
144 } 121 }
145 } 122 }
146 123
147 private void checkAAStatusAndProceed() 124 private void checkAAStatusAndProceed()
148 { 125 {
149 final SharedPreferences prefs = this.getSharedPreferences(); 126 final boolean aaInfoShown = SharedPrefsUtils.getBoolean(this, R.string.key_a a_info_shown, false);
150 final String keyAaInfoShown = this.getString(R.string.key_aa_info_shown);
151 final boolean aaInfoShown = prefs.getBoolean(keyAaInfoShown, false);
152 if (!aaInfoShown) 127 if (!aaInfoShown)
153 { 128 {
154 this.dialogTitleResId = R.string.aa_dialog_title; 129 this.dialogTitleResId = R.string.aa_dialog_title;
155 this.dialog = new AlertDialog.Builder(this) 130 this.dialog = new AlertDialog.Builder(this)
156 .setCancelable(false) 131 .setCancelable(false)
157 .setTitle(this.dialogTitleResId) 132 .setTitle(this.dialogTitleResId)
158 .setMessage(Html.fromHtml(getString(R.string.aa_dialog_message))) 133 .setMessage(Html.fromHtml(getString(R.string.aa_dialog_message)))
159 .setNeutralButton(R.string.aa_dialog_button, new OnClickListener() 134 .setNeutralButton(R.string.aa_dialog_button, new OnClickListener()
160 { 135 {
161 @Override 136 @Override
162 public void onClick(DialogInterface dialog, int which) 137 public void onClick(DialogInterface dialog, int which)
163 { 138 {
164 prefs.edit() 139 SharedPrefsUtils.putBoolean(MainPreferences.this, R.string.key_aa_ info_shown, true);
165 .putBoolean(keyAaInfoShown, true)
166 .commit();
167 MainPreferences.this.checkSetupStatus(); 140 MainPreferences.this.checkSetupStatus();
168 } 141 }
169 }).create(); 142 }).create();
170 this.dialog.show(); 143 this.dialog.show();
171 } 144 }
172 else 145 else
173 { 146 {
174 this.checkSetupStatus(); 147 this.checkSetupStatus();
175 } 148 }
176 } 149 }
177 150
178 private void checkSetupStatus() 151 private void checkSetupStatus()
179 { 152 {
180 final boolean applicationActivated = this.getSharedPreferences() 153 final boolean applicationActivated = SharedPrefsUtils.getBoolean(
181 .getBoolean(this.getString(R.string.key_application_activated), false); 154 this, R.string.key_application_activated, false);
182 155
183 if (!applicationActivated) 156 if (!applicationActivated)
184 { 157 {
185 Log.d(TAG, "Showing setup dialog"); 158 Log.d(TAG, "Showing setup dialog");
186 this.dialogTitleResId = R.string.setup_dialog_title; 159 this.dialogTitleResId = R.string.setup_dialog_title;
187 this.dialog = new AlertDialog.Builder(this) 160 this.dialog = new AlertDialog.Builder(this)
188 .setCancelable(false) 161 .setCancelable(false)
189 .setTitle(this.dialogTitleResId) 162 .setTitle(this.dialogTitleResId)
190 .setMessage(Html.fromHtml(getString(Engine.hasSamsungInternetVersion5O rNewer(MainPreferences.this) ? 163 .setMessage(Html.fromHtml(getString(Engine.hasSamsungInternetVersion5O rNewer(MainPreferences.this) ?
191 R.string.setup_dialog_message_sbrowser_5 : R.string.setup_dial og_message_sbrowser_4))) 164 R.string.setup_dialog_message_sbrowser_5 : R.string.setup_dial og_message_sbrowser_4)))
(...skipping 23 matching lines...) Expand all
215 this.engine = success ? engine : null; 188 this.engine = success ? engine : null;
216 if (this.dialogTitleResId == R.string.initialization_title) 189 if (this.dialogTitleResId == R.string.initialization_title)
217 { 190 {
218 this.dismissDialog(); 191 this.dismissDialog();
219 192
220 this.checkForCompatibleSBrowserAndProceed(); 193 this.checkForCompatibleSBrowserAndProceed();
221 } 194 }
222 } 195 }
223 196
224 @Override 197 @Override
225 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, Str ing key) 198 public void onSharedPreferenceChanged(String key)
226 { 199 {
227 if (this.getString(R.string.key_automatic_updates).equals(key) && this.engin e != null) 200 if (this.getString(R.string.key_automatic_updates).equals(key) && this.engin e != null)
228 { 201 {
229 this.engine.connectivityChanged(); 202 this.engine.connectivityChanged();
230 } 203 }
231 else if (this.getString(R.string.key_acceptable_ads).equals(key)) 204 else if (this.getString(R.string.key_acceptable_ads).equals(key))
232 { 205 {
233 boolean enabled = sharedPreferences.getBoolean(key, true); 206 final boolean enabled = SharedPrefsUtils.getBoolean(this, R.string.key_acc eptable_ads, true);
234 final String id = "url:" + this.engine.getPrefsDefault(Engine.SUBSCRIPTION S_EXCEPTIONSURL); 207 final String id = "url:" + this.engine.getPrefsDefault(Engine.SUBSCRIPTION S_EXCEPTIONSURL);
235 Log.d(TAG, "Acceptable ads " + (enabled ? "enabled" : "disabled")); 208 Log.d(TAG, "Acceptable ads " + (enabled ? "enabled" : "disabled"));
236 this.engine.changeSubscriptionState(id, enabled); 209 this.engine.changeSubscriptionState(id, enabled);
237 } 210 }
238 else if (this.getString(R.string.key_application_activated).equals(key)) 211 else if (this.getString(R.string.key_application_activated).equals(key))
239 { 212 {
240 if (this.dialogTitleResId == R.string.setup_dialog_title) 213 if (this.dialogTitleResId == R.string.setup_dialog_title)
241 { 214 {
242 this.dismissDialog(); 215 this.dismissDialog();
243 } 216 }
244 } 217 }
245 } 218 }
246 } 219 }
OLDNEW

Powered by Google App Engine
This is Rietveld