Search in sources :

Example 86 with NetworkCapabilities

use of android.net.NetworkCapabilities in project android_frameworks_base by DirtyUnicorns.

the class NetworkNotificationManagerTest method testNotificationsNotShownIfNoInternetCapability.

@SmallTest
public void testNotificationsNotShownIfNoInternetCapability() {
    mWifiNai.networkCapabilities = new NetworkCapabilities();
    mWifiNai.networkCapabilities.addTransportType(NetworkCapabilities.TRANSPORT_WIFI);
    mManager.showNotification(102, NO_INTERNET, mWifiNai, mCellNai, null, false);
    mManager.showNotification(103, LOST_INTERNET, mWifiNai, mCellNai, null, false);
    mManager.showNotification(104, NETWORK_SWITCH, mWifiNai, mCellNai, null, false);
    verify(mNotificationManager, never()).notifyAsUser(any(), anyInt(), any(), any());
}
Also used : NetworkCapabilities(android.net.NetworkCapabilities) SmallTest(android.test.suitebuilder.annotation.SmallTest)

Example 87 with NetworkCapabilities

use of android.net.NetworkCapabilities in project android_frameworks_base by DirtyUnicorns.

the class AccessPoint method getSummary.

public static String getSummary(Context context, String ssid, DetailedState state, boolean isEphemeral, String passpointProvider) {
    if (state == DetailedState.CONNECTED && ssid == null) {
        if (TextUtils.isEmpty(passpointProvider) == false) {
            // Special case for connected + passpoint networks.
            String format = context.getString(R.string.connected_via_passpoint);
            return String.format(format, passpointProvider);
        } else if (isEphemeral) {
            // Special case for connected + ephemeral networks.
            return context.getString(R.string.connected_via_wfa);
        }
    }
    // Case when there is wifi connected without internet connectivity.
    final ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (state == DetailedState.CONNECTED) {
        IWifiManager wifiManager = IWifiManager.Stub.asInterface(ServiceManager.getService(Context.WIFI_SERVICE));
        NetworkCapabilities nc = null;
        try {
            nc = cm.getNetworkCapabilities(wifiManager.getCurrentNetwork());
        } catch (RemoteException e) {
        }
        if (nc != null) {
            if (nc.hasCapability(nc.NET_CAPABILITY_CAPTIVE_PORTAL)) {
                return context.getString(com.android.internal.R.string.network_available_sign_in);
            } else if (!nc.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED)) {
                return context.getString(R.string.wifi_connected_no_internet);
            }
        }
    }
    if (state == null) {
        Log.w(TAG, "state is null, returning empty summary");
        return "";
    }
    String[] formats = context.getResources().getStringArray((ssid == null) ? R.array.wifi_status : R.array.wifi_status_with_ssid);
    int index = state.ordinal();
    if (index >= formats.length || formats[index].length() == 0) {
        return "";
    }
    return String.format(formats[index], ssid);
}
Also used : ConnectivityManager(android.net.ConnectivityManager) SpannableString(android.text.SpannableString) RemoteException(android.os.RemoteException) NetworkCapabilities(android.net.NetworkCapabilities) IWifiManager(android.net.wifi.IWifiManager)

Example 88 with NetworkCapabilities

use of android.net.NetworkCapabilities in project android_frameworks_base by ResurrectionRemix.

the class ConnectivityServiceTest method testInvalidNetworkSpecifier.

@SmallTest
public void testInvalidNetworkSpecifier() {
    boolean execptionCalled = true;
    try {
        NetworkRequest.Builder builder = new NetworkRequest.Builder();
        builder.setNetworkSpecifier(MATCH_ALL_REQUESTS_NETWORK_SPECIFIER);
        execptionCalled = false;
    } catch (IllegalArgumentException e) {
    // do nothing - should get here
    }
    assertTrue("NetworkRequest builder with MATCH_ALL_REQUESTS_NETWORK_SPECIFIER", execptionCalled);
    try {
        NetworkCapabilities networkCapabilities = new NetworkCapabilities();
        networkCapabilities.addTransportType(TRANSPORT_WIFI).setNetworkSpecifier(NetworkCapabilities.MATCH_ALL_REQUESTS_NETWORK_SPECIFIER);
        mService.requestNetwork(networkCapabilities, null, 0, null, ConnectivityManager.TYPE_WIFI);
        execptionCalled = false;
    } catch (IllegalArgumentException e) {
    // do nothing - should get here
    }
    assertTrue("ConnectivityService requestNetwork with MATCH_ALL_REQUESTS_NETWORK_SPECIFIER", execptionCalled);
}
Also used : NetworkRequest(android.net.NetworkRequest) NetworkCapabilities(android.net.NetworkCapabilities) SmallTest(android.test.suitebuilder.annotation.SmallTest)

Example 89 with NetworkCapabilities

use of android.net.NetworkCapabilities in project android_frameworks_base by ResurrectionRemix.

the class ConnectivityServiceTest method tryNetworkFactoryRequests.

private void tryNetworkFactoryRequests(int capability) throws Exception {
    // Verify NOT_RESTRICTED is set appropriately
    final NetworkCapabilities nc = new NetworkRequest.Builder().addCapability(capability).build().networkCapabilities;
    if (capability == NET_CAPABILITY_CBS || capability == NET_CAPABILITY_DUN || capability == NET_CAPABILITY_EIMS || capability == NET_CAPABILITY_FOTA || capability == NET_CAPABILITY_IA || capability == NET_CAPABILITY_IMS || capability == NET_CAPABILITY_RCS || capability == NET_CAPABILITY_XCAP) {
        assertFalse(nc.hasCapability(NET_CAPABILITY_NOT_RESTRICTED));
    } else {
        assertTrue(nc.hasCapability(NET_CAPABILITY_NOT_RESTRICTED));
    }
    NetworkCapabilities filter = new NetworkCapabilities();
    filter.addCapability(capability);
    final HandlerThread handlerThread = new HandlerThread("testNetworkFactoryRequests");
    handlerThread.start();
    final MockNetworkFactory testFactory = new MockNetworkFactory(handlerThread.getLooper(), mServiceContext, "testFactory", filter);
    testFactory.setScoreFilter(40);
    ConditionVariable cv = testFactory.getNetworkStartedCV();
    testFactory.expectAddRequests(1);
    testFactory.register();
    testFactory.waitForNetworkRequests(1);
    int expectedRequestCount = 1;
    NetworkCallback networkCallback = null;
    // add one.
    if (capability != NET_CAPABILITY_INTERNET) {
        assertFalse(testFactory.getMyStartRequested());
        NetworkRequest request = new NetworkRequest.Builder().addCapability(capability).build();
        networkCallback = new NetworkCallback();
        testFactory.expectAddRequests(1);
        mCm.requestNetwork(request, networkCallback);
        expectedRequestCount++;
        testFactory.waitForNetworkRequests(expectedRequestCount);
    }
    waitFor(cv);
    assertEquals(expectedRequestCount, testFactory.getMyRequestCount());
    assertTrue(testFactory.getMyStartRequested());
    // Now bring in a higher scored network.
    MockNetworkAgent testAgent = new MockNetworkAgent(TRANSPORT_CELLULAR);
    // Rather than create a validated network which complicates things by registering it's
    // own NetworkRequest during startup, just bump up the score to cancel out the
    // unvalidated penalty.
    testAgent.adjustScore(40);
    cv = testFactory.getNetworkStoppedCV();
    // When testAgent connects, ConnectivityService will re-send us all current requests with
    // the new score. There are expectedRequestCount such requests, and we must wait for all of
    // them.
    testFactory.expectAddRequests(expectedRequestCount);
    testAgent.connect(false);
    testAgent.addCapability(capability);
    waitFor(cv);
    testFactory.waitForNetworkRequests(expectedRequestCount);
    assertFalse(testFactory.getMyStartRequested());
    // Bring in a bunch of requests.
    testFactory.expectAddRequests(10);
    assertEquals(expectedRequestCount, testFactory.getMyRequestCount());
    ConnectivityManager.NetworkCallback[] networkCallbacks = new ConnectivityManager.NetworkCallback[10];
    for (int i = 0; i < networkCallbacks.length; i++) {
        networkCallbacks[i] = new ConnectivityManager.NetworkCallback();
        NetworkRequest.Builder builder = new NetworkRequest.Builder();
        builder.addCapability(capability);
        mCm.requestNetwork(builder.build(), networkCallbacks[i]);
    }
    testFactory.waitForNetworkRequests(10 + expectedRequestCount);
    assertFalse(testFactory.getMyStartRequested());
    // Remove the requests.
    testFactory.expectRemoveRequests(10);
    for (int i = 0; i < networkCallbacks.length; i++) {
        mCm.unregisterNetworkCallback(networkCallbacks[i]);
    }
    testFactory.waitForNetworkRequests(expectedRequestCount);
    assertFalse(testFactory.getMyStartRequested());
    // Drop the higher scored network.
    cv = testFactory.getNetworkStartedCV();
    testAgent.disconnect();
    waitFor(cv);
    assertEquals(expectedRequestCount, testFactory.getMyRequestCount());
    assertTrue(testFactory.getMyStartRequested());
    testFactory.unregister();
    if (networkCallback != null)
        mCm.unregisterNetworkCallback(networkCallback);
    handlerThread.quit();
}
Also used : ConnectivityManager(android.net.ConnectivityManager) NetworkRequest(android.net.NetworkRequest) NetworkCallback(android.net.ConnectivityManager.NetworkCallback) NetworkCapabilities(android.net.NetworkCapabilities) NetworkCallback(android.net.ConnectivityManager.NetworkCallback) ConditionVariable(android.os.ConditionVariable) HandlerThread(android.os.HandlerThread)

Example 90 with NetworkCapabilities

use of android.net.NetworkCapabilities in project android_frameworks_base by ResurrectionRemix.

the class ConnectivityServiceTest method isForegroundNetwork.

private boolean isForegroundNetwork(MockNetworkAgent network) {
    NetworkCapabilities nc = mCm.getNetworkCapabilities(network.getNetwork());
    assertNotNull(nc);
    return nc.hasCapability(NET_CAPABILITY_FOREGROUND);
}
Also used : NetworkCapabilities(android.net.NetworkCapabilities)

Aggregations

NetworkCapabilities (android.net.NetworkCapabilities)114 NetworkRequest (android.net.NetworkRequest)53 Network (android.net.Network)27 RemoteException (android.os.RemoteException)25 LinkProperties (android.net.LinkProperties)23 NetworkAgentInfo (com.android.server.connectivity.NetworkAgentInfo)20 NetworkInfo (android.net.NetworkInfo)18 ConnectivityManager (android.net.ConnectivityManager)15 NetworkPolicyManager.uidRulesToString (android.net.NetworkPolicyManager.uidRulesToString)15 SmallTest (android.test.suitebuilder.annotation.SmallTest)12 NetworkCallback (android.net.ConnectivityManager.NetworkCallback)10 NetworkState (android.net.NetworkState)8 HandlerThread (android.os.HandlerThread)8 NetworkMisc (android.net.NetworkMisc)5 IWifiManager (android.net.wifi.IWifiManager)5 WifiManager (android.net.wifi.WifiManager)5 Bundle (android.os.Bundle)5 Message (android.os.Message)5 ServiceState (android.telephony.ServiceState)5 SignalStrength (android.telephony.SignalStrength)5