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