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