import android.annotation.IntDef;
import android.annotation.Nullable;
+import android.annotation.RequiresPermission;
import android.annotation.SdkConstant;
import android.annotation.SdkConstant.SdkConstantType;
import android.annotation.SystemApi;
}
/**
+ * Requests that the system open the captive portal app on the specified network.
+ *
+ * @param network The network to log into.
+ *
+ * @hide
+ */
+ @RequiresPermission(android.Manifest.permission.CONNECTIVITY_INTERNAL)
+ public void startCaptivePortalApp(Network network) {
+ try {
+ mService.startCaptivePortalApp(network);
+ } catch (RemoteException e) {
+ throw e.rethrowFromSystemServer();
+ }
+ }
+
+ /**
* It is acceptable to briefly use multipath data to provide seamless connectivity for
* time-sensitive user-facing operations when the system default network is temporarily
* unresponsive. The amount of data should be limited (less than one megabyte), and the
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.Resources;
+import android.net.CaptivePortal;
import android.net.ConnectivityManager;
import android.net.ConnectivityManager.NetworkCallback;
import android.net.ConnectivityManager.PacketKeepalive;
import android.os.Parcelable;
import android.os.Process;
import android.os.SystemClock;
+import android.os.UserHandle;
import android.provider.Settings;
import android.test.AndroidTestCase;
import android.test.mock.MockContentResolver;
private static final int TIMEOUT_MS = 500;
private static final int TEST_LINGER_DELAY_MS = 120;
- private BroadcastInterceptingContext mServiceContext;
+ private MockContext mServiceContext;
private WrappedConnectivityService mService;
private WrappedConnectivityManager mCm;
private MockNetworkAgent mWiFiNetworkAgent;
private final MockContentResolver mContentResolver;
@Spy private Resources mResources;
+ private final LinkedBlockingQueue<Intent> mStartedActivities = new LinkedBlockingQueue<>();
MockContext(Context base) {
super(base);
}
@Override
+ public void startActivityAsUser(Intent intent, UserHandle handle) {
+ mStartedActivities.offer(intent);
+ }
+
+ public Intent expectStartActivityIntent(int timeoutMs) {
+ Intent intent = null;
+ try {
+ intent = mStartedActivities.poll(timeoutMs, TimeUnit.MILLISECONDS);
+ } catch (InterruptedException e) {}
+ assertNotNull("Did not receive sign-in intent after " + timeoutMs + "ms", intent);
+ return intent;
+ }
+
+ public void expectNoStartActivityIntent(int timeoutMs) {
+ try {
+ assertNull("Received unexpected Intent to start activity",
+ mStartedActivities.poll(timeoutMs, TimeUnit.MILLISECONDS));
+ } catch (InterruptedException e) {}
+ }
+
+ @Override
public Object getSystemService(String name) {
if (Context.CONNECTIVITY_SERVICE.equals(name)) return mCm;
if (Context.NOTIFICATION_SERVICE.equals(name)) return mock(NotificationManager.class);
}
@SmallTest
+ public void testCaptivePortalApp() {
+ final TestNetworkCallback captivePortalCallback = new TestNetworkCallback();
+ final NetworkRequest captivePortalRequest = new NetworkRequest.Builder()
+ .addCapability(NET_CAPABILITY_CAPTIVE_PORTAL).build();
+ mCm.registerNetworkCallback(captivePortalRequest, captivePortalCallback);
+
+ final TestNetworkCallback validatedCallback = new TestNetworkCallback();
+ final NetworkRequest validatedRequest = new NetworkRequest.Builder()
+ .addCapability(NET_CAPABILITY_VALIDATED).build();
+ mCm.registerNetworkCallback(validatedRequest, validatedCallback);
+
+ // Bring up wifi.
+ mWiFiNetworkAgent = new MockNetworkAgent(TRANSPORT_WIFI);
+ mWiFiNetworkAgent.connect(true);
+ validatedCallback.expectAvailableAndValidatedCallbacks(mWiFiNetworkAgent);
+ Network wifiNetwork = mWiFiNetworkAgent.getNetwork();
+
+ // Check that calling startCaptivePortalApp does nothing.
+ final int fastTimeoutMs = 100;
+ mCm.startCaptivePortalApp(wifiNetwork);
+ mServiceContext.expectNoStartActivityIntent(fastTimeoutMs);
+
+ // Turn into a captive portal.
+ mWiFiNetworkAgent.getWrappedNetworkMonitor().gen204ProbeResult = 302;
+ mCm.reportNetworkConnectivity(wifiNetwork, false);
+ captivePortalCallback.expectAvailableCallbacks(mWiFiNetworkAgent);
+ validatedCallback.expectCallback(CallbackState.LOST, mWiFiNetworkAgent);
+
+ // Check that startCaptivePortalApp sends the expected intent.
+ mCm.startCaptivePortalApp(wifiNetwork);
+ Intent intent = mServiceContext.expectStartActivityIntent(TIMEOUT_MS);
+ assertEquals(ConnectivityManager.ACTION_CAPTIVE_PORTAL_SIGN_IN, intent.getAction());
+ assertEquals(wifiNetwork, intent.getExtra(ConnectivityManager.EXTRA_NETWORK));
+
+ // Have the app report that the captive portal is dismissed, and check that we revalidate.
+ mWiFiNetworkAgent.getWrappedNetworkMonitor().gen204ProbeResult = 204;
+ CaptivePortal c = (CaptivePortal) intent.getExtra(ConnectivityManager.EXTRA_CAPTIVE_PORTAL);
+ c.reportCaptivePortalDismissed();
+ validatedCallback.expectAvailableCallbacks(mWiFiNetworkAgent);
+ captivePortalCallback.expectCallback(CallbackState.LOST, mWiFiNetworkAgent);
+
+ mCm.unregisterNetworkCallback(validatedCallback);
+ mCm.unregisterNetworkCallback(captivePortalCallback);
+ }
+
+ @SmallTest
public void testAvoidOrIgnoreCaptivePortals() {
final TestNetworkCallback captivePortalCallback = new TestNetworkCallback();
final NetworkRequest captivePortalRequest = new NetworkRequest.Builder()