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

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

Issue 9811019: ABP/Android Remember lart port used (Closed)
Patch Set: Created March 13, 2013, 10:13 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
« no previous file with comments | « res/values/sysstrings.xml ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
67 67
68 static 68 static
69 { 69 {
70 RootTools.debugMode = false; 70 RootTools.debugMode = false;
71 } 71 }
72 72
73 private static final String TAG = "ProxyService"; 73 private static final String TAG = "ProxyService";
74 private static final boolean logRequests = true; 74 private static final boolean logRequests = true;
75 75
76 // Do not use 8080 because it is a "dirty" port, Android uses it if something goes wrong 76 // Do not use 8080 because it is a "dirty" port, Android uses it if something goes wrong
77 private static final int[] portVariants = new int[] {2020, 3030, 4040, 5050, 6 060, 7070, 9090, 1234, 12345, 4321, 0}; 77 // First element is reserved for previously used port
78 private static final int[] portVariants = new int[] {-1, 2020, 3030, 4040, 505 0, 6060, 7070, 9090, 1234, 12345, 4321, 0};
78 79
79 private final static int DEFAULT_TIMEOUT = 3000; 80 private final static int DEFAULT_TIMEOUT = 3000;
80 private final static int NO_TRAFFIC_TIMEOUT = 5 * 60 * 1000; // 5 minutes 81 private final static int NO_TRAFFIC_TIMEOUT = 5 * 60 * 1000; // 5 minutes
81 82
82 final static int ONGOING_NOTIFICATION_ID = R.string.app_name; 83 final static int ONGOING_NOTIFICATION_ID = R.string.app_name;
83 private static final long POSITION_RIGHT = Build.VERSION.SDK_INT >= Build.VERS ION_CODES.GINGERBREAD ? Long.MIN_VALUE : Long.MAX_VALUE; 84 private static final long POSITION_RIGHT = Build.VERSION.SDK_INT >= Build.VERS ION_CODES.GINGERBREAD ? Long.MIN_VALUE : Long.MAX_VALUE;
84 private final static int NOTRAFFIC_NOTIFICATION_ID = R.string.app_name + 3; 85 private final static int NOTRAFFIC_NOTIFICATION_ID = R.string.app_name + 3;
85 86
86 /** 87 /**
87 * Broadcasted when service starts or stops. 88 * Broadcasted when service starts or stops.
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after
205 { 206 {
206 // Try to set native proxy 207 // Try to set native proxy
207 nativeProxyAutoConfigured = ProxySettings.setConnectionProxy(getApplicatio nContext(), LOCALHOST, port, ""); 208 nativeProxyAutoConfigured = ProxySettings.setConnectionProxy(getApplicatio nContext(), LOCALHOST, port, "");
208 209
209 if (NATIVE_PROXY_SUPPORTED) 210 if (NATIVE_PROXY_SUPPORTED)
210 { 211 {
211 registerReceiver(connectionReceiver, new IntentFilter(ConnectivityManage r.CONNECTIVITY_ACTION)); 212 registerReceiver(connectionReceiver, new IntentFilter(ConnectivityManage r.CONNECTIVITY_ACTION));
212 registerReceiver(connectionReceiver, new IntentFilter(Proxy.PROXY_CHANGE _ACTION)); 213 registerReceiver(connectionReceiver, new IntentFilter(Proxy.PROXY_CHANGE _ACTION));
213 } 214 }
214 } 215 }
215 216
216 // Save current native proxy situation. The service is always started on the first run so 217 // Save current native proxy situation. The service is always started on the first run so
217 // we will always have a correct value from the box 218 // we will always have a correct value from the box
218 SharedPreferences.Editor editor = prefs.edit(); 219 SharedPreferences.Editor editor = prefs.edit();
219 editor.putBoolean(getString(R.string.pref_proxyautoconfigured), transparent || nativeProxyAutoConfigured); 220 editor.putBoolean(getString(R.string.pref_proxyautoconfigured), transparent || nativeProxyAutoConfigured);
220 editor.commit(); 221 editor.commit();
221 222
222 registerReceiver(proxyReceiver, new IntentFilter(ProxyService.BROADCAST_PROX Y_FAILED)); 223 registerReceiver(proxyReceiver, new IntentFilter(ProxyService.BROADCAST_PROX Y_FAILED));
223 registerReceiver(filterReceiver, new IntentFilter(AdblockPlus.BROADCAST_FILT ERING_CHANGE)); 224 registerReceiver(filterReceiver, new IntentFilter(AdblockPlus.BROADCAST_FILT ERING_CHANGE));
224 registerReceiver(filterReceiver, new IntentFilter(AdblockPlus.BROADCAST_FILT ER_MATCHES)); 225 registerReceiver(filterReceiver, new IntentFilter(AdblockPlus.BROADCAST_FILT ER_MATCHES));
225 226
226 // Start proxy 227 // Start proxy
227 if (proxy == null) 228 if (proxy == null)
228 { 229 {
230 // Select available port and bind to it, use previously selected port by d efault
231 portVariants[0] = prefs.getInt(getString(R.string.pref_lastport), -1);
Felix Dahlke 2013/03/13 13:16:49 I think it would be more elegant here to convert t
Andrey Novikov 2013/03/13 13:22:38 This was the first what I thought about but constr
Felix Dahlke 2013/03/13 13:24:57 But it's less error-prone (this would fall apart i
229 ServerSocket listen = null; 232 ServerSocket listen = null;
230 String msg = null; 233 String msg = null;
231 for (int p : portVariants) 234 for (int p : portVariants)
232 { 235 {
236 if (p < 0)
237 continue;
233 try 238 try
234 { 239 {
235 listen = new ServerSocket(p, 1024); 240 listen = new ServerSocket(p, 1024);
236 port = p; 241 port = p;
237 break; 242 break;
238 } 243 }
239 catch (IOException e) 244 catch (IOException e)
240 { 245 {
241 Log.e(TAG, null, e); 246 Log.e(TAG, null, e);
242 msg = e.getMessage(); 247 msg = e.getMessage();
243 } 248 }
244 } 249 }
245 if (listen == null) 250 if (listen == null)
246 { 251 {
247 sendBroadcast(new Intent(BROADCAST_PROXY_FAILED).putExtra("msg", msg)); 252 sendBroadcast(new Intent(BROADCAST_PROXY_FAILED).putExtra("msg", msg));
248 return; 253 return;
249 } 254 }
250 255
256 // Save selected port
257 editor.putInt(getString(R.string.pref_lastport), port);
258 editor.commit();
259
260 // Initialize proxy
251 proxyConfiguration.put("handler", "main"); 261 proxyConfiguration.put("handler", "main");
252 proxyConfiguration.put("main.prefix", ""); 262 proxyConfiguration.put("main.prefix", "");
253 proxyConfiguration.put("main.class", "sunlabs.brazil.server.ChainHandler") ; 263 proxyConfiguration.put("main.class", "sunlabs.brazil.server.ChainHandler") ;
254 if (transparent) 264 if (transparent)
255 { 265 {
256 proxyConfiguration.put("main.handlers", "urlmodifier adblock"); 266 proxyConfiguration.put("main.handlers", "urlmodifier adblock");
257 proxyConfiguration.put("urlmodifier.class", "org.adblockplus.brazil.Tran sparentProxyHandler"); 267 proxyConfiguration.put("urlmodifier.class", "org.adblockplus.brazil.Tran sparentProxyHandler");
258 } 268 }
259 else 269 else
260 { 270 {
261 proxyConfiguration.put("main.handlers", "https adblock"); 271 proxyConfiguration.put("main.handlers", "https adblock");
262 proxyConfiguration.put("https.class", "org.adblockplus.brazil.SSLConnect ionHandler"); 272 proxyConfiguration.put("https.class", "org.adblockplus.brazil.SSLConnect ionHandler");
263 } 273 }
264 proxyConfiguration.put("adblock.class", "org.adblockplus.brazil.RequestHan dler"); 274 proxyConfiguration.put("adblock.class", "org.adblockplus.brazil.RequestHan dler");
265 if (logRequests) 275 if (logRequests)
266 proxyConfiguration.put("adblock.proxylog", "yes"); 276 proxyConfiguration.put("adblock.proxylog", "yes");
267 277
268 configureUserProxy(proxyConfiguration, proxyHost, proxyPort, proxyExcl, pr oxyUser, proxyPass); 278 configureUserProxy(proxyConfiguration, proxyHost, proxyPort, proxyExcl, pr oxyUser, proxyPass);
269 279
270 proxy = new ProxyServer(); 280 proxy = new ProxyServer();
271 proxy.logLevel = Server.LOG_DIAGNOSTIC; 281 proxy.logLevel = Server.LOG_DIAGNOSTIC;
272 proxy.setup(listen, proxyConfiguration.getProperty("handler"), proxyConfig uration); 282 proxy.setup(listen, proxyConfiguration.getProperty("handler"), proxyConfig uration);
273 proxy.start(); 283 proxy.start();
274 } 284 }
275 285
276 if (transparent) 286 if (transparent)
277 { 287 {
278 // Redirect traffic via iptables 288 // Redirect traffic via iptables
279 try 289 try
280 { 290 {
281 StringBuffer cmd = new StringBuffer(); 291 StringBuffer cmd = new StringBuffer();
282 cmd.append(iptables); 292 cmd.append(iptables);
283 cmd.append(IPTABLES_ADD_HTTP.replace("{{PORT}}", String.valueOf(port))); 293 cmd.append(IPTABLES_ADD_HTTP.replace("{{PORT}}", String.valueOf(port)));
284 String rules = cmd.toString(); 294 String rules = cmd.toString();
285 RootTools.sendShell(rules, DEFAULT_TIMEOUT); 295 RootTools.sendShell(rules, DEFAULT_TIMEOUT);
(...skipping 232 matching lines...) Expand 10 before | Expand all | Expand 10 after
518 return nativeProxyAutoConfigured; 528 return nativeProxyAutoConfigured;
519 } 529 }
520 530
521 /** 531 /**
522 * Checks if user has to set proxy settings manually 532 * Checks if user has to set proxy settings manually
523 */ 533 */
524 public boolean isManual() 534 public boolean isManual()
525 { 535 {
526 return !transparent && !nativeProxyAutoConfigured; 536 return !transparent && !nativeProxyAutoConfigured;
527 } 537 }
528 538
529 /** 539 /**
530 * Checks whether traffic check is pending 540 * Checks whether traffic check is pending
531 */ 541 */
532 public boolean noTraffic() 542 public boolean noTraffic()
533 { 543 {
534 return notrafficHandler != null; 544 return notrafficHandler != null;
535 } 545 }
536 546
537 /** 547 /**
538 * Checks if specified host is local. 548 * Checks if specified host is local.
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after
711 else 721 else
712 { 722 {
713 builder.setWhen(0); 723 builder.setWhen(0);
714 builder.setSmallIcon(R.drawable.ic_stat_blocking); 724 builder.setSmallIcon(R.drawable.ic_stat_blocking);
715 } 725 }
716 PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent( this, Preferences.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_A CTIVITY_NEW_TASK), 0); 726 PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent( this, Preferences.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_A CTIVITY_NEW_TASK), 0);
717 builder.setContentIntent(contentIntent); 727 builder.setContentIntent(contentIntent);
718 builder.setContentTitle(getText(R.string.app_name)); 728 builder.setContentTitle(getText(R.string.app_name));
719 builder.setContentText(getString(msgId, port)); 729 builder.setContentText(getString(msgId, port));
720 builder.setOngoing(true); 730 builder.setOngoing(true);
721 731
722 Notification notification = builder.getNotification(); 732 Notification notification = builder.getNotification();
723 return notification; 733 return notification;
724 } 734 }
725 735
726 public void setEmptyIcon(boolean hide) 736 public void setEmptyIcon(boolean hide)
727 { 737 {
728 hideIcon = hide; 738 hideIcon = hide;
729 NotificationManager notificationManager = (NotificationManager) getSystemSer vice(NOTIFICATION_SERVICE); 739 NotificationManager notificationManager = (NotificationManager) getSystemSer vice(NOTIFICATION_SERVICE);
730 notificationManager.notify(ONGOING_NOTIFICATION_ID, getNotification()); 740 notificationManager.notify(ONGOING_NOTIFICATION_ID, getNotification());
731 } 741 }
(...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after
910 @Override 920 @Override
911 public void log(int level, Object obj, String message) 921 public void log(int level, Object obj, String message)
912 { 922 {
913 if (level <= logLevel) 923 if (level <= logLevel)
914 { 924 {
915 Log.println(7 - level, obj != null ? obj.toString() : TAG, message); 925 Log.println(7 - level, obj != null ? obj.toString() : TAG, message);
916 } 926 }
917 } 927 }
918 } 928 }
919 } 929 }
OLDNEW
« no previous file with comments | « res/values/sysstrings.xml ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld