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

Delta Between Two Patch Sets: src/org/adblockplus/android/Preferences.java

Issue 8493083: ABP/Android UI (Closed)
Left Patch Set: Created Oct. 5, 2012, 9:42 a.m.
Right Patch Set: ABP/Android UI Created Oct. 12, 2012, 1:24 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
LEFTRIGHT
1 package org.adblockplus.android; 1 package org.adblockplus.android;
2 2
3 import java.io.IOException; 3 import java.io.IOException;
4 import java.io.InputStream; 4 import java.io.InputStream;
5 import java.io.OutputStream; 5 import java.io.OutputStream;
6 import java.util.Calendar; 6 import java.util.Calendar;
7 import java.util.Date; 7 import java.util.Date;
8 import java.util.List; 8 import java.util.List;
9 9
10 import android.app.ActivityManager; 10 import android.app.ActivityManager;
(...skipping 20 matching lines...) Expand all
31 import android.view.MenuItem; 31 import android.view.MenuItem;
32 import android.view.View; 32 import android.view.View;
33 import android.view.Window; 33 import android.view.Window;
34 import android.widget.TextView; 34 import android.widget.TextView;
35 35
36 /** 36 /**
37 * Main settings UI. 37 * Main settings UI.
38 */ 38 */
39 public class Preferences extends SummarizedPreferences 39 public class Preferences extends SummarizedPreferences
40 { 40 {
41 private final static String TAG = "Preferences"; 41 private static final String TAG = "Preferences";
Felix Dahlke 2012/10/09 14:27:29 As in AdvancedPreferences, "static final"?
Andrey Novikov 2012/10/12 13:19:14 Done.
42 42
43 private AboutDialog aboutDialog; 43 private AboutDialog aboutDialog;
44 private boolean showAbout = false; 44 private boolean showAbout = false;
45 private String configurationMsg; 45 private String configurationMsg;
46 private String subscriptionSummary; 46 private String subscriptionSummary;
47 47
48 @Override 48 @Override
49 public void onCreate(Bundle savedInstanceState) 49 public void onCreate(Bundle savedInstanceState)
50 { 50 {
51 requestWindowFeature(Window.FEATURE_NO_TITLE); 51 requestWindowFeature(Window.FEATURE_NO_TITLE);
(...skipping 15 matching lines...) Expand all
67 if (lastVersion != thisVersion) 67 if (lastVersion != thisVersion)
68 { 68 {
69 copyAssets(); 69 copyAssets();
70 SharedPreferences.Editor editor = prefs.edit(); 70 SharedPreferences.Editor editor = prefs.edit();
71 editor.putInt(getString(R.string.pref_version), thisVersion); 71 editor.putInt(getString(R.string.pref_version), thisVersion);
72 editor.commit(); 72 editor.commit();
73 } 73 }
74 } 74 }
75 catch (NameNotFoundException e) 75 catch (NameNotFoundException e)
76 { 76 {
77 copyAssets(); 77 copyAssets();
Felix Dahlke 2012/10/09 14:27:29 Why can this getPackageInfo fail while those in Ab
Andrey Novikov 2012/10/12 13:19:14 It can not. But I prefer to be reinsured in this c
78 } 78 }
79 } 79 }
80 80
81 @Override 81 @Override
82 protected void onStart() 82 protected void onStart()
83 { 83 {
84 super.onStart(); 84 super.onStart();
85 AdblockPlus application = AdblockPlus.getApplication(); 85 AdblockPlus application = AdblockPlus.getApplication();
86 application.startEngine(); 86 application.startEngine();
87 application.startInteractive(); 87 application.startInteractive();
88 } 88 }
89 89
90 @Override 90 @Override
91 public void onResume() 91 public void onResume()
Felix Dahlke 2012/10/09 14:27:29 Another long method, same comment as for AdvancedP
Andrey Novikov 2012/10/12 13:19:14 Same comment :) See code comments - they are for y
92 { 92 {
93 super.onResume(); 93 super.onResume();
94 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this ); 94 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this );
95 95
96 final AdblockPlus application = AdblockPlus.getApplication(); 96 final AdblockPlus application = AdblockPlus.getApplication();
97 97
98 // Construct subscription list 98 // Construct subscription list
99 RefreshableListPreference subscriptionList = (RefreshableListPreference) fin dPreference(getString(R.string.pref_subscription)); 99 RefreshableListPreference subscriptionList = (RefreshableListPreference) fin dPreference(getString(R.string.pref_subscription));
100 List<Subscription> subscriptions = application.getSubscriptions(); 100 List<Subscription> subscriptions = application.getSubscriptions();
101 String[] entries = new String[subscriptions.size()]; 101 String[] entries = new String[subscriptions.size()];
(...skipping 20 matching lines...) Expand all
122 if (offer != null) 122 if (offer != null)
123 { 123 {
124 subscriptionList.setValue(offer.url); 124 subscriptionList.setValue(offer.url);
125 application.setSubscription(offer); 125 application.setSubscription(offer);
126 new AlertDialog.Builder(this).setTitle(R.string.app_name).setMessage(Str ing.format(getString(R.string.msg_subscription_offer, offer.title))).setIcon(and roid.R.drawable.ic_dialog_info) 126 new AlertDialog.Builder(this).setTitle(R.string.app_name).setMessage(Str ing.format(getString(R.string.msg_subscription_offer, offer.title))).setIcon(and roid.R.drawable.ic_dialog_info)
127 .setPositiveButton(R.string.ok, null).create().show(); 127 .setPositiveButton(R.string.ok, null).create().show();
128 } 128 }
129 } 129 }
130 130
131 // Enable manual subscription refresh 131 // Enable manual subscription refresh
132 subscriptionList.setOnRefreshClickListener(new View.OnClickListener() { 132 subscriptionList.setOnRefreshClickListener(new View.OnClickListener()
Felix Dahlke 2012/10/09 14:27:29 Brace on its own line please.
Andrey Novikov 2012/10/12 13:19:14 Done.
133 {
133 @Override 134 @Override
134 public void onClick(View v) 135 public void onClick(View v)
135 { 136 {
136 application.refreshSubscription(); 137 application.refreshSubscription();
137 } 138 }
138 }); 139 });
139 140
140 // Set subscription status message 141 // Set subscription status message
141 if (subscriptionSummary != null) 142 if (subscriptionSummary != null)
142 subscriptionList.setSummary(subscriptionSummary); 143 subscriptionList.setSummary(subscriptionSummary);
143 else 144 else
144 setPrefSummary(subscriptionList); 145 setPrefSummary(subscriptionList);
145 146
146 // Time to start listening for events 147 // Time to start listening for events
147 registerReceiver(receiver, new IntentFilter(AdblockPlus.BROADCAST_SUBSCRIPTI ON_STATUS)); 148 registerReceiver(receiver, new IntentFilter(AdblockPlus.BROADCAST_SUBSCRIPTI ON_STATUS));
148 registerReceiver(receiver, new IntentFilter(AdblockPlus.BROADCAST_FILTER_MAT CHES)); 149 registerReceiver(receiver, new IntentFilter(AdblockPlus.BROADCAST_FILTER_MAT CHES));
149 registerReceiver(receiver, new IntentFilter(ProxyService.BROADCAST_STATE_CHA NGED)); 150 registerReceiver(receiver, new IntentFilter(ProxyService.BROADCAST_STATE_CHA NGED));
150 registerReceiver(receiver, new IntentFilter(ProxyService.BROADCAST_PROXY_FAI LED)); 151 registerReceiver(receiver, new IntentFilter(ProxyService.BROADCAST_PROXY_FAI LED));
151 152
152 final String url = current; 153 final String url = current;
153 154
154 // Initialize subscription verification 155 // Initialize subscription verification
155 (new Thread() { 156 (new Thread()
Felix Dahlke 2012/10/09 14:27:29 Brace on its own line please.
Andrey Novikov 2012/10/12 13:19:14 Done.
157 {
156 @Override 158 @Override
157 public void run() 159 public void run()
158 { 160 {
159 if (!application.verifySubscriptions()) 161 if (!application.verifySubscriptions())
160 { 162 {
161 Subscription subscription = application.getSubscription(url); 163 Subscription subscription = application.getSubscription(url);
162 application.setSubscription(subscription); 164 application.setSubscription(subscription);
163 } 165 }
164 } 166 }
165 }).start(); 167 }).start();
166 168
167 // Check if service is running and update UI accordingly 169 // Check if service is running and update UI accordingly
168 boolean enabled = prefs.getBoolean(getString(R.string.pref_enabled), false); 170 boolean enabled = prefs.getBoolean(getString(R.string.pref_enabled), false);
169 if (enabled && !isServiceRunning()) 171 if (enabled && !isServiceRunning())
170 { 172 {
171 setEnabled(false); 173 setEnabled(false);
172 enabled = false;
Felix Dahlke 2012/10/09 14:27:29 enabled isn't read past this point, Setting it to
Andrey Novikov 2012/10/12 13:19:14 Done.
173 } 174 }
174 // Run service if this is first application run 175 // Run service if this is first application run
175 else if (!enabled && firstRun) 176 else if (!enabled && firstRun)
176 { 177 {
177 startService(new Intent(this, ProxyService.class)); 178 startService(new Intent(this, ProxyService.class));
178 setEnabled(true); 179 setEnabled(true);
179 } 180 }
180 181
181 // Process screen rotation 182 // Process screen rotation
182 if (configurationMsg != null) 183 if (configurationMsg != null)
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
260 private void copyAssets() 261 private void copyAssets()
261 { 262 {
262 AssetManager assetManager = getAssets(); 263 AssetManager assetManager = getAssets();
263 String[] files = null; 264 String[] files = null;
264 try 265 try
265 { 266 {
266 files = assetManager.list("install"); 267 files = assetManager.list("install");
267 } 268 }
268 catch (IOException e) 269 catch (IOException e)
269 { 270 {
270 Log.e(TAG, e.getMessage()); 271 Log.e(TAG, "Failed to get assets list", e);
Felix Dahlke 2012/10/09 14:27:29 Why not pass e as the third parameter, like below?
Andrey Novikov 2012/10/12 13:19:14 Done.
271 } 272 }
272 for (int i = 0; i < files.length; i++) 273 for (int i = 0; i < files.length; i++)
273 { 274 {
274 InputStream in = null;
Felix Dahlke 2012/10/09 14:27:29 in and out can be declared in the try block, they
Andrey Novikov 2012/10/12 13:19:14 Done.
275 OutputStream out = null;
276 try 275 try
277 { 276 {
278 Log.d(TAG, "Copy: install/" + files[i]); 277 Log.d(TAG, "Copy: install/" + files[i]);
279 in = assetManager.open("install/" + files[i]); 278 InputStream in = assetManager.open("install/" + files[i]);
280 out = openFileOutput(files[i], MODE_PRIVATE); 279 OutputStream out = openFileOutput(files[i], MODE_PRIVATE);
281 byte[] buffer = new byte[1024]; 280 byte[] buffer = new byte[1024];
282 int read; 281 int read;
283 while ((read = in.read(buffer)) != -1) 282 while ((read = in.read(buffer)) != -1)
284 { 283 {
285 out.write(buffer, 0, read); 284 out.write(buffer, 0, read);
286 } 285 }
287 in.close(); 286 in.close();
288 in = null;
Felix Dahlke 2012/10/09 14:27:29 You don't need to set in/out to null, they aren't
Andrey Novikov 2012/10/12 13:19:14 Done.
289 out.flush(); 287 out.flush();
290 out.close(); 288 out.close();
291 out = null;
292
293 } 289 }
294 catch (Exception e) 290 catch (Exception e)
295 { 291 {
296 Log.e(TAG, "Asset copy error", e); 292 Log.e(TAG, "Asset copy error", e);
297 } 293 }
298 } 294 }
299 } 295 }
300 296
301 /** 297 /**
302 * Redirects user to configuration URL. 298 * Redirects user to configuration URL.
303 */ 299 */
304 public void onHelp(View view) 300 public void onHelp(View view)
305 { 301 {
306 Uri uri = Uri.parse(getString(R.string.configuring_url)); 302 Uri uri = Uri.parse(getString(R.string.configuring_url));
307 final Intent intent = new Intent(Intent.ACTION_VIEW, uri); 303 Intent intent = new Intent(Intent.ACTION_VIEW, uri);
Felix Dahlke 2012/10/09 14:27:29 Why make intent final?
Andrey Novikov 2012/10/12 13:19:14 No reason, just copied url opening code from sampl
308 startActivity(intent); 304 startActivity(intent);
309 } 305 }
310 306
311 /** 307 /**
312 * Shows about dialog. 308 * Shows about dialog.
313 */ 309 */
314 public void onAbout(View view) 310 public void onAbout(View view)
315 { 311 {
316 aboutDialog = new AboutDialog(this); 312 aboutDialog = new AboutDialog(this);
317 aboutDialog.setOnDismissListener(new OnDismissListener() { 313 aboutDialog.setOnDismissListener(new OnDismissListener()
314 {
318 315
319 @Override 316 @Override
320 public void onDismiss(DialogInterface dialog) 317 public void onDismiss(DialogInterface dialog)
321 { 318 {
322 showAbout = false; 319 showAbout = false;
Felix Dahlke 2012/10/09 14:27:29 Naming nit pick: This sounds imperative, how about
Andrey Novikov 2012/10/12 13:19:14 For me name is correct - it flags if have to show
323 aboutDialog = null; 320 aboutDialog = null;
324 } 321 }
325 }); 322 });
326 showAbout = true; 323 showAbout = true;
327 aboutDialog.show(); 324 aboutDialog.show();
328 } 325 }
329 326
330 @Override 327 @Override
331 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, Str ing key) 328 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, Str ing key)
332 { 329 {
333 if (getString(R.string.pref_enabled).equals(key)) 330 if (getString(R.string.pref_enabled).equals(key))
334 { 331 {
335 boolean enabled = sharedPreferences.getBoolean(key, false); 332 boolean enabled = sharedPreferences.getBoolean(key, false);
336 if (enabled && !isServiceRunning()) 333 boolean serviceRunning = isServiceRunning();
334 if (enabled && !serviceRunning)
337 startService(new Intent(this, ProxyService.class)); 335 startService(new Intent(this, ProxyService.class));
338 else if (!enabled && isServiceRunning()) 336 else if (!enabled && serviceRunning)
Felix Dahlke 2012/10/09 14:27:29 I'd feel safer is isServiceRunning() was called ju
Andrey Novikov 2012/10/12 13:19:14 Done.
339 stopService(new Intent(this, ProxyService.class)); 337 stopService(new Intent(this, ProxyService.class));
340 } 338 }
341 if (getString(R.string.pref_subscription).equals(key)) 339 if (getString(R.string.pref_subscription).equals(key))
342 { 340 {
343 String current = sharedPreferences.getString(key, null); 341 String current = sharedPreferences.getString(key, null);
344 AdblockPlus application = AdblockPlus.getApplication(); 342 AdblockPlus application = AdblockPlus.getApplication();
345 Subscription subscription = application.getSubscription(current); 343 Subscription subscription = application.getSubscription(current);
346 application.setSubscription(subscription); 344 application.setSubscription(subscription);
347 } 345 }
348 super.onSharedPreferenceChanged(sharedPreferences, key); 346 super.onSharedPreferenceChanged(sharedPreferences, key);
349 } 347 }
350 348
351 private void showConfigurationMsg(String message) 349 private void showConfigurationMsg(String message)
352 { 350 {
353 TextView msg = (TextView) findViewById(R.id.txt_configuration); 351 TextView msg = (TextView) findViewById(R.id.txt_configuration);
354 msg.setText(message); 352 msg.setText(message);
355 msg.setVisibility(View.VISIBLE); 353 msg.setVisibility(View.VISIBLE);
356 configurationMsg = message; 354 configurationMsg = message;
357 } 355 }
358 356
359 private void hideConfigurationMsg() 357 private void hideConfigurationMsg()
360 { 358 {
361 if (configurationMsg == null) 359 if (configurationMsg == null)
362 return; 360 return;
363 TextView msg = (TextView) findViewById(R.id.txt_configuration); 361 TextView msg = (TextView) findViewById(R.id.txt_configuration);
364 msg.setVisibility(View.GONE); 362 msg.setVisibility(View.GONE);
365 configurationMsg = null; 363 configurationMsg = null;
366 } 364 }
367 365
368 private BroadcastReceiver receiver = new BroadcastReceiver() { 366 private BroadcastReceiver receiver = new BroadcastReceiver()
Felix Dahlke 2012/10/09 14:27:29 Brace on a new line please.
Andrey Novikov 2012/10/12 13:19:14 Done.
367 {
369 @Override 368 @Override
370 public void onReceive(final Context context, Intent intent) 369 public void onReceive(Context context, Intent intent)
Felix Dahlke 2012/10/09 14:27:29 Why make context final?
Andrey Novikov 2012/10/12 13:19:14 It was previously used in a new thread, fixed.
371 { 370 {
372 String action = intent.getAction(); 371 String action = intent.getAction();
373 Bundle extra = intent.getExtras(); 372 Bundle extra = intent.getExtras();
374 if (action.equals(ProxyService.BROADCAST_STATE_CHANGED)) 373 if (action.equals(ProxyService.BROADCAST_STATE_CHANGED))
375 { 374 {
376 if (extra.getBoolean("enabled")) 375 if (extra.getBoolean("enabled"))
377 { 376 {
378 // If service is enabled in manual mode, show configuration message 377 // If service is enabled in manual mode, show configuration message
379 if (extra.getBoolean("manual")) 378 if (extra.getBoolean("manual"))
380 { 379 {
(...skipping 14 matching lines...) Expand all
395 if (action.equals(ProxyService.BROADCAST_PROXY_FAILED)) 394 if (action.equals(ProxyService.BROADCAST_PROXY_FAILED))
396 { 395 {
397 String msg = extra.getString("msg"); 396 String msg = extra.getString("msg");
398 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(); 397 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();
399 setEnabled(false); 398 setEnabled(false);
400 } 399 }
401 if (action.equals(AdblockPlus.BROADCAST_SUBSCRIPTION_STATUS)) 400 if (action.equals(AdblockPlus.BROADCAST_SUBSCRIPTION_STATUS))
402 { 401 {
403 final String text = extra.getString("text"); 402 final String text = extra.getString("text");
404 final long time = extra.getLong("time"); 403 final long time = extra.getLong("time");
405 runOnUiThread(new Runnable() { 404 runOnUiThread(new Runnable()
405 {
406 public void run() 406 public void run()
407 { 407 {
408 setSubscriptionStatus(text, time); 408 setSubscriptionStatus(text, time);
409 } 409 }
410 }); 410 });
411 } 411 }
412 } 412 }
413 }; 413 };
414 414
415 /** 415 /**
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
464 464
465 @Override 465 @Override
466 protected void onSaveInstanceState(Bundle outState) 466 protected void onSaveInstanceState(Bundle outState)
467 { 467 {
468 outState.putString("subscriptionSummary", subscriptionSummary); 468 outState.putString("subscriptionSummary", subscriptionSummary);
469 outState.putString("configurationMsg", configurationMsg); 469 outState.putString("configurationMsg", configurationMsg);
470 outState.putBoolean("showAbout", showAbout); 470 outState.putBoolean("showAbout", showAbout);
471 super.onSaveInstanceState(outState); 471 super.onSaveInstanceState(outState);
472 } 472 }
473 } 473 }
LEFTRIGHT

Powered by Google App Engine
This is Rietveld