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

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

Issue 9437197: ABP/Android Proxy switch (Closed)
Patch Set: ABP/Android Proxy switch Created March 11, 2013, 7:29 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 the Adblock Plus, 2 * This file is part of the Adblock Plus,
3 * Copyright (C) 2006-2012 Eyeo GmbH 3 * Copyright (C) 2006-2012 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.android; 18 package org.adblockplus.android;
19 19
20 import java.io.IOException; 20 import java.io.IOException;
21 import java.io.InputStream; 21 import java.io.InputStream;
22 import java.io.OutputStream; 22 import java.io.OutputStream;
23 import java.util.Calendar; 23 import java.util.Calendar;
24 import java.util.Date; 24 import java.util.Date;
25 import java.util.List; 25 import java.util.List;
26 26
27 import org.jraf.android.backport.switchwidget.SwitchPreference;
28
27 import android.app.ActivityManager; 29 import android.app.ActivityManager;
28 import android.app.ActivityManager.RunningServiceInfo; 30 import android.app.ActivityManager.RunningServiceInfo;
29 import android.app.AlertDialog; 31 import android.app.AlertDialog;
30 import android.app.Dialog; 32 import android.app.Dialog;
31 import android.content.BroadcastReceiver; 33 import android.content.BroadcastReceiver;
32 import android.content.ComponentName; 34 import android.content.ComponentName;
33 import android.content.Context; 35 import android.content.Context;
34 import android.content.DialogInterface; 36 import android.content.DialogInterface;
35 import android.content.Intent; 37 import android.content.Intent;
36 import android.content.IntentFilter; 38 import android.content.IntentFilter;
37 import android.content.ServiceConnection; 39 import android.content.ServiceConnection;
38 import android.content.SharedPreferences; 40 import android.content.SharedPreferences;
39 import android.content.pm.PackageManager.NameNotFoundException; 41 import android.content.pm.PackageManager.NameNotFoundException;
40 import android.content.res.AssetManager; 42 import android.content.res.AssetManager;
41 import android.net.Uri; 43 import android.net.Uri;
42 import android.os.Build; 44 import android.os.Build;
43 import android.os.Bundle; 45 import android.os.Bundle;
44 import android.os.IBinder; 46 import android.os.IBinder;
45 import android.preference.CheckBoxPreference;
46 import android.preference.ListPreference; 47 import android.preference.ListPreference;
47 import android.preference.PreferenceManager; 48 import android.preference.PreferenceManager;
48 import android.text.Html; 49 import android.text.Html;
49 import android.text.format.DateFormat; 50 import android.text.format.DateFormat;
50 import android.util.Log; 51 import android.util.Log;
51 import android.view.View; 52 import android.view.View;
52 import android.view.ViewGroup; 53 import android.view.ViewGroup;
53 import android.widget.TextView; 54 import android.widget.TextView;
54 55
55 import com.actionbarsherlock.view.Menu; 56 import com.actionbarsherlock.view.Menu;
(...skipping 14 matching lines...) Expand all
70 71
71 private RefreshableListPreference subscriptionList; 72 private RefreshableListPreference subscriptionList;
72 73
73 private String subscriptionSummary; 74 private String subscriptionSummary;
74 75
75 @Override 76 @Override
76 public void onCreate(Bundle savedInstanceState) 77 public void onCreate(Bundle savedInstanceState)
77 { 78 {
78 super.onCreate(savedInstanceState); 79 super.onCreate(savedInstanceState);
79 80
80 PreferenceManager.setDefaultValues(this, R.xml.preferences, false); 81 PreferenceManager.setDefaultValues(this, R.xml.preferences, true);
81 PreferenceManager.setDefaultValues(this, R.xml.preferences_advanced, false); 82 PreferenceManager.setDefaultValues(this, R.xml.preferences_advanced, true);
82 setContentView(R.layout.preferences); 83 setContentView(R.layout.preferences);
83 addPreferencesFromResource(R.xml.preferences); 84 addPreferencesFromResource(R.xml.preferences);
84 85
85 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this ); 86 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this );
86 87
87 // Check if we need to update assets 88 // Check if we need to update assets
88 int lastVersion = prefs.getInt(getString(R.string.pref_version), 0); 89 int lastVersion = prefs.getInt(getString(R.string.pref_version), 0);
89 try 90 try
90 { 91 {
91 int thisVersion = getPackageManager().getPackageInfo(getPackageName(), 0). versionCode; 92 int thisVersion = getPackageManager().getPackageInfo(getPackageName(), 0). versionCode;
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
187 public void run() 188 public void run()
188 { 189 {
189 if (!application.verifySubscriptions()) 190 if (!application.verifySubscriptions())
190 { 191 {
191 Subscription subscription = application.getSubscription(url); 192 Subscription subscription = application.getSubscription(url);
192 application.setSubscription(subscription); 193 application.setSubscription(subscription);
193 } 194 }
194 } 195 }
195 }).start(); 196 }).start();
196 197
197 // Check if service is running and update UI accordingly 198 // Update service and UI state according to user settings
198 boolean enabled = prefs.getBoolean(getString(R.string.pref_enabled), false); 199 boolean enabled = prefs.getBoolean(getString(R.string.pref_enabled), false);
199 if (enabled && !isServiceRunning()) 200 boolean proxyenabled = prefs.getBoolean(getString(R.string.pref_proxyenabled ), false);
200 { 201 if (enabled || firstRun)
201 setEnabled(false); 202 setFilteringEnabled(true);
202 } 203 if (enabled || firstRun || proxyenabled)
203 // Run service if this is first application run 204 setProxyEnabled(true);
204 else if (!enabled && firstRun)
205 {
206 startService(new Intent(this, ProxyService.class));
207 setEnabled(true);
208 }
209 205
210 bindService(new Intent(this, ProxyService.class), proxyServiceConnection, 0) ; 206 bindService(new Intent(this, ProxyService.class), proxyServiceConnection, 0) ;
211 } 207 }
212 208
213 @Override 209 @Override
214 public void onPause() 210 public void onPause()
215 { 211 {
216 super.onPause(); 212 super.onPause();
217 try 213 try
218 { 214 {
219 unregisterReceiver(receiver); 215 unregisterReceiver(receiver);
220 } 216 }
221 catch (IllegalArgumentException e) 217 catch (IllegalArgumentException e)
222 { 218 {
223 // ignore - it is thrown if receiver is not registered but it can not be 219 // ignore - it is thrown if receiver is not registered but it can not be
224 // true in normal conditions 220 // true in normal conditions
225 } 221 }
226 unbindService(proxyServiceConnection); 222 unbindService(proxyServiceConnection);
227 proxyService = null; 223 proxyService = null;
228 224
229 hideConfigurationMsg(); 225 hideConfigurationMsg();
230 } 226 }
231 227
232 @Override 228 @Override
233 protected void onStop() 229 protected void onStop()
234 { 230 {
235 super.onStop(); 231 super.onStop();
236 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this );
237 boolean enabled = prefs.getBoolean(getString(R.string.pref_enabled), false);
238 AdblockPlus application = AdblockPlus.getApplication(); 232 AdblockPlus application = AdblockPlus.getApplication();
239 application.stopInteractive(); 233 application.stopInteractive();
240 if (!enabled) 234 if (!application.isFilteringEnabled())
241 application.stopEngine(true); 235 application.stopEngine(true);
242 } 236 }
243 237
244 @Override 238 @Override
245 public boolean onCreateOptionsMenu(Menu menu) 239 public boolean onCreateOptionsMenu(Menu menu)
246 { 240 {
247 MenuInflater inflater = getSupportMenuInflater(); 241 MenuInflater inflater = getSupportMenuInflater();
248 inflater.inflate(R.menu.menu_preferences, menu); 242 inflater.inflate(R.menu.menu_preferences, menu);
249 return true; 243 return true;
250 } 244 }
(...skipping 12 matching lines...) Expand all
263 showDialog(ABOUT_DIALOG); 257 showDialog(ABOUT_DIALOG);
264 return true; 258 return true;
265 case R.id.menu_advanced: 259 case R.id.menu_advanced:
266 startActivity(new Intent(this, AdvancedPreferences.class)); 260 startActivity(new Intent(this, AdvancedPreferences.class));
267 return true; 261 return true;
268 default: 262 default:
269 return super.onOptionsItemSelected(item); 263 return super.onOptionsItemSelected(item);
270 } 264 }
271 } 265 }
272 266
273 private void setEnabled(boolean enabled) 267 private void setFilteringEnabled(boolean enabled)
274 { 268 {
275 SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferen ces(this).edit(); 269 SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferen ces(this).edit();
276 editor.putBoolean(getString(R.string.pref_enabled), enabled); 270 editor.putBoolean(getString(R.string.pref_enabled), enabled);
277 editor.commit(); 271 editor.commit();
278 ((CheckBoxPreference) findPreference(getString(R.string.pref_enabled))).setC hecked(enabled); 272 ((SwitchPreference) findPreference(getString(R.string.pref_enabled))).setChe cked(enabled);
273 AdblockPlus application = AdblockPlus.getApplication();
274 application.setFilteringEnabled(enabled);
275 }
276
277 private void setProxyEnabled(boolean enabled)
278 {
279 SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferen ces(this).edit();
280 editor.putBoolean(getString(R.string.pref_proxyenabled), enabled);
281 editor.commit();
282 AdblockPlus application = AdblockPlus.getApplication();
283 if (enabled && !application.isServiceRunning())
284 startService(new Intent(this, ProxyService.class));
279 } 285 }
280 286
281 /** 287 /**
282 * Checks if ProxyService is running.
283 *
284 * @return true if service is running
285 */
286 private boolean isServiceRunning()
287 {
288 ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVIC E);
289 // Actually it returns not only running services, so extra check is required
290 for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VAL UE))
291 {
292 if ("org.adblockplus.android.ProxyService".equals(service.service.getClass Name()) && service.pid > 0)
293 return true;
294 }
295 return false;
296 }
297
298 /**
299 * Copies file assets from installation package to filesystem. 288 * Copies file assets from installation package to filesystem.
300 */ 289 */
301 private void copyAssets() 290 private void copyAssets()
302 { 291 {
303 AssetManager assetManager = getAssets(); 292 AssetManager assetManager = getAssets();
304 String[] files = null; 293 String[] files = null;
305 try 294 try
306 { 295 {
307 files = assetManager.list("install"); 296 files = assetManager.list("install");
308 } 297 }
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
378 builder.setMessage(Html.fromHtml(message.toString())); 367 builder.setMessage(Html.fromHtml(message.toString()));
379 dialog = builder.create(); 368 dialog = builder.create();
380 break; 369 break;
381 } 370 }
382 return dialog; 371 return dialog;
383 } 372 }
384 373
385 @Override 374 @Override
386 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, Str ing key) 375 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, Str ing key)
387 { 376 {
377 AdblockPlus application = AdblockPlus.getApplication();
388 if (getString(R.string.pref_enabled).equals(key)) 378 if (getString(R.string.pref_enabled).equals(key))
389 { 379 {
390 boolean enabled = sharedPreferences.getBoolean(key, false); 380 boolean enabled = sharedPreferences.getBoolean(key, false);
391 boolean serviceRunning = isServiceRunning(); 381 boolean autoconfigured = sharedPreferences.getBoolean(getString(R.string.p ref_proxyautoconfigured), false);
392 if (enabled && !serviceRunning) 382 boolean serviceRunning = application.isServiceRunning();
393 startService(new Intent(this, ProxyService.class)); 383 application.setFilteringEnabled(enabled);
394 else if (!enabled && serviceRunning) 384 if (enabled)
385 {
386 // If user has enabled filtering, enable proxy as well
387 setProxyEnabled(true);
388 }
389 else if (serviceRunning && autoconfigured)
390 {
391 // If user disabled filtering disable proxy only if it was autoconfigure d
395 stopService(new Intent(this, ProxyService.class)); 392 stopService(new Intent(this, ProxyService.class));
393 }
396 } 394 }
397 else if (getString(R.string.pref_subscription).equals(key)) 395 else if (getString(R.string.pref_subscription).equals(key))
398 { 396 {
399 String current = sharedPreferences.getString(key, null); 397 String current = sharedPreferences.getString(key, null);
400 AdblockPlus application = AdblockPlus.getApplication();
401 Subscription subscription = application.getSubscription(current); 398 Subscription subscription = application.getSubscription(current);
402 application.setSubscription(subscription); 399 application.setSubscription(subscription);
403 } 400 }
404 else if (getString(R.string.pref_hideicon).equals(key)) 401 else if (getString(R.string.pref_hideicon).equals(key))
405 { 402 {
406 boolean hideIcon = sharedPreferences.getBoolean(key, false); 403 boolean hideIcon = sharedPreferences.getBoolean(key, false);
407 if (hideIcon) 404 if (hideIcon)
408 showDialog(HIDEICONWARNING_DIALOG); 405 showDialog(HIDEICONWARNING_DIALOG);
409 if (proxyService != null) 406 if (proxyService != null)
410 proxyService.setEmptyIcon(hideIcon); 407 proxyService.setEmptyIcon(hideIcon);
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
444 { 441 {
445 // Proxy is properly configured 442 // Proxy is properly configured
446 if (extra.getBoolean("configured")) 443 if (extra.getBoolean("configured"))
447 hideConfigurationMsg(); 444 hideConfigurationMsg();
448 else 445 else
449 showConfigurationMsg(getString(R.string.msg_configuration, extra.g etInt("port"))); 446 showConfigurationMsg(getString(R.string.msg_configuration, extra.g etInt("port")));
450 } 447 }
451 } 448 }
452 else 449 else
453 { 450 {
454 setEnabled(false); 451 setFilteringEnabled(false);
455 hideConfigurationMsg(); 452 hideConfigurationMsg();
456 } 453 }
457 } 454 }
458 if (action.equals(ProxyService.BROADCAST_PROXY_FAILED)) 455 if (action.equals(ProxyService.BROADCAST_PROXY_FAILED))
459 { 456 {
460 String msg = extra.getString("msg"); 457 String msg = extra.getString("msg");
461 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(); 458 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();
462 setEnabled(false); 459 setFilteringEnabled(false);
463 } 460 }
464 if (action.equals(AdblockPlus.BROADCAST_SUBSCRIPTION_STATUS)) 461 if (action.equals(AdblockPlus.BROADCAST_SUBSCRIPTION_STATUS))
465 { 462 {
466 final String text = extra.getString("text"); 463 final String text = extra.getString("text");
467 final long time = extra.getLong("time"); 464 final long time = extra.getLong("time");
468 runOnUiThread(new Runnable() 465 runOnUiThread(new Runnable()
469 { 466 {
470 public void run() 467 public void run()
471 { 468 {
472 setSubscriptionStatus(text, time); 469 setSubscriptionStatus(text, time);
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
542 showConfigurationMsg(getString(R.string.msg_configuration, proxyService. port)); 539 showConfigurationMsg(getString(R.string.msg_configuration, proxyService. port));
543 } 540 }
544 541
545 public void onServiceDisconnected(ComponentName className) 542 public void onServiceDisconnected(ComponentName className)
546 { 543 {
547 proxyService = null; 544 proxyService = null;
548 Log.d(TAG, "Proxy service disconnected"); 545 Log.d(TAG, "Proxy service disconnected");
549 } 546 }
550 }; 547 };
551 } 548 }
OLDNEW
« no previous file with comments | « src/org/adblockplus/android/AdvancedPreferences.java ('k') | src/org/adblockplus/android/ProxyService.java » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld