From f0ec974d143556e4729ac21c7fcf6a1f1a3687df Mon Sep 17 00:00:00 2001 From: cyBerta Date: Fri, 3 Jan 2020 01:01:56 +0100 Subject: detect hotspot state and disable/enable controls in TetheringDialog accordingly --- .../tethering/WifiHotspotObserver.java | 55 ++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 app/src/main/java/se/leap/bitmaskclient/tethering/WifiHotspotObserver.java (limited to 'app/src/main/java/se/leap/bitmaskclient/tethering/WifiHotspotObserver.java') diff --git a/app/src/main/java/se/leap/bitmaskclient/tethering/WifiHotspotObserver.java b/app/src/main/java/se/leap/bitmaskclient/tethering/WifiHotspotObserver.java new file mode 100644 index 00000000..578c5552 --- /dev/null +++ b/app/src/main/java/se/leap/bitmaskclient/tethering/WifiHotspotObserver.java @@ -0,0 +1,55 @@ +package se.leap.bitmaskclient.tethering; + +import android.content.Context; +import android.content.IntentFilter; +import android.net.wifi.WifiManager; + +import java.lang.reflect.Method; + +public class WifiHotspotObserver { + private static WifiHotspotObserver instance; + + private boolean isEnabled; + private WifiManager wifiManager; + private WifiHotspotBroadcastReceiver broadcastReceiver; + + private WifiHotspotObserver() { + } + + public static void init(Context context) { + if (instance == null) { + instance = new WifiHotspotObserver(); + instance.broadcastReceiver = new WifiHotspotBroadcastReceiver(); + IntentFilter intentFilter = new IntentFilter("android.net.wifi.WIFI_AP_STATE_CHANGED"); + context.getApplicationContext().registerReceiver(instance.broadcastReceiver, intentFilter); + instance.wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE); + instance.setEnabled(instance.isWifiApEnabled()); + } + } + + private boolean isWifiApEnabled() { + try { + Method method = instance.wifiManager.getClass().getMethod("getWifiApState"); + int tmp = ((Integer) method.invoke(wifiManager)); + return WifiHotspotState.WIFI_AP_STATE_ENABLED.ordinal() == tmp % 10; + } catch (Exception e) { + return false; + } + } + + public static WifiHotspotObserver getInstance() { + if (instance == null) { + throw new RuntimeException("Call init() first!"); + } + + return instance; + } + + void setEnabled(boolean enabled) { + isEnabled = enabled; + } + + public boolean isEnabled() { + return isEnabled; + } +} -- cgit v1.2.3