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