Home | History | Annotate | Download | only in connectivity
      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 
     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     }
     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 
    124     @VisibleForTesting
    125     public boolean isNotificationEnabled(NetworkAgentInfo fromNai, NetworkAgentInfo toNai) {
    126         // TODO: Evaluate moving to CarrierConfigManager.
    127         String[] notifySwitches =
    128                 mContext.getResources().getStringArray(R.array.config_networkNotifySwitches);
    129 
    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             }
    141             int fromTransport = TRANSPORT_NAMES.get("TRANSPORT_" + transports[0]);
    142             int toTransport = TRANSPORT_NAMES.get("TRANSPORT_" + transports[1]);
    143             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) {
    152         mNotifier.showNotification(fromNai.network.netId, NotificationType.NETWORK_SWITCH,
    153                 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);
    160     }
    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) {
    174         int notifyType =
    175                 mContext.getResources().getInteger(R.integer.config_networkNotifySwitchType);
    176         if (notifyType == NOTIFY_TYPE_NOTIFICATION && forceToast) {
    177             notifyType = NOTIFY_TYPE_TOAST;
    178         }
    179 
    180         if (VDBG) {
    181             Log.d(TAG, "Notify type: " + sNotifyTypeNames.get(notifyType, "" + notifyType));
    182         }
    183 
    184         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;
    196         }
    197 
    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);
    205     }
    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 
    259         // 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 
    265         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);
    271     }
    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     }
    279 
    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     }
    304 }
    305