Search in sources :

Example 31 with WifiInfo

use of android.net.wifi.WifiInfo in project android_frameworks_base by DirtyUnicorns.

the class DevicePolicyManagerTest method testGetMacAddress.

public void testGetMacAddress() throws Exception {
    mContext.callerPermissions.add(permission.MANAGE_DEVICE_ADMINS);
    mContext.callerPermissions.add(permission.MANAGE_PROFILE_AND_DEVICE_OWNERS);
    mContext.callerPermissions.add(permission.INTERACT_ACROSS_USERS_FULL);
    // In this test, change the caller user to "system".
    mContext.binder.callingUid = DpmMockContext.CALLER_SYSTEM_USER_UID;
    // Make sure admin1 is installed on system user.
    setUpPackageManagerForAdmin(admin1, DpmMockContext.CALLER_SYSTEM_USER_UID);
    // Test 1. Caller doesn't have DO or DA.
    try {
        dpm.getWifiMacAddress(admin1);
        fail();
    } catch (SecurityException e) {
        MoreAsserts.assertContainsRegex("No active admin", e.getMessage());
    }
    // DO needs to be an DA.
    dpm.setActiveAdmin(admin1, /* replace =*/
    false);
    assertTrue(dpm.isAdminActive(admin1));
    // Test 2. Caller has DA, but not DO.
    try {
        dpm.getWifiMacAddress(admin1);
        fail();
    } catch (SecurityException e) {
        MoreAsserts.assertContainsRegex("does not own the device", e.getMessage());
    }
    // Test 3. Caller has PO, but not DO.
    assertTrue(dpm.setProfileOwner(admin1, null, UserHandle.USER_SYSTEM));
    try {
        dpm.getWifiMacAddress(admin1);
        fail();
    } catch (SecurityException e) {
        MoreAsserts.assertContainsRegex("does not own the device", e.getMessage());
    }
    // Remove PO.
    dpm.clearProfileOwner(admin1);
    dpm.setActiveAdmin(admin1, false);
    // Test 4, Caller is DO now.
    assertTrue(dpm.setDeviceOwner(admin1, null, UserHandle.USER_SYSTEM));
    // 4-1.  But no WifiInfo.
    assertNull(dpm.getWifiMacAddress(admin1));
    // 4-2.  Returns WifiInfo, but with the default MAC.
    when(mContext.wifiManager.getConnectionInfo()).thenReturn(new WifiInfo());
    assertNull(dpm.getWifiMacAddress(admin1));
    // 4-3. With a real MAC address.
    final WifiInfo wi = new WifiInfo();
    wi.setMacAddress("11:22:33:44:55:66");
    when(mContext.wifiManager.getConnectionInfo()).thenReturn(wi);
    assertEquals("11:22:33:44:55:66", dpm.getWifiMacAddress(admin1));
}
Also used : WifiInfo(android.net.wifi.WifiInfo)

Example 32 with WifiInfo

use of android.net.wifi.WifiInfo in project android_frameworks_base by DirtyUnicorns.

the class AccessPointTest method testOnAccessPointChangedCallback.

public void testOnAccessPointChangedCallback() {
    WifiInfo wifiInfo = Mockito.mock(WifiInfo.class);
    Mockito.when(wifiInfo.getNetworkId()).thenReturn(NETWORK_ID);
    mAccessPoint.update(wifiInfo, null);
    verifyOnAccessPointsCallback(1);
    mAccessPoint.update(null, null);
    verifyOnAccessPointsCallback(2);
    ScanResult result = new ScanResult();
    result.capabilities = "";
    result.SSID = TEST_SSID;
    mAccessPoint.update(result);
    verifyOnAccessPointsCallback(3);
}
Also used : ScanResult(android.net.wifi.ScanResult) WifiInfo(android.net.wifi.WifiInfo)

Example 33 with WifiInfo

use of android.net.wifi.WifiInfo in project PhotoNoter by yydcdut.

the class PhoneUtils method getMacAddress.

