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

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

Issue 9437197: ABP/Android Proxy switch (Closed)
Patch Set: Created Feb. 28, 2013, 1:20 p.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 the Adblock Plus, 2 * This file is part of the Adblock Plus,
3 * Copyright (C) 2006-2012 Eyeo GmbH 3 * Copyright (C) 2006-2012 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 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
43 import javax.xml.parsers.SAXParser; 43 import javax.xml.parsers.SAXParser;
44 import javax.xml.parsers.SAXParserFactory; 44 import javax.xml.parsers.SAXParserFactory;
45 45
46 import org.adblockplus.android.updater.AlarmReceiver; 46 import org.adblockplus.android.updater.AlarmReceiver;
47 import org.apache.commons.lang.StringEscapeUtils; 47 import org.apache.commons.lang.StringEscapeUtils;
48 import org.apache.commons.lang.StringUtils; 48 import org.apache.commons.lang.StringUtils;
49 import org.json.JSONException; 49 import org.json.JSONException;
50 import org.json.JSONObject; 50 import org.json.JSONObject;
51 import org.xml.sax.SAXException; 51 import org.xml.sax.SAXException;
52 52
53 import android.app.ActivityManager;
53 import android.app.AlarmManager; 54 import android.app.AlarmManager;
54 import android.app.Application; 55 import android.app.Application;
55 import android.app.PendingIntent; 56 import android.app.PendingIntent;
57 import android.app.ActivityManager.RunningServiceInfo;
56 import android.content.Context; 58 import android.content.Context;
57 import android.content.Intent; 59 import android.content.Intent;
58 import android.content.SharedPreferences; 60 import android.content.SharedPreferences;
59 import android.content.pm.PackageInfo; 61 import android.content.pm.PackageInfo;
60 import android.content.pm.PackageManager; 62 import android.content.pm.PackageManager;
61 import android.content.pm.PackageManager.NameNotFoundException; 63 import android.content.pm.PackageManager.NameNotFoundException;
62 import android.content.res.AssetManager; 64 import android.content.res.AssetManager;
63 import android.net.ConnectivityManager; 65 import android.net.ConnectivityManager;
64 import android.net.NetworkInfo; 66 import android.net.NetworkInfo;
65 import android.net.Uri; 67 import android.net.Uri;
66 import android.os.AsyncTask; 68 import android.os.AsyncTask;
67 import android.os.Build; 69 import android.os.Build;
68 import android.os.Bundle; 70 import android.os.Bundle;
69 import android.os.Handler; 71 import android.os.Handler;
70 import android.os.Message; 72 import android.os.Message;
71 import android.os.SystemClock; 73 import android.os.SystemClock;
72 import android.preference.PreferenceManager; 74 import android.preference.PreferenceManager;
73 import android.provider.Settings; 75 import android.provider.Settings;
74 import android.util.Log; 76 import android.util.Log;
75 import android.widget.Toast; 77 import android.widget.Toast;
76 78
77 public class AdblockPlus extends Application 79 public class AdblockPlus extends Application
78 { 80 {
79 private final static String TAG = "Application"; 81 private final static String TAG = "Application";
80 82
81 private final static int MSG_TOAST = 1; 83 private final static int MSG_TOAST = 1;
82 84
83 /** 85 /**
86 * Broadcasted when filtering is enabled or disabled.
87 */
88 public static final String BROADCAST_FILTERING_CHANGE = "org.adblockplus.andro id.filtering.status";
89 /**
84 * Broadcasted when subscription status changes. 90 * Broadcasted when subscription status changes.
85 */ 91 */
86 public final static String BROADCAST_SUBSCRIPTION_STATUS = "org.adblockplus.an droid.subscription.status"; 92 public final static String BROADCAST_SUBSCRIPTION_STATUS = "org.adblockplus.an droid.subscription.status";
87 /** 93 /**
88 * Broadcasted when filter match check is performed. 94 * Broadcasted when filter match check is performed.
89 */ 95 */
90 public final static String BROADCAST_FILTER_MATCHES = "org.adblockplus.android .filter.matches"; 96 public final static String BROADCAST_FILTER_MATCHES = "org.adblockplus.android .filter.matches";
91 97
92 private List<Subscription> subscriptions; 98 private List<Subscription> subscriptions;
93 99
94 private JSThread js; 100 private JSThread js;
95 101
96 /** 102 /**
97 * Indicates interactive mode (used to listen for subscription status 103 * Indicates interactive mode (used to listen for subscription status
98 * changes). 104 * changes).
99 */ 105 */
100 private boolean interactive = false; 106 private boolean interactive = false;
101 107
108 /**
109 * Indicates whether filtering is enabled or not.
110 */
111 private boolean filteringEnabled = false;
112
102 private static AdblockPlus instance; 113 private static AdblockPlus instance;
103 114
104 /** 115 /**
105 * Returns pointer to itself (singleton pattern). 116 * Returns pointer to itself (singleton pattern).
106 */ 117 */
107 public static AdblockPlus getApplication() 118 public static AdblockPlus getApplication()
108 { 119 {
109 return instance; 120 return instance;
110 } 121 }
111 122
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
198 public static boolean isWiFiConnected(Context context) 209 public static boolean isWiFiConnected(Context context)
199 { 210 {
200 ConnectivityManager connectivityManager = (ConnectivityManager) context.getS ystemService(Context.CONNECTIVITY_SERVICE); 211 ConnectivityManager connectivityManager = (ConnectivityManager) context.getS ystemService(Context.CONNECTIVITY_SERVICE);
201 NetworkInfo networkInfo = null; 212 NetworkInfo networkInfo = null;
202 if (connectivityManager != null) 213 if (connectivityManager != null)
203 { 214 {
204 networkInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_ WIFI); 215 networkInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_ WIFI);
205 } 216 }
206 return networkInfo == null ? false : networkInfo.isConnected(); 217 return networkInfo == null ? false : networkInfo.isConnected();
207 } 218 }
219
220 /**
221 * Checks if ProxyService is running.
222 *
223 * @return true if service is running
224 */
225 public boolean isServiceRunning()
226 {
227 ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVIC E);
228 // Actually it returns not only running services, so extra check is required
229 for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VAL UE))
230 {
231 if ("org.adblockplus.android.ProxyService".equals(service.service.getClass Name()) && service.pid > 0)
Felix Dahlke 2013/03/11 06:57:38 How about ProxyService.getClass().getCanonicalName
Andrey Novikov 2013/03/11 07:32:55 ProxyService.class.getCanonicalName(), done
232 return true;
233 }
234 return false;
235 }
208 236
209 /** 237 /**
210 * Checks if application can write to external storage. 238 * Checks if application can write to external storage.
211 */ 239 */
212 public boolean checkWriteExternalPermission() 240 public boolean checkWriteExternalPermission()
213 { 241 {
214 String permission = "android.permission.WRITE_EXTERNAL_STORAGE"; 242 String permission = "android.permission.WRITE_EXTERNAL_STORAGE";
215 int res = checkCallingOrSelfPermission(permission); 243 int res = checkCallingOrSelfPermission(permission);
216 return res == PackageManager.PERMISSION_GRANTED; 244 return res == PackageManager.PERMISSION_GRANTED;
217 } 245 }
(...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after
396 return false; 424 return false;
397 } 425 }
398 426
399 /** 427 /**
400 * Returns ElemHide selectors for domain. 428 * Returns ElemHide selectors for domain.
401 * 429 *
402 * @return ready to use HTML element with CSS selectors 430 * @return ready to use HTML element with CSS selectors
403 */ 431 */
404 public String getSelectorsForDomain(final String domain) 432 public String getSelectorsForDomain(final String domain)
405 { 433 {
434 if (!filteringEnabled)
435 return null;
436
406 Future<String> future = js.submit(new Callable<String>() 437 Future<String> future = js.submit(new Callable<String>()
407 { 438 {
408 @Override 439 @Override
409 public String call() throws Exception 440 public String call() throws Exception
410 { 441 {
411 String result = (String) js.evaluate("ElemHide.getSelectorsForDomain('" + domain + "')"); 442 String result = (String) js.evaluate("ElemHide.getSelectorsForDomain('" + domain + "')");
412 return result; 443 return result;
413 } 444 }
414 }); 445 });
415 try 446 try
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
465 * Request host 496 * Request host
466 * @param refHost 497 * @param refHost
467 * Request referrer header 498 * Request referrer header
468 * @param accept 499 * @param accept
469 * Request accept header 500 * Request accept header
470 * @return true if matched filter was found 501 * @return true if matched filter was found
471 * @throws Exception 502 * @throws Exception
472 */ 503 */
473 public boolean matches(String url, String query, String reqHost, String refHos t, String accept) throws Exception 504 public boolean matches(String url, String query, String reqHost, String refHos t, String accept) throws Exception
474 { 505 {
506 if (!filteringEnabled)
507 return false;
508
475 Callable<Boolean> callable = new MatchesCallable(url, query, reqHost, refHos t, accept); 509 Callable<Boolean> callable = new MatchesCallable(url, query, reqHost, refHos t, accept);
476 Future<Boolean> future = js.submit(callable); 510 Future<Boolean> future = js.submit(callable);
477 boolean matches = future.get().booleanValue(); 511 boolean matches = future.get().booleanValue();
478 sendBroadcast(new Intent(BROADCAST_FILTER_MATCHES).putExtra("url", url).putE xtra("matches", matches)); 512 sendBroadcast(new Intent(BROADCAST_FILTER_MATCHES).putExtra("url", url).putE xtra("matches", matches));
479 return matches; 513 return matches;
480 } 514 }
481 515
482 /** 516 /**
517 * Checks if filtering is enabled.
518 */
519 public boolean isFilteringEnabled()
520 {
521 return filteringEnabled;
522 }
523
524 /**
525 * Enables or disables filtering.
526 */
527 public void setFilteringEnabled(boolean enable)
528 {
529 filteringEnabled = enable;
530 sendBroadcast(new Intent(BROADCAST_FILTERING_CHANGE).putExtra("enabled", fil teringEnabled));
531 }
532
533 /**
483 * Notifies JS code that application entered interactive mode. 534 * Notifies JS code that application entered interactive mode.
484 */ 535 */
485 public void startInteractive() 536 public void startInteractive()
486 { 537 {
487 js.execute(new Runnable() 538 js.execute(new Runnable()
488 { 539 {
489 @Override 540 @Override
490 public void run() 541 public void run()
491 { 542 {
492 js.evaluate("startInteractive()"); 543 js.evaluate("startInteractive()");
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
559 /** 610 /**
560 * Stops JS engine. 611 * Stops JS engine.
561 * 612 *
562 * @param implicitly 613 * @param implicitly
563 * stop even in interactive mode 614 * stop even in interactive mode
564 */ 615 */
565 public void stopEngine(boolean implicitly) 616 public void stopEngine(boolean implicitly)
566 { 617 {
567 if ((implicitly || !interactive) && js != null) 618 if ((implicitly || !interactive) && js != null)
568 { 619 {
620 Log.i(TAG, "stopEngine");
569 js.stopEngine(); 621 js.stopEngine();
570 try 622 try
571 { 623 {
572 js.join(); 624 js.join();
573 } 625 }
574 catch (InterruptedException e) 626 catch (InterruptedException e)
575 { 627 {
576 Log.e(TAG, e.getMessage(), e); 628 Log.e(TAG, e.getMessage(), e);
577 } 629 }
630 Log.i(TAG, "Engine stopped");
578 js = null; 631 js = null;
579 } 632 }
580 } 633 }
581 634
582 /** 635 /**
583 * Sets Alarm to call updater after specified number of minutes or after one 636 * Sets Alarm to call updater after specified number of minutes or after one
584 * day if 637 * day if
585 * minutes are set to 0. 638 * minutes are set to 0.
586 * 639 *
587 * @param minutes 640 * @param minutes
(...skipping 269 matching lines...) Expand 10 before | Expand all | Expand 10 after
857 jsEngine.callback(callback, params); 910 jsEngine.callback(callback, params);
858 } 911 }
859 912
860 public final void stopEngine() 913 public final void stopEngine()
861 { 914 {
862 run = false; 915 run = false;
863 synchronized (queue) 916 synchronized (queue)
864 { 917 {
865 queue.notify(); 918 queue.notify();
866 } 919 }
920 System.gc();
Felix Dahlke 2013/03/11 06:57:38 I think we shouldn't do this, won't help us work a
Andrey Novikov 2013/03/11 07:32:55 Done.
867 } 921 }
868 922
869 public void execute(Runnable r) 923 public void execute(Runnable r)
870 { 924 {
871 synchronized (queue) 925 synchronized (queue)
872 { 926 {
873 queue.addLast(r); 927 queue.addLast(r);
874 queue.notify(); 928 queue.notify();
875 } 929 }
876 } 930 }
(...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after
1089 } 1143 }
1090 return result; 1144 return result;
1091 } 1145 }
1092 1146
1093 protected void onProgressUpdate(Integer... progress) 1147 protected void onProgressUpdate(Integer... progress)
1094 { 1148 {
1095 Log.d("HTTP", "Progress: " + progress[0].intValue()); 1149 Log.d("HTTP", "Progress: " + progress[0].intValue());
1096 } 1150 }
1097 } 1151 }
1098 } 1152 }
OLDNEW

Powered by Google App Engine
This is Rietveld