Search in sources :

Example 16 with NetworkCallback

use of android.net.ConnectivityManager.NetworkCallback in project platform_packages_apps_Settings by BlissRoms.

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

use of android.net.ConnectivityManager.NetworkCallback 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);
    final NetworkCapabilities nc = mCM.getNetworkCapabilities(mNetwork);
    if (ni == null || !ni.isConnectedOrConnecting() || nc == null) {
        Log.d(TAG, "Network " + mNetwork + " is not connected: " + ni);
        finish();
        return;
    }
    mNetworkName = nc.getSSID();
    if (mNetworkName != null) {
        mNetworkName = WifiInfo.removeDoubleQuotes(mNetworkName);
    }
    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 18 with NetworkCallback

use of android.net.ConnectivityManager.NetworkCallback in project Resurrection_packages_apps_Settings by ResurrectionRemix.

the class WifiScanWorkerTest method NetworkCallback_onCapabilitiesChanged_sliceIsUnpinned_shouldNotSendBroadcast.

@Test
public void NetworkCallback_onCapabilitiesChanged_sliceIsUnpinned_shouldNotSendBroadcast() {
    final Intent intent = getIntentWithAccessPoint("ap1");
    setConnectionInfoSSID("ap1");
    final Network network = mConnectivityManager.getActiveNetwork();
    mWifiScanWorker.registerNetworkCallback(network);
    final NetworkCallback callback = mWifiScanWorker.mNetworkCallback;
    mWifiScanWorker.onSlicePinned();
    mConnectToWifiHandler.onReceive(mContext, intent);
    mWifiScanWorker.onSliceUnpinned();
    callback.onCapabilitiesChanged(network, WifiSliceTest.makeCaptivePortalNetworkCapabilities());
    verify(mContext, never()).sendBroadcastAsUser(any(Intent.class), eq(UserHandle.CURRENT));
}
Also used : Network(android.net.Network) Intent(android.content.Intent) NetworkCallback(android.net.ConnectivityManager.NetworkCallback) Test(org.junit.Test)

Example 19 with NetworkCallback

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

the class ConnectivityServiceTest method testNoMutableNetworkRequests.

@LargeTest
public void testNoMutableNetworkRequests() throws Exception {
    PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, 0, new Intent("a"), 0);
    NetworkRequest.Builder builder = new NetworkRequest.Builder();
    builder.addCapability(NET_CAPABILITY_VALIDATED);
    try {
        mCm.requestNetwork(builder.build(), new NetworkCallback());
        fail();
    } catch (IllegalArgumentException expected) {
    }
    try {
        mCm.requestNetwork(builder.build(), pendingIntent);
        fail();
    } catch (IllegalArgumentException expected) {
    }
    builder = new NetworkRequest.Builder();
    builder.addCapability(NET_CAPABILITY_CAPTIVE_PORTAL);
    try {
        mCm.requestNetwork(builder.build(), new NetworkCallback());
        fail();
    } catch (IllegalArgumentException expected) {
    }
    try {
        mCm.requestNetwork(builder.build(), pendingIntent);
        fail();
    } catch (IllegalArgumentException expected) {
    }
}
Also used : NetworkRequest(android.net.NetworkRequest) PendingIntent(android.app.PendingIntent) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) NetworkCallback(android.net.ConnectivityManager.NetworkCallback) LargeTest(android.test.suitebuilder.annotation.LargeTest)

Example 20 with NetworkCallback

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

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)

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