]> gitweb.stoutner.com Git - PrivacyBrowserAndroid.git/blob - app/src/main/java/com/stoutner/privacybrowser/helpers/OrbotProxyHelper.java
Create a dark theme for `MainWebViewActivity`.
[PrivacyBrowserAndroid.git] / app / src / main / java / com / stoutner / privacybrowser / helpers / OrbotProxyHelper.java
1 /*
2  * Copyright © 2016-2017 Soren Stoutner <soren@stoutner.com>.
3  *
4  * This file is part of Privacy Browser <https://www.stoutner.com/privacy-browser>.
5  *
6  * Privacy Browser is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * Privacy Browser is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Privacy Browser.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 package com.stoutner.privacybrowser.helpers;
21
22 import android.app.Activity;
23 import android.content.Context;
24 import android.content.DialogInterface;
25 import android.content.Intent;
26 import android.content.pm.PackageManager;
27 import android.net.Proxy;
28 import android.support.v7.app.AlertDialog;
29 import android.util.ArrayMap;
30 import android.util.Log;
31
32 import com.stoutner.privacybrowser.activities.MainWebViewActivity;
33 import com.stoutner.privacybrowser.R;
34
35 import java.lang.reflect.Field;
36 import java.lang.reflect.InvocationTargetException;
37 import java.lang.reflect.Method;
38
39 public class OrbotProxyHelper {
40     public static void setProxy(Context privacyBrowserContext, Activity parentActivity, String proxyHost, String proxyPort) {
41
42         // Set the proxy values
43         System.setProperty("http.proxyHost", proxyHost);
44         System.setProperty("http.proxyPort", proxyPort);
45         System.setProperty("https.proxyHost", proxyHost);
46         System.setProperty("https.proxyPort", proxyPort);
47
48         // Use reflection to apply the new proxy values.
49         try {
50             Class applicationClass = Class.forName("android.app.Application");
51             Field mLoadedApkField = applicationClass.getDeclaredField("mLoadedApk");
52             // `setAccessible(true)` allows us to change the value of `mLoadedApkField`.
53             mLoadedApkField.setAccessible(true);
54             Object mLoadedApkObject = mLoadedApkField.get(privacyBrowserContext);
55
56             Class loadedApkClass = Class.forName("android.app.LoadedApk");
57             Field mReceiversField = loadedApkClass.getDeclaredField("mReceivers");
58             // `setAccessible(true)` allows us to change the value of `mReceiversField`.
59             mReceiversField.setAccessible(true);
60
61             ArrayMap receivers = (ArrayMap) mReceiversField.get(mLoadedApkObject);
62
63             for (Object receiverMap : receivers.values()) {
64                 for (Object receiver : ((ArrayMap) receiverMap).keySet()) {
65                     // We have to use `Class<?>`, which is an `unbounded wildcard parameterized type`, instead of `Class`, which is a `raw type`, or `receiveClass.getDeclaredMethod` below will produce an error.
66                     Class<?> receiverClass = receiver.getClass();
67                     if (receiverClass.getName().contains("ProxyChangeListener")) {
68                         Method onReceiveMethod = receiverClass.getDeclaredMethod("onReceive", Context.class, Intent.class);
69                         Intent intent = new Intent(Proxy.PROXY_CHANGE_ACTION);
70                         onReceiveMethod.invoke(receiver, privacyBrowserContext, intent);
71                     }
72                 }
73             }
74         } catch (ClassNotFoundException | NoSuchFieldException | IllegalAccessException | NoSuchMethodException | InvocationTargetException exception) {
75             Log.d("enableProxyThroughOrbot", "Exception: " + exception);
76         }
77
78         if (proxyPort.equals("8118")) {  // Orbot proxy was turned on.
79             try {  // Check to see if Orbot is installed.
80                 PackageManager packageManager = privacyBrowserContext.getPackageManager();
81                 packageManager.getPackageInfo("org.torproject.android", PackageManager.GET_ACTIVITIES);
82
83                 // Ask Orbot to connect if its current status is not "ON".
84                 if (!MainWebViewActivity.orbotStatus.equals("ON")) {
85                     // Request Orbot to start.
86                     Intent orbotIntent = new Intent("org.torproject.android.intent.action.START");
87
88                     // Send the intent to the Orbot package.
89                     orbotIntent.setPackage("org.torproject.android");
90
91                     // Request a status response be sent back to this package.
92                     orbotIntent.putExtra("org.torproject.android.intent.extra.PACKAGE_NAME", privacyBrowserContext.getPackageName());
93
94                     // Make it so.
95                     privacyBrowserContext.sendBroadcast(orbotIntent);
96                 }
97             } catch (PackageManager.NameNotFoundException exception){  // If an exception is thrown, Orbot is not installed.
98                 // Build an `AlertDialog`.  `R.style.LightAlertDialog` formats the color of the button text.
99                 AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(parentActivity, R.style.LightAlertDialog);
100                 dialogBuilder.setMessage(R.string.orbot_proxy_not_installed);
101                 dialogBuilder.setPositiveButton(R.string.close, new DialogInterface.OnClickListener() {
102                     @Override
103                     public void onClick(DialogInterface dialog, int which) {
104                         // Do nothing.  The `AlertDialog` will close automatically.
105                     }
106                 });
107
108                 // Convert `dialogBuilder` to `alertDialog` and display it on the screen.
109                 AlertDialog alertDialog = dialogBuilder.create();
110                 alertDialog.show();
111             }
112         }
113     }
114 }