Irfan Sheriff | b8c0e00 | 2013-02-20 14:19:54 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.server.wifi; |
| 18 | |
| 19 | import android.app.Notification; |
| 20 | import android.app.NotificationManager; |
| 21 | import android.app.TaskStackBuilder; |
| 22 | import android.content.BroadcastReceiver; |
| 23 | import android.content.ContentResolver; |
| 24 | import android.content.Context; |
| 25 | import android.content.Intent; |
| 26 | import android.content.IntentFilter; |
| 27 | import android.database.ContentObserver; |
| 28 | import android.net.NetworkInfo; |
| 29 | import android.net.wifi.ScanResult; |
| 30 | import android.net.wifi.WifiManager; |
Irfan Sheriff | b8c0e00 | 2013-02-20 14:19:54 -0800 | [diff] [blame] | 31 | import android.os.Handler; |
| 32 | import android.os.Message; |
| 33 | import android.os.UserHandle; |
| 34 | import android.provider.Settings; |
| 35 | |
| 36 | import java.io.FileDescriptor; |
| 37 | import java.io.PrintWriter; |
| 38 | import java.util.List; |
| 39 | |
| 40 | /* Takes care of handling the "open wi-fi network available" notification @hide */ |
| 41 | final class WifiNotificationController { |
| 42 | /** |
| 43 | * The icon to show in the 'available networks' notification. This will also |
| 44 | * be the ID of the Notification given to the NotificationManager. |
| 45 | */ |
| 46 | private static final int ICON_NETWORKS_AVAILABLE = |
| 47 | com.android.internal.R.drawable.stat_notify_wifi_in_range; |
| 48 | /** |
| 49 | * When a notification is shown, we wait this amount before possibly showing it again. |
| 50 | */ |
| 51 | private final long NOTIFICATION_REPEAT_DELAY_MS; |
| 52 | /** |
| 53 | * Whether the user has set the setting to show the 'available networks' notification. |
| 54 | */ |
| 55 | private boolean mNotificationEnabled; |
| 56 | /** |
| 57 | * Observes the user setting to keep {@link #mNotificationEnabled} in sync. |
| 58 | */ |
| 59 | private NotificationEnabledSettingObserver mNotificationEnabledSettingObserver; |
| 60 | /** |
| 61 | * The {@link System#currentTimeMillis()} must be at least this value for us |
| 62 | * to show the notification again. |
| 63 | */ |
| 64 | private long mNotificationRepeatTime; |
| 65 | /** |
| 66 | * The Notification object given to the NotificationManager. |
| 67 | */ |
| 68 | private Notification mNotification; |
| 69 | /** |
| 70 | * Whether the notification is being shown, as set by us. That is, if the |
| 71 | * user cancels the notification, we will not receive the callback so this |
| 72 | * will still be true. We only guarantee if this is false, then the |
| 73 | * notification is not showing. |
| 74 | */ |
| 75 | private boolean mNotificationShown; |
| 76 | /** |
| 77 | * The number of continuous scans that must occur before consider the |
| 78 | * supplicant in a scanning state. This allows supplicant to associate with |
| 79 | * remembered networks that are in the scan results. |
| 80 | */ |
| 81 | private static final int NUM_SCANS_BEFORE_ACTUALLY_SCANNING = 3; |
| 82 | /** |
| 83 | * The number of scans since the last network state change. When this |
| 84 | * exceeds {@link #NUM_SCANS_BEFORE_ACTUALLY_SCANNING}, we consider the |
| 85 | * supplicant to actually be scanning. When the network state changes to |
| 86 | * something other than scanning, we reset this to 0. |
| 87 | */ |
| 88 | private int mNumScansSinceNetworkStateChange; |
| 89 | |
| 90 | private final Context mContext; |
| 91 | private final WifiStateMachine mWifiStateMachine; |
| 92 | private NetworkInfo mNetworkInfo; |
Robert Greenwalt | 8c77692 | 2013-05-21 11:17:11 -0700 | [diff] [blame] | 93 | private volatile int mWifiState; |
Irfan Sheriff | b8c0e00 | 2013-02-20 14:19:54 -0800 | [diff] [blame] | 94 | |
| 95 | WifiNotificationController(Context context, WifiStateMachine wsm) { |
| 96 | mContext = context; |
| 97 | mWifiStateMachine = wsm; |
Robert Greenwalt | 8c77692 | 2013-05-21 11:17:11 -0700 | [diff] [blame] | 98 | mWifiState = WifiManager.WIFI_STATE_UNKNOWN; |
Irfan Sheriff | b8c0e00 | 2013-02-20 14:19:54 -0800 | [diff] [blame] | 99 | |
| 100 | IntentFilter filter = new IntentFilter(); |
| 101 | filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION); |
| 102 | filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION); |
| 103 | filter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION); |
| 104 | |
| 105 | mContext.registerReceiver( |
| 106 | new BroadcastReceiver() { |
| 107 | @Override |
| 108 | public void onReceive(Context context, Intent intent) { |
| 109 | if (intent.getAction().equals(WifiManager.WIFI_STATE_CHANGED_ACTION)) { |
Robert Greenwalt | 8c77692 | 2013-05-21 11:17:11 -0700 | [diff] [blame] | 110 | mWifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, |
| 111 | WifiManager.WIFI_STATE_UNKNOWN); |
Irfan Sheriff | b8c0e00 | 2013-02-20 14:19:54 -0800 | [diff] [blame] | 112 | resetNotification(); |
| 113 | } else if (intent.getAction().equals( |
| 114 | WifiManager.NETWORK_STATE_CHANGED_ACTION)) { |
| 115 | mNetworkInfo = (NetworkInfo) intent.getParcelableExtra( |
| 116 | WifiManager.EXTRA_NETWORK_INFO); |
| 117 | // reset & clear notification on a network connect & disconnect |
| 118 | switch(mNetworkInfo.getDetailedState()) { |
| 119 | case CONNECTED: |
| 120 | case DISCONNECTED: |
| 121 | case CAPTIVE_PORTAL_CHECK: |
| 122 | resetNotification(); |
| 123 | break; |
| 124 | } |
| 125 | } else if (intent.getAction().equals( |
| 126 | WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) { |
| 127 | checkAndSetNotification(mNetworkInfo, |
| 128 | mWifiStateMachine.syncGetScanResultsList()); |
| 129 | } |
| 130 | } |
| 131 | }, filter); |
| 132 | |
| 133 | // Setting is in seconds |
| 134 | NOTIFICATION_REPEAT_DELAY_MS = Settings.Global.getInt(context.getContentResolver(), |
| 135 | Settings.Global.WIFI_NETWORKS_AVAILABLE_REPEAT_DELAY, 900) * 1000l; |
| 136 | mNotificationEnabledSettingObserver = new NotificationEnabledSettingObserver(new Handler()); |
| 137 | mNotificationEnabledSettingObserver.register(); |
| 138 | } |
| 139 | |
| 140 | private synchronized void checkAndSetNotification(NetworkInfo networkInfo, |
| 141 | List<ScanResult> scanResults) { |
| 142 | // TODO: unregister broadcast so we do not have to check here |
| 143 | // If we shouldn't place a notification on available networks, then |
| 144 | // don't bother doing any of the following |
| 145 | if (!mNotificationEnabled) return; |
| 146 | if (networkInfo == null) return; |
Robert Greenwalt | 8c77692 | 2013-05-21 11:17:11 -0700 | [diff] [blame] | 147 | if (mWifiState != WifiManager.WIFI_STATE_ENABLED) return; |
Irfan Sheriff | b8c0e00 | 2013-02-20 14:19:54 -0800 | [diff] [blame] | 148 | |
| 149 | NetworkInfo.State state = networkInfo.getState(); |
| 150 | if ((state == NetworkInfo.State.DISCONNECTED) |
| 151 | || (state == NetworkInfo.State.UNKNOWN)) { |
| 152 | if (scanResults != null) { |
| 153 | int numOpenNetworks = 0; |
| 154 | for (int i = scanResults.size() - 1; i >= 0; i--) { |
| 155 | ScanResult scanResult = scanResults.get(i); |
| 156 | |
| 157 | //A capability of [ESS] represents an open access point |
| 158 | //that is available for an STA to connect |
| 159 | if (scanResult.capabilities != null && |
| 160 | scanResult.capabilities.equals("[ESS]")) { |
| 161 | numOpenNetworks++; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | if (numOpenNetworks > 0) { |
| 166 | if (++mNumScansSinceNetworkStateChange >= NUM_SCANS_BEFORE_ACTUALLY_SCANNING) { |
| 167 | /* |
| 168 | * We've scanned continuously at least |
| 169 | * NUM_SCANS_BEFORE_NOTIFICATION times. The user |
| 170 | * probably does not have a remembered network in range, |
| 171 | * since otherwise supplicant would have tried to |
| 172 | * associate and thus resetting this counter. |
| 173 | */ |
| 174 | setNotificationVisible(true, numOpenNetworks, false, 0); |
| 175 | } |
| 176 | return; |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | // No open networks in range, remove the notification |
| 182 | setNotificationVisible(false, 0, false, 0); |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Clears variables related to tracking whether a notification has been |
| 187 | * shown recently and clears the current notification. |
| 188 | */ |
| 189 | private synchronized void resetNotification() { |
| 190 | mNotificationRepeatTime = 0; |
| 191 | mNumScansSinceNetworkStateChange = 0; |
| 192 | setNotificationVisible(false, 0, false, 0); |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * Display or don't display a notification that there are open Wi-Fi networks. |
| 197 | * @param visible {@code true} if notification should be visible, {@code false} otherwise |
| 198 | * @param numNetworks the number networks seen |
| 199 | * @param force {@code true} to force notification to be shown/not-shown, |
| 200 | * even if it is already shown/not-shown. |
| 201 | * @param delay time in milliseconds after which the notification should be made |
| 202 | * visible or invisible. |
| 203 | */ |
| 204 | private void setNotificationVisible(boolean visible, int numNetworks, boolean force, |
| 205 | int delay) { |
| 206 | |
| 207 | // Since we use auto cancel on the notification, when the |
| 208 | // mNetworksAvailableNotificationShown is true, the notification may |
| 209 | // have actually been canceled. However, when it is false we know |
| 210 | // for sure that it is not being shown (it will not be shown any other |
| 211 | // place than here) |
| 212 | |
| 213 | // If it should be hidden and it is already hidden, then noop |
| 214 | if (!visible && !mNotificationShown && !force) { |
| 215 | return; |
| 216 | } |
| 217 | |
| 218 | NotificationManager notificationManager = (NotificationManager) mContext |
| 219 | .getSystemService(Context.NOTIFICATION_SERVICE); |
| 220 | |
| 221 | Message message; |
| 222 | if (visible) { |
| 223 | |
| 224 | // Not enough time has passed to show the notification again |
| 225 | if (System.currentTimeMillis() < mNotificationRepeatTime) { |
| 226 | return; |
| 227 | } |
| 228 | |
| 229 | if (mNotification == null) { |
| 230 | // Cache the Notification object. |
| 231 | mNotification = new Notification(); |
| 232 | mNotification.when = 0; |
| 233 | mNotification.icon = ICON_NETWORKS_AVAILABLE; |
| 234 | mNotification.flags = Notification.FLAG_AUTO_CANCEL; |
| 235 | mNotification.contentIntent = TaskStackBuilder.create(mContext) |
| 236 | .addNextIntentWithParentStack( |
| 237 | new Intent(WifiManager.ACTION_PICK_WIFI_NETWORK)) |
| 238 | .getPendingIntent(0, 0, null, UserHandle.CURRENT); |
| 239 | } |
| 240 | |
| 241 | CharSequence title = mContext.getResources().getQuantityText( |
| 242 | com.android.internal.R.plurals.wifi_available, numNetworks); |
| 243 | CharSequence details = mContext.getResources().getQuantityText( |
| 244 | com.android.internal.R.plurals.wifi_available_detailed, numNetworks); |
| 245 | mNotification.tickerText = title; |
| 246 | mNotification.setLatestEventInfo(mContext, title, details, mNotification.contentIntent); |
| 247 | |
| 248 | mNotificationRepeatTime = System.currentTimeMillis() + NOTIFICATION_REPEAT_DELAY_MS; |
| 249 | |
| 250 | notificationManager.notifyAsUser(null, ICON_NETWORKS_AVAILABLE, mNotification, |
| 251 | UserHandle.ALL); |
| 252 | } else { |
| 253 | notificationManager.cancelAsUser(null, ICON_NETWORKS_AVAILABLE, UserHandle.ALL); |
| 254 | } |
| 255 | |
| 256 | mNotificationShown = visible; |
| 257 | } |
| 258 | |
| 259 | void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 260 | pw.println("mNotificationEnabled " + mNotificationEnabled); |
| 261 | pw.println("mNotificationRepeatTime " + mNotificationRepeatTime); |
| 262 | pw.println("mNotificationShown " + mNotificationShown); |
| 263 | pw.println("mNumScansSinceNetworkStateChange " + mNumScansSinceNetworkStateChange); |
| 264 | } |
| 265 | |
| 266 | private class NotificationEnabledSettingObserver extends ContentObserver { |
| 267 | public NotificationEnabledSettingObserver(Handler handler) { |
| 268 | super(handler); |
| 269 | } |
| 270 | |
| 271 | public void register() { |
| 272 | ContentResolver cr = mContext.getContentResolver(); |
| 273 | cr.registerContentObserver(Settings.Global.getUriFor( |
| 274 | Settings.Global.WIFI_NETWORKS_AVAILABLE_NOTIFICATION_ON), true, this); |
| 275 | synchronized (WifiNotificationController.this) { |
| 276 | mNotificationEnabled = getValue(); |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | @Override |
| 281 | public void onChange(boolean selfChange) { |
| 282 | super.onChange(selfChange); |
| 283 | |
| 284 | synchronized (WifiNotificationController.this) { |
| 285 | mNotificationEnabled = getValue(); |
| 286 | resetNotification(); |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | private boolean getValue() { |
| 291 | return Settings.Global.getInt(mContext.getContentResolver(), |
| 292 | Settings.Global.WIFI_NETWORKS_AVAILABLE_NOTIFICATION_ON, 1) == 1; |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | } |