summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcyBerta <cyberta@riseup.net>2020-01-03 02:24:57 +0100
committercyberta <cyberta@riseup.net>2020-01-24 10:38:35 -0600
commit01e2f7e8f76248c791bd61429687da7c2cb584b9 (patch)
treee9661dd6ee534159b12eef5ca73d3205f493e0b1
parentf0ec974d143556e4729ac21c7fcf6a1f1a3687df (diff)
implement simple detection for usb tethering
-rw-r--r--app/src/main/java/se/leap/bitmaskclient/BitmaskApp.java4
-rw-r--r--app/src/main/java/se/leap/bitmaskclient/fragments/TetheringDialog.java9
-rw-r--r--app/src/main/java/se/leap/bitmaskclient/tethering/TetheringBroadcastReceiver.java (renamed from app/src/main/java/se/leap/bitmaskclient/tethering/WifiHotspotBroadcastReceiver.java)8
-rw-r--r--app/src/main/java/se/leap/bitmaskclient/tethering/TetheringObserver.java92
-rw-r--r--app/src/main/java/se/leap/bitmaskclient/tethering/WifiHotspotObserver.java55
5 files changed, 104 insertions, 64 deletions
diff --git a/app/src/main/java/se/leap/bitmaskclient/BitmaskApp.java b/app/src/main/java/se/leap/bitmaskclient/BitmaskApp.java
index 6a657d33..83691802 100644
--- a/app/src/main/java/se/leap/bitmaskclient/BitmaskApp.java
+++ b/app/src/main/java/se/leap/bitmaskclient/BitmaskApp.java
@@ -8,7 +8,7 @@ import android.support.v7.app.AppCompatDelegate;
import com.squareup.leakcanary.LeakCanary;
import com.squareup.leakcanary.RefWatcher;
-import se.leap.bitmaskclient.tethering.WifiHotspotObserver;
+import se.leap.bitmaskclient.tethering.TetheringObserver;
import static se.leap.bitmaskclient.Constants.SHARED_PREFERENCES;
import static se.leap.bitmaskclient.utils.PreferenceHelper.getSavedProviderFromSharedPreferences;
@@ -40,7 +40,7 @@ public class BitmaskApp extends MultiDexApplication {
providerObservable.updateProvider(getSavedProviderFromSharedPreferences(preferences));
EipSetupObserver.init(this, preferences);
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
- WifiHotspotObserver.init(this);
+ TetheringObserver.init(this);
}
/**
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 04d3071f..65287afe 100644
--- a/app/src/main/java/se/leap/bitmaskclient/fragments/TetheringDialog.java
+++ b/app/src/main/java/se/leap/bitmaskclient/fragments/TetheringDialog.java
@@ -23,7 +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.tethering.TetheringObserver;
import se.leap.bitmaskclient.utils.PreferenceHelper;
import se.leap.bitmaskclient.views.IconCheckboxEntry;
@@ -142,7 +142,8 @@ public class TetheringDialog extends AppCompatDialogFragment {
@Override
public void onResume() {
super.onResume();
- dataset[0].enabled = WifiHotspotObserver.getInstance().isEnabled();
+ dataset[0].enabled = TetheringObserver.getInstance().isWifiTetheringEnabled();
+ dataset[1].enabled = TetheringObserver.getInstance().isUsbTetheringEnabled();
adapter.notifyDataSetChanged();
}
@@ -175,11 +176,11 @@ public class TetheringDialog extends AppCompatDialogFragment {
new DialogListAdapter.ViewModel(getContext().getResources().getDrawable(R.drawable.ic_wifi),
getContext().getString(R.string.tethering_wifi),
PreferenceHelper.getWifiTethering(getContext()),
- WifiHotspotObserver.getInstance().isEnabled()),
+ TetheringObserver.getInstance().isWifiTetheringEnabled()),
new DialogListAdapter.ViewModel(getContext().getResources().getDrawable(R.drawable.ic_usb),
getContext().getString(R.string.tethering_usb),
PreferenceHelper.getUsbTethering(getContext()),
- true),
+ TetheringObserver.getInstance().isUsbTetheringEnabled()),
new DialogListAdapter.ViewModel(getContext().getResources().getDrawable(R.drawable.ic_bluetooth),
getContext().getString(R.string.tethering_bluetooth),
PreferenceHelper.getBluetoothTethering(getContext()),
diff --git a/app/src/main/java/se/leap/bitmaskclient/tethering/WifiHotspotBroadcastReceiver.java b/app/src/main/java/se/leap/bitmaskclient/tethering/TetheringBroadcastReceiver.java
index 1d81eff5..c5840171 100644
--- a/app/src/main/java/se/leap/bitmaskclient/tethering/WifiHotspotBroadcastReceiver.java
+++ b/app/src/main/java/se/leap/bitmaskclient/tethering/TetheringBroadcastReceiver.java
@@ -5,16 +5,18 @@ import android.content.Context;
import android.content.Intent;
import android.net.wifi.WifiManager;
-public class WifiHotspotBroadcastReceiver extends BroadcastReceiver {
+public class TetheringBroadcastReceiver 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);
+ TetheringObserver.getInstance().setWifiTetheringEnabled(true);
} else {
- WifiHotspotObserver.getInstance().setEnabled(false);
+ TetheringObserver.getInstance().setWifiTetheringEnabled(false);
}
+ } else if ("android.net.conn.TETHER_STATE_CHANGED".equals(intent.getAction())) {
+ TetheringObserver.getInstance().updateUsbTetheringState();
}
}
}
diff --git a/app/src/main/java/se/leap/bitmaskclient/tethering/TetheringObserver.java b/app/src/main/java/se/leap/bitmaskclient/tethering/TetheringObserver.java
new file mode 100644
index 00000000..1d1fa951
--- /dev/null
+++ b/app/src/main/java/se/leap/bitmaskclient/tethering/TetheringObserver.java
@@ -0,0 +1,92 @@
+package se.leap.bitmaskclient.tethering;
+
+import android.content.Context;
+import android.content.IntentFilter;
+import android.net.ConnectivityManager;
+import android.net.wifi.WifiManager;
+
+import java.lang.reflect.Method;
+import java.net.InetAddress;
+import java.net.NetworkInterface;
+import java.util.Enumeration;
+
+public class TetheringObserver {
+ private static TetheringObserver instance;
+
+ private boolean isWifiTetheringEnabled;
+ private boolean isUsbTetheringEnabled;
+ private WifiManager wifiManager;
+ private ConnectivityManager connectivityManager;
+ private TetheringBroadcastReceiver broadcastReceiver;
+
+ private TetheringObserver() {
+ }
+
+ public static void init(Context context) {
+ if (instance == null) {
+ instance = new TetheringObserver();
+ instance.broadcastReceiver = new TetheringBroadcastReceiver();
+ IntentFilter intentFilter = new IntentFilter("android.net.wifi.WIFI_AP_STATE_CHANGED");
+ intentFilter.addAction("android.net.conn.TETHER_STATE_CHANGED");
+ context.getApplicationContext().registerReceiver(instance.broadcastReceiver, intentFilter);
+ instance.wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
+ instance.setWifiTetheringEnabled(instance.isWifiApEnabled());
+ instance.connectivityManager = (ConnectivityManager) context.getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
+ }
+ }
+
+ 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;
+ }
+ }
+
+ private boolean getUsbTetheringState() {
+ try {
+
+ for(Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
+ NetworkInterface networkInterface = en.nextElement();
+ for(Enumeration<InetAddress> enumIpAddr = networkInterface.getInetAddresses(); enumIpAddr.hasMoreElements();){
+ InetAddress inetAddress = enumIpAddr.nextElement();
+ if(!networkInterface.isLoopback()){
+ if(networkInterface.getName().contains("rndis") || networkInterface.getName().contains("usb")){
+ return true;
+ }
+ }
+ }
+ }
+ } catch(Exception e){
+ e.printStackTrace();
+ }
+
+ return false;
+ }
+
+ public static TetheringObserver getInstance() {
+ if (instance == null) {
+ throw new RuntimeException("Call init() first!");
+ }
+
+ return instance;
+ }
+
+ void setWifiTetheringEnabled(boolean enabled) {
+ isWifiTetheringEnabled = enabled;
+ }
+
+ public boolean isWifiTetheringEnabled() {
+ return isWifiTetheringEnabled;
+ }
+
+ void updateUsbTetheringState() {
+ isUsbTetheringEnabled = getUsbTetheringState();
+ }
+
+ public boolean isUsbTetheringEnabled() {
+ return isUsbTetheringEnabled;
+ }
+}
diff --git a/app/src/main/java/se/leap/bitmaskclient/tethering/WifiHotspotObserver.java b/app/src/main/java/se/leap/bitmaskclient/tethering/WifiHotspotObserver.java
deleted file mode 100644
index 578c5552..00000000
--- a/app/src/main/java/se/leap/bitmaskclient/tethering/WifiHotspotObserver.java
+++ /dev/null
@@ -1,55 +0,0 @@
-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;
- }
-}