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 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
52 import android.os.StrictMode; | 52 import android.os.StrictMode; |
53 import android.preference.PreferenceManager; | 53 import android.preference.PreferenceManager; |
54 import android.support.v4.app.NotificationCompat; | 54 import android.support.v4.app.NotificationCompat; |
55 import android.util.Log; | 55 import android.util.Log; |
56 | 56 |
57 import com.stericson.RootTools.RootTools; | 57 import com.stericson.RootTools.RootTools; |
58 import com.stericson.RootTools.RootToolsException; | 58 import com.stericson.RootTools.RootToolsException; |
59 | 59 |
60 public class ProxyService extends Service implements OnSharedPreferenceChangeLis
tener | 60 public class ProxyService extends Service implements OnSharedPreferenceChangeLis
tener |
61 { | 61 { |
62 private static final String LOCALHOST = "127.0.0.1"; | |
63 /** | |
64 * Indicates that system supports native proxy configuration. | |
65 */ | |
66 public static final boolean NATIVE_PROXY_SUPPORTED = Build.VERSION.SDK_INT >=
12; // Honeycomb 3.1 | |
67 | |
68 static | 62 static |
69 { | 63 { |
70 RootTools.debugMode = false; | 64 RootTools.debugMode = false; |
71 } | 65 } |
72 | 66 |
73 private static final String TAG = "ProxyService"; | 67 private static final String TAG = Utils.getTag(ProxyService.class); |
| 68 |
| 69 private static final String LOCALHOST = "127.0.0.1"; |
| 70 /** |
| 71 * Indicates that system supports native proxy configuration. |
| 72 */ |
74 private static final boolean logRequests = false; | 73 private static final boolean logRequests = false; |
75 | 74 |
76 // Do not use 8080 because it is a "dirty" port, Android uses it if something
goes wrong | 75 public static final boolean NATIVE_PROXY_SUPPORTED = Build.VERSION.SDK_INT >=
12; // Honeycomb |
77 // First element is reserved for previously used port | 76 private static final int NOTRAFFIC_NOTIFICATION_ID = R.string.app_name + 3; |
78 private static final int[] portVariants = new int[] {-1, 2020, 3030, 4040, 505
0, 6060, 7070, 9090, 1234, 12345, 4321, 0}; | 77 static final int ONGOING_NOTIFICATION_ID = R.string.app_name; |
79 | 78 |
80 private final static int DEFAULT_TIMEOUT = 3000; | 79 private static final long POSITION_RIGHT = Build.VERSION.SDK_INT >= Build.VERS
ION_CODES.GINGERBREAD ? Long.MIN_VALUE : Long.MAX_VALUE; |
81 private final static int NO_TRAFFIC_TIMEOUT = 5 * 60 * 1000; // 5 minutes | |
82 | 80 |
83 final static int ONGOING_NOTIFICATION_ID = R.string.app_name; | 81 private static final int DEFAULT_TIMEOUT = 3000; |
84 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 int NO_TRAFFIC_TIMEOUT = 5 * 60 * 1000; // 5 minutes |
85 private final static int NOTRAFFIC_NOTIFICATION_ID = R.string.app_name + 3; | 83 |
| 84 // Do not use 8080 because it is a "dirty" port, Android uses it if something |
| 85 // goes wrong |
| 86 // first element is reserved for previously used port |
| 87 private static final int[] portVariants = new int[] { -1, 2020, 3030, 4040, 50
50, 6060, 7070, 9090, 1234, 12345, 4321, 0 }; |
86 | 88 |
87 /** | 89 /** |
88 * Broadcasted when service starts or stops. | 90 * Broadcasted when service starts or stops. |
89 */ | 91 */ |
90 public final static String BROADCAST_STATE_CHANGED = "org.adblockplus.android.
service.state"; | 92 public static final String BROADCAST_STATE_CHANGED = "org.adblockplus.android.
service.state"; |
91 /** | 93 /** |
92 * Broadcasted if proxy fails to start. | 94 * Broadcasted if proxy fails to start. |
93 */ | 95 */ |
94 public final static String BROADCAST_PROXY_FAILED = "org.adblockplus.android.p
roxy.failure"; | 96 public static final String BROADCAST_PROXY_FAILED = "org.adblockplus.android.p
roxy.failure"; |
95 | 97 |
96 private final static String IPTABLES_RETURN = " -t nat -m owner --uid-owner {{
UID}} -A OUTPUT -p tcp -j RETURN\n"; | 98 private static final String IPTABLES_RETURN = " -t nat -m owner --uid-owner {{
UID}} -A OUTPUT -p tcp -j RETURN\n"; |
97 private final static String IPTABLES_ADD_HTTP = " -t nat -A OUTPUT -p tcp --dp
ort 80 -j REDIRECT --to {{PORT}}\n"; | 99 private static final String IPTABLES_ADD_HTTP = " -t nat -A OUTPUT -p tcp --dp
ort 80 -j REDIRECT --to {{PORT}}\n"; |
98 | 100 |
99 boolean hideIcon; | 101 boolean hideIcon; |
100 private Handler notrafficHandler; | 102 private Handler notrafficHandler; |
101 | 103 |
102 protected ProxyServer proxy = null; | 104 protected ProxyServer proxy = null; |
103 protected int port; | 105 protected int port; |
104 private Properties proxyConfiguration = new Properties(); | 106 private final Properties proxyConfiguration = new Properties(); |
105 | 107 |
106 /** | 108 /** |
107 * Indicates that service is working with root privileges. | 109 * Indicates that service is working with root privileges. |
108 */ | 110 */ |
109 private boolean transparent = false; | 111 private boolean transparent = false; |
110 /** | 112 /** |
111 * Indicates that service has autoconfigured Android proxy settings (version 3
.1+). | 113 * Indicates that service has autoconfigured Android proxy settings (version 3
.1+). |
112 */ | 114 */ |
113 private boolean nativeProxyAutoConfigured = false; | 115 private boolean nativeProxyAutoConfigured = false; |
114 /** | 116 /** |
115 * Indicates that Android proxy settings are correctly configured (version 4.1
.2+ 4.2.2+). | 117 * Indicates that Android proxy settings are correctly configured (version 4.1
.2+ 4.2.2+). |
116 */ | 118 */ |
117 private boolean proxyManualyConfigured = false; | 119 private boolean proxyManualyConfigured = false; |
118 | 120 |
119 private String iptables = null; | 121 private String iptables = null; |
120 | 122 |
121 @SuppressLint("NewApi") | 123 @SuppressLint("NewApi") |
122 @Override | 124 @Override |
123 public void onCreate() | 125 public void onCreate() |
124 { | 126 { |
125 super.onCreate(); | 127 super.onCreate(); |
126 | 128 |
127 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) | 129 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) |
128 { | 130 { |
129 // Proxy is running in separate thread, it's just some resolution request
during initialization. | 131 // Proxy is running in separate thread, it's just some resolution request |
130 // Not worth spawning a separate thread for this. | 132 // during initialization. |
131 StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().per
mitNetwork().build(); | 133 // Not worth spawning a separate thread for |
| 134 final StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder
().permitNetwork().build(); |
132 StrictMode.setThreadPolicy(policy); | 135 StrictMode.setThreadPolicy(policy); |
133 } | 136 } |
134 | 137 |
135 // Get port for local proxy | 138 // Get port for local proxy |
136 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this
); | 139 final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreference
s(this); |
137 Resources resources = getResources(); | 140 final Resources resources = getResources(); |
138 | 141 |
139 // Try to read user proxy settings | 142 // Try to read user proxy settings |
140 String proxyHost = null; | 143 String proxyHost = null; |
141 String proxyPort = null; | 144 String proxyPort = null; |
142 String proxyExcl = null; | 145 String proxyExcl = null; |
143 String proxyUser = null; | 146 String proxyUser = null; |
144 String proxyPass = null; | 147 String proxyPass = null; |
145 | 148 |
146 if (NATIVE_PROXY_SUPPORTED) | 149 if (NATIVE_PROXY_SUPPORTED) |
147 { | 150 { |
148 // Read system settings | 151 // Read system settings |
149 proxyHost = System.getProperty("http.proxyHost"); | 152 proxyHost = System.getProperty("http.proxyHost"); |
150 proxyPort = System.getProperty("http.proxyPort"); | 153 proxyPort = System.getProperty("http.proxyPort"); |
151 proxyExcl = System.getProperty("http.nonProxyHosts"); | 154 proxyExcl = System.getProperty("http.nonProxyHosts"); |
152 | 155 |
153 Log.d(TAG, "PRX: " + proxyHost + ":" + proxyPort + "(" + proxyExcl + ")"); | 156 Log.d(TAG, "PRX: " + proxyHost + ":" + proxyPort + "(" + proxyExcl + ")"); |
154 // not used but left for future reference | 157 // not used but left for future reference |
155 String[] px = ProxySettings.getUserProxy(getApplicationContext()); | 158 final String[] px = ProxySettings.getUserProxy(getApplicationContext()); |
156 if (px != null) | 159 if (px != null) |
157 Log.d(TAG, "PRX: " + px[0] + ":" + px[1] + "(" + px[2] + ")"); | 160 Log.d(TAG, "PRX: " + px[0] + ":" + px[1] + "(" + px[2] + ")"); |
158 } | 161 } |
159 else | 162 else |
160 { | 163 { |
161 // Read application settings | 164 // Read application settings |
162 proxyHost = prefs.getString(getString(R.string.pref_proxyhost), null); | 165 proxyHost = prefs.getString(getString(R.string.pref_proxyhost), null); |
163 proxyPort = prefs.getString(getString(R.string.pref_proxyport), null); | 166 proxyPort = prefs.getString(getString(R.string.pref_proxyport), null); |
164 proxyUser = prefs.getString(getString(R.string.pref_proxyuser), null); | 167 proxyUser = prefs.getString(getString(R.string.pref_proxyuser), null); |
165 proxyPass = prefs.getString(getString(R.string.pref_proxypass), null); | 168 proxyPass = prefs.getString(getString(R.string.pref_proxypass), null); |
166 } | 169 } |
167 | 170 |
168 // Check for root privileges and try to install transparent proxy | 171 // Check for root privileges and try to install transparent proxy |
169 if (RootTools.isAccessGiven()) | 172 if (RootTools.isAccessGiven()) |
170 { | 173 { |
171 try | 174 try |
172 { | 175 { |
173 initIptables(); | 176 initIptables(); |
174 | 177 |
175 StringBuffer cmd = new StringBuffer(); | 178 final StringBuffer cmd = new StringBuffer(); |
176 int uid = getPackageManager().getPackageInfo(getPackageName(), 0).applic
ationInfo.uid; | 179 final int uid = getPackageManager().getPackageInfo(getPackageName(), 0).
applicationInfo.uid; |
177 cmd.append(iptables); | 180 cmd.append(iptables); |
178 cmd.append(IPTABLES_RETURN.replace("{{UID}}", String.valueOf(uid))); | 181 cmd.append(IPTABLES_RETURN.replace("{{UID}}", String.valueOf(uid))); |
179 String rules = cmd.toString(); | 182 final String rules = cmd.toString(); |
180 RootTools.sendShell(rules, DEFAULT_TIMEOUT); | 183 RootTools.sendShell(rules, DEFAULT_TIMEOUT); |
181 transparent = true; | 184 transparent = true; |
182 } | 185 } |
183 catch (FileNotFoundException e) | 186 catch (final FileNotFoundException e) |
184 { | 187 { |
185 // ignore - this is "normal" case | 188 // ignore - this is "normal" case |
186 } | 189 } |
187 catch (NameNotFoundException e) | 190 catch (final NameNotFoundException e) |
188 { | 191 { |
189 Log.e(TAG, "Failed to initialize iptables", e); | 192 Log.e(TAG, "Failed to initialize iptables", e); |
190 } | 193 } |
191 catch (IOException e) | 194 catch (final IOException e) |
192 { | 195 { |
193 Log.e(TAG, "Failed to initialize iptables", e); | 196 Log.e(TAG, "Failed to initialize iptables", e); |
194 } | 197 } |
195 catch (RootToolsException e) | 198 catch (final RootToolsException e) |
196 { | 199 { |
197 Log.e(TAG, "Failed to initialize iptables", e); | 200 Log.e(TAG, "Failed to initialize iptables", e); |
198 } | 201 } |
199 catch (TimeoutException e) | 202 catch (final TimeoutException e) |
200 { | 203 { |
201 Log.e(TAG, "Failed to initialize iptables", e); | 204 Log.e(TAG, "Failed to initialize iptables", e); |
202 } | 205 } |
203 } | 206 } |
204 | 207 |
205 if (!transparent) | 208 if (!transparent) |
206 { | 209 { |
207 // Try to set native proxy | 210 // Try to set native proxy |
208 nativeProxyAutoConfigured = ProxySettings.setConnectionProxy(getApplicatio
nContext(), LOCALHOST, port, ""); | 211 nativeProxyAutoConfigured = ProxySettings.setConnectionProxy(getApplicatio
nContext(), LOCALHOST, port, ""); |
209 | 212 |
210 if (NATIVE_PROXY_SUPPORTED) | 213 if (NATIVE_PROXY_SUPPORTED) |
211 { | 214 { |
212 registerReceiver(connectionReceiver, new IntentFilter(ConnectivityManage
r.CONNECTIVITY_ACTION)); | 215 registerReceiver(connectionReceiver, new IntentFilter(ConnectivityManage
r.CONNECTIVITY_ACTION)); |
213 registerReceiver(connectionReceiver, new IntentFilter(Proxy.PROXY_CHANGE
_ACTION)); | 216 registerReceiver(connectionReceiver, new IntentFilter(Proxy.PROXY_CHANGE
_ACTION)); |
214 } | 217 } |
215 } | 218 } |
216 | 219 |
217 // Save current native proxy situation. The service is always started on the
first run so | 220 // Save current native proxy situation. The service is always started on the |
| 221 // first run so |
218 // we will always have a correct value from the box | 222 // we will always have a correct value from the box |
219 SharedPreferences.Editor editor = prefs.edit(); | 223 final SharedPreferences.Editor editor = prefs.edit(); |
220 editor.putBoolean(getString(R.string.pref_proxyautoconfigured), transparent
|| nativeProxyAutoConfigured); | 224 editor.putBoolean(getString(R.string.pref_proxyautoconfigured), transparent
|| nativeProxyAutoConfigured); |
221 editor.commit(); | 225 editor.commit(); |
222 | 226 |
223 registerReceiver(proxyReceiver, new IntentFilter(ProxyService.BROADCAST_PROX
Y_FAILED)); | 227 registerReceiver(proxyReceiver, new IntentFilter(ProxyService.BROADCAST_PROX
Y_FAILED)); |
224 registerReceiver(filterReceiver, new IntentFilter(AdblockPlus.BROADCAST_FILT
ERING_CHANGE)); | 228 registerReceiver(filterReceiver, new IntentFilter(AdblockPlus.BROADCAST_FILT
ERING_CHANGE)); |
225 registerReceiver(filterReceiver, new IntentFilter(AdblockPlus.BROADCAST_FILT
ER_MATCHES)); | 229 registerReceiver(filterReceiver, new IntentFilter(AdblockPlus.BROADCAST_FILT
ER_MATCHES)); |
226 | 230 |
227 // Start proxy | 231 // Start proxy |
228 if (proxy == null) | 232 if (proxy == null) |
229 { | 233 { |
230 // Select available port and bind to it, use previously selected port by d
efault | 234 // Select available port and bind to it, use previously selected port by |
| 235 // default |
231 portVariants[0] = prefs.getInt(getString(R.string.pref_lastport), -1); | 236 portVariants[0] = prefs.getInt(getString(R.string.pref_lastport), -1); |
232 ServerSocket listen = null; | 237 ServerSocket listen = null; |
233 String msg = null; | 238 String msg = null; |
234 for (int p : portVariants) | 239 for (final int p : portVariants) |
235 { | 240 { |
236 if (p < 0) | 241 if (p < 0) |
237 continue; | 242 continue; |
238 try | 243 try |
239 { | 244 { |
240 // Fix for #232, bind proxy socket to loopback only | 245 // Fix for #232, bind proxy socket to loopback only |
241 listen = new ServerSocket(p, 1024, InetAddress.getByName(LOCALHOST)); | 246 listen = new ServerSocket(p, 1024, InetAddress.getByName(LOCALHOST)); |
242 port = p; | 247 port = p; |
243 break; | 248 break; |
244 } | 249 } |
245 catch (IOException e) | 250 catch (final IOException e) |
246 { | 251 { |
247 Log.e(TAG, null, e); | 252 Log.e(TAG, null, e); |
248 msg = e.getMessage(); | 253 msg = e.getMessage(); |
249 } | 254 } |
250 } | 255 } |
251 if (listen == null) | 256 if (listen == null) |
252 { | 257 { |
253 sendBroadcast(new Intent(BROADCAST_PROXY_FAILED).putExtra("msg", msg)); | 258 sendBroadcast(new Intent(BROADCAST_PROXY_FAILED).putExtra("msg", msg)); |
254 return; | 259 return; |
255 } | 260 } |
(...skipping 26 matching lines...) Expand all Loading... |
282 proxy.logLevel = Server.LOG_DIAGNOSTIC; | 287 proxy.logLevel = Server.LOG_DIAGNOSTIC; |
283 proxy.setup(listen, proxyConfiguration.getProperty("handler"), proxyConfig
uration); | 288 proxy.setup(listen, proxyConfiguration.getProperty("handler"), proxyConfig
uration); |
284 proxy.start(); | 289 proxy.start(); |
285 } | 290 } |
286 | 291 |
287 if (transparent) | 292 if (transparent) |
288 { | 293 { |
289 // Redirect traffic via iptables | 294 // Redirect traffic via iptables |
290 try | 295 try |
291 { | 296 { |
292 StringBuffer cmd = new StringBuffer(); | 297 final StringBuffer cmd = new StringBuffer(); |
293 cmd.append(iptables); | 298 cmd.append(iptables); |
294 cmd.append(IPTABLES_ADD_HTTP.replace("{{PORT}}", String.valueOf(port))); | 299 cmd.append(IPTABLES_ADD_HTTP.replace("{{PORT}}", String.valueOf(port))); |
295 String rules = cmd.toString(); | 300 final String rules = cmd.toString(); |
296 RootTools.sendShell(rules, DEFAULT_TIMEOUT); | 301 RootTools.sendShell(rules, DEFAULT_TIMEOUT); |
297 } | 302 } |
298 catch (FileNotFoundException e) | 303 catch (final FileNotFoundException e) |
299 { | 304 { |
300 // ignore - this is "normal" case | 305 // ignore - this is "normal" case |
301 } | 306 } |
302 catch (IOException e) | 307 catch (final IOException e) |
303 { | 308 { |
304 Log.e(TAG, "Failed to initialize iptables", e); | 309 Log.e(TAG, "Failed to initialize iptables", e); |
305 } | 310 } |
306 catch (RootToolsException e) | 311 catch (final RootToolsException e) |
307 { | 312 { |
308 Log.e(TAG, "Failed to initialize iptables", e); | 313 Log.e(TAG, "Failed to initialize iptables", e); |
309 } | 314 } |
310 catch (TimeoutException e) | 315 catch (final TimeoutException e) |
311 { | 316 { |
312 Log.e(TAG, "Failed to initialize iptables", e); | 317 Log.e(TAG, "Failed to initialize iptables", e); |
313 } | 318 } |
314 } | 319 } |
315 | 320 |
316 prefs.registerOnSharedPreferenceChangeListener(this); | 321 prefs.registerOnSharedPreferenceChangeListener(this); |
317 | 322 |
318 // Lock service | 323 // Lock service |
319 hideIcon = prefs.getBoolean(getString(R.string.pref_hideicon), resources.get
Boolean(R.bool.def_hideicon)); | 324 hideIcon = prefs.getBoolean(getString(R.string.pref_hideicon), resources.get
Boolean(R.bool.def_hideicon)); |
320 startForeground(ONGOING_NOTIFICATION_ID, getNotification()); | 325 startForeground(ONGOING_NOTIFICATION_ID, getNotification()); |
321 | 326 |
322 // If automatic setting of proxy was blocked, check if user has set it manua
lly | 327 // If automatic setting of proxy was blocked, check if user has set it |
323 boolean manual = isManual(); | 328 // manually |
| 329 final boolean manual = isManual(); |
324 if (manual && NATIVE_PROXY_SUPPORTED) | 330 if (manual && NATIVE_PROXY_SUPPORTED) |
325 { | 331 { |
326 ConnectivityManager connectivityManager = (ConnectivityManager) getSystemS
ervice(Context.CONNECTIVITY_SERVICE); | 332 final ConnectivityManager connectivityManager = (ConnectivityManager) getS
ystemService(Context.CONNECTIVITY_SERVICE); |
327 updateNoTrafficCheck(connectivityManager); | 333 updateNoTrafficCheck(connectivityManager); |
328 } | 334 } |
329 | 335 |
330 sendStateChangedBroadcast(); | 336 sendStateChangedBroadcast(); |
331 Log.i(TAG, "Service started"); | 337 Log.i(TAG, "Service started"); |
332 } | 338 } |
333 | 339 |
334 @Override | 340 @Override |
335 public int onStartCommand(Intent intent, int flags, int startId) | 341 public int onStartCommand(final Intent intent, final int flags, final int star
tId) |
336 { | 342 { |
337 return START_STICKY; | 343 return START_STICKY; |
338 } | 344 } |
339 | 345 |
340 @Override | 346 @Override |
341 public void onDestroy() | 347 public void onDestroy() |
342 { | 348 { |
343 super.onDestroy(); | 349 super.onDestroy(); |
344 | 350 |
345 stopNoTrafficCheck(); | 351 stopNoTrafficCheck(); |
346 | 352 |
347 unregisterReceiver(filterReceiver); | 353 unregisterReceiver(filterReceiver); |
348 unregisterReceiver(proxyReceiver); | 354 unregisterReceiver(proxyReceiver); |
349 | 355 |
350 // Stop IP redirecting | 356 // Stop IP redirecting |
351 if (transparent) | 357 if (transparent) |
352 { | 358 { |
353 new Thread() | 359 new Thread() |
354 { | 360 { |
355 @Override | 361 @Override |
356 public void run() | 362 public void run() |
357 { | 363 { |
358 try | 364 try |
359 { | 365 { |
360 RootTools.sendShell(iptables + " -t nat -F OUTPUT", DEFAULT_TIMEOUT)
; | 366 RootTools.sendShell(iptables + " -t nat -F OUTPUT", DEFAULT_TIMEOUT)
; |
361 } | 367 } |
362 catch (Exception e) | 368 catch (final Exception e) |
363 { | 369 { |
364 Log.e(TAG, "Failed to clear iptables", e); | 370 Log.e(TAG, "Failed to clear iptables", e); |
365 } | 371 } |
366 } | 372 } |
367 }.start(); | 373 }.start(); |
368 } | 374 } |
369 | 375 |
370 if (!transparent && NATIVE_PROXY_SUPPORTED) | 376 if (!transparent && NATIVE_PROXY_SUPPORTED) |
371 unregisterReceiver(connectionReceiver); | 377 unregisterReceiver(connectionReceiver); |
372 | 378 |
373 // Clear native proxy | 379 // Clear native proxy |
374 if (nativeProxyAutoConfigured) | 380 if (nativeProxyAutoConfigured) |
375 { | 381 { |
376 clearConnectionProxy(); | 382 clearConnectionProxy(); |
377 } | 383 } |
378 | 384 |
379 sendBroadcast(new Intent(BROADCAST_STATE_CHANGED).putExtra("enabled", false)
); | 385 sendBroadcast(new Intent(BROADCAST_STATE_CHANGED).putExtra("enabled", false)
); |
380 | 386 |
381 // Stop proxy server | 387 // Stop proxy server |
382 if (proxy != null) | 388 if (proxy != null) |
383 proxy.close(); | 389 proxy.close(); |
384 | 390 |
385 // Release service lock | 391 // Release service lock |
386 stopForeground(true); | 392 stopForeground(true); |
387 | 393 |
388 Log.i(TAG, "Service stopped"); | 394 Log.i(TAG, "Service stopped"); |
389 } | 395 } |
390 | 396 |
391 /** | 397 /** |
392 * Restores system proxy settings via native call on Android 3.1+ devices | 398 * Restores system proxy settings via native call on Android 3.1+ devices usin
g Java reflection. |
393 * using Java reflection. | |
394 */ | 399 */ |
395 private void clearConnectionProxy() | 400 private void clearConnectionProxy() |
396 { | 401 { |
397 String proxyHost = proxyConfiguration.getProperty("adblock.proxyHost"); | 402 final String proxyHost = proxyConfiguration.getProperty("adblock.proxyHost")
; |
398 String proxyPort = proxyConfiguration.getProperty("adblock.proxyPort"); | 403 final String proxyPort = proxyConfiguration.getProperty("adblock.proxyPort")
; |
399 String proxyExcl = proxyConfiguration.getProperty("adblock.proxyExcl"); | 404 final String proxyExcl = proxyConfiguration.getProperty("adblock.proxyExcl")
; |
400 int port = 0; | 405 int port = 0; |
401 try | 406 try |
402 { | 407 { |
403 if (proxyHost != null) | 408 if (proxyHost != null) |
404 port = Integer.valueOf(proxyPort); | 409 port = Integer.valueOf(proxyPort); |
405 } | 410 } |
406 catch (NumberFormatException e) | 411 catch (final NumberFormatException e) |
407 { | 412 { |
408 Log.e(TAG, "Bad port setting", e); | 413 Log.e(TAG, "Bad port setting", e); |
409 } | 414 } |
410 ProxySettings.setConnectionProxy(getApplicationContext(), proxyHost, port, p
roxyExcl); | 415 ProxySettings.setConnectionProxy(getApplicationContext(), proxyHost, port, p
roxyExcl); |
411 } | 416 } |
412 | 417 |
413 /** | 418 /** |
414 * Sets user proxy settings in proxy service properties. | 419 * Sets user proxy settings in proxy service properties. |
415 */ | 420 */ |
416 private void configureUserProxy(Properties config, String proxyHost, String pr
oxyPort, String proxyExcl, String proxyUser, String proxyPass) | 421 private void configureUserProxy(final Properties config, final String proxyHos
t, final String proxyPort, final String proxyExcl, final String proxyUser, final
String proxyPass) |
417 { | 422 { |
418 // Clean previous settings | 423 // Clean previous settings |
419 config.remove("adblock.proxyHost"); | 424 config.remove("adblock.proxyHost"); |
420 config.remove("adblock.proxyPort"); | 425 config.remove("adblock.proxyPort"); |
421 config.remove("adblock.auth"); | 426 config.remove("adblock.auth"); |
422 config.remove("adblock.proxyExcl"); | 427 config.remove("adblock.proxyExcl"); |
423 if (!transparent) | 428 if (!transparent) |
424 { | 429 { |
425 config.remove("https.proxyHost"); | 430 config.remove("https.proxyHost"); |
426 config.remove("https.proxyPort"); | 431 config.remove("https.proxyPort"); |
(...skipping 11 matching lines...) Expand all Loading... |
438 // proxy points to ourselves | 443 // proxy points to ourselves |
439 // proxy port is null, 0 or not a number | 444 // proxy port is null, 0 or not a number |
440 // proxy is 127.0.0.1:8080 | 445 // proxy is 127.0.0.1:8080 |
441 if (proxyPort == null) | 446 if (proxyPort == null) |
442 return; | 447 return; |
443 int p = 0; | 448 int p = 0; |
444 try | 449 try |
445 { | 450 { |
446 p = Integer.valueOf(proxyPort); | 451 p = Integer.valueOf(proxyPort); |
447 } | 452 } |
448 catch (NumberFormatException e) | 453 catch (final NumberFormatException e) |
449 { | 454 { |
450 return; | 455 return; |
451 } | 456 } |
452 if (p == 0 || isLocalHost(proxyHost) && (p == port || p == 8080)) | 457 if (p == 0 || isLocalHost(proxyHost) && (p == port || p == 8080)) |
453 { | 458 { |
454 if (nativeProxyAutoConfigured) | 459 if (nativeProxyAutoConfigured) |
455 passProxySettings(null, null, null); | 460 passProxySettings(null, null, null); |
456 return; | 461 return; |
457 } | 462 } |
458 | 463 |
459 config.put("adblock.proxyHost", proxyHost); | 464 config.put("adblock.proxyHost", proxyHost); |
460 config.put("adblock.proxyPort", proxyPort); | 465 config.put("adblock.proxyPort", proxyPort); |
461 if (!transparent) | 466 if (!transparent) |
462 { | 467 { |
463 config.put("https.proxyHost", proxyHost); | 468 config.put("https.proxyHost", proxyHost); |
464 config.put("https.proxyPort", proxyPort); | 469 config.put("https.proxyPort", proxyPort); |
465 } | 470 } |
466 | 471 |
467 // TODO Not implemented in our proxy but needed to restore settings | 472 // TODO Not implemented in our proxy but needed to restore settings |
468 if (proxyExcl != null) | 473 if (proxyExcl != null) |
469 config.put("adblock.proxyExcl", proxyExcl); | 474 config.put("adblock.proxyExcl", proxyExcl); |
470 | 475 |
471 if (proxyUser != null && !"".equals(proxyUser) && proxyPass != null && !"".e
quals(proxyPass)) | 476 if (proxyUser != null && !"".equals(proxyUser) && proxyPass != null && !"".e
quals(proxyPass)) |
472 { | 477 { |
473 // Base64 encode user:password | 478 // Base64 encode user:password |
474 String proxyAuth = "Basic " + new String(Base64.encode(proxyUser + ":" + p
roxyPass)); | 479 final String proxyAuth = "Basic " + new String(Base64.encode(proxyUser + "
:" + proxyPass)); |
475 config.put("adblock.auth", proxyAuth); | 480 config.put("adblock.auth", proxyAuth); |
476 if (!transparent) | 481 if (!transparent) |
477 config.put("https.auth", proxyAuth); | 482 config.put("https.auth", proxyAuth); |
478 } | 483 } |
479 } | 484 } |
480 | 485 |
481 private void passProxySettings(String proxyHost, String proxyPort, String prox
yExcl) | 486 private void passProxySettings(final String proxyHost, final String proxyPort,
final String proxyExcl) |
482 { | 487 { |
483 try | 488 try |
484 { | 489 { |
485 CrashHandler handler = (CrashHandler) Thread.getDefaultUncaughtExceptionHa
ndler(); | 490 final CrashHandler handler = (CrashHandler) Thread.getDefaultUncaughtExcep
tionHandler(); |
486 handler.saveProxySettings(proxyHost, proxyPort, proxyExcl); | 491 handler.saveProxySettings(proxyHost, proxyPort, proxyExcl); |
487 } | 492 } |
488 catch (ClassCastException e) | 493 catch (final ClassCastException e) |
489 { | 494 { |
490 // ignore - default handler in use | 495 // ignore - default handler in use |
491 } | 496 } |
492 } | 497 } |
493 | 498 |
494 @Override | 499 @Override |
495 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, Str
ing key) | 500 public void onSharedPreferenceChanged(final SharedPreferences sharedPreference
s, final String key) |
496 { | 501 { |
497 if (!NATIVE_PROXY_SUPPORTED) | 502 if (!NATIVE_PROXY_SUPPORTED) |
498 { | 503 { |
499 String ketHost = getString(R.string.pref_proxyhost); | 504 final String ketHost = getString(R.string.pref_proxyhost); |
500 String keyPort = getString(R.string.pref_proxyport); | 505 final String keyPort = getString(R.string.pref_proxyport); |
501 String keyUser = getString(R.string.pref_proxyuser); | 506 final String keyUser = getString(R.string.pref_proxyuser); |
502 String keyPass = getString(R.string.pref_proxypass); | 507 final String keyPass = getString(R.string.pref_proxypass); |
503 if (key.equals(ketHost) || key.equals(keyPort) || key.equals(keyUser) || k
ey.equals(keyPass)) | 508 if (key.equals(ketHost) || key.equals(keyPort) || key.equals(keyUser) || k
ey.equals(keyPass)) |
504 { | 509 { |
505 String proxyHost = sharedPreferences.getString(ketHost, null); | 510 final String proxyHost = sharedPreferences.getString(ketHost, null); |
506 String proxyPort = sharedPreferences.getString(keyPort, null); | 511 final String proxyPort = sharedPreferences.getString(keyPort, null); |
507 String proxyUser = sharedPreferences.getString(keyUser, null); | 512 final String proxyUser = sharedPreferences.getString(keyUser, null); |
508 String proxyPass = sharedPreferences.getString(keyPass, null); | 513 final String proxyPass = sharedPreferences.getString(keyPass, null); |
509 if (proxy != null) | 514 if (proxy != null) |
510 { | 515 { |
511 configureUserProxy(proxyConfiguration, proxyHost, proxyPort, null, pro
xyUser, proxyPass); | 516 configureUserProxy(proxyConfiguration, proxyHost, proxyPort, null, pro
xyUser, proxyPass); |
512 proxy.restart(proxyConfiguration.getProperty("handler")); | 517 proxy.restart(proxyConfiguration.getProperty("handler")); |
513 } | 518 } |
514 } | 519 } |
515 } | 520 } |
516 } | 521 } |
517 | 522 |
518 public boolean isTransparent() | 523 public boolean isTransparent() |
(...skipping 18 matching lines...) Expand all Loading... |
537 * Checks whether traffic check is pending | 542 * Checks whether traffic check is pending |
538 */ | 543 */ |
539 public boolean noTraffic() | 544 public boolean noTraffic() |
540 { | 545 { |
541 return notrafficHandler != null; | 546 return notrafficHandler != null; |
542 } | 547 } |
543 | 548 |
544 /** | 549 /** |
545 * Checks if specified host is local. | 550 * Checks if specified host is local. |
546 */ | 551 */ |
547 private static final boolean isLocalHost(String host) | 552 private static final boolean isLocalHost(final String host) |
548 { | 553 { |
549 if (host == null) | 554 if (host == null) |
| 555 { |
550 return false; | 556 return false; |
| 557 } |
551 | 558 |
552 try | 559 try |
553 { | 560 { |
554 if (host.equalsIgnoreCase("localhost")) | 561 if (host.equalsIgnoreCase("localhost")) |
555 return true; | 562 return true; |
556 | 563 |
557 String className = "android.net.NetworkUtils"; | 564 final String className = "android.net.NetworkUtils"; |
558 Class<?> c = Class.forName(className); | 565 final Class<?> c = Class.forName(className); |
559 /* | 566 /* |
560 * InetAddress address = NetworkUtils.numericToInetAddress(host); | 567 * InetAddress address = NetworkUtils.numericToInetAddress(host); |
561 */ | 568 */ |
562 Method method = c.getMethod("numericToInetAddress", String.class); | 569 final Method method = c.getMethod("numericToInetAddress", String.class); |
563 InetAddress address = (InetAddress) method.invoke(null, host); | 570 final InetAddress address = (InetAddress) method.invoke(null, host); |
564 | 571 |
565 if (address.isLoopbackAddress()) | 572 if (address.isLoopbackAddress()) |
566 return true; | 573 return true; |
567 } | 574 } |
568 catch (Exception e) | 575 catch (final Exception e) |
569 { | 576 { |
570 Log.w(TAG, null, e); | 577 Log.w(TAG, null, e); |
571 } | 578 } |
572 return false; | 579 return false; |
573 } | 580 } |
574 | 581 |
575 /** | 582 /** |
576 * Initializes iptables executable. | 583 * Initializes iptables executable. |
577 * | 584 * |
578 * @throws FileNotFoundException | 585 * @throws FileNotFoundException |
579 * If iptables initialization failed due to provided reasons. | 586 * If iptables initialization failed due to provided reasons. |
580 */ | 587 */ |
581 private void initIptables() throws IOException, RootToolsException, TimeoutExc
eption, FileNotFoundException | 588 private void initIptables() throws IOException, RootToolsException, TimeoutExc
eption, FileNotFoundException |
582 { | 589 { |
583 if (!RootTools.isAccessGiven()) | 590 if (!RootTools.isAccessGiven()) |
584 throw new FileNotFoundException("No root access"); | 591 throw new FileNotFoundException("No root access"); |
585 | 592 |
586 File ipt = getFileStreamPath("iptables"); | 593 final File ipt = getFileStreamPath("iptables"); |
587 | 594 |
588 if (!ipt.exists()) | 595 if (!ipt.exists()) |
589 { | 596 { |
590 Log.e(TAG, "No iptables excutable found"); | 597 Log.e(TAG, "No iptables excutable found"); |
591 throw new FileNotFoundException("No iptables executable"); | 598 throw new FileNotFoundException("No iptables executable"); |
592 } | 599 } |
593 | 600 |
594 String path = ipt.getAbsolutePath(); | 601 final String path = ipt.getAbsolutePath(); |
595 | 602 |
596 RootTools.sendShell("chmod 700 " + path, DEFAULT_TIMEOUT); | 603 RootTools.sendShell("chmod 700 " + path, DEFAULT_TIMEOUT); |
597 | 604 |
598 boolean compatible = false; | 605 boolean compatible = false; |
599 boolean version = false; | 606 boolean version = false; |
600 | 607 |
601 String command = path + " --version\n" + path + " -L -t nat -n\n"; | 608 final String command = path + " --version\n" + path + " -L -t nat -n\n"; |
602 | 609 |
603 List<String> result = RootTools.sendShell(command, DEFAULT_TIMEOUT); | 610 final List<String> result = RootTools.sendShell(command, DEFAULT_TIMEOUT); |
604 for (String line : result) | 611 for (final String line : result) |
605 { | 612 { |
606 if (line.contains("OUTPUT")) | 613 if (line.contains("OUTPUT")) |
607 compatible = true; | 614 compatible = true; |
608 if (line.contains("v1.4.")) | 615 if (line.contains("v1.4.")) |
609 version = true; | 616 version = true; |
610 } | 617 } |
611 | 618 |
612 if (!compatible || !version) | 619 if (!compatible || !version) |
613 { | 620 { |
614 Log.e(TAG, "Incompatible iptables excutable"); | 621 Log.e(TAG, "Incompatible iptables excutable"); |
615 throw new FileNotFoundException("Incompatible iptables excutable"); | 622 throw new FileNotFoundException("Incompatible iptables excutable"); |
616 } | 623 } |
617 | 624 |
618 iptables = path; | 625 iptables = path; |
619 } | 626 } |
620 | 627 |
621 public List<String> getIptablesOutput() | 628 public List<String> getIptablesOutput() |
622 { | 629 { |
623 if (iptables == null) | 630 if (iptables == null) |
624 return null; | 631 return null; |
625 | 632 |
626 String command = iptables + " -L -t nat -n\n"; | 633 final String command = iptables + " -L -t nat -n\n"; |
627 try | 634 try |
628 { | 635 { |
629 return RootTools.sendShell(command, DEFAULT_TIMEOUT); | 636 return RootTools.sendShell(command, DEFAULT_TIMEOUT); |
630 } | 637 } |
631 catch (Exception e) | 638 catch (final Exception e) |
632 { | 639 { |
633 Log.e(TAG, "Failed to get iptables configuration", e); | 640 Log.e(TAG, "Failed to get iptables configuration", e); |
634 return null; | 641 return null; |
635 } | 642 } |
636 } | 643 } |
637 | 644 |
638 /** | 645 /** |
639 * Raises or removes no traffic notification based on current link proxy | 646 * Raises or removes no traffic notification based on current link proxy setti
ngs |
640 * settings | |
641 */ | 647 */ |
642 private void updateNoTrafficCheck(ConnectivityManager connectivityManager) | 648 private void updateNoTrafficCheck(final ConnectivityManager connectivityManage
r) |
643 { | 649 { |
644 try | 650 try |
645 { | 651 { |
646 Object pp = ProxySettings.getActiveLinkProxy(connectivityManager); | 652 final Object pp = ProxySettings.getActiveLinkProxy(connectivityManager); |
647 String[] userProxy = ProxySettings.getUserProxy(pp); | 653 final String[] userProxy = ProxySettings.getUserProxy(pp); |
648 if (userProxy != null) | 654 if (userProxy != null) |
649 Log.i(TAG, "Proxy settings: " + userProxy[0] + ":" + userProxy[1] + "("
+ userProxy[2] + ")"); | 655 Log.i(TAG, "Proxy settings: " + userProxy[0] + ":" + userProxy[1] + "("
+ userProxy[2] + ")"); |
650 updateNoTrafficCheck(userProxy); | 656 updateNoTrafficCheck(userProxy); |
651 } | 657 } |
652 catch (Exception e) | 658 catch (final Exception e) |
653 { | 659 { |
654 // This should not happen | 660 // This should not happen |
655 Log.e(TAG, null, e); | 661 Log.e(TAG, null, e); |
656 } | 662 } |
657 } | 663 } |
658 | 664 |
659 /** | 665 /** |
660 * Raises or removes no traffic notification based on the user proxy settings | 666 * Raises or removes no traffic notification based on the user proxy settings |
661 */ | 667 */ |
662 private void updateNoTrafficCheck(String[] userProxy) | 668 private void updateNoTrafficCheck(final String[] userProxy) |
663 { | 669 { |
664 boolean ourProxy = userProxy != null && isLocalHost(userProxy[0]) && Integer
.valueOf(userProxy[1]) == port; | 670 final boolean ourProxy = userProxy != null && isLocalHost(userProxy[0]) && I
nteger.valueOf(userProxy[1]) == port; |
665 if (ourProxy != proxyManualyConfigured) | 671 if (ourProxy != proxyManualyConfigured) |
666 { | 672 { |
667 proxyManualyConfigured = ourProxy; | 673 proxyManualyConfigured = ourProxy; |
668 sendStateChangedBroadcast(); | 674 sendStateChangedBroadcast(); |
669 } | 675 } |
670 if (ourProxy) | 676 if (ourProxy) |
671 { | 677 { |
672 stopNoTrafficCheck(); | 678 stopNoTrafficCheck(); |
673 } | 679 } |
674 else | 680 else |
675 { | 681 { |
676 // Initiate no traffic check | 682 // Initiate no traffic check |
677 notrafficHandler = new Handler(); | 683 notrafficHandler = new Handler(); |
678 notrafficHandler.postDelayed(noTraffic, NO_TRAFFIC_TIMEOUT); | 684 notrafficHandler.postDelayed(noTraffic, NO_TRAFFIC_TIMEOUT); |
679 } | 685 } |
680 NotificationManager notificationManager = (NotificationManager) getSystemSer
vice(NOTIFICATION_SERVICE); | 686 final NotificationManager notificationManager = (NotificationManager) getSys
temService(NOTIFICATION_SERVICE); |
681 notificationManager.notify(ONGOING_NOTIFICATION_ID, getNotification()); | 687 notificationManager.notify(ONGOING_NOTIFICATION_ID, getNotification()); |
682 } | 688 } |
683 | 689 |
684 /** | 690 /** |
685 * Stops no traffic check and resets notification message. | 691 * Stops no traffic check and resets notification message. |
686 */ | 692 */ |
687 private void stopNoTrafficCheck() | 693 private void stopNoTrafficCheck() |
688 { | 694 { |
689 if (notrafficHandler != null) | 695 if (notrafficHandler != null) |
690 { | 696 { |
691 notrafficHandler.removeCallbacks(noTraffic); | 697 notrafficHandler.removeCallbacks(noTraffic); |
692 sendStateChangedBroadcast(); | 698 sendStateChangedBroadcast(); |
693 NotificationManager notificationManager = (NotificationManager) getSystemS
ervice(NOTIFICATION_SERVICE); | 699 final NotificationManager notificationManager = (NotificationManager) getS
ystemService(NOTIFICATION_SERVICE); |
694 notificationManager.notify(ONGOING_NOTIFICATION_ID, getNotification()); | 700 notificationManager.notify(ONGOING_NOTIFICATION_ID, getNotification()); |
695 notificationManager.cancel(NOTRAFFIC_NOTIFICATION_ID); | 701 notificationManager.cancel(NOTRAFFIC_NOTIFICATION_ID); |
696 } | 702 } |
697 notrafficHandler = null; | 703 notrafficHandler = null; |
698 } | 704 } |
699 | 705 |
700 @SuppressLint("NewApi") | 706 @SuppressLint("NewApi") |
701 private Notification getNotification() | 707 private Notification getNotification() |
702 { | 708 { |
703 boolean filtering = AdblockPlus.getApplication().isFilteringEnabled(); | 709 final boolean filtering = AdblockPlus.getApplication().isFilteringEnabled(); |
704 | 710 |
705 int msgId = R.string.notif_waiting; | 711 int msgId = R.string.notif_waiting; |
706 if (nativeProxyAutoConfigured || proxyManualyConfigured) | 712 if (nativeProxyAutoConfigured || proxyManualyConfigured) |
707 msgId = filtering ? R.string.notif_wifi : R.string.notif_wifi_nofiltering; | 713 msgId = filtering ? R.string.notif_wifi : R.string.notif_wifi_nofiltering; |
708 if (transparent) | 714 if (transparent) |
709 msgId = R.string.notif_all; | 715 msgId = R.string.notif_all; |
710 | 716 |
711 NotificationCompat.Builder builder = new NotificationCompat.Builder(this); | 717 final NotificationCompat.Builder builder = new NotificationCompat.Builder(th
is); |
712 if (hideIcon && msgId != R.string.notif_waiting) | 718 if (hideIcon && msgId != R.string.notif_waiting) |
713 { | 719 { |
714 builder.setWhen(POSITION_RIGHT); | 720 builder.setWhen(POSITION_RIGHT); |
715 builder.setSmallIcon(R.drawable.transparent); | 721 builder.setSmallIcon(R.drawable.transparent); |
716 //builder.setContent(new RemoteViews(getPackageName(), R.layout.notif_hidd
en)); | 722 // builder.setContent(new RemoteViews(getPackageName(), |
| 723 // R.layout.notif_hidden)); |
717 } | 724 } |
718 else | 725 else |
719 { | 726 { |
720 builder.setWhen(0); | 727 builder.setWhen(0); |
721 builder.setSmallIcon(R.drawable.ic_stat_blocking); | 728 builder.setSmallIcon(R.drawable.ic_stat_blocking); |
722 } | 729 } |
723 PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(
this, Preferences.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_A
CTIVITY_NEW_TASK), 0); | 730 final PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new I
ntent(this, Preferences.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.
FLAG_ACTIVITY_NEW_TASK), 0); |
724 builder.setContentIntent(contentIntent); | 731 builder.setContentIntent(contentIntent); |
725 builder.setContentTitle(getText(R.string.app_name)); | 732 builder.setContentTitle(getText(R.string.app_name)); |
726 builder.setContentText(getString(msgId, port)); | 733 builder.setContentText(getString(msgId, port)); |
727 builder.setOngoing(true); | 734 builder.setOngoing(true); |
728 | 735 |
729 Notification notification = builder.getNotification(); | 736 final Notification notification = builder.getNotification(); |
730 return notification; | 737 return notification; |
731 } | 738 } |
732 | 739 |
733 public void setEmptyIcon(boolean hide) | 740 public void setEmptyIcon(final boolean hide) |
734 { | 741 { |
735 hideIcon = hide; | 742 hideIcon = hide; |
736 NotificationManager notificationManager = (NotificationManager) getSystemSer
vice(NOTIFICATION_SERVICE); | 743 final NotificationManager notificationManager = (NotificationManager) getSys
temService(NOTIFICATION_SERVICE); |
737 notificationManager.notify(ONGOING_NOTIFICATION_ID, getNotification()); | 744 notificationManager.notify(ONGOING_NOTIFICATION_ID, getNotification()); |
738 } | 745 } |
739 | 746 |
740 public void sendStateChangedBroadcast() | 747 public void sendStateChangedBroadcast() |
741 { | 748 { |
742 Log.i(TAG, "Broadcasting " + BROADCAST_STATE_CHANGED); | 749 Log.i(TAG, "Broadcasting " + BROADCAST_STATE_CHANGED); |
743 boolean manual = isManual(); | 750 final boolean manual = isManual(); |
744 Intent stateIntent = new Intent(BROADCAST_STATE_CHANGED).putExtra("enabled",
true).putExtra("port", port).putExtra("manual", manual); | 751 final Intent stateIntent = new Intent(BROADCAST_STATE_CHANGED).putExtra("ena
bled", true).putExtra("port", port).putExtra("manual", manual); |
745 if (manual) | 752 if (manual) |
746 stateIntent.putExtra("configured", proxyManualyConfigured); | 753 stateIntent.putExtra("configured", proxyManualyConfigured); |
747 sendBroadcast(stateIntent); | 754 sendBroadcast(stateIntent); |
748 } | 755 } |
749 | 756 |
750 private final IBinder binder = new LocalBinder(); | 757 private final IBinder binder = new LocalBinder(); |
751 | 758 |
752 public final class LocalBinder extends Binder | 759 public final class LocalBinder extends Binder |
753 { | 760 { |
754 public ProxyService getService() | 761 public ProxyService getService() |
755 { | 762 { |
756 return ProxyService.this; | 763 return ProxyService.this; |
757 } | 764 } |
758 } | 765 } |
759 | 766 |
760 @Override | 767 @Override |
761 public IBinder onBind(Intent intent) | 768 public IBinder onBind(final Intent intent) |
762 { | 769 { |
763 return binder; | 770 return binder; |
764 } | 771 } |
765 | 772 |
766 /** | 773 /** |
767 * Executed if no traffic is detected after a period of time. Notifies user | 774 * Executed if no traffic is detected after a period of time. Notifies user ab
out possible |
768 * about possible configuration problems. | 775 * configuration problems. |
769 */ | 776 */ |
770 private Runnable noTraffic = new Runnable() | 777 private final Runnable noTraffic = new Runnable() |
771 { | 778 { |
| 779 @Override |
772 public void run() | 780 public void run() |
773 { | 781 { |
774 // It's weird but notrafficHandler.removeCallbacks(noTraffic) does not rem
ove this callback | 782 // It's weird but notrafficHandler.removeCallbacks(noTraffic) does not |
| 783 // remove this callback |
775 if (notrafficHandler == null) | 784 if (notrafficHandler == null) |
| 785 { |
776 return; | 786 return; |
| 787 } |
777 // Show warning notification | 788 // Show warning notification |
778 NotificationCompat.Builder builder = new NotificationCompat.Builder(ProxyS
ervice.this); | 789 final NotificationCompat.Builder builder = new NotificationCompat.Builder(
ProxyService.this); |
779 builder.setSmallIcon(R.drawable.ic_stat_warning); | 790 builder.setSmallIcon(R.drawable.ic_stat_warning); |
780 builder.setWhen(System.currentTimeMillis()); | 791 builder.setWhen(System.currentTimeMillis()); |
781 builder.setAutoCancel(true); | 792 builder.setAutoCancel(true); |
782 Intent intent = new Intent(ProxyService.this, ConfigurationActivity.class)
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); | 793 final Intent intent = new Intent(ProxyService.this, ConfigurationActivity.
class).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
783 intent.putExtra("port", port); | 794 intent.putExtra("port", port); |
784 PendingIntent contentIntent = PendingIntent.getActivity(ProxyService.this,
0, intent, PendingIntent.FLAG_UPDATE_CURRENT); | 795 final PendingIntent contentIntent = PendingIntent.getActivity(ProxyService
.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); |
785 builder.setContentIntent(contentIntent); | 796 builder.setContentIntent(contentIntent); |
786 builder.setContentTitle(getText(R.string.app_name)); | 797 builder.setContentTitle(getText(R.string.app_name)); |
787 builder.setContentText(getText(R.string.notif_notraffic)); | 798 builder.setContentText(getText(R.string.notif_notraffic)); |
788 NotificationManager notificationManager = (NotificationManager) getSystemS
ervice(NOTIFICATION_SERVICE); | 799 final NotificationManager notificationManager = (NotificationManager) getS
ystemService(NOTIFICATION_SERVICE); |
789 notificationManager.notify(NOTRAFFIC_NOTIFICATION_ID, builder.getNotificat
ion()); | 800 notificationManager.notify(NOTRAFFIC_NOTIFICATION_ID, builder.getNotificat
ion()); |
790 } | 801 } |
791 }; | 802 }; |
792 | 803 |
793 /** | 804 /** |
794 * Stops no traffic check if traffic is detected by proxy service. | 805 * Stops no traffic check if traffic is detected by proxy service. |
795 */ | 806 */ |
796 private BroadcastReceiver filterReceiver = new BroadcastReceiver() | 807 private final BroadcastReceiver filterReceiver = new BroadcastReceiver() |
797 { | 808 { |
798 @Override | 809 @Override |
799 public void onReceive(final Context context, Intent intent) | 810 public void onReceive(final Context context, final Intent intent) |
800 { | 811 { |
801 if (intent.getAction().equals(AdblockPlus.BROADCAST_FILTERING_CHANGE)) | 812 if (intent.getAction().equals(AdblockPlus.BROADCAST_FILTERING_CHANGE)) |
802 { | 813 { |
803 // It's rather a hack but things are happening simultaneously and we | 814 // It's rather a hack but things are happening simultaneously and we |
804 // receive this broadcast despite the fact we have unsubscribed from | 815 // receive this broadcast despite the fact we have unsubscribed from |
805 // it and notification is not removed because it is changed to new one | 816 // it and notification is not removed because it is changed to new one |
806 // during removal. | 817 // during removal. |
807 if (!ProxyService.this.isNativeProxyAutoConfigured()) | 818 if (!isNativeProxyAutoConfigured()) |
808 { | 819 { |
809 NotificationManager notificationManager = (NotificationManager) getSys
temService(NOTIFICATION_SERVICE); | 820 final NotificationManager notificationManager = (NotificationManager)
getSystemService(NOTIFICATION_SERVICE); |
810 notificationManager.notify(ONGOING_NOTIFICATION_ID, getNotification())
; | 821 notificationManager.notify(ONGOING_NOTIFICATION_ID, getNotification())
; |
811 } | 822 } |
812 } | 823 } |
813 if (intent.getAction().equals(AdblockPlus.BROADCAST_FILTER_MATCHES)) | 824 if (intent.getAction().equals(AdblockPlus.BROADCAST_FILTER_MATCHES)) |
814 { | 825 { |
815 proxyManualyConfigured = true; | 826 proxyManualyConfigured = true; |
816 stopNoTrafficCheck(); | 827 stopNoTrafficCheck(); |
817 } | 828 } |
818 } | 829 } |
819 }; | 830 }; |
820 | 831 |
821 /** | 832 /** |
822 * Stops service if proxy fails. | 833 * Stops service if proxy fails. |
823 */ | 834 */ |
824 private BroadcastReceiver proxyReceiver = new BroadcastReceiver() | 835 private final BroadcastReceiver proxyReceiver = new BroadcastReceiver() |
825 { | 836 { |
826 @Override | 837 @Override |
827 public void onReceive(final Context context, Intent intent) | 838 public void onReceive(final Context context, final Intent intent) |
828 { | 839 { |
829 if (intent.getAction().equals(ProxyService.BROADCAST_PROXY_FAILED)) | 840 if (intent.getAction().equals(ProxyService.BROADCAST_PROXY_FAILED)) |
830 { | 841 { |
831 stopSelf(); | 842 stopSelf(); |
832 } | 843 } |
833 } | 844 } |
834 }; | 845 }; |
835 | 846 |
836 /** | 847 /** |
837 * Monitors system network connection settings changes and updates proxy | 848 * Monitors system network connection settings changes and updates proxy setti
ngs accordingly. |
838 * settings accordingly. | |
839 */ | 849 */ |
840 private BroadcastReceiver connectionReceiver = new BroadcastReceiver() | 850 private final BroadcastReceiver connectionReceiver = new BroadcastReceiver() |
841 { | 851 { |
842 @Override | 852 @Override |
843 public void onReceive(Context ctx, Intent intent) | 853 public void onReceive(final Context ctx, final Intent intent) |
844 { | 854 { |
845 String action = intent.getAction(); | 855 final String action = intent.getAction(); |
846 Log.i(TAG, "Action: " + action); | 856 Log.i(TAG, "Action: " + action); |
847 // Connectivity change | 857 // Connectivity change |
848 if (ConnectivityManager.CONNECTIVITY_ACTION.equals(action)) | 858 if (ConnectivityManager.CONNECTIVITY_ACTION.equals(action)) |
849 { | 859 { |
850 ConnectivityManager connectivityManager = (ConnectivityManager) getSyste
mService(Context.CONNECTIVITY_SERVICE); | 860 final ConnectivityManager connectivityManager = (ConnectivityManager) ge
tSystemService(Context.CONNECTIVITY_SERVICE); |
851 // TODO Should we use ConnectivityManagerCompat.getNetworkInfoFromBroadc
ast() instead? | 861 // TODO Should we use ConnectivityManagerCompat.getNetworkInfoFromBroadc
ast() instead? |
852 NetworkInfo info = connectivityManager.getActiveNetworkInfo(); | 862 final NetworkInfo info = connectivityManager.getActiveNetworkInfo(); |
853 if (info == null) | 863 if (info == null) |
854 return; | 864 return; |
855 String typeName = info.getTypeName(); | 865 final String typeName = info.getTypeName(); |
856 String subtypeName = info.getSubtypeName(); | 866 final String subtypeName = info.getSubtypeName(); |
857 boolean available = info.isAvailable(); | 867 final boolean available = info.isAvailable(); |
858 | 868 |
859 Log.i(TAG, "Network Type: " + typeName + ", subtype: " + subtypeName + "
, available: " + available); | 869 Log.i(TAG, "Network Type: " + typeName + ", subtype: " + subtypeName + "
, available: " + available); |
860 if (info.getType() == ConnectivityManager.TYPE_WIFI) | 870 if (info.getType() == ConnectivityManager.TYPE_WIFI) |
861 { | 871 { |
862 if (nativeProxyAutoConfigured) | 872 if (nativeProxyAutoConfigured) |
863 { | 873 { |
864 ProxySettings.setConnectionProxy(getApplicationContext(), LOCALHOST,
port, ""); | 874 ProxySettings.setConnectionProxy(getApplicationContext(), LOCALHOST,
port, ""); |
865 } | 875 } |
866 else | 876 else |
867 { | 877 { |
868 updateNoTrafficCheck(connectivityManager); | 878 updateNoTrafficCheck(connectivityManager); |
869 } | 879 } |
870 } | 880 } |
871 } | 881 } |
872 // Proxy change | 882 // Proxy change |
873 else if (Proxy.PROXY_CHANGE_ACTION.equals(action)) | 883 else if (Proxy.PROXY_CHANGE_ACTION.equals(action)) |
874 { | 884 { |
875 Object pp = intent.getParcelableExtra("proxy"); | 885 final Object pp = intent.getParcelableExtra("proxy"); |
876 try | 886 try |
877 { | 887 { |
878 String[] userProxy = ProxySettings.getUserProxy(pp); | 888 final String[] userProxy = ProxySettings.getUserProxy(pp); |
879 if (nativeProxyAutoConfigured) | 889 if (nativeProxyAutoConfigured) |
880 { | 890 { |
881 if (userProxy != null && Integer.valueOf(userProxy[1]) != port) | 891 if (userProxy != null && Integer.valueOf(userProxy[1]) != port) |
882 { | 892 { |
883 Log.i(TAG, "User has set new proxy: " + userProxy[0] + ":" + userP
roxy[1] + "(" + userProxy[2] + ")"); | 893 Log.i(TAG, "User has set new proxy: " + userProxy[0] + ":" + userP
roxy[1] + "(" + userProxy[2] + ")"); |
884 if (proxy != null) | 894 if (proxy != null) |
885 { | 895 { |
886 configureUserProxy(proxyConfiguration, userProxy[0], userProxy[1
], userProxy[2], null, null); | 896 configureUserProxy(proxyConfiguration, userProxy[0], userProxy[1
], userProxy[2], null, null); |
887 proxy.restart(proxyConfiguration.getProperty("handler")); | 897 proxy.restart(proxyConfiguration.getProperty("handler")); |
888 } | 898 } |
889 } | 899 } |
890 } | 900 } |
891 else | 901 else |
892 { | 902 { |
893 Log.i(TAG, "User has set proxy: " + userProxy[0] + ":" + userProxy[1
] + "(" + userProxy[2] + ")"); | 903 Log.i(TAG, "User has set proxy: " + userProxy[0] + ":" + userProxy[1
] + "(" + userProxy[2] + ")"); |
894 updateNoTrafficCheck(userProxy); | 904 updateNoTrafficCheck(userProxy); |
895 } | 905 } |
896 } | 906 } |
897 catch (Exception e) | 907 catch (final Exception e) |
898 { | 908 { |
899 // This should not happen | 909 // This should not happen |
900 Log.e(TAG, null, e); | 910 Log.e(TAG, null, e); |
901 } | 911 } |
902 } | 912 } |
903 } | 913 } |
904 }; | 914 }; |
905 | 915 |
906 final class ProxyServer extends Server | 916 final class ProxyServer extends Server |
907 { | 917 { |
908 @Override | 918 @Override |
909 public void close() | 919 public void close() |
910 { | 920 { |
911 try | 921 try |
912 { | 922 { |
913 listen.close(); | 923 listen.close(); |
914 this.interrupt(); | 924 this.interrupt(); |
915 this.join(); | 925 this.join(); |
916 } | 926 } |
917 catch (Exception e) | 927 catch (final Exception e) |
918 { | 928 { |
919 // ignore - it always happens | 929 // ignore - it always happens |
920 } | 930 } |
921 log(LOG_WARNING, null, "server stopped"); | 931 log(LOG_WARNING, null, "server stopped"); |
922 } | 932 } |
923 | 933 |
924 @Override | 934 @Override |
925 public void log(int level, Object obj, String message) | 935 public void log(final int level, final Object obj, final String message) |
926 { | 936 { |
927 if (level <= logLevel) | 937 if (level <= logLevel) |
928 { | 938 { |
929 Log.println(7 - level, obj != null ? obj.toString() : TAG, message); | 939 Log.println(7 - level, obj != null ? obj.toString() : TAG, message); |
930 } | 940 } |
931 } | 941 } |
932 } | 942 } |
933 } | 943 } |
OLD | NEW |