diff options
author | cyBerta <cyberta@riseup.net> | 2020-01-03 01:01:56 +0100 |
---|---|---|
committer | cyberta <cyberta@riseup.net> | 2020-01-24 10:38:35 -0600 |
commit | f0ec974d143556e4729ac21c7fcf6a1f1a3687df (patch) | |
tree | 74154f471b1ede8987bf6efd9c25ba6cf5291ff6 /app/src/main/java/se/leap/bitmaskclient/tethering | |
parent | 2c4d5096453dfe5dfe5f1b2067930de3e6962ea3 (diff) |
detect hotspot state and disable/enable controls in TetheringDialog accordingly
Diffstat (limited to 'app/src/main/java/se/leap/bitmaskclient/tethering')
3 files changed, 84 insertions, 0 deletions
diff --git a/app/src/main/java/se/leap/bitmaskclient/tethering/WifiHotspotBroadcastReceiver.java b/app/src/main/java/se/leap/bitmaskclient/tethering/WifiHotspotBroadcastReceiver.java new file mode 100644 index 00000000..1d81eff5 --- /dev/null +++ b/app/src/main/java/se/leap/bitmaskclient/tethering/WifiHotspotBroadcastReceiver.java @@ -0,0 +1,20 @@ +package se.leap.bitmaskclient.tethering; + +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.net.wifi.WifiManager; + +public class WifiHotspotBroadcastReceiver extends BroadcastReceiver { + @Override + public void onReceive(Context context, Intent intent) { + if ("android.net.wifi.WIFI_AP_STATE_CHANGED".equals(intent.getAction())) { + int apState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, 0); + if (WifiHotspotState.WIFI_AP_STATE_ENABLED.ordinal() == apState % 10) { + WifiHotspotObserver.getInstance().setEnabled(true); + } else { + WifiHotspotObserver.getInstance().setEnabled(false); + } + } + } +} 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; + } +} diff --git a/app/src/main/java/se/leap/bitmaskclient/tethering/WifiHotspotState.java b/app/src/main/java/se/leap/bitmaskclient/tethering/WifiHotspotState.java new file mode 100644 index 00000000..9cd7f793 --- /dev/null +++ b/app/src/main/java/se/leap/bitmaskclient/tethering/WifiHotspotState.java @@ -0,0 +1,9 @@ +package se.leap.bitmaskclient.tethering; + +public enum WifiHotspotState { + WIFI_AP_STATE_DISABLING, + WIFI_AP_STATE_DISABLED, + WIFI_AP_STATE_ENABLING, + WIFI_AP_STATE_ENABLED, + WIFI_AP_STATE_FAILED +} |