|
| 1 | +package it.pgp.currenttoggles.utils.oreoap; |
| 2 | + |
| 3 | +import android.content.Context; |
| 4 | +import android.net.ConnectivityManager; |
| 5 | +import android.net.wifi.WifiConfiguration; |
| 6 | +import android.net.wifi.WifiManager; |
| 7 | +import android.os.Handler; |
| 8 | +import android.util.Log; |
| 9 | + |
| 10 | +import com.android.dx.stock.ProxyBuilder; |
| 11 | + |
| 12 | +import java.io.File; |
| 13 | +import java.lang.reflect.Method; |
| 14 | +import java.util.Arrays; |
| 15 | + |
| 16 | +// Source: https://github.com/aegis1980/WifiHotSpot |
| 17 | + |
| 18 | +// @RequiresApi(api = Build.VERSION_CODES.O) |
| 19 | +public class MyOreoWifiManager { |
| 20 | + private static final String TAG = MyOreoWifiManager.class.getSimpleName(); |
| 21 | + |
| 22 | + private Context mContext; |
| 23 | + private WifiManager mWifiManager; |
| 24 | + private ConnectivityManager mConnectivityManager; |
| 25 | + |
| 26 | + public MyOreoWifiManager(Context c) { |
| 27 | + mContext = c; |
| 28 | + mWifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE); |
| 29 | + mConnectivityManager = (ConnectivityManager) mContext.getSystemService(ConnectivityManager.class); |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * This sets the Wifi SSID and password |
| 34 | + * Call this before {@code startTethering} if app is a system/privileged app |
| 35 | + * Requires: android.permission.TETHER_PRIVILEGED which is only granted to system apps |
| 36 | + */ |
| 37 | + public void configureHotspot(String name, String password) { |
| 38 | + WifiConfiguration apConfig = new WifiConfiguration(); |
| 39 | + apConfig.SSID = name; |
| 40 | + apConfig.preSharedKey = password; |
| 41 | + apConfig.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK); |
| 42 | + try { |
| 43 | + Method setConfigMethod = mWifiManager.getClass().getMethod("setWifiApConfiguration", WifiConfiguration.class); |
| 44 | + boolean status = (boolean) setConfigMethod.invoke(mWifiManager, apConfig); |
| 45 | + Log.d(TAG, "setWifiApConfiguration - success? " + status); |
| 46 | + } catch (Exception e) { |
| 47 | + Log.e(TAG, "Error in configureHotspot"); |
| 48 | + e.printStackTrace(); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Checks where tethering is on. |
| 54 | + * This is determined by the getTetheredIfaces() method, |
| 55 | + * that will return an empty array if not devices are tethered |
| 56 | + * |
| 57 | + * @return true if a tethered device is found, false if not found |
| 58 | + */ |
| 59 | + public boolean isTetherActive() { |
| 60 | + try { |
| 61 | + Method method = mConnectivityManager.getClass().getDeclaredMethod("getTetheredIfaces"); |
| 62 | + if (method == null) { |
| 63 | + Log.e(TAG, "getTetheredIfaces is null"); |
| 64 | + } else { |
| 65 | + String res[] = (String []) method.invoke(mConnectivityManager, null); |
| 66 | + Log.d(TAG, "getTetheredIfaces invoked"); |
| 67 | + Log.d(TAG, Arrays.toString(res)); |
| 68 | + if (res.length > 0) { |
| 69 | + return true; |
| 70 | + } |
| 71 | + } |
| 72 | + } catch (Exception e) { |
| 73 | + Log.e(TAG, "Error in getTetheredIfaces"); |
| 74 | + e.printStackTrace(); |
| 75 | + } |
| 76 | + return false; |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * This enables tethering using the ssid/password defined in Settings App>Hotspot & tethering |
| 81 | + * Does not require app to have system/privileged access |
| 82 | + * Credit: Vishal Sharma - https://stackoverflow.com/a/52219887 |
| 83 | + */ |
| 84 | + public boolean startTethering(final MyOnStartTetheringCallback callback) { |
| 85 | + |
| 86 | + // On Pie if we try to start tethering while it is already on, it will |
| 87 | + // be disabled. This is needed when startTethering() is called programmatically. |
| 88 | + if (isTetherActive()) { |
| 89 | + Log.d(TAG, "Tether already active, returning"); |
| 90 | + return false; |
| 91 | + } |
| 92 | + |
| 93 | + File outputDir = mContext.getCodeCacheDir(); |
| 94 | + Object proxy; |
| 95 | + try { |
| 96 | + proxy = ProxyBuilder.forClass(OnStartTetheringCallbackClass()) |
| 97 | + .dexCache(outputDir).handler((proxy1, method, args) -> { |
| 98 | + switch (method.getName()) { |
| 99 | + case "onTetheringStarted": |
| 100 | + callback.onTetheringStarted(); |
| 101 | + break; |
| 102 | + case "onTetheringFailed": |
| 103 | + callback.onTetheringFailed(); |
| 104 | + break; |
| 105 | + default: |
| 106 | + ProxyBuilder.callSuper(proxy1, method, args); |
| 107 | + } |
| 108 | + return null; |
| 109 | + }).build(); |
| 110 | + } catch (Exception e) { |
| 111 | + Log.e(TAG, "Error in enableTethering ProxyBuilder"); |
| 112 | + e.printStackTrace(); |
| 113 | + return false; |
| 114 | + } |
| 115 | + |
| 116 | + Method method; |
| 117 | + try { |
| 118 | + method = mConnectivityManager.getClass().getDeclaredMethod("startTethering", int.class, boolean.class, OnStartTetheringCallbackClass(), Handler.class); |
| 119 | + if (method == null) { |
| 120 | + Log.e(TAG, "startTetheringMethod is null"); |
| 121 | + } else { |
| 122 | + method.invoke(mConnectivityManager, ConnectivityManager.TYPE_MOBILE, false, proxy, null); |
| 123 | + Log.d(TAG, "startTethering invoked"); |
| 124 | + } |
| 125 | + return true; |
| 126 | + } catch (Exception e) { |
| 127 | + Log.e(TAG, "Error in enableTethering"); |
| 128 | + e.printStackTrace(); |
| 129 | + } |
| 130 | + return false; |
| 131 | + } |
| 132 | + |
| 133 | + public void stopTethering() { |
| 134 | + try { |
| 135 | + Method method = mConnectivityManager.getClass().getDeclaredMethod("stopTethering", int.class); |
| 136 | + if (method == null) { |
| 137 | + Log.e(TAG, "stopTetheringMethod is null"); |
| 138 | + } else { |
| 139 | + method.invoke(mConnectivityManager, ConnectivityManager.TYPE_MOBILE); |
| 140 | + Log.d(TAG, "stopTethering invoked"); |
| 141 | + } |
| 142 | + } catch (Exception e) { |
| 143 | + Log.e(TAG, "stopTethering error: " + e.toString()); |
| 144 | + e.printStackTrace(); |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + private Class OnStartTetheringCallbackClass() { |
| 149 | + try { |
| 150 | + return Class.forName("android.net.ConnectivityManager$OnStartTetheringCallback"); |
| 151 | + } catch (ClassNotFoundException e) { |
| 152 | + Log.e(TAG, "OnStartTetheringCallbackClass error: " + e.toString()); |
| 153 | + e.printStackTrace(); |
| 154 | + } |
| 155 | + return null; |
| 156 | + } |
| 157 | +} |
| 158 | + |
0 commit comments