Search in sources :

Example 1 with NetworkCallback

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

the class CaptivePortalLoginActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mCm = ConnectivityManager.from(this);
    mNetwork = getIntent().getParcelableExtra(ConnectivityManager.EXTRA_NETWORK);
    mCaptivePortal = getIntent().getParcelableExtra(ConnectivityManager.EXTRA_CAPTIVE_PORTAL);
    mUserAgent = getIntent().getParcelableExtra(ConnectivityManager.EXTRA_CAPTIVE_PORTAL_USER_AGENT);
    mUrl = getUrl();
    if (mUrl == null) {
        // getUrl() failed to parse the url provided in the intent: bail out in a way that
        // at least provides network access.
        done(Result.WANTED_AS_IS);
        return;
    }
    if (DBG) {
        Log.d(TAG, String.format("onCreate for %s", mUrl.toString()));
    }
    // Also initializes proxy system properties.
    mCm.bindProcessToNetwork(mNetwork);
    // Proxy system properties must be initialized before setContentView is called because
    // setContentView initializes the WebView logic which in turn reads the system properties.
    setContentView(R.layout.activity_captive_portal_login);
    getActionBar().setDisplayShowHomeEnabled(false);
    // Exit app if Network disappears.
    final NetworkCapabilities networkCapabilities = mCm.getNetworkCapabilities(mNetwork);
    if (networkCapabilities == null) {
        finishAndRemoveTask();
        return;
    }
    mNetworkCallback = new NetworkCallback() {

        @Override
        public void onLost(Network lostNetwork) {
            if (mNetwork.equals(lostNetwork))
                done(Result.UNWANTED);
        }
    };
    final NetworkRequest.Builder builder = new NetworkRequest.Builder();
    for (int transportType : networkCapabilities.getTransportTypes()) {
        builder.addTransportType(transportType);
    }
    mCm.registerNetworkCallback(builder.build(), mNetworkCallback);
    final WebView myWebView = (WebView) findViewById(R.id.webview);
    myWebView.clearCache(true);
    WebSettings webSettings = myWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE);
    webSettings.setUseWideViewPort(true);
    webSettings.setLoadWithOverviewMode(true);
    webSettings.setSupportZoom(true);
    webSettings.setBuiltInZoomControls(true);
    webSettings.setDisplayZoomControls(false);
    mWebViewClient = new MyWebViewClient();
    myWebView.setWebViewClient(mWebViewClient);
    myWebView.setWebChromeClient(new MyWebChromeClient());
    // Start initial page load so WebView finishes loading proxy settings.
    // Actual load of mUrl is initiated by MyWebViewClient.
    myWebView.loadData("", "text/html", null);
}
Also used : WebSettings(android.webkit.WebSettings) Network(android.net.Network) NetworkRequest(android.net.NetworkRequest) WebView(android.webkit.WebView) NetworkCapabilities(android.net.NetworkCapabilities) NetworkCallback(android.net.ConnectivityManager.NetworkCallback)

Example 2 with NetworkCallback

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

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

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

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

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

the class ConnectivityServiceTest method testRequestBenchmark.

