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.os.Handler;
32import android.os.Message;
33import android.os.UserHandle;
34import android.provider.Settings;
35
36import java.io.FileDescriptor;
37import java.io.PrintWriter;
38import java.util.List;
39
40/* Takes care of handling the "open wi-fi network available" notification @hide */
41final 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;
93    private volatile int mWifiState;
94
95    WifiNotificationController(Context context, WifiStateMachine wsm) {
96        mContext = context;
97        mWifiStateMachine = wsm;
98        mWifiState = WifiManager.WIFI_STATE_UNKNOWN;
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)) {
110                            mWifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE,
111                                    WifiManager.WIFI_STATE_UNKNOWN);
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
143        // TODO: unregister broadcast so we do not have to check here
144        // If we shouldn't place a notification on available networks, then
145        // don't bother doing any of the following
146        if (!mNotificationEnabled) return;
147        if (mWifiState != WifiManager.WIFI_STATE_ENABLED) return;
148
149        NetworkInfo.State state = NetworkInfo.State.DISCONNECTED;
150        if (networkInfo != null)
151            state = networkInfo.getState();
152
153        if ((state == NetworkInfo.State.DISCONNECTED)
154                || (state == NetworkInfo.State.UNKNOWN)) {
155            if (scanResults != null) {
156                int numOpenNetworks = 0;
157                for (int i = scanResults.size() - 1; i >= 0; i--) {
158                    ScanResult scanResult = scanResults.get(i);
159
160                    //A capability of [ESS] represents an open access point
161                    //that is available for an STA to connect
162                    if (scanResult.capabilities != null &&
163                            scanResult.capabilities.equals("[ESS]")) {
164                        numOpenNetworks++;
165                    }
166                }
167
168                if (numOpenNetworks > 0) {
169                    if (++mNumScansSinceNetworkStateChange >= NUM_SCANS_BEFORE_ACTUALLY_SCANNING) {
170                        /*
171                         * We've scanned continuously at least
172                         * NUM_SCANS_BEFORE_NOTIFICATION times. The user
173                         * probably does not have a remembered network in range,
174                         * since otherwise supplicant would have tried to
175                         * associate and thus resetting this counter.
176                         */
177                        setNotificationVisible(true, numOpenNetworks, false, 0);
178                    }
179                    return;
180                }
181            }
182        }
183
184        // No open networks in range, remove the notification
185        setNotificationVisible(false, 0, false, 0);
186    }
187
188    /**
189     * Clears variables related to tracking whether a notification has been
190     * shown recently and clears the current notification.
191     */
192    private synchronized void resetNotification() {
193        mNotificationRepeatTime = 0;
194        mNumScansSinceNetworkStateChange = 0;
195        setNotificationVisible(false, 0, false, 0);
196    }
197
198    /**
199     * Display or don't display a notification that there are open Wi-Fi networks.
200     * @param visible {@code true} if notification should be visible, {@code false} otherwise
201     * @param numNetworks the number networks seen
202     * @param force {@code true} to force notification to be shown/not-shown,
203     * even if it is already shown/not-shown.
204     * @param delay time in milliseconds after which the notification should be made
205     * visible or invisible.
206     */
207    private void setNotificationVisible(boolean visible, int numNetworks, boolean force,
208            int delay) {
209
210        // Since we use auto cancel on the notification, when the
211        // mNetworksAvailableNotificationShown is true, the notification may
212        // have actually been canceled.  However, when it is false we know
213        // for sure that it is not being shown (it will not be shown any other
214        // place than here)
215
216        // If it should be hidden and it is already hidden, then noop
217        if (!visible && !mNotificationShown && !force) {
218            return;
219        }
220
221        NotificationManager notificationManager = (NotificationManager) mContext
222                .getSystemService(Context.NOTIFICATION_SERVICE);
223
224        Message message;
225        if (visible) {
226
227            // Not enough time has passed to show the notification again
228            if (System.currentTimeMillis() < mNotificationRepeatTime) {
229                return;
230            }
231
232            if (mNotification == null) {
233                // Cache the Notification object.
234                mNotification = new Notification();
235                mNotification.when = 0;
236                mNotification.icon = ICON_NETWORKS_AVAILABLE;
237                mNotification.flags = Notification.FLAG_AUTO_CANCEL;
238                mNotification.contentIntent = TaskStackBuilder.create(mContext)
239                        .addNextIntentWithParentStack(
240                                new Intent(WifiManager.ACTION_PICK_WIFI_NETWORK))
241                        .getPendingIntent(0, 0, null, UserHandle.CURRENT);
242            }
243
244            CharSequence title = mContext.getResources().getQuantityText(
245                    com.android.internal.R.plurals.wifi_available, numNetworks);
246            CharSequence details = mContext.getResources().getQuantityText(
247                    com.android.internal.R.plurals.wifi_available_detailed, numNetworks);
248            mNotification.tickerText = title;
249            mNotification.color = mContext.getResources().getColor(
250                    com.android.internal.R.color.system_notification_accent_color);
251            mNotification.setLatestEventInfo(mContext, title, details, mNotification.contentIntent);
252
253            mNotificationRepeatTime = System.currentTimeMillis() + NOTIFICATION_REPEAT_DELAY_MS;
254
255            notificationManager.notifyAsUser(null, ICON_NETWORKS_AVAILABLE, mNotification,
256                    UserHandle.ALL);
257        } else {
258            notificationManager.cancelAsUser(null, ICON_NETWORKS_AVAILABLE, UserHandle.ALL);
259        }
260
261        mNotificationShown = visible;
262    }
263
264    void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
265        pw.println("mNotificationEnabled " + mNotificationEnabled);
266        pw.println("mNotificationRepeatTime " + mNotificationRepeatTime);
267        pw.println("mNotificationShown " + mNotificationShown);
268        pw.println("mNumScansSinceNetworkStateChange " + mNumScansSinceNetworkStateChange);
269    }
270
271    private class NotificationEnabledSettingObserver extends ContentObserver {
272        public NotificationEnabledSettingObserver(Handler handler) {
273            super(handler);
274        }
275
276        public void register() {
277            ContentResolver cr = mContext.getContentResolver();
278            cr.registerContentObserver(Settings.Global.getUriFor(
279                    Settings.Global.WIFI_NETWORKS_AVAILABLE_NOTIFICATION_ON), true, this);
280            synchronized (WifiNotificationController.this) {
281                mNotificationEnabled = getValue();
282            }
283        }
284
285        @Override
286        public void onChange(boolean selfChange) {
287            super.onChange(selfChange);
288
289            synchronized (WifiNotificationController.this) {
290                mNotificationEnabled = getValue();
291                resetNotification();
292            }
293        }
294
295        private boolean getValue() {
296            return Settings.Global.getInt(mContext.getContentResolver(),
297                    Settings.Global.WIFI_NETWORKS_AVAILABLE_NOTIFICATION_ON, 1) == 1;
298        }
299    }
300
301}
302