public static String getMacAddress(Context var0) {
    try {
        WifiManager var1 = (WifiManager) var0.getSystemService(Context.WIFI_SERVICE);
        if (checkPermission(var0, "android.permission.ACCESS_WIFI_STATE")) {
            WifiInfo var2 = var1.getConnectionInfo();
            return var2.getMacAddress();
        }
        YLog.e(TAG, "Could not get mac address.[no permission android.permission.ACCESS_WIFI_STATE");
    } catch (Exception var3) {
        YLog.e(TAG, "Could not get mac address." + var3.toString());
    }
    return "";
}
Also used : WifiManager(android.net.wifi.WifiManager) WifiInfo(android.net.wifi.WifiInfo) JSONException(org.json.JSONException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Example 34 with WifiInfo

use of android.net.wifi.WifiInfo in project android_frameworks_base by DirtyUnicorns.

the class NetworkMonitor method sendNetworkConditionsBroadcast.

/**
     * @param responseReceived - whether or not we received a valid HTTP response to our request.
     * If false, isCaptivePortal and responseTimestampMs are ignored
     * TODO: This should be moved to the transports.  The latency could be passed to the transports
     * along with the captive portal result.  Currently the TYPE_MOBILE broadcasts appear unused so
     * perhaps this could just be added to the WiFi transport only.
     */
private void sendNetworkConditionsBroadcast(boolean responseReceived, boolean isCaptivePortal, long requestTimestampMs, long responseTimestampMs) {
    if (Settings.Global.getInt(mContext.getContentResolver(), Settings.Global.WIFI_SCAN_ALWAYS_AVAILABLE, 0) == 0) {
        return;
    }
    if (systemReady == false)
        return;
    Intent latencyBroadcast = new Intent(ACTION_NETWORK_CONDITIONS_MEASURED);
    switch(mNetworkAgentInfo.networkInfo.getType()) {
        case ConnectivityManager.TYPE_WIFI:
            WifiInfo currentWifiInfo = mWifiManager.getConnectionInfo();
            if (currentWifiInfo != null) {
                // NOTE: getSSID()'s behavior changed in API 17; before that, SSIDs were not
                // surrounded by double quotation marks (thus violating the Javadoc), but this
                // was changed to match the Javadoc in API 17. Since clients may have started
                // sanitizing the output of this method since API 17 was released, we should
                // not change it here as it would become impossible to tell whether the SSID is
                // simply being surrounded by quotes due to the API, or whether those quotes
                // are actually part of the SSID.
                latencyBroadcast.putExtra(EXTRA_SSID, currentWifiInfo.getSSID());
                latencyBroadcast.putExtra(EXTRA_BSSID, currentWifiInfo.getBSSID());
            } else {
                if (VDBG)
                    logw("network info is TYPE_WIFI but no ConnectionInfo found");
                return;
            }
            break;
        case ConnectivityManager.TYPE_MOBILE:
            latencyBroadcast.putExtra(EXTRA_NETWORK_TYPE, mTelephonyManager.getNetworkType());
            List<CellInfo> info = mTelephonyManager.getAllCellInfo();
            if (info == null)
                return;
            int numRegisteredCellInfo = 0;
            for (CellInfo cellInfo : info) {
                if (cellInfo.isRegistered()) {
                    numRegisteredCellInfo++;
                    if (numRegisteredCellInfo > 1) {
                        if (VDBG)
                            logw("more than one registered CellInfo." + " Can't tell which is active.  Bailing.");
                        return;
                    }
                    if (cellInfo instanceof CellInfoCdma) {
                        CellIdentityCdma cellId = ((CellInfoCdma) cellInfo).getCellIdentity();
                        latencyBroadcast.putExtra(EXTRA_CELL_ID, cellId);
                    } else if (cellInfo instanceof CellInfoGsm) {
                        CellIdentityGsm cellId = ((CellInfoGsm) cellInfo).getCellIdentity();
                        latencyBroadcast.putExtra(EXTRA_CELL_ID, cellId);
                    } else if (cellInfo instanceof CellInfoLte) {
                        CellIdentityLte cellId = ((CellInfoLte) cellInfo).getCellIdentity();
                        latencyBroadcast.putExtra(EXTRA_CELL_ID, cellId);
                    } else if (cellInfo instanceof CellInfoWcdma) {
                        CellIdentityWcdma cellId = ((CellInfoWcdma) cellInfo).getCellIdentity();
                        latencyBroadcast.putExtra(EXTRA_CELL_ID, cellId);
                    } else {
                        if (VDBG)
                            logw("Registered cellinfo is unrecognized");
                        return;
                    }
                }
            }
            break;
        default:
            return;
    }
    latencyBroadcast.putExtra(EXTRA_CONNECTIVITY_TYPE, mNetworkAgentInfo.networkInfo.getType());
    latencyBroadcast.putExtra(EXTRA_RESPONSE_RECEIVED, responseReceived);
    latencyBroadcast.putExtra(EXTRA_REQUEST_TIMESTAMP_MS, requestTimestampMs);
    if (responseReceived) {
        latencyBroadcast.putExtra(EXTRA_IS_CAPTIVE_PORTAL, isCaptivePortal);
        latencyBroadcast.putExtra(EXTRA_RESPONSE_TIMESTAMP_MS, responseTimestampMs);
    }
    mContext.sendBroadcastAsUser(latencyBroadcast, UserHandle.CURRENT, PERMISSION_ACCESS_NETWORK_CONDITIONS);
}
Also used : CellInfoCdma(android.telephony.CellInfoCdma) CellInfoWcdma(android.telephony.CellInfoWcdma) CellIdentityGsm(android.telephony.CellIdentityGsm) PendingIntent(android.app.PendingIntent) Intent(android.content.Intent) WifiInfo(android.net.wifi.WifiInfo) CellInfoGsm(android.telephony.CellInfoGsm) CellInfoLte(android.telephony.CellInfoLte) CellIdentityCdma(android.telephony.CellIdentityCdma) CellInfo(android.telephony.CellInfo) CellIdentityLte(android.telephony.CellIdentityLte) CellIdentityWcdma(android.telephony.CellIdentityWcdma)

Example 35 with WifiInfo

use of android.net.wifi.WifiInfo in project android_frameworks_base by AOSPA.

the class NetworkControllerWifiTest method setWifiState.

protected void setWifiState(boolean connected, String ssid) {
    Intent i = new Intent(WifiManager.NETWORK_STATE_CHANGED_ACTION);
    NetworkInfo networkInfo = Mockito.mock(NetworkInfo.class);
    Mockito.when(networkInfo.isConnected()).thenReturn(connected);
    WifiInfo wifiInfo = Mockito.mock(WifiInfo.class);
    Mockito.when(wifiInfo.getSSID()).thenReturn(ssid);
    i.putExtra(WifiManager.EXTRA_NETWORK_INFO, networkInfo);
    i.putExtra(WifiManager.EXTRA_WIFI_INFO, wifiInfo);
    mNetworkController.onReceive(mContext, i);
}
Also used : NetworkInfo(android.net.NetworkInfo) Intent(android.content.Intent) WifiInfo(android.net.wifi.WifiInfo)

Aggregations

WifiInfo (android.net.wifi.WifiInfo)99 WifiManager (android.net.wifi.WifiManager)53 WifiConfiguration (android.net.wifi.WifiConfiguration)16 NetworkInfo (android.net.NetworkInfo)13 Intent (android.content.Intent)11 IOException (java.io.IOException)8 Test (org.junit.Test)8 Bundle (android.os.Bundle)7 ScanResult (android.net.wifi.ScanResult)6 WifiAssociationTestRunner (com.android.connectivitymanagertest.WifiAssociationTestRunner)6 PendingIntent (android.app.PendingIntent)5 ConnectivityManager (android.net.ConnectivityManager)5 Network (android.net.Network)5 CellIdentityCdma (android.telephony.CellIdentityCdma)5 CellIdentityGsm (android.telephony.CellIdentityGsm)5 CellIdentityLte (android.telephony.CellIdentityLte)5 CellIdentityWcdma (android.telephony.CellIdentityWcdma)5 CellInfo (android.telephony.CellInfo)5 CellInfoCdma (android.telephony.CellInfoCdma)5 CellInfoGsm (android.telephony.CellInfoGsm)5