+
+ public Proxy getCurrentProxy(Context context) {
+ // Define a proxy variable.
+ Proxy proxy;
+
+ // Get the proxy according to the current proxy mode.
+ switch (MainWebViewActivity.proxyMode) {
+ case (ProxyHelper.TOR):
+ if (Build.VERSION.SDK_INT >= 21) {
+ // Use localhost port 9050 as the socket address.
+ SocketAddress torSocketAddress = new InetSocketAddress("localhost", 9050);
+
+ // Create a SOCKS proxy.
+ proxy = new Proxy(Proxy.Type.SOCKS, torSocketAddress);
+ } else {
+ // Use localhost port 8118 as the socket address.
+ SocketAddress oldTorSocketAddress = new InetSocketAddress("localhost", 8118);
+
+ // Create an HTTP proxy.
+ proxy = new Proxy(Proxy.Type.HTTP, oldTorSocketAddress);
+ }
+ break;
+
+ case (ProxyHelper.I2P):
+ // Use localhost port 4444 as the socket address.
+ SocketAddress i2pSocketAddress = new InetSocketAddress("localhost", 4444);
+
+ // Create an HTTP proxy.
+ proxy = new Proxy(Proxy.Type.HTTP, i2pSocketAddress);
+ break;
+
+ case (ProxyHelper.CUSTOM):
+ // Get the shared preferences.
+ SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
+
+ // Get the custom proxy URL string.
+ String customProxyUrlString = sharedPreferences.getString("proxy_custom_url", context.getString(R.string.proxy_custom_url_default_value));
+
+ // Parse the custom proxy URL.
+ try {
+ // Convert the custom proxy URL string to a URI.
+ Uri customProxyUri = Uri.parse(customProxyUrlString);
+
+ // Get the custom socket address.
+ SocketAddress customSocketAddress = new InetSocketAddress(customProxyUri.getHost(), customProxyUri.getPort());
+
+ // Get the custom proxy scheme.
+ String customProxyScheme = customProxyUri.getScheme();
+
+ // Create a proxy according to the scheme.
+ if ((customProxyScheme != null) && customProxyScheme.startsWith("socks")) { // A SOCKS proxy is specified.
+ // Create a SOCKS proxy.
+ proxy = new Proxy(Proxy.Type.SOCKS, customSocketAddress);
+ } else { // A SOCKS proxy is not specified.
+ // Create an HTTP proxy.
+ proxy = new Proxy(Proxy.Type.HTTP, customSocketAddress);
+ }
+ } catch (Exception exception) { // The custom proxy cannot be parsed.
+ // Disable the proxy.
+ proxy = Proxy.NO_PROXY;
+ }
+ break;
+
+ default: // No proxy is in use.
+ // Create a direct proxy.
+ proxy = Proxy.NO_PROXY;
+ break;
+ }
+
+ // Return the proxy.
+ return proxy;
+ }