Search in sources :

Example 11 with NetworkCallback

use of android.net.ConnectivityManager.NetworkCallback in project android_frameworks_base by crdroidandroid.

the class ConnectivityServiceTest method testNetworkRequestMaximum.

@SmallTest
public void testNetworkRequestMaximum() {
    final int MAX_REQUESTS = 100;
    // Test that the limit is enforced when MAX_REQUESTS simultaneous requests are added.
    NetworkRequest networkRequest = new NetworkRequest.Builder().build();
    ArrayList<NetworkCallback> networkCallbacks = new ArrayList<NetworkCallback>();
    try {
        for (int i = 0; i < MAX_REQUESTS; i++) {
            NetworkCallback networkCallback = new NetworkCallback();
            mCm.requestNetwork(networkRequest, networkCallback);
            networkCallbacks.add(networkCallback);
        }
        fail("Registering " + MAX_REQUESTS + " NetworkRequests did not throw exception");
    } catch (IllegalArgumentException expected) {
    }
    for (NetworkCallback networkCallback : networkCallbacks) {
        mCm.unregisterNetworkCallback(networkCallback);
    }
    networkCallbacks.clear();
    try {
        for (int i = 0; i < MAX_REQUESTS; i++) {
            NetworkCallback networkCallback = new NetworkCallback();
            mCm.registerNetworkCallback(networkRequest, networkCallback);
            networkCallbacks.add(networkCallback);
        }
        fail("Registering " + MAX_REQUESTS + " NetworkCallbacks did not throw exception");
    } catch (IllegalArgumentException expected) {
    }
    for (NetworkCallback networkCallback : networkCallbacks) {
        mCm.unregisterNetworkCallback(networkCallback);
    }
    networkCallbacks.clear();
    ArrayList<PendingIntent> pendingIntents = new ArrayList<PendingIntent>();
    try {
        for (int i = 0; i < MAX_REQUESTS + 1; i++) {
            PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, 0, new Intent("a" + i), 0);
            mCm.requestNetwork(networkRequest, pendingIntent);
            pendingIntents.add(pendingIntent);
        }
        fail("Registering " + MAX_REQUESTS + " PendingIntent NetworkRequests did not throw exception");
    } catch (IllegalArgumentException expected) {
    }
    for (PendingIntent pendingIntent : pendingIntents) {
        mCm.unregisterNetworkCallback(pendingIntent);
    }
    pendingIntents.clear();
    try {
        for (int i = 0; i < MAX_REQUESTS + 1; i++) {
            PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, 0, new Intent("a" + i), 0);
            mCm.registerNetworkCallback(networkRequest, pendingIntent);
            pendingIntents.add(pendingIntent);
        }
        fail("Registering " + MAX_REQUESTS + " PendingIntent NetworkCallbacks did not throw exception");
    } catch (IllegalArgumentException expected) {
    }
    for (PendingIntent pendingIntent : pendingIntents) {
        mCm.unregisterNetworkCallback(pendingIntent);
    }
    pendingIntents.clear();
    mService.waitForIdle(5000);
    // Test that the limit is not hit when MAX_REQUESTS requests are added and removed.
    for (int i = 0; i < MAX_REQUESTS; i++) {
        NetworkCallback networkCallback = new NetworkCallback();
        mCm.requestNetwork(networkRequest, networkCallback);
        mCm.unregisterNetworkCallback(networkCallback);
    }
    mService.waitForIdle();
    for (int i = 0; i < MAX_REQUESTS; i++) {
        NetworkCallback networkCallback = new NetworkCallback();
        mCm.registerNetworkCallback(networkRequest, networkCallback);
        mCm.unregisterNetworkCallback(networkCallback);
    }
    mService.waitForIdle();
    for (int i = 0; i < MAX_REQUESTS; i++) {
        PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, 0, new Intent("b" + i), 0);
        mCm.requestNetwork(networkRequest, pendingIntent);
        mCm.unregisterNetworkCallback(pendingIntent);
    }
    mService.waitForIdle();
    for (int i = 0; i < MAX_REQUESTS; i++) {
        PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, 0, new Intent("c" + i), 0);
        mCm.registerNetworkCallback(networkRequest, pendingIntent);
        mCm.unregisterNetworkCallback(pendingIntent);
    }
}
Also used : NetworkRequest(android.net.NetworkRequest) ArrayList(java.util.ArrayList) PendingIntent(android.app.PendingIntent) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) NetworkCallback(android.net.ConnectivityManager.NetworkCallback) SmallTest(android.test.suitebuilder.annotation.SmallTest)

Example 12 with NetworkCallback

use of android.net.ConnectivityManager.NetworkCallback in project android_packages_apps_Settings by omnirom.

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 13 with NetworkCallback

use of android.net.ConnectivityManager.NetworkCallback in project android_packages_apps_Settings by SudaMod.

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 14 with NetworkCallback

use of android.net.ConnectivityManager.NetworkCallback in project android_packages_apps_Settings by DirtyUnicorns.

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 15 with NetworkCallback

use of android.net.ConnectivityManager.NetworkCallback in project android_packages_apps_Settings by crdroidandroid.

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)

Aggregations

NetworkCallback (android.net.ConnectivityManager.NetworkCallback)35 NetworkRequest (android.net.NetworkRequest)32 Network (android.net.Network)18 Intent (android.content.Intent)17 NetworkCapabilities (android.net.NetworkCapabilities)16 SmallTest (android.test.suitebuilder.annotation.SmallTest)13 PendingIntent (android.app.PendingIntent)8 NetworkInfo (android.net.NetworkInfo)7 WebSettings (android.webkit.WebSettings)5 WebView (android.webkit.WebView)5 ConnectivityManager (android.net.ConnectivityManager)4 ConditionVariable (android.os.ConditionVariable)4 HandlerThread (android.os.HandlerThread)4 ArrayList (java.util.ArrayList)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 LargeTest (android.test.suitebuilder.annotation.LargeTest)3 Test (org.junit.Test)2