Lorenzo Colitti | 5526f9c | 2016-08-22 16:46:40 +0900 | [diff] [blame^] | 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.net.ConnectivityManager; |
| 21 | import android.net.NetworkCapabilities; |
| 22 | import android.net.Uri; |
| 23 | import android.content.ComponentName; |
| 24 | import android.content.Context; |
| 25 | import android.content.Intent; |
| 26 | import android.os.UserHandle; |
| 27 | import android.provider.Settings; |
| 28 | import android.text.TextUtils; |
| 29 | import android.util.Log; |
| 30 | import android.util.SparseArray; |
| 31 | import android.util.SparseIntArray; |
| 32 | import android.util.SparseBooleanArray; |
| 33 | import java.util.Arrays; |
| 34 | import java.util.HashMap; |
| 35 | |
| 36 | import com.android.internal.util.MessageUtils; |
| 37 | import com.android.server.connectivity.NetworkNotificationManager; |
| 38 | import com.android.server.connectivity.NetworkNotificationManager.NotificationType; |
| 39 | |
| 40 | import static android.net.ConnectivityManager.NETID_UNSET; |
| 41 | |
| 42 | /** |
| 43 | * Class that monitors default network linger events and possibly notifies the user of network |
| 44 | * switches. |
| 45 | * |
| 46 | * This class is not thread-safe and all its methods must be called on the ConnectivityService |
| 47 | * handler thread. |
| 48 | */ |
| 49 | public class LingerMonitor { |
| 50 | |
| 51 | private static final boolean DBG = true; |
| 52 | private static final boolean VDBG = false; |
| 53 | private static final String TAG = LingerMonitor.class.getSimpleName(); |
| 54 | |
| 55 | private static final HashMap<String, Integer> sTransportNames = makeTransportToNameMap(); |
| 56 | private static final Intent CELLULAR_SETTINGS = new Intent().setComponent(new ComponentName( |
| 57 | "com.android.settings", "com.android.settings.Settings$DataUsageSummaryActivity")); |
| 58 | |
| 59 | private static final int NOTIFY_TYPE_NONE = 0; |
| 60 | private static final int NOTIFY_TYPE_NOTIFICATION = 1; |
| 61 | private static final int NOTIFY_TYPE_TOAST = 2; |
| 62 | |
| 63 | private static SparseArray<String> sNotifyTypeNames = MessageUtils.findMessageNames( |
| 64 | new Class[] { LingerMonitor.class }, new String[]{ "NOTIFY_TYPE_" }); |
| 65 | |
| 66 | private final Context mContext; |
| 67 | private final NetworkNotificationManager mNotifier; |
| 68 | |
| 69 | /** Current notifications. Maps the netId we switched away from to the netId we switched to. */ |
| 70 | private final SparseIntArray mNotifications = new SparseIntArray(); |
| 71 | |
| 72 | /** Whether we ever notified that we switched away from a particular network. */ |
| 73 | private final SparseBooleanArray mEverNotified = new SparseBooleanArray(); |
| 74 | |
| 75 | public LingerMonitor(Context context, NetworkNotificationManager notifier) { |
| 76 | mContext = context; |
| 77 | mNotifier = notifier; |
| 78 | } |
| 79 | |
| 80 | private static HashMap<String, Integer> makeTransportToNameMap() { |
| 81 | SparseArray<String> numberToName = MessageUtils.findMessageNames( |
| 82 | new Class[] { NetworkCapabilities.class }, new String[]{ "TRANSPORT_" }); |
| 83 | HashMap<String, Integer> nameToNumber = new HashMap<>(); |
| 84 | for (int i = 0; i < numberToName.size(); i++) { |
| 85 | // MessageUtils will fail to initialize if there are duplicate constant values, so there |
| 86 | // are no duplicates here. |
| 87 | nameToNumber.put(numberToName.valueAt(i), numberToName.keyAt(i)); |
| 88 | } |
| 89 | return nameToNumber; |
| 90 | } |
| 91 | |
| 92 | private static boolean hasTransport(NetworkAgentInfo nai, int transport) { |
| 93 | return nai.networkCapabilities.hasTransport(transport); |
| 94 | } |
| 95 | |
| 96 | private int getNotificationSource(NetworkAgentInfo toNai) { |
| 97 | for (int i = 0; i < mNotifications.size(); i++) { |
| 98 | if (mNotifications.valueAt(i) == toNai.network.netId) { |
| 99 | return mNotifications.keyAt(i); |
| 100 | } |
| 101 | } |
| 102 | return NETID_UNSET; |
| 103 | } |
| 104 | |
| 105 | private boolean everNotified(NetworkAgentInfo nai) { |
| 106 | return mEverNotified.get(nai.network.netId, false); |
| 107 | } |
| 108 | |
| 109 | private boolean isNotificationEnabled(NetworkAgentInfo fromNai, NetworkAgentInfo toNai) { |
| 110 | // TODO: Evaluate moving to CarrierConfigManager. |
| 111 | String[] notifySwitches = mContext.getResources().getStringArray( |
| 112 | com.android.internal.R.array.config_networkNotifySwitches); |
| 113 | |
| 114 | if (VDBG) { |
| 115 | Log.d(TAG, "Notify on network switches: " + Arrays.toString(notifySwitches)); |
| 116 | } |
| 117 | |
| 118 | for (String notifySwitch : notifySwitches) { |
| 119 | if (TextUtils.isEmpty(notifySwitch)) continue; |
| 120 | String[] transports = notifySwitch.split("-", 2); |
| 121 | if (transports.length != 2) { |
| 122 | Log.e(TAG, "Invalid network switch notification configuration: " + notifySwitch); |
| 123 | continue; |
| 124 | } |
| 125 | int fromTransport = sTransportNames.get("TRANSPORT_" + transports[0]); |
| 126 | int toTransport = sTransportNames.get("TRANSPORT_" + transports[1]); |
| 127 | if (hasTransport(fromNai, fromTransport) && hasTransport(toNai, toTransport)) { |
| 128 | return true; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | return false; |
| 133 | } |
| 134 | |
| 135 | private void showNotification(NetworkAgentInfo fromNai, NetworkAgentInfo toNai) { |
| 136 | PendingIntent pendingIntent = PendingIntent.getActivityAsUser( |
| 137 | mContext, 0, CELLULAR_SETTINGS, PendingIntent.FLAG_CANCEL_CURRENT, null, |
| 138 | UserHandle.CURRENT); |
| 139 | |
| 140 | mNotifier.showNotification(fromNai.network.netId, NotificationType.NETWORK_SWITCH, |
| 141 | fromNai, toNai, pendingIntent, true); |
| 142 | } |
| 143 | |
| 144 | // Removes any notification that was put up as a result of switching to nai. |
| 145 | private void maybeStopNotifying(NetworkAgentInfo nai) { |
| 146 | int fromNetId = getNotificationSource(nai); |
| 147 | if (fromNetId != NETID_UNSET) { |
| 148 | mNotifications.delete(fromNetId); |
| 149 | mNotifier.clearNotification(fromNetId); |
| 150 | // Toasts can't be deleted. |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | // Notify the user of a network switch using a notification or a toast. |
| 155 | private void notify(NetworkAgentInfo fromNai, NetworkAgentInfo toNai, boolean forceToast) { |
| 156 | boolean notify = false; |
| 157 | int notifyType = mContext.getResources().getInteger( |
| 158 | com.android.internal.R.integer.config_networkNotifySwitchType); |
| 159 | |
| 160 | if (notifyType == NOTIFY_TYPE_NOTIFICATION && forceToast) { |
| 161 | notifyType = NOTIFY_TYPE_TOAST; |
| 162 | } |
| 163 | |
| 164 | switch (notifyType) { |
| 165 | case NOTIFY_TYPE_NONE: |
| 166 | break; |
| 167 | case NOTIFY_TYPE_NOTIFICATION: |
| 168 | showNotification(fromNai, toNai); |
| 169 | notify = true; |
| 170 | break; |
| 171 | case NOTIFY_TYPE_TOAST: |
| 172 | mNotifier.showToast(fromNai, toNai); |
| 173 | notify = true; |
| 174 | break; |
| 175 | default: |
| 176 | Log.e(TAG, "Unknown notify type " + notifyType); |
| 177 | } |
| 178 | |
| 179 | if (VDBG) { |
| 180 | Log.d(TAG, "Notify type: " + sNotifyTypeNames.get(notifyType, "" + notifyType)); |
| 181 | } |
| 182 | |
| 183 | if (notify) { |
| 184 | if (DBG) { |
| 185 | Log.d(TAG, "Notifying switch from=" + fromNai.name() + " to=" + toNai.name() + |
| 186 | " type=" + sNotifyTypeNames.get(notifyType, "unknown(" + notifyType + ")")); |
| 187 | } |
| 188 | mNotifications.put(fromNai.network.netId, toNai.network.netId); |
| 189 | mEverNotified.put(fromNai.network.netId, true); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | // The default network changed from fromNai to toNai due to a change in score. |
| 194 | public void noteLingerDefaultNetwork(NetworkAgentInfo fromNai, NetworkAgentInfo toNai) { |
| 195 | if (VDBG) { |
| 196 | Log.d(TAG, "noteLingerDefaultNetwork from=" + fromNai.name() + |
| 197 | " everValidated=" + fromNai.everValidated + |
| 198 | " lastValidated=" + fromNai.lastValidated + |
| 199 | " to=" + toNai.name()); |
| 200 | } |
| 201 | |
| 202 | // If we are currently notifying the user because the device switched to fromNai, now that |
| 203 | // we are switching away from it we should remove the notification. This includes the case |
| 204 | // where we switch back to toNai because its score improved again (e.g., because it regained |
| 205 | // Internet access). |
| 206 | maybeStopNotifying(fromNai); |
| 207 | |
| 208 | // If this network never validated, don't notify. Otherwise, we could do things like: |
| 209 | // |
| 210 | // 1. Unvalidated wifi connects. |
| 211 | // 2. Unvalidated mobile data connects. |
| 212 | // 3. Cell validates, and we show a notification. |
| 213 | // or: |
| 214 | // 1. User connects to wireless printer. |
| 215 | // 2. User turns on cellular data. |
| 216 | // 3. We show a notification. |
| 217 | if (!fromNai.everValidated) return; |
| 218 | |
| 219 | // If this network is a captive portal, don't notify. This cannot happen on initial connect |
| 220 | // to a captive portal, because the everValidated check above will fail. However, it can |
| 221 | // happen if the captive portal reasserts itself (e.g., because its timeout fires). In that |
| 222 | // case, as soon as the captive portal reasserts itself, we'll show a sign-in notification. |
| 223 | // We don't want to overwrite that notification with this one; the user has already been |
| 224 | // notified, and of the two, the captive portal notification is the more useful one because |
| 225 | // it allows the user to sign in to the captive portal. In this case, display a toast |
| 226 | // in addition to the captive portal notification. |
| 227 | // |
| 228 | // Note that if the network we switch to is already up when the captive portal reappears, |
| 229 | // this won't work because NetworkMonitor tells ConnectivityService that the network is |
| 230 | // unvalidated (causing a switch) before asking it to show the sign in notification. In this |
| 231 | // case, the toast won't show and we'll only display the sign in notification. This is the |
| 232 | // best we can do at this time. |
| 233 | boolean forceToast = fromNai.networkCapabilities.hasCapability( |
| 234 | NetworkCapabilities.NET_CAPABILITY_CAPTIVE_PORTAL); |
| 235 | |
| 236 | // Only show the notification once, in order to avoid irritating the user every time. |
| 237 | // TODO: should we do this? |
| 238 | if (everNotified(fromNai)) { |
| 239 | if (VDBG) { |
| 240 | Log.d(TAG, "Not notifying handover from " + fromNai.name() + ", already notified"); |
| 241 | } |
| 242 | return; |
| 243 | } |
| 244 | |
| 245 | if (isNotificationEnabled(fromNai, toNai)) { |
| 246 | notify(fromNai, toNai, forceToast); |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | public void noteDisconnect(NetworkAgentInfo nai) { |
| 251 | mNotifications.delete(nai.network.netId); |
| 252 | mEverNotified.delete(nai.network.netId); |
| 253 | maybeStopNotifying(nai); |
| 254 | // No need to cancel notifications on nai: NetworkMonitor does that on disconnect. |
| 255 | } |
| 256 | } |