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 --- app/src/main/AndroidManifest.xml | 1 + .../java/se/leap/bitmaskclient/BitmaskApp.java | 3 ++ .../se/leap/bitmaskclient/EipSetupObserver.java | 1 - .../bitmaskclient/fragments/TetheringDialog.java | 21 +++++++-- .../tethering/WifiHotspotBroadcastReceiver.java | 20 ++++++++ .../tethering/WifiHotspotObserver.java | 55 ++++++++++++++++++++++ .../bitmaskclient/tethering/WifiHotspotState.java | 9 ++++ .../bitmaskclient/views/IconCheckboxEntry.java | 15 ++++-- 8 files changed, 117 insertions(+), 8 deletions(-) create mode 100644 app/src/main/java/se/leap/bitmaskclient/tethering/WifiHotspotBroadcastReceiver.java create mode 100644 app/src/main/java/se/leap/bitmaskclient/tethering/WifiHotspotObserver.java create mode 100644 app/src/main/java/se/leap/bitmaskclient/tethering/WifiHotspotState.java (limited to 'app') diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 9e8c0d98..83394209 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -20,6 +20,7 @@ + diff --git a/app/src/main/java/se/leap/bitmaskclient/BitmaskApp.java b/app/src/main/java/se/leap/bitmaskclient/BitmaskApp.java index 45664653..6a657d33 100644 --- a/app/src/main/java/se/leap/bitmaskclient/BitmaskApp.java +++ b/app/src/main/java/se/leap/bitmaskclient/BitmaskApp.java @@ -8,6 +8,8 @@ import android.support.v7.app.AppCompatDelegate; import com.squareup.leakcanary.LeakCanary; import com.squareup.leakcanary.RefWatcher; +import se.leap.bitmaskclient.tethering.WifiHotspotObserver; + import static se.leap.bitmaskclient.Constants.SHARED_PREFERENCES; import static se.leap.bitmaskclient.utils.PreferenceHelper.getSavedProviderFromSharedPreferences; @@ -38,6 +40,7 @@ public class BitmaskApp extends MultiDexApplication { providerObservable.updateProvider(getSavedProviderFromSharedPreferences(preferences)); EipSetupObserver.init(this, preferences); AppCompatDelegate.setCompatVectorFromResourcesEnabled(true); + WifiHotspotObserver.init(this); } /** diff --git a/app/src/main/java/se/leap/bitmaskclient/EipSetupObserver.java b/app/src/main/java/se/leap/bitmaskclient/EipSetupObserver.java index 74f132b9..84516ee3 100644 --- a/app/src/main/java/se/leap/bitmaskclient/EipSetupObserver.java +++ b/app/src/main/java/se/leap/bitmaskclient/EipSetupObserver.java @@ -44,7 +44,6 @@ import static se.leap.bitmaskclient.Constants.PROVIDER_KEY; import static se.leap.bitmaskclient.Constants.PROVIDER_PROFILE; import static se.leap.bitmaskclient.ProviderAPI.CORRECTLY_DOWNLOADED_EIP_SERVICE; import static se.leap.bitmaskclient.ProviderAPI.CORRECTLY_UPDATED_INVALID_VPN_CERTIFICATE; -import static se.leap.bitmaskclient.ProviderAPI.PROVIDER_NOK; /** * Created by cyberta on 05.12.18. diff --git a/app/src/main/java/se/leap/bitmaskclient/fragments/TetheringDialog.java b/app/src/main/java/se/leap/bitmaskclient/fragments/TetheringDialog.java index 145264d5..04d3071f 100644 --- a/app/src/main/java/se/leap/bitmaskclient/fragments/TetheringDialog.java +++ b/app/src/main/java/se/leap/bitmaskclient/fragments/TetheringDialog.java @@ -23,6 +23,7 @@ import butterknife.ButterKnife; import butterknife.InjectView; import se.leap.bitmaskclient.R; import se.leap.bitmaskclient.eip.EipCommand; +import se.leap.bitmaskclient.tethering.WifiHotspotObserver; import se.leap.bitmaskclient.utils.PreferenceHelper; import se.leap.bitmaskclient.views.IconCheckboxEntry; @@ -81,11 +82,13 @@ public class TetheringDialog extends AppCompatDialogFragment { public Drawable image; public String text; public boolean checked; + public boolean enabled; - ViewModel(Drawable image, String text, boolean checked) { + ViewModel(Drawable image, String text, boolean checked, boolean enabled) { this.image = image; this.text = text; this.checked = checked; + this.enabled = enabled; } } @@ -136,6 +139,13 @@ public class TetheringDialog extends AppCompatDialogFragment { return builder.create(); } + @Override + public void onResume() { + super.onResume(); + dataset[0].enabled = WifiHotspotObserver.getInstance().isEnabled(); + adapter.notifyDataSetChanged(); + } + public void onItemClick(DialogListAdapter.ViewModel item) { } @@ -164,13 +174,16 @@ public class TetheringDialog extends AppCompatDialogFragment { dataset = new DialogListAdapter.ViewModel[] { new DialogListAdapter.ViewModel(getContext().getResources().getDrawable(R.drawable.ic_wifi), getContext().getString(R.string.tethering_wifi), - PreferenceHelper.getWifiTethering(getContext())), + PreferenceHelper.getWifiTethering(getContext()), + WifiHotspotObserver.getInstance().isEnabled()), new DialogListAdapter.ViewModel(getContext().getResources().getDrawable(R.drawable.ic_usb), getContext().getString(R.string.tethering_usb), - PreferenceHelper.getUsbTethering(getContext())), + PreferenceHelper.getUsbTethering(getContext()), + true), new DialogListAdapter.ViewModel(getContext().getResources().getDrawable(R.drawable.ic_bluetooth), getContext().getString(R.string.tethering_bluetooth), - PreferenceHelper.getBluetoothTethering(getContext())) + PreferenceHelper.getBluetoothTethering(getContext()), + true) }; } 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 +} diff --git a/app/src/main/java/se/leap/bitmaskclient/views/IconCheckboxEntry.java b/app/src/main/java/se/leap/bitmaskclient/views/IconCheckboxEntry.java index 933d391b..ca44592e 100644 --- a/app/src/main/java/se/leap/bitmaskclient/views/IconCheckboxEntry.java +++ b/app/src/main/java/se/leap/bitmaskclient/views/IconCheckboxEntry.java @@ -57,15 +57,24 @@ public class IconCheckboxEntry extends LinearLayout { View rootview = inflater.inflate(R.layout.v_icon_select_text_list_item, this, true); ButterKnife.inject(this, rootview); - Drawable checkIcon = DrawableCompat.wrap(getResources().getDrawable(R.drawable.ic_check_bold)); - DrawableCompat.setTint(checkIcon, ContextCompat.getColor(getContext(), R.color.colorSuccess)); - checkedIcon.setImageDrawable(checkIcon); + } public void bind(TetheringDialog.DialogListAdapter.ViewModel model) { + this.setEnabled(model.enabled); textView.setText(model.text); + textView.setEnabled(model.enabled); + + Drawable checkIcon = DrawableCompat.wrap(getResources().getDrawable(R.drawable.ic_check_bold)).mutate(); + if (model.enabled) { + DrawableCompat.setTint(checkIcon, ContextCompat.getColor(getContext(), R.color.colorSuccess)); + } else { + DrawableCompat.setTint(checkIcon, ContextCompat.getColor(getContext(), R.color.colorDisabled)); + } + iconView.setImageDrawable(model.image); + checkedIcon.setImageDrawable(checkIcon); setChecked(model.checked); } -- cgit v1.2.3