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