blob: 635db19521d672ceaf5726576c17e96bf09c0ae3 [file] [log] [blame]
Lorenzo Colitti5526f9c2016-08-22 16:46:40 +09001/*
2 * Copyright (C) 2016 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.connectivity;
18
19import android.app.PendingIntent;
Lorenzo Colitti5526f9c2016-08-22 16:46:40 +090020import android.content.ComponentName;
21import android.content.Context;
22import android.content.Intent;
Lorenzo Colitti84e6f1232016-08-29 14:03:11 +090023import android.net.NetworkCapabilities;
24import android.os.SystemClock;
Lorenzo Colitti5526f9c2016-08-22 16:46:40 +090025import android.os.UserHandle;
Lorenzo Colitti5526f9c2016-08-22 16:46:40 +090026import android.text.TextUtils;
Lorenzo Colitti84e6f1232016-08-29 14:03:11 +090027import android.text.format.DateUtils;
Lorenzo Colitti5526f9c2016-08-22 16:46:40 +090028import android.util.Log;
29import android.util.SparseArray;
30import android.util.SparseIntArray;
31import android.util.SparseBooleanArray;
32import java.util.Arrays;
33import java.util.HashMap;
34
Lorenzo Colitti84e6f1232016-08-29 14:03:11 +090035import com.android.internal.R;
Hugo Benichia43a0952016-08-30 10:01:15 +090036import com.android.internal.annotations.VisibleForTesting;
Lorenzo Colitti5526f9c2016-08-22 16:46:40 +090037import com.android.internal.util.MessageUtils;
38import com.android.server.connectivity.NetworkNotificationManager;
39import com.android.server.connectivity.NetworkNotificationManager.NotificationType;
40
41import static android.net.ConnectivityManager.NETID_UNSET;
42
43/**
44 * Class that monitors default network linger events and possibly notifies the user of network
45 * switches.
46 *
47 * This class is not thread-safe and all its methods must be called on the ConnectivityService
48 * handler thread.
49 */
50public class LingerMonitor {
51
52 private static final boolean DBG = true;
53 private static final boolean VDBG = false;
54 private static final String TAG = LingerMonitor.class.getSimpleName();
55
Lorenzo Colitti84e6f1232016-08-29 14:03:11 +090056 public static final int DEFAULT_NOTIFICATION_DAILY_LIMIT = 3;
57 public static final long DEFAULT_NOTIFICATION_RATE_LIMIT_MILLIS = DateUtils.MINUTE_IN_MILLIS;
58
Hugo Benichia43a0952016-08-30 10:01:15 +090059 private static final HashMap<String, Integer> TRANSPORT_NAMES = makeTransportToNameMap();
60 @VisibleForTesting
61 public static final Intent CELLULAR_SETTINGS = new Intent().setComponent(new ComponentName(
Lorenzo Colitti5526f9c2016-08-22 16:46:40 +090062 "com.android.settings", "com.android.settings.Settings$DataUsageSummaryActivity"));
63
Hugo Benichia43a0952016-08-30 10:01:15 +090064 @VisibleForTesting
65 public static final int NOTIFY_TYPE_NONE = 0;
66 public static final int NOTIFY_TYPE_NOTIFICATION = 1;
67 public static final int NOTIFY_TYPE_TOAST = 2;
Lorenzo Colitti5526f9c2016-08-22 16:46:40 +090068
69 private static SparseArray<String> sNotifyTypeNames = MessageUtils.findMessageNames(
70 new Class[] { LingerMonitor.class }, new String[]{ "NOTIFY_TYPE_" });
71
72 private final Context mContext;
73 private final NetworkNotificationManager mNotifier;
Lorenzo Colitti84e6f1232016-08-29 14:03:11 +090074 private final int mDailyLimit;
75 private final long mRateLimitMillis;
76
77 private long mFirstNotificationMillis;
78 private long mLastNotificationMillis;
79 private int mNotificationCounter;
Lorenzo Colitti5526f9c2016-08-22 16:46:40 +090080
81 /** Current notifications. Maps the netId we switched away from to the netId we switched to. */
82 private final SparseIntArray mNotifications = new SparseIntArray();
83
84 /** Whether we ever notified that we switched away from a particular network. */
85 private final SparseBooleanArray mEverNotified = new SparseBooleanArray();
86
Lorenzo Colitti84e6f1232016-08-29 14:03:11 +090087 public LingerMonitor(Context context, NetworkNotificationManager notifier,
88 int dailyLimit, long rateLimitMillis) {
Lorenzo Colitti5526f9c2016-08-22 16:46:40 +090089 mContext = context;
90 mNotifier = notifier;
Lorenzo Colitti84e6f1232016-08-29 14:03:11 +090091 mDailyLimit = dailyLimit;
92 mRateLimitMillis = rateLimitMillis;
Lorenzo Colitti5526f9c2016-08-22 16:46:40 +090093 }
94
95 private static HashMap<String, Integer> makeTransportToNameMap() {
96 SparseArray<String> numberToName = MessageUtils.findMessageNames(
97 new Class[] { NetworkCapabilities.class }, new String[]{ "TRANSPORT_" });
98 HashMap<String, Integer> nameToNumber = new HashMap<>();
99 for (int i = 0; i < numberToName.size(); i++) {
100 // MessageUtils will fail to initialize if there are duplicate constant values, so there
101 // are no duplicates here.
102 nameToNumber.put(numberToName.valueAt(i), numberToName.keyAt(i));
103 }
104 return nameToNumber;
105 }
106
107 private static boolean hasTransport(NetworkAgentInfo nai, int transport) {
108 return nai.networkCapabilities.hasTransport(transport);
109 }
110
111 private int getNotificationSource(NetworkAgentInfo toNai) {
112 for (int i = 0; i < mNotifications.size(); i++) {
113 if (mNotifications.valueAt(i) == toNai.network.netId) {
114 return mNotifications.keyAt(i);
115 }
116 }
117 return NETID_UNSET;
118 }
119
120 private boolean everNotified(NetworkAgentInfo nai) {
121 return mEverNotified.get(nai.network.netId, false);
122 }
123
Hugo Benichia43a0952016-08-30 10:01:15 +0900124 @VisibleForTesting
125 public boolean isNotificationEnabled(NetworkAgentInfo fromNai, NetworkAgentInfo toNai) {
Lorenzo Colitti5526f9c2016-08-22 16:46:40 +0900126 // TODO: Evaluate moving to CarrierConfigManager.
Lorenzo Colitti84e6f1232016-08-29 14:03:11 +0900127 String[] notifySwitches =
128 mContext.getResources().getStringArray(R.array.config_networkNotifySwitches);
Lorenzo Colitti5526f9c2016-08-22 16:46:40 +0900129
130 if (VDBG) {
131 Log.d(TAG, "Notify on network switches: " + Arrays.toString(notifySwitches));
132 }
133
134 for (String notifySwitch : notifySwitches) {
135 if (TextUtils.isEmpty(notifySwitch)) continue;
136 String[] transports = notifySwitch.split("-", 2);
137 if (transports.length != 2) {
138 Log.e(TAG, "Invalid network switch notification configuration: " + notifySwitch);
139 continue;
140 }
Hugo Benichia43a0952016-08-30 10:01:15 +0900141 int fromTransport = TRANSPORT_NAMES.get("TRANSPORT_" + transports[0]);
142 int toTransport = TRANSPORT_NAMES.get("TRANSPORT_" + transports[1]);
Lorenzo Colitti5526f9c2016-08-22 16:46:40 +0900143 if (hasTransport(fromNai, fromTransport) && hasTransport(toNai, toTransport)) {
144 return true;
145 }
146 }
147
148 return false;
149 }
150
151 private void showNotification(NetworkAgentInfo fromNai, NetworkAgentInfo toNai) {
Lorenzo Colitti5526f9c2016-08-22 16:46:40 +0900152 mNotifier.showNotification(fromNai.network.netId, NotificationType.NETWORK_SWITCH,
Hugo Benichia43a0952016-08-30 10:01:15 +0900153 fromNai, toNai, createNotificationIntent(), true);
154 }
155
156 @VisibleForTesting
157 protected PendingIntent createNotificationIntent() {
158 return PendingIntent.getActivityAsUser(mContext, 0, CELLULAR_SETTINGS,
159 PendingIntent.FLAG_CANCEL_CURRENT, null, UserHandle.CURRENT);
Lorenzo Colitti5526f9c2016-08-22 16:46:40 +0900160 }
161
162 // Removes any notification that was put up as a result of switching to nai.
163 private void maybeStopNotifying(NetworkAgentInfo nai) {
164 int fromNetId = getNotificationSource(nai);
165 if (fromNetId != NETID_UNSET) {
166 mNotifications.delete(fromNetId);
167 mNotifier.clearNotification(fromNetId);
168 // Toasts can't be deleted.
169 }
170 }
171
172 // Notify the user of a network switch using a notification or a toast.
173 private void notify(NetworkAgentInfo fromNai, NetworkAgentInfo toNai, boolean forceToast) {
Lorenzo Colitti84e6f1232016-08-29 14:03:11 +0900174 int notifyType =
175 mContext.getResources().getInteger(R.integer.config_networkNotifySwitchType);
Lorenzo Colitti5526f9c2016-08-22 16:46:40 +0900176 if (notifyType == NOTIFY_TYPE_NOTIFICATION && forceToast) {
177 notifyType = NOTIFY_TYPE_TOAST;
178 }
179
Lorenzo Colitti5526f9c2016-08-22 16:46:40 +0900180 if (VDBG) {
181 Log.d(TAG, "Notify type: " + sNotifyTypeNames.get(notifyType, "" + notifyType));
182 }
183
Lorenzo Colitti84e6f1232016-08-29 14:03:11 +0900184 switch (notifyType) {
185 case NOTIFY_TYPE_NONE:
186 return;
187 case NOTIFY_TYPE_NOTIFICATION:
188 showNotification(fromNai, toNai);
189 break;
190 case NOTIFY_TYPE_TOAST:
191 mNotifier.showToast(fromNai, toNai);
192 break;
193 default:
194 Log.e(TAG, "Unknown notify type " + notifyType);
195 return;
Lorenzo Colitti5526f9c2016-08-22 16:46:40 +0900196 }
Lorenzo Colitti84e6f1232016-08-29 14:03:11 +0900197
198 if (DBG) {
199 Log.d(TAG, "Notifying switch from=" + fromNai.name() + " to=" + toNai.name() +
200 " type=" + sNotifyTypeNames.get(notifyType, "unknown(" + notifyType + ")"));
201 }
202
203 mNotifications.put(fromNai.network.netId, toNai.network.netId);
204 mEverNotified.put(fromNai.network.netId, true);
Lorenzo Colitti5526f9c2016-08-22 16:46:40 +0900205 }
206
207 // The default network changed from fromNai to toNai due to a change in score.
208 public void noteLingerDefaultNetwork(NetworkAgentInfo fromNai, NetworkAgentInfo toNai) {
209 if (VDBG) {
210 Log.d(TAG, "noteLingerDefaultNetwork from=" + fromNai.name() +
211 " everValidated=" + fromNai.everValidated +
212 " lastValidated=" + fromNai.lastValidated +
213 " to=" + toNai.name());
214 }
215
216 // If we are currently notifying the user because the device switched to fromNai, now that
217 // we are switching away from it we should remove the notification. This includes the case
218 // where we switch back to toNai because its score improved again (e.g., because it regained
219 // Internet access).
220 maybeStopNotifying(fromNai);
221
222 // If this network never validated, don't notify. Otherwise, we could do things like:
223 //
224 // 1. Unvalidated wifi connects.
225 // 2. Unvalidated mobile data connects.
226 // 3. Cell validates, and we show a notification.
227 // or:
228 // 1. User connects to wireless printer.
229 // 2. User turns on cellular data.
230 // 3. We show a notification.
231 if (!fromNai.everValidated) return;
232
233 // If this network is a captive portal, don't notify. This cannot happen on initial connect
234 // to a captive portal, because the everValidated check above will fail. However, it can
235 // happen if the captive portal reasserts itself (e.g., because its timeout fires). In that
236 // case, as soon as the captive portal reasserts itself, we'll show a sign-in notification.
237 // We don't want to overwrite that notification with this one; the user has already been
238 // notified, and of the two, the captive portal notification is the more useful one because
239 // it allows the user to sign in to the captive portal. In this case, display a toast
240 // in addition to the captive portal notification.
241 //
242 // Note that if the network we switch to is already up when the captive portal reappears,
243 // this won't work because NetworkMonitor tells ConnectivityService that the network is
244 // unvalidated (causing a switch) before asking it to show the sign in notification. In this
245 // case, the toast won't show and we'll only display the sign in notification. This is the
246 // best we can do at this time.
247 boolean forceToast = fromNai.networkCapabilities.hasCapability(
248 NetworkCapabilities.NET_CAPABILITY_CAPTIVE_PORTAL);
249
250 // Only show the notification once, in order to avoid irritating the user every time.
251 // TODO: should we do this?
252 if (everNotified(fromNai)) {
253 if (VDBG) {
254 Log.d(TAG, "Not notifying handover from " + fromNai.name() + ", already notified");
255 }
256 return;
257 }
258
Lorenzo Colittic2e10bb2016-08-29 14:03:11 +0900259 // Only show the notification if we switched away because a network became unvalidated, not
260 // because its score changed.
261 // TODO: instead of just skipping notification, keep a note of it, and show it if it becomes
262 // unvalidated.
263 if (fromNai.lastValidated) return;
264
Lorenzo Colitti84e6f1232016-08-29 14:03:11 +0900265 if (!isNotificationEnabled(fromNai, toNai)) return;
266
267 final long now = SystemClock.elapsedRealtime();
268 if (isRateLimited(now) || isAboveDailyLimit(now)) return;
269
270 notify(fromNai, toNai, forceToast);
Lorenzo Colitti5526f9c2016-08-22 16:46:40 +0900271 }
272
273 public void noteDisconnect(NetworkAgentInfo nai) {
274 mNotifications.delete(nai.network.netId);
275 mEverNotified.delete(nai.network.netId);
276 maybeStopNotifying(nai);
277 // No need to cancel notifications on nai: NetworkMonitor does that on disconnect.
278 }
Lorenzo Colitti84e6f1232016-08-29 14:03:11 +0900279
280 private boolean isRateLimited(long now) {
281 final long millisSinceLast = now - mLastNotificationMillis;
282 if (millisSinceLast < mRateLimitMillis) {
283 return true;
284 }
285 mLastNotificationMillis = now;
286 return false;
287 }
288
289 private boolean isAboveDailyLimit(long now) {
290 if (mFirstNotificationMillis == 0) {
291 mFirstNotificationMillis = now;
292 }
293 final long millisSinceFirst = now - mFirstNotificationMillis;
294 if (millisSinceFirst > DateUtils.DAY_IN_MILLIS) {
295 mNotificationCounter = 0;
296 mFirstNotificationMillis = 0;
297 }
298 if (mNotificationCounter >= mDailyLimit) {
299 return true;
300 }
301 mNotificationCounter++;
302 return false;
303 }
Lorenzo Colitti5526f9c2016-08-22 16:46:40 +0900304}