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

Side by Side Diff: src/org/adblockplus/android/Preferences.java

Issue 11172036: ABP/Android libadblockplus integration (Closed)
Patch Set: Created July 22, 2013, 8:52 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
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-2013 Eyeo GmbH 3 * Copyright (C) 2006-2013 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
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 17
18 package org.adblockplus.android; 18 package org.adblockplus.android;
19 19
20 import java.io.IOException; 20 import java.io.IOException;
21 import java.io.InputStream; 21 import java.io.InputStream;
22 import java.io.OutputStream; 22 import java.io.OutputStream;
23 import java.util.Calendar; 23 import java.util.Calendar;
24 import java.util.Date; 24 import java.util.Date;
25 import java.util.List;
26 25
27 import org.jraf.android.backport.switchwidget.SwitchPreference; 26 import org.jraf.android.backport.switchwidget.SwitchPreference;
28 27
29 import android.app.AlertDialog; 28 import android.app.AlertDialog;
30 import android.app.Dialog; 29 import android.app.Dialog;
31 import android.content.BroadcastReceiver; 30 import android.content.BroadcastReceiver;
32 import android.content.ComponentName; 31 import android.content.ComponentName;
33 import android.content.Context; 32 import android.content.Context;
34 import android.content.DialogInterface; 33 import android.content.DialogInterface;
35 import android.content.Intent; 34 import android.content.Intent;
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
93 copyAssets(); 92 copyAssets();
94 SharedPreferences.Editor editor = prefs.edit(); 93 SharedPreferences.Editor editor = prefs.edit();
95 editor.putInt(getString(R.string.pref_version), thisVersion); 94 editor.putInt(getString(R.string.pref_version), thisVersion);
96 editor.commit(); 95 editor.commit();
97 } 96 }
98 } 97 }
99 catch (NameNotFoundException e) 98 catch (NameNotFoundException e)
100 { 99 {
101 copyAssets(); 100 copyAssets();
102 } 101 }
102 }
103 103
104 @Override
105 protected void onStart()
106 {
107 super.onStart();
104 AdblockPlus application = AdblockPlus.getApplication(); 108 AdblockPlus application = AdblockPlus.getApplication();
109 application.startEngine();
105 110
106 // Initialize subscription list 111 // Initialize subscription list
107 subscriptionList = (RefreshableListPreference) findPreference(getString(R.st ring.pref_subscription)); 112 subscriptionList = (RefreshableListPreference) findPreference(getString(R.st ring.pref_subscription));
108 List<Subscription> subscriptions = application.getSubscriptions(); 113 Subscription[] subscriptions = application.getRecommendedSubscriptions();
109 String[] entries = new String[subscriptions.size()]; 114 String[] entries = new String[subscriptions.length];
110 String[] entryValues = new String[subscriptions.size()]; 115 String[] entryValues = new String[subscriptions.length];
111 int i = 0; 116 int i = 0;
112 for (Subscription subscription : subscriptions) 117 for (Subscription subscription : subscriptions)
113 { 118 {
114 entries[i] = subscription.title; 119 entries[i] = subscription.title;
115 entryValues[i] = subscription.url; 120 entryValues[i] = subscription.url;
116 i++; 121 i++;
117 } 122 }
118 subscriptionList.setEntries(entries); 123 subscriptionList.setEntries(entries);
119 subscriptionList.setEntryValues(entryValues); 124 subscriptionList.setEntryValues(entryValues);
Felix Dahlke 2013/09/13 14:40:39 Nit: Trailing whitespace.
120 } 125 }
121 126
122 @Override 127 @Override
123 protected void onStart()
124 {
125 super.onStart();
126 AdblockPlus application = AdblockPlus.getApplication();
127 application.startEngine();
128 application.startInteractive();
129 }
130
131 @Override
132 public void onResume() 128 public void onResume()
133 { 129 {
134 super.onResume(); 130 super.onResume();
135 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this ); 131 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this );
136 132
137 final AdblockPlus application = AdblockPlus.getApplication(); 133 final AdblockPlus application = AdblockPlus.getApplication();
138 134
135 Subscription current = null;
136 Subscription[] subscriptions = application.getListedSubscriptions();
137 if (subscriptions.length > 0)
138 {
139 current = subscriptions[0];
140 }
141
139 boolean firstRun = false; 142 boolean firstRun = false;
Wladimir Palant 2013/09/12 11:31:14 I don't think you need the firstRun variable, just
140 143 if (application.isFirstRun())
141 // Get current subscription
142 String current = prefs.getString(getString(R.string.pref_subscription), (Str ing) null);
143
144 // If there is no current subscription autoselect one
145 if (current == null)
146 { 144 {
147 firstRun = true; 145 firstRun = true;
148 Subscription offer = application.offerSubscription(); 146
149 current = offer.url; 147 if (current != null)
150 if (offer != null)
151 { 148 {
152 subscriptionList.setValue(offer.url); 149 new AlertDialog.Builder(this).setTitle(R.string.app_name).setMessage(Str ing.format(getString(R.string.msg_subscription_offer, current.title))).setIcon(a ndroid.R.drawable.ic_dialog_info)
153 application.setSubscription(offer);
154 new AlertDialog.Builder(this).setTitle(R.string.app_name).setMessage(Str ing.format(getString(R.string.msg_subscription_offer, offer.title))).setIcon(and roid.R.drawable.ic_dialog_info)
155 .setPositiveButton(R.string.ok, null).create().show(); 150 .setPositiveButton(R.string.ok, null).create().show();
156 } 151 }
157 } 152 }
158 153
159 // Enable manual subscription refresh 154 // Enable manual subscription refresh
160 subscriptionList.setOnRefreshClickListener(new View.OnClickListener() 155 subscriptionList.setOnRefreshClickListener(new View.OnClickListener()
161 { 156 {
162 @Override 157 @Override
163 public void onClick(View v) 158 public void onClick(View v)
164 { 159 {
165 application.refreshSubscription(); 160 application.refreshSubscriptions();
166 } 161 }
167 }); 162 });
168 163
169 // Set subscription status message 164 // Set subscription status message
170 if (subscriptionSummary != null) 165 if (subscriptionSummary != null)
171 subscriptionList.setSummary(subscriptionSummary); 166 subscriptionList.setSummary(subscriptionSummary);
172 else 167 else
173 setPrefSummary(subscriptionList); 168 setPrefSummary(subscriptionList);
174 169
175 // Time to start listening for events 170 // Time to start listening for events
176 registerReceiver(receiver, new IntentFilter(AdblockPlus.BROADCAST_SUBSCRIPTI ON_STATUS)); 171 registerReceiver(receiver, new IntentFilter(AdblockPlus.BROADCAST_SUBSCRIPTI ON_STATUS));
177 registerReceiver(receiver, new IntentFilter(ProxyService.BROADCAST_STATE_CHA NGED)); 172 registerReceiver(receiver, new IntentFilter(ProxyService.BROADCAST_STATE_CHA NGED));
178 registerReceiver(receiver, new IntentFilter(ProxyService.BROADCAST_PROXY_FAI LED)); 173 registerReceiver(receiver, new IntentFilter(ProxyService.BROADCAST_PROXY_FAI LED));
179 174
180 final String url = current; 175 // Update service and UI state according to user settings
181 176 if (current != null)
182 // Initialize subscription verification
183 (new Thread()
184 { 177 {
185 @Override 178 subscriptionList.setValue(current.url);
186 public void run() 179 application.actualizeSubscriptionStatus(current.url);
187 { 180 }
188 if (!application.verifySubscriptions())
189 {
190 Subscription subscription = application.getSubscription(url);
191 application.setSubscription(subscription);
192 }
193 }
194 }).start();
195
196 // Update service and UI state according to user settings
197 boolean enabled = prefs.getBoolean(getString(R.string.pref_enabled), false); 181 boolean enabled = prefs.getBoolean(getString(R.string.pref_enabled), false);
198 boolean proxyenabled = prefs.getBoolean(getString(R.string.pref_proxyenabled ), true); 182 boolean proxyenabled = prefs.getBoolean(getString(R.string.pref_proxyenabled ), true);
199 boolean autoconfigured = prefs.getBoolean(getString(R.string.pref_proxyautoc onfigured), false); 183 boolean autoconfigured = prefs.getBoolean(getString(R.string.pref_proxyautoc onfigured), false);
184
185 // This is weird but UI does not update on back button (when returning from advanced preferences)
186 ((SwitchPreference) findPreference(getString(R.string.pref_enabled))).setChe cked(enabled);
187
200 if (enabled || firstRun) 188 if (enabled || firstRun)
201 setFilteringEnabled(true); 189 setFilteringEnabled(true);
202 if (enabled || firstRun || (proxyenabled && !autoconfigured)) 190 if (enabled || firstRun || (proxyenabled && !autoconfigured))
203 setProxyEnabled(true); 191 setProxyEnabled(true);
204 192
205 bindService(new Intent(this, ProxyService.class), proxyServiceConnection, 0) ; 193 bindService(new Intent(this, ProxyService.class), proxyServiceConnection, 0) ;
206 } 194 }
207 195
208 @Override 196 @Override
209 public void onPause() 197 public void onPause()
(...skipping 12 matching lines...) Expand all
222 proxyService = null; 210 proxyService = null;
223 211
224 hideConfigurationMsg(); 212 hideConfigurationMsg();
225 } 213 }
226 214
227 @Override 215 @Override
228 protected void onStop() 216 protected void onStop()
229 { 217 {
230 super.onStop(); 218 super.onStop();
231 AdblockPlus application = AdblockPlus.getApplication(); 219 AdblockPlus application = AdblockPlus.getApplication();
232 application.stopInteractive();
233 if (!application.isFilteringEnabled()) 220 if (!application.isFilteringEnabled())
234 application.stopEngine(true); 221 application.stopEngine();
235 } 222 }
236 223
237 @Override 224 @Override
238 public boolean onCreateOptionsMenu(Menu menu) 225 public boolean onCreateOptionsMenu(Menu menu)
239 { 226 {
240 MenuInflater inflater = getSupportMenuInflater(); 227 MenuInflater inflater = getSupportMenuInflater();
241 inflater.inflate(R.menu.menu_preferences, menu); 228 inflater.inflate(R.menu.menu_preferences, menu);
242 return true; 229 return true;
243 } 230 }
244 231
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after
386 setProxyEnabled(true); 373 setProxyEnabled(true);
387 } 374 }
388 else if (serviceRunning && autoconfigured) 375 else if (serviceRunning && autoconfigured)
389 { 376 {
390 // If user disabled filtering disable proxy only if it was autoconfigure d 377 // If user disabled filtering disable proxy only if it was autoconfigure d
391 stopService(new Intent(this, ProxyService.class)); 378 stopService(new Intent(this, ProxyService.class));
392 } 379 }
393 } 380 }
394 else if (getString(R.string.pref_subscription).equals(key)) 381 else if (getString(R.string.pref_subscription).equals(key))
395 { 382 {
396 String current = sharedPreferences.getString(key, null); 383 String url = sharedPreferences.getString(key, null);
397 Subscription subscription = application.getSubscription(current); 384 if (url != null)
398 application.setSubscription(subscription); 385 application.setSubscription(url);
399 } 386 }
400 else if (getString(R.string.pref_hideicon).equals(key)) 387 else if (getString(R.string.pref_hideicon).equals(key))
401 { 388 {
402 boolean hideIcon = sharedPreferences.getBoolean(key, false); 389 boolean hideIcon = sharedPreferences.getBoolean(key, false);
403 if (hideIcon) 390 if (hideIcon)
404 showDialog(HIDEICONWARNING_DIALOG); 391 showDialog(HIDEICONWARNING_DIALOG);
405 if (proxyService != null) 392 if (proxyService != null)
406 proxyService.setEmptyIcon(hideIcon); 393 proxyService.setEmptyIcon(hideIcon);
407 } 394 }
408 super.onSharedPreferenceChanged(sharedPreferences, key); 395 super.onSharedPreferenceChanged(sharedPreferences, key);
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
450 } 437 }
451 } 438 }
452 if (action.equals(ProxyService.BROADCAST_PROXY_FAILED)) 439 if (action.equals(ProxyService.BROADCAST_PROXY_FAILED))
453 { 440 {
454 String msg = extra.getString("msg"); 441 String msg = extra.getString("msg");
455 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(); 442 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();
456 setFilteringEnabled(false); 443 setFilteringEnabled(false);
457 } 444 }
458 if (action.equals(AdblockPlus.BROADCAST_SUBSCRIPTION_STATUS)) 445 if (action.equals(AdblockPlus.BROADCAST_SUBSCRIPTION_STATUS))
459 { 446 {
460 final String text = extra.getString("text"); 447 //TODO Should check if url matches active subscription
448 final String text = extra.getString("status");
461 final long time = extra.getLong("time"); 449 final long time = extra.getLong("time");
462 runOnUiThread(new Runnable() 450 runOnUiThread(new Runnable()
463 { 451 {
464 public void run() 452 public void run()
465 { 453 {
466 setSubscriptionStatus(text, time); 454 setSubscriptionStatus(text, time);
467 } 455 }
468 }); 456 });
469 } 457 }
470 } 458 }
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
536 showConfigurationMsg(getString(R.string.msg_configuration)); 524 showConfigurationMsg(getString(R.string.msg_configuration));
537 } 525 }
538 526
539 public void onServiceDisconnected(ComponentName className) 527 public void onServiceDisconnected(ComponentName className)
540 { 528 {
541 proxyService = null; 529 proxyService = null;
542 Log.d(TAG, "Proxy service disconnected"); 530 Log.d(TAG, "Proxy service disconnected");
543 } 531 }
544 }; 532 };
545 } 533 }
OLDNEW

Powered by Google App Engine
This is Rietveld