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