summaryrefslogtreecommitdiff
path: root/app/src/main/java/se/leap/bitmaskclient/tethering/TetheringStateManager.java
blob: 1e2521b820aa1cd1ddd93563eb9d07e654cc07a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package se.leap.bitmaskclient.tethering;

import android.content.Context;
import android.content.IntentFilter;
import android.net.wifi.WifiManager;

import java.lang.reflect.Method;
import java.net.NetworkInterface;
import java.util.Enumeration;

import se.leap.bitmaskclient.utils.Cmd;

public class TetheringStateManager {
    private static final String TAG = TetheringStateManager.class.getSimpleName();
    private static TetheringStateManager instance;

    private WifiManager wifiManager;

    private TetheringStateManager() { }

    public static TetheringStateManager getInstance() {
        if (instance == null) {
            instance = new TetheringStateManager();
        }
        return instance;
    }

    public void init(Context context) {
        TetheringBroadcastReceiver broadcastReceiver = new TetheringBroadcastReceiver();
        IntentFilter intentFilter = new IntentFilter("android.net.conn.TETHER_STATE_CHANGED");
        intentFilter.addAction("android.net.wifi.WIFI_AP_STATE_CHANGED");
        context.getApplicationContext().registerReceiver(broadcastReceiver, intentFilter);
        instance.wifiManager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        updateWifiTetheringState();
        updateUsbTetheringState();
        updateBluetoothTetheringState();
    }

    private static boolean isWifiApEnabled() {
        try {
            Method method = instance.wifiManager.getClass().getMethod("getWifiApState");
            int tmp = ((Integer) method.invoke(instance.wifiManager));
            return WifiHotspotState.WIFI_AP_STATE_ENABLED.ordinal() == tmp % 10;
        } catch (Exception e) {
            return false;
        }
    }


    private static boolean getUsbTetheringState() {
        try {
            for(Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
                NetworkInterface networkInterface = en.nextElement();
                if(!networkInterface.isLoopback()){
                    if(networkInterface.getName().contains("rndis") || networkInterface.getName().contains("usb")){
                        return true;
                    }
                }
            }
        } catch(Exception e){
            e.printStackTrace();
        }

        return false;
    }

    // Check whether Bluetooth tethering is enabled.
    private static boolean isBluetoothTetheringEnabled() {
        StringBuilder log = new StringBuilder();
        boolean hasBtPan = false;
        try {
            hasBtPan = Cmd.runBlockingCmd(new String[] {"ifconfig bt-pan"}, log) == 0;
            //Log.d(TAG, "ifconfig result: " + log.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return hasBtPan;

    }

    static void updateUsbTetheringState() {
        TetheringObservable.setUsbTethering(getUsbTetheringState());
    }

    static void updateBluetoothTetheringState() {
        TetheringObservable.setBluetoothTethering(isBluetoothTetheringEnabled());
    }

    static void updateWifiTetheringState() {
        TetheringObservable.setWifiTethering(isWifiApEnabled());
    }

}