From 938bfba0c3e8f529cf01a407920c87a6c0826cb8 Mon Sep 17 00:00:00 2001 From: Chiachang Wang Date: Thu, 9 Jan 2020 13:50:55 +0800 Subject: [PATCH] Support reevaluation request from captive portal app CaptivePortalLogin use similar way to validate the network as NetworkMonitor. Provide API to reuse NetworkMonitor facilities for consistent captive portal detection. Bug: 134892996 Test: atest FrameworksNetTests NetworkStackTests NetworkStackNextTests Test: manually test with captive portal Wi-Fi Test: make system-api-stubs-docs-update-current-api \ test-api-stubs-docs-update-current-api Change-Id: I6f039eae7ceb18eb6c3cf15587cdc27a7fe35b00 --- core/java/android/net/CaptivePortal.java | 25 +++++++++++++++++ .../android/server/ConnectivityService.java | 27 ++++++++++++++----- .../java/android/net/CaptivePortalTest.java | 11 ++++++++ 3 files changed, 57 insertions(+), 6 deletions(-) diff --git a/core/java/android/net/CaptivePortal.java b/core/java/android/net/CaptivePortal.java index a66fcae7d4..fb35b4bde3 100644 --- a/core/java/android/net/CaptivePortal.java +++ b/core/java/android/net/CaptivePortal.java @@ -60,6 +60,18 @@ public class CaptivePortal implements Parcelable { @SystemApi @TestApi public static final int APP_RETURN_WANTED_AS_IS = 2; + /** Event offset of request codes from captive portal application. */ + private static final int APP_REQUEST_BASE = 100; + /** + * Request code from the captive portal application, indicating that the network condition may + * have changed and the network should be re-validated. + * @see ICaptivePortal#appRequest(int) + * @see android.net.INetworkMonitor#forceReevaluation(int) + * @hide + */ + @SystemApi + @TestApi + public static final int APP_REQUEST_REEVALUATION_REQUIRED = APP_REQUEST_BASE + 0; private final IBinder mBinder; @@ -135,6 +147,19 @@ public class CaptivePortal implements Parcelable { } } + /** + * Request that the system reevaluates the captive portal status. + * @hide + */ + @SystemApi + @TestApi + public void reevaluateNetwork() { + try { + ICaptivePortal.Stub.asInterface(mBinder).appRequest(APP_REQUEST_REEVALUATION_REQUIRED); + } catch (RemoteException e) { + } + } + /** * Log a captive portal login event. * @param eventId one of the CAPTIVE_PORTAL_LOGIN_* constants in metrics_constants.proto. diff --git a/services/core/java/com/android/server/ConnectivityService.java b/services/core/java/com/android/server/ConnectivityService.java index 49cfa7b6de..a728342707 100644 --- a/services/core/java/com/android/server/ConnectivityService.java +++ b/services/core/java/com/android/server/ConnectivityService.java @@ -3602,16 +3602,31 @@ public class ConnectivityService extends IConnectivityManager.Stub enforceSettingsPermission(); } - // getNetworkAgentInfoForNetwork is thread-safe - final NetworkAgentInfo nai = getNetworkAgentInfoForNetwork(mNetwork); - if (nai == null) return; - - // nai.networkMonitor() is thread-safe - final NetworkMonitorManager nm = nai.networkMonitor(); + final NetworkMonitorManager nm = getNetworkMonitorManager(mNetwork); if (nm == null) return; nm.notifyCaptivePortalAppFinished(response); } + @Override + public void appRequest(final int request) { + final NetworkMonitorManager nm = getNetworkMonitorManager(mNetwork); + if (nm == null) return; + + if (request == CaptivePortal.APP_REQUEST_REEVALUATION_REQUIRED) { + nm.forceReevaluation(Binder.getCallingUid()); + } + } + + @Nullable + private NetworkMonitorManager getNetworkMonitorManager(final Network network) { + // getNetworkAgentInfoForNetwork is thread-safe + final NetworkAgentInfo nai = getNetworkAgentInfoForNetwork(network); + if (nai == null) return null; + + // nai.networkMonitor() is thread-safe + return nai.networkMonitor(); + } + @Override public void logEvent(int eventId, String packageName) { enforceSettingsPermission(); diff --git a/tests/net/common/java/android/net/CaptivePortalTest.java b/tests/net/common/java/android/net/CaptivePortalTest.java index eed7159ffd..ca4ba63142 100644 --- a/tests/net/common/java/android/net/CaptivePortalTest.java +++ b/tests/net/common/java/android/net/CaptivePortalTest.java @@ -43,6 +43,11 @@ public class CaptivePortalTest { mCode = response; } + @Override + public void appRequest(final int request) throws RemoteException { + mCode = request; + } + @Override public void logEvent(int eventId, String packageName) throws RemoteException { mCode = eventId; @@ -79,6 +84,12 @@ public class CaptivePortalTest { assertEquals(result.mCode, CaptivePortal.APP_RETURN_WANTED_AS_IS); } + @Test + public void testReevaluateNetwork() { + final MyCaptivePortalImpl result = runCaptivePortalTest(c -> c.reevaluateNetwork()); + assertEquals(result.mCode, CaptivePortal.APP_REQUEST_REEVALUATION_REQUIRED); + } + @Test public void testLogEvent() { final MyCaptivePortalImpl result = runCaptivePortalTest(c -> c.logEvent(