Search in sources :

Example 1 with DhcpInfo

use of android.net.DhcpInfo in project XPrivacy by M66B.

the class XWifiManager method after.

@Override
@SuppressWarnings("rawtypes")
protected void after(XParam param) throws Throwable {
    switch(mMethod) {
        case Srv_getBatchedScanResults:
            if (param.getResult() != null)
                if (isRestricted(param))
                    param.setResult(new ArrayList());
            break;
        case getConfiguredNetworks:
        case Srv_getConfiguredNetworks:
            if (param.getResult() != null)
                if (isRestricted(param))
                    param.setResult(new ArrayList<WifiConfiguration>());
            break;
        case getConnectionInfo:
        case Srv_getConnectionInfo:
            if (param.getResult() != null)
                if (isRestricted(param)) {
                    WifiInfo result = (WifiInfo) param.getResult();
                    WifiInfo wInfo = WifiInfo.class.getConstructor(WifiInfo.class).newInstance(result);
                    if (getRestrictionName().equals(PrivacyManager.cInternet)) {
                        // Supplicant state
                        try {
                            Field fieldState = WifiInfo.class.getDeclaredField("mSupplicantState");
                            fieldState.setAccessible(true);
                            fieldState.set(wInfo, SupplicantState.DISCONNECTED);
                        } catch (Throwable ex) {
                            Util.bug(this, ex);
                        }
                    } else {
                        // BSSID
                        try {
                            Field fieldBSSID = WifiInfo.class.getDeclaredField("mBSSID");
                            fieldBSSID.setAccessible(true);
                            fieldBSSID.set(wInfo, PrivacyManager.getDefacedProp(Binder.getCallingUid(), "MAC"));
                        } catch (Throwable ex) {
                            Util.bug(this, ex);
                        }
                        // IP address
                        try {
                            Field fieldIp = WifiInfo.class.getDeclaredField("mIpAddress");
                            fieldIp.setAccessible(true);
                            fieldIp.set(wInfo, PrivacyManager.getDefacedProp(Binder.getCallingUid(), "InetAddress"));
                        } catch (Throwable ex) {
                            Util.bug(this, ex);
                        }
                        // MAC address
                        try {
                            Field fieldMAC = WifiInfo.class.getDeclaredField("mMacAddress");
                            fieldMAC.setAccessible(true);
                            fieldMAC.set(wInfo, PrivacyManager.getDefacedProp(Binder.getCallingUid(), "MAC"));
                        } catch (Throwable ex) {
                            Util.bug(this, ex);
                        }
                        // SSID
                        String ssid = (String) PrivacyManager.getDefacedProp(Binder.getCallingUid(), "SSID");
                        try {
                            Field fieldSSID = WifiInfo.class.getDeclaredField("mSSID");
                            fieldSSID.setAccessible(true);
                            fieldSSID.set(wInfo, ssid);
                        } catch (Throwable ex) {
                            try {
                                Field fieldWifiSsid = WifiInfo.class.getDeclaredField("mWifiSsid");
                                fieldWifiSsid.setAccessible(true);
                                Object mWifiSsid = fieldWifiSsid.get(wInfo);
                                if (mWifiSsid != null) {
                                    // public static WifiSsid
                                    // createFromAsciiEncoded(String
                                    // asciiEncoded)
                                    Method methodCreateFromAsciiEncoded = mWifiSsid.getClass().getDeclaredMethod("createFromAsciiEncoded", String.class);
                                    fieldWifiSsid.set(wInfo, methodCreateFromAsciiEncoded.invoke(null, ssid));
                                }
                            } catch (Throwable exex) {
                                Util.bug(this, exex);
                            }
                        }
                    }
                    param.setResult(wInfo);
                }
            break;
        case getDhcpInfo:
        case Srv_getDhcpInfo:
            if (param.getResult() != null)
                if (isRestricted(param)) {
                    DhcpInfo result = (DhcpInfo) param.getResult();
                    DhcpInfo dInfo = DhcpInfo.class.getConstructor(DhcpInfo.class).newInstance(result);
                    dInfo.ipAddress = (Integer) PrivacyManager.getDefacedProp(Binder.getCallingUid(), "IPInt");
                    dInfo.gateway = dInfo.ipAddress;
                    dInfo.dns1 = dInfo.ipAddress;
                    dInfo.dns2 = dInfo.ipAddress;
                    dInfo.serverAddress = dInfo.ipAddress;
                    param.setResult(dInfo);
                }
            break;
        case getScanResults:
        case Srv_getScanResults:
            if (param.getResult() != null)
                if (isRestricted(param))
                    param.setResult(new ArrayList<ScanResult>());
            break;
        case getWifiApConfiguration:
        case Srv_getWifiApConfiguration:
            if (param.getResult() != null)
                if (isRestricted(param))
                    param.setResult(null);
            break;
    }
}
Also used : ScanResult(android.net.wifi.ScanResult) WifiConfiguration(android.net.wifi.WifiConfiguration) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) DhcpInfo(android.net.DhcpInfo) WifiInfo(android.net.wifi.WifiInfo) Field(java.lang.reflect.Field)

