Search in sources :

Example 26 with NetworkCapabilities

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

the class NetworkControllerImpl method updateConnectivity.

/**
     * Update the Inet conditions and what network we are connected to.
     */
private void updateConnectivity() {
    mConnectedTransports.clear();
    mValidatedTransports.clear();
    for (NetworkCapabilities nc : mConnectivityManager.getDefaultNetworkCapabilitiesForUser(mCurrentUserId)) {
        for (int transportType : nc.getTransportTypes()) {
            mConnectedTransports.set(transportType);
            if (nc.hasCapability(NET_CAPABILITY_VALIDATED)) {
                mValidatedTransports.set(transportType);
            }
        }
    }
    if (CHATTY) {
        Log.d(TAG, "updateConnectivity: mConnectedTransports=" + mConnectedTransports);
        Log.d(TAG, "updateConnectivity: mValidatedTransports=" + mValidatedTransports);
    }
    mInetCondition = !mValidatedTransports.isEmpty();
    pushConnectivityToSignals();
}
Also used : NetworkCapabilities(android.net.NetworkCapabilities)

Example 27 with NetworkCapabilities

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

the class WifiNoInternetDialog method onCreate.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    final Intent intent = getIntent();
    if (intent == null || !isKnownAction(intent) || !"netId".equals(intent.getScheme())) {
        Log.e(TAG, "Unexpected intent " + intent + ", exiting");
        finish();
        return;
    }
    mAction = intent.getAction();
    try {
        mNetwork = new Network(Integer.parseInt(intent.getData().getSchemeSpecificPart()));
    } catch (NullPointerException | NumberFormatException e) {
        mNetwork = null;
    }
    if (mNetwork == null) {
        Log.e(TAG, "Can't determine network from '" + intent.getData() + "' , exiting");
        finish();
        return;
    }
    // TODO: add a registerNetworkCallback(Network network, NetworkCallback networkCallback) and
    // simplify this.
    final NetworkRequest request = new NetworkRequest.Builder().clearCapabilities().build();
    mNetworkCallback = new NetworkCallback() {

        @Override
        public void onLost(Network network) {
            // Close the dialog if the network disconnects.
            if (mNetwork.equals(network)) {
                Log.d(TAG, "Network " + mNetwork + " disconnected");
                finish();
            }
        }

        @Override
        public void onCapabilitiesChanged(Network network, NetworkCapabilities nc) {
            // Close the dialog if the network validates.
            if (mNetwork.equals(network) && nc.hasCapability(NET_CAPABILITY_VALIDATED)) {
                Log.d(TAG, "Network " + mNetwork + " validated");
                finish();
            }
        }
    };
    mCM = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    mCM.registerNetworkCallback(request, mNetworkCallback);
    final NetworkInfo ni = mCM.getNetworkInfo(mNetwork);
    if (ni == null || !ni.isConnectedOrConnecting()) {
        Log.d(TAG, "Network " + mNetwork + " is not connected: " + ni);
        finish();
        return;
    }
    mNetworkName = ni.getExtraInfo();
    if (mNetworkName != null) {
        // Remove double quotes
        mNetworkName = mNetworkName.replaceAll("^\"|\"$", "");
    }
    createDialog();
}
Also used : NetworkInfo(android.net.NetworkInfo) Network(android.net.Network) NetworkRequest(android.net.NetworkRequest) Intent(android.content.Intent) NetworkCapabilities(android.net.NetworkCapabilities) NetworkCallback(android.net.ConnectivityManager.NetworkCallback)

Example 28 with NetworkCapabilities

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

the class ConnectivityServiceTest method testMobileDataAlwaysOn.

@SmallTest
public void testMobileDataAlwaysOn() throws Exception {
    final TestNetworkCallback cellNetworkCallback = new TestNetworkCallback();
    final NetworkRequest cellRequest = new NetworkRequest.Builder().addTransportType(TRANSPORT_CELLULAR).build();
    mCm.registerNetworkCallback(cellRequest, cellNetworkCallback);
    final HandlerThread handlerThread = new HandlerThread("MobileDataAlwaysOnFactory");
    handlerThread.start();
    NetworkCapabilities filter = new NetworkCapabilities().addTransportType(TRANSPORT_CELLULAR).addCapability(NET_CAPABILITY_INTERNET);
    final MockNetworkFactory testFactory = new MockNetworkFactory(handlerThread.getLooper(), mServiceContext, "testFactory", filter);
    testFactory.setScoreFilter(40);
    // Register the factory and expect it to start looking for a network.
    testFactory.expectAddRequests(1);
    testFactory.register();
    testFactory.waitForNetworkRequests(1);
    assertTrue(testFactory.getMyStartRequested());
    // Bring up wifi. The factory stops looking for a network.
    mWiFiNetworkAgent = new MockNetworkAgent(TRANSPORT_WIFI);
    // Because the default request changes score twice.
    testFactory.expectAddRequests(2);
    mWiFiNetworkAgent.connect(true);
    testFactory.waitForNetworkRequests(1);
    assertFalse(testFactory.getMyStartRequested());
    ContentResolver cr = mServiceContext.getContentResolver();
    // Turn on mobile data always on. The factory starts looking again.
    testFactory.expectAddRequests(1);
    setMobileDataAlwaysOn(true);
    testFactory.waitForNetworkRequests(2);
    assertTrue(testFactory.getMyStartRequested());
    // Bring up cell data and check that the factory stops looking.
    assertEquals(1, mCm.getAllNetworks().length);
    mCellNetworkAgent = new MockNetworkAgent(TRANSPORT_CELLULAR);
    // Because the cell request changes score twice.
    testFactory.expectAddRequests(2);
    mCellNetworkAgent.connect(true);
    cellNetworkCallback.expectCallback(CallbackState.AVAILABLE, mCellNetworkAgent);
    testFactory.waitForNetworkRequests(2);
    // Because the cell network outscores us.
    assertFalse(testFactory.getMyStartRequested());
    // Check that cell data stays up.
    mService.waitForIdle();
    verifyActiveNetwork(TRANSPORT_WIFI);
    assertEquals(2, mCm.getAllNetworks().length);
    // Turn off mobile data always on and expect the request to disappear...
    testFactory.expectRemoveRequests(1);
    setMobileDataAlwaysOn(false);
    testFactory.waitForNetworkRequests(1);
    // ...  and cell data to be torn down.
    cellNetworkCallback.expectCallback(CallbackState.LOST, mCellNetworkAgent);
    assertEquals(1, mCm.getAllNetworks().length);
    testFactory.unregister();
    mCm.unregisterNetworkCallback(cellNetworkCallback);
    handlerThread.quit();
}
Also used : HandlerThread(android.os.HandlerThread) NetworkRequest(android.net.NetworkRequest) NetworkCapabilities(android.net.NetworkCapabilities) ContentResolver(android.content.ContentResolver) MockContentResolver(android.test.mock.MockContentResolver) SmallTest(android.test.suitebuilder.annotation.SmallTest)

Example 29 with NetworkCapabilities

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

the class NetworkControllerImpl method updateConnectivity.

/**
     * Update the Inet conditions and what network we are connected to.
     */
private void updateConnectivity() {
    mConnectedTransports.clear();
    mValidatedTransports.clear();
    for (NetworkCapabilities nc : mConnectivityManager.getDefaultNetworkCapabilitiesForUser(mCurrentUserId)) {
        for (int transportType : nc.getTransportTypes()) {
            mConnectedTransports.set(transportType);
            if (nc.hasCapability(NET_CAPABILITY_VALIDATED)) {
                mValidatedTransports.set(transportType);
            }
        }
    }
    if (CHATTY) {
        Log.d(TAG, "updateConnectivity: mConnectedTransports=" + mConnectedTransports);
        Log.d(TAG, "updateConnectivity: mValidatedTransports=" + mValidatedTransports);
    }
    mInetCondition = !mValidatedTransports.isEmpty();
    pushConnectivityToSignals();
}
Also used : NetworkCapabilities(android.net.NetworkCapabilities)

Example 30 with NetworkCapabilities

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

the class ConnectivityService method updateCapabilities.

/**
     * Update the NetworkCapabilities for {@code networkAgent} to {@code networkCapabilities}
     * augmented with any stateful capabilities implied from {@code networkAgent}
     * (e.g., validated status and captive portal status).
     *
     * @param oldScore score of the network before any of the changes that prompted us
     *                 to call this function.
     * @param nai the network having its capabilities updated.
     * @param networkCapabilities the new network capabilities.
     */
private void updateCapabilities(int oldScore, NetworkAgentInfo nai, NetworkCapabilities networkCapabilities) {
    if (nai.everConnected && !nai.networkCapabilities.equalImmutableCapabilities(networkCapabilities)) {
        Slog.wtf(TAG, "BUG: " + nai + " changed immutable capabilities: " + nai.networkCapabilities + " -> " + networkCapabilities);
    }
    // Don't modify caller's NetworkCapabilities.
    networkCapabilities = new NetworkCapabilities(networkCapabilities);
    if (nai.lastValidated) {
        networkCapabilities.addCapability(NET_CAPABILITY_VALIDATED);
    } else {
        networkCapabilities.removeCapability(NET_CAPABILITY_VALIDATED);
    }
    if (nai.lastCaptivePortalDetected) {
        networkCapabilities.addCapability(NET_CAPABILITY_CAPTIVE_PORTAL);
    } else {
        networkCapabilities.removeCapability(NET_CAPABILITY_CAPTIVE_PORTAL);
    }
    if (nai.isBackgroundNetwork()) {
        networkCapabilities.removeCapability(NET_CAPABILITY_FOREGROUND);
    } else {
        networkCapabilities.addCapability(NET_CAPABILITY_FOREGROUND);
    }
    if (Objects.equals(nai.networkCapabilities, networkCapabilities))
        return;
    final String oldPermission = getNetworkPermission(nai.networkCapabilities);
    final String newPermission = getNetworkPermission(networkCapabilities);
    if (!Objects.equals(oldPermission, newPermission) && nai.created && !nai.isVPN()) {
        try {
            mNetd.setNetworkPermission(nai.network.netId, newPermission);
        } catch (RemoteException e) {
            loge("Exception in setNetworkPermission: " + e);
        }
    }
    final NetworkCapabilities prevNc = nai.networkCapabilities;
    synchronized (nai) {
        nai.networkCapabilities = networkCapabilities;
    }
    if (nai.getCurrentScore() == oldScore && networkCapabilities.equalRequestableCapabilities(prevNc)) {
        // If the requestable capabilities haven't changed, and the score hasn't changed, then
        // the change we're processing can't affect any requests, it can only affect the listens
        // on this network. We might have been called by rematchNetworkAndRequests when a
        // network changed foreground state.
        processListenRequests(nai, true);
    } else {
        // If the requestable capabilities have changed or the score changed, we can't have been
        // called by rematchNetworkAndRequests, so it's safe to start a rematch.
        rematchAllNetworksAndRequests(nai, oldScore);
        notifyNetworkCallbacks(nai, ConnectivityManager.CALLBACK_CAP_CHANGED);
    }
}
Also used : NetworkPolicyManager.uidRulesToString(android.net.NetworkPolicyManager.uidRulesToString) RemoteException(android.os.RemoteException) 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