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