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.Context; 24 import android.content.Intent; 25 import android.content.IntentFilter; 26 import android.database.ContentObserver; 27 import android.net.NetworkInfo; 28 import android.net.wifi.ScanResult; 29 import android.net.wifi.WifiManager; 30 import android.net.wifi.WifiScanner; 31 import android.os.Handler; 32 import android.os.Looper; 33 import android.os.Message; 34 import android.os.UserHandle; 35 import android.os.UserManager; 36 import android.provider.Settings; 37 38 import com.android.internal.notification.SystemNotificationChannels; 39 40 import java.io.FileDescriptor; 41 import java.io.PrintWriter; 42 import java.util.List; 43 44 /** 45 * Takes care of handling the "open wi-fi network available" notification 46 * @hide 47 */ 48 public class WifiNotificationController { 49 /** 50 * The icon to show in the 'available networks' notification. This will also 51 * be the ID of the Notification given to the NotificationManager. 52 */ 53 private static final int ICON_NETWORKS_AVAILABLE = 54 com.android.internal.R.drawable.stat_notify_wifi_in_range; 55 /** 56 * When a notification is shown, we wait this amount before possibly showing it again. 57 */ 58 private final long NOTIFICATION_REPEAT_DELAY_MS; 59 60 /** 61 * Whether the user has set the setting to show the 'available networks' notification. 62 */ 63 private boolean mNotificationEnabled; 64 /** 65 * Observes the user setting to keep {@link #mNotificationEnabled} in sync. 66 */ 67 private NotificationEnabledSettingObserver mNotificationEnabledSettingObserver; 68 69 /** 70 * The {@link System#currentTimeMillis()} must be at least this value for us 71 * to show the notification again. 72 */ 73 private long mNotificationRepeatTime; 74 /** 75 * The Notification object given to the NotificationManager. 76 */ 77 private Notification.Builder mNotificationBuilder; 78 /** 79 * Whether the notification is being shown, as set by us. That is, if the 80 * user cancels the notification, we will not receive the callback so this 81 * will still be true. We only guarantee if this is false, then the 82 * notification is not showing. 83 */ 84 private boolean mNotificationShown; 85 /** 86 * The number of continuous scans that must occur before consider the 87 * supplicant in a scanning state. This allows supplicant to associate with 88 * remembered networks that are in the scan results. 89 */ 90 private static final int NUM_SCANS_BEFORE_ACTUALLY_SCANNING = 3; 91 /** 92 * The number of scans since the last network state change. When this 93 * exceeds {@link #NUM_SCANS_BEFORE_ACTUALLY_SCANNING}, we consider the 94 * supplicant to actually be scanning. When the network state changes to 95 * something other than scanning, we reset this to 0. 96 */ 97 private int mNumScansSinceNetworkStateChange; 98 99 private final Context mContext; 100 private NetworkInfo mNetworkInfo; 101 private NetworkInfo.DetailedState mDetailedState; 102 private volatile int mWifiState; 103 private FrameworkFacade mFrameworkFacade; 104 private WifiInjector mWifiInjector; 105 private WifiScanner mWifiScanner; 106 WifiNotificationController(Context context, Looper looper, FrameworkFacade framework, Notification.Builder builder, WifiInjector wifiInjector)107 WifiNotificationController(Context context, 108 Looper looper, 109 FrameworkFacade framework, 110 Notification.Builder builder, 111 WifiInjector wifiInjector) { 112 mContext = context; 113 mFrameworkFacade = framework; 114 mNotificationBuilder = builder; 115 mWifiInjector = wifiInjector; 116 mWifiState = WifiManager.WIFI_STATE_UNKNOWN; 117 mDetailedState = NetworkInfo.DetailedState.IDLE; 118 119 IntentFilter filter = new IntentFilter(); 120 filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION); 121 filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION); 122 filter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION); 123 124 mContext.registerReceiver( 125 new BroadcastReceiver() { 126 @Override 127 public void onReceive(Context context, Intent intent) { 128 if (intent.getAction() 129 .equals(WifiManager.WIFI_STATE_CHANGED_ACTION)) { 130 mWifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, 131 WifiManager.WIFI_STATE_UNKNOWN); 132 resetNotification(); 133 } else if (intent.getAction().equals( 134 WifiManager.NETWORK_STATE_CHANGED_ACTION)) { 135 mNetworkInfo = (NetworkInfo) intent.getParcelableExtra( 136 WifiManager.EXTRA_NETWORK_INFO); 137 NetworkInfo.DetailedState detailedState = 138 mNetworkInfo.getDetailedState(); 139 if (detailedState != NetworkInfo.DetailedState.SCANNING 140 && detailedState != mDetailedState) { 141 mDetailedState = detailedState; 142 // reset & clear notification on a network connect & disconnect 143 switch(mDetailedState) { 144 case CONNECTED: 145 case DISCONNECTED: 146 case CAPTIVE_PORTAL_CHECK: 147 resetNotification(); 148 break; 149 150 case IDLE: 151 case SCANNING: 152 case CONNECTING: 153 case AUTHENTICATING: 154 case OBTAINING_IPADDR: 155 case SUSPENDED: 156 case FAILED: 157 case BLOCKED: 158 case VERIFYING_POOR_LINK: 159 break; 160 } 161 } 162 } else if (intent.getAction().equals( 163 WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) { 164 if (mWifiScanner == null) { 165 mWifiScanner = mWifiInjector.getWifiScanner(); 166 } 167 checkAndSetNotification(mNetworkInfo, 168 mWifiScanner.getSingleScanResults()); 169 } 170 } 171 }, filter); 172 173 // Setting is in seconds 174 NOTIFICATION_REPEAT_DELAY_MS = mFrameworkFacade.getIntegerSetting(context, 175 Settings.Global.WIFI_NETWORKS_AVAILABLE_REPEAT_DELAY, 900) * 1000L; 176 mNotificationEnabledSettingObserver = new NotificationEnabledSettingObserver( 177 new Handler(looper)); 178 mNotificationEnabledSettingObserver.register(); 179 } 180 checkAndSetNotification(NetworkInfo networkInfo, List<ScanResult> scanResults)181 private synchronized void checkAndSetNotification(NetworkInfo networkInfo, 182 List<ScanResult> scanResults) { 183 184 // TODO: unregister broadcast so we do not have to check here 185 // If we shouldn't place a notification on available networks, then 186 // don't bother doing any of the following 187 if (!mNotificationEnabled) return; 188 if (mWifiState != WifiManager.WIFI_STATE_ENABLED) return; 189 if (UserManager.get(mContext) 190 .hasUserRestriction(UserManager.DISALLOW_CONFIG_WIFI, UserHandle.CURRENT)) { 191 return; 192 } 193 194 NetworkInfo.State state = NetworkInfo.State.DISCONNECTED; 195 if (networkInfo != null) 196 state = networkInfo.getState(); 197 198 if ((state == NetworkInfo.State.DISCONNECTED) 199 || (state == NetworkInfo.State.UNKNOWN)) { 200 if (scanResults != null) { 201 int numOpenNetworks = 0; 202 for (int i = scanResults.size() - 1; i >= 0; i--) { 203 ScanResult scanResult = scanResults.get(i); 204 205 //A capability of [ESS] represents an open access point 206 //that is available for an STA to connect 207 if (scanResult.capabilities != null && 208 scanResult.capabilities.equals("[ESS]")) { 209 numOpenNetworks++; 210 } 211 } 212 213 if (numOpenNetworks > 0) { 214 if (++mNumScansSinceNetworkStateChange >= NUM_SCANS_BEFORE_ACTUALLY_SCANNING) { 215 /* 216 * We've scanned continuously at least 217 * NUM_SCANS_BEFORE_NOTIFICATION times. The user 218 * probably does not have a remembered network in range, 219 * since otherwise supplicant would have tried to 220 * associate and thus resetting this counter. 221 */ 222 setNotificationVisible(true, numOpenNetworks, false, 0); 223 } 224 return; 225 } 226 } 227 } 228 229 // No open networks in range, remove the notification 230 setNotificationVisible(false, 0, false, 0); 231 } 232 233 /** 234 * Clears variables related to tracking whether a notification has been 235 * shown recently and clears the current notification. 236 */ resetNotification()237 private synchronized void resetNotification() { 238 mNotificationRepeatTime = 0; 239 mNumScansSinceNetworkStateChange = 0; 240 setNotificationVisible(false, 0, false, 0); 241 } 242 243 /** 244 * Display or don't display a notification that there are open Wi-Fi networks. 245 * @param visible {@code true} if notification should be visible, {@code false} otherwise 246 * @param numNetworks the number networks seen 247 * @param force {@code true} to force notification to be shown/not-shown, 248 * even if it is already shown/not-shown. 249 * @param delay time in milliseconds after which the notification should be made 250 * visible or invisible. 251 */ setNotificationVisible(boolean visible, int numNetworks, boolean force, int delay)252 private void setNotificationVisible(boolean visible, int numNetworks, boolean force, 253 int delay) { 254 255 // Since we use auto cancel on the notification, when the 256 // mNetworksAvailableNotificationShown is true, the notification may 257 // have actually been canceled. However, when it is false we know 258 // for sure that it is not being shown (it will not be shown any other 259 // place than here) 260 261 // If it should be hidden and it is already hidden, then noop 262 if (!visible && !mNotificationShown && !force) { 263 return; 264 } 265 266 NotificationManager notificationManager = (NotificationManager) mContext 267 .getSystemService(Context.NOTIFICATION_SERVICE); 268 269 Message message; 270 if (visible) { 271 272 // Not enough time has passed to show the notification again 273 if (System.currentTimeMillis() < mNotificationRepeatTime) { 274 return; 275 } 276 277 if (mNotificationBuilder == null) { 278 // Cache the Notification builder object. 279 mNotificationBuilder = new Notification.Builder(mContext, 280 SystemNotificationChannels.NETWORK_AVAILABLE) 281 .setWhen(0) 282 .setSmallIcon(ICON_NETWORKS_AVAILABLE) 283 .setAutoCancel(true) 284 .setContentIntent(TaskStackBuilder.create(mContext) 285 .addNextIntentWithParentStack( 286 new Intent(WifiManager.ACTION_PICK_WIFI_NETWORK)) 287 .getPendingIntent(0, 0, null, UserHandle.CURRENT)) 288 .setColor(mContext.getResources().getColor( 289 com.android.internal.R.color.system_notification_accent_color)); 290 } 291 292 CharSequence title = mContext.getResources().getQuantityText( 293 com.android.internal.R.plurals.wifi_available, numNetworks); 294 CharSequence details = mContext.getResources().getQuantityText( 295 com.android.internal.R.plurals.wifi_available_detailed, numNetworks); 296 mNotificationBuilder.setTicker(title); 297 mNotificationBuilder.setContentTitle(title); 298 mNotificationBuilder.setContentText(details); 299 300 mNotificationRepeatTime = System.currentTimeMillis() + NOTIFICATION_REPEAT_DELAY_MS; 301 302 notificationManager.notifyAsUser(null, ICON_NETWORKS_AVAILABLE, 303 mNotificationBuilder.build(), UserHandle.ALL); 304 } else { 305 notificationManager.cancelAsUser(null, ICON_NETWORKS_AVAILABLE, UserHandle.ALL); 306 } 307 308 mNotificationShown = visible; 309 } 310 dump(FileDescriptor fd, PrintWriter pw, String[] args)311 void dump(FileDescriptor fd, PrintWriter pw, String[] args) { 312 pw.println("mNotificationEnabled " + mNotificationEnabled); 313 pw.println("mNotificationRepeatTime " + mNotificationRepeatTime); 314 pw.println("mNotificationShown " + mNotificationShown); 315 pw.println("mNumScansSinceNetworkStateChange " + mNumScansSinceNetworkStateChange); 316 } 317 318 private class NotificationEnabledSettingObserver extends ContentObserver { NotificationEnabledSettingObserver(Handler handler)319 public NotificationEnabledSettingObserver(Handler handler) { 320 super(handler); 321 } 322 register()323 public void register() { 324 mFrameworkFacade.registerContentObserver(mContext, Settings.Global.getUriFor( 325 Settings.Global.WIFI_NETWORKS_AVAILABLE_NOTIFICATION_ON), true, this); 326 synchronized (WifiNotificationController.this) { 327 mNotificationEnabled = getValue(); 328 } 329 } 330 331 @Override onChange(boolean selfChange)332 public void onChange(boolean selfChange) { 333 super.onChange(selfChange); 334 335 synchronized (WifiNotificationController.this) { 336 mNotificationEnabled = getValue(); 337 resetNotification(); 338 } 339 } 340 getValue()341 private boolean getValue() { 342 return mFrameworkFacade.getIntegerSetting(mContext, 343 Settings.Global.WIFI_NETWORKS_AVAILABLE_NOTIFICATION_ON, 1) == 1; 344 } 345 } 346 } 347