1 /*
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 
17 package com.android.server.connectivity;
18 
19 import android.app.PendingIntent;
20 import android.content.ComponentName;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.net.NetworkCapabilities;
24 import android.os.SystemClock;
25 import android.os.UserHandle;
26 import android.text.TextUtils;
27 import android.text.format.DateUtils;
28 import android.util.Log;
29 import android.util.SparseArray;
30 import android.util.SparseIntArray;
31 import android.util.SparseBooleanArray;
32 import java.util.Arrays;
33 import java.util.HashMap;
34 
35 import com.android.internal.R;
36 import com.android.internal.annotations.VisibleForTesting;
37 import com.android.internal.util.MessageUtils;
38 import com.android.server.connectivity.NetworkNotificationManager;
39 import com.android.server.connectivity.NetworkNotificationManager.NotificationType;
40 
41 import 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  */
50 public 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 
56     public static final int DEFAULT_NOTIFICATION_DAILY_LIMIT = 3;
57     public static final long DEFAULT_NOTIFICATION_RATE_LIMIT_MILLIS = DateUtils.MINUTE_IN_MILLIS;
58 
59     private static final HashMap<String, Integer> TRANSPORT_NAMES = makeTransportToNameMap();
60     @VisibleForTesting
61     public static final Intent CELLULAR_SETTINGS = new Intent().setComponent(new ComponentName(
62             "com.android.settings", "com.android.settings.Settings$DataUsageSummaryActivity"));
63 
64     @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;
68 
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;
74     private final int mDailyLimit;
75     private final long mRateLimitMillis;
76 
77     private long mFirstNotificationMillis;
78     private long mLastNotificationMillis;
79     private int mNotificationCounter;
80 
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 
LingerMonitor(Context context, NetworkNotificationManager notifier, int dailyLimit, long rateLimitMillis)87     public LingerMonitor(Context context, NetworkNotificationManager notifier,
88             int dailyLimit, long rateLimitMillis) {
89         mContext = context;
90         mNotifier = notifier;
91         mDailyLimit = dailyLimit;
92         mRateLimitMillis = rateLimitMillis;
93         // Ensure that (now - mLastNotificationMillis) >= rateLimitMillis at first
94         mLastNotificationMillis = -rateLimitMillis;
95     }
96 
makeTransportToNameMap()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 
hasTransport(NetworkAgentInfo nai, int transport)109     private static boolean hasTransport(NetworkAgentInfo nai, int transport) {
110         return nai.networkCapabilities.hasTransport(transport);
111     }
112 
getNotificationSource(NetworkAgentInfo toNai)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 
everNotified(NetworkAgentInfo nai)122     private boolean everNotified(NetworkAgentInfo nai) {
123         return mEverNotified.get(nai.network.netId, false);
124     }
125 
126     @VisibleForTesting
isNotificationEnabled(NetworkAgentInfo fromNai, NetworkAgentInfo toNai)127     public boolean isNotificationEnabled(NetworkAgentInfo fromNai, NetworkAgentInfo toNai) {
128         // TODO: Evaluate moving to CarrierConfigManager.
129         String[] notifySwitches =
130                 mContext.getResources().getStringArray(R.array.config_networkNotifySwitches);
131 
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             }
143             int fromTransport = TRANSPORT_NAMES.get("TRANSPORT_" + transports[0]);
144             int toTransport = TRANSPORT_NAMES.get("TRANSPORT_" + transports[1]);
145             if (hasTransport(fromNai, fromTransport) && hasTransport(toNai, toTransport)) {
146                 return true;
147             }
148         }
149 
150         return false;
151     }
152 
showNotification(NetworkAgentInfo fromNai, NetworkAgentInfo toNai)153     private void showNotification(NetworkAgentInfo fromNai, NetworkAgentInfo toNai) {
154         mNotifier.showNotification(fromNai.network.netId, NotificationType.NETWORK_SWITCH,
155                 fromNai, toNai, createNotificationIntent(), true);
156     }
157 
158     @VisibleForTesting
createNotificationIntent()159     protected PendingIntent createNotificationIntent() {
160         return PendingIntent.getActivityAsUser(mContext, 0, CELLULAR_SETTINGS,
161                 PendingIntent.FLAG_CANCEL_CURRENT, null, UserHandle.CURRENT);
162     }
163 
164     // Removes any notification that was put up as a result of switching to nai.
maybeStopNotifying(NetworkAgentInfo 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.
notify(NetworkAgentInfo fromNai, NetworkAgentInfo toNai, boolean forceToast)175     private void notify(NetworkAgentInfo fromNai, NetworkAgentInfo toNai, boolean forceToast) {
176         int notifyType =
177                 mContext.getResources().getInteger(R.integer.config_networkNotifySwitchType);
178         if (notifyType == NOTIFY_TYPE_NOTIFICATION && forceToast) {
179             notifyType = NOTIFY_TYPE_TOAST;
180         }
181 
182         if (VDBG) {
183             Log.d(TAG, "Notify type: " + sNotifyTypeNames.get(notifyType, "" + notifyType));
184         }
185 
186         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;
198         }
199 
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);
207     }
208 
209     // The default network changed from fromNai to toNai due to a change in score.
noteLingerDefaultNetwork(NetworkAgentInfo fromNai, NetworkAgentInfo toNai)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 
261         // 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 
267         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);
273     }
274 
noteDisconnect(NetworkAgentInfo nai)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     }
281 
isRateLimited(long now)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 
isAboveDailyLimit(long now)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     }
306 }
307