Example 2 with DhcpInfo

use of android.net.DhcpInfo in project robolectric by robolectric.

the class ShadowWifiManagerTest method shouldReturnDhcpInfo.

@Test
public void shouldReturnDhcpInfo() {
    DhcpInfo dhcpInfo = new DhcpInfo();
    shadowWifiManager.setDhcpInfo(dhcpInfo);
    assertThat(wifiManager.getDhcpInfo()).isSameAs(dhcpInfo);
}
Also used : DhcpInfo(android.net.DhcpInfo) Test(org.junit.Test)

Example 3 with DhcpInfo

use of android.net.DhcpInfo in project android_frameworks_base by ParanoidAndroid.

the class WifiService method getDhcpInfo.

/**
     * Return the DHCP-assigned addresses from the last successful DHCP request,
     * if any.
     * @return the DHCP information
     * @deprecated
     */
public DhcpInfo getDhcpInfo() {
    enforceAccessPermission();
    DhcpResults dhcpResults = mWifiStateMachine.syncGetDhcpResults();
    if (dhcpResults.linkProperties == null)
        return null;
    DhcpInfo info = new DhcpInfo();
    for (LinkAddress la : dhcpResults.linkProperties.getLinkAddresses()) {
        InetAddress addr = la.getAddress();
        if (addr instanceof Inet4Address) {
            info.ipAddress = NetworkUtils.inetAddressToInt((Inet4Address) addr);
            break;
        }
    }
    for (RouteInfo r : dhcpResults.linkProperties.getRoutes()) {
        if (r.isDefaultRoute()) {
            InetAddress gateway = r.getGateway();
            if (gateway instanceof Inet4Address) {
                info.gateway = NetworkUtils.inetAddressToInt((Inet4Address) gateway);
            }
        } else if (r.hasGateway() == false) {
            LinkAddress dest = r.getDestination();
            if (dest.getAddress() instanceof Inet4Address) {
                info.netmask = NetworkUtils.prefixLengthToNetmaskInt(dest.getNetworkPrefixLength());
            }
        }
    }
    int dnsFound = 0;
    for (InetAddress dns : dhcpResults.linkProperties.getDnses()) {
        if (dns instanceof Inet4Address) {
            if (dnsFound == 0) {
                info.dns1 = NetworkUtils.inetAddressToInt((Inet4Address) dns);
            } else {
                info.dns2 = NetworkUtils.inetAddressToInt((Inet4Address) dns);
            }
            if (++dnsFound > 1)
                break;
        }
    }
    InetAddress serverAddress = dhcpResults.serverAddress;
    if (serverAddress instanceof Inet4Address) {
        info.serverAddress = NetworkUtils.inetAddressToInt((Inet4Address) serverAddress);
    }
    info.leaseDuration = dhcpResults.leaseDuration;
    return info;
}
Also used : LinkAddress(android.net.LinkAddress) Inet4Address(java.net.Inet4Address) DhcpResults(android.net.DhcpResults) DhcpInfo(android.net.DhcpInfo) RouteInfo(android.net.RouteInfo) InetAddress(java.net.InetAddress)

Example 4 with DhcpInfo

use of android.net.DhcpInfo in project dobby-android by InceptAi.

the class WifiAnalyzer method processNetworkStateChangedIntent.

private void processNetworkStateChangedIntent(Intent intent) {
    final NetworkInfo networkInfo = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
    if (networkInfo != null) {
        NetworkInfo.DetailedState detailedWifiState = networkInfo.getDetailedState();
        updateWifiStatsDetailedState(detailedWifiState);
    }
    boolean wasConnected = wifiConnected;
    wifiConnected = networkInfo != null && networkInfo.isConnected();
    //If no longer connected, clear the connection info
    if (!wifiConnected) {
        wifiState.clearWifiConnectionInfo();
    }
    // If we just connected, grab the initial signal strength and SSID
    if (wifiConnected && !wasConnected) {
        postToEventBus(DobbyEvent.EventType.WIFI_CONNECTED);
        // try getting it out of the intent first
        WifiInfo info = (WifiInfo) intent.getParcelableExtra(WifiManager.EXTRA_WIFI_INFO);
        updateWifiStatsWithWifiInfo(info);
        DhcpInfo dhcpInfo = getDhcpInfo();
        if (dhcpInfo != null && dhcpInfo.ipAddress != 0) {
            eventBus.postEvent(new DobbyEvent(DobbyEvent.EventType.DHCP_INFO_AVAILABLE));
        }
    } else if (!wifiConnected && wasConnected) {
        postToEventBus(DobbyEvent.EventType.WIFI_NOT_CONNECTED);
    } else {
        if (wifiConnected) {
            if (!publishedWifiState) {
                postToEventBus(DobbyEvent.EventType.WIFI_CONNECTED);
            }
            DobbyLog.v("No change in wifi state -- we were connected and are connected");
        } else {
            //So that we publish this event at least once
            if (!publishedWifiState) {
                postToEventBus(DobbyEvent.EventType.WIFI_NOT_CONNECTED);
            }
            DobbyLog.v("No change in wifi state -- we were NOT connected and are still NOT connected");
        }
    }
}
Also used : NetworkInfo(android.net.NetworkInfo) DhcpInfo(android.net.DhcpInfo) DobbyEvent(com.inceptai.dobby.eventbus.DobbyEvent) WifiInfo(android.net.wifi.WifiInfo) DobbyWifiInfo(com.inceptai.dobby.model.DobbyWifiInfo)

Example 5 with DhcpInfo

use of android.net.DhcpInfo in project Resurrection_packages_apps_Settings by ResurrectionRemix.

the class ConfigureWifiSettings method refreshWifiInfo.

private void refreshWifiInfo() {
    final Context context = getActivity();
    WifiInfo wifiInfo = mWifiManager.getConnectionInfo();
    Preference wifiMacAddressPref = findPreference(KEY_MAC_ADDRESS);
    String macAddress = wifiInfo == null ? null : wifiInfo.getMacAddress();
    wifiMacAddressPref.setSummary(!TextUtils.isEmpty(macAddress) ? macAddress : context.getString(R.string.status_unavailable));
    wifiMacAddressPref.setSelectable(false);
    Preference wifiIpAddressPref = findPreference(KEY_CURRENT_IP_ADDRESS);
    String ipAddress = Utils.getWifiIpAddresses(context);
    wifiIpAddressPref.setSummary(ipAddress == null ? context.getString(R.string.status_unavailable) : ipAddress);
    wifiIpAddressPref.setSelectable(false);
    // Wifi extension requirement
    Preference wifiGatewayPref = findPreference(KEY_CURRENT_GATEWAY);
    String gateway = null;
    Preference wifiNetmaskPref = findPreference(KEY_CURRENT_NETMASK);
    String netmask = null;
    if (getResources().getBoolean(R.bool.config_netinfo)) {
        DhcpInfo dhcpInfo = mWifiManager.getDhcpInfo();
        if (wifiInfo != null) {
            if (dhcpInfo != null) {
                gateway = Formatter.formatIpAddress(dhcpInfo.gateway);
                netmask = Formatter.formatIpAddress(dhcpInfo.netmask);
            }
        }
        if (wifiGatewayPref != null) {
            wifiGatewayPref.setSummary((gateway == null || dhcpInfo.gateway == 0) ? getString(R.string.status_unavailable) : gateway);
        }
        if (wifiNetmaskPref != null) {
            wifiNetmaskPref.setSummary((netmask == null || dhcpInfo.netmask == 0) ? getString(R.string.status_unavailable) : netmask);
        }
    } else {
        if (wifiGatewayPref != null) {
            getPreferenceScreen().removePreference(wifiGatewayPref);
        }
        if (wifiNetmaskPref != null) {
            getPreferenceScreen().removePreference(wifiNetmaskPref);
        }
    }
}
Also used : Context(android.content.Context) ListPreference(android.support.v7.preference.ListPreference) Preference(android.support.v7.preference.Preference) AppListSwitchPreference(com.android.settings.AppListSwitchPreference) SwitchPreference(android.support.v14.preference.SwitchPreference) DhcpInfo(android.net.DhcpInfo) WifiInfo(android.net.wifi.WifiInfo)

Aggregations

DhcpInfo (android.net.DhcpInfo)6 WifiInfo (android.net.wifi.WifiInfo)3 Context (android.content.Context)1 DhcpResults (android.net.DhcpResults)1 LinkAddress (android.net.LinkAddress)1 NetworkInfo (android.net.NetworkInfo)1 RouteInfo (android.net.RouteInfo)1 ScanResult (android.net.wifi.ScanResult)1 WifiConfiguration (android.net.wifi.WifiConfiguration)1 WifiManager (android.net.wifi.WifiManager)1 SwitchPreference (android.support.v14.preference.SwitchPreference)1 ListPreference (android.support.v7.preference.ListPreference)1 Preference (android.support.v7.preference.Preference)1 AppListSwitchPreference (com.android.settings.AppListSwitchPreference)1 DobbyEvent (com.inceptai.dobby.eventbus.DobbyEvent)1 DobbyWifiInfo (com.inceptai.dobby.model.DobbyWifiInfo)1 Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1 Inet4Address (java.net.Inet4Address)1 InetAddress (java.net.InetAddress)1