Search in sources :

Example 51 with NetworkCapabilities

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

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

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

the class ConnectivityService method pendingRequestForNetwork.

@Override
public NetworkRequest pendingRequestForNetwork(NetworkCapabilities networkCapabilities, PendingIntent operation) {
    checkNotNull(operation, "PendingIntent cannot be null.");
    networkCapabilities = new NetworkCapabilities(networkCapabilities);
    enforceNetworkRequestPermissions(networkCapabilities);
    enforceMeteredApnPolicy(networkCapabilities);
    ensureRequestableCapabilities(networkCapabilities);
    NetworkRequest networkRequest = new NetworkRequest(networkCapabilities, TYPE_NONE, nextNetworkRequestId(), NetworkRequest.Type.REQUEST);
    NetworkRequestInfo nri = new NetworkRequestInfo(networkRequest, operation);
    if (DBG)
        log("pendingRequest for " + nri);
    mHandler.sendMessage(mHandler.obtainMessage(EVENT_REGISTER_NETWORK_REQUEST_WITH_INTENT, nri));
    return networkRequest;
}
Also used : NetworkRequest(android.net.NetworkRequest) NetworkCapabilities(android.net.NetworkCapabilities)

Example 53 with NetworkCapabilities

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

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)

Example 54 with NetworkCapabilities

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

the class ConnectivityService method pendingListenForNetwork.

@Override
public void pendingListenForNetwork(NetworkCapabilities networkCapabilities, PendingIntent operation) {
    checkNotNull(operation, "PendingIntent cannot be null.");
    if (!hasWifiNetworkListenPermission(networkCapabilities)) {
        enforceAccessPermission();
    }
    NetworkRequest networkRequest = new NetworkRequest(new NetworkCapabilities(networkCapabilities), TYPE_NONE, nextNetworkRequestId(), NetworkRequest.Type.LISTEN);
    NetworkRequestInfo nri = new NetworkRequestInfo(networkRequest, operation);
    if (VDBG)
        log("pendingListenForNetwork for " + nri);
    mHandler.sendMessage(mHandler.obtainMessage(EVENT_REGISTER_NETWORK_LISTENER, nri));
}
Also used : NetworkRequest(android.net.NetworkRequest) NetworkCapabilities(android.net.NetworkCapabilities)

Example 55 with NetworkCapabilities

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

the class ConnectivityManagerTest method verifyRestrictedMobileNetworkCapabilities.

static void verifyRestrictedMobileNetworkCapabilities(int legacyType, int capability) {
    final NetworkCapabilities nc = verifyNetworkCapabilities(legacyType, TRANSPORT_CELLULAR, capability, NET_CAPABILITY_NOT_VPN, NET_CAPABILITY_TRUSTED);
    assertFalse(nc.hasCapability(NET_CAPABILITY_INTERNET));
    assertFalse(nc.hasCapability(NET_CAPABILITY_NOT_RESTRICTED));
}
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