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

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

Issue 9437197: ABP/Android Proxy switch (Closed)
Patch Set: ABP/Android Proxy switch Created March 11, 2013, 7:29 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 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 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
63 * Indicates that system supports native proxy configuration. 63 * Indicates that system supports native proxy configuration.
64 */ 64 */
65 public static final boolean NATIVE_PROXY_SUPPORTED = Build.VERSION.SDK_INT >= 12; // Honeycomb 3.1 65 public static final boolean NATIVE_PROXY_SUPPORTED = Build.VERSION.SDK_INT >= 12; // Honeycomb 3.1
66 66
67 static 67 static
68 { 68 {
69 RootTools.debugMode = false; 69 RootTools.debugMode = false;
70 } 70 }
71 71
72 private static final String TAG = "ProxyService"; 72 private static final String TAG = "ProxyService";
73 private static final boolean logRequests = false; 73 private static final boolean logRequests = true;
74 74
75 private static final int[] portVariants = new int[] {8080, 8888, 1111, 2222, 3 333, 4444, 5555, 6666, 7777, 9999, 26571, 0}; 75 // Do not use 8080 because it is a "dirty" port, Android uses it if something goes wrong
76 private static final int[] portVariants = new int[] {8888, 1111, 2222, 3333, 4 444, 5555, 6666, 7777, 9999, 26571, 0};
76 77
77 private final static int DEFAULT_TIMEOUT = 3000; 78 private final static int DEFAULT_TIMEOUT = 3000;
78 private final static int NO_TRAFFIC_TIMEOUT = 5 * 60 * 1000; // 5 minutes 79 private final static int NO_TRAFFIC_TIMEOUT = 5 * 60 * 1000; // 5 minutes
79 80
80 final static int ONGOING_NOTIFICATION_ID = R.string.app_name; 81 final static int ONGOING_NOTIFICATION_ID = R.string.app_name;
81 private static final long POSITION_RIGHT = Build.VERSION.SDK_INT >= Build.VERS ION_CODES.GINGERBREAD ? Long.MIN_VALUE : Long.MAX_VALUE; 82 private static final long POSITION_RIGHT = Build.VERSION.SDK_INT >= Build.VERS ION_CODES.GINGERBREAD ? Long.MIN_VALUE : Long.MAX_VALUE;
82 private final static int NOTRAFFIC_NOTIFICATION_ID = R.string.app_name + 3; 83 private final static int NOTRAFFIC_NOTIFICATION_ID = R.string.app_name + 3;
83 84
84 /** 85 /**
85 * Broadcasted when service starts or stops. 86 * Broadcasted when service starts or stops.
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after
196 { 197 {
197 // Try to set native proxy 198 // Try to set native proxy
198 nativeProxyAutoConfigured = ProxySettings.setConnectionProxy(getApplicatio nContext(), LOCALHOST, port, ""); 199 nativeProxyAutoConfigured = ProxySettings.setConnectionProxy(getApplicatio nContext(), LOCALHOST, port, "");
199 200
200 if (NATIVE_PROXY_SUPPORTED) 201 if (NATIVE_PROXY_SUPPORTED)
201 { 202 {
202 registerReceiver(connectionReceiver, new IntentFilter(ConnectivityManage r.CONNECTIVITY_ACTION)); 203 registerReceiver(connectionReceiver, new IntentFilter(ConnectivityManage r.CONNECTIVITY_ACTION));
203 registerReceiver(connectionReceiver, new IntentFilter(Proxy.PROXY_CHANGE _ACTION)); 204 registerReceiver(connectionReceiver, new IntentFilter(Proxy.PROXY_CHANGE _ACTION));
204 } 205 }
205 } 206 }
206 207
207 // Start engine 208 // Save current native proxy situation. The service is always started on the first run so
208 AdblockPlus.getApplication().startEngine(); 209 // we will always have a correct value from the box
210 SharedPreferences.Editor editor = prefs.edit();
211 editor.putBoolean(getString(R.string.pref_proxyautoconfigured), transparent || nativeProxyAutoConfigured);
212 editor.commit();
209 213
210 registerReceiver(proxyReceiver, new IntentFilter(ProxyService.BROADCAST_PROX Y_FAILED)); 214 registerReceiver(proxyReceiver, new IntentFilter(ProxyService.BROADCAST_PROX Y_FAILED));
211 registerReceiver(matchesReceiver, new IntentFilter(AdblockPlus.BROADCAST_FIL TER_MATCHES)); 215 registerReceiver(filterReceiver, new IntentFilter(AdblockPlus.BROADCAST_FILT ERING_CHANGE));
216 registerReceiver(filterReceiver, new IntentFilter(AdblockPlus.BROADCAST_FILT ER_MATCHES));
212 217
213 // Start proxy 218 // Start proxy
214 if (proxy == null) 219 if (proxy == null)
215 { 220 {
216 ServerSocket listen = null; 221 ServerSocket listen = null;
217 String msg = null; 222 String msg = null;
218 for (int p : portVariants) 223 for (int p : portVariants)
219 { 224 {
220 try 225 try
221 { 226 {
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
284 return START_STICKY; 289 return START_STICKY;
285 } 290 }
286 291
287 @Override 292 @Override
288 public void onDestroy() 293 public void onDestroy()
289 { 294 {
290 super.onDestroy(); 295 super.onDestroy();
291 296
292 stopNoTrafficCheck(); 297 stopNoTrafficCheck();
293 298
294 unregisterReceiver(matchesReceiver); 299 unregisterReceiver(filterReceiver);
295 unregisterReceiver(proxyReceiver); 300 unregisterReceiver(proxyReceiver);
296 301
297 // Stop IP redirecting 302 // Stop IP redirecting
298 if (transparent) 303 if (transparent)
299 { 304 {
300 new Thread() 305 new Thread()
301 { 306 {
302 @Override 307 @Override
303 public void run() 308 public void run()
304 { 309 {
(...skipping 17 matching lines...) Expand all
322 { 327 {
323 clearConnectionProxy(); 328 clearConnectionProxy();
324 } 329 }
325 330
326 sendBroadcast(new Intent(BROADCAST_STATE_CHANGED).putExtra("enabled", false) ); 331 sendBroadcast(new Intent(BROADCAST_STATE_CHANGED).putExtra("enabled", false) );
327 332
328 // Stop proxy server 333 // Stop proxy server
329 if (proxy != null) 334 if (proxy != null)
330 proxy.close(); 335 proxy.close();
331 336
337 // TODO Do we have to check current state?
332 // Stop engine if not in interactive mode 338 // Stop engine if not in interactive mode
333 AdblockPlus.getApplication().stopEngine(false); 339 AdblockPlus.getApplication().stopEngine(false);
334 340
335 // Release service lock 341 // Release service lock
336 stopForeground(true); 342 stopForeground(true);
337 343
338 Log.i(TAG, "Service stopped"); 344 Log.i(TAG, "Service stopped");
339 } 345 }
340 346
341 /** 347 /**
(...skipping 293 matching lines...) Expand 10 before | Expand all | Expand 10 after
635 * Stops no traffic check and resets notification message. 641 * Stops no traffic check and resets notification message.
636 */ 642 */
637 private void stopNoTrafficCheck() 643 private void stopNoTrafficCheck()
638 { 644 {
639 if (notrafficHandler != null) 645 if (notrafficHandler != null)
640 { 646 {
641 notrafficHandler.removeCallbacks(noTraffic); 647 notrafficHandler.removeCallbacks(noTraffic);
642 sendStateChangedBroadcast(); 648 sendStateChangedBroadcast();
643 NotificationManager notificationManager = (NotificationManager) getSystemS ervice(NOTIFICATION_SERVICE); 649 NotificationManager notificationManager = (NotificationManager) getSystemS ervice(NOTIFICATION_SERVICE);
644 notificationManager.notify(ONGOING_NOTIFICATION_ID, getNotification()); 650 notificationManager.notify(ONGOING_NOTIFICATION_ID, getNotification());
651 notificationManager.cancel(NOTRAFFIC_NOTIFICATION_ID);
645 } 652 }
646 notrafficHandler = null; 653 notrafficHandler = null;
647 } 654 }
648 655
649 @SuppressLint("NewApi") 656 @SuppressLint("NewApi")
650 private Notification getNotification() 657 private Notification getNotification()
651 { 658 {
659 boolean filtering = AdblockPlus.getApplication().isFilteringEnabled();
660
652 int msgId = R.string.notif_waiting; 661 int msgId = R.string.notif_waiting;
653 if (nativeProxyAutoConfigured || proxyManualyConfigured) 662 if (nativeProxyAutoConfigured || proxyManualyConfigured)
654 msgId = R.string.notif_wifi; 663 msgId = filtering ? R.string.notif_wifi : R.string.notif_wifi_nofiltering;
655 if (transparent) 664 if (transparent)
656 msgId = R.string.notif_all; 665 msgId = R.string.notif_all;
657 666
658 NotificationCompat.Builder builder = new NotificationCompat.Builder(this); 667 NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
659 if (hideIcon && msgId != R.string.notif_waiting) 668 if (hideIcon && msgId != R.string.notif_waiting)
660 { 669 {
661 builder.setWhen(POSITION_RIGHT); 670 builder.setWhen(POSITION_RIGHT);
662 builder.setSmallIcon(R.drawable.transparent); 671 builder.setSmallIcon(R.drawable.transparent);
663 //builder.setContent(new RemoteViews(getPackageName(), R.layout.notif_hidd en)); 672 //builder.setContent(new RemoteViews(getPackageName(), R.layout.notif_hidd en));
664 } 673 }
665 else 674 else
666 { 675 {
667 builder.setWhen(0); 676 builder.setWhen(0);
668 builder.setSmallIcon(R.drawable.ic_stat_blocking); 677 builder.setSmallIcon(R.drawable.ic_stat_blocking);
669 } 678 }
670 PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent( this, Preferences.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_A CTIVITY_NEW_TASK), 0); 679 PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent( this, Preferences.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_A CTIVITY_NEW_TASK), 0);
671 builder.setContentIntent(contentIntent); 680 builder.setContentIntent(contentIntent);
672 builder.setContentTitle(getText(R.string.app_name)); 681 builder.setContentTitle(getText(R.string.app_name));
673 builder.setContentText(getText(msgId)); 682 builder.setContentText(getText(msgId));
683 builder.setOngoing(true);
674 684
675 Notification notification = builder.getNotification(); 685 Notification notification = builder.getNotification();
676 return notification; 686 return notification;
677 } 687 }
678 688
679 public void setEmptyIcon(boolean hide) 689 public void setEmptyIcon(boolean hide)
680 { 690 {
681 hideIcon = hide; 691 hideIcon = hide;
682 NotificationManager notificationManager = (NotificationManager) getSystemSer vice(NOTIFICATION_SERVICE); 692 NotificationManager notificationManager = (NotificationManager) getSystemSer vice(NOTIFICATION_SERVICE);
683 notificationManager.notify(ONGOING_NOTIFICATION_ID, getNotification()); 693 notificationManager.notify(ONGOING_NOTIFICATION_ID, getNotification());
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
733 builder.setContentTitle(getText(R.string.app_name)); 743 builder.setContentTitle(getText(R.string.app_name));
734 builder.setContentText(getText(R.string.notif_notraffic)); 744 builder.setContentText(getText(R.string.notif_notraffic));
735 NotificationManager notificationManager = (NotificationManager) getSystemS ervice(NOTIFICATION_SERVICE); 745 NotificationManager notificationManager = (NotificationManager) getSystemS ervice(NOTIFICATION_SERVICE);
736 notificationManager.notify(NOTRAFFIC_NOTIFICATION_ID, builder.getNotificat ion()); 746 notificationManager.notify(NOTRAFFIC_NOTIFICATION_ID, builder.getNotificat ion());
737 } 747 }
738 }; 748 };
739 749
740 /** 750 /**
741 * Stops no traffic check if traffic is detected by proxy service. 751 * Stops no traffic check if traffic is detected by proxy service.
742 */ 752 */
743 private BroadcastReceiver matchesReceiver = new BroadcastReceiver() 753 private BroadcastReceiver filterReceiver = new BroadcastReceiver()
744 { 754 {
745 @Override 755 @Override
746 public void onReceive(final Context context, Intent intent) 756 public void onReceive(final Context context, Intent intent)
747 { 757 {
758 if (intent.getAction().equals(AdblockPlus.BROADCAST_FILTERING_CHANGE))
759 {
760 NotificationManager notificationManager = (NotificationManager) getSyste mService(NOTIFICATION_SERVICE);
761 notificationManager.notify(ONGOING_NOTIFICATION_ID, getNotification());
762 }
748 if (intent.getAction().equals(AdblockPlus.BROADCAST_FILTER_MATCHES)) 763 if (intent.getAction().equals(AdblockPlus.BROADCAST_FILTER_MATCHES))
749 { 764 {
750 proxyManualyConfigured = true; 765 proxyManualyConfigured = true;
751 stopNoTrafficCheck(); 766 stopNoTrafficCheck();
752 } 767 }
753 } 768 }
754 }; 769 };
755 770
756 /** 771 /**
757 * Stops service if proxy fails. 772 * Stops service if proxy fails.
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after
858 @Override 873 @Override
859 public void log(int level, Object obj, String message) 874 public void log(int level, Object obj, String message)
860 { 875 {
861 if (level <= logLevel) 876 if (level <= logLevel)
862 { 877 {
863 Log.println(7 - level, obj != null ? obj.toString() : TAG, message); 878 Log.println(7 - level, obj != null ? obj.toString() : TAG, message);
864 } 879 }
865 } 880 }
866 } 881 }
867 } 882 }
OLDNEW
« no previous file with comments | « src/org/adblockplus/android/Preferences.java ('k') | src/org/adblockplus/android/ProxySettings.java » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld