Search in sources :

Example 66 with NetworkCapabilities

use of android.net.NetworkCapabilities in project platform_frameworks_base by android.

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 67 with NetworkCapabilities

use of android.net.NetworkCapabilities in project platform_frameworks_base by android.

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 68 with NetworkCapabilities

use of android.net.NetworkCapabilities in project platform_frameworks_base by android.

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));
        Network nw;
        try {
            nw = wifiManager.getCurrentNetwork();
        } catch (RemoteException e) {
            nw = null;
        }
        NetworkCapabilities nc = cm.getNetworkCapabilities(nw);
        if (nc != null && !nc.hasCapability(nc.NET_CAPABILITY_VALIDATED)) {
            return context.getString(R.string.wifi_connected_no_internet);
        }
    }
    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) Network(android.net.Network) SpannableString(android.text.SpannableString) RemoteException(android.os.RemoteException) NetworkCapabilities(android.net.NetworkCapabilities) IWifiManager(android.net.wifi.IWifiManager)

Example 69 with NetworkCapabilities

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

the class ConnectivityService method rematchNetworkAndRequests.

// Handles a network appearing or improving its score.
//
// - Evaluates all current NetworkRequests that can be
//   satisfied by newNetwork, and reassigns to newNetwork
//   any such requests for which newNetwork is the best.
//
// - Lingers any validated Networks that as a result are no longer
//   needed. A network is needed if it is the best network for
//   one or more NetworkRequests, or if it is a VPN.
//
// - Tears down newNetwork if it just became validated
//   but turns out to be unneeded.
//
// - If reapUnvalidatedNetworks==REAP, tears down unvalidated
//   networks that have no chance (i.e. even if validated)
//   of becoming the highest scoring network.
//
// NOTE: This function only adds NetworkRequests that "newNetwork" could satisfy,
// it does not remove NetworkRequests that other Networks could better satisfy.
// If you need to handle decreases in score, use {@link rematchAllNetworksAndRequests}.
// This function should be used when possible instead of {@code rematchAllNetworksAndRequests}
// as it performs better by a factor of the number of Networks.
//
// @param newNetwork is the network to be matched against NetworkRequests.
// @param reapUnvalidatedNetworks indicates if an additional pass over all networks should be
//               performed to tear down unvalidated networks that have no chance (i.e. even if
//               validated) of becoming the highest scoring network.
private void rematchNetworkAndRequests(NetworkAgentInfo newNetwork, ReapUnvalidatedNetworks reapUnvalidatedNetworks, long now) {
    if (!newNetwork.everConnected)
        return;
    boolean keep = newNetwork.isVPN();
    boolean isNewDefault = false;
    NetworkAgentInfo oldDefaultNetwork = null;
    final boolean wasBackgroundNetwork = newNetwork.isBackgroundNetwork();
    final int score = newNetwork.getCurrentScore();
    if (VDBG)
        log("rematching " + newNetwork.name());
    // Find and migrate to this Network any NetworkRequests for
    // which this network is now the best.
    ArrayList<NetworkAgentInfo> affectedNetworks = new ArrayList<NetworkAgentInfo>();
    ArrayList<NetworkRequestInfo> addedRequests = new ArrayList<NetworkRequestInfo>();
    NetworkCapabilities nc = newNetwork.networkCapabilities;
    if (VDBG)
        log(" network has: " + nc);
    for (NetworkRequestInfo nri : mNetworkRequests.values()) {
        // requests or not, and doesn't affect the network's score.
        if (nri.request.isListen())
            continue;
        final NetworkAgentInfo currentNetwork = mNetworkForRequestId.get(nri.request.requestId);
        final boolean satisfies = newNetwork.satisfies(nri.request);
        if (newNetwork == currentNetwork && satisfies) {
            if (VDBG) {
                log("Network " + newNetwork.name() + " was already satisfying" + " request " + nri.request.requestId + ". No change.");
            }
            keep = true;
            continue;
        }
        // check if it satisfies the NetworkCapabilities
        if (VDBG)
            log("  checking if request is satisfied: " + nri.request);
        if (satisfies) {
            // this request
            if (VDBG) {
                log("currentScore = " + (currentNetwork != null ? currentNetwork.getCurrentScore() : 0) + ", newScore = " + score);
            }
            if (currentNetwork == null || currentNetwork.getCurrentScore() < score) {
                if (VDBG)
                    log("rematch for " + newNetwork.name());
                if (currentNetwork != null) {
                    if (VDBG)
                        log("   accepting network in place of " + currentNetwork.name());
                    currentNetwork.removeRequest(nri.request.requestId);
                    currentNetwork.lingerRequest(nri.request, now, mLingerDelayMs);
                    affectedNetworks.add(currentNetwork);
                } else {
                    if (VDBG)
                        log("   accepting network in place of null");
                }
                newNetwork.unlingerRequest(nri.request);
                mNetworkForRequestId.put(nri.request.requestId, newNetwork);
                if (!newNetwork.addRequest(nri.request)) {
                    Slog.wtf(TAG, "BUG: " + newNetwork.name() + " already has " + nri.request);
                }
                addedRequests.add(nri);
                keep = true;
                // Tell NetworkFactories about the new score, so they can stop
                // trying to connect if they know they cannot match it.
                // TODO - this could get expensive if we have alot of requests for this
                // network.  Think about if there is a way to reduce this.  Push
                // netid->request mapping to each factory?
                sendUpdatedScoreToFactories(nri.request, score);
                if (isDefaultRequest(nri)) {
                    isNewDefault = true;
                    oldDefaultNetwork = currentNetwork;
                    if (currentNetwork != null) {
                        mLingerMonitor.noteLingerDefaultNetwork(currentNetwork, newNetwork);
                    }
                }
            }
        } else if (newNetwork.isSatisfyingRequest(nri.request.requestId)) {
            // longer satisfies "nri" when "currentNetwork" does not equal "newNetwork".
            if (DBG) {
                log("Network " + newNetwork.name() + " stopped satisfying" + " request " + nri.request.requestId);
            }
            newNetwork.removeRequest(nri.request.requestId);
            if (currentNetwork == newNetwork) {
                mNetworkForRequestId.remove(nri.request.requestId);
                sendUpdatedScoreToFactories(nri.request, 0);
            } else {
                Slog.wtf(TAG, "BUG: Removing request " + nri.request.requestId + " from " + newNetwork.name() + " without updating mNetworkForRequestId or factories!");
            }
            // TODO: Technically, sending CALLBACK_LOST here is
            // incorrect if there is a replacement network currently
            // connected that can satisfy nri, which is a request
            // (not a listen). However, the only capability that can both
            // a) be requested and b) change is NET_CAPABILITY_TRUSTED,
            // so this code is only incorrect for a network that loses
            // the TRUSTED capability, which is a rare case.
            callCallbackForRequest(nri, newNetwork, ConnectivityManager.CALLBACK_LOST, 0);
        }
    }
    if (isNewDefault) {
        // Notify system services that this network is up.
        makeDefault(newNetwork);
        // Log 0 -> X and Y -> X default network transitions, where X is the new default.
        logDefaultNetworkEvent(newNetwork, oldDefaultNetwork);
        synchronized (ConnectivityService.this) {
            // to reconnect over the new network
            if (mNetTransitionWakeLock.isHeld()) {
                mHandler.sendMessageDelayed(mHandler.obtainMessage(EVENT_CLEAR_NET_TRANSITION_WAKELOCK, mNetTransitionWakeLockSerialNumber, 0), 1000);
            }
        }
    }
    if (!newNetwork.networkCapabilities.equalRequestableCapabilities(nc)) {
        Slog.wtf(TAG, String.format("BUG: %s changed requestable capabilities during rematch: %s -> %s", nc, newNetwork.networkCapabilities));
    }
    if (newNetwork.getCurrentScore() != score) {
        Slog.wtf(TAG, String.format("BUG: %s changed score during rematch: %d -> %d", score, newNetwork.getCurrentScore()));
    }
    // Second pass: process all listens.
    if (wasBackgroundNetwork != newNetwork.isBackgroundNetwork()) {
        // If the network went from background to foreground or vice versa, we need to update
        // its foreground state. It is safe to do this after rematching the requests because
        // NET_CAPABILITY_FOREGROUND does not affect requests, as is not a requestable
        // capability and does not affect the network's score (see the Slog.wtf call above).
        updateCapabilities(score, newNetwork, newNetwork.networkCapabilities);
    } else {
        processListenRequests(newNetwork, false);
    }
    // before LegacyTypeTracker sends legacy broadcasts
    for (NetworkRequestInfo nri : addedRequests) notifyNetworkCallback(newNetwork, nri);
    // available callback for newNetwork.
    for (NetworkAgentInfo nai : affectedNetworks) {
        updateLingerState(nai, now);
    }
    // Possibly unlinger newNetwork. Unlingering a network does not send any callbacks so it
    // does not need to be done in any particular order.
    updateLingerState(newNetwork, now);
    if (isNewDefault) {
        // the new one connected.
        if (oldDefaultNetwork != null) {
            mLegacyTypeTracker.remove(oldDefaultNetwork.networkInfo.getType(), oldDefaultNetwork, true);
        }
        mDefaultInetConditionPublished = newNetwork.lastValidated ? 100 : 0;
        mLegacyTypeTracker.add(newNetwork.networkInfo.getType(), newNetwork);
        notifyLockdownVpn(newNetwork);
    }
    if (keep) {
        // TODO: Avoid redoing this; this must only be done once when a network comes online.
        try {
            final IBatteryStats bs = BatteryStatsService.getService();
            final int type = newNetwork.networkInfo.getType();
            final String baseIface = newNetwork.linkProperties.getInterfaceName();
            bs.noteNetworkInterfaceType(baseIface, type);
            for (LinkProperties stacked : newNetwork.linkProperties.getStackedLinks()) {
                final String stackedIface = stacked.getInterfaceName();
                bs.noteNetworkInterfaceType(stackedIface, type);
                NetworkStatsFactory.noteStackedIface(stackedIface, baseIface);
            }
        } catch (RemoteException ignored) {
        }
        // This is on top of the multiple intent sequencing referenced in the todo above.
        for (int i = 0; i < newNetwork.numNetworkRequests(); i++) {
            NetworkRequest nr = newNetwork.requestAt(i);
            if (nr.legacyType != TYPE_NONE && nr.isRequest()) {
                // legacy type tracker filters out repeat adds
                mLegacyTypeTracker.add(nr.legacyType, newNetwork);
            }
        }
        // newNetwork to the tracker explicitly (it's a no-op if it has already been added).
        if (newNetwork.isVPN()) {
            mLegacyTypeTracker.add(TYPE_VPN, newNetwork);
        }
    }
    if (reapUnvalidatedNetworks == ReapUnvalidatedNetworks.REAP) {
        for (NetworkAgentInfo nai : mNetworkAgentInfos.values()) {
            if (unneeded(nai, UnneededFor.TEARDOWN)) {
                if (nai.getLingerExpiry() > 0) {
                    // This network has active linger timers and no requests, but is not
                    // lingering. Linger it.
                    //
                    // One way (the only way?) this can happen if this network is unvalidated
                    // and became unneeded due to another network improving its score to the
                    // point where this network will no longer be able to satisfy any requests
                    // even if it validates.
                    updateLingerState(nai, now);
                } else {
                    if (DBG)
                        log("Reaping " + nai.name());
                    teardownUnneededNetwork(nai);
                }
            }
        }
    }
}
Also used : IBatteryStats(com.android.internal.app.IBatteryStats) NetworkAgentInfo(com.android.server.connectivity.NetworkAgentInfo) ArrayList(java.util.ArrayList) NetworkRequest(android.net.NetworkRequest) NetworkPolicyManager.uidRulesToString(android.net.NetworkPolicyManager.uidRulesToString) NetworkCapabilities(android.net.NetworkCapabilities) LinkProperties(android.net.LinkProperties) RemoteException(android.os.RemoteException)

Example 70 with NetworkCapabilities

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

the class ConnectivityService method removeDataActivityTracking.

/**
     * Remove data activity tracking when network disconnects.
     */
private void removeDataActivityTracking(NetworkAgentInfo networkAgent) {
    final String iface = networkAgent.linkProperties.getInterfaceName();
    final NetworkCapabilities caps = networkAgent.networkCapabilities;
    if (iface != null && (caps.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) || caps.hasTransport(NetworkCapabilities.TRANSPORT_WIFI))) {
        try {
            // the call fails silently if no idletimer setup for this interface
            mNetd.removeIdleTimer(iface);
        } catch (Exception e) {
            loge("Exception in removeDataActivityTracking " + e);
        }
    }
}
Also used : NetworkPolicyManager.uidRulesToString(android.net.NetworkPolicyManager.uidRulesToString) NetworkCapabilities(android.net.NetworkCapabilities) RemoteException(android.os.RemoteException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) FileNotFoundException(java.io.FileNotFoundException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException)

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