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

Side by Side Diff: src/org/adblockplus/android/Preferences.java

Issue 5697499218051072: Usage of new API, cleanups (reduced) (Closed)
Patch Set: Fixed leftover first-batch review issues. Created April 16, 2014, 5:51 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 <http://adblockplus.org/>, 2 * This file is part of Adblock Plus <http://adblockplus.org/>,
3 * Copyright (C) 2006-2014 Eyeo GmbH 3 * Copyright (C) 2006-2014 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
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
55 55
56 import com.actionbarsherlock.view.Menu; 56 import com.actionbarsherlock.view.Menu;
57 import com.actionbarsherlock.view.MenuInflater; 57 import com.actionbarsherlock.view.MenuInflater;
58 import com.actionbarsherlock.view.MenuItem; 58 import com.actionbarsherlock.view.MenuItem;
59 59
60 /** 60 /**
61 * Main settings UI. 61 * Main settings UI.
62 */ 62 */
63 public class Preferences extends SummarizedPreferences 63 public class Preferences extends SummarizedPreferences
64 { 64 {
65 private static final String TAG = "Preferences"; 65 private static final String TAG = Utils.getTag(Preferences.class);
66
67 private static final int ABOUT_DIALOG = 1; 66 private static final int ABOUT_DIALOG = 1;
68 private static final int HIDEICONWARNING_DIALOG = 2; 67 private static final int HIDEICONWARNING_DIALOG = 2;
69 68
70 private static ProxyService proxyService = null; 69 private static ProxyService proxyService = null;
71
72 private static boolean firstRunActionsPending = true; 70 private static boolean firstRunActionsPending = true;
73 71
74 private RefreshableListPreference subscriptionList; 72 private RefreshableListPreference subscriptionList;
75
76 private String subscriptionSummary; 73 private String subscriptionSummary;
77 74
75 @SuppressWarnings("deprecation")
78 @Override 76 @Override
79 public void onCreate(Bundle savedInstanceState) 77 public void onCreate(final Bundle savedInstanceState)
80 { 78 {
81 super.onCreate(savedInstanceState); 79 super.onCreate(savedInstanceState);
82 80
83 PreferenceManager.setDefaultValues(this, R.xml.preferences, true); 81 PreferenceManager.setDefaultValues(this, R.xml.preferences, true);
84 PreferenceManager.setDefaultValues(this, R.xml.preferences_advanced, true); 82 PreferenceManager.setDefaultValues(this, R.xml.preferences_advanced, true);
85 setContentView(R.layout.preferences); 83 setContentView(R.layout.preferences);
86 addPreferencesFromResource(R.xml.preferences); 84 addPreferencesFromResource(R.xml.preferences);
87 85
88 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this ); 86 final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreference s(this);
89 87
90 // Check if we need to update assets 88 // Check if we need to update assets
91 int lastVersion = prefs.getInt(getString(R.string.pref_version), 0); 89 final int lastVersion = prefs.getInt(getString(R.string.pref_version), 0);
92 try 90 try
93 { 91 {
94 int thisVersion = getPackageManager().getPackageInfo(getPackageName(), 0). versionCode; 92 final int thisVersion = getPackageManager().getPackageInfo(getPackageName( ), 0).versionCode;
95 if (lastVersion != thisVersion) 93 if (lastVersion != thisVersion)
96 { 94 {
97 copyAssets(); 95 copyAssets();
98 SharedPreferences.Editor editor = prefs.edit(); 96 final SharedPreferences.Editor editor = prefs.edit();
99 editor.putInt(getString(R.string.pref_version), thisVersion); 97 editor.putInt(getString(R.string.pref_version), thisVersion);
100 editor.commit(); 98 editor.commit();
101 } 99 }
102 } 100 }
103 catch (NameNotFoundException e) 101 catch (final NameNotFoundException e)
104 { 102 {
105 copyAssets(); 103 copyAssets();
106 } 104 }
107 } 105 }
108 106
107 @SuppressWarnings("deprecation")
109 @Override 108 @Override
110 protected void onStart() 109 protected void onStart()
111 { 110 {
112 super.onStart(); 111 super.onStart();
113 AdblockPlus application = AdblockPlus.getApplication(); 112 final AdblockPlus application = AdblockPlus.getApplication();
114 application.startEngine(); 113 application.startEngine();
115 114
116 // Initialize subscription list 115 // Initialize subscription list
117 subscriptionList = (RefreshableListPreference) findPreference(getString(R.st ring.pref_subscription)); 116 subscriptionList = (RefreshableListPreference) findPreference(getString(R.st ring.pref_subscription));
118 Subscription[] subscriptions = application.getRecommendedSubscriptions(); 117 final Subscription[] subscriptions = application.getRecommendedSubscriptions ();
119 String[] entries = new String[subscriptions.length]; 118 final String[] entries = new String[subscriptions.length];
120 String[] entryValues = new String[subscriptions.length]; 119 final String[] entryValues = new String[subscriptions.length];
121 int i = 0; 120 int i = 0;
122 for (Subscription subscription : subscriptions) 121 for (final Subscription subscription : subscriptions)
123 { 122 {
124 entries[i] = subscription.title; 123 entries[i] = subscription.title;
125 entryValues[i] = subscription.url; 124 entryValues[i] = subscription.url;
126 i++; 125 i++;
127 } 126 }
128 subscriptionList.setEntries(entries); 127 subscriptionList.setEntries(entries);
129 subscriptionList.setEntryValues(entryValues); 128 subscriptionList.setEntryValues(entryValues);
130 129
131 // Set Acceptable Ads FAQ link 130 // Set Acceptable Ads FAQ link
132 HelpfulCheckBoxPreference acceptableAdsCheckBox = 131 final HelpfulCheckBoxPreference acceptableAdsCheckBox =
133 (HelpfulCheckBoxPreference) findPreference(getString(R.string.pref_accep tableads)); 132 (HelpfulCheckBoxPreference) findPreference(getString(R.string.pref_accep tableads));
134 acceptableAdsCheckBox.setHelpUrl(AdblockPlus.getApplication().getAcceptableA dsUrl()); 133 acceptableAdsCheckBox.setHelpUrl(AdblockPlus.getApplication().getAcceptableA dsUrl());
135 } 134 }
136 135
136 @SuppressWarnings("deprecation")
137 @Override 137 @Override
138 public void onResume() 138 public void onResume()
139 { 139 {
140 super.onResume(); 140 super.onResume();
141 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this ); 141 final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreference s(this);
142 142
143 final AdblockPlus application = AdblockPlus.getApplication(); 143 final AdblockPlus application = AdblockPlus.getApplication();
144 144
145 Subscription current = null; 145 Subscription current = null;
146 Subscription[] subscriptions = application.getListedSubscriptions(); 146 final Subscription[] subscriptions = application.getListedSubscriptions();
147 if (subscriptions.length > 0) 147 if (subscriptions.length > 0)
148 { 148 {
149 current = subscriptions[0]; 149 current = subscriptions[0];
150 } 150 }
151 151
152 boolean firstRun = firstRunActionsPending && application.isFirstRun(); 152 final boolean firstRun = firstRunActionsPending && application.isFirstRun();
153 firstRunActionsPending = false; 153 firstRunActionsPending = false;
154 154
155 if (firstRun && current != null) 155 if (firstRun && current != null)
156 { 156 {
157 showNotificationDialog(getString(R.string.install_name), 157 showNotificationDialog(getString(R.string.install_name),
158 String.format(getString(R.string.msg_subscription_offer, current.title )), 158 String.format(getString(R.string.msg_subscription_offer), current.titl e),
159 application.getAcceptableAdsUrl()); 159 application.getAcceptableAdsUrl());
160 application.setNotifiedAboutAcceptableAds(true); 160 application.setNotifiedAboutAcceptableAds(true);
161 setAcceptableAdsEnabled(true); 161 setAcceptableAdsEnabled(true);
162 } 162 }
163 else if (!application.isNotifiedAboutAcceptableAds()) 163 else if (!application.isNotifiedAboutAcceptableAds())
164 { 164 {
165 showNotificationDialog(getString(R.string.acceptableads_name), 165 showNotificationDialog(getString(R.string.acceptableads_name),
166 getString(R.string.msg_acceptable_ads), application.getAcceptableAdsUr l()); 166 getString(R.string.msg_acceptable_ads), application.getAcceptableAdsUr l());
167 application.setNotifiedAboutAcceptableAds(true); 167 application.setNotifiedAboutAcceptableAds(true);
168 setAcceptableAdsEnabled(true); 168 setAcceptableAdsEnabled(true);
169 } 169 }
170 170
171 // Enable manual subscription refresh 171 // Enable manual subscription refresh
172 subscriptionList.setOnRefreshClickListener(new View.OnClickListener() 172 subscriptionList.setOnRefreshClickListener(new View.OnClickListener()
173 { 173 {
174 @Override 174 @Override
175 public void onClick(View v) 175 public void onClick(final View v)
176 { 176 {
177 application.refreshSubscriptions(); 177 application.refreshSubscriptions();
178 } 178 }
179 }); 179 });
180 180
181 // Set subscription status message 181 // Set subscription status message
182 if (subscriptionSummary != null) 182 if (subscriptionSummary != null)
183 subscriptionList.setSummary(subscriptionSummary); 183 subscriptionList.setSummary(subscriptionSummary);
184 else 184 else
185 setPrefSummary(subscriptionList); 185 setPrefSummary(subscriptionList);
186 186
187 // Time to start listening for events 187 // Time to start listening for events
188 registerReceiver(receiver, new IntentFilter(AdblockPlus.BROADCAST_SUBSCRIPTI ON_STATUS)); 188 registerReceiver(receiver, new IntentFilter(AdblockPlus.BROADCAST_SUBSCRIPTI ON_STATUS));
189 registerReceiver(receiver, new IntentFilter(ProxyService.BROADCAST_STATE_CHA NGED)); 189 registerReceiver(receiver, new IntentFilter(ProxyService.BROADCAST_STATE_CHA NGED));
190 registerReceiver(receiver, new IntentFilter(ProxyService.BROADCAST_PROXY_FAI LED)); 190 registerReceiver(receiver, new IntentFilter(ProxyService.BROADCAST_PROXY_FAI LED));
191 191
192 // Update service and UI state according to user settings 192 // Update service and UI state according to user settings
193 if (current != null) 193 if (current != null)
194 { 194 {
195 subscriptionList.setValue(current.url); 195 subscriptionList.setValue(current.url);
196 application.actualizeSubscriptionStatus(current.url); 196 application.updateSubscriptionStatus(current.url);
197 } 197 }
198 boolean enabled = prefs.getBoolean(getString(R.string.pref_enabled), false); 198 final boolean enabled = prefs.getBoolean(getString(R.string.pref_enabled), f alse);
199 boolean proxyenabled = prefs.getBoolean(getString(R.string.pref_proxyenabled ), true); 199 final boolean proxyenabled = prefs.getBoolean(getString(R.string.pref_proxye nabled), true);
200 boolean autoconfigured = prefs.getBoolean(getString(R.string.pref_proxyautoc onfigured), false); 200 final boolean autoconfigured = prefs.getBoolean(getString(R.string.pref_prox yautoconfigured), false);
201 201
202 // This is weird but UI does not update on back button (when returning from advanced preferences) 202 // This is weird but UI does not update on back button (when returning from advanced preferences)
203 ((SwitchPreference) findPreference(getString(R.string.pref_enabled))).setChe cked(enabled); 203 ((SwitchPreference) findPreference(getString(R.string.pref_enabled))).setChe cked(enabled);
204 204
205 if (enabled || firstRun) 205 if (enabled || firstRun)
206 setFilteringEnabled(true); 206 setFilteringEnabled(true);
207 if (enabled || firstRun || (proxyenabled && !autoconfigured)) 207 if (enabled || firstRun || (proxyenabled && !autoconfigured))
208 setProxyEnabled(true); 208 setProxyEnabled(true);
209 209
210 bindService(new Intent(this, ProxyService.class), proxyServiceConnection, 0) ; 210 bindService(new Intent(this, ProxyService.class), proxyServiceConnection, 0) ;
211 } 211 }
212 212
213 private void showNotificationDialog(String title, String message, String url) 213 private void showNotificationDialog(final String title, String message, String url)
214 { 214 {
215 url = TextUtils.htmlEncode(url); 215 url = TextUtils.htmlEncode(url);
216 message = TextUtils.htmlEncode(message) 216 message = TextUtils.htmlEncode(message)
217 .replaceAll("&lt;a&gt;(.*?)&lt;/a&gt;", "<a href=\"" + url + "\">$1</a>" ); 217 .replaceAll("&lt;a&gt;(.*?)&lt;/a&gt;", "<a href=\"" + url + "\">$1</a>" );
218 final TextView messageView = new TextView(this); 218 final TextView messageView = new TextView(this);
219 messageView.setText(Html.fromHtml(message)); 219 messageView.setText(Html.fromHtml(message));
220 messageView.setMovementMethod(LinkMovementMethod.getInstance()); 220 messageView.setMovementMethod(LinkMovementMethod.getInstance());
221 final int padding = 10; 221 final int padding = 10;
222 messageView.setPadding(padding, padding, padding, padding); 222 messageView.setPadding(padding, padding, padding, padding);
223 new AlertDialog.Builder(this).setTitle(title) 223 new AlertDialog.Builder(this).setTitle(title)
224 .setView(messageView) 224 .setView(messageView)
225 .setIcon(android.R.drawable.ic_dialog_info) 225 .setIcon(android.R.drawable.ic_dialog_info)
226 .setPositiveButton(R.string.ok, null).create().show(); 226 .setPositiveButton(R.string.ok, null).create().show();
227 } 227 }
228 228
229 @Override 229 @Override
230 public void onPause() 230 public void onPause()
231 { 231 {
232 super.onPause(); 232 super.onPause();
233 try 233 try
234 { 234 {
235 unregisterReceiver(receiver); 235 unregisterReceiver(receiver);
236 } 236 }
237 catch (IllegalArgumentException e) 237 catch (final IllegalArgumentException e)
238 { 238 {
239 // ignore - it is thrown if receiver is not registered but it can not be 239 // ignore - it is thrown if receiver is not registered but it can not be
240 // true in normal conditions 240 // true in normal conditions
241 } 241 }
242 unbindService(proxyServiceConnection); 242 unbindService(proxyServiceConnection);
243 proxyService = null; 243 proxyService = null;
244 244
245 hideConfigurationMsg(); 245 hideConfigurationMsg();
246 } 246 }
247 247
248 @Override 248 @Override
249 protected void onStop() 249 protected void onStop()
250 { 250 {
251 super.onStop(); 251 super.onStop();
252 AdblockPlus application = AdblockPlus.getApplication(); 252 final AdblockPlus application = AdblockPlus.getApplication();
253 if (!application.isFilteringEnabled()) 253 if (!application.isFilteringEnabled())
254 application.stopEngine(); 254 application.stopEngine();
255 } 255 }
256 256
257 @Override 257 @Override
258 public boolean onCreateOptionsMenu(Menu menu) 258 public boolean onCreateOptionsMenu(final Menu menu)
259 { 259 {
260 MenuInflater inflater = getSupportMenuInflater(); 260 final MenuInflater inflater = getSupportMenuInflater();
261 inflater.inflate(R.menu.menu_preferences, menu); 261 inflater.inflate(R.menu.menu_preferences, menu);
262 return true; 262 return true;
263 } 263 }
264 264
265 @SuppressWarnings("deprecation")
265 @Override 266 @Override
266 public boolean onOptionsItemSelected(MenuItem item) 267 public boolean onOptionsItemSelected(final MenuItem item)
267 { 268 {
268 switch (item.getItemId()) 269 switch (item.getItemId())
269 { 270 {
270 case R.id.menu_help: 271 case R.id.menu_help:
271 Uri uri = Uri.parse(getString(R.string.configuring_url)); 272 final Uri uri = Uri.parse(getString(R.string.configuring_url));
272 Intent intent = new Intent(Intent.ACTION_VIEW, uri); 273 final Intent intent = new Intent(Intent.ACTION_VIEW, uri);
273 startActivity(intent); 274 startActivity(intent);
274 return true; 275 return true;
275 case R.id.menu_about: 276 case R.id.menu_about:
276 showDialog(ABOUT_DIALOG); 277 showDialog(ABOUT_DIALOG);
277 return true; 278 return true;
278 case R.id.menu_advanced: 279 case R.id.menu_advanced:
279 startActivity(new Intent(this, AdvancedPreferences.class)); 280 startActivity(new Intent(this, AdvancedPreferences.class));
280 return true; 281 return true;
281 default: 282 default:
282 return super.onOptionsItemSelected(item); 283 return super.onOptionsItemSelected(item);
283 } 284 }
284 } 285 }
285 286
286 private void setAcceptableAdsEnabled(boolean enabled) 287 @SuppressWarnings("deprecation")
288 private void setAcceptableAdsEnabled(final boolean enabled)
287 { 289 {
288 CheckBoxPreference acceptableAdsPreference = 290 final CheckBoxPreference acceptableAdsPreference =
289 (CheckBoxPreference) findPreference(getString(R.string.pref_acceptablead s)); 291 (CheckBoxPreference) findPreference(getString(R.string.pref_acceptablead s));
290 acceptableAdsPreference.setChecked(enabled); 292 acceptableAdsPreference.setChecked(enabled);
291 AdblockPlus application = AdblockPlus.getApplication(); 293 final AdblockPlus application = AdblockPlus.getApplication();
292 application.setAcceptableAdsEnabled(enabled); 294 application.setAcceptableAdsEnabled(enabled);
293 } 295 }
294 296
295 private void setFilteringEnabled(boolean enabled) 297 @SuppressWarnings("deprecation")
298 private void setFilteringEnabled(final boolean enabled)
296 { 299 {
297 SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferen ces(this).edit(); 300 final SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPr eferences(this).edit();
298 editor.putBoolean(getString(R.string.pref_enabled), enabled); 301 editor.putBoolean(getString(R.string.pref_enabled), enabled);
299 editor.commit(); 302 editor.commit();
300 ((SwitchPreference) findPreference(getString(R.string.pref_enabled))).setChe cked(enabled); 303 ((SwitchPreference) findPreference(getString(R.string.pref_enabled))).setChe cked(enabled);
301 AdblockPlus application = AdblockPlus.getApplication(); 304 final AdblockPlus application = AdblockPlus.getApplication();
302 application.setFilteringEnabled(enabled); 305 application.setFilteringEnabled(enabled);
303 } 306 }
304 307
305 private void setProxyEnabled(boolean enabled) 308 private void setProxyEnabled(final boolean enabled)
306 { 309 {
307 SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferen ces(this).edit(); 310 final SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPr eferences(this).edit();
308 editor.putBoolean(getString(R.string.pref_proxyenabled), enabled); 311 editor.putBoolean(getString(R.string.pref_proxyenabled), enabled);
309 editor.commit(); 312 editor.commit();
310 AdblockPlus application = AdblockPlus.getApplication(); 313 final AdblockPlus application = AdblockPlus.getApplication();
311 if (enabled && !application.isServiceRunning()) 314 if (enabled && !application.isServiceRunning())
312 startService(new Intent(this, ProxyService.class)); 315 startService(new Intent(this, ProxyService.class));
313 } 316 }
314 317
315 /** 318 /**
316 * Copies file assets from installation package to filesystem. 319 * Copies file assets from installation package to filesystem.
317 */ 320 */
318 private void copyAssets() 321 private void copyAssets()
319 { 322 {
320 AssetManager assetManager = getAssets(); 323 final AssetManager assetManager = getAssets();
321 String[] files = null; 324 String[] files = null;
322 try 325 try
323 { 326 {
324 files = assetManager.list("install"); 327 files = assetManager.list("install");
325 } 328 }
326 catch (IOException e) 329 catch (final IOException e)
327 { 330 {
328 Log.e(TAG, "Failed to get assets list", e); 331 Log.e(TAG, "Failed to get assets list", e);
329 } 332 }
330 for (int i = 0; i < files.length; i++) 333 for (int i = 0; i < files.length; i++)
331 { 334 {
332 try 335 try
333 { 336 {
334 Log.d(TAG, "Copy: install/" + files[i]); 337 Log.d(TAG, "Copy: install/" + files[i]);
335 InputStream in = assetManager.open("install/" + files[i]); 338 final InputStream in = assetManager.open("install/" + files[i]);
336 OutputStream out = openFileOutput(files[i], MODE_PRIVATE); 339 final OutputStream out = openFileOutput(files[i], MODE_PRIVATE);
337 byte[] buffer = new byte[1024]; 340 final byte[] buffer = new byte[1024];
338 int read; 341 int read;
339 while ((read = in.read(buffer)) != -1) 342 while ((read = in.read(buffer)) != -1)
340 { 343 {
341 out.write(buffer, 0, read); 344 out.write(buffer, 0, read);
342 } 345 }
343 in.close(); 346 in.close();
344 out.flush(); 347 out.flush();
345 out.close(); 348 out.close();
346 } 349 }
347 catch (Exception e) 350 catch (final Exception e)
348 { 351 {
349 Log.e(TAG, "Asset copy error", e); 352 Log.e(TAG, "Asset copy error", e);
350 } 353 }
351 } 354 }
352 } 355 }
353 356
354 public void showProxySettings(View v) 357 public void showProxySettings(final View v)
355 { 358 {
356 startActivity(new Intent(this, ProxyConfigurationActivity.class).putExtra("p ort", proxyService.port)); 359 startActivity(new Intent(this, ProxyConfigurationActivity.class).putExtra("p ort", proxyService.port));
357 } 360 }
358 361
359 @Override 362 @Override
360 protected Dialog onCreateDialog(int id) 363 protected Dialog onCreateDialog(final int id)
361 { 364 {
362 Dialog dialog = null; 365 Dialog dialog = null;
363 switch (id) 366 switch (id)
364 { 367 {
365 case ABOUT_DIALOG: 368 case ABOUT_DIALOG:
366 dialog = new AboutDialog(this); 369 dialog = new AboutDialog(this);
367 break; 370 break;
368 case HIDEICONWARNING_DIALOG: 371 case HIDEICONWARNING_DIALOG:
369 AlertDialog.Builder builder = new AlertDialog.Builder(this); 372 final AlertDialog.Builder builder = new AlertDialog.Builder(this);
370 builder.setTitle(R.string.warning); 373 builder.setTitle(R.string.warning);
371 builder.setIcon(android.R.drawable.ic_dialog_alert); 374 builder.setIcon(android.R.drawable.ic_dialog_alert);
372 builder.setCancelable(false); 375 builder.setCancelable(false);
373 StringBuffer message = new StringBuffer(); 376 final StringBuffer message = new StringBuffer();
374 message.append(getString(R.string.msg_hideicon_warning)); 377 message.append(getString(R.string.msg_hideicon_warning));
375 builder.setPositiveButton(R.string.gotit, new DialogInterface.OnClickLis tener() 378 builder.setPositiveButton(R.string.gotit, new DialogInterface.OnClickLis tener()
376 { 379 {
377 public void onClick(DialogInterface dialog, int id) 380 @Override
378 { 381 public void onClick(final DialogInterface dialog, final int id)
379 dialog.cancel(); 382 {
380 } 383 dialog.cancel();
381 }); 384 }
385 });
382 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) 386 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
383 { 387 {
384 message.append("<br/><br/>"); 388 message.append("<br/><br/>");
385 message.append(getString(R.string.msg_hideicon_native)); 389 message.append(getString(R.string.msg_hideicon_native));
386 builder.setNeutralButton(R.string.showme, new DialogInterface.OnClick Listener() 390 builder.setNeutralButton(R.string.showme, new DialogInterface.OnClickL istener()
391 {
392 @Override
393 public void onClick(final DialogInterface dialog, final int id)
387 { 394 {
388 public void onClick(DialogInterface dialog, int id) 395 AdblockPlus.showAppDetails(getApplicationContext());
389 { 396 dialog.cancel();
390 AdblockPlus.showAppDetails(getApplicationContext()); 397 }
391 dialog.cancel(); 398 });
392 }
393 });
394 } 399 }
395 builder.setMessage(Html.fromHtml(message.toString())); 400 builder.setMessage(Html.fromHtml(message.toString()));
396 dialog = builder.create(); 401 dialog = builder.create();
397 break; 402 break;
398 } 403 }
399 return dialog; 404 return dialog;
400 } 405 }
401 406
407 @SuppressWarnings("deprecation")
402 @Override 408 @Override
403 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, Str ing key) 409 public void onSharedPreferenceChanged(final SharedPreferences sharedPreference s, final String key)
404 { 410 {
405 AdblockPlus application = AdblockPlus.getApplication(); 411 final AdblockPlus application = AdblockPlus.getApplication();
406 if (getString(R.string.pref_enabled).equals(key)) 412 if (getString(R.string.pref_enabled).equals(key))
407 { 413 {
408 boolean enabled = sharedPreferences.getBoolean(key, false); 414 final boolean enabled = sharedPreferences.getBoolean(key, false);
409 boolean autoconfigured = sharedPreferences.getBoolean(getString(R.string.p ref_proxyautoconfigured), false); 415 final boolean autoconfigured = sharedPreferences.getBoolean(getString(R.st ring.pref_proxyautoconfigured), false);
410 boolean serviceRunning = application.isServiceRunning(); 416 final boolean serviceRunning = application.isServiceRunning();
411 application.setFilteringEnabled(enabled); 417 application.setFilteringEnabled(enabled);
412 if (enabled) 418 if (enabled)
413 { 419 {
414 // If user has enabled filtering, enable proxy as well 420 // If user has enabled filtering, enable proxy as well
415 setProxyEnabled(true); 421 setProxyEnabled(true);
416 } 422 }
417 else if (serviceRunning && autoconfigured) 423 else if (serviceRunning && autoconfigured)
418 { 424 {
419 // If user disabled filtering disable proxy only if it was autoconfigure d 425 // If user disabled filtering disable proxy only if it was autoconfigure d
420 stopService(new Intent(this, ProxyService.class)); 426 stopService(new Intent(this, ProxyService.class));
421 } 427 }
422 } 428 }
423 else if (getString(R.string.pref_acceptableads).equals(key)) 429 else if (getString(R.string.pref_acceptableads).equals(key))
424 { 430 {
425 boolean enabled = sharedPreferences.getBoolean(key, false); 431 final boolean enabled = sharedPreferences.getBoolean(key, false);
426 application.setAcceptableAdsEnabled(enabled); 432 application.setAcceptableAdsEnabled(enabled);
427 } 433 }
428 else if (getString(R.string.pref_subscription).equals(key)) 434 else if (getString(R.string.pref_subscription).equals(key))
429 { 435 {
430 String url = sharedPreferences.getString(key, null); 436 final String url = sharedPreferences.getString(key, null);
431 if (url != null) 437 if (url != null)
432 application.setSubscription(url); 438 application.setSubscription(url);
433 } 439 }
434 else if (getString(R.string.pref_hideicon).equals(key)) 440 else if (getString(R.string.pref_hideicon).equals(key))
435 { 441 {
436 boolean hideIcon = sharedPreferences.getBoolean(key, false); 442 final boolean hideIcon = sharedPreferences.getBoolean(key, false);
437 if (hideIcon) 443 if (hideIcon)
438 showDialog(HIDEICONWARNING_DIALOG); 444 showDialog(HIDEICONWARNING_DIALOG);
439 if (proxyService != null) 445 if (proxyService != null)
440 proxyService.setEmptyIcon(hideIcon); 446 proxyService.setEmptyIcon(hideIcon);
441 } 447 }
442 super.onSharedPreferenceChanged(sharedPreferences, key); 448 super.onSharedPreferenceChanged(sharedPreferences, key);
443 } 449 }
444 450
445 private void showConfigurationMsg(String message) 451 private void showConfigurationMsg(final String message)
446 { 452 {
447 ViewGroup grp = (ViewGroup) findViewById(R.id.grp_configuration); 453 final ViewGroup grp = (ViewGroup) findViewById(R.id.grp_configuration);
448 TextView msg = (TextView) findViewById(R.id.txt_configuration); 454 final TextView msg = (TextView) findViewById(R.id.txt_configuration);
449 msg.setText(Html.fromHtml(message)); 455 msg.setText(Html.fromHtml(message));
450 grp.setVisibility(View.VISIBLE); 456 grp.setVisibility(View.VISIBLE);
451 } 457 }
452 458
453 private void hideConfigurationMsg() 459 private void hideConfigurationMsg()
454 { 460 {
455 ViewGroup grp = (ViewGroup) findViewById(R.id.grp_configuration); 461 final ViewGroup grp = (ViewGroup) findViewById(R.id.grp_configuration);
456 grp.setVisibility(View.GONE); 462 grp.setVisibility(View.GONE);
457 } 463 }
458 464
459 private BroadcastReceiver receiver = new BroadcastReceiver() 465 private final BroadcastReceiver receiver = new BroadcastReceiver()
460 { 466 {
461 @Override 467 @Override
462 public void onReceive(Context context, Intent intent) 468 public void onReceive(final Context context, final Intent intent)
463 { 469 {
464 String action = intent.getAction(); 470 final String action = intent.getAction();
465 Bundle extra = intent.getExtras(); 471 final Bundle extra = intent.getExtras();
466 if (action.equals(ProxyService.BROADCAST_STATE_CHANGED)) 472 if (action.equals(ProxyService.BROADCAST_STATE_CHANGED))
467 { 473 {
468 if (extra.getBoolean("enabled")) 474 if (extra.getBoolean("enabled"))
469 { 475 {
470 // Service is enabled in manual mode 476 // Service is enabled in manual mode
471 if (extra.getBoolean("manual")) 477 if (extra.getBoolean("manual"))
472 { 478 {
473 // Proxy is properly configured 479 // Proxy is properly configured
474 if (extra.getBoolean("configured")) 480 if (extra.getBoolean("configured"))
475 hideConfigurationMsg(); 481 hideConfigurationMsg();
476 else 482 else
477 showConfigurationMsg(getString(R.string.msg_configuration)); 483 showConfigurationMsg(getString(R.string.msg_configuration));
478 } 484 }
479 } 485 }
480 else 486 else
481 { 487 {
482 setFilteringEnabled(false); 488 setFilteringEnabled(false);
483 hideConfigurationMsg(); 489 hideConfigurationMsg();
484 } 490 }
485 } 491 }
486 if (action.equals(ProxyService.BROADCAST_PROXY_FAILED)) 492 if (action.equals(ProxyService.BROADCAST_PROXY_FAILED))
487 { 493 {
488 String msg = extra.getString("msg"); 494 final String msg = extra.getString("msg");
489 new AlertDialog.Builder(Preferences.this).setTitle(R.string.error).setMe ssage(msg).setIcon(android.R.drawable.ic_dialog_alert).setPositiveButton(R.strin g.ok, null).create().show(); 495 new AlertDialog.Builder(Preferences.this).setTitle(R.string.error).setMe ssage(msg).setIcon(android.R.drawable.ic_dialog_alert).setPositiveButton(R.strin g.ok, null).create().show();
490 setFilteringEnabled(false); 496 setFilteringEnabled(false);
491 } 497 }
492 if (action.equals(AdblockPlus.BROADCAST_SUBSCRIPTION_STATUS)) 498 if (action.equals(AdblockPlus.BROADCAST_SUBSCRIPTION_STATUS))
493 { 499 {
494 //TODO Should check if url matches active subscription 500 // TODO Should check if url matches active subscription
495 final String text = extra.getString("status"); 501 final String text = extra.getString("status");
496 final long time = extra.getLong("time"); 502 final long time = extra.getLong("time");
497 runOnUiThread(new Runnable() 503 runOnUiThread(new Runnable()
498 { 504 {
505 @Override
499 public void run() 506 public void run()
500 { 507 {
501 setSubscriptionStatus(text, time); 508 setSubscriptionStatus(text, time);
502 } 509 }
503 }); 510 });
504 } 511 }
505 } 512 }
506 }; 513 };
507 514
508 /** 515 /**
509 * Constructs and updates subscription status text. 516 * Constructs and updates subscription status text.
510 * 517 *
511 * @param text 518 * @param text
512 * status message 519 * status message
513 * @param time 520 * @param time
514 * time of last change 521 * time of last change
515 */ 522 */
516 private void setSubscriptionStatus(String text, long time) 523 @SuppressWarnings("deprecation")
524 private void setSubscriptionStatus(final String text, final long time)
517 { 525 {
518 ListPreference subscriptionList = (ListPreference) findPreference(getString( R.string.pref_subscription)); 526 final ListPreference subscriptionList = (ListPreference) findPreference(getS tring(R.string.pref_subscription));
519 CharSequence summary = subscriptionList.getEntry(); 527 final CharSequence summary = subscriptionList.getEntry();
520 StringBuilder builder = new StringBuilder(); 528 final StringBuilder builder = new StringBuilder();
521 if (summary != null) 529 if (summary != null)
522 { 530 {
523 builder.append(summary); 531 builder.append(summary);
524 if (text != "") 532 if (text != "")
525 { 533 {
526 builder.append(" ("); 534 builder.append(" (");
527 int id = getResources().getIdentifier(text, "string", getPackageName()); 535 final int id = getResources().getIdentifier(text, "string", getPackageNa me());
528 if (id > 0) 536 if (id > 0)
529 builder.append(getString(id, text)); 537 builder.append(getString(id, text));
530 else 538 else
531 builder.append(text); 539 builder.append(text);
532 if (time > 0) 540 if (time > 0)
533 { 541 {
534 builder.append(": "); 542 builder.append(": ");
535 Calendar calendar = Calendar.getInstance(); 543 final Calendar calendar = Calendar.getInstance();
536 calendar.setTimeInMillis(time); 544 calendar.setTimeInMillis(time);
537 Date date = calendar.getTime(); 545 final Date date = calendar.getTime();
538 builder.append(DateFormat.getDateFormat(this).format(date)); 546 builder.append(DateFormat.getDateFormat(this).format(date));
539 builder.append(" "); 547 builder.append(" ");
540 builder.append(DateFormat.getTimeFormat(this).format(date)); 548 builder.append(DateFormat.getTimeFormat(this).format(date));
541 } 549 }
542 builder.append(")"); 550 builder.append(")");
543 } 551 }
544 subscriptionSummary = builder.toString(); 552 subscriptionSummary = builder.toString();
545 subscriptionList.setSummary(subscriptionSummary); 553 subscriptionList.setSummary(subscriptionSummary);
546 } 554 }
547 } 555 }
548 556
549 @Override 557 @Override
550 protected void onRestoreInstanceState(Bundle state) 558 protected void onRestoreInstanceState(final Bundle state)
551 { 559 {
552 super.onRestoreInstanceState(state); 560 super.onRestoreInstanceState(state);
553 subscriptionSummary = state.getString("subscriptionSummary"); 561 subscriptionSummary = state.getString("subscriptionSummary");
554 } 562 }
555 563
556 @Override 564 @Override
557 protected void onSaveInstanceState(Bundle outState) 565 protected void onSaveInstanceState(final Bundle outState)
558 { 566 {
559 outState.putString("subscriptionSummary", subscriptionSummary); 567 outState.putString("subscriptionSummary", subscriptionSummary);
560 super.onSaveInstanceState(outState); 568 super.onSaveInstanceState(outState);
561 } 569 }
562 570
563 private ServiceConnection proxyServiceConnection = new ServiceConnection() 571 private final ServiceConnection proxyServiceConnection = new ServiceConnection ()
564 { 572 {
565 public void onServiceConnected(ComponentName className, IBinder service) 573 @Override
574 public void onServiceConnected(final ComponentName className, final IBinder service)
566 { 575 {
567 proxyService = ((ProxyService.LocalBinder) service).getService(); 576 proxyService = ((ProxyService.LocalBinder) service).getService();
568 Log.d(TAG, "Proxy service connected"); 577 Log.d(TAG, "Proxy service connected");
569 578
570 if (proxyService.isManual() && proxyService.noTraffic()) 579 if (proxyService.isManual() && proxyService.noTraffic())
571 showConfigurationMsg(getString(R.string.msg_configuration)); 580 showConfigurationMsg(getString(R.string.msg_configuration));
572 } 581 }
573 582
574 public void onServiceDisconnected(ComponentName className) 583 @Override
584 public void onServiceDisconnected(final ComponentName className)
575 { 585 {
576 proxyService = null; 586 proxyService = null;
577 Log.d(TAG, "Proxy service disconnected"); 587 Log.d(TAG, "Proxy service disconnected");
578 } 588 }
579 }; 589 };
580 } 590 }
OLDNEW

Powered by Google App Engine
This is Rietveld