@SmallTest
public void testRequestBenchmark() throws Exception {
    // Benchmarks connecting and switching performance in the presence of a large number of
    // NetworkRequests.
    // 1. File NUM_REQUESTS requests.
    // 2. Have a network connect. Wait for NUM_REQUESTS onAvailable callbacks to fire.
    // 3. Have a new network connect and outscore the previous. Wait for NUM_REQUESTS onLosing
    //    and NUM_REQUESTS onAvailable callbacks to fire.
    // See how long it took.
    final int NUM_REQUESTS = 90;
    final NetworkRequest request = new NetworkRequest.Builder().clearCapabilities().build();
    final NetworkCallback[] callbacks = new NetworkCallback[NUM_REQUESTS];
    final CountDownLatch availableLatch = new CountDownLatch(NUM_REQUESTS);
    final CountDownLatch losingLatch = new CountDownLatch(NUM_REQUESTS);
    final int REGISTER_TIME_LIMIT_MS = 100;
    long startTime = System.currentTimeMillis();
    for (int i = 0; i < NUM_REQUESTS; i++) {
        callbacks[i] = new NetworkCallback() {

            @Override
            public void onAvailable(Network n) {
                availableLatch.countDown();
            }

            @Override
            public void onLosing(Network n, int t) {
                losingLatch.countDown();
            }
        };
        mCm.registerNetworkCallback(request, callbacks[i]);
    }
    long timeTaken = System.currentTimeMillis() - startTime;
    String msg = String.format("Register %d callbacks: %dms, acceptable %dms", NUM_REQUESTS, timeTaken, REGISTER_TIME_LIMIT_MS);
    Log.d(TAG, msg);
    assertTrue(msg, timeTaken < REGISTER_TIME_LIMIT_MS);
    final int CONNECT_TIME_LIMIT_MS = 30;
    mCellNetworkAgent = new MockNetworkAgent(TRANSPORT_CELLULAR);
    // Don't request that the network validate, because otherwise connect() will block until
    // the network gets NET_CAPABILITY_VALIDATED, after all the callbacks below have fired,
    // and we won't actually measure anything.
    mCellNetworkAgent.connect(false);
    startTime = System.currentTimeMillis();
    if (!availableLatch.await(CONNECT_TIME_LIMIT_MS, TimeUnit.MILLISECONDS)) {
        fail(String.format("Only dispatched %d/%d onAvailable callbacks in %dms", NUM_REQUESTS - availableLatch.getCount(), NUM_REQUESTS, CONNECT_TIME_LIMIT_MS));
    }
    timeTaken = System.currentTimeMillis() - startTime;
    Log.d(TAG, String.format("Connect, %d callbacks: %dms, acceptable %dms", NUM_REQUESTS, timeTaken, CONNECT_TIME_LIMIT_MS));
    final int SWITCH_TIME_LIMIT_MS = 30;
    mWiFiNetworkAgent = new MockNetworkAgent(TRANSPORT_WIFI);
    // Give wifi a high enough score that we'll linger cell when wifi comes up.
    mWiFiNetworkAgent.adjustScore(40);
    mWiFiNetworkAgent.connect(false);
    startTime = System.currentTimeMillis();
    if (!losingLatch.await(SWITCH_TIME_LIMIT_MS, TimeUnit.MILLISECONDS)) {
        fail(String.format("Only dispatched %d/%d onLosing callbacks in %dms", NUM_REQUESTS - losingLatch.getCount(), NUM_REQUESTS, SWITCH_TIME_LIMIT_MS));
    }
    timeTaken = System.currentTimeMillis() - startTime;
    Log.d(TAG, String.format("Linger, %d callbacks: %dms, acceptable %dms", NUM_REQUESTS, timeTaken, SWITCH_TIME_LIMIT_MS));
    final int UNREGISTER_TIME_LIMIT_MS = 10;
    startTime = System.currentTimeMillis();
    for (int i = 0; i < NUM_REQUESTS; i++) {
        mCm.unregisterNetworkCallback(callbacks[i]);
    }
    timeTaken = System.currentTimeMillis() - startTime;
    msg = String.format("Unregister %d callbacks: %dms, acceptable %dms", NUM_REQUESTS, timeTaken, UNREGISTER_TIME_LIMIT_MS);
    Log.d(TAG, msg);
    assertTrue(msg, timeTaken < UNREGISTER_TIME_LIMIT_MS);
}
Also used : Network(android.net.Network) NetworkRequest(android.net.NetworkRequest) CountDownLatch(java.util.concurrent.CountDownLatch) NetworkCallback(android.net.ConnectivityManager.NetworkCallback) SmallTest(android.test.suitebuilder.annotation.SmallTest)

Example 5 with NetworkCallback

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

the class CaptivePortalLoginActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mCm = ConnectivityManager.from(this);
    mNetwork = getIntent().getParcelableExtra(ConnectivityManager.EXTRA_NETWORK);
    mCaptivePortal = getIntent().getParcelableExtra(ConnectivityManager.EXTRA_CAPTIVE_PORTAL);
    mUserAgent = getIntent().getParcelableExtra(ConnectivityManager.EXTRA_CAPTIVE_PORTAL_USER_AGENT);
    mUrl = getUrl();
    if (mUrl == null) {
        // getUrl() failed to parse the url provided in the intent: bail out in a way that
        // at least provides network access.
        done(Result.WANTED_AS_IS);
        return;
    }
    if (DBG) {
        Log.d(TAG, String.format("onCreate for %s", mUrl.toString()));
    }
    // Also initializes proxy system properties.
    mCm.bindProcessToNetwork(mNetwork);
    // Proxy system properties must be initialized before setContentView is called because
    // setContentView initializes the WebView logic which in turn reads the system properties.
    setContentView(R.layout.activity_captive_portal_login);
    getActionBar().setDisplayShowHomeEnabled(false);
    // Exit app if Network disappears.
    final NetworkCapabilities networkCapabilities = mCm.getNetworkCapabilities(mNetwork);
    if (networkCapabilities == null) {
        finishAndRemoveTask();
        return;
    }
    mNetworkCallback = new NetworkCallback() {

        @Override
        public void onLost(Network lostNetwork) {
            if (mNetwork.equals(lostNetwork))
                done(Result.UNWANTED);
        }
    };
    final NetworkRequest.Builder builder = new NetworkRequest.Builder();
    for (int transportType : networkCapabilities.getTransportTypes()) {
        builder.addTransportType(transportType);
    }
    mCm.registerNetworkCallback(builder.build(), mNetworkCallback);
    final WebView myWebView = (WebView) findViewById(R.id.webview);
    myWebView.clearCache(true);
    WebSettings webSettings = myWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE);
    webSettings.setUseWideViewPort(true);
    webSettings.setLoadWithOverviewMode(true);
    webSettings.setSupportZoom(true);
    webSettings.setBuiltInZoomControls(true);
    webSettings.setDisplayZoomControls(false);
    mWebViewClient = new MyWebViewClient();
    myWebView.setWebViewClient(mWebViewClient);
    myWebView.setWebChromeClient(new MyWebChromeClient());
    // Start initial page load so WebView finishes loading proxy settings.
    // Actual load of mUrl is initiated by MyWebViewClient.
    myWebView.loadData("", "text/html", null);
}
Also used : WebSettings(android.webkit.WebSettings) Network(android.net.Network) NetworkRequest(android.net.NetworkRequest) WebView(android.webkit.WebView) 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