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