From 08ffa580f6756129929667785408da377b363787 Mon Sep 17 00:00:00 2001 From: Stephen Chen Date: Fri, 25 Aug 2017 14:51:32 -0700 Subject: ONA: Implement connection attempt and failure callback. Bug: 37357441 Test: frameworks/opt/net/wifi/tests/wifitests/runtests.sh Change-Id: I2d531304598249dcb18bdc45492e48ee90ddff5f --- .../android/server/wifi/OpenNetworkNotifier.java | 66 ++++++++++++++++++++-- .../server/wifi/WifiConnectivityManager.java | 11 ++++ .../java/com/android/server/wifi/WifiInjector.java | 3 +- .../com/android/server/wifi/WifiStateMachine.java | 5 +- 4 files changed, 78 insertions(+), 7 deletions(-) (limited to 'service') diff --git a/service/java/com/android/server/wifi/OpenNetworkNotifier.java b/service/java/com/android/server/wifi/OpenNetworkNotifier.java index 692c8e22c..279223759 100644 --- a/service/java/com/android/server/wifi/OpenNetworkNotifier.java +++ b/service/java/com/android/server/wifi/OpenNetworkNotifier.java @@ -24,8 +24,12 @@ import android.content.Intent; import android.content.IntentFilter; import android.database.ContentObserver; import android.net.wifi.ScanResult; +import android.net.wifi.WifiConfiguration; +import android.net.wifi.WifiManager; import android.os.Handler; import android.os.Looper; +import android.os.Message; +import android.os.Messenger; import android.os.UserHandle; import android.os.UserManager; import android.provider.Settings; @@ -34,6 +38,7 @@ import android.util.Log; import com.android.internal.annotations.VisibleForTesting; import com.android.internal.messages.nano.SystemMessageProto.SystemMessage; +import com.android.server.wifi.util.ScanResultUtil; import java.io.FileDescriptor; import java.io.PrintWriter; @@ -54,6 +59,8 @@ public class OpenNetworkNotifier { "com.android.server.wifi.OpenNetworkNotifier.USER_DISMISSED_NOTIFICATION"; static final String ACTION_USER_TAPPED_CONTENT = "com.android.server.wifi.OpenNetworkNotifier.USER_TAPPED_CONTENT"; + static final String ACTION_CONNECT_TO_NETWORK = + "com.android.server.wifi.OpenNetworkNotifier.CONNECT_TO_NETWORK"; /** Identifier of the {@link SsidSetStoreData}. */ private static final String STORE_DATA_IDENTIFIER = "OpenNetworkNotifierBlacklist"; @@ -85,6 +92,8 @@ public class OpenNetworkNotifier { private final FrameworkFacade mFrameworkFacade; private final Clock mClock; private final WifiConfigManager mConfigManager; + private final WifiStateMachine mWifiStateMachine; + private final Messenger mSrcMessenger; private final OpenNetworkRecommender mOpenNetworkRecommender; private final OpenNetworkNotificationBuilder mOpenNetworkNotificationBuilder; @@ -97,15 +106,18 @@ public class OpenNetworkNotifier { Clock clock, WifiConfigManager wifiConfigManager, WifiConfigStore wifiConfigStore, + WifiStateMachine wifiStateMachine, OpenNetworkRecommender openNetworkRecommender) { mContext = context; mHandler = new Handler(looper); mFrameworkFacade = framework; mClock = clock; mConfigManager = wifiConfigManager; + mWifiStateMachine = wifiStateMachine; mOpenNetworkRecommender = openNetworkRecommender; mOpenNetworkNotificationBuilder = new OpenNetworkNotificationBuilder(context, framework); mScreenOn = false; + mSrcMessenger = new Messenger(new Handler(looper, mConnectionStateCallback)); mBlacklistedSsids = new ArraySet<>(); wifiConfigStore.registerStoreData(new SsidSetStoreData( @@ -122,6 +134,7 @@ public class OpenNetworkNotifier { IntentFilter filter = new IntentFilter(); filter.addAction(ACTION_USER_DISMISSED_NOTIFICATION); filter.addAction(ACTION_USER_TAPPED_CONTENT); + filter.addAction(ACTION_CONNECT_TO_NETWORK); mContext.registerReceiver( mBroadcastReceiver, filter, null /* broadcastPermission */, mHandler); } @@ -130,14 +143,38 @@ public class OpenNetworkNotifier { new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { - if (ACTION_USER_TAPPED_CONTENT.equals(intent.getAction())) { - handleUserClickedContentAction(); - } else if (ACTION_USER_DISMISSED_NOTIFICATION.equals(intent.getAction())) { - handleUserDismissedAction(); + switch (intent.getAction()) { + case ACTION_USER_TAPPED_CONTENT: + handleUserClickedContentAction(); + break; + case ACTION_USER_DISMISSED_NOTIFICATION: + handleUserDismissedAction(); + break; + case ACTION_CONNECT_TO_NETWORK: + handleConnectToNetworkAction(); + break; + default: + Log.e(TAG, "Unknown action " + intent.getAction()); } } }; + private final Handler.Callback mConnectionStateCallback = (Message msg) -> { + switch (msg.what) { + // Success here means that an attempt to connect to the network has been initiated. + // Successful connection updates are received via the + // WifiConnectivityManager#handleConnectionStateChanged() callback. + case WifiManager.CONNECT_NETWORK_SUCCEEDED: + break; + case WifiManager.CONNECT_NETWORK_FAILED: + handleConnectionFailure(); + break; + default: + Log.e(TAG, "Unknown message " + msg.what); + } + return true; + }; + /** * Clears the pending notification. This is called by {@link WifiConnectivityManager} on stop. * @@ -213,6 +250,27 @@ public class OpenNetworkNotifier { mNotificationRepeatTime = mClock.getWallClockMillis() + mNotificationRepeatDelay; } + private void handleConnectToNetworkAction() { + if (mRecommendedNetwork == null) { + return; + } + Log.d(TAG, "User initiated connection to recommended network: " + mRecommendedNetwork.SSID); + WifiConfiguration network = ScanResultUtil.createNetworkFromScanResult(mRecommendedNetwork); + Message msg = Message.obtain(); + msg.what = WifiManager.CONNECT_NETWORK; + msg.arg1 = WifiConfiguration.INVALID_NETWORK_ID; + msg.obj = network; + msg.replyTo = mSrcMessenger; + mWifiStateMachine.sendMessage(msg); + } + + /** + * Handles when a Wi-Fi connection attempt failed. + */ + public void handleConnectionFailure() { + // Stub. Should post connection failure notification once implemented. + } + /** Opens Wi-Fi picker. */ private void handleUserClickedContentAction() { mNotificationShown = false; diff --git a/service/java/com/android/server/wifi/WifiConnectivityManager.java b/service/java/com/android/server/wifi/WifiConnectivityManager.java index 9603357a1..a21c8473c 100644 --- a/service/java/com/android/server/wifi/WifiConnectivityManager.java +++ b/service/java/com/android/server/wifi/WifiConnectivityManager.java @@ -1085,6 +1085,17 @@ public class WifiConnectivityManager { } } + /** + * Handler when a WiFi connection attempt ended. + * + * @param failureCode {@link WifiMetrics.ConnectionEvent} failure code. + */ + public void handleConnectionAttemptEnded(int failureCode) { + if (failureCode != WifiMetrics.ConnectionEvent.FAILURE_NONE) { + mOpenNetworkNotifier.handleConnectionFailure(); + } + } + /** * Handler when user toggles whether untrusted connection is allowed */ diff --git a/service/java/com/android/server/wifi/WifiInjector.java b/service/java/com/android/server/wifi/WifiInjector.java index 26d702477..a0d0e82d5 100644 --- a/service/java/com/android/server/wifi/WifiInjector.java +++ b/service/java/com/android/server/wifi/WifiInjector.java @@ -233,7 +233,8 @@ public class WifiInjector { mCertManager = new WifiCertManager(mContext); mOpenNetworkNotifier = new OpenNetworkNotifier(mContext, mWifiStateMachineHandlerThread.getLooper(), mFrameworkFacade, mClock, - mWifiConfigManager, mWifiConfigStore, new OpenNetworkRecommender()); + mWifiConfigManager, mWifiConfigStore, mWifiStateMachine, + new OpenNetworkRecommender()); mLockManager = new WifiLockManager(mContext, BatteryStatsService.getService()); mWifiController = new WifiController(mContext, mWifiStateMachine, mSettingsStore, mLockManager, mWifiServiceHandlerThread.getLooper(), mFrameworkFacade); diff --git a/service/java/com/android/server/wifi/WifiStateMachine.java b/service/java/com/android/server/wifi/WifiStateMachine.java index ce9c09432..90fb2f6e1 100644 --- a/service/java/com/android/server/wifi/WifiStateMachine.java +++ b/service/java/com/android/server/wifi/WifiStateMachine.java @@ -3425,11 +3425,12 @@ public class WifiStateMachine extends StateMachine implements WifiNative.WifiRss } /** - * Inform other components (WifiMetrics, WifiDiagnostics, etc.) that the current connection attempt - * has concluded. + * Inform other components (WifiMetrics, WifiDiagnostics, WifiConnectivityManager, etc.) that + * the current connection attempt has concluded. */ private void reportConnectionAttemptEnd(int level2FailureCode, int connectivityFailureCode) { mWifiMetrics.endConnectionEvent(level2FailureCode, connectivityFailureCode); + mWifiConnectivityManager.handleConnectionAttemptEnded(level2FailureCode); switch (level2FailureCode) { case WifiMetrics.ConnectionEvent.FAILURE_NONE: // Ideally, we'd wait until IP reachability has been confirmed. this code falls -- cgit v1.2.3