Index: src/org/adblockplus/android/ProxyService.java |
diff --git a/src/org/adblockplus/android/ProxyService.java b/src/org/adblockplus/android/ProxyService.java |
index 093ea1f894d70a63465354fa7c14dedf8821e367..208341fb426b150df7c6efb359ff4dcb61f1cb0f 100755 |
--- a/src/org/adblockplus/android/ProxyService.java |
+++ b/src/org/adblockplus/android/ProxyService.java |
@@ -59,30 +59,32 @@ import com.stericson.RootTools.RootToolsException; |
public class ProxyService extends Service implements OnSharedPreferenceChangeListener |
{ |
- private static final String LOCALHOST = "127.0.0.1"; |
- /** |
- * Indicates that system supports native proxy configuration. |
- */ |
- public static final boolean NATIVE_PROXY_SUPPORTED = Build.VERSION.SDK_INT >= 12; // Honeycomb 3.1 |
- |
static |
{ |
RootTools.debugMode = false; |
} |
- private static final String TAG = "ProxyService"; |
- private static final boolean logRequests = false; |
+ private final static String TAG = Utils.getTag(ProxyService.class); |
+ |
+ private final static String LOCALHOST = "127.0.0.1"; |
+ /** |
+ * Indicates that system supports native proxy configuration. |
+ */ |
+ private final static boolean logRequests = false; |
+ |
+ public static final boolean NATIVE_PROXY_SUPPORTED = Build.VERSION.SDK_INT >= 12; // Honeycomb |
+ private final static int NOTRAFFIC_NOTIFICATION_ID = R.string.app_name + 3; |
+ final static int ONGOING_NOTIFICATION_ID = R.string.app_name; |
- // Do not use 8080 because it is a "dirty" port, Android uses it if something goes wrong |
- // First element is reserved for previously used port |
- private static final int[] portVariants = new int[] {-1, 2020, 3030, 4040, 5050, 6060, 7070, 9090, 1234, 12345, 4321, 0}; |
+ private final static long POSITION_RIGHT = Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD ? Long.MIN_VALUE : Long.MAX_VALUE; |
private final static int DEFAULT_TIMEOUT = 3000; |
private final static int NO_TRAFFIC_TIMEOUT = 5 * 60 * 1000; // 5 minutes |
- final static int ONGOING_NOTIFICATION_ID = R.string.app_name; |
- private static final long POSITION_RIGHT = Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD ? Long.MIN_VALUE : Long.MAX_VALUE; |
- private final static int NOTRAFFIC_NOTIFICATION_ID = R.string.app_name + 3; |
+ // Do not use 8080 because it is a "dirty" port, Android uses it if something |
+ // goes wrong |
+ // first element is reserved for previously used port |
+ private final static int[] portVariants = new int[] { -1, 2020, 3030, 4040, 5050, 6060, 7070, 9090, 1234, 12345, 4321, 0 }; |
/** |
* Broadcasted when service starts or stops. |
@@ -101,18 +103,20 @@ public class ProxyService extends Service implements OnSharedPreferenceChangeLis |
protected ProxyServer proxy = null; |
protected int port; |
- private Properties proxyConfiguration = new Properties(); |
+ private final Properties proxyConfiguration = new Properties(); |
/** |
* Indicates that service is working with root privileges. |
*/ |
private boolean transparent = false; |
/** |
- * Indicates that service has autoconfigured Android proxy settings (version 3.1+). |
+ * Indicates that service has autoconfigured Android proxy settings (version |
+ * 3.1+). |
*/ |
private boolean nativeProxyAutoConfigured = false; |
/** |
- * Indicates that Android proxy settings are correctly configured (version 4.1.2+ 4.2.2+). |
+ * Indicates that Android proxy settings are correctly configured (version |
+ * 4.1.2+ 4.2.2+). |
*/ |
private boolean proxyManualyConfigured = false; |
@@ -126,15 +130,16 @@ public class ProxyService extends Service implements OnSharedPreferenceChangeLis |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) |
{ |
- // Proxy is running in separate thread, it's just some resolution request during initialization. |
+ // Proxy is running in separate thread, it's just some resolution request |
+ // during initialization. |
// Not worth spawning a separate thread for this. |
- StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitNetwork().build(); |
+ final StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitNetwork().build(); |
StrictMode.setThreadPolicy(policy); |
} |
// Get port for local proxy |
- SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); |
- Resources resources = getResources(); |
+ final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); |
+ final Resources resources = this.getResources(); |
// Try to read user proxy settings |
String proxyHost = null; |
@@ -152,17 +157,19 @@ public class ProxyService extends Service implements OnSharedPreferenceChangeLis |
Log.d(TAG, "PRX: " + proxyHost + ":" + proxyPort + "(" + proxyExcl + ")"); |
// not used but left for future reference |
- String[] px = ProxySettings.getUserProxy(getApplicationContext()); |
+ final String[] px = ProxySettings.getUserProxy(this.getApplicationContext()); |
if (px != null) |
+ { |
Log.d(TAG, "PRX: " + px[0] + ":" + px[1] + "(" + px[2] + ")"); |
+ } |
} |
else |
{ |
// Read application settings |
- proxyHost = prefs.getString(getString(R.string.pref_proxyhost), null); |
- proxyPort = prefs.getString(getString(R.string.pref_proxyport), null); |
- proxyUser = prefs.getString(getString(R.string.pref_proxyuser), null); |
- proxyPass = prefs.getString(getString(R.string.pref_proxypass), null); |
+ proxyHost = prefs.getString(this.getString(R.string.pref_proxyhost), null); |
+ proxyPort = prefs.getString(this.getString(R.string.pref_proxyport), null); |
+ proxyUser = prefs.getString(this.getString(R.string.pref_proxyuser), null); |
+ proxyPass = prefs.getString(this.getString(R.string.pref_proxypass), null); |
} |
// Check for root privileges and try to install transparent proxy |
@@ -170,79 +177,83 @@ public class ProxyService extends Service implements OnSharedPreferenceChangeLis |
{ |
try |
{ |
- initIptables(); |
+ this.initIptables(); |
- StringBuffer cmd = new StringBuffer(); |
- int uid = getPackageManager().getPackageInfo(getPackageName(), 0).applicationInfo.uid; |
- cmd.append(iptables); |
+ final StringBuffer cmd = new StringBuffer(); |
+ final int uid = this.getPackageManager().getPackageInfo(this.getPackageName(), 0).applicationInfo.uid; |
+ cmd.append(this.iptables); |
cmd.append(IPTABLES_RETURN.replace("{{UID}}", String.valueOf(uid))); |
- String rules = cmd.toString(); |
+ final String rules = cmd.toString(); |
RootTools.sendShell(rules, DEFAULT_TIMEOUT); |
- transparent = true; |
+ this.transparent = true; |
} |
- catch (FileNotFoundException e) |
+ catch (final FileNotFoundException e) |
{ |
// ignore - this is "normal" case |
} |
- catch (NameNotFoundException e) |
+ catch (final NameNotFoundException e) |
{ |
Log.e(TAG, "Failed to initialize iptables", e); |
} |
- catch (IOException e) |
+ catch (final IOException e) |
{ |
Log.e(TAG, "Failed to initialize iptables", e); |
} |
- catch (RootToolsException e) |
+ catch (final RootToolsException e) |
{ |
Log.e(TAG, "Failed to initialize iptables", e); |
} |
- catch (TimeoutException e) |
+ catch (final TimeoutException e) |
{ |
Log.e(TAG, "Failed to initialize iptables", e); |
} |
} |
- if (!transparent) |
+ if (!this.transparent) |
{ |
// Try to set native proxy |
- nativeProxyAutoConfigured = ProxySettings.setConnectionProxy(getApplicationContext(), LOCALHOST, port, ""); |
+ this.nativeProxyAutoConfigured = ProxySettings.setConnectionProxy(this.getApplicationContext(), LOCALHOST, this.port, ""); |
if (NATIVE_PROXY_SUPPORTED) |
{ |
- registerReceiver(connectionReceiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION)); |
- registerReceiver(connectionReceiver, new IntentFilter(Proxy.PROXY_CHANGE_ACTION)); |
+ this.registerReceiver(this.connectionReceiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION)); |
+ this.registerReceiver(this.connectionReceiver, new IntentFilter(Proxy.PROXY_CHANGE_ACTION)); |
} |
} |
- // Save current native proxy situation. The service is always started on the first run so |
+ // Save current native proxy situation. The service is always started on the |
+ // first run so |
// we will always have a correct value from the box |
- SharedPreferences.Editor editor = prefs.edit(); |
- editor.putBoolean(getString(R.string.pref_proxyautoconfigured), transparent || nativeProxyAutoConfigured); |
+ final SharedPreferences.Editor editor = prefs.edit(); |
+ editor.putBoolean(this.getString(R.string.pref_proxyautoconfigured), this.transparent || this.nativeProxyAutoConfigured); |
editor.commit(); |
- registerReceiver(proxyReceiver, new IntentFilter(ProxyService.BROADCAST_PROXY_FAILED)); |
- registerReceiver(filterReceiver, new IntentFilter(AdblockPlus.BROADCAST_FILTERING_CHANGE)); |
- registerReceiver(filterReceiver, new IntentFilter(AdblockPlus.BROADCAST_FILTER_MATCHES)); |
+ this.registerReceiver(this.proxyReceiver, new IntentFilter(ProxyService.BROADCAST_PROXY_FAILED)); |
+ this.registerReceiver(this.filterReceiver, new IntentFilter(AdblockPlus.BROADCAST_FILTERING_CHANGE)); |
+ this.registerReceiver(this.filterReceiver, new IntentFilter(AdblockPlus.BROADCAST_FILTER_MATCHES)); |
// Start proxy |
- if (proxy == null) |
+ if (this.proxy == null) |
{ |
- // Select available port and bind to it, use previously selected port by default |
- portVariants[0] = prefs.getInt(getString(R.string.pref_lastport), -1); |
+ // Select available port and bind to it, use previously selected port by |
+ // default |
+ portVariants[0] = prefs.getInt(this.getString(R.string.pref_lastport), -1); |
ServerSocket listen = null; |
String msg = null; |
- for (int p : portVariants) |
+ for (final int p : portVariants) |
{ |
if (p < 0) |
+ { |
continue; |
+ } |
try |
{ |
// Fix for #232, bind proxy socket to loopback only |
listen = new ServerSocket(p, 1024, InetAddress.getByName(LOCALHOST)); |
- port = p; |
+ this.port = p; |
break; |
} |
- catch (IOException e) |
+ catch (final IOException e) |
{ |
Log.e(TAG, null, e); |
msg = e.getMessage(); |
@@ -250,64 +261,66 @@ public class ProxyService extends Service implements OnSharedPreferenceChangeLis |
} |
if (listen == null) |
{ |
- sendBroadcast(new Intent(BROADCAST_PROXY_FAILED).putExtra("msg", msg)); |
+ this.sendBroadcast(new Intent(BROADCAST_PROXY_FAILED).putExtra("msg", msg)); |
return; |
} |
// Save selected port |
- editor.putInt(getString(R.string.pref_lastport), port); |
+ editor.putInt(this.getString(R.string.pref_lastport), this.port); |
editor.commit(); |
// Initialize proxy |
- proxyConfiguration.put("handler", "main"); |
- proxyConfiguration.put("main.prefix", ""); |
- proxyConfiguration.put("main.class", "sunlabs.brazil.server.ChainHandler"); |
- if (transparent) |
+ this.proxyConfiguration.put("handler", "main"); |
+ this.proxyConfiguration.put("main.prefix", ""); |
+ this.proxyConfiguration.put("main.class", "sunlabs.brazil.server.ChainHandler"); |
+ if (this.transparent) |
{ |
- proxyConfiguration.put("main.handlers", "urlmodifier adblock"); |
- proxyConfiguration.put("urlmodifier.class", "org.adblockplus.brazil.TransparentProxyHandler"); |
+ this.proxyConfiguration.put("main.handlers", "urlmodifier adblock"); |
+ this.proxyConfiguration.put("urlmodifier.class", "org.adblockplus.brazil.TransparentProxyHandler"); |
} |
else |
{ |
- proxyConfiguration.put("main.handlers", "https adblock"); |
- proxyConfiguration.put("https.class", "org.adblockplus.brazil.SSLConnectionHandler"); |
+ this.proxyConfiguration.put("main.handlers", "https adblock"); |
+ this.proxyConfiguration.put("https.class", "org.adblockplus.brazil.SSLConnectionHandler"); |
} |
- proxyConfiguration.put("adblock.class", "org.adblockplus.brazil.RequestHandler"); |
+ this.proxyConfiguration.put("adblock.class", "org.adblockplus.brazil.RequestHandler"); |
if (logRequests) |
- proxyConfiguration.put("adblock.proxylog", "yes"); |
+ { |
+ this.proxyConfiguration.put("adblock.proxylog", "yes"); |
+ } |
- configureUserProxy(proxyConfiguration, proxyHost, proxyPort, proxyExcl, proxyUser, proxyPass); |
+ this.configureUserProxy(this.proxyConfiguration, proxyHost, proxyPort, proxyExcl, proxyUser, proxyPass); |
- proxy = new ProxyServer(); |
- proxy.logLevel = Server.LOG_DIAGNOSTIC; |
- proxy.setup(listen, proxyConfiguration.getProperty("handler"), proxyConfiguration); |
- proxy.start(); |
+ this.proxy = new ProxyServer(); |
+ this.proxy.logLevel = Server.LOG_DIAGNOSTIC; |
+ this.proxy.setup(listen, this.proxyConfiguration.getProperty("handler"), this.proxyConfiguration); |
+ this.proxy.start(); |
} |
- if (transparent) |
+ if (this.transparent) |
{ |
// Redirect traffic via iptables |
try |
{ |
- StringBuffer cmd = new StringBuffer(); |
- cmd.append(iptables); |
- cmd.append(IPTABLES_ADD_HTTP.replace("{{PORT}}", String.valueOf(port))); |
- String rules = cmd.toString(); |
+ final StringBuffer cmd = new StringBuffer(); |
+ cmd.append(this.iptables); |
+ cmd.append(IPTABLES_ADD_HTTP.replace("{{PORT}}", String.valueOf(this.port))); |
+ final String rules = cmd.toString(); |
RootTools.sendShell(rules, DEFAULT_TIMEOUT); |
} |
- catch (FileNotFoundException e) |
+ catch (final FileNotFoundException e) |
{ |
// ignore - this is "normal" case |
} |
- catch (IOException e) |
+ catch (final IOException e) |
{ |
Log.e(TAG, "Failed to initialize iptables", e); |
} |
- catch (RootToolsException e) |
+ catch (final RootToolsException e) |
{ |
Log.e(TAG, "Failed to initialize iptables", e); |
} |
- catch (TimeoutException e) |
+ catch (final TimeoutException e) |
{ |
Log.e(TAG, "Failed to initialize iptables", e); |
} |
@@ -316,23 +329,24 @@ public class ProxyService extends Service implements OnSharedPreferenceChangeLis |
prefs.registerOnSharedPreferenceChangeListener(this); |
// Lock service |
- hideIcon = prefs.getBoolean(getString(R.string.pref_hideicon), resources.getBoolean(R.bool.def_hideicon)); |
- startForeground(ONGOING_NOTIFICATION_ID, getNotification()); |
+ this.hideIcon = prefs.getBoolean(this.getString(R.string.pref_hideicon), resources.getBoolean(R.bool.def_hideicon)); |
+ this.startForeground(ONGOING_NOTIFICATION_ID, this.getNotification()); |
- // If automatic setting of proxy was blocked, check if user has set it manually |
- boolean manual = isManual(); |
+ // If automatic setting of proxy was blocked, check if user has set it |
+ // manually |
+ final boolean manual = this.isManual(); |
if (manual && NATIVE_PROXY_SUPPORTED) |
{ |
- ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); |
- updateNoTrafficCheck(connectivityManager); |
+ final ConnectivityManager connectivityManager = (ConnectivityManager)this.getSystemService(Context.CONNECTIVITY_SERVICE); |
+ this.updateNoTrafficCheck(connectivityManager); |
} |
- sendStateChangedBroadcast(); |
+ this.sendStateChangedBroadcast(); |
Log.i(TAG, "Service started"); |
} |
@Override |
- public int onStartCommand(Intent intent, int flags, int startId) |
+ public int onStartCommand(final Intent intent, final int flags, final int startId) |
{ |
return START_STICKY; |
} |
@@ -342,13 +356,13 @@ public class ProxyService extends Service implements OnSharedPreferenceChangeLis |
{ |
super.onDestroy(); |
- stopNoTrafficCheck(); |
+ this.stopNoTrafficCheck(); |
- unregisterReceiver(filterReceiver); |
- unregisterReceiver(proxyReceiver); |
+ this.unregisterReceiver(this.filterReceiver); |
+ this.unregisterReceiver(this.proxyReceiver); |
// Stop IP redirecting |
- if (transparent) |
+ if (this.transparent) |
{ |
new Thread() |
{ |
@@ -357,9 +371,9 @@ public class ProxyService extends Service implements OnSharedPreferenceChangeLis |
{ |
try |
{ |
- RootTools.sendShell(iptables + " -t nat -F OUTPUT", DEFAULT_TIMEOUT); |
+ RootTools.sendShell(ProxyService.this.iptables + " -t nat -F OUTPUT", DEFAULT_TIMEOUT); |
} |
- catch (Exception e) |
+ catch (final Exception e) |
{ |
Log.e(TAG, "Failed to clear iptables", e); |
} |
@@ -367,23 +381,27 @@ public class ProxyService extends Service implements OnSharedPreferenceChangeLis |
}.start(); |
} |
- if (!transparent && NATIVE_PROXY_SUPPORTED) |
- unregisterReceiver(connectionReceiver); |
+ if (!this.transparent && NATIVE_PROXY_SUPPORTED) |
+ { |
+ this.unregisterReceiver(this.connectionReceiver); |
+ } |
// Clear native proxy |
- if (nativeProxyAutoConfigured) |
+ if (this.nativeProxyAutoConfigured) |
{ |
- clearConnectionProxy(); |
+ this.clearConnectionProxy(); |
} |
- sendBroadcast(new Intent(BROADCAST_STATE_CHANGED).putExtra("enabled", false)); |
+ this.sendBroadcast(new Intent(BROADCAST_STATE_CHANGED).putExtra("enabled", false)); |
// Stop proxy server |
- if (proxy != null) |
- proxy.close(); |
+ if (this.proxy != null) |
+ { |
+ this.proxy.close(); |
+ } |
// Release service lock |
- stopForeground(true); |
+ this.stopForeground(true); |
Log.i(TAG, "Service stopped"); |
} |
@@ -394,71 +412,82 @@ public class ProxyService extends Service implements OnSharedPreferenceChangeLis |
*/ |
private void clearConnectionProxy() |
{ |
- String proxyHost = proxyConfiguration.getProperty("adblock.proxyHost"); |
- String proxyPort = proxyConfiguration.getProperty("adblock.proxyPort"); |
- String proxyExcl = proxyConfiguration.getProperty("adblock.proxyExcl"); |
+ final String proxyHost = this.proxyConfiguration.getProperty("adblock.proxyHost"); |
+ final String proxyPort = this.proxyConfiguration.getProperty("adblock.proxyPort"); |
+ final String proxyExcl = this.proxyConfiguration.getProperty("adblock.proxyExcl"); |
int port = 0; |
try |
{ |
if (proxyHost != null) |
+ { |
port = Integer.valueOf(proxyPort); |
+ } |
} |
- catch (NumberFormatException e) |
+ catch (final NumberFormatException e) |
{ |
Log.e(TAG, "Bad port setting", e); |
} |
- ProxySettings.setConnectionProxy(getApplicationContext(), proxyHost, port, proxyExcl); |
+ ProxySettings.setConnectionProxy(this.getApplicationContext(), proxyHost, port, proxyExcl); |
} |
/** |
* Sets user proxy settings in proxy service properties. |
*/ |
- private void configureUserProxy(Properties config, String proxyHost, String proxyPort, String proxyExcl, String proxyUser, String proxyPass) |
+ private void configureUserProxy(final Properties config, final String proxyHost, final String proxyPort, final String proxyExcl, |
+ final String proxyUser, final String proxyPass) |
{ |
// Clean previous settings |
config.remove("adblock.proxyHost"); |
config.remove("adblock.proxyPort"); |
config.remove("adblock.auth"); |
config.remove("adblock.proxyExcl"); |
- if (!transparent) |
+ if (!this.transparent) |
{ |
config.remove("https.proxyHost"); |
config.remove("https.proxyPort"); |
config.remove("https.auth"); |
} |
- if (nativeProxyAutoConfigured) |
- passProxySettings(proxyHost, proxyPort, proxyExcl); |
+ if (this.nativeProxyAutoConfigured) |
+ { |
+ this.passProxySettings(proxyHost, proxyPort, proxyExcl); |
+ } |
// Check if there are any settings |
if (proxyHost == null || "".equals(proxyHost)) |
+ { |
return; |
+ } |
// Check for dirty proxy settings - this indicated previous crash: |
// proxy points to ourselves |
// proxy port is null, 0 or not a number |
// proxy is 127.0.0.1:8080 |
if (proxyPort == null) |
+ { |
return; |
+ } |
int p = 0; |
try |
{ |
p = Integer.valueOf(proxyPort); |
} |
- catch (NumberFormatException e) |
+ catch (final NumberFormatException e) |
{ |
return; |
} |
- if (p == 0 || isLocalHost(proxyHost) && (p == port || p == 8080)) |
+ if (p == 0 || isLocalHost(proxyHost) && (p == this.port || p == 8080)) |
{ |
- if (nativeProxyAutoConfigured) |
- passProxySettings(null, null, null); |
+ if (this.nativeProxyAutoConfigured) |
+ { |
+ this.passProxySettings(null, null, null); |
+ } |
return; |
} |
config.put("adblock.proxyHost", proxyHost); |
config.put("adblock.proxyPort", proxyPort); |
- if (!transparent) |
+ if (!this.transparent) |
{ |
config.put("https.proxyHost", proxyHost); |
config.put("https.proxyPort", proxyPort); |
@@ -466,50 +495,54 @@ public class ProxyService extends Service implements OnSharedPreferenceChangeLis |
// TODO Not implemented in our proxy but needed to restore settings |
if (proxyExcl != null) |
+ { |
config.put("adblock.proxyExcl", proxyExcl); |
+ } |
if (proxyUser != null && !"".equals(proxyUser) && proxyPass != null && !"".equals(proxyPass)) |
{ |
// Base64 encode user:password |
- String proxyAuth = "Basic " + new String(Base64.encode(proxyUser + ":" + proxyPass)); |
+ final String proxyAuth = "Basic " + new String(Base64.encode(proxyUser + ":" + proxyPass)); |
config.put("adblock.auth", proxyAuth); |
- if (!transparent) |
+ if (!this.transparent) |
+ { |
config.put("https.auth", proxyAuth); |
+ } |
} |
} |
- private void passProxySettings(String proxyHost, String proxyPort, String proxyExcl) |
+ private void passProxySettings(final String proxyHost, final String proxyPort, final String proxyExcl) |
{ |
try |
{ |
- CrashHandler handler = (CrashHandler) Thread.getDefaultUncaughtExceptionHandler(); |
+ final CrashHandler handler = (CrashHandler)Thread.getDefaultUncaughtExceptionHandler(); |
handler.saveProxySettings(proxyHost, proxyPort, proxyExcl); |
} |
- catch (ClassCastException e) |
+ catch (final ClassCastException e) |
{ |
// ignore - default handler in use |
} |
} |
@Override |
- public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) |
+ public void onSharedPreferenceChanged(final SharedPreferences sharedPreferences, final String key) |
{ |
if (!NATIVE_PROXY_SUPPORTED) |
{ |
- String ketHost = getString(R.string.pref_proxyhost); |
- String keyPort = getString(R.string.pref_proxyport); |
- String keyUser = getString(R.string.pref_proxyuser); |
- String keyPass = getString(R.string.pref_proxypass); |
+ final String ketHost = this.getString(R.string.pref_proxyhost); |
+ final String keyPort = this.getString(R.string.pref_proxyport); |
+ final String keyUser = this.getString(R.string.pref_proxyuser); |
+ final String keyPass = this.getString(R.string.pref_proxypass); |
if (key.equals(ketHost) || key.equals(keyPort) || key.equals(keyUser) || key.equals(keyPass)) |
{ |
- String proxyHost = sharedPreferences.getString(ketHost, null); |
- String proxyPort = sharedPreferences.getString(keyPort, null); |
- String proxyUser = sharedPreferences.getString(keyUser, null); |
- String proxyPass = sharedPreferences.getString(keyPass, null); |
- if (proxy != null) |
+ final String proxyHost = sharedPreferences.getString(ketHost, null); |
+ final String proxyPort = sharedPreferences.getString(keyPort, null); |
+ final String proxyUser = sharedPreferences.getString(keyUser, null); |
+ final String proxyPass = sharedPreferences.getString(keyPass, null); |
+ if (this.proxy != null) |
{ |
- configureUserProxy(proxyConfiguration, proxyHost, proxyPort, null, proxyUser, proxyPass); |
- proxy.restart(proxyConfiguration.getProperty("handler")); |
+ this.configureUserProxy(this.proxyConfiguration, proxyHost, proxyPort, null, proxyUser, proxyPass); |
+ this.proxy.restart(this.proxyConfiguration.getProperty("handler")); |
} |
} |
} |
@@ -517,12 +550,12 @@ public class ProxyService extends Service implements OnSharedPreferenceChangeLis |
public boolean isTransparent() |
{ |
- return transparent; |
+ return this.transparent; |
} |
public boolean isNativeProxyAutoConfigured() |
{ |
- return nativeProxyAutoConfigured; |
+ return this.nativeProxyAutoConfigured; |
} |
/** |
@@ -530,7 +563,7 @@ public class ProxyService extends Service implements OnSharedPreferenceChangeLis |
*/ |
public boolean isManual() |
{ |
- return !transparent && !nativeProxyAutoConfigured; |
+ return !this.transparent && !this.nativeProxyAutoConfigured; |
} |
/** |
@@ -538,34 +571,40 @@ public class ProxyService extends Service implements OnSharedPreferenceChangeLis |
*/ |
public boolean noTraffic() |
{ |
- return notrafficHandler != null; |
+ return this.notrafficHandler != null; |
} |
/** |
* Checks if specified host is local. |
*/ |
- private static final boolean isLocalHost(String host) |
+ private static final boolean isLocalHost(final String host) |
{ |
if (host == null) |
+ { |
return false; |
+ } |
try |
{ |
if (host.equalsIgnoreCase("localhost")) |
+ { |
return true; |
+ } |
- String className = "android.net.NetworkUtils"; |
- Class<?> c = Class.forName(className); |
+ final String className = "android.net.NetworkUtils"; |
+ final Class<?> c = Class.forName(className); |
/* |
* InetAddress address = NetworkUtils.numericToInetAddress(host); |
*/ |
- Method method = c.getMethod("numericToInetAddress", String.class); |
- InetAddress address = (InetAddress) method.invoke(null, host); |
+ final Method method = c.getMethod("numericToInetAddress", String.class); |
+ final InetAddress address = (InetAddress)method.invoke(null, host); |
if (address.isLoopbackAddress()) |
+ { |
return true; |
+ } |
} |
- catch (Exception e) |
+ catch (final Exception e) |
{ |
Log.w(TAG, null, e); |
} |
@@ -581,9 +620,11 @@ public class ProxyService extends Service implements OnSharedPreferenceChangeLis |
private void initIptables() throws IOException, RootToolsException, TimeoutException, FileNotFoundException |
{ |
if (!RootTools.isAccessGiven()) |
+ { |
throw new FileNotFoundException("No root access"); |
+ } |
- File ipt = getFileStreamPath("iptables"); |
+ final File ipt = this.getFileStreamPath("iptables"); |
if (!ipt.exists()) |
{ |
@@ -591,22 +632,26 @@ public class ProxyService extends Service implements OnSharedPreferenceChangeLis |
throw new FileNotFoundException("No iptables executable"); |
} |
- String path = ipt.getAbsolutePath(); |
+ final String path = ipt.getAbsolutePath(); |
RootTools.sendShell("chmod 700 " + path, DEFAULT_TIMEOUT); |
boolean compatible = false; |
boolean version = false; |
- String command = path + " --version\n" + path + " -L -t nat -n\n"; |
+ final String command = path + " --version\n" + path + " -L -t nat -n\n"; |
- List<String> result = RootTools.sendShell(command, DEFAULT_TIMEOUT); |
- for (String line : result) |
+ final List<String> result = RootTools.sendShell(command, DEFAULT_TIMEOUT); |
+ for (final String line : result) |
{ |
if (line.contains("OUTPUT")) |
+ { |
compatible = true; |
+ } |
if (line.contains("v1.4.")) |
+ { |
version = true; |
+ } |
} |
if (!compatible || !version) |
@@ -615,20 +660,22 @@ public class ProxyService extends Service implements OnSharedPreferenceChangeLis |
throw new FileNotFoundException("Incompatible iptables excutable"); |
} |
- iptables = path; |
+ this.iptables = path; |
} |
public List<String> getIptablesOutput() |
{ |
- if (iptables == null) |
+ if (this.iptables == null) |
+ { |
return null; |
+ } |
- String command = iptables + " -L -t nat -n\n"; |
+ final String command = this.iptables + " -L -t nat -n\n"; |
try |
{ |
return RootTools.sendShell(command, DEFAULT_TIMEOUT); |
} |
- catch (Exception e) |
+ catch (final Exception e) |
{ |
Log.e(TAG, "Failed to get iptables configuration", e); |
return null; |
@@ -639,17 +686,19 @@ public class ProxyService extends Service implements OnSharedPreferenceChangeLis |
* Raises or removes no traffic notification based on current link proxy |
* settings |
*/ |
- private void updateNoTrafficCheck(ConnectivityManager connectivityManager) |
+ private void updateNoTrafficCheck(final ConnectivityManager connectivityManager) |
{ |
try |
{ |
- Object pp = ProxySettings.getActiveLinkProxy(connectivityManager); |
- String[] userProxy = ProxySettings.getUserProxy(pp); |
+ final Object pp = ProxySettings.getActiveLinkProxy(connectivityManager); |
+ final String[] userProxy = ProxySettings.getUserProxy(pp); |
if (userProxy != null) |
+ { |
Log.i(TAG, "Proxy settings: " + userProxy[0] + ":" + userProxy[1] + "(" + userProxy[2] + ")"); |
- updateNoTrafficCheck(userProxy); |
+ } |
+ this.updateNoTrafficCheck(userProxy); |
} |
- catch (Exception e) |
+ catch (final Exception e) |
{ |
// This should not happen |
Log.e(TAG, null, e); |
@@ -659,26 +708,26 @@ public class ProxyService extends Service implements OnSharedPreferenceChangeLis |
/** |
* Raises or removes no traffic notification based on the user proxy settings |
*/ |
- private void updateNoTrafficCheck(String[] userProxy) |
+ private void updateNoTrafficCheck(final String[] userProxy) |
{ |
- boolean ourProxy = userProxy != null && isLocalHost(userProxy[0]) && Integer.valueOf(userProxy[1]) == port; |
- if (ourProxy != proxyManualyConfigured) |
+ final boolean ourProxy = userProxy != null && isLocalHost(userProxy[0]) && Integer.valueOf(userProxy[1]) == this.port; |
+ if (ourProxy != this.proxyManualyConfigured) |
{ |
- proxyManualyConfigured = ourProxy; |
- sendStateChangedBroadcast(); |
+ this.proxyManualyConfigured = ourProxy; |
+ this.sendStateChangedBroadcast(); |
} |
if (ourProxy) |
{ |
- stopNoTrafficCheck(); |
+ this.stopNoTrafficCheck(); |
} |
else |
{ |
// Initiate no traffic check |
- notrafficHandler = new Handler(); |
- notrafficHandler.postDelayed(noTraffic, NO_TRAFFIC_TIMEOUT); |
+ this.notrafficHandler = new Handler(); |
+ this.notrafficHandler.postDelayed(this.noTraffic, NO_TRAFFIC_TIMEOUT); |
} |
- NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); |
- notificationManager.notify(ONGOING_NOTIFICATION_ID, getNotification()); |
+ final NotificationManager notificationManager = (NotificationManager)this.getSystemService(NOTIFICATION_SERVICE); |
+ notificationManager.notify(ONGOING_NOTIFICATION_ID, this.getNotification()); |
} |
/** |
@@ -686,65 +735,73 @@ public class ProxyService extends Service implements OnSharedPreferenceChangeLis |
*/ |
private void stopNoTrafficCheck() |
{ |
- if (notrafficHandler != null) |
+ if (this.notrafficHandler != null) |
{ |
- notrafficHandler.removeCallbacks(noTraffic); |
- sendStateChangedBroadcast(); |
- NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); |
- notificationManager.notify(ONGOING_NOTIFICATION_ID, getNotification()); |
+ this.notrafficHandler.removeCallbacks(this.noTraffic); |
+ this.sendStateChangedBroadcast(); |
+ final NotificationManager notificationManager = (NotificationManager)this.getSystemService(NOTIFICATION_SERVICE); |
+ notificationManager.notify(ONGOING_NOTIFICATION_ID, this.getNotification()); |
notificationManager.cancel(NOTRAFFIC_NOTIFICATION_ID); |
} |
- notrafficHandler = null; |
+ this.notrafficHandler = null; |
} |
@SuppressLint("NewApi") |
private Notification getNotification() |
{ |
- boolean filtering = AdblockPlus.getApplication().isFilteringEnabled(); |
+ final boolean filtering = AdblockPlus.getApplication().isFilteringEnabled(); |
int msgId = R.string.notif_waiting; |
- if (nativeProxyAutoConfigured || proxyManualyConfigured) |
+ if (this.nativeProxyAutoConfigured || this.proxyManualyConfigured) |
+ { |
msgId = filtering ? R.string.notif_wifi : R.string.notif_wifi_nofiltering; |
- if (transparent) |
+ } |
+ if (this.transparent) |
+ { |
msgId = R.string.notif_all; |
+ } |
- NotificationCompat.Builder builder = new NotificationCompat.Builder(this); |
- if (hideIcon && msgId != R.string.notif_waiting) |
+ final NotificationCompat.Builder builder = new NotificationCompat.Builder(this); |
+ if (this.hideIcon && msgId != R.string.notif_waiting) |
{ |
builder.setWhen(POSITION_RIGHT); |
builder.setSmallIcon(R.drawable.transparent); |
- //builder.setContent(new RemoteViews(getPackageName(), R.layout.notif_hidden)); |
+ // builder.setContent(new RemoteViews(getPackageName(), |
+ // R.layout.notif_hidden)); |
} |
else |
{ |
builder.setWhen(0); |
builder.setSmallIcon(R.drawable.ic_stat_blocking); |
} |
- PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, Preferences.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK), 0); |
+ final PendingIntent contentIntent = PendingIntent.getActivity(this, 0, |
+ new Intent(this, Preferences.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK), 0); |
builder.setContentIntent(contentIntent); |
- builder.setContentTitle(getText(R.string.app_name)); |
- builder.setContentText(getString(msgId, port)); |
+ builder.setContentTitle(this.getText(R.string.app_name)); |
+ builder.setContentText(this.getString(msgId, this.port)); |
builder.setOngoing(true); |
- Notification notification = builder.getNotification(); |
+ final Notification notification = builder.getNotification(); |
return notification; |
} |
- public void setEmptyIcon(boolean hide) |
+ public void setEmptyIcon(final boolean hide) |
{ |
- hideIcon = hide; |
- NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); |
- notificationManager.notify(ONGOING_NOTIFICATION_ID, getNotification()); |
+ this.hideIcon = hide; |
+ final NotificationManager notificationManager = (NotificationManager)this.getSystemService(NOTIFICATION_SERVICE); |
+ notificationManager.notify(ONGOING_NOTIFICATION_ID, this.getNotification()); |
} |
public void sendStateChangedBroadcast() |
{ |
Log.i(TAG, "Broadcasting " + BROADCAST_STATE_CHANGED); |
- boolean manual = isManual(); |
- Intent stateIntent = new Intent(BROADCAST_STATE_CHANGED).putExtra("enabled", true).putExtra("port", port).putExtra("manual", manual); |
+ final boolean manual = this.isManual(); |
+ final Intent stateIntent = new Intent(BROADCAST_STATE_CHANGED).putExtra("enabled", true).putExtra("port", this.port).putExtra("manual", manual); |
if (manual) |
- stateIntent.putExtra("configured", proxyManualyConfigured); |
- sendBroadcast(stateIntent); |
+ { |
+ stateIntent.putExtra("configured", this.proxyManualyConfigured); |
+ } |
+ this.sendBroadcast(stateIntent); |
} |
private final IBinder binder = new LocalBinder(); |
@@ -758,34 +815,38 @@ public class ProxyService extends Service implements OnSharedPreferenceChangeLis |
} |
@Override |
- public IBinder onBind(Intent intent) |
+ public IBinder onBind(final Intent intent) |
{ |
- return binder; |
+ return this.binder; |
} |
/** |
* Executed if no traffic is detected after a period of time. Notifies user |
* about possible configuration problems. |
*/ |
- private Runnable noTraffic = new Runnable() |
+ private final Runnable noTraffic = new Runnable() |
{ |
+ @Override |
public void run() |
{ |
- // It's weird but notrafficHandler.removeCallbacks(noTraffic) does not remove this callback |
- if (notrafficHandler == null) |
+ // It's weird but notrafficHandler.removeCallbacks(noTraffic) does not |
+ // remove this callback |
+ if (ProxyService.this.notrafficHandler == null) |
+ { |
return; |
+ } |
// Show warning notification |
- NotificationCompat.Builder builder = new NotificationCompat.Builder(ProxyService.this); |
+ final NotificationCompat.Builder builder = new NotificationCompat.Builder(ProxyService.this); |
builder.setSmallIcon(R.drawable.ic_stat_warning); |
builder.setWhen(System.currentTimeMillis()); |
builder.setAutoCancel(true); |
- Intent intent = new Intent(ProxyService.this, ConfigurationActivity.class).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
- intent.putExtra("port", port); |
- PendingIntent contentIntent = PendingIntent.getActivity(ProxyService.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); |
+ final Intent intent = new Intent(ProxyService.this, ConfigurationActivity.class).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
+ intent.putExtra("port", ProxyService.this.port); |
+ final PendingIntent contentIntent = PendingIntent.getActivity(ProxyService.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); |
builder.setContentIntent(contentIntent); |
- builder.setContentTitle(getText(R.string.app_name)); |
- builder.setContentText(getText(R.string.notif_notraffic)); |
- NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); |
+ builder.setContentTitle(ProxyService.this.getText(R.string.app_name)); |
+ builder.setContentText(ProxyService.this.getText(R.string.notif_notraffic)); |
+ final NotificationManager notificationManager = (NotificationManager)ProxyService.this.getSystemService(NOTIFICATION_SERVICE); |
notificationManager.notify(NOTRAFFIC_NOTIFICATION_ID, builder.getNotification()); |
} |
}; |
@@ -793,10 +854,10 @@ public class ProxyService extends Service implements OnSharedPreferenceChangeLis |
/** |
* Stops no traffic check if traffic is detected by proxy service. |
*/ |
- private BroadcastReceiver filterReceiver = new BroadcastReceiver() |
+ private final BroadcastReceiver filterReceiver = new BroadcastReceiver() |
{ |
@Override |
- public void onReceive(final Context context, Intent intent) |
+ public void onReceive(final Context context, final Intent intent) |
{ |
if (intent.getAction().equals(AdblockPlus.BROADCAST_FILTERING_CHANGE)) |
{ |
@@ -806,14 +867,14 @@ public class ProxyService extends Service implements OnSharedPreferenceChangeLis |
// during removal. |
if (!ProxyService.this.isNativeProxyAutoConfigured()) |
{ |
- NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); |
- notificationManager.notify(ONGOING_NOTIFICATION_ID, getNotification()); |
+ final NotificationManager notificationManager = (NotificationManager)ProxyService.this.getSystemService(NOTIFICATION_SERVICE); |
+ notificationManager.notify(ONGOING_NOTIFICATION_ID, ProxyService.this.getNotification()); |
} |
} |
if (intent.getAction().equals(AdblockPlus.BROADCAST_FILTER_MATCHES)) |
{ |
- proxyManualyConfigured = true; |
- stopNoTrafficCheck(); |
+ ProxyService.this.proxyManualyConfigured = true; |
+ ProxyService.this.stopNoTrafficCheck(); |
} |
} |
}; |
@@ -821,14 +882,14 @@ public class ProxyService extends Service implements OnSharedPreferenceChangeLis |
/** |
* Stops service if proxy fails. |
*/ |
- private BroadcastReceiver proxyReceiver = new BroadcastReceiver() |
+ private final BroadcastReceiver proxyReceiver = new BroadcastReceiver() |
{ |
@Override |
- public void onReceive(final Context context, Intent intent) |
+ public void onReceive(final Context context, final Intent intent) |
{ |
if (intent.getAction().equals(ProxyService.BROADCAST_PROXY_FAILED)) |
{ |
- stopSelf(); |
+ ProxyService.this.stopSelf(); |
} |
} |
}; |
@@ -837,64 +898,67 @@ public class ProxyService extends Service implements OnSharedPreferenceChangeLis |
* Monitors system network connection settings changes and updates proxy |
* settings accordingly. |
*/ |
- private BroadcastReceiver connectionReceiver = new BroadcastReceiver() |
+ private final BroadcastReceiver connectionReceiver = new BroadcastReceiver() |
{ |
@Override |
- public void onReceive(Context ctx, Intent intent) |
+ public void onReceive(final Context ctx, final Intent intent) |
{ |
- String action = intent.getAction(); |
+ final String action = intent.getAction(); |
Log.i(TAG, "Action: " + action); |
// Connectivity change |
if (ConnectivityManager.CONNECTIVITY_ACTION.equals(action)) |
{ |
- ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); |
- // TODO Should we use ConnectivityManagerCompat.getNetworkInfoFromBroadcast() instead? |
- NetworkInfo info = connectivityManager.getActiveNetworkInfo(); |
+ final ConnectivityManager connectivityManager = (ConnectivityManager)ProxyService.this.getSystemService(Context.CONNECTIVITY_SERVICE); |
+ // TODO Should we use |
+ // ConnectivityManagerCompat.getNetworkInfoFromBroadcast() instead? |
+ final NetworkInfo info = connectivityManager.getActiveNetworkInfo(); |
if (info == null) |
+ { |
return; |
- String typeName = info.getTypeName(); |
- String subtypeName = info.getSubtypeName(); |
- boolean available = info.isAvailable(); |
+ } |
+ final String typeName = info.getTypeName(); |
+ final String subtypeName = info.getSubtypeName(); |
+ final boolean available = info.isAvailable(); |
Log.i(TAG, "Network Type: " + typeName + ", subtype: " + subtypeName + ", available: " + available); |
if (info.getType() == ConnectivityManager.TYPE_WIFI) |
{ |
- if (nativeProxyAutoConfigured) |
+ if (ProxyService.this.nativeProxyAutoConfigured) |
{ |
- ProxySettings.setConnectionProxy(getApplicationContext(), LOCALHOST, port, ""); |
+ ProxySettings.setConnectionProxy(ProxyService.this.getApplicationContext(), LOCALHOST, ProxyService.this.port, ""); |
} |
else |
{ |
- updateNoTrafficCheck(connectivityManager); |
+ ProxyService.this.updateNoTrafficCheck(connectivityManager); |
} |
} |
} |
// Proxy change |
else if (Proxy.PROXY_CHANGE_ACTION.equals(action)) |
{ |
- Object pp = intent.getParcelableExtra("proxy"); |
+ final Object pp = intent.getParcelableExtra("proxy"); |
try |
{ |
- String[] userProxy = ProxySettings.getUserProxy(pp); |
- if (nativeProxyAutoConfigured) |
+ final String[] userProxy = ProxySettings.getUserProxy(pp); |
+ if (ProxyService.this.nativeProxyAutoConfigured) |
{ |
- if (userProxy != null && Integer.valueOf(userProxy[1]) != port) |
+ if (userProxy != null && Integer.valueOf(userProxy[1]) != ProxyService.this.port) |
{ |
Log.i(TAG, "User has set new proxy: " + userProxy[0] + ":" + userProxy[1] + "(" + userProxy[2] + ")"); |
- if (proxy != null) |
+ if (ProxyService.this.proxy != null) |
{ |
- configureUserProxy(proxyConfiguration, userProxy[0], userProxy[1], userProxy[2], null, null); |
- proxy.restart(proxyConfiguration.getProperty("handler")); |
+ ProxyService.this.configureUserProxy(ProxyService.this.proxyConfiguration, userProxy[0], userProxy[1], userProxy[2], null, null); |
+ ProxyService.this.proxy.restart(ProxyService.this.proxyConfiguration.getProperty("handler")); |
} |
} |
} |
else |
{ |
Log.i(TAG, "User has set proxy: " + userProxy[0] + ":" + userProxy[1] + "(" + userProxy[2] + ")"); |
- updateNoTrafficCheck(userProxy); |
+ ProxyService.this.updateNoTrafficCheck(userProxy); |
} |
} |
- catch (Exception e) |
+ catch (final Exception e) |
{ |
// This should not happen |
Log.e(TAG, null, e); |
@@ -910,21 +974,21 @@ public class ProxyService extends Service implements OnSharedPreferenceChangeLis |
{ |
try |
{ |
- listen.close(); |
+ this.listen.close(); |
this.interrupt(); |
this.join(); |
} |
- catch (Exception e) |
+ catch (final Exception e) |
{ |
// ignore - it always happens |
} |
- log(LOG_WARNING, null, "server stopped"); |
+ this.log(LOG_WARNING, null, "server stopped"); |
} |
@Override |
- public void log(int level, Object obj, String message) |
+ public void log(final int level, final Object obj, final String message) |
{ |
- if (level <= logLevel) |
+ if (level <= this.logLevel) |
{ |
Log.println(7 - level, obj != null ? obj.toString() : TAG, message); |
} |