Left: | ||
Right: |
OLD | NEW |
---|---|
(Empty) | |
1 package org.adblockplus.android; | |
2 | |
3 import java.io.IOException; | |
4 import java.io.InputStream; | |
5 import java.io.OutputStream; | |
6 import java.util.Calendar; | |
7 import java.util.Date; | |
8 import java.util.List; | |
9 | |
10 import android.app.ActivityManager; | |
11 import android.app.ActivityManager.RunningServiceInfo; | |
12 import android.app.AlertDialog; | |
13 import android.content.BroadcastReceiver; | |
14 import android.content.Context; | |
15 import android.content.DialogInterface; | |
16 import android.content.DialogInterface.OnDismissListener; | |
17 import android.content.Intent; | |
18 import android.content.IntentFilter; | |
19 import android.content.SharedPreferences; | |
20 import android.content.pm.PackageManager.NameNotFoundException; | |
21 import android.content.res.AssetManager; | |
22 import android.net.Uri; | |
23 import android.os.Build; | |
24 import android.os.Bundle; | |
25 import android.preference.CheckBoxPreference; | |
26 import android.preference.ListPreference; | |
27 import android.preference.PreferenceManager; | |
28 import android.preference.PreferenceScreen; | |
29 import android.text.format.DateFormat; | |
30 import android.util.Log; | |
31 import android.view.Menu; | |
32 import android.view.MenuInflater; | |
33 import android.view.MenuItem; | |
34 import android.view.View; | |
35 import android.view.Window; | |
36 import android.widget.TextView; | |
37 | |
38 public class Preferences extends SummarizedPreferences | |
39 { | |
40 private final static String TAG = "Preferences"; | |
41 | |
42 private AboutDialog aboutDialog; | |
43 private boolean showAbout = false; | |
44 private String configurationMsg; | |
45 private String subscriptionSummary; | |
46 | |
47 @Override | |
48 public void onCreate(Bundle savedInstanceState) | |
49 { | |
50 requestWindowFeature(Window.FEATURE_NO_TITLE); | |
51 | |
52 super.onCreate(savedInstanceState); | |
53 | |
54 PreferenceManager.setDefaultValues(this, R.xml.preferences, fals e); | |
55 setContentView(R.layout.preferences); | |
56 addPreferencesFromResource(R.xml.preferences); | |
57 | |
58 SharedPreferences prefs = PreferenceManager.getDefaultSharedPref erences(this); | |
59 | |
60 int lastVersion = prefs.getInt(getString(R.string.pref_version), 0); | |
61 try | |
62 { | |
63 int thisVersion = getPackageManager().getPackageInfo(get PackageName(), 0).versionCode; | |
64 if (lastVersion != thisVersion) | |
65 { | |
66 copyAssets(); | |
67 SharedPreferences.Editor editor = prefs.edit(); | |
68 editor.putInt(getString(R.string.pref_version), thisVersion); | |
69 editor.commit(); | |
70 } | |
71 } | |
72 catch (NameNotFoundException e) | |
73 { | |
74 copyAssets(); | |
75 } | |
76 } | |
77 | |
78 @Override | |
79 protected void onStart() | |
80 { | |
81 super.onStart(); | |
82 AdblockPlus.getApplication().startEngine(); | |
Felix Dahlke
2012/09/18 15:32:49
Since you're using the result of getApplication()
| |
83 AdblockPlus.getApplication().startInteractive(); | |
84 } | |
85 | |
86 @Override | |
87 public void onResume() | |
Felix Dahlke
2012/09/18 15:32:49
I'd love it if you could move some of this method'
| |
88 { | |
89 super.onResume(); | |
90 SharedPreferences prefs = PreferenceManager.getDefaultSharedPref erences(this); | |
91 | |
92 final AdblockPlus application = AdblockPlus.getApplication(); | |
93 | |
94 RefreshableListPreference subscriptionList = (RefreshableListPre ference) findPreference(getString(R.string.pref_subscription)); | |
95 List<Subscription> subscriptions = application.getSubscriptions( ); | |
96 String[] entries = new String[subscriptions.size()]; | |
Felix Dahlke
2012/09/18 15:32:49
Like above, I'd appreciate it if you call subscrip
| |
97 String[] entryValues = new String[subscriptions.size()]; | |
98 String current = prefs.getString(getString(R.string.pref_subscri ption), (String) null); | |
99 int i = 0; | |
100 for (Subscription subscription : subscriptions) | |
101 { | |
102 entries[i] = subscription.title; | |
103 entryValues[i] = subscription.url; | |
104 i++; | |
105 } | |
106 subscriptionList.setEntries(entries); | |
107 subscriptionList.setEntryValues(entryValues); | |
108 | |
109 boolean firstRun = false; | |
110 | |
111 if (current == null) | |
112 { | |
113 firstRun = true; | |
114 Subscription offer = application.offerSubscription(); | |
115 current = offer.url; | |
116 if (offer != null) | |
117 { | |
118 subscriptionList.setValue(offer.url); | |
119 application.setSubscription(offer); | |
120 new AlertDialog.Builder(this) | |
121 .setTitle(R.string.app_name) | |
122 .setMessage(String.format(getString(R.st ring.msg_subscription_offer, offer.title))) | |
123 .setIcon(android.R.drawable.ic_dialog_in fo) | |
124 .setPositiveButton(R.string.ok, null) | |
125 .create() | |
126 .show(); | |
127 } | |
128 } | |
129 | |
130 subscriptionList.setOnRefreshClickListener(new View.OnClickListe ner() { | |
131 @Override | |
132 public void onClick(View v) | |
133 { | |
134 application.refreshSubscription(); | |
135 } | |
136 }); | |
137 | |
138 if (subscriptionSummary != null) | |
139 subscriptionList.setSummary(subscriptionSummary); | |
140 else | |
141 setPrefSummary(subscriptionList); | |
142 | |
143 registerReceiver(receiver, new IntentFilter(AdblockPlus.BROADCAS T_SUBSCRIPTION_STATUS)); | |
144 registerReceiver(receiver, new IntentFilter(AdblockPlus.BROADCAS T_FILTER_MATCHES)); | |
145 registerReceiver(receiver, new IntentFilter(ProxyService.BROADCA ST_STATE_CHANGED)); | |
146 registerReceiver(receiver, new IntentFilter(ProxyService.BROADCA ST_PROXY_FAILED)); | |
147 | |
148 final String url = current; | |
149 | |
150 (new Thread() { | |
151 @Override | |
152 public void run() | |
153 { | |
154 if (!application.verifySubscriptions()) | |
155 { | |
156 Subscription subscription = application. getSubscription(url); | |
157 application.setSubscription(subscription ); | |
158 } | |
159 } | |
160 }).start(); | |
161 | |
162 boolean enabled = prefs.getBoolean(getString(R.string.pref_enabl ed), false); | |
163 if (enabled && !isServiceRunning()) | |
164 { | |
165 setEnabled(false); | |
166 enabled = false; | |
167 } | |
168 else if (! enabled && firstRun) | |
Felix Dahlke
2012/09/18 15:32:49
I think "!enabled" read better than "! enabled". I
| |
169 { | |
170 startService(new Intent(this, ProxyService.class)); | |
171 setEnabled(true); | |
172 } | |
173 | |
174 if (configurationMsg != null) | |
175 showConfigurationMsg(configurationMsg); | |
176 | |
177 if (showAbout) | |
178 onAbout(findViewById(R.id.btn_about)); | |
179 } | |
180 | |
181 @Override | |
182 public void onPause() | |
183 { | |
184 super.onPause(); | |
185 unregisterReceiver(receiver); | |
186 } | |
187 | |
188 @Override | |
189 protected void onStop() | |
190 { | |
191 super.onStop(); | |
192 SharedPreferences prefs = PreferenceManager.getDefaultSharedPref erences(this); | |
193 boolean enabled = prefs.getBoolean(getString(R.string.pref_enabl ed), false); | |
194 AdblockPlus.getApplication().stopInteractive(); | |
195 if (!enabled) | |
196 AdblockPlus.getApplication().stopEngine(true); | |
Felix Dahlke
2012/09/18 15:32:49
Double getApplication() again, see above.
| |
197 | |
198 if (aboutDialog != null) | |
199 aboutDialog.dismiss(); | |
200 } | |
201 | |
202 @Override | |
203 public boolean onCreateOptionsMenu(Menu menu) | |
204 { | |
205 MenuInflater inflater = getMenuInflater(); | |
206 inflater.inflate(R.menu.menu_preferences, menu); | |
207 return true; | |
208 } | |
209 | |
210 @Override | |
211 public boolean onOptionsItemSelected(MenuItem item) | |
212 { | |
213 switch (item.getItemId()) | |
214 { | |
215 case R.id.menu_advanced: | |
216 Class<?> activity = Preferences.InnerPreferences .class; | |
217 startActivity(new Intent(Preferences.this, activ ity).putExtra("KEY", "preferences_advanced")); | |
218 return true; | |
219 default: | |
220 return super.onOptionsItemSelected(item); | |
221 } | |
222 } | |
223 | |
224 private void setEnabled(boolean enabled) | |
225 { | |
226 SharedPreferences.Editor editor = PreferenceManager.getDefaultSh aredPreferences(this).edit(); | |
227 editor.putBoolean(getString(R.string.pref_enabled), enabled); | |
228 editor.commit(); | |
229 ((CheckBoxPreference) findPreference(getString(R.string.pref_ena bled))).setChecked(enabled); | |
230 } | |
231 | |
232 private boolean isServiceRunning() | |
233 { | |
234 ActivityManager manager = (ActivityManager) getSystemService(ACT IVITY_SERVICE); | |
235 for (RunningServiceInfo service : manager.getRunningServices(Int eger.MAX_VALUE)) | |
236 { | |
237 if ("org.adblockplus.android.ProxyService".equals(servic e.service.getClassName())) | |
238 return true; | |
239 } | |
240 return false; | |
241 } | |
242 | |
243 private void copyAssets() | |
244 { | |
245 AssetManager assetManager = getAssets(); | |
246 String[] files = null; | |
247 try | |
248 { | |
249 files = assetManager.list("install"); | |
250 } | |
251 catch (IOException e) | |
252 { | |
253 Log.e(TAG, e.getMessage()); | |
254 } | |
255 for (int i = 0; i < files.length; i++) | |
256 { | |
257 InputStream in = null; | |
258 OutputStream out = null; | |
259 try | |
260 { | |
261 Log.d(TAG, "Copy: install/" + files[i]); | |
262 in = assetManager.open("install/" + files[i]); | |
263 out = openFileOutput(files[i], MODE_PRIVATE); | |
264 byte[] buffer = new byte[1024]; | |
265 int read; | |
266 while ((read = in.read(buffer)) != -1) | |
267 { | |
268 out.write(buffer, 0, read); | |
269 } | |
270 in.close(); | |
271 in = null; | |
Felix Dahlke
2012/09/18 15:32:49
You don't need to set in to null, it's going out o
| |
272 out.flush(); | |
273 out.close(); | |
274 out = null; | |
275 | |
276 } | |
277 catch (Exception e) | |
278 { | |
279 Log.e(TAG, "Asset copy error", e); | |
280 } | |
281 } | |
282 } | |
283 | |
284 public void onHelp(View view) | |
285 { | |
286 Uri uri = Uri.parse(getString(R.string.configuring_url)); | |
287 final Intent intent = new Intent(Intent.ACTION_VIEW, uri); | |
288 startActivity(intent); | |
289 } | |
290 | |
291 public void onAbout(View view) | |
292 { | |
293 aboutDialog = new AboutDialog(this); | |
294 aboutDialog.setOnDismissListener(new OnDismissListener() { | |
295 | |
296 @Override | |
297 public void onDismiss(DialogInterface dialog) | |
298 { | |
299 showAbout = false; | |
300 aboutDialog = null; | |
301 } | |
302 }); | |
303 showAbout = true; | |
304 aboutDialog.show(); | |
305 } | |
306 | |
307 @Override | |
308 public void onSharedPreferenceChanged(SharedPreferences sharedPreference s, String key) | |
309 { | |
310 if (getString(R.string.pref_enabled).equals(key)) | |
311 { | |
312 boolean enabled = sharedPreferences.getBoolean(key, fals e); | |
313 if (enabled && !isServiceRunning()) | |
314 startService(new Intent(this, ProxyService.class )); | |
315 else if (!enabled && isServiceRunning()) | |
316 stopService(new Intent(this, ProxyService.class) ); | |
317 } | |
318 if (getString(R.string.pref_subscription).equals(key)) | |
319 { | |
320 String current = sharedPreferences.getString(key, null); | |
321 AdblockPlus application = AdblockPlus.getApplication(); | |
322 Subscription subscription = application.getSubscription( current); | |
323 application.setSubscription(subscription); | |
324 } | |
325 if (getString(R.string.pref_refresh).equals(key)) | |
326 { | |
327 int refresh = Integer.valueOf(sharedPreferences.getStrin g(getString(R.string.pref_refresh), "0")); | |
328 findPreference(getString(R.string.pref_wifirefresh)).set Enabled(refresh > 0); | |
329 } | |
330 if (getString(R.string.pref_crashreport).equals(key)) | |
331 { | |
332 AdblockPlus application = AdblockPlus.getApplication(); | |
333 application.updateCrashReportStatus(); | |
334 } | |
335 super.onSharedPreferenceChanged(sharedPreferences, key); | |
336 } | |
337 | |
338 private void showConfigurationMsg(String message) | |
339 { | |
340 TextView msg = (TextView) findViewById(R.id.txt_configuration); | |
341 msg.setText(message); | |
342 msg.setVisibility(View.VISIBLE); | |
343 configurationMsg = message; | |
344 } | |
345 | |
346 private void hideConfigurationMsg() | |
347 { | |
348 if (configurationMsg == null) | |
349 return; | |
350 TextView msg = (TextView) findViewById(R.id.txt_configuration); | |
351 msg.setVisibility(View.GONE); | |
352 configurationMsg = null; | |
353 } | |
354 | |
355 private BroadcastReceiver receiver = new BroadcastReceiver() { | |
356 @Override | |
357 public void onReceive(final Context context, Intent intent) | |
Felix Dahlke
2012/09/18 15:32:49
Another somewhat large method, can you split it up
| |
358 { | |
359 String action = intent.getAction(); | |
360 Bundle extra = intent.getExtras(); | |
361 if (action.equals(ProxyService.BROADCAST_STATE_CHANGED)) | |
362 { | |
363 if (extra.getBoolean("enabled")) | |
364 { | |
365 if (extra.getBoolean("manual")) | |
366 { | |
367 showConfigurationMsg(getString(R .string.msg_configuration, extra.getInt("port"))); | |
Felix Dahlke
2012/09/18 15:32:49
This string is used in multiple places, how about
| |
368 } | |
369 } | |
370 else | |
371 { | |
372 setEnabled(false); | |
373 hideConfigurationMsg(); | |
374 } | |
375 } | |
376 if (action.equals(AdblockPlus.BROADCAST_FILTER_MATCHES)) | |
377 { | |
378 hideConfigurationMsg(); | |
379 } | |
380 if (action.equals(ProxyService.BROADCAST_PROXY_FAILED)) | |
381 { | |
382 String msg = extra.getString("msg"); | |
383 new AlertDialog.Builder(Preferences.this).setTit le(R.string.error).setMessage(msg).setIcon(android.R.drawable.ic_dialog_alert).s etPositiveButton(R.string.ok, null).create().show(); | |
384 setEnabled(false); | |
385 } | |
386 if (action.equals(AdblockPlus.BROADCAST_SUBSCRIPTION_STA TUS)) | |
387 { | |
388 final String text = extra.getString("text"); | |
389 final long time = extra.getLong("time"); | |
390 runOnUiThread(new Runnable() { | |
391 public void run() | |
392 { | |
393 ListPreference subscriptionList = (ListPreference) findPreference(getString(R.string.pref_subscription)); | |
394 CharSequence summary = subscript ionList.getEntry(); | |
395 StringBuilder builder = new Stri ngBuilder(); | |
396 if (summary != null) | |
397 { | |
398 builder.append(summary); | |
399 if (text != "") | |
400 { | |
401 builder.append(" ("); | |
402 int id = getReso urces().getIdentifier(text, "string", getPackageName()); | |
403 if (id > 0) | |
Felix Dahlke
2012/09/18 15:32:49
How about using ?: here? Would safe three lines. I
| |
404 builder. append(getString(id, text)); | |
405 else | |
406 builder. append(text); | |
407 if (time > 0) | |
408 { | |
409 builder. append(": "); | |
Felix Dahlke
2012/09/18 15:32:49
I think 8 nesting levels are too much. Can you ref
| |
410 Calendar calendar = Calendar.getInstance(); | |
411 calendar .setTimeInMillis(time); | |
412 Date dat e = calendar.getTime(); | |
413 builder. append(DateFormat.getDateFormat(context).format(date)); | |
414 builder. append(" "); | |
415 builder. append(DateFormat.getTimeFormat(context).format(date)); | |
416 } | |
417 builder.append(" )"); | |
418 } | |
419 subscriptionSummary = bu ilder.toString(); | |
420 subscriptionList.setSumm ary(subscriptionSummary); | |
421 } | |
422 } | |
423 }); | |
424 } | |
425 } | |
426 }; | |
427 | |
428 public static class InnerPreferences extends SummarizedPreferences | |
429 { | |
430 @Override | |
431 public void onCreate(Bundle savedInstanceState) | |
432 { | |
433 super.onCreate(savedInstanceState); | |
434 | |
435 String key = getIntent().getExtras().getString("KEY"); | |
436 int res = getResources().getIdentifier(key, "xml", getPa ckageName()); | |
437 | |
438 addPreferencesFromResource(res); | |
439 | |
440 PreferenceScreen screen = getPreferenceScreen(); | |
441 if (Build.VERSION.SDK_INT >= 12) // Honeycomb 3.1 | |
442 { | |
443 screen.removePreference(findPreference(getString (R.string.pref_proxy))); | |
444 } | |
445 if (getResources().getBoolean(R.bool.def_release)) | |
446 { | |
447 screen.removePreference(findPreference(getString (R.string.pref_support))); | |
448 } | |
449 } | |
450 | |
451 @Override | |
452 public void onResume() | |
453 { | |
454 super.onResume(); | |
455 SharedPreferences prefs = PreferenceManager.getDefaultSh aredPreferences(this); | |
456 int refresh = Integer.valueOf(prefs.getString(getString( R.string.pref_refresh), "0")); | |
457 findPreference(getString(R.string.pref_wifirefresh)).set Enabled(refresh > 0); | |
458 } | |
459 | |
460 @Override | |
461 public void onSharedPreferenceChanged(SharedPreferences sharedPr eferences, String key) | |
462 { | |
463 if (getString(R.string.pref_refresh).equals(key)) | |
464 { | |
465 int refresh = Integer.valueOf(sharedPreferences. getString(getString(R.string.pref_refresh), "0")); | |
466 findPreference(getString(R.string.pref_wifirefre sh)).setEnabled(refresh > 0); | |
467 } | |
468 if (getString(R.string.pref_crashreport).equals(key)) | |
469 { | |
470 AdblockPlus application = AdblockPlus.getApplica tion(); | |
471 application.updateCrashReportStatus(); | |
472 } | |
473 super.onSharedPreferenceChanged(sharedPreferences, key); | |
474 } | |
475 } | |
476 | |
477 @Override | |
478 protected void onRestoreInstanceState(Bundle state) | |
479 { | |
480 super.onRestoreInstanceState(state); | |
481 showAbout = state.getBoolean("showAbout"); | |
482 configurationMsg = state.getString("configurationMsg"); | |
483 subscriptionSummary = state.getString("subscriptionSummary"); | |
484 } | |
485 | |
486 @Override | |
487 protected void onSaveInstanceState(Bundle outState) | |
488 { | |
489 outState.putString("subscriptionSummary", subscriptionSummary); | |
490 outState.putString("configurationMsg", configurationMsg); | |
491 outState.putBoolean("showAbout", showAbout); | |
492 super.onSaveInstanceState(outState); | |
493 } | |
494 } | |
OLD | NEW |