Erik Kline | 9bba340 | 2017-01-13 16:46:52 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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.tethering; |
| 18 | |
Erik Kline | 3760999 | 2017-06-05 16:02:02 +0900 | [diff] [blame] | 19 | import static android.net.ConnectivityManager.getNetworkTypeName; |
| 20 | import static android.net.ConnectivityManager.TYPE_NONE; |
Erik Kline | dd8e891 | 2017-01-18 16:08:06 +0900 | [diff] [blame] | 21 | import static android.net.ConnectivityManager.TYPE_MOBILE_DUN; |
| 22 | import static android.net.ConnectivityManager.TYPE_MOBILE_HIPRI; |
| 23 | |
Erik Kline | 9bba340 | 2017-01-13 16:46:52 +0900 | [diff] [blame] | 24 | import android.content.Context; |
Erik Kline | b583b03 | 2017-02-22 12:58:24 +0900 | [diff] [blame] | 25 | import android.os.Handler; |
| 26 | import android.os.Looper; |
Erik Kline | 9bba340 | 2017-01-13 16:46:52 +0900 | [diff] [blame] | 27 | import android.net.ConnectivityManager; |
| 28 | import android.net.ConnectivityManager.NetworkCallback; |
| 29 | import android.net.LinkProperties; |
| 30 | import android.net.Network; |
| 31 | import android.net.NetworkCapabilities; |
| 32 | import android.net.NetworkRequest; |
| 33 | import android.net.NetworkState; |
Erik Kline | 7747fd4 | 2017-05-12 16:52:48 +0900 | [diff] [blame] | 34 | import android.net.util.SharedLog; |
Erik Kline | 9bba340 | 2017-01-13 16:46:52 +0900 | [diff] [blame] | 35 | import android.util.Log; |
| 36 | |
Erik Kline | 885a909 | 2017-01-16 16:27:22 +0900 | [diff] [blame] | 37 | import com.android.internal.annotations.VisibleForTesting; |
Erik Kline | 9bba340 | 2017-01-13 16:46:52 +0900 | [diff] [blame] | 38 | import com.android.internal.util.StateMachine; |
| 39 | |
| 40 | import java.util.HashMap; |
| 41 | |
| 42 | |
| 43 | /** |
| 44 | * A class to centralize all the network and link properties information |
| 45 | * pertaining to the current and any potential upstream network. |
| 46 | * |
| 47 | * Calling #start() registers two callbacks: one to track the system default |
Erik Kline | d2ec391 | 2017-01-25 00:53:04 +0900 | [diff] [blame] | 48 | * network and a second to observe all networks. The latter is necessary |
| 49 | * while the expression of preferred upstreams remains a list of legacy |
| 50 | * connectivity types. In future, this can be revisited. |
Erik Kline | 9bba340 | 2017-01-13 16:46:52 +0900 | [diff] [blame] | 51 | * |
| 52 | * The methods and data members of this class are only to be accessed and |
| 53 | * modified from the tethering master state machine thread. Any other |
| 54 | * access semantics would necessitate the addition of locking. |
| 55 | * |
| 56 | * TODO: Move upstream selection logic here. |
| 57 | * |
Erik Kline | d2ec391 | 2017-01-25 00:53:04 +0900 | [diff] [blame] | 58 | * All callback methods are run on the same thread as the specified target |
| 59 | * state machine. This class does not require locking when accessed from this |
| 60 | * thread. Access from other threads is not advised. |
| 61 | * |
Erik Kline | 9bba340 | 2017-01-13 16:46:52 +0900 | [diff] [blame] | 62 | * @hide |
| 63 | */ |
| 64 | public class UpstreamNetworkMonitor { |
| 65 | private static final String TAG = UpstreamNetworkMonitor.class.getSimpleName(); |
| 66 | private static final boolean DBG = false; |
| 67 | private static final boolean VDBG = false; |
| 68 | |
| 69 | public static final int EVENT_ON_AVAILABLE = 1; |
| 70 | public static final int EVENT_ON_CAPABILITIES = 2; |
| 71 | public static final int EVENT_ON_LINKPROPERTIES = 3; |
| 72 | public static final int EVENT_ON_LOST = 4; |
| 73 | |
Erik Kline | 35bf06c | 2017-01-26 18:08:28 +0900 | [diff] [blame] | 74 | private static final int CALLBACK_LISTEN_ALL = 1; |
| 75 | private static final int CALLBACK_TRACK_DEFAULT = 2; |
| 76 | private static final int CALLBACK_MOBILE_REQUEST = 3; |
Erik Kline | d2ec391 | 2017-01-25 00:53:04 +0900 | [diff] [blame] | 77 | |
Erik Kline | 9bba340 | 2017-01-13 16:46:52 +0900 | [diff] [blame] | 78 | private final Context mContext; |
Erik Kline | 7747fd4 | 2017-05-12 16:52:48 +0900 | [diff] [blame] | 79 | private final SharedLog mLog; |
Erik Kline | 9bba340 | 2017-01-13 16:46:52 +0900 | [diff] [blame] | 80 | private final StateMachine mTarget; |
Erik Kline | b583b03 | 2017-02-22 12:58:24 +0900 | [diff] [blame] | 81 | private final Handler mHandler; |
Erik Kline | 9bba340 | 2017-01-13 16:46:52 +0900 | [diff] [blame] | 82 | private final int mWhat; |
| 83 | private final HashMap<Network, NetworkState> mNetworkMap = new HashMap<>(); |
| 84 | private ConnectivityManager mCM; |
Erik Kline | d2ec391 | 2017-01-25 00:53:04 +0900 | [diff] [blame] | 85 | private NetworkCallback mListenAllCallback; |
Erik Kline | 9bba340 | 2017-01-13 16:46:52 +0900 | [diff] [blame] | 86 | private NetworkCallback mDefaultNetworkCallback; |
Erik Kline | 9bba340 | 2017-01-13 16:46:52 +0900 | [diff] [blame] | 87 | private NetworkCallback mMobileNetworkCallback; |
| 88 | private boolean mDunRequired; |
Erik Kline | d2ec391 | 2017-01-25 00:53:04 +0900 | [diff] [blame] | 89 | private Network mCurrentDefault; |
Erik Kline | 9bba340 | 2017-01-13 16:46:52 +0900 | [diff] [blame] | 90 | |
Erik Kline | 7747fd4 | 2017-05-12 16:52:48 +0900 | [diff] [blame] | 91 | public UpstreamNetworkMonitor(Context ctx, StateMachine tgt, int what, SharedLog log) { |
Erik Kline | 9bba340 | 2017-01-13 16:46:52 +0900 | [diff] [blame] | 92 | mContext = ctx; |
| 93 | mTarget = tgt; |
Erik Kline | b583b03 | 2017-02-22 12:58:24 +0900 | [diff] [blame] | 94 | mHandler = mTarget.getHandler(); |
Erik Kline | 9bba340 | 2017-01-13 16:46:52 +0900 | [diff] [blame] | 95 | mWhat = what; |
Erik Kline | 7747fd4 | 2017-05-12 16:52:48 +0900 | [diff] [blame] | 96 | mLog = log.forSubComponent(TAG); |
Erik Kline | 9bba340 | 2017-01-13 16:46:52 +0900 | [diff] [blame] | 97 | } |
| 98 | |
Erik Kline | 885a909 | 2017-01-16 16:27:22 +0900 | [diff] [blame] | 99 | @VisibleForTesting |
Erik Kline | 7747fd4 | 2017-05-12 16:52:48 +0900 | [diff] [blame] | 100 | public UpstreamNetworkMonitor( |
| 101 | StateMachine tgt, int what, ConnectivityManager cm, SharedLog log) { |
| 102 | this(null, tgt, what, log); |
Erik Kline | 885a909 | 2017-01-16 16:27:22 +0900 | [diff] [blame] | 103 | mCM = cm; |
| 104 | } |
| 105 | |
Erik Kline | 9bba340 | 2017-01-13 16:46:52 +0900 | [diff] [blame] | 106 | public void start() { |
| 107 | stop(); |
| 108 | |
Erik Kline | d2ec391 | 2017-01-25 00:53:04 +0900 | [diff] [blame] | 109 | final NetworkRequest listenAllRequest = new NetworkRequest.Builder() |
| 110 | .clearCapabilities().build(); |
Erik Kline | 35bf06c | 2017-01-26 18:08:28 +0900 | [diff] [blame] | 111 | mListenAllCallback = new UpstreamNetworkCallback(CALLBACK_LISTEN_ALL); |
Erik Kline | b583b03 | 2017-02-22 12:58:24 +0900 | [diff] [blame] | 112 | cm().registerNetworkCallback(listenAllRequest, mListenAllCallback, mHandler); |
Erik Kline | 9bba340 | 2017-01-13 16:46:52 +0900 | [diff] [blame] | 113 | |
Erik Kline | 35bf06c | 2017-01-26 18:08:28 +0900 | [diff] [blame] | 114 | mDefaultNetworkCallback = new UpstreamNetworkCallback(CALLBACK_TRACK_DEFAULT); |
Erik Kline | b583b03 | 2017-02-22 12:58:24 +0900 | [diff] [blame] | 115 | cm().registerDefaultNetworkCallback(mDefaultNetworkCallback, mHandler); |
Erik Kline | 9bba340 | 2017-01-13 16:46:52 +0900 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | public void stop() { |
| 119 | releaseMobileNetworkRequest(); |
| 120 | |
| 121 | releaseCallback(mDefaultNetworkCallback); |
| 122 | mDefaultNetworkCallback = null; |
| 123 | |
Erik Kline | d2ec391 | 2017-01-25 00:53:04 +0900 | [diff] [blame] | 124 | releaseCallback(mListenAllCallback); |
| 125 | mListenAllCallback = null; |
Erik Kline | 9bba340 | 2017-01-13 16:46:52 +0900 | [diff] [blame] | 126 | |
| 127 | mNetworkMap.clear(); |
| 128 | } |
| 129 | |
Erik Kline | efdd3f4c | 2017-01-20 16:31:29 +0900 | [diff] [blame] | 130 | public void updateMobileRequiresDun(boolean dunRequired) { |
Erik Kline | 9bba340 | 2017-01-13 16:46:52 +0900 | [diff] [blame] | 131 | final boolean valueChanged = (mDunRequired != dunRequired); |
| 132 | mDunRequired = dunRequired; |
| 133 | if (valueChanged && mobileNetworkRequested()) { |
| 134 | releaseMobileNetworkRequest(); |
| 135 | registerMobileNetworkRequest(); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | public boolean mobileNetworkRequested() { |
| 140 | return (mMobileNetworkCallback != null); |
| 141 | } |
| 142 | |
| 143 | public void registerMobileNetworkRequest() { |
Erik Kline | efdd3f4c | 2017-01-20 16:31:29 +0900 | [diff] [blame] | 144 | if (mMobileNetworkCallback != null) { |
Erik Kline | 7747fd4 | 2017-05-12 16:52:48 +0900 | [diff] [blame] | 145 | mLog.e("registerMobileNetworkRequest() already registered"); |
Erik Kline | efdd3f4c | 2017-01-20 16:31:29 +0900 | [diff] [blame] | 146 | return; |
| 147 | } |
Erik Kline | 9bba340 | 2017-01-13 16:46:52 +0900 | [diff] [blame] | 148 | |
Erik Kline | 35bf06c | 2017-01-26 18:08:28 +0900 | [diff] [blame] | 149 | // The following use of the legacy type system cannot be removed until |
| 150 | // after upstream selection no longer finds networks by legacy type. |
| 151 | // See also http://b/34364553 . |
| 152 | final int legacyType = mDunRequired ? TYPE_MOBILE_DUN : TYPE_MOBILE_HIPRI; |
| 153 | |
| 154 | final NetworkRequest mobileUpstreamRequest = new NetworkRequest.Builder() |
| 155 | .setCapabilities(ConnectivityManager.networkCapabilitiesForType(legacyType)) |
| 156 | .build(); |
Erik Kline | 9bba340 | 2017-01-13 16:46:52 +0900 | [diff] [blame] | 157 | |
| 158 | // The existing default network and DUN callbacks will be notified. |
| 159 | // Therefore, to avoid duplicate notifications, we only register a no-op. |
Erik Kline | 35bf06c | 2017-01-26 18:08:28 +0900 | [diff] [blame] | 160 | mMobileNetworkCallback = new UpstreamNetworkCallback(CALLBACK_MOBILE_REQUEST); |
Erik Kline | 9bba340 | 2017-01-13 16:46:52 +0900 | [diff] [blame] | 161 | |
Erik Kline | efdd3f4c | 2017-01-20 16:31:29 +0900 | [diff] [blame] | 162 | // TODO: Change the timeout from 0 (no onUnavailable callback) to some |
| 163 | // moderate callback timeout. This might be useful for updating some UI. |
| 164 | // Additionally, we log a message to aid in any subsequent debugging. |
Erik Kline | 7747fd4 | 2017-05-12 16:52:48 +0900 | [diff] [blame] | 165 | mLog.i("requesting mobile upstream network: " + mobileUpstreamRequest); |
Erik Kline | 9bba340 | 2017-01-13 16:46:52 +0900 | [diff] [blame] | 166 | |
Erik Kline | b583b03 | 2017-02-22 12:58:24 +0900 | [diff] [blame] | 167 | cm().requestNetwork(mobileUpstreamRequest, mMobileNetworkCallback, 0, legacyType, mHandler); |
Erik Kline | 9bba340 | 2017-01-13 16:46:52 +0900 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | public void releaseMobileNetworkRequest() { |
| 171 | if (mMobileNetworkCallback == null) return; |
| 172 | |
| 173 | cm().unregisterNetworkCallback(mMobileNetworkCallback); |
| 174 | mMobileNetworkCallback = null; |
| 175 | } |
| 176 | |
| 177 | public NetworkState lookup(Network network) { |
| 178 | return (network != null) ? mNetworkMap.get(network) : null; |
| 179 | } |
| 180 | |
Erik Kline | 3760999 | 2017-06-05 16:02:02 +0900 | [diff] [blame] | 181 | // So many TODOs here, but chief among them is: make this functionality an |
| 182 | // integral part of this class such that whenever a higher priority network |
| 183 | // becomes available and useful we (a) file a request to keep it up as |
| 184 | // necessary and (b) change all upstream tracking state accordingly (by |
| 185 | // passing LinkProperties up to Tethering). |
| 186 | // |
| 187 | // Next TODO: return NetworkState instead of just the type. |
| 188 | public int selectPreferredUpstreamType(Iterable<Integer> preferredTypes) { |
| 189 | final TypeStatePair typeStatePair = findFirstAvailableUpstreamByType( |
| 190 | mNetworkMap.values(), preferredTypes); |
| 191 | |
| 192 | mLog.log("preferred upstream type: " + getNetworkTypeName(typeStatePair.type)); |
| 193 | |
| 194 | switch (typeStatePair.type) { |
| 195 | case TYPE_MOBILE_DUN: |
| 196 | case TYPE_MOBILE_HIPRI: |
| 197 | // If we're on DUN, put our own grab on it. |
| 198 | registerMobileNetworkRequest(); |
| 199 | break; |
| 200 | case TYPE_NONE: |
| 201 | break; |
| 202 | default: |
| 203 | /* If we've found an active upstream connection that's not DUN/HIPRI |
| 204 | * we should stop any outstanding DUN/HIPRI start requests. |
| 205 | * |
| 206 | * If we found NONE we don't want to do this as we want any previous |
| 207 | * requests to keep trying to bring up something we can use. |
| 208 | */ |
| 209 | releaseMobileNetworkRequest(); |
| 210 | break; |
| 211 | } |
| 212 | |
| 213 | return typeStatePair.type; |
| 214 | } |
| 215 | |
Erik Kline | d2ec391 | 2017-01-25 00:53:04 +0900 | [diff] [blame] | 216 | private void handleAvailable(int callbackType, Network network) { |
| 217 | if (VDBG) Log.d(TAG, "EVENT_ON_AVAILABLE for " + network); |
| 218 | |
Erik Kline | 9bba340 | 2017-01-13 16:46:52 +0900 | [diff] [blame] | 219 | if (!mNetworkMap.containsKey(network)) { |
| 220 | mNetworkMap.put(network, |
| 221 | new NetworkState(null, null, null, network, null, null)); |
| 222 | } |
| 223 | |
Erik Kline | d2ec391 | 2017-01-25 00:53:04 +0900 | [diff] [blame] | 224 | // Always request whatever extra information we can, in case this |
| 225 | // was already up when start() was called, in which case we would |
| 226 | // not have been notified of any information that had not changed. |
Erik Kline | 35bf06c | 2017-01-26 18:08:28 +0900 | [diff] [blame] | 227 | switch (callbackType) { |
| 228 | case CALLBACK_LISTEN_ALL: |
| 229 | break; |
Erik Kline | 465c46d | 2017-03-07 15:25:32 +0900 | [diff] [blame] | 230 | |
Erik Kline | 35bf06c | 2017-01-26 18:08:28 +0900 | [diff] [blame] | 231 | case CALLBACK_TRACK_DEFAULT: |
Erik Kline | 32858e1 | 2017-02-21 16:21:46 +0900 | [diff] [blame] | 232 | if (mDefaultNetworkCallback == null) { |
| 233 | // The callback was unregistered in the interval between |
Erik Kline | b583b03 | 2017-02-22 12:58:24 +0900 | [diff] [blame] | 234 | // ConnectivityService enqueueing onAvailable() and our |
| 235 | // handling of it here on the mHandler thread. |
| 236 | // |
Erik Kline | 32858e1 | 2017-02-21 16:21:46 +0900 | [diff] [blame] | 237 | // Clean-up of this network entry is deferred to the |
| 238 | // handling of onLost() by other callbacks. |
Erik Kline | b583b03 | 2017-02-22 12:58:24 +0900 | [diff] [blame] | 239 | // |
| 240 | // These request*() calls can be deleted post oag/339444. |
Erik Kline | 32858e1 | 2017-02-21 16:21:46 +0900 | [diff] [blame] | 241 | return; |
| 242 | } |
Erik Kline | 35bf06c | 2017-01-26 18:08:28 +0900 | [diff] [blame] | 243 | mCurrentDefault = network; |
| 244 | break; |
Erik Kline | 465c46d | 2017-03-07 15:25:32 +0900 | [diff] [blame] | 245 | |
Erik Kline | 35bf06c | 2017-01-26 18:08:28 +0900 | [diff] [blame] | 246 | case CALLBACK_MOBILE_REQUEST: |
Erik Kline | 32858e1 | 2017-02-21 16:21:46 +0900 | [diff] [blame] | 247 | if (mMobileNetworkCallback == null) { |
| 248 | // The callback was unregistered in the interval between |
Erik Kline | b583b03 | 2017-02-22 12:58:24 +0900 | [diff] [blame] | 249 | // ConnectivityService enqueueing onAvailable() and our |
| 250 | // handling of it here on the mHandler thread. |
| 251 | // |
Erik Kline | 32858e1 | 2017-02-21 16:21:46 +0900 | [diff] [blame] | 252 | // Clean-up of this network entry is deferred to the |
| 253 | // handling of onLost() by other callbacks. |
Erik Kline | 32858e1 | 2017-02-21 16:21:46 +0900 | [diff] [blame] | 254 | return; |
| 255 | } |
Erik Kline | 35bf06c | 2017-01-26 18:08:28 +0900 | [diff] [blame] | 256 | break; |
Erik Kline | d2ec391 | 2017-01-25 00:53:04 +0900 | [diff] [blame] | 257 | } |
Erik Kline | 9bba340 | 2017-01-13 16:46:52 +0900 | [diff] [blame] | 258 | |
Erik Kline | d2ec391 | 2017-01-25 00:53:04 +0900 | [diff] [blame] | 259 | // Requesting updates for mListenAllCallback is not currently possible |
| 260 | // because it's a "listen". Two possible solutions to getting updates |
| 261 | // about networks without waiting for a change (which might never come) |
| 262 | // are: |
| 263 | // |
| 264 | // [1] extend request{NetworkCapabilities,LinkProperties}() to |
| 265 | // take a Network argument and have ConnectivityService do |
| 266 | // what's required (if the network satisfies the request) |
| 267 | // |
| 268 | // [2] explicitly file a NetworkRequest for each connectivity type |
| 269 | // listed as a preferred upstream and wait for these callbacks |
| 270 | // to be notified (requires tracking many more callbacks). |
| 271 | // |
| 272 | // Until this is addressed, networks that exist prior to the "listen" |
| 273 | // registration and which do not subsequently change will not cause |
| 274 | // us to learn their NetworkCapabilities nor their LinkProperties. |
| 275 | |
| 276 | // TODO: If sufficient information is available to select a more |
| 277 | // preferable upstream, do so now and notify the target. |
Erik Kline | 9bba340 | 2017-01-13 16:46:52 +0900 | [diff] [blame] | 278 | notifyTarget(EVENT_ON_AVAILABLE, network); |
| 279 | } |
| 280 | |
| 281 | private void handleNetCap(Network network, NetworkCapabilities newNc) { |
Erik Kline | d2ec391 | 2017-01-25 00:53:04 +0900 | [diff] [blame] | 282 | final NetworkState prev = mNetworkMap.get(network); |
| 283 | if (prev == null || newNc.equals(prev.networkCapabilities)) { |
| 284 | // Ignore notifications about networks for which we have not yet |
| 285 | // received onAvailable() (should never happen) and any duplicate |
| 286 | // notifications (e.g. matching more than one of our callbacks). |
Erik Kline | 9bba340 | 2017-01-13 16:46:52 +0900 | [diff] [blame] | 287 | return; |
| 288 | } |
Erik Kline | d2ec391 | 2017-01-25 00:53:04 +0900 | [diff] [blame] | 289 | |
Erik Kline | 9bba340 | 2017-01-13 16:46:52 +0900 | [diff] [blame] | 290 | if (VDBG) { |
| 291 | Log.d(TAG, String.format("EVENT_ON_CAPABILITIES for %s: %s", |
| 292 | network, newNc)); |
| 293 | } |
| 294 | |
Erik Kline | d2ec391 | 2017-01-25 00:53:04 +0900 | [diff] [blame] | 295 | mNetworkMap.put(network, new NetworkState( |
| 296 | null, prev.linkProperties, newNc, network, null, null)); |
| 297 | // TODO: If sufficient information is available to select a more |
| 298 | // preferable upstream, do so now and notify the target. |
Erik Kline | 9bba340 | 2017-01-13 16:46:52 +0900 | [diff] [blame] | 299 | notifyTarget(EVENT_ON_CAPABILITIES, network); |
| 300 | } |
| 301 | |
| 302 | private void handleLinkProp(Network network, LinkProperties newLp) { |
Erik Kline | d2ec391 | 2017-01-25 00:53:04 +0900 | [diff] [blame] | 303 | final NetworkState prev = mNetworkMap.get(network); |
| 304 | if (prev == null || newLp.equals(prev.linkProperties)) { |
| 305 | // Ignore notifications about networks for which we have not yet |
| 306 | // received onAvailable() (should never happen) and any duplicate |
| 307 | // notifications (e.g. matching more than one of our callbacks). |
Erik Kline | 9bba340 | 2017-01-13 16:46:52 +0900 | [diff] [blame] | 308 | return; |
| 309 | } |
Erik Kline | d2ec391 | 2017-01-25 00:53:04 +0900 | [diff] [blame] | 310 | |
Erik Kline | 9bba340 | 2017-01-13 16:46:52 +0900 | [diff] [blame] | 311 | if (VDBG) { |
| 312 | Log.d(TAG, String.format("EVENT_ON_LINKPROPERTIES for %s: %s", |
| 313 | network, newLp)); |
| 314 | } |
| 315 | |
Erik Kline | d2ec391 | 2017-01-25 00:53:04 +0900 | [diff] [blame] | 316 | mNetworkMap.put(network, new NetworkState( |
| 317 | null, newLp, prev.networkCapabilities, network, null, null)); |
| 318 | // TODO: If sufficient information is available to select a more |
| 319 | // preferable upstream, do so now and notify the target. |
Erik Kline | 9bba340 | 2017-01-13 16:46:52 +0900 | [diff] [blame] | 320 | notifyTarget(EVENT_ON_LINKPROPERTIES, network); |
| 321 | } |
| 322 | |
Erik Kline | d2ec391 | 2017-01-25 00:53:04 +0900 | [diff] [blame] | 323 | private void handleLost(int callbackType, Network network) { |
Erik Kline | 35bf06c | 2017-01-26 18:08:28 +0900 | [diff] [blame] | 324 | if (callbackType == CALLBACK_TRACK_DEFAULT) { |
Erik Kline | d2ec391 | 2017-01-25 00:53:04 +0900 | [diff] [blame] | 325 | mCurrentDefault = null; |
| 326 | // Receiving onLost() for a default network does not necessarily |
| 327 | // mean the network is gone. We wait for a separate notification |
| 328 | // on either the LISTEN_ALL or MOBILE_REQUEST callbacks before |
| 329 | // clearing all state. |
Erik Kline | 9bba340 | 2017-01-13 16:46:52 +0900 | [diff] [blame] | 330 | return; |
| 331 | } |
Erik Kline | d2ec391 | 2017-01-25 00:53:04 +0900 | [diff] [blame] | 332 | |
| 333 | if (!mNetworkMap.containsKey(network)) { |
| 334 | // Ignore loss of networks about which we had not previously |
| 335 | // learned any information or for which we have already processed |
| 336 | // an onLost() notification. |
| 337 | return; |
Erik Kline | 9bba340 | 2017-01-13 16:46:52 +0900 | [diff] [blame] | 338 | } |
Erik Kline | d2ec391 | 2017-01-25 00:53:04 +0900 | [diff] [blame] | 339 | |
| 340 | if (VDBG) Log.d(TAG, "EVENT_ON_LOST for " + network); |
| 341 | |
| 342 | // TODO: If sufficient information is available to select a more |
| 343 | // preferable upstream, do so now and notify the target. Likewise, |
| 344 | // if the current upstream network is gone, notify the target of the |
| 345 | // fact that we now have no upstream at all. |
Erik Kline | 9bba340 | 2017-01-13 16:46:52 +0900 | [diff] [blame] | 346 | notifyTarget(EVENT_ON_LOST, mNetworkMap.remove(network)); |
| 347 | } |
| 348 | |
| 349 | // Fetch (and cache) a ConnectivityManager only if and when we need one. |
| 350 | private ConnectivityManager cm() { |
| 351 | if (mCM == null) { |
Erik Kline | ea9cc48 | 2017-03-10 19:35:34 +0900 | [diff] [blame] | 352 | // MUST call the String variant to be able to write unittests. |
| 353 | mCM = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE); |
Erik Kline | 9bba340 | 2017-01-13 16:46:52 +0900 | [diff] [blame] | 354 | } |
| 355 | return mCM; |
| 356 | } |
| 357 | |
| 358 | /** |
Erik Kline | b583b03 | 2017-02-22 12:58:24 +0900 | [diff] [blame] | 359 | * A NetworkCallback class that handles information of interest directly |
| 360 | * in the thread on which it is invoked. To avoid locking, this MUST be |
| 361 | * run on the same thread as the target state machine's handler. |
Erik Kline | 9bba340 | 2017-01-13 16:46:52 +0900 | [diff] [blame] | 362 | */ |
| 363 | private class UpstreamNetworkCallback extends NetworkCallback { |
Erik Kline | d2ec391 | 2017-01-25 00:53:04 +0900 | [diff] [blame] | 364 | private final int mCallbackType; |
| 365 | |
| 366 | UpstreamNetworkCallback(int callbackType) { |
| 367 | mCallbackType = callbackType; |
| 368 | } |
| 369 | |
Erik Kline | 9bba340 | 2017-01-13 16:46:52 +0900 | [diff] [blame] | 370 | @Override |
| 371 | public void onAvailable(Network network) { |
Erik Kline | b583b03 | 2017-02-22 12:58:24 +0900 | [diff] [blame] | 372 | handleAvailable(mCallbackType, network); |
Erik Kline | 9bba340 | 2017-01-13 16:46:52 +0900 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | @Override |
| 376 | public void onCapabilitiesChanged(Network network, NetworkCapabilities newNc) { |
Erik Kline | b583b03 | 2017-02-22 12:58:24 +0900 | [diff] [blame] | 377 | handleNetCap(network, newNc); |
Erik Kline | 9bba340 | 2017-01-13 16:46:52 +0900 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | @Override |
| 381 | public void onLinkPropertiesChanged(Network network, LinkProperties newLp) { |
Erik Kline | b583b03 | 2017-02-22 12:58:24 +0900 | [diff] [blame] | 382 | handleLinkProp(network, newLp); |
Erik Kline | 9bba340 | 2017-01-13 16:46:52 +0900 | [diff] [blame] | 383 | } |
| 384 | |
Erik Kline | b583b03 | 2017-02-22 12:58:24 +0900 | [diff] [blame] | 385 | // TODO: Handle onNetworkSuspended(); |
| 386 | // TODO: Handle onNetworkResumed(); |
| 387 | |
Erik Kline | 9bba340 | 2017-01-13 16:46:52 +0900 | [diff] [blame] | 388 | @Override |
| 389 | public void onLost(Network network) { |
Erik Kline | b583b03 | 2017-02-22 12:58:24 +0900 | [diff] [blame] | 390 | handleLost(mCallbackType, network); |
| 391 | } |
Erik Kline | 9bba340 | 2017-01-13 16:46:52 +0900 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | private void releaseCallback(NetworkCallback cb) { |
| 395 | if (cb != null) cm().unregisterNetworkCallback(cb); |
| 396 | } |
| 397 | |
| 398 | private void notifyTarget(int which, Network network) { |
| 399 | notifyTarget(which, mNetworkMap.get(network)); |
| 400 | } |
| 401 | |
| 402 | private void notifyTarget(int which, NetworkState netstate) { |
| 403 | mTarget.sendMessage(mWhat, which, 0, netstate); |
| 404 | } |
Erik Kline | 3760999 | 2017-06-05 16:02:02 +0900 | [diff] [blame] | 405 | |
| 406 | static private class TypeStatePair { |
| 407 | public int type = TYPE_NONE; |
| 408 | public NetworkState ns = null; |
| 409 | } |
| 410 | |
| 411 | static private TypeStatePair findFirstAvailableUpstreamByType( |
| 412 | Iterable<NetworkState> netStates, Iterable<Integer> preferredTypes) { |
| 413 | final TypeStatePair result = new TypeStatePair(); |
| 414 | |
| 415 | for (int type : preferredTypes) { |
| 416 | NetworkCapabilities nc; |
| 417 | try { |
| 418 | nc = ConnectivityManager.networkCapabilitiesForType(type); |
| 419 | } catch (IllegalArgumentException iae) { |
| 420 | Log.e(TAG, "No NetworkCapabilities mapping for legacy type: " + |
| 421 | ConnectivityManager.getNetworkTypeName(type)); |
| 422 | continue; |
| 423 | } |
| 424 | |
| 425 | for (NetworkState value : netStates) { |
| 426 | if (!nc.satisfiedByNetworkCapabilities(value.networkCapabilities)) { |
| 427 | continue; |
| 428 | } |
| 429 | |
| 430 | result.type = type; |
| 431 | result.ns = value; |
| 432 | return result; |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | return result; |
| 437 | } |
Erik Kline | 9bba340 | 2017-01-13 16:46:52 +0900 | [diff] [blame] | 438 | } |