blob: 0e727c5e0b5f93816f985b902dcdbe7267cb69a3 [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;
Remi NGUYEN VAN1fedd292018-11-02 16:33:23 +090093 // Ensure that (now - mFirstNotificationMillis) >= rateLimitMillis at first
94 mFirstNotificationMillis = -rateLimitMillis;
Lorenzo Colitti5526f9c2016-08-22 16:46:40 +090095 }
96
97 private static HashMap<String, Integer> makeTransportToNameMap() {
98 SparseArray<String> numberToName = MessageUtils.findMessageNames(
99 new Class[] { NetworkCapabilities.class }, new String[]{ "TRANSPORT_" });
100 HashMap<String, Integer> nameToNumber = new HashMap<>();
101 for (int i = 0; i < numberToName.size(); i++) {
102 // MessageUtils will fail to initialize if there are duplicate constant values, so there
103 // are no duplicates here.
104 nameToNumber.put(numberToName.valueAt(i), numberToName.keyAt(i));
105 }
106 return nameToNumber;
107 }
108
109 private static boolean hasTransport(NetworkAgentInfo nai, int transport) {
110 return nai.networkCapabilities.hasTransport(transport);
111 }
112
113 private int getNotificationSource(NetworkAgentInfo toNai) {
114 for (int i = 0; i < mNotifications.size(); i++) {
115 if (mNotifications.valueAt(i) == toNai.network.netId) {
116 return mNotifications.keyAt(i);
117 }
118 }
119 return NETID_UNSET;
120 }
121
122 private boolean everNotified(NetworkAgentInfo nai) {
123 return mEverNotified.get(nai.network.netId, false);
124 }
125
Hugo Benichia43a0952016-08-30 10:01:15 +0900126 @VisibleForTesting
127 public boolean isNotificationEnabled(NetworkAgentInfo fromNai, NetworkAgentInfo toNai) {
Lorenzo Colitti5526f9c2016-08-22 16:46:40 +0900128 // TODO: Evaluate moving to CarrierConfigManager.
Lorenzo Colitti84e6f1232016-08-29 14:03:11 +0900129 String[] notifySwitches =
130 mContext.getResources().getStringArray(R.array.config_networkNotifySwitches);
Lorenzo Colitti5526f9c2016-08-22 16:46:40 +0900131
132 if (VDBG) {
133 Log.d(TAG, "Notify on network switches: " + Arrays.toString(notifySwitches));
134 }
135
136 for (String notifySwitch : notifySwitches) {
137 if (TextUtils.isEmpty(notifySwitch)) continue;
138 String[] transports = notifySwitch.split("-", 2);
139 if (transports.length != 2) {
140 Log.e(TAG, "Invalid network switch notification configuration: " + notifySwitch);
141 continue;
142 }
Hugo Benichia43a0952016-08-30 10:01:15 +0900143 int fromTransport = TRANSPORT_NAMES.get("TRANSPORT_" + transports[0]);
144 int toTransport = TRANSPORT_NAMES.get("TRANSPORT_" + transports[1]);
Lorenzo Colitti5526f9c2016-08-22 16:46:40 +0900145 if (hasTransport(fromNai, fromTransport) && hasTransport(toNai, toTransport)) {
146 return true;
147 }
148 }
149
150 return false;
151 }
152
153 private void showNotification(NetworkAgentInfo fromNai, NetworkAgentInfo toNai) {
Lorenzo Colitti5526f9c2016-08-22 16:46:40 +0900154 mNotifier.showNotification(fromNai.network.netId, NotificationType.NETWORK_SWITCH,
Hugo Benichia43a0952016-08-30 10:01:15 +0900155 fromNai, toNai, createNotificationIntent(), true);
156 }
157
158 @VisibleForTesting
159 protected PendingIntent createNotificationIntent() {
160 return PendingIntent.getActivityAsUser(mContext, 0, CELLULAR_SETTINGS,
161 PendingIntent.FLAG_CANCEL_CURRENT, null, UserHandle.CURRENT);
Lorenzo Colitti5526f9c2016-08-22 16:46:40 +0900162 }
163
164 // Removes any notification that was put up as a result of switching to nai.
165 private void maybeStopNotifying(NetworkAgentInfo nai) {
166 int fromNetId = getNotificationSource(nai);
167 if (fromNetId != NETID_UNSET) {
168 mNotifications.delete(fromNetId);
169 mNotifier.clearNotification(fromNetId);
170 // Toasts can't be deleted.
171 }
172 }
173
174 // Notify the user of a network switch using a notification or a toast.
175 private void notify(NetworkAgentInfo fromNai, NetworkAgentInfo toNai, boolean forceToast) {
Lorenzo Colitti84e6f1232016-08-29 14:03:11 +0900176 int notifyType =
177 mContext.getResources().getInteger(R.integer.config_networkNotifySwitchType);
Lorenzo Colitti5526f9c2016-08-22 16:46:40 +0900178 if (notifyType == NOTIFY_TYPE_NOTIFICATION && forceToast) {
179 notifyType = NOTIFY_TYPE_TOAST;
180 }
181
Lorenzo Colitti5526f9c2016-08-22 16:46:40 +0900182 if (VDBG) {
183 Log.d(TAG, "Notify type: " + sNotifyTypeNames.get(notifyType, "" + notifyType));
184 }
185
Lorenzo Colitti84e6f1232016-08-29 14:03:11 +0900186 switch (notifyType) {
187 case NOTIFY_TYPE_NONE:
188 return;
189 case NOTIFY_TYPE_NOTIFICATION:
190 showNotification(fromNai, toNai);
191 break;
192 case NOTIFY_TYPE_TOAST:
193 mNotifier.showToast(fromNai, toNai);
194 break;
195 default:
196 Log.e(TAG, "Unknown notify type " + notifyType);
197 return;
Lorenzo Colitti5526f9c2016-08-22 16:46:40 +0900198 }
Lorenzo Colitti84e6f1232016-08-29 14:03:11 +0900199
200 if (DBG) {
201 Log.d(TAG, "Notifying switch from=" + fromNai.name() + " to=" + toNai.name() +
202 " type=" + sNotifyTypeNames.get(notifyType, "unknown(" + notifyType + ")"));
203 }
204
205 mNotifications.put(fromNai.network.netId, toNai.network.netId);
206 mEverNotified.put(fromNai.network.netId, true);
Lorenzo Colitti5526f9c2016-08-22 16:46:40 +0900207 }
208
209 // The default network changed from fromNai to toNai due to a change in score.
210 public void noteLingerDefaultNetwork(NetworkAgentInfo fromNai, NetworkAgentInfo toNai) {
211 if (VDBG) {
212 Log.d(TAG, "noteLingerDefaultNetwork from=" + fromNai.name() +
213 " everValidated=" + fromNai.everValidated +
214 " lastValidated=" + fromNai.lastValidated +
215 " to=" + toNai.name());
216 }
217
218 // If we are currently notifying the user because the device switched to fromNai, now that
219 // we are switching away from it we should remove the notification. This includes the case
220 // where we switch back to toNai because its score improved again (e.g., because it regained
221 // Internet access).
222 maybeStopNotifying(fromNai);
223
224 // If this network never validated, don't notify. Otherwise, we could do things like:
225 //
226 // 1. Unvalidated wifi connects.
227 // 2. Unvalidated mobile data connects.
228 // 3. Cell validates, and we show a notification.
229 // or:
230 // 1. User connects to wireless printer.
231 // 2. User turns on cellular data.
232 // 3. We show a notification.
233 if (!fromNai.everValidated) return;
234
235 // If this network is a captive portal, don't notify. This cannot happen on initial connect
236 // to a captive portal, because the everValidated check above will fail. However, it can
237 // happen if the captive portal reasserts itself (e.g., because its timeout fires). In that
238 // case, as soon as the captive portal reasserts itself, we'll show a sign-in notification.
239 // We don't want to overwrite that notification with this one; the user has already been
240 // notified, and of the two, the captive portal notification is the more useful one because
241 // it allows the user to sign in to the captive portal. In this case, display a toast
242 // in addition to the captive portal notification.
243 //
244 // Note that if the network we switch to is already up when the captive portal reappears,
245 // this won't work because NetworkMonitor tells ConnectivityService that the network is
246 // unvalidated (causing a switch) before asking it to show the sign in notification. In this
247 // case, the toast won't show and we'll only display the sign in notification. This is the
248 // best we can do at this time.
249 boolean forceToast = fromNai.networkCapabilities.hasCapability(
250 NetworkCapabilities.NET_CAPABILITY_CAPTIVE_PORTAL);
251
252 // Only show the notification once, in order to avoid irritating the user every time.
253 // TODO: should we do this?
254 if (everNotified(fromNai)) {
255 if (VDBG) {
256 Log.d(TAG, "Not notifying handover from " + fromNai.name() + ", already notified");
257 }
258 return;
259 }
260
Lorenzo Colittic2e10bb2016-08-29 14:03:11 +0900261 // Only show the notification if we switched away because a network became unvalidated, not
262 // because its score changed.
263 // TODO: instead of just skipping notification, keep a note of it, and show it if it becomes
264 // unvalidated.
265 if (fromNai.lastValidated) return;
266
Lorenzo Colitti84e6f1232016-08-29 14:03:11 +0900267 if (!isNotificationEnabled(fromNai, toNai)) return;
268
269 final long now = SystemClock.elapsedRealtime();
270 if (isRateLimited(now) || isAboveDailyLimit(now)) return;
271
272 notify(fromNai, toNai, forceToast);
Lorenzo Colitti5526f9c2016-08-22 16:46:40 +0900273 }
274
275 public void noteDisconnect(NetworkAgentInfo nai) {
276 mNotifications.delete(nai.network.netId);
277 mEverNotified.delete(nai.network.netId);
278 maybeStopNotifying(nai);
279 // No need to cancel notifications on nai: NetworkMonitor does that on disconnect.
280 }
Lorenzo Colitti84e6f1232016-08-29 14:03:11 +0900281
282 private boolean isRateLimited(long now) {
283 final long millisSinceLast = now - mLastNotificationMillis;
284 if (millisSinceLast < mRateLimitMillis) {
285 return true;
286 }
287 mLastNotificationMillis = now;
288 return false;
289 }
290
291 private boolean isAboveDailyLimit(long now) {
292 if (mFirstNotificationMillis == 0) {
293 mFirstNotificationMillis = now;
294 }
295 final long millisSinceFirst = now - mFirstNotificationMillis;
296 if (millisSinceFirst > DateUtils.DAY_IN_MILLIS) {
297 mNotificationCounter = 0;
298 mFirstNotificationMillis = 0;
299 }
300 if (mNotificationCounter >= mDailyLimit) {
301 return true;
302 }
303 mNotificationCounter++;
304 return false;
305 }
Lorenzo Colitti5526f9c2016-08-22 16:46:40 +0900306}