Left: | ||
Right: |
LEFT | RIGHT |
---|---|
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 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
50 import android.util.Log; | 50 import android.util.Log; |
51 | 51 |
52 public class ProxyService extends Service implements OnSharedPreferenceChangeLis tener | 52 public class ProxyService extends Service implements OnSharedPreferenceChangeLis tener |
53 { | 53 { |
54 private static final String TAG = Utils.getTag(ProxyService.class); | 54 private static final String TAG = Utils.getTag(ProxyService.class); |
55 | 55 |
56 private static final String LOCALHOST = "127.0.0.1"; | 56 private static final String LOCALHOST = "127.0.0.1"; |
57 | 57 |
58 private static final int[] PORT_VARIANTS = new int[] {-1, 2020, 3030, 4040, 50 50, 6060, 7070, 9090, 1234, 12345, 4321, 0}; | 58 private static final int[] PORT_VARIANTS = new int[] {-1, 2020, 3030, 4040, 50 50, 6060, 7070, 9090, 1234, 12345, 4321, 0}; |
59 | 59 |
60 // FIXME remove me | |
Felix Dahlke
2014/08/19 09:06:00
Why not remove it?
René Jeschke
2014/08/19 10:41:33
When I remove it, we don't have a way to enable re
Felix Dahlke
2014/08/19 12:37:26
Can you remove the FIXME comment then? :)
René Jeschke
2014/08/19 12:57:36
Done.
| |
61 private static final boolean LOG_REQUESTS = false; | 60 private static final boolean LOG_REQUESTS = false; |
62 | 61 |
63 static final int ONGOING_NOTIFICATION_ID = R.string.app_name; | 62 static final int ONGOING_NOTIFICATION_ID = R.string.app_name; |
64 | 63 |
65 private static final long POSITION_RIGHT = Build.VERSION.SDK_INT >= Build.VERS ION_CODES.GINGERBREAD ? Long.MIN_VALUE : Long.MAX_VALUE; | 64 private static final long POSITION_RIGHT = Build.VERSION.SDK_INT >= Build.VERS ION_CODES.GINGERBREAD ? Long.MIN_VALUE : Long.MAX_VALUE; |
66 | 65 |
67 /** | 66 /** |
68 * Broadcasted when service starts or stops. | 67 * Broadcasted when service starts or stops. |
69 */ | 68 */ |
70 public static final String BROADCAST_STATE_CHANGED = "org.adblockplus.android. SERVICE_STATE_CHANGED"; | 69 public static final String BROADCAST_STATE_CHANGED = "org.adblockplus.android. SERVICE_STATE_CHANGED"; |
71 | 70 |
72 /** | 71 /** |
73 * Broadcasted if proxy fails to start. | 72 * Broadcasted if proxy fails to start. |
74 */ | 73 */ |
75 public static final String BROADCAST_PROXY_FAILED = "org.adblockplus.android.P ROXY_FAILURE"; | 74 public static final String BROADCAST_PROXY_FAILED = "org.adblockplus.android.P ROXY_FAILURE"; |
76 | 75 |
77 /** | 76 /** |
78 * Common command bridge | 77 * Common command bridge |
79 */ | 78 */ |
80 public static final String COMMAND_BRIDGE_ACTION = "org.adblockplus.android.CO MMAND_BRIDGE"; | 79 public static final String PROXY_STATE_CHANGED_ACTION = "org.adblockplus.andro id.PROXY_STATE_CHANGED"; |
81 | 80 |
82 boolean hideIcon; | 81 boolean hideIcon; |
83 | 82 |
84 protected ProxyServer proxy = null; | 83 protected ProxyServer proxy = null; |
85 | 84 |
86 protected int port; | 85 protected int port; |
87 | 86 |
88 private final Properties proxyConfiguration = new Properties(); | 87 private final Properties proxyConfiguration = new Properties(); |
89 | 88 |
90 private ProxyConfigurator proxyConfigurator = null; | 89 private ProxyConfigurator proxyConfigurator = null; |
91 | 90 |
92 @SuppressLint("NewApi") | 91 @SuppressLint("NewApi") |
93 @Override | 92 @Override |
94 public void onCreate() | 93 public void onCreate() |
95 { | 94 { |
96 super.onCreate(); | 95 super.onCreate(); |
97 | 96 |
98 // FIXME 'StrictMode' is API9 | |
Felix Dahlke
2014/08/19 09:06:00
Isn't our conclusion so far that API 9 should be o
René Jeschke
2014/08/19 10:41:33
Hm, there are devices out there that run API7 on a
Felix Dahlke
2014/08/19 12:37:26
Still - we're using API9 all over the place, don't
René Jeschke
2014/08/19 12:57:36
Thing is, as you disliked my tries to remove the w
Felix Dahlke
2014/08/19 13:17:50
Did I? I'm all for getting rid of warnings!
René Jeschke
2014/08/19 13:23:53
Yeah, think it's been unrelated changes back then,
Felix Dahlke
2014/08/19 13:28:51
8)
| |
99 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder() | 97 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder() |
100 .permitAll() | 98 .permitAll() |
101 .penaltyLog() | 99 .penaltyLog() |
102 .build()); | 100 .build()); |
103 | 101 |
104 final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreference s(this); | 102 final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreference s(this); |
105 | 103 |
106 // Try to read user proxy settings | 104 // Try to read user proxy settings |
107 String proxyHost = System.getProperty("http.proxyHost");; | 105 String proxyHost = System.getProperty("http.proxyHost");; |
108 String proxyPort = System.getProperty("http.proxyPort"); | 106 String proxyPort = System.getProperty("http.proxyPort"); |
109 final String proxyExcl = System.getProperty("http.nonProxyHosts"); | 107 final String proxyExcl = System.getProperty("http.nonProxyHosts"); |
110 String proxyUser = null; | 108 String proxyUser = null; |
111 String proxyPass = null; | 109 String proxyPass = null; |
112 | 110 |
113 if (proxyHost == null || proxyPort == null) | 111 if (proxyHost == null || proxyPort == null) |
114 { | 112 { |
115 // Read application settings | 113 // Read application settings |
116 proxyHost = prefs.getString(getString(R.string.pref_proxyhost), null); | 114 proxyHost = prefs.getString(getString(R.string.pref_proxyhost), null); |
117 proxyPort = prefs.getString(getString(R.string.pref_proxyport), null); | 115 proxyPort = prefs.getString(getString(R.string.pref_proxyport), null); |
118 proxyUser = prefs.getString(getString(R.string.pref_proxyuser), null); | 116 proxyUser = prefs.getString(getString(R.string.pref_proxyuser), null); |
119 proxyPass = prefs.getString(getString(R.string.pref_proxypass), null); | 117 proxyPass = prefs.getString(getString(R.string.pref_proxypass), null); |
120 } | 118 } |
121 | 119 |
122 registerReceiver(this.proxyReceiver, new IntentFilter(ProxyService.BROADCAST _PROXY_FAILED)); | 120 registerReceiver(this.proxyReceiver, new IntentFilter(ProxyService.BROADCAST _PROXY_FAILED)); |
123 registerReceiver(this.commandBridge, new IntentFilter(ProxyService.COMMAND_B RIDGE_ACTION)); | 121 registerReceiver(this.proxyStateChangedReceiver, new IntentFilter(ProxyServi ce.PROXY_STATE_CHANGED_ACTION)); |
124 | 122 |
125 final InetAddress inetAddress; | 123 final InetAddress inetAddress; |
126 try | 124 try |
127 { | 125 { |
128 inetAddress = InetAddress.getByName(LOCALHOST); | 126 inetAddress = InetAddress.getByName(LOCALHOST); |
129 } | 127 } |
130 catch (final UnknownHostException e) | 128 catch (final UnknownHostException e) |
131 { | 129 { |
132 sendBroadcast(new Intent(BROADCAST_PROXY_FAILED) | 130 sendBroadcast(new Intent(BROADCAST_PROXY_FAILED) |
133 .putExtra("msg", "Could not resolve localhost, this one is serious^^") ); | 131 .putExtra("msg", "Could not resolve 'localhost'")); |
Felix Dahlke
2014/08/19 09:06:00
I can see your point, but I think we shouldn't hav
René Jeschke
2014/08/19 10:41:33
Done.
| |
134 return; | 132 return; |
135 } | 133 } |
136 | 134 |
137 // Start proxy | 135 // Start proxy |
138 if (proxy == null) | 136 if (proxy == null) |
139 { | 137 { |
140 // Select available port and bind to it, use previously selected port by d efault | 138 // Select available port and bind to it, use previously selected port by d efault |
141 ServerSocket listen = null; | 139 ServerSocket listen = null; |
142 String msg = null; | 140 String msg = null; |
143 for (final int p : PORT_VARIANTS) | 141 for (final int p : PORT_VARIANTS) |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
190 { | 188 { |
191 case HTTP: | 189 case HTTP: |
192 proxyConfiguration.put("main.handlers", "urlmodifier adblock"); | 190 proxyConfiguration.put("main.handlers", "urlmodifier adblock"); |
193 proxyConfiguration.put("urlmodifier.class", "org.adblockplus.brazil.Tran sparentProxyHandler"); | 191 proxyConfiguration.put("urlmodifier.class", "org.adblockplus.brazil.Tran sparentProxyHandler"); |
194 break; | 192 break; |
195 case HTTPS: | 193 case HTTPS: |
196 proxyConfiguration.put("main.handlers", "https adblock"); | 194 proxyConfiguration.put("main.handlers", "https adblock"); |
197 proxyConfiguration.put("https.class", "org.adblockplus.brazil.SSLConnect ionHandler"); | 195 proxyConfiguration.put("https.class", "org.adblockplus.brazil.SSLConnect ionHandler"); |
198 break; | 196 break; |
199 default: | 197 default: |
200 // FIXME should we reset the settings here? (Can't happen currently) | |
Felix Dahlke
2014/08/19 09:06:00
As with most TODO comments, I think this should be
René Jeschke
2014/08/19 10:41:33
Ah, just see that this TODO is now out-of-scope, r
| |
201 sendBroadcast(new Intent(BROADCAST_PROXY_FAILED) | 198 sendBroadcast(new Intent(BROADCAST_PROXY_FAILED) |
202 .putExtra("msg", "Unsupported proxy server type: " + this.getProxyRe gistrationType().getProxyType())); | 199 .putExtra("msg", "Unsupported proxy server type: " + this.getProxyRe gistrationType().getProxyType())); |
203 return; | 200 return; |
204 } | 201 } |
205 proxyConfiguration.put("adblock.class", "org.adblockplus.brazil.RequestHan dler"); | 202 proxyConfiguration.put("adblock.class", "org.adblockplus.brazil.RequestHan dler"); |
206 if (LOG_REQUESTS) | 203 if (LOG_REQUESTS) |
207 proxyConfiguration.put("adblock.proxylog", "yes"); | 204 proxyConfiguration.put("adblock.proxylog", "yes"); |
208 | 205 |
209 configureUserProxy(proxyConfiguration, proxyHost, proxyPort, proxyExcl, pr oxyUser, proxyPass); | 206 configureUserProxy(proxyConfiguration, proxyHost, proxyPort, proxyExcl, pr oxyUser, proxyPass); |
210 | 207 |
211 proxy = new ProxyServer(); | 208 proxy = new ProxyServer(); |
212 proxy.logLevel = Server.LOG_LOG; | 209 proxy.logLevel = Server.LOG_LOG; |
213 proxy.setup(listen, proxyConfiguration.getProperty("handler"), proxyConfig uration); | 210 proxy.setup(listen, proxyConfiguration.getProperty("handler"), proxyConfig uration); |
214 proxy.start(); | 211 proxy.start(); |
215 } // if (proxy == null) // Start proxy | 212 } |
Felix Dahlke
2014/08/19 09:06:00
I think this comment is ridiculously likely to get
René Jeschke
2014/08/19 10:41:33
Done.
| |
216 | 213 |
217 prefs.registerOnSharedPreferenceChangeListener(this); | 214 prefs.registerOnSharedPreferenceChangeListener(this); |
218 | 215 |
219 // Lock service | 216 // Lock service |
220 hideIcon = prefs.getBoolean(getString(R.string.pref_hideicon), getResources( ).getBoolean(R.bool.def_hideicon)); | 217 hideIcon = prefs.getBoolean(getString(R.string.pref_hideicon), getResources( ).getBoolean(R.bool.def_hideicon)); |
221 startForeground(ONGOING_NOTIFICATION_ID, getNotification()); | 218 startForeground(ONGOING_NOTIFICATION_ID, getNotification()); |
222 | 219 |
223 sendStateChangedBroadcast(); | 220 sendStateChangedBroadcast(); |
224 Log.i(TAG, "Service started"); | 221 Log.i(TAG, "Service started"); |
225 } | 222 } |
226 | 223 |
227 @Override | 224 @Override |
228 public int onStartCommand(final Intent intent, final int flags, final int star tId) | 225 public int onStartCommand(final Intent intent, final int flags, final int star tId) |
229 { | 226 { |
230 return START_STICKY; | 227 return START_STICKY; |
231 } | 228 } |
232 | 229 |
233 @Override | 230 @Override |
234 public void onDestroy() | 231 public void onDestroy() |
235 { | 232 { |
236 super.onDestroy(); | 233 super.onDestroy(); |
237 | 234 |
238 unregisterReceiver(this.proxyReceiver); | 235 unregisterReceiver(this.proxyReceiver); |
239 unregisterReceiver(this.commandBridge); | 236 unregisterReceiver(this.proxyStateChangedReceiver); |
240 | 237 |
241 if (this.proxyConfigurator != null) | 238 if (this.proxyConfigurator != null) |
242 { | 239 { |
243 this.proxyConfigurator.unregisterProxy(); | 240 this.proxyConfigurator.unregisterProxy(); |
244 this.proxyConfigurator.shutdown(); | 241 this.proxyConfigurator.shutdown(); |
245 } | 242 } |
246 | 243 |
247 sendBroadcast(new Intent(BROADCAST_STATE_CHANGED).putExtra("enabled", false) ); | 244 sendBroadcast(new Intent(BROADCAST_STATE_CHANGED).putExtra("enabled", false) ); |
248 | 245 |
249 // Stop proxy server | 246 // Stop proxy server |
250 if (proxy != null) | 247 if (proxy != null) |
251 { | 248 { |
252 proxy.close(); | 249 proxy.close(); |
253 proxy = null; | 250 proxy = null; |
254 } | 251 } |
255 | 252 |
256 // Release service lock | 253 // Release service lock |
257 stopForeground(true); | 254 stopForeground(true); |
258 | 255 |
259 Log.i(TAG, "Service stopped"); | 256 Log.i(TAG, "Service stopped"); |
260 } | 257 } |
261 | 258 |
262 /** | 259 /** |
263 * Sets user proxy settings in proxy service properties. | 260 * Sets user proxy settings in proxy service properties. |
264 */ | 261 */ |
265 private void configureUserProxy(final Properties config, final String proxyHos t, final String proxyPort, final String proxyExcl, | 262 private void configureUserProxy(final Properties config, final String proxyHos t, final String proxyPort, final String proxyExcl, |
266 final String proxyUser, final String proxyPass) | 263 final String proxyUser, final String proxyPass) |
267 { | 264 { |
268 final ProxyRegistrationType regType = this.getProxyRegistrationType(); | |
Felix Dahlke
2014/08/19 09:06:00
How about moving this down to where it's used?
René Jeschke
2014/08/19 10:41:33
Done.
| |
269 | |
270 // Clean previous settings | 265 // Clean previous settings |
271 config.remove("adblock.proxyHost"); | 266 config.remove("adblock.proxyHost"); |
272 config.remove("adblock.proxyPort"); | 267 config.remove("adblock.proxyPort"); |
273 config.remove("adblock.auth"); | 268 config.remove("adblock.auth"); |
274 config.remove("adblock.proxyExcl"); | 269 config.remove("adblock.proxyExcl"); |
275 config.remove("https.proxyHost"); | 270 config.remove("https.proxyHost"); |
276 config.remove("https.proxyPort"); | 271 config.remove("https.proxyPort"); |
277 config.remove("https.auth"); | 272 config.remove("https.auth"); |
278 | 273 |
279 if (regType == ProxyRegistrationType.REFLECTION) | 274 final ProxyRegistrationType regType = this.getProxyRegistrationType(); |
275 if (regType == ProxyRegistrationType.NATIVE) | |
280 { | 276 { |
281 passProxySettings(proxyHost, proxyPort, proxyExcl); | 277 passProxySettings(proxyHost, proxyPort, proxyExcl); |
282 } | 278 } |
283 | 279 |
284 // Check if there are any settings | 280 // Check if there are any settings |
285 if (StringUtils.isEmpty(proxyHost)) | 281 if (StringUtils.isEmpty(proxyHost)) |
286 { | 282 { |
287 return; | 283 return; |
288 } | 284 } |
289 | 285 |
(...skipping 11 matching lines...) Expand all Loading... | |
301 { | 297 { |
302 p = Integer.valueOf(proxyPort); | 298 p = Integer.valueOf(proxyPort); |
303 } | 299 } |
304 catch (final NumberFormatException e) | 300 catch (final NumberFormatException e) |
305 { | 301 { |
306 return; | 302 return; |
307 } | 303 } |
308 | 304 |
309 if (p == 0 || isLocalhost(proxyHost) && (p == port || p == 8080)) | 305 if (p == 0 || isLocalhost(proxyHost) && (p == port || p == 8080)) |
310 { | 306 { |
311 if (regType == ProxyRegistrationType.REFLECTION) | 307 if (regType == ProxyRegistrationType.NATIVE) |
312 { | 308 { |
313 passProxySettings(null, null, null); | 309 passProxySettings(null, null, null); |
314 } | 310 } |
315 return; | 311 return; |
316 } | 312 } |
317 | 313 |
318 config.put("adblock.proxyHost", proxyHost); | 314 config.put("adblock.proxyHost", proxyHost); |
319 config.put("adblock.proxyPort", proxyPort); | 315 config.put("adblock.proxyPort", proxyPort); |
320 if (regType.getProxyType() == ProxyServerType.HTTPS) | 316 if (regType.getProxyType() == ProxyServerType.HTTPS) |
321 { | 317 { |
(...skipping 13 matching lines...) Expand all Loading... | |
335 final String proxyAuth = "Basic " + new String(Base64.encode(proxyUser + " :" + proxyPass)); | 331 final String proxyAuth = "Basic " + new String(Base64.encode(proxyUser + " :" + proxyPass)); |
336 config.put("adblock.auth", proxyAuth); | 332 config.put("adblock.auth", proxyAuth); |
337 if (regType.getProxyType() == ProxyServerType.HTTPS) | 333 if (regType.getProxyType() == ProxyServerType.HTTPS) |
338 { | 334 { |
339 config.put("https.auth", proxyAuth); | 335 config.put("https.auth", proxyAuth); |
340 } | 336 } |
341 } | 337 } |
342 } | 338 } |
343 | 339 |
344 /** | 340 /** |
345 * Lean method to send a {@link BridgeCommand} without additional extras. | |
346 * | |
347 * @param context | |
348 * the context to use for sending the broadcast | |
349 * @param command | |
350 * the command | |
351 */ | |
352 public static void sendBridgeCommand(final Context context, final BridgeComman d command) | |
353 { | |
354 context.sendBroadcast( | |
355 new Intent(COMMAND_BRIDGE_ACTION) | |
356 .putExtra("command", command.toString())); | |
357 } | |
358 | |
359 /** | |
360 * @return {@code true} if the given host string resolves to {@code localhost} | 341 * @return {@code true} if the given host string resolves to {@code localhost} |
361 */ | 342 */ |
362 public static boolean isLocalhost(final String host) | 343 public static boolean isLocalhost(final String host) |
363 { | 344 { |
364 try | 345 try |
365 { | 346 { |
366 if (StringUtils.isEmpty(host)) | 347 if (StringUtils.isEmpty(host)) |
367 { | 348 { |
368 return false; | 349 return false; |
369 } | 350 } |
370 | 351 |
371 if (host.equals("127.0.0.1") || host.equalsIgnoreCase("localhost")) | 352 if (host.equals("127.0.0.1") || host.equalsIgnoreCase("localhost")) |
372 { | 353 { |
373 return true; | 354 return true; |
374 } | 355 } |
375 | 356 |
376 return InetAddress.getByName(host).isLoopbackAddress(); | 357 return InetAddress.getByName(host).isLoopbackAddress(); |
377 } | 358 } |
378 catch (Exception e) | 359 catch (final Exception e) |
379 { | 360 { |
380 return false; | 361 return false; |
381 } | 362 } |
382 } | 363 } |
383 | 364 |
384 private void passProxySettings(final String proxyHost, final String proxyPort, final String proxyExcl) | 365 private void passProxySettings(final String proxyHost, final String proxyPort, final String proxyExcl) |
385 { | 366 { |
386 try | 367 try |
387 { | 368 { |
388 final CrashHandler handler = (CrashHandler) Thread.getDefaultUncaughtExcep tionHandler(); | 369 final CrashHandler handler = (CrashHandler) Thread.getDefaultUncaughtExcep tionHandler(); |
389 handler.saveProxySettings(proxyHost, proxyPort, proxyExcl); | 370 handler.saveProxySettings(proxyHost, proxyPort, proxyExcl); |
390 } | 371 } |
391 catch (final ClassCastException e) | 372 catch (final ClassCastException e) |
392 { | 373 { |
393 // ignore - default handler in use | 374 // ignore - default handler in use |
394 } | 375 } |
395 } | 376 } |
396 | 377 |
397 @Override | 378 @Override |
398 public void onSharedPreferenceChanged(final SharedPreferences sharedPreference s, final String key) | 379 public void onSharedPreferenceChanged(final SharedPreferences sharedPreference s, final String key) |
399 { | 380 { |
400 // TODO verify this | 381 if (this.getProxyRegistrationType() != ProxyRegistrationType.NATIVE) |
Felix Dahlke
2014/08/19 09:06:00
We should probably do that before pushing the code
René Jeschke
2014/08/19 10:41:33
Right, it's the '!NATIVE_PROXY_SUPPORTED' thingy I
| |
401 if (this.getProxyRegistrationType() != ProxyRegistrationType.REFLECTION) | |
402 { | 382 { |
403 final String keyHost = getString(R.string.pref_proxyhost); | 383 final String keyHost = getString(R.string.pref_proxyhost); |
404 final String keyPort = getString(R.string.pref_proxyport); | 384 final String keyPort = getString(R.string.pref_proxyport); |
405 final String keyUser = getString(R.string.pref_proxyuser); | 385 final String keyUser = getString(R.string.pref_proxyuser); |
406 final String keyPass = getString(R.string.pref_proxypass); | 386 final String keyPass = getString(R.string.pref_proxypass); |
407 if (key.equals(keyHost) || key.equals(keyPort) || key.equals(keyUser) || k ey.equals(keyPass)) | 387 if (key.equals(keyHost) || key.equals(keyPort) || key.equals(keyUser) || k ey.equals(keyPass)) |
408 { | 388 { |
409 final String proxyHost = sharedPreferences.getString(keyHost, null); | 389 final String proxyHost = sharedPreferences.getString(keyHost, null); |
410 final String proxyPort = sharedPreferences.getString(keyPort, null); | 390 final String proxyPort = sharedPreferences.getString(keyPort, null); |
411 final String proxyUser = sharedPreferences.getString(keyUser, null); | 391 final String proxyUser = sharedPreferences.getString(keyUser, null); |
412 final String proxyPass = sharedPreferences.getString(keyPass, null); | 392 final String proxyPass = sharedPreferences.getString(keyPass, null); |
413 if (proxy != null) | 393 if (proxy != null) |
414 { | 394 { |
415 configureUserProxy(proxyConfiguration, proxyHost, proxyPort, null, pro xyUser, proxyPass); | 395 configureUserProxy(proxyConfiguration, proxyHost, proxyPort, null, pro xyUser, proxyPass); |
416 proxy.restart(proxyConfiguration.getProperty("handler")); | 396 proxy.restart(proxyConfiguration.getProperty("handler")); |
417 } | 397 } |
418 } | 398 } |
419 } | 399 } |
420 } | 400 } |
421 | 401 |
422 /** | 402 /** |
423 * @return the active proxy configuration's type or {@link ProxyRegistrationTy pe#UNKNOWN} if none is configured | 403 * @return the active proxy configuration's type or {@link ProxyRegistrationTy pe#UNKNOWN} if none is configured |
424 */ | 404 */ |
425 public ProxyRegistrationType getProxyRegistrationType() | 405 private ProxyRegistrationType getProxyRegistrationType() |
426 { | 406 { |
427 return this.proxyConfigurator != null ? this.proxyConfigurator.getType() : P roxyRegistrationType.UNKNOWN; | 407 return this.proxyConfigurator != null ? this.proxyConfigurator.getType() : P roxyRegistrationType.UNKNOWN; |
428 } | 408 } |
429 | 409 |
430 /** | 410 /** |
431 * @return {@code true} if there is a valid proxy configurator and registratio n succeeded | 411 * @return {@code true} if there is a valid proxy configurator and registratio n succeeded |
432 */ | 412 */ |
433 public boolean isRegistered() | 413 public boolean isRegistered() |
434 { | 414 { |
435 return this.proxyConfigurator != null && this.proxyConfigurator.isRegistered (); | 415 return this.proxyConfigurator != null && this.proxyConfigurator.isRegistered (); |
436 } | 416 } |
437 | 417 |
438 /** | 418 /** |
439 * @return {@code true} if registration type is {@link ProxyRegistrationType#R EFLECTION} and registration succeeded | 419 * @return {@code true} if registration type is {@link ProxyRegistrationType#N ATIVE} and registration succeeded |
440 */ | 420 */ |
441 public boolean isNativeProxyAutoConfigured() | 421 public boolean isNativeProxyAutoConfigured() |
442 { | 422 { |
443 return this.getProxyRegistrationType() == ProxyRegistrationType.REFLECTION & & this.isRegistered(); | 423 return this.getProxyRegistrationType() == ProxyRegistrationType.NATIVE && th is.isRegistered(); |
444 } | 424 } |
445 | 425 |
446 /** | 426 /** |
447 * @return {@code true} if registration type is {@link ProxyRegistrationType#M ANUAL} | 427 * @return {@code true} if registration type is {@link ProxyRegistrationType#M ANUAL} |
448 */ | 428 */ |
449 public boolean isManual() | 429 public boolean isManual() |
450 { | 430 { |
451 return this.getProxyRegistrationType() == ProxyRegistrationType.MANUAL; | 431 return this.getProxyRegistrationType() == ProxyRegistrationType.MANUAL; |
432 } | |
433 | |
434 /** | |
435 * @return {@code true} if registration type is {@link ProxyRegistrationType#I PTABLES} | |
436 */ | |
437 public boolean isIptables() | |
438 { | |
439 return this.getProxyRegistrationType() == ProxyRegistrationType.IPTABLES; | |
452 } | 440 } |
453 | 441 |
454 @SuppressLint("NewApi") | 442 @SuppressLint("NewApi") |
455 private Notification getNotification() | 443 private Notification getNotification() |
456 { | 444 { |
457 final boolean filtering = AdblockPlus.getApplication().isFilteringEnabled(); | 445 final boolean filtering = AdblockPlus.getApplication().isFilteringEnabled(); |
458 | 446 |
459 final int msgId; | 447 final int msgId; |
460 switch(this.getProxyRegistrationType()) | 448 switch(this.getProxyRegistrationType()) |
461 { | 449 { |
462 case MANUAL: | 450 case MANUAL: |
463 if (this.isRegistered()) | 451 if (this.isRegistered()) |
464 { | 452 { |
465 msgId = filtering ? R.string.notif_wifi : R.string.notif_wifi_nofilterin g; | 453 msgId = filtering ? R.string.notif_wifi : R.string.notif_wifi_nofilterin g; |
466 } | 454 } |
467 else | 455 else |
468 { | 456 { |
469 msgId = filtering ? R.string.notif_waiting : R.string.notif_wifi_nofilte ring; | 457 msgId = filtering ? R.string.notif_waiting : R.string.notif_wifi_nofilte ring; |
470 } | 458 } |
471 break; | 459 break; |
472 case REFLECTION: | 460 case NATIVE: |
473 msgId = filtering ? R.string.notif_wifi : R.string.notif_wifi_nofiltering; | 461 msgId = filtering ? R.string.notif_wifi : R.string.notif_wifi_nofiltering; |
474 break; | 462 break; |
475 case IPTABLES: | 463 case IPTABLES: |
476 case CYANOGENMOD: | 464 case CYANOGENMOD: |
477 msgId = R.string.notif_all; | 465 msgId = R.string.notif_all; |
478 break; | 466 break; |
479 default: | 467 default: |
480 msgId = R.string.notif_waiting; | 468 msgId = R.string.notif_waiting; |
481 break; | 469 break; |
482 } | 470 } |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
552 { | 540 { |
553 if (intent.getAction().equals(ProxyService.BROADCAST_PROXY_FAILED)) | 541 if (intent.getAction().equals(ProxyService.BROADCAST_PROXY_FAILED)) |
554 { | 542 { |
555 stopSelf(); | 543 stopSelf(); |
556 } | 544 } |
557 } | 545 } |
558 }; | 546 }; |
559 | 547 |
560 /** | 548 /** |
561 * <p> | 549 * <p> |
562 * A BR for simple message passing from various ProxyConfigurators. | 550 * Proxy state change receiver. |
563 * </p> | 551 * </p> |
564 * <p> | 552 */ |
565 * The purpose of this BroadcastReceiver is to allow other components to send notifications to the service and to be prepared for upcoming service | 553 private final BroadcastReceiver proxyStateChangedReceiver = new BroadcastRecei ver() |
566 * refactorings. | |
567 * </p> | |
568 */ | |
569 private final BroadcastReceiver commandBridge = new BroadcastReceiver() | |
570 { | 554 { |
571 @Override | 555 @Override |
572 public void onReceive(final Context context, final Intent intent) | 556 public void onReceive(final Context context, final Intent intent) |
573 { | 557 { |
574 if (intent != null) | 558 if (intent != null && PROXY_STATE_CHANGED_ACTION.equals(intent.getAction() )) |
575 { | 559 { |
576 switch (BridgeCommand.fromString(intent.getStringExtra("command"))) | 560 final NotificationManager notificationManager = (NotificationManager) ge tSystemService(NOTIFICATION_SERVICE); |
577 { | 561 notificationManager.notify(ONGOING_NOTIFICATION_ID, getNotification()); |
578 case STATE_CHANGED: | 562 ProxyService.this.sendStateChangedBroadcast(); |
579 final NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); | |
580 notificationManager.notify(ONGOING_NOTIFICATION_ID, getNotification()) ; | |
581 ProxyService.this.sendStateChangedBroadcast(); | |
582 break; | |
583 default: | |
584 // ignore | |
585 break; | |
586 } | |
587 } | 563 } |
588 } | 564 } |
589 }; | 565 }; |
590 | 566 |
591 final class ProxyServer extends Server | 567 final class ProxyServer extends Server |
592 { | 568 { |
593 @Override | 569 @Override |
594 public void close() | 570 public void close() |
595 { | 571 { |
596 try | 572 try |
(...skipping 12 matching lines...) Expand all Loading... | |
609 @Override | 585 @Override |
610 public void log(final int level, final Object obj, final String message) | 586 public void log(final int level, final Object obj, final String message) |
611 { | 587 { |
612 if (level <= logLevel) | 588 if (level <= logLevel) |
613 { | 589 { |
614 Log.println(7 - level, obj != null ? obj.toString() : TAG, message); | 590 Log.println(7 - level, obj != null ? obj.toString() : TAG, message); |
615 } | 591 } |
616 } | 592 } |
617 } | 593 } |
618 } | 594 } |
LEFT | RIGHT |