summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorandroid-build-team Robot <android-build-team-robot@google.com>2017-09-10 07:28:45 +0000
committerandroid-build-team Robot <android-build-team-robot@google.com>2017-09-10 07:28:45 +0000
commitb94b47e8de99697600f79623d127e3d19e753106 (patch)
tree0618c4c1fc1d262b2638210df87732f57e662953
parentfe18b4c251efd0258f4c22bc16aef9b25a5fad48 (diff)
parentf0653448fc27d0f73d58a3abf948ef38609331e4 (diff)
downloadandroid_frameworks_opt_net_wifi-b94b47e8de99697600f79623d127e3d19e753106.tar.gz
android_frameworks_opt_net_wifi-b94b47e8de99697600f79623d127e3d19e753106.tar.bz2
android_frameworks_opt_net_wifi-b94b47e8de99697600f79623d127e3d19e753106.zip
release-request-946c768b-83eb-4b7b-83e7-5737856fa8ec-for-git_oc-mr1-release-4326576 snap-temp-L80300000101054689
Change-Id: Ic238c9bb3a576e1ffdf5e0f33562950a36852250
-rw-r--r--service/java/com/android/server/wifi/ConnectToNetworkNotificationBuilder.java146
-rw-r--r--service/java/com/android/server/wifi/OpenNetworkNotificationBuilder.java91
-rw-r--r--service/java/com/android/server/wifi/OpenNetworkNotifier.java215
-rw-r--r--service/java/com/android/server/wifi/WifiConnectivityManager.java2
-rw-r--r--service/java/com/android/server/wifi/WifiInjector.java3
-rw-r--r--tests/wifitests/src/com/android/server/wifi/OpenNetworkNotifierTest.java265
-rw-r--r--tests/wifitests/src/com/android/server/wifi/WifiConnectivityManagerTest.java9
7 files changed, 531 insertions, 200 deletions
diff --git a/service/java/com/android/server/wifi/ConnectToNetworkNotificationBuilder.java b/service/java/com/android/server/wifi/ConnectToNetworkNotificationBuilder.java
new file mode 100644
index 000000000..38c0ad058
--- /dev/null
+++ b/service/java/com/android/server/wifi/ConnectToNetworkNotificationBuilder.java
@@ -0,0 +1,146 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.server.wifi;
+
+import android.app.Notification;
+import android.app.PendingIntent;
+import android.content.Context;
+import android.content.Intent;
+import android.content.res.Resources;
+import android.net.wifi.ScanResult;
+
+import com.android.internal.R;
+import com.android.internal.notification.SystemNotificationChannels;
+
+/**
+ * Helper to create notifications for {@link OpenNetworkNotifier}.
+ */
+public class ConnectToNetworkNotificationBuilder {
+
+ /** Intent when user dismissed the "Connect to Network" notification. */
+ public static final String ACTION_USER_DISMISSED_NOTIFICATION =
+ "com.android.server.wifi.ConnectToNetworkNotification.USER_DISMISSED_NOTIFICATION";
+
+ /** Intent when user tapped action button to connect to recommended network. */
+ public static final String ACTION_CONNECT_TO_NETWORK =
+ "com.android.server.wifi.ConnectToNetworkNotification.CONNECT_TO_NETWORK";
+
+ /** Intent when user tapped action button to open Wi-Fi Settings. */
+ public static final String ACTION_PICK_WIFI_NETWORK =
+ "com.android.server.wifi.ConnectToNetworkNotification.PICK_WIFI_NETWORK";
+
+ /** Intent when user tapped "Failed to connect" notification to open Wi-Fi Settings. */
+ public static final String ACTION_PICK_WIFI_NETWORK_AFTER_CONNECT_FAILURE =
+ "com.android.server.wifi.ConnectToNetworkNotification.PICK_NETWORK_AFTER_FAILURE";
+
+ private Context mContext;
+ private Resources mResources;
+ private FrameworkFacade mFrameworkFacade;
+
+ public ConnectToNetworkNotificationBuilder(
+ Context context,
+ FrameworkFacade framework) {
+ mContext = context;
+ mResources = context.getResources();
+ mFrameworkFacade = framework;
+ }
+
+ /**
+ * Creates the connect to network notification that alerts users of a recommended connectable
+ * network.
+ *
+ * There are two actions - "Options" link to the Wi-Fi picker activity, and "Connect" prompts
+ * the connection to the recommended network.
+ *
+ * @param network The network to be recommended
+ */
+ public Notification createConnectToNetworkNotification(ScanResult network) {
+ Notification.Action connectAction = new Notification.Action.Builder(
+ null /* icon */,
+ mResources.getText(R.string.wifi_available_action_connect),
+ getPrivateBroadcast(ACTION_CONNECT_TO_NETWORK)).build();
+ Notification.Action allNetworksAction = new Notification.Action.Builder(
+ null /* icon */,
+ mResources.getText(R.string.wifi_available_action_all_networks),
+ getPrivateBroadcast(ACTION_PICK_WIFI_NETWORK)).build();
+ return createNotificationBuilder(
+ mContext.getText(R.string.wifi_available_title), network.SSID)
+ .addAction(connectAction)
+ .addAction(allNetworksAction)
+ .build();
+ }
+
+ /**
+ * Creates the notification that indicates the controller is attempting to connect to the
+ * recommended network.
+ *
+ * @param network The network to be recommended
+ */
+ public Notification createNetworkConnectingNotification(ScanResult network) {
+ return createNotificationBuilder(
+ mContext.getText(R.string.wifi_available_title_connecting), network.SSID)
+ .setProgress(0 /* max */, 0 /* progress */, true /* indeterminate */)
+ .build();
+ }
+
+ /**
+ * Creates the notification that indicates the controller successfully connected to the
+ * recommended network.
+ *
+ * @param network The network to be recommended
+ */
+ public Notification createNetworkConnectedNotification(ScanResult network) {
+ return createNotificationBuilder(
+ mContext.getText(R.string.wifi_available_title_connected), network.SSID)
+ .build();
+ }
+
+ /**
+ * Creates the notification that indicates the controller failed to connect to the recommended
+ * network. Tapping this notification opens the wifi picker.
+ */
+ public Notification createNetworkFailedNotification() {
+ return createNotificationBuilder(
+ mContext.getText(R.string.wifi_available_title_failed_to_connect),
+ mContext.getText(R.string.wifi_available_content_failed_to_connect))
+ .setContentIntent(
+ getPrivateBroadcast(ACTION_PICK_WIFI_NETWORK_AFTER_CONNECT_FAILURE))
+ .setAutoCancel(true)
+ .build();
+ }
+
+ private Notification.Builder createNotificationBuilder(
+ CharSequence title, CharSequence content) {
+ return mFrameworkFacade.makeNotificationBuilder(mContext,
+ SystemNotificationChannels.NETWORK_AVAILABLE)
+ .setSmallIcon(R.drawable.stat_notify_wifi_in_range)
+ .setTicker(title)
+ .setContentTitle(title)
+ .setContentText(content)
+ .setDeleteIntent(getPrivateBroadcast(ACTION_USER_DISMISSED_NOTIFICATION))
+ .setShowWhen(false)
+ .setLocalOnly(true)
+ .setColor(mResources.getColor(R.color.system_notification_accent_color,
+ mContext.getTheme()));
+ }
+
+ private PendingIntent getPrivateBroadcast(String action) {
+ Intent intent = new Intent(action).setPackage("android");
+ return mFrameworkFacade.getBroadcast(
+ mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
+ }
+}
diff --git a/service/java/com/android/server/wifi/OpenNetworkNotificationBuilder.java b/service/java/com/android/server/wifi/OpenNetworkNotificationBuilder.java
deleted file mode 100644
index 5963b57a3..000000000
--- a/service/java/com/android/server/wifi/OpenNetworkNotificationBuilder.java
+++ /dev/null
@@ -1,91 +0,0 @@
-/*
- * Copyright (C) 2017 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package com.android.server.wifi;
-
-import static com.android.server.wifi.OpenNetworkNotifier.ACTION_USER_DISMISSED_NOTIFICATION;
-import static com.android.server.wifi.OpenNetworkNotifier.ACTION_USER_TAPPED_CONTENT;
-
-import android.app.Notification;
-import android.app.PendingIntent;
-import android.content.Context;
-import android.content.Intent;
-import android.content.res.Resources;
-
-import com.android.internal.R;
-import com.android.internal.notification.SystemNotificationChannels;
-
-/**
- * Helper to create notifications for {@link OpenNetworkNotifier}.
- */
-public class OpenNetworkNotificationBuilder {
-
- private Context mContext;
- private Resources mResources;
- private FrameworkFacade mFrameworkFacade;
-
- public OpenNetworkNotificationBuilder(
- Context context,
- FrameworkFacade framework) {
- mContext = context;
- mResources = context.getResources();
- mFrameworkFacade = framework;
- }
-
- /**
- * Creates the open network available notification that alerts users there are open networks
- * nearby.
- */
- public Notification createOpenNetworkAvailableNotification(int numNetworks) {
-
- CharSequence title = mResources.getQuantityText(
- com.android.internal.R.plurals.wifi_available, numNetworks);
- CharSequence content = mResources.getQuantityText(
- com.android.internal.R.plurals.wifi_available_detailed, numNetworks);
-
- PendingIntent contentIntent =
- mFrameworkFacade.getBroadcast(
- mContext,
- 0,
- new Intent(ACTION_USER_TAPPED_CONTENT),
- PendingIntent.FLAG_UPDATE_CURRENT);
- return createNotificationBuilder(title, content)
- .setContentIntent(contentIntent)
- .build();
- }
-
- private Notification.Builder createNotificationBuilder(
- CharSequence title, CharSequence content) {
- PendingIntent deleteIntent =
- mFrameworkFacade.getBroadcast(
- mContext,
- 0,
- new Intent(ACTION_USER_DISMISSED_NOTIFICATION),
- PendingIntent.FLAG_UPDATE_CURRENT);
- return mFrameworkFacade.makeNotificationBuilder(mContext,
- SystemNotificationChannels.NETWORK_AVAILABLE)
- .setSmallIcon(R.drawable.stat_notify_wifi_in_range)
- .setAutoCancel(true)
- .setTicker(title)
- .setContentTitle(title)
- .setContentText(content)
- .setDeleteIntent(deleteIntent)
- .setShowWhen(false)
- .setLocalOnly(true)
- .setColor(mResources.getColor(R.color.system_notification_accent_color,
- mContext.getTheme()));
- }
-}
diff --git a/service/java/com/android/server/wifi/OpenNetworkNotifier.java b/service/java/com/android/server/wifi/OpenNetworkNotifier.java
index 279223759..31ff44b72 100644
--- a/service/java/com/android/server/wifi/OpenNetworkNotifier.java
+++ b/service/java/com/android/server/wifi/OpenNetworkNotifier.java
@@ -16,7 +16,14 @@
package com.android.server.wifi;
+import static com.android.server.wifi.ConnectToNetworkNotificationBuilder.ACTION_CONNECT_TO_NETWORK;
+import static com.android.server.wifi.ConnectToNetworkNotificationBuilder.ACTION_PICK_WIFI_NETWORK;
+import static com.android.server.wifi.ConnectToNetworkNotificationBuilder.ACTION_PICK_WIFI_NETWORK_AFTER_CONNECT_FAILURE;
+import static com.android.server.wifi.ConnectToNetworkNotificationBuilder.ACTION_USER_DISMISSED_NOTIFICATION;
+
+import android.annotation.IntDef;
import android.annotation.NonNull;
+import android.app.Notification;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
@@ -42,6 +49,8 @@ import com.android.server.wifi.util.ScanResultUtil;
import java.io.FileDescriptor;
import java.io.PrintWriter;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
import java.util.List;
import java.util.Set;
@@ -55,12 +64,39 @@ public class OpenNetworkNotifier {
private static final String TAG = "OpenNetworkNotifier";
- static final String ACTION_USER_DISMISSED_NOTIFICATION =
- "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";
+ /** Time in milliseconds to display the Connecting notification. */
+ private static final int TIME_TO_SHOW_CONNECTING_MILLIS = 10000;
+
+ /** Time in milliseconds to display the Connected notification. */
+ private static final int TIME_TO_SHOW_CONNECTED_MILLIS = 5000;
+
+ /** Time in milliseconds to display the Failed To Connect notification. */
+ private static final int TIME_TO_SHOW_FAILED_MILLIS = 5000;
+
+ /** The state of the notification */
+ @IntDef({
+ STATE_NO_NOTIFICATION,
+ STATE_SHOWING_RECOMMENDATION_NOTIFICATION,
+ STATE_CONNECTING_IN_NOTIFICATION,
+ STATE_CONNECTED_NOTIFICATION,
+ STATE_CONNECT_FAILED_NOTIFICATION
+ })
+ @Retention(RetentionPolicy.SOURCE)
+ private @interface State {}
+
+ /** No recommendation is made and no notifications are shown. */
+ private static final int STATE_NO_NOTIFICATION = 0;
+ /** The initial notification recommending an open network to connect to is shown. */
+ private static final int STATE_SHOWING_RECOMMENDATION_NOTIFICATION = 1;
+ /** The notification of status of connecting to the recommended network is shown. */
+ private static final int STATE_CONNECTING_IN_NOTIFICATION = 2;
+ /** The notification that the connection to the recommended network was successful is shown. */
+ private static final int STATE_CONNECTED_NOTIFICATION = 3;
+ /** The notification to show that connection to the recommended network failed is shown. */
+ private static final int STATE_CONNECT_FAILED_NOTIFICATION = 4;
+
+ /** Current state of the notification. */
+ @State private int mState = STATE_NO_NOTIFICATION;
/** Identifier of the {@link SsidSetStoreData}. */
private static final String STORE_DATA_IDENTIFIER = "OpenNetworkNotifierBlacklist";
@@ -79,8 +115,6 @@ public class OpenNetworkNotifier {
/** Whether the user has set the setting to show the 'available networks' notification. */
private boolean mSettingEnabled;
- /** Whether the notification is being shown. */
- private boolean mNotificationShown;
/** Whether the screen is on or not. */
private boolean mScreenOn;
@@ -95,7 +129,7 @@ public class OpenNetworkNotifier {
private final WifiStateMachine mWifiStateMachine;
private final Messenger mSrcMessenger;
private final OpenNetworkRecommender mOpenNetworkRecommender;
- private final OpenNetworkNotificationBuilder mOpenNetworkNotificationBuilder;
+ private final ConnectToNetworkNotificationBuilder mNotificationBuilder;
private ScanResult mRecommendedNetwork;
@@ -107,7 +141,8 @@ public class OpenNetworkNotifier {
WifiConfigManager wifiConfigManager,
WifiConfigStore wifiConfigStore,
WifiStateMachine wifiStateMachine,
- OpenNetworkRecommender openNetworkRecommender) {
+ OpenNetworkRecommender openNetworkRecommender,
+ ConnectToNetworkNotificationBuilder connectToNetworkNotificationBuilder) {
mContext = context;
mHandler = new Handler(looper);
mFrameworkFacade = framework;
@@ -115,7 +150,7 @@ public class OpenNetworkNotifier {
mConfigManager = wifiConfigManager;
mWifiStateMachine = wifiStateMachine;
mOpenNetworkRecommender = openNetworkRecommender;
- mOpenNetworkNotificationBuilder = new OpenNetworkNotificationBuilder(context, framework);
+ mNotificationBuilder = connectToNetworkNotificationBuilder;
mScreenOn = false;
mSrcMessenger = new Messenger(new Handler(looper, mConnectionStateCallback));
@@ -133,8 +168,9 @@ 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);
+ filter.addAction(ACTION_PICK_WIFI_NETWORK);
+ filter.addAction(ACTION_PICK_WIFI_NETWORK_AFTER_CONNECT_FAILURE);
mContext.registerReceiver(
mBroadcastReceiver, filter, null /* broadcastPermission */, mHandler);
}
@@ -144,15 +180,18 @@ public class OpenNetworkNotifier {
@Override
public void onReceive(Context context, Intent intent) {
switch (intent.getAction()) {
- case ACTION_USER_TAPPED_CONTENT:
- handleUserClickedContentAction();
- break;
case ACTION_USER_DISMISSED_NOTIFICATION:
handleUserDismissedAction();
break;
case ACTION_CONNECT_TO_NETWORK:
handleConnectToNetworkAction();
break;
+ case ACTION_PICK_WIFI_NETWORK:
+ handleSeeAllNetworksAction();
+ break;
+ case ACTION_PICK_WIFI_NETWORK_AFTER_CONNECT_FAILURE:
+ handlePickWifiNetworkAfterConnectFailure();
+ break;
default:
Log.e(TAG, "Unknown action " + intent.getAction());
}
@@ -178,17 +217,17 @@ public class OpenNetworkNotifier {
/**
* Clears the pending notification. This is called by {@link WifiConnectivityManager} on stop.
*
- * @param resetRepeatDelay resets the time delay for repeated notification if true.
+ * @param resetRepeatTime resets the time delay for repeated notification if true.
*/
- public void clearPendingNotification(boolean resetRepeatDelay) {
- if (resetRepeatDelay) {
+ public void clearPendingNotification(boolean resetRepeatTime) {
+ if (resetRepeatTime) {
mNotificationRepeatTime = 0;
}
- if (mNotificationShown) {
+ if (mState != STATE_NO_NOTIFICATION) {
getNotificationManager().cancel(SystemMessage.NOTE_NETWORK_AVAILABLE);
+ mState = STATE_NO_NOTIFICATION;
mRecommendedNetwork = null;
- mNotificationShown = false;
}
}
@@ -205,26 +244,37 @@ public class OpenNetworkNotifier {
*/
public void handleScanResults(@NonNull List<ScanDetail> availableNetworks) {
if (!isControllerEnabled()) {
- clearPendingNotification(true /* resetRepeatDelay */);
+ clearPendingNotification(true /* resetRepeatTime */);
return;
}
if (availableNetworks.isEmpty()) {
- clearPendingNotification(false /* resetRepeatDelay */);
+ clearPendingNotification(false /* resetRepeatTime */);
return;
}
- // Do not show or update the notification if screen is off. We want to avoid a race that
- // could occur between a user picking a network in settings and a network candidate picked
- // through network selection, which will happen because screen on triggers a new
- // connectivity scan.
- if (mNotificationShown || !mScreenOn) {
+ // Not enough time has passed to show a recommendation notification again
+ if (mState == STATE_NO_NOTIFICATION
+ && mClock.getWallClockMillis() < mNotificationRepeatTime) {
return;
}
- mRecommendedNetwork = mOpenNetworkRecommender.recommendNetwork(
- availableNetworks, new ArraySet<>(mBlacklistedSsids));
+ // Do nothing when the screen is off and no notification is showing.
+ if (mState == STATE_NO_NOTIFICATION && !mScreenOn) {
+ return;
+ }
- postNotification(availableNetworks.size());
+ // Only show a new or update an existing recommendation notification.
+ if (mState == STATE_NO_NOTIFICATION
+ || mState == STATE_SHOWING_RECOMMENDATION_NOTIFICATION) {
+ ScanResult recommendation = mOpenNetworkRecommender.recommendNetwork(
+ availableNetworks, new ArraySet<>(mBlacklistedSsids));
+
+ if (recommendation != null) {
+ postInitialNotification(recommendation);
+ } else {
+ clearPendingNotification(false /* resetRepeatTime */);
+ }
+ }
}
/** Handles screen state changes. */
@@ -232,28 +282,70 @@ public class OpenNetworkNotifier {
mScreenOn = screenOn;
}
- private NotificationManager getNotificationManager() {
- return (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
+ /**
+ * Called by {@link WifiConnectivityManager} when Wi-Fi is connected. If the notification
+ * was in the connecting state, update the notification to show that it has connected to the
+ * recommended network.
+ */
+ public void handleWifiConnected() {
+ if (mState != STATE_CONNECTING_IN_NOTIFICATION) {
+ clearPendingNotification(true /* resetRepeatTime */);
+ return;
+ }
+
+ postNotification(mNotificationBuilder.createNetworkConnectedNotification(
+ mRecommendedNetwork));
+ mState = STATE_CONNECTED_NOTIFICATION;
+ mHandler.postDelayed(
+ () -> {
+ if (mState == STATE_CONNECTED_NOTIFICATION) {
+ clearPendingNotification(true /* resetRepeatTime */);
+ }
+ },
+ TIME_TO_SHOW_CONNECTED_MILLIS);
}
- private void postNotification(int numNetworks) {
- // Not enough time has passed to show the notification again
- if (mClock.getWallClockMillis() < mNotificationRepeatTime) {
+ /**
+ * Handles when a Wi-Fi connection attempt failed.
+ */
+ public void handleConnectionFailure() {
+ if (mState != STATE_CONNECTING_IN_NOTIFICATION) {
return;
}
+ postNotification(mNotificationBuilder.createNetworkFailedNotification());
+ mState = STATE_CONNECT_FAILED_NOTIFICATION;
+ mHandler.postDelayed(
+ () -> {
+ if (mState == STATE_CONNECT_FAILED_NOTIFICATION) {
+ clearPendingNotification(false /* resetRepeatTime */);
+ }
+ },
+ TIME_TO_SHOW_FAILED_MILLIS);
+ }
+
+ private NotificationManager getNotificationManager() {
+ return (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
+ }
- getNotificationManager().notify(
- SystemMessage.NOTE_NETWORK_AVAILABLE,
- mOpenNetworkNotificationBuilder.createOpenNetworkAvailableNotification(
- numNetworks));
- mNotificationShown = true;
+ private void postInitialNotification(ScanResult recommendedNetwork) {
+ postNotification(mNotificationBuilder.createConnectToNetworkNotification(
+ recommendedNetwork));
+ mState = STATE_SHOWING_RECOMMENDATION_NOTIFICATION;
+ mRecommendedNetwork = recommendedNetwork;
mNotificationRepeatTime = mClock.getWallClockMillis() + mNotificationRepeatDelay;
}
+ private void postNotification(Notification notification) {
+ getNotificationManager().notify(SystemMessage.NOTE_NETWORK_AVAILABLE, notification);
+ }
+
private void handleConnectToNetworkAction() {
- if (mRecommendedNetwork == null) {
+ if (mState != STATE_SHOWING_RECOMMENDATION_NOTIFICATION) {
return;
}
+ postNotification(mNotificationBuilder.createNetworkConnectingNotification(
+ mRecommendedNetwork));
+
Log.d(TAG, "User initiated connection to recommended network: " + mRecommendedNetwork.SSID);
WifiConfiguration network = ScanResultUtil.createNetworkFromScanResult(mRecommendedNetwork);
Message msg = Message.obtain();
@@ -262,32 +354,49 @@ public class OpenNetworkNotifier {
msg.obj = network;
msg.replyTo = mSrcMessenger;
mWifiStateMachine.sendMessage(msg);
+
+ mState = STATE_CONNECTING_IN_NOTIFICATION;
+ mHandler.postDelayed(
+ () -> {
+ if (mState == STATE_CONNECTING_IN_NOTIFICATION) {
+ handleConnectionFailure();
+ }
+ },
+ TIME_TO_SHOW_CONNECTING_MILLIS);
}
- /**
- * Handles when a Wi-Fi connection attempt failed.
- */
- public void handleConnectionFailure() {
- // Stub. Should post connection failure notification once implemented.
+ private void handleSeeAllNetworksAction() {
+ startWifiSettings();
}
- /** Opens Wi-Fi picker. */
- private void handleUserClickedContentAction() {
- mNotificationShown = false;
+ private void startWifiSettings() {
+ // Close notification drawer before opening the picker.
+ mContext.sendBroadcast(new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS));
mContext.startActivity(
new Intent(Settings.ACTION_WIFI_SETTINGS)
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
+ clearPendingNotification(false /* resetRepeatTime */);
+ }
+
+ private void handlePickWifiNetworkAfterConnectFailure() {
+ startWifiSettings();
}
private void handleUserDismissedAction() {
- if (mRecommendedNetwork != null) {
+ if (mState == STATE_SHOWING_RECOMMENDATION_NOTIFICATION) {
// blacklist dismissed network
mBlacklistedSsids.add(mRecommendedNetwork.SSID);
mConfigManager.saveToStore(false /* forceWrite */);
Log.d(TAG, "Network is added to the open network notification blacklist: "
+ mRecommendedNetwork.SSID);
}
- mNotificationShown = false;
+ resetStateAndDelayNotification();
+ }
+
+ private void resetStateAndDelayNotification() {
+ mState = STATE_NO_NOTIFICATION;
+ mNotificationRepeatTime = System.currentTimeMillis() + mNotificationRepeatDelay;
+ mRecommendedNetwork = null;
}
/** Dump ONA controller state. */
@@ -296,7 +405,7 @@ public class OpenNetworkNotifier {
pw.println("mSettingEnabled " + mSettingEnabled);
pw.println("currentTime: " + mClock.getWallClockMillis());
pw.println("mNotificationRepeatTime: " + mNotificationRepeatTime);
- pw.println("mNotificationShown: " + mNotificationShown);
+ pw.println("mState: " + mState);
pw.println("mBlacklistedSsids: " + mBlacklistedSsids.toString());
}
@@ -327,7 +436,7 @@ public class OpenNetworkNotifier {
public void onChange(boolean selfChange) {
super.onChange(selfChange);
mSettingEnabled = getValue();
- clearPendingNotification(true /* resetRepeatDelay */);
+ clearPendingNotification(true /* resetRepeatTime */);
}
private boolean getValue() {
diff --git a/service/java/com/android/server/wifi/WifiConnectivityManager.java b/service/java/com/android/server/wifi/WifiConnectivityManager.java
index 1c5ac2821..7e730c838 100644
--- a/service/java/com/android/server/wifi/WifiConnectivityManager.java
+++ b/service/java/com/android/server/wifi/WifiConnectivityManager.java
@@ -1073,7 +1073,7 @@ public class WifiConnectivityManager {
mWifiState = state;
if (mWifiState == WIFI_STATE_CONNECTED) {
- mOpenNetworkNotifier.clearPendingNotification(false /* resetRepeatDelay */);
+ mOpenNetworkNotifier.handleWifiConnected();
}
// Reset BSSID of last connection attempt and kick off
diff --git a/service/java/com/android/server/wifi/WifiInjector.java b/service/java/com/android/server/wifi/WifiInjector.java
index 7778c2ceb..fc3af83e5 100644
--- a/service/java/com/android/server/wifi/WifiInjector.java
+++ b/service/java/com/android/server/wifi/WifiInjector.java
@@ -227,7 +227,8 @@ public class WifiInjector {
mOpenNetworkNotifier = new OpenNetworkNotifier(mContext,
mWifiStateMachineHandlerThread.getLooper(), mFrameworkFacade, mClock,
mWifiConfigManager, mWifiConfigStore, mWifiStateMachine,
- new OpenNetworkRecommender());
+ new OpenNetworkRecommender(),
+ new ConnectToNetworkNotificationBuilder(mContext, mFrameworkFacade));
mLockManager = new WifiLockManager(mContext, BatteryStatsService.getService());
mWifiController = new WifiController(mContext, mWifiStateMachine, mSettingsStore,
mLockManager, mWifiServiceHandlerThread.getLooper(), mFrameworkFacade);
diff --git a/tests/wifitests/src/com/android/server/wifi/OpenNetworkNotifierTest.java b/tests/wifitests/src/com/android/server/wifi/OpenNetworkNotifierTest.java
index 7c1223a79..46ec1597e 100644
--- a/tests/wifitests/src/com/android/server/wifi/OpenNetworkNotifierTest.java
+++ b/tests/wifitests/src/com/android/server/wifi/OpenNetworkNotifierTest.java
@@ -18,22 +18,23 @@ package com.android.server.wifi;
import static com.android.server.wifi.OpenNetworkNotifier.DEFAULT_REPEAT_DELAY_SEC;
+import static org.junit.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.any;
-import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.anyInt;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
-import android.app.Notification;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.net.wifi.ScanResult;
+import android.net.wifi.WifiManager;
import android.os.Message;
+import android.os.RemoteException;
import android.os.UserHandle;
import android.os.UserManager;
import android.os.test.TestLooper;
@@ -42,7 +43,6 @@ import android.util.ArraySet;
import org.junit.Before;
import org.junit.Test;
-import org.mockito.Answers;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
@@ -65,12 +65,13 @@ public class OpenNetworkNotifierTest {
@Mock private Clock mClock;
@Mock private WifiConfigStore mWifiConfigStore;
@Mock private WifiConfigManager mWifiConfigManager;
- @Mock(answer = Answers.RETURNS_DEEP_STUBS) private Notification.Builder mNotificationBuilder;
@Mock private NotificationManager mNotificationManager;
@Mock private WifiStateMachine mWifiStateMachine;
@Mock private OpenNetworkRecommender mOpenNetworkRecommender;
+ @Mock private ConnectToNetworkNotificationBuilder mNotificationBuilder;
@Mock private UserManager mUserManager;
private OpenNetworkNotifier mNotificationController;
+ private TestLooper mLooper;
private BroadcastReceiver mBroadcastReceiver;
private ScanResult mDummyNetwork;
private List<ScanDetail> mOpenNetworks;
@@ -88,8 +89,6 @@ public class OpenNetworkNotifierTest {
when(mFrameworkFacade.getIntegerSetting(mContext,
Settings.Global.WIFI_NETWORKS_AVAILABLE_REPEAT_DELAY, DEFAULT_REPEAT_DELAY_SEC))
.thenReturn(DEFAULT_REPEAT_DELAY_SEC);
- when(mFrameworkFacade.makeNotificationBuilder(any(), anyString()))
- .thenReturn(mNotificationBuilder);
when(mContext.getSystemService(Context.USER_SERVICE))
.thenReturn(mUserManager);
when(mContext.getResources()).thenReturn(mResources);
@@ -102,10 +101,10 @@ public class OpenNetworkNotifierTest {
mOpenNetworks.add(new ScanDetail(mDummyNetwork, null /* networkDetail */));
mBlacklistedSsids = new ArraySet<>();
- TestLooper mock_looper = new TestLooper();
+ mLooper = new TestLooper();
mNotificationController = new OpenNetworkNotifier(
- mContext, mock_looper.getLooper(), mFrameworkFacade, mClock, mWifiConfigManager,
- mWifiConfigStore, mWifiStateMachine, mOpenNetworkRecommender);
+ mContext, mLooper.getLooper(), mFrameworkFacade, mClock, mWifiConfigManager,
+ mWifiConfigStore, mWifiStateMachine, mOpenNetworkRecommender, mNotificationBuilder);
ArgumentCaptor<BroadcastReceiver> broadcastReceiverCaptor =
ArgumentCaptor.forClass(BroadcastReceiver.class);
verify(mContext).registerReceiver(broadcastReceiverCaptor.capture(), any(), any(), any());
@@ -121,6 +120,7 @@ public class OpenNetworkNotifierTest {
mNotificationController.handleScanResults(mOpenNetworks);
verify(mOpenNetworkRecommender).recommendNetwork(mOpenNetworks, mBlacklistedSsids);
+ verify(mNotificationBuilder).createConnectToNetworkNotification(mDummyNetwork);
verify(mNotificationManager).notify(anyInt(), any());
}
@@ -144,39 +144,48 @@ public class OpenNetworkNotifierTest {
mNotificationController.handleScanResults(mOpenNetworks);
verify(mOpenNetworkRecommender).recommendNetwork(mOpenNetworks, mBlacklistedSsids);
+ verify(mNotificationBuilder).createConnectToNetworkNotification(mDummyNetwork);
verify(mNotificationManager).notify(anyInt(), any());
mNotificationController.handleScanResults(new ArrayList<>());
verify(mNotificationManager).cancel(anyInt());
}
+
/**
- * When a notification is showing, screen is off, and scan results with no open networks are
- * handled, the notification is cleared.
+ * When a notification is showing and no recommendation is made for the new scan results, the
+ * notification is cleared.
*/
@Test
- public void handleScanResults_notificationShown_screenOff_emptyList_notificationCleared() {
+ public void handleScanResults_notificationShown_noRecommendation_notificationCleared() {
mNotificationController.handleScanResults(mOpenNetworks);
verify(mOpenNetworkRecommender).recommendNetwork(mOpenNetworks, mBlacklistedSsids);
+ verify(mNotificationBuilder).createConnectToNetworkNotification(mDummyNetwork);
verify(mNotificationManager).notify(anyInt(), any());
- mNotificationController.handleScreenStateChanged(false);
- mNotificationController.handleScanResults(new ArrayList<>());
+ when(mOpenNetworkRecommender.recommendNetwork(any(), any())).thenReturn(null);
+ mNotificationController.handleScanResults(mOpenNetworks);
verify(mNotificationManager).cancel(anyInt());
}
/**
- * If notification is showing, do not post another notification.
+ * When a notification is showing, screen is off, and scan results with no open networks are
+ * handled, the notification is cleared.
*/
@Test
- public void handleScanResults_notificationShowing_doesNotRepostNotification() {
- mNotificationController.handleScanResults(mOpenNetworks);
+ public void handleScanResults_notificationShown_screenOff_emptyList_notificationCleared() {
mNotificationController.handleScanResults(mOpenNetworks);
verify(mOpenNetworkRecommender).recommendNetwork(mOpenNetworks, mBlacklistedSsids);
+ verify(mNotificationBuilder).createConnectToNetworkNotification(mDummyNetwork);
verify(mNotificationManager).notify(anyInt(), any());
+
+ mNotificationController.handleScreenStateChanged(false);
+ mNotificationController.handleScanResults(new ArrayList<>());
+
+ verify(mNotificationManager).cancel(anyInt());
}
/**
@@ -188,6 +197,7 @@ public class OpenNetworkNotifierTest {
mNotificationController.handleScanResults(mOpenNetworks);
verify(mOpenNetworkRecommender).recommendNetwork(mOpenNetworks, mBlacklistedSsids);
+ verify(mNotificationBuilder).createConnectToNetworkNotification(mDummyNetwork);
verify(mNotificationManager).notify(anyInt(), any());
mNotificationController.clearPendingNotification(true);
@@ -211,7 +221,7 @@ public class OpenNetworkNotifierTest {
* new scan results with open networks.
*/
@Test
- public void screenOff_handleScanResults_notificationNotDisplayed() {
+ public void screenOff_notificationNotShowing_handleScanResults_notificationNotDisplayed() {
mNotificationController.handleScreenStateChanged(false);
mNotificationController.handleScanResults(mOpenNetworks);
@@ -220,61 +230,69 @@ public class OpenNetworkNotifierTest {
}
/**
- * When a notification is posted and cleared without reseting delay, the next scan with open
- * networks should not post another notification.
+ * When screen is off and notification is displayed, the notification can be updated with a new
+ * recommendation.
*/
@Test
- public void postNotification_clearNotificationWithoutDelayReset_shouldNotPostNotification() {
+ public void screenOff_notificationShowing_handleScanResults_recommendationCanBeUpdated() {
mNotificationController.handleScanResults(mOpenNetworks);
verify(mOpenNetworkRecommender).recommendNetwork(mOpenNetworks, mBlacklistedSsids);
+ verify(mNotificationBuilder).createConnectToNetworkNotification(mDummyNetwork);
verify(mNotificationManager).notify(anyInt(), any());
- mNotificationController.clearPendingNotification(false);
-
+ mNotificationController.handleScreenStateChanged(false);
mNotificationController.handleScanResults(mOpenNetworks);
- // Recommendation made twice but no new notification posted.
+ // Recommendation made twice
verify(mOpenNetworkRecommender, times(2)).recommendNetwork(
mOpenNetworks, mBlacklistedSsids);
- verify(mNotificationManager).notify(anyInt(), any());
- verify(mNotificationManager).cancel(anyInt());
+ verify(mNotificationBuilder, times(2)).createConnectToNetworkNotification(mDummyNetwork);
+ verify(mNotificationManager, times(2)).notify(anyInt(), any());
}
/**
- * When a notification is posted and cleared without reseting delay, the next scan with open
- * networks should post a notification.
+ * When a notification is posted and cleared without resetting delay, the next scan with open
+ * networks should not post another notification.
*/
@Test
- public void postNotification_clearNotificationWithDelayReset_shouldPostNotification() {
+ public void postNotification_clearNotificationWithoutDelayReset_shouldNotPostNotification() {
mNotificationController.handleScanResults(mOpenNetworks);
verify(mOpenNetworkRecommender).recommendNetwork(mOpenNetworks, mBlacklistedSsids);
+ verify(mNotificationBuilder).createConnectToNetworkNotification(mDummyNetwork);
verify(mNotificationManager).notify(anyInt(), any());
- mNotificationController.clearPendingNotification(true);
+ mNotificationController.clearPendingNotification(false);
+
+ verify(mNotificationManager).cancel(anyInt());
mNotificationController.handleScanResults(mOpenNetworks);
- verify(mOpenNetworkRecommender, times(2)).recommendNetwork(
- mOpenNetworks, mBlacklistedSsids);
- verify(mNotificationManager, times(2)).notify(anyInt(), any());
+ // no new notification posted
+ verify(mNotificationManager).notify(anyInt(), any());
}
/**
- * When a notification is tapped, open Wi-Fi settings.
+ * When a notification is posted and cleared without resetting delay, the next scan with open
+ * networks should post a notification.
*/
@Test
- public void notificationTap_opensWifiSettings() {
+ public void postNotification_clearNotificationWithDelayReset_shouldPostNotification() {
mNotificationController.handleScanResults(mOpenNetworks);
verify(mOpenNetworkRecommender).recommendNetwork(mOpenNetworks, mBlacklistedSsids);
+ verify(mNotificationBuilder).createConnectToNetworkNotification(mDummyNetwork);
verify(mNotificationManager).notify(anyInt(), any());
- mBroadcastReceiver.onReceive(
- mContext, new Intent(OpenNetworkNotifier.ACTION_USER_TAPPED_CONTENT));
+ mNotificationController.clearPendingNotification(true);
+
+ mNotificationController.handleScanResults(mOpenNetworks);
- verify(mContext).startActivity(any());
+ verify(mOpenNetworkRecommender, times(2)).recommendNetwork(
+ mOpenNetworks, mBlacklistedSsids);
+ verify(mNotificationBuilder, times(2)).createConnectToNetworkNotification(mDummyNetwork);
+ verify(mNotificationManager, times(2)).notify(anyInt(), any());
}
/**
@@ -286,14 +304,18 @@ public class OpenNetworkNotifierTest {
mNotificationController.handleScanResults(mOpenNetworks);
verify(mOpenNetworkRecommender).recommendNetwork(mOpenNetworks, mBlacklistedSsids);
+ verify(mNotificationBuilder).createConnectToNetworkNotification(mDummyNetwork);
verify(mNotificationManager).notify(anyInt(), any());
mBroadcastReceiver.onReceive(
- mContext, new Intent(OpenNetworkNotifier.ACTION_USER_DISMISSED_NOTIFICATION));
+ mContext,
+ new Intent(ConnectToNetworkNotificationBuilder.ACTION_USER_DISMISSED_NOTIFICATION));
verify(mWifiConfigManager).saveToStore(false /* forceWrite */);
- mNotificationController.handleScanResults(mOpenNetworks);
+ mNotificationController.clearPendingNotification(true);
+ List<ScanDetail> scanResults = mOpenNetworks;
+ mNotificationController.handleScanResults(scanResults);
Set<String> expectedBlacklist = new ArraySet<>();
expectedBlacklist.add(mDummyNetwork.SSID);
@@ -301,7 +323,7 @@ public class OpenNetworkNotifierTest {
}
/**
- * When a notification is posted and cleared without reseting delay, after the delay has passed
+ * When a notification is posted and cleared without resetting delay, after the delay has passed
* the next scan with open networks should post a notification.
*/
@Test
@@ -309,6 +331,7 @@ public class OpenNetworkNotifierTest {
mNotificationController.handleScanResults(mOpenNetworks);
verify(mOpenNetworkRecommender).recommendNetwork(mOpenNetworks, mBlacklistedSsids);
+ verify(mNotificationBuilder).createConnectToNetworkNotification(mDummyNetwork);
verify(mNotificationManager).notify(anyInt(), any());
mNotificationController.clearPendingNotification(false);
@@ -320,6 +343,7 @@ public class OpenNetworkNotifierTest {
verify(mOpenNetworkRecommender, times(2)).recommendNetwork(
mOpenNetworks, mBlacklistedSsids);
+ verify(mNotificationBuilder, times(2)).createConnectToNetworkNotification(mDummyNetwork);
verify(mNotificationManager, times(2)).notify(anyInt(), any());
}
@@ -341,6 +365,7 @@ public class OpenNetworkNotifierTest {
mNotificationController.handleScanResults(mOpenNetworks);
verify(mOpenNetworkRecommender).recommendNetwork(mOpenNetworks, mBlacklistedSsids);
+ verify(mNotificationBuilder).createConnectToNetworkNotification(mDummyNetwork);
verify(mNotificationManager).notify(anyInt(), any());
when(mUserManager.hasUserRestriction(UserManager.DISALLOW_CONFIG_WIFI, UserHandle.CURRENT))
@@ -352,30 +377,172 @@ public class OpenNetworkNotifierTest {
}
/**
- * {@link OpenNetworkNotifier#ACTION_CONNECT_TO_NETWORK} does not connect to any network if
- * there is no current recommendation.
+ * {@link ConnectToNetworkNotificationBuilder#ACTION_CONNECT_TO_NETWORK} does not connect to
+ * any network if the initial notification is not showing.
*/
@Test
- public void actionConnectToNetwork_currentRecommendationIsNull_doesNothing() {
+ public void actionConnectToNetwork_notificationNotShowing_doesNothing() {
mBroadcastReceiver.onReceive(mContext,
- new Intent(OpenNetworkNotifier.ACTION_CONNECT_TO_NETWORK));
+ new Intent(ConnectToNetworkNotificationBuilder.ACTION_CONNECT_TO_NETWORK));
verify(mWifiStateMachine, never()).sendMessage(any(Message.class));
}
/**
- * {@link OpenNetworkNotifier#ACTION_CONNECT_TO_NETWORK} connects to the currently recommended
- * network if it exists.
+ * {@link ConnectToNetworkNotificationBuilder#ACTION_CONNECT_TO_NETWORK} connects to the
+ * currently recommended network if it exists.
*/
@Test
- public void actionConnectToNetwork_currentRecommendationExists_connectsToNetwork() {
+ public void actionConnectToNetwork_currentRecommendationExists_connectsAndPostsNotification() {
mNotificationController.handleScanResults(mOpenNetworks);
verify(mOpenNetworkRecommender).recommendNetwork(mOpenNetworks, mBlacklistedSsids);
+ // Initial Notification
+ verify(mNotificationBuilder).createConnectToNetworkNotification(mDummyNetwork);
+ verify(mNotificationManager).notify(anyInt(), any());
mBroadcastReceiver.onReceive(mContext,
- new Intent(OpenNetworkNotifier.ACTION_CONNECT_TO_NETWORK));
+ new Intent(ConnectToNetworkNotificationBuilder.ACTION_CONNECT_TO_NETWORK));
verify(mWifiStateMachine).sendMessage(any(Message.class));
+ // Connecting Notification
+ verify(mNotificationBuilder).createNetworkConnectingNotification(mDummyNetwork);
+ verify(mNotificationManager, times(2)).notify(anyInt(), any());
+ }
+
+ /**
+ * {@link OpenNetworkNotifier#handleWifiConnected()} does not post connected notification if
+ * the connecting notification is not showing
+ */
+ @Test
+ public void networkConnectionSuccess_wasNotInConnectingFlow_doesNothing() {
+ mNotificationController.handleWifiConnected();
+
+ verify(mNotificationManager, never()).notify(anyInt(), any());
+ }
+
+ /**
+ * {@link OpenNetworkNotifier#handleWifiConnected()} clears notification that is not connecting.
+ */
+ @Test
+ public void networkConnectionSuccess_wasShowingNotification_clearsNotification() {
+ mNotificationController.handleScanResults(mOpenNetworks);
+
+ verify(mOpenNetworkRecommender).recommendNetwork(mOpenNetworks, mBlacklistedSsids);
+ // Initial Notification
+ verify(mNotificationBuilder).createConnectToNetworkNotification(mDummyNetwork);
+ verify(mNotificationManager).notify(anyInt(), any());
+
+ mNotificationController.handleWifiConnected();
+
+ verify(mNotificationManager).cancel(anyInt());
+ }
+
+ /**
+ * {@link OpenNetworkNotifier#handleWifiConnected()} posts the connected notification if
+ * the connecting notification is showing.
+ */
+ @Test
+ public void networkConnectionSuccess_wasInConnectingFlow_postsConnectedNotification() {
+ mNotificationController.handleScanResults(mOpenNetworks);
+
+ verify(mOpenNetworkRecommender).recommendNetwork(mOpenNetworks, mBlacklistedSsids);
+ // Initial Notification
+ verify(mNotificationBuilder).createConnectToNetworkNotification(mDummyNetwork);
+ verify(mNotificationManager).notify(anyInt(), any());
+
+ mBroadcastReceiver.onReceive(mContext,
+ new Intent(ConnectToNetworkNotificationBuilder.ACTION_CONNECT_TO_NETWORK));
+
+ // Connecting Notification
+ verify(mNotificationBuilder).createNetworkConnectingNotification(mDummyNetwork);
+ verify(mNotificationManager, times(2)).notify(anyInt(), any());
+
+ mNotificationController.handleWifiConnected();
+
+ // Connected Notification
+ verify(mNotificationBuilder).createNetworkConnectedNotification(mDummyNetwork);
+ verify(mNotificationManager, times(3)).notify(anyInt(), any());
+ }
+
+ /**
+ * {@link OpenNetworkNotifier#handleConnectionFailure()} posts the Failed to Connect
+ * notification if the connecting notification is showing.
+ */
+ @Test
+ public void networkConnectionFailure_wasNotInConnectingFlow_doesNothing() {
+ mNotificationController.handleConnectionFailure();
+
+ verify(mNotificationManager, never()).notify(anyInt(), any());
+ }
+
+ /**
+ * {@link OpenNetworkNotifier#handleConnectionFailure()} posts the Failed to Connect
+ * notification if the connecting notification is showing.
+ */
+ @Test
+ public void networkConnectionFailure_wasInConnectingFlow_postsFailedToConnectNotification() {
+ mNotificationController.handleScanResults(mOpenNetworks);
+
+ verify(mOpenNetworkRecommender).recommendNetwork(mOpenNetworks, mBlacklistedSsids);
+ // Initial Notification
+ verify(mNotificationBuilder).createConnectToNetworkNotification(mDummyNetwork);
+ verify(mNotificationManager).notify(anyInt(), any());
+
+ mBroadcastReceiver.onReceive(mContext,
+ new Intent(ConnectToNetworkNotificationBuilder.ACTION_CONNECT_TO_NETWORK));
+
+ // Connecting Notification
+ verify(mNotificationBuilder).createNetworkConnectingNotification(mDummyNetwork);
+ verify(mNotificationManager, times(2)).notify(anyInt(), any());
+
+ mNotificationController.handleConnectionFailure();
+
+ // Failed to Connect Notification
+ verify(mNotificationBuilder).createNetworkFailedNotification();
+ verify(mNotificationManager, times(3)).notify(anyInt(), any());
+ }
+
+ /**
+ * When a {@link WifiManager#CONNECT_NETWORK_FAILED} is received from the connection callback
+ * of {@link WifiStateMachine#sendMessage(Message)}, a Failed to Connect notification should
+ * be posted. On tapping this notification, Wi-Fi Settings should be launched.
+ */
+ @Test
+ public void connectionFailedCallback_postsFailedToConnectNotification() throws RemoteException {
+ mNotificationController.handleScanResults(mOpenNetworks);
+
+ verify(mOpenNetworkRecommender).recommendNetwork(mOpenNetworks, mBlacklistedSsids);
+ // Initial Notification
+ verify(mNotificationBuilder).createConnectToNetworkNotification(mDummyNetwork);
+ verify(mNotificationManager).notify(anyInt(), any());
+
+ mBroadcastReceiver.onReceive(mContext,
+ new Intent(ConnectToNetworkNotificationBuilder.ACTION_CONNECT_TO_NETWORK));
+
+ ArgumentCaptor<Message> connectMessageCaptor = ArgumentCaptor.forClass(Message.class);
+ verify(mWifiStateMachine).sendMessage(connectMessageCaptor.capture());
+ Message connectMessage = connectMessageCaptor.getValue();
+
+ // Connecting Notification
+ verify(mNotificationBuilder).createNetworkConnectingNotification(mDummyNetwork);
+ verify(mNotificationManager, times(2)).notify(anyInt(), any());
+
+ Message connectFailedMsg = Message.obtain();
+ connectFailedMsg.what = WifiManager.CONNECT_NETWORK_FAILED;
+ connectMessage.replyTo.send(connectFailedMsg);
+ mLooper.dispatchAll();
+
+ // Failed to Connect Notification
+ verify(mNotificationBuilder).createNetworkFailedNotification();
+ verify(mNotificationManager, times(3)).notify(anyInt(), any());
+
+ mBroadcastReceiver.onReceive(mContext,
+ new Intent(ConnectToNetworkNotificationBuilder
+ .ACTION_PICK_WIFI_NETWORK_AFTER_CONNECT_FAILURE));
+
+ ArgumentCaptor<Intent> pickerIntentCaptor = ArgumentCaptor.forClass(Intent.class);
+ verify(mContext).startActivity(pickerIntentCaptor.capture());
+ assertEquals(pickerIntentCaptor.getValue().getAction(), Settings.ACTION_WIFI_SETTINGS);
}
}
diff --git a/tests/wifitests/src/com/android/server/wifi/WifiConnectivityManagerTest.java b/tests/wifitests/src/com/android/server/wifi/WifiConnectivityManagerTest.java
index cb1160cc1..6420fac88 100644
--- a/tests/wifitests/src/com/android/server/wifi/WifiConnectivityManagerTest.java
+++ b/tests/wifitests/src/com/android/server/wifi/WifiConnectivityManagerTest.java
@@ -637,18 +637,17 @@ public class WifiConnectivityManagerTest {
}
/**
- * When wifi is connected, {@link OpenNetworkNotifier} tries to clear the pending
- * notification and does not reset notification repeat delay.
+ * When wifi is connected, {@link OpenNetworkNotifier} handles the Wi-Fi connected behavior.
*
- * Expected behavior: ONA clears pending notification and does not reset repeat delay.
+ * Expected behavior: ONA handles connected behavior
*/
@Test
- public void wifiConnected_openNetworkNotifierClearsPendingNotification() {
+ public void wifiConnected_openNetworkNotifierHandlesConnection() {
// Set WiFi to connected state
mWifiConnectivityManager.handleConnectionStateChanged(
WifiConnectivityManager.WIFI_STATE_CONNECTED);
- verify(mOpenNetworkNotifier).clearPendingNotification(false /* resetRepeatDelay*/);
+ verify(mOpenNetworkNotifier).handleWifiConnected();
}
/**