markchien | b6eb2c2 | 2018-07-18 14:29:20 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 | |
| 19 | import static android.net.ConnectivityManager.EXTRA_ADD_TETHER_TYPE; |
| 20 | import static android.net.ConnectivityManager.EXTRA_PROVISION_CALLBACK; |
markchien | b6eb2c2 | 2018-07-18 14:29:20 +0800 | [diff] [blame] | 21 | import static android.net.ConnectivityManager.EXTRA_RUN_PROVISION; |
markchien | 3b51963 | 2018-09-07 16:19:12 +0800 | [diff] [blame] | 22 | import static android.net.ConnectivityManager.TETHERING_BLUETOOTH; |
| 23 | import static android.net.ConnectivityManager.TETHERING_INVALID; |
| 24 | import static android.net.ConnectivityManager.TETHERING_USB; |
| 25 | import static android.net.ConnectivityManager.TETHERING_WIFI; |
markchien | f273127 | 2019-01-16 17:44:13 +0800 | [diff] [blame] | 26 | import static android.net.ConnectivityManager.TETHER_ERROR_ENTITLEMENT_UNKONWN; |
| 27 | import static android.net.ConnectivityManager.TETHER_ERROR_NO_ERROR; |
| 28 | import static android.net.ConnectivityManager.TETHER_ERROR_PROVISION_FAILED; |
markchien | b6eb2c2 | 2018-07-18 14:29:20 +0800 | [diff] [blame] | 29 | |
| 30 | import static com.android.internal.R.string.config_wifi_tether_enable; |
| 31 | |
markchien | 3b51963 | 2018-09-07 16:19:12 +0800 | [diff] [blame] | 32 | import android.app.AlarmManager; |
| 33 | import android.app.PendingIntent; |
| 34 | import android.content.BroadcastReceiver; |
markchien | b6eb2c2 | 2018-07-18 14:29:20 +0800 | [diff] [blame] | 35 | import android.content.ComponentName; |
| 36 | import android.content.Context; |
| 37 | import android.content.Intent; |
markchien | 3b51963 | 2018-09-07 16:19:12 +0800 | [diff] [blame] | 38 | import android.content.IntentFilter; |
markchien | b6eb2c2 | 2018-07-18 14:29:20 +0800 | [diff] [blame] | 39 | import android.content.res.Resources; |
| 40 | import android.net.util.SharedLog; |
| 41 | import android.os.Binder; |
markchien | f273127 | 2019-01-16 17:44:13 +0800 | [diff] [blame] | 42 | import android.os.Bundle; |
| 43 | import android.os.Handler; |
markchien | 3b51963 | 2018-09-07 16:19:12 +0800 | [diff] [blame] | 44 | import android.os.Looper; |
| 45 | import android.os.Message; |
markchien | f273127 | 2019-01-16 17:44:13 +0800 | [diff] [blame] | 46 | import android.os.Parcel; |
markchien | b6eb2c2 | 2018-07-18 14:29:20 +0800 | [diff] [blame] | 47 | import android.os.PersistableBundle; |
| 48 | import android.os.ResultReceiver; |
markchien | 3b51963 | 2018-09-07 16:19:12 +0800 | [diff] [blame] | 49 | import android.os.SystemClock; |
markchien | b6eb2c2 | 2018-07-18 14:29:20 +0800 | [diff] [blame] | 50 | import android.os.UserHandle; |
| 51 | import android.provider.Settings; |
| 52 | import android.telephony.CarrierConfigManager; |
| 53 | import android.util.ArraySet; |
markchien | f273127 | 2019-01-16 17:44:13 +0800 | [diff] [blame] | 54 | import android.util.SparseIntArray; |
markchien | b6eb2c2 | 2018-07-18 14:29:20 +0800 | [diff] [blame] | 55 | |
markchien | b6eb2c2 | 2018-07-18 14:29:20 +0800 | [diff] [blame] | 56 | import com.android.internal.annotations.VisibleForTesting; |
markchien | f273127 | 2019-01-16 17:44:13 +0800 | [diff] [blame] | 57 | import com.android.internal.util.StateMachine; |
markchien | b6eb2c2 | 2018-07-18 14:29:20 +0800 | [diff] [blame] | 58 | import com.android.server.connectivity.MockableSystemProperties; |
| 59 | |
markchien | 3b51963 | 2018-09-07 16:19:12 +0800 | [diff] [blame] | 60 | import java.io.PrintWriter; |
| 61 | |
markchien | b6eb2c2 | 2018-07-18 14:29:20 +0800 | [diff] [blame] | 62 | /** |
markchien | 3b51963 | 2018-09-07 16:19:12 +0800 | [diff] [blame] | 63 | * Re-check tethering provisioning for enabled downstream tether types. |
| 64 | * Reference ConnectivityManager.TETHERING_{@code *} for each tether type. |
markchien | b6eb2c2 | 2018-07-18 14:29:20 +0800 | [diff] [blame] | 65 | * |
markchien | 3b51963 | 2018-09-07 16:19:12 +0800 | [diff] [blame] | 66 | * All methods of this class must be accessed from the thread of tethering |
| 67 | * state machine. |
markchien | b6eb2c2 | 2018-07-18 14:29:20 +0800 | [diff] [blame] | 68 | * @hide |
| 69 | */ |
| 70 | public class EntitlementManager { |
| 71 | private static final String TAG = EntitlementManager.class.getSimpleName(); |
markchien | f273127 | 2019-01-16 17:44:13 +0800 | [diff] [blame] | 72 | private static final boolean DBG = false; |
markchien | b6eb2c2 | 2018-07-18 14:29:20 +0800 | [diff] [blame] | 73 | |
markchien | 3b51963 | 2018-09-07 16:19:12 +0800 | [diff] [blame] | 74 | @VisibleForTesting |
| 75 | protected static final String DISABLE_PROVISIONING_SYSPROP_KEY = "net.tethering.noprovisioning"; |
| 76 | private static final String ACTION_PROVISIONING_ALARM = |
| 77 | "com.android.server.connectivity.tethering.PROVISIONING_RECHECK_ALARM"; |
markchien | 3394e14 | 2019-04-09 15:53:24 +0800 | [diff] [blame] | 78 | private static final String EXTRA_SUBID = "subId"; |
markchien | 3b51963 | 2018-09-07 16:19:12 +0800 | [diff] [blame] | 79 | |
markchien | b6eb2c2 | 2018-07-18 14:29:20 +0800 | [diff] [blame] | 80 | // {@link ComponentName} of the Service used to run tether provisioning. |
| 81 | private static final ComponentName TETHER_SERVICE = ComponentName.unflattenFromString( |
| 82 | Resources.getSystem().getString(config_wifi_tether_enable)); |
markchien | 3b51963 | 2018-09-07 16:19:12 +0800 | [diff] [blame] | 83 | private static final int MS_PER_HOUR = 60 * 60 * 1000; |
| 84 | private static final int EVENT_START_PROVISIONING = 0; |
| 85 | private static final int EVENT_STOP_PROVISIONING = 1; |
| 86 | private static final int EVENT_UPSTREAM_CHANGED = 2; |
| 87 | private static final int EVENT_MAYBE_RUN_PROVISIONING = 3; |
| 88 | private static final int EVENT_GET_ENTITLEMENT_VALUE = 4; |
| 89 | |
markchien | b6eb2c2 | 2018-07-18 14:29:20 +0800 | [diff] [blame] | 90 | // The ArraySet contains enabled downstream types, ex: |
| 91 | // {@link ConnectivityManager.TETHERING_WIFI} |
| 92 | // {@link ConnectivityManager.TETHERING_USB} |
| 93 | // {@link ConnectivityManager.TETHERING_BLUETOOTH} |
markchien | b6eb2c2 | 2018-07-18 14:29:20 +0800 | [diff] [blame] | 94 | private final ArraySet<Integer> mCurrentTethers; |
| 95 | private final Context mContext; |
markchien | 3b51963 | 2018-09-07 16:19:12 +0800 | [diff] [blame] | 96 | private final int mPermissionChangeMessageCode; |
markchien | b6eb2c2 | 2018-07-18 14:29:20 +0800 | [diff] [blame] | 97 | private final MockableSystemProperties mSystemProperties; |
| 98 | private final SharedLog mLog; |
markchien | f273127 | 2019-01-16 17:44:13 +0800 | [diff] [blame] | 99 | private final SparseIntArray mEntitlementCacheValue; |
markchien | 3b51963 | 2018-09-07 16:19:12 +0800 | [diff] [blame] | 100 | private final EntitlementHandler mHandler; |
markchien | 3b51963 | 2018-09-07 16:19:12 +0800 | [diff] [blame] | 101 | private final StateMachine mTetherMasterSM; |
| 102 | // Key: ConnectivityManager.TETHERING_*(downstream). |
| 103 | // Value: ConnectivityManager.TETHER_ERROR_{NO_ERROR or PROVISION_FAILED}(provisioning result). |
| 104 | private final SparseIntArray mCellularPermitted; |
| 105 | private PendingIntent mProvisioningRecheckAlarm; |
| 106 | private boolean mCellularUpstreamPermitted = true; |
| 107 | private boolean mUsingCellularAsUpstream = false; |
| 108 | private boolean mNeedReRunProvisioningUi = false; |
markchien | 29b7014 | 2019-03-26 21:41:59 +0800 | [diff] [blame] | 109 | private OnUiEntitlementFailedListener mListener; |
markchien | 3394e14 | 2019-04-09 15:53:24 +0800 | [diff] [blame] | 110 | private TetheringConfigurationFetcher mFetcher; |
markchien | b6eb2c2 | 2018-07-18 14:29:20 +0800 | [diff] [blame] | 111 | |
markchien | f273127 | 2019-01-16 17:44:13 +0800 | [diff] [blame] | 112 | public EntitlementManager(Context ctx, StateMachine tetherMasterSM, SharedLog log, |
markchien | 3b51963 | 2018-09-07 16:19:12 +0800 | [diff] [blame] | 113 | int permissionChangeMessageCode, MockableSystemProperties systemProperties) { |
markchien | b6eb2c2 | 2018-07-18 14:29:20 +0800 | [diff] [blame] | 114 | mContext = ctx; |
markchien | f273127 | 2019-01-16 17:44:13 +0800 | [diff] [blame] | 115 | mLog = log.forSubComponent(TAG); |
markchien | b6eb2c2 | 2018-07-18 14:29:20 +0800 | [diff] [blame] | 116 | mCurrentTethers = new ArraySet<Integer>(); |
markchien | 3b51963 | 2018-09-07 16:19:12 +0800 | [diff] [blame] | 117 | mCellularPermitted = new SparseIntArray(); |
markchien | b6eb2c2 | 2018-07-18 14:29:20 +0800 | [diff] [blame] | 118 | mSystemProperties = systemProperties; |
markchien | f273127 | 2019-01-16 17:44:13 +0800 | [diff] [blame] | 119 | mEntitlementCacheValue = new SparseIntArray(); |
markchien | 3b51963 | 2018-09-07 16:19:12 +0800 | [diff] [blame] | 120 | mTetherMasterSM = tetherMasterSM; |
| 121 | mPermissionChangeMessageCode = permissionChangeMessageCode; |
| 122 | final Handler masterHandler = tetherMasterSM.getHandler(); |
| 123 | // Create entitlement's own handler which is associated with TetherMaster thread |
| 124 | // let all entitlement processes run in the same thread. |
| 125 | mHandler = new EntitlementHandler(masterHandler.getLooper()); |
| 126 | mContext.registerReceiver(mReceiver, new IntentFilter(ACTION_PROVISIONING_ALARM), |
| 127 | null, mHandler); |
markchien | b6eb2c2 | 2018-07-18 14:29:20 +0800 | [diff] [blame] | 128 | } |
| 129 | |
markchien | 29b7014 | 2019-03-26 21:41:59 +0800 | [diff] [blame] | 130 | public void setOnUiEntitlementFailedListener(final OnUiEntitlementFailedListener listener) { |
| 131 | mListener = listener; |
| 132 | } |
| 133 | |
| 134 | /** Callback fired when UI entitlement failed. */ |
| 135 | public interface OnUiEntitlementFailedListener { |
| 136 | /** |
| 137 | * Ui entitlement check fails in |downstream|. |
| 138 | * |
markchien | f303c5a | 2019-09-23 20:29:54 +0800 | [diff] [blame] | 139 | * @param downstream tethering type from ConnectivityManager.TETHERING_{@code *}. |
markchien | 29b7014 | 2019-03-26 21:41:59 +0800 | [diff] [blame] | 140 | */ |
| 141 | void onUiEntitlementFailed(int downstream); |
| 142 | } |
| 143 | |
markchien | 3394e14 | 2019-04-09 15:53:24 +0800 | [diff] [blame] | 144 | public void setTetheringConfigurationFetcher(final TetheringConfigurationFetcher fetcher) { |
| 145 | mFetcher = fetcher; |
| 146 | } |
| 147 | |
| 148 | /** Interface to fetch TetheringConfiguration. */ |
| 149 | public interface TetheringConfigurationFetcher { |
| 150 | /** |
| 151 | * Fetch current tethering configuration. This will be called to ensure whether entitlement |
| 152 | * check is needed. |
| 153 | * @return TetheringConfiguration instance. |
| 154 | */ |
| 155 | TetheringConfiguration fetchTetheringConfiguration(); |
markchien | b6eb2c2 | 2018-07-18 14:29:20 +0800 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | /** |
markchien | 3b51963 | 2018-09-07 16:19:12 +0800 | [diff] [blame] | 159 | * Check if cellular upstream is permitted. |
markchien | b6eb2c2 | 2018-07-18 14:29:20 +0800 | [diff] [blame] | 160 | */ |
markchien | 3b51963 | 2018-09-07 16:19:12 +0800 | [diff] [blame] | 161 | public boolean isCellularUpstreamPermitted() { |
markchien | 426311b | 2019-08-09 21:43:38 +0800 | [diff] [blame] | 162 | // If provisioning is required and EntitlementManager don't know any downstream, |
| 163 | // cellular upstream should not be allowed. |
| 164 | final TetheringConfiguration config = mFetcher.fetchTetheringConfiguration(); |
| 165 | if (mCurrentTethers.size() == 0 && isTetherProvisioningRequired(config)) { |
| 166 | return false; |
| 167 | } |
markchien | 3b51963 | 2018-09-07 16:19:12 +0800 | [diff] [blame] | 168 | return mCellularUpstreamPermitted; |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * This is called when tethering starts. |
| 173 | * Launch provisioning app if upstream is cellular. |
| 174 | * |
| 175 | * @param downstreamType tethering type from ConnectivityManager.TETHERING_{@code *} |
| 176 | * @param showProvisioningUi a boolean indicating whether to show the |
| 177 | * provisioning app UI if there is one. |
| 178 | */ |
| 179 | public void startProvisioningIfNeeded(int downstreamType, boolean showProvisioningUi) { |
| 180 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_START_PROVISIONING, |
| 181 | downstreamType, encodeBool(showProvisioningUi))); |
| 182 | } |
| 183 | |
| 184 | private void handleStartProvisioningIfNeeded(int type, boolean showProvisioningUi) { |
| 185 | if (!isValidDownstreamType(type)) return; |
| 186 | |
| 187 | if (!mCurrentTethers.contains(type)) mCurrentTethers.add(type); |
| 188 | |
markchien | 3394e14 | 2019-04-09 15:53:24 +0800 | [diff] [blame] | 189 | final TetheringConfiguration config = mFetcher.fetchTetheringConfiguration(); |
| 190 | if (isTetherProvisioningRequired(config)) { |
markchien | 3b51963 | 2018-09-07 16:19:12 +0800 | [diff] [blame] | 191 | // If provisioning is required and the result is not available yet, |
| 192 | // cellular upstream should not be allowed. |
| 193 | if (mCellularPermitted.size() == 0) { |
| 194 | mCellularUpstreamPermitted = false; |
| 195 | } |
| 196 | // If upstream is not cellular, provisioning app would not be launched |
| 197 | // till upstream change to cellular. |
| 198 | if (mUsingCellularAsUpstream) { |
| 199 | if (showProvisioningUi) { |
markchien | 3394e14 | 2019-04-09 15:53:24 +0800 | [diff] [blame] | 200 | runUiTetherProvisioning(type, config.subId); |
markchien | 3b51963 | 2018-09-07 16:19:12 +0800 | [diff] [blame] | 201 | } else { |
markchien | 3394e14 | 2019-04-09 15:53:24 +0800 | [diff] [blame] | 202 | runSilentTetherProvisioning(type, config.subId); |
markchien | 3b51963 | 2018-09-07 16:19:12 +0800 | [diff] [blame] | 203 | } |
| 204 | mNeedReRunProvisioningUi = false; |
| 205 | } else { |
| 206 | mNeedReRunProvisioningUi |= showProvisioningUi; |
| 207 | } |
| 208 | } else { |
| 209 | mCellularUpstreamPermitted = true; |
markchien | b6eb2c2 | 2018-07-18 14:29:20 +0800 | [diff] [blame] | 210 | } |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * Tell EntitlementManager that a given type of tethering has been disabled |
| 215 | * |
markchien | 3b51963 | 2018-09-07 16:19:12 +0800 | [diff] [blame] | 216 | * @param type tethering type from ConnectivityManager.TETHERING_{@code *} |
markchien | b6eb2c2 | 2018-07-18 14:29:20 +0800 | [diff] [blame] | 217 | */ |
markchien | 3b51963 | 2018-09-07 16:19:12 +0800 | [diff] [blame] | 218 | public void stopProvisioningIfNeeded(int type) { |
| 219 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_STOP_PROVISIONING, type, 0)); |
| 220 | } |
| 221 | |
| 222 | private void handleStopProvisioningIfNeeded(int type) { |
| 223 | if (!isValidDownstreamType(type)) return; |
| 224 | |
| 225 | mCurrentTethers.remove(type); |
| 226 | // There are lurking bugs where the notion of "provisioning required" or |
| 227 | // "tethering supported" may change without without tethering being notified properly. |
| 228 | // Remove the mapping all the time no matter provisioning is required or not. |
| 229 | removeDownstreamMapping(type); |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * Notify EntitlementManager if upstream is cellular or not. |
| 234 | * |
| 235 | * @param isCellular whether tethering upstream is cellular. |
| 236 | */ |
| 237 | public void notifyUpstream(boolean isCellular) { |
| 238 | mHandler.sendMessage(mHandler.obtainMessage( |
| 239 | EVENT_UPSTREAM_CHANGED, encodeBool(isCellular), 0)); |
| 240 | } |
| 241 | |
| 242 | private void handleNotifyUpstream(boolean isCellular) { |
| 243 | if (DBG) { |
markchien | 9f246bd | 2019-04-03 10:43:09 +0800 | [diff] [blame] | 244 | mLog.i("notifyUpstream: " + isCellular |
markchien | 3b51963 | 2018-09-07 16:19:12 +0800 | [diff] [blame] | 245 | + ", mCellularUpstreamPermitted: " + mCellularUpstreamPermitted |
| 246 | + ", mNeedReRunProvisioningUi: " + mNeedReRunProvisioningUi); |
| 247 | } |
| 248 | mUsingCellularAsUpstream = isCellular; |
| 249 | |
| 250 | if (mUsingCellularAsUpstream) { |
markchien | 3394e14 | 2019-04-09 15:53:24 +0800 | [diff] [blame] | 251 | final TetheringConfiguration config = mFetcher.fetchTetheringConfiguration(); |
| 252 | handleMaybeRunProvisioning(config); |
markchien | 3b51963 | 2018-09-07 16:19:12 +0800 | [diff] [blame] | 253 | } |
| 254 | } |
| 255 | |
| 256 | /** Run provisioning if needed */ |
| 257 | public void maybeRunProvisioning() { |
| 258 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_MAYBE_RUN_PROVISIONING)); |
| 259 | } |
| 260 | |
markchien | 3394e14 | 2019-04-09 15:53:24 +0800 | [diff] [blame] | 261 | private void handleMaybeRunProvisioning(final TetheringConfiguration config) { |
| 262 | if (mCurrentTethers.size() == 0 || !isTetherProvisioningRequired(config)) { |
markchien | 3b51963 | 2018-09-07 16:19:12 +0800 | [diff] [blame] | 263 | return; |
| 264 | } |
| 265 | |
| 266 | // Whenever any entitlement value changes, all downstreams will re-evaluate whether they |
| 267 | // are allowed. Therefore even if the silent check here ends in a failure and the UI later |
| 268 | // yields success, then the downstream that got a failure will re-evaluate as a result of |
| 269 | // the change and get the new correct value. |
| 270 | for (Integer downstream : mCurrentTethers) { |
| 271 | if (mCellularPermitted.indexOfKey(downstream) < 0) { |
| 272 | if (mNeedReRunProvisioningUi) { |
| 273 | mNeedReRunProvisioningUi = false; |
markchien | 3394e14 | 2019-04-09 15:53:24 +0800 | [diff] [blame] | 274 | runUiTetherProvisioning(downstream, config.subId); |
markchien | 3b51963 | 2018-09-07 16:19:12 +0800 | [diff] [blame] | 275 | } else { |
markchien | 3394e14 | 2019-04-09 15:53:24 +0800 | [diff] [blame] | 276 | runSilentTetherProvisioning(downstream, config.subId); |
markchien | 3b51963 | 2018-09-07 16:19:12 +0800 | [diff] [blame] | 277 | } |
| 278 | } |
markchien | b6eb2c2 | 2018-07-18 14:29:20 +0800 | [diff] [blame] | 279 | } |
| 280 | } |
| 281 | |
| 282 | /** |
| 283 | * Check if the device requires a provisioning check in order to enable tethering. |
| 284 | * |
markchien | 3394e14 | 2019-04-09 15:53:24 +0800 | [diff] [blame] | 285 | * @param config an object that encapsulates the various tethering configuration elements. |
markchien | b6eb2c2 | 2018-07-18 14:29:20 +0800 | [diff] [blame] | 286 | * @return a boolean - {@code true} indicating tether provisioning is required by the carrier. |
| 287 | */ |
| 288 | @VisibleForTesting |
markchien | 3394e14 | 2019-04-09 15:53:24 +0800 | [diff] [blame] | 289 | protected boolean isTetherProvisioningRequired(final TetheringConfiguration config) { |
markchien | b6eb2c2 | 2018-07-18 14:29:20 +0800 | [diff] [blame] | 290 | if (mSystemProperties.getBoolean(DISABLE_PROVISIONING_SYSPROP_KEY, false) |
markchien | 3394e14 | 2019-04-09 15:53:24 +0800 | [diff] [blame] | 291 | || config.provisioningApp.length == 0) { |
markchien | b6eb2c2 | 2018-07-18 14:29:20 +0800 | [diff] [blame] | 292 | return false; |
| 293 | } |
markchien | 3394e14 | 2019-04-09 15:53:24 +0800 | [diff] [blame] | 294 | if (carrierConfigAffirmsEntitlementCheckNotRequired(config)) { |
markchien | b6eb2c2 | 2018-07-18 14:29:20 +0800 | [diff] [blame] | 295 | return false; |
| 296 | } |
markchien | 3394e14 | 2019-04-09 15:53:24 +0800 | [diff] [blame] | 297 | return (config.provisioningApp.length == 2); |
markchien | b6eb2c2 | 2018-07-18 14:29:20 +0800 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | /** |
markchien | 3b51963 | 2018-09-07 16:19:12 +0800 | [diff] [blame] | 301 | * Re-check tethering provisioning for all enabled tether types. |
markchien | b6eb2c2 | 2018-07-18 14:29:20 +0800 | [diff] [blame] | 302 | * Reference ConnectivityManager.TETHERING_{@code *} for each tether type. |
markchien | 3b51963 | 2018-09-07 16:19:12 +0800 | [diff] [blame] | 303 | * |
markchien | 3394e14 | 2019-04-09 15:53:24 +0800 | [diff] [blame] | 304 | * @param config an object that encapsulates the various tethering configuration elements. |
markchien | 3b51963 | 2018-09-07 16:19:12 +0800 | [diff] [blame] | 305 | * Note: this method is only called from TetherMaster on the handler thread. |
| 306 | * If there are new callers from different threads, the logic should move to |
| 307 | * masterHandler to avoid race conditions. |
markchien | b6eb2c2 | 2018-07-18 14:29:20 +0800 | [diff] [blame] | 308 | */ |
markchien | 3394e14 | 2019-04-09 15:53:24 +0800 | [diff] [blame] | 309 | public void reevaluateSimCardProvisioning(final TetheringConfiguration config) { |
markchien | 9f246bd | 2019-04-03 10:43:09 +0800 | [diff] [blame] | 310 | if (DBG) mLog.i("reevaluateSimCardProvisioning"); |
markchien | 3b51963 | 2018-09-07 16:19:12 +0800 | [diff] [blame] | 311 | |
| 312 | if (!mHandler.getLooper().isCurrentThread()) { |
| 313 | // Except for test, this log should not appear in normal flow. |
| 314 | mLog.log("reevaluateSimCardProvisioning() don't run in TetherMaster thread"); |
| 315 | } |
| 316 | mEntitlementCacheValue.clear(); |
| 317 | mCellularPermitted.clear(); |
| 318 | |
| 319 | // TODO: refine provisioning check to isTetherProvisioningRequired() ?? |
markchien | 3394e14 | 2019-04-09 15:53:24 +0800 | [diff] [blame] | 320 | if (!config.hasMobileHotspotProvisionApp() |
| 321 | || carrierConfigAffirmsEntitlementCheckNotRequired(config)) { |
| 322 | evaluateCellularPermission(config); |
markchien | 3b51963 | 2018-09-07 16:19:12 +0800 | [diff] [blame] | 323 | return; |
markchien | f273127 | 2019-01-16 17:44:13 +0800 | [diff] [blame] | 324 | } |
| 325 | |
markchien | 3b51963 | 2018-09-07 16:19:12 +0800 | [diff] [blame] | 326 | if (mUsingCellularAsUpstream) { |
markchien | 3394e14 | 2019-04-09 15:53:24 +0800 | [diff] [blame] | 327 | handleMaybeRunProvisioning(config); |
markchien | b6eb2c2 | 2018-07-18 14:29:20 +0800 | [diff] [blame] | 328 | } |
| 329 | } |
| 330 | |
markchien | 3394e14 | 2019-04-09 15:53:24 +0800 | [diff] [blame] | 331 | /** |
| 332 | * Get carrier configuration bundle. |
| 333 | * @param config an object that encapsulates the various tethering configuration elements. |
| 334 | * */ |
| 335 | public PersistableBundle getCarrierConfig(final TetheringConfiguration config) { |
markchien | 29a650c | 2019-03-19 20:57:04 +0800 | [diff] [blame] | 336 | final CarrierConfigManager configManager = (CarrierConfigManager) mContext |
| 337 | .getSystemService(Context.CARRIER_CONFIG_SERVICE); |
| 338 | if (configManager == null) return null; |
| 339 | |
markchien | 3394e14 | 2019-04-09 15:53:24 +0800 | [diff] [blame] | 340 | final PersistableBundle carrierConfig = configManager.getConfigForSubId(config.subId); |
markchien | 29a650c | 2019-03-19 20:57:04 +0800 | [diff] [blame] | 341 | |
| 342 | if (CarrierConfigManager.isConfigForIdentifiedCarrier(carrierConfig)) { |
| 343 | return carrierConfig; |
| 344 | } |
| 345 | |
| 346 | return null; |
| 347 | } |
| 348 | |
markchien | b6eb2c2 | 2018-07-18 14:29:20 +0800 | [diff] [blame] | 349 | // The logic here is aimed solely at confirming that a CarrierConfig exists |
| 350 | // and affirms that entitlement checks are not required. |
| 351 | // |
| 352 | // TODO: find a better way to express this, or alter the checking process |
| 353 | // entirely so that this is more intuitive. |
markchien | 3394e14 | 2019-04-09 15:53:24 +0800 | [diff] [blame] | 354 | private boolean carrierConfigAffirmsEntitlementCheckNotRequired( |
| 355 | final TetheringConfiguration config) { |
markchien | b6eb2c2 | 2018-07-18 14:29:20 +0800 | [diff] [blame] | 356 | // Check carrier config for entitlement checks |
markchien | 3394e14 | 2019-04-09 15:53:24 +0800 | [diff] [blame] | 357 | final PersistableBundle carrierConfig = getCarrierConfig(config); |
markchien | b6eb2c2 | 2018-07-18 14:29:20 +0800 | [diff] [blame] | 358 | if (carrierConfig == null) return false; |
| 359 | |
| 360 | // A CarrierConfigManager was found and it has a config. |
| 361 | final boolean isEntitlementCheckRequired = carrierConfig.getBoolean( |
| 362 | CarrierConfigManager.KEY_REQUIRE_ENTITLEMENT_CHECKS_BOOL); |
| 363 | return !isEntitlementCheckRequired; |
| 364 | } |
| 365 | |
markchien | 3b51963 | 2018-09-07 16:19:12 +0800 | [diff] [blame] | 366 | /** |
| 367 | * Run no UI tethering provisioning check. |
| 368 | * @param type tethering type from ConnectivityManager.TETHERING_{@code *} |
markchien | 3394e14 | 2019-04-09 15:53:24 +0800 | [diff] [blame] | 369 | * @param subId default data subscription ID. |
markchien | 3b51963 | 2018-09-07 16:19:12 +0800 | [diff] [blame] | 370 | */ |
markchien | 3394e14 | 2019-04-09 15:53:24 +0800 | [diff] [blame] | 371 | @VisibleForTesting |
| 372 | protected void runSilentTetherProvisioning(int type, int subId) { |
markchien | 9f246bd | 2019-04-03 10:43:09 +0800 | [diff] [blame] | 373 | if (DBG) mLog.i("runSilentTetherProvisioning: " + type); |
markchien | 29b7014 | 2019-03-26 21:41:59 +0800 | [diff] [blame] | 374 | // For silent provisioning, settings would stop tethering when entitlement fail. |
markchien | 3394e14 | 2019-04-09 15:53:24 +0800 | [diff] [blame] | 375 | ResultReceiver receiver = buildProxyReceiver(type, false/* notifyFail */, null); |
markchien | 3b51963 | 2018-09-07 16:19:12 +0800 | [diff] [blame] | 376 | |
markchien | b6eb2c2 | 2018-07-18 14:29:20 +0800 | [diff] [blame] | 377 | Intent intent = new Intent(); |
| 378 | intent.putExtra(EXTRA_ADD_TETHER_TYPE, type); |
| 379 | intent.putExtra(EXTRA_RUN_PROVISION, true); |
| 380 | intent.putExtra(EXTRA_PROVISION_CALLBACK, receiver); |
markchien | 3394e14 | 2019-04-09 15:53:24 +0800 | [diff] [blame] | 381 | intent.putExtra(EXTRA_SUBID, subId); |
markchien | b6eb2c2 | 2018-07-18 14:29:20 +0800 | [diff] [blame] | 382 | intent.setComponent(TETHER_SERVICE); |
| 383 | final long ident = Binder.clearCallingIdentity(); |
| 384 | try { |
| 385 | mContext.startServiceAsUser(intent, UserHandle.CURRENT); |
| 386 | } finally { |
| 387 | Binder.restoreCallingIdentity(ident); |
| 388 | } |
| 389 | } |
| 390 | |
markchien | 3394e14 | 2019-04-09 15:53:24 +0800 | [diff] [blame] | 391 | private void runUiTetherProvisioning(int type, int subId) { |
| 392 | ResultReceiver receiver = buildProxyReceiver(type, true/* notifyFail */, null); |
| 393 | runUiTetherProvisioning(type, subId, receiver); |
| 394 | } |
| 395 | |
markchien | 3b51963 | 2018-09-07 16:19:12 +0800 | [diff] [blame] | 396 | /** |
| 397 | * Run the UI-enabled tethering provisioning check. |
| 398 | * @param type tethering type from ConnectivityManager.TETHERING_{@code *} |
markchien | 3394e14 | 2019-04-09 15:53:24 +0800 | [diff] [blame] | 399 | * @param subId default data subscription ID. |
| 400 | * @param receiver to receive entitlement check result. |
markchien | 3b51963 | 2018-09-07 16:19:12 +0800 | [diff] [blame] | 401 | */ |
| 402 | @VisibleForTesting |
markchien | 3394e14 | 2019-04-09 15:53:24 +0800 | [diff] [blame] | 403 | protected void runUiTetherProvisioning(int type, int subId, ResultReceiver receiver) { |
markchien | 9f246bd | 2019-04-03 10:43:09 +0800 | [diff] [blame] | 404 | if (DBG) mLog.i("runUiTetherProvisioning: " + type); |
markchien | 3b51963 | 2018-09-07 16:19:12 +0800 | [diff] [blame] | 405 | |
markchien | b6eb2c2 | 2018-07-18 14:29:20 +0800 | [diff] [blame] | 406 | Intent intent = new Intent(Settings.ACTION_TETHER_PROVISIONING); |
| 407 | intent.putExtra(EXTRA_ADD_TETHER_TYPE, type); |
| 408 | intent.putExtra(EXTRA_PROVISION_CALLBACK, receiver); |
markchien | 3394e14 | 2019-04-09 15:53:24 +0800 | [diff] [blame] | 409 | intent.putExtra(EXTRA_SUBID, subId); |
markchien | b6eb2c2 | 2018-07-18 14:29:20 +0800 | [diff] [blame] | 410 | intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 411 | final long ident = Binder.clearCallingIdentity(); |
| 412 | try { |
| 413 | mContext.startActivityAsUser(intent, UserHandle.CURRENT); |
| 414 | } finally { |
| 415 | Binder.restoreCallingIdentity(ident); |
| 416 | } |
| 417 | } |
| 418 | |
markchien | 3b51963 | 2018-09-07 16:19:12 +0800 | [diff] [blame] | 419 | // Not needed to check if this don't run on the handler thread because it's private. |
markchien | 3394e14 | 2019-04-09 15:53:24 +0800 | [diff] [blame] | 420 | private void scheduleProvisioningRechecks(final TetheringConfiguration config) { |
markchien | 3b51963 | 2018-09-07 16:19:12 +0800 | [diff] [blame] | 421 | if (mProvisioningRecheckAlarm == null) { |
markchien | 3394e14 | 2019-04-09 15:53:24 +0800 | [diff] [blame] | 422 | final int period = config.provisioningCheckPeriod; |
markchien | 3b51963 | 2018-09-07 16:19:12 +0800 | [diff] [blame] | 423 | if (period <= 0) return; |
markchien | b6eb2c2 | 2018-07-18 14:29:20 +0800 | [diff] [blame] | 424 | |
markchien | 3b51963 | 2018-09-07 16:19:12 +0800 | [diff] [blame] | 425 | Intent intent = new Intent(ACTION_PROVISIONING_ALARM); |
| 426 | mProvisioningRecheckAlarm = PendingIntent.getBroadcast(mContext, 0, intent, 0); |
| 427 | AlarmManager alarmManager = (AlarmManager) mContext.getSystemService( |
| 428 | Context.ALARM_SERVICE); |
| 429 | long periodMs = period * MS_PER_HOUR; |
| 430 | long firstAlarmTime = SystemClock.elapsedRealtime() + periodMs; |
| 431 | alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME, firstAlarmTime, periodMs, |
| 432 | mProvisioningRecheckAlarm); |
markchien | b6eb2c2 | 2018-07-18 14:29:20 +0800 | [diff] [blame] | 433 | } |
| 434 | } |
| 435 | |
markchien | 3b51963 | 2018-09-07 16:19:12 +0800 | [diff] [blame] | 436 | private void cancelTetherProvisioningRechecks() { |
| 437 | if (mProvisioningRecheckAlarm != null) { |
| 438 | AlarmManager alarmManager = (AlarmManager) mContext.getSystemService( |
| 439 | Context.ALARM_SERVICE); |
| 440 | alarmManager.cancel(mProvisioningRecheckAlarm); |
| 441 | mProvisioningRecheckAlarm = null; |
| 442 | } |
| 443 | } |
| 444 | |
markchien | 3394e14 | 2019-04-09 15:53:24 +0800 | [diff] [blame] | 445 | private void evaluateCellularPermission(final TetheringConfiguration config) { |
markchien | 3b51963 | 2018-09-07 16:19:12 +0800 | [diff] [blame] | 446 | final boolean oldPermitted = mCellularUpstreamPermitted; |
markchien | 3394e14 | 2019-04-09 15:53:24 +0800 | [diff] [blame] | 447 | mCellularUpstreamPermitted = (!isTetherProvisioningRequired(config) |
markchien | 3b51963 | 2018-09-07 16:19:12 +0800 | [diff] [blame] | 448 | || mCellularPermitted.indexOfValue(TETHER_ERROR_NO_ERROR) > -1); |
| 449 | |
| 450 | if (DBG) { |
markchien | 9f246bd | 2019-04-03 10:43:09 +0800 | [diff] [blame] | 451 | mLog.i("Cellular permission change from " + oldPermitted |
markchien | 3b51963 | 2018-09-07 16:19:12 +0800 | [diff] [blame] | 452 | + " to " + mCellularUpstreamPermitted); |
| 453 | } |
| 454 | |
| 455 | if (mCellularUpstreamPermitted != oldPermitted) { |
| 456 | mLog.log("Cellular permission change: " + mCellularUpstreamPermitted); |
| 457 | mTetherMasterSM.sendMessage(mPermissionChangeMessageCode); |
| 458 | } |
| 459 | // Only schedule periodic re-check when tether is provisioned |
| 460 | // and the result is ok. |
| 461 | if (mCellularUpstreamPermitted && mCellularPermitted.size() > 0) { |
markchien | 3394e14 | 2019-04-09 15:53:24 +0800 | [diff] [blame] | 462 | scheduleProvisioningRechecks(config); |
markchien | 3b51963 | 2018-09-07 16:19:12 +0800 | [diff] [blame] | 463 | } else { |
| 464 | cancelTetherProvisioningRechecks(); |
| 465 | } |
| 466 | } |
| 467 | |
| 468 | /** |
| 469 | * Add the mapping between provisioning result and tethering type. |
| 470 | * Notify UpstreamNetworkMonitor if Cellular permission changes. |
| 471 | * |
| 472 | * @param type tethering type from ConnectivityManager.TETHERING_{@code *} |
| 473 | * @param resultCode Provisioning result |
| 474 | */ |
| 475 | protected void addDownstreamMapping(int type, int resultCode) { |
markchien | 9f246bd | 2019-04-03 10:43:09 +0800 | [diff] [blame] | 476 | mLog.i("addDownstreamMapping: " + type + ", result: " + resultCode |
| 477 | + " ,TetherTypeRequested: " + mCurrentTethers.contains(type)); |
markchien | 3b51963 | 2018-09-07 16:19:12 +0800 | [diff] [blame] | 478 | if (!mCurrentTethers.contains(type)) return; |
| 479 | |
| 480 | mCellularPermitted.put(type, resultCode); |
markchien | 3394e14 | 2019-04-09 15:53:24 +0800 | [diff] [blame] | 481 | final TetheringConfiguration config = mFetcher.fetchTetheringConfiguration(); |
| 482 | evaluateCellularPermission(config); |
markchien | 3b51963 | 2018-09-07 16:19:12 +0800 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | /** |
| 486 | * Remove the mapping for input tethering type. |
| 487 | * @param type tethering type from ConnectivityManager.TETHERING_{@code *} |
| 488 | */ |
| 489 | protected void removeDownstreamMapping(int type) { |
markchien | 9f246bd | 2019-04-03 10:43:09 +0800 | [diff] [blame] | 490 | mLog.i("removeDownstreamMapping: " + type); |
markchien | 3b51963 | 2018-09-07 16:19:12 +0800 | [diff] [blame] | 491 | mCellularPermitted.delete(type); |
markchien | 3394e14 | 2019-04-09 15:53:24 +0800 | [diff] [blame] | 492 | final TetheringConfiguration config = mFetcher.fetchTetheringConfiguration(); |
| 493 | evaluateCellularPermission(config); |
markchien | 3b51963 | 2018-09-07 16:19:12 +0800 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | private final BroadcastReceiver mReceiver = new BroadcastReceiver() { |
| 497 | @Override |
| 498 | public void onReceive(Context context, Intent intent) { |
| 499 | if (ACTION_PROVISIONING_ALARM.equals(intent.getAction())) { |
| 500 | mLog.log("Received provisioning alarm"); |
markchien | 3394e14 | 2019-04-09 15:53:24 +0800 | [diff] [blame] | 501 | final TetheringConfiguration config = mFetcher.fetchTetheringConfiguration(); |
| 502 | reevaluateSimCardProvisioning(config); |
markchien | 3b51963 | 2018-09-07 16:19:12 +0800 | [diff] [blame] | 503 | } |
| 504 | } |
| 505 | }; |
| 506 | |
| 507 | private class EntitlementHandler extends Handler { |
| 508 | EntitlementHandler(Looper looper) { |
| 509 | super(looper); |
| 510 | } |
| 511 | |
| 512 | @Override |
| 513 | public void handleMessage(Message msg) { |
| 514 | switch (msg.what) { |
| 515 | case EVENT_START_PROVISIONING: |
| 516 | handleStartProvisioningIfNeeded(msg.arg1, toBool(msg.arg2)); |
| 517 | break; |
| 518 | case EVENT_STOP_PROVISIONING: |
| 519 | handleStopProvisioningIfNeeded(msg.arg1); |
| 520 | break; |
| 521 | case EVENT_UPSTREAM_CHANGED: |
| 522 | handleNotifyUpstream(toBool(msg.arg1)); |
| 523 | break; |
| 524 | case EVENT_MAYBE_RUN_PROVISIONING: |
markchien | 3394e14 | 2019-04-09 15:53:24 +0800 | [diff] [blame] | 525 | final TetheringConfiguration config = mFetcher.fetchTetheringConfiguration(); |
| 526 | handleMaybeRunProvisioning(config); |
markchien | 3b51963 | 2018-09-07 16:19:12 +0800 | [diff] [blame] | 527 | break; |
| 528 | case EVENT_GET_ENTITLEMENT_VALUE: |
| 529 | handleGetLatestTetheringEntitlementValue(msg.arg1, (ResultReceiver) msg.obj, |
| 530 | toBool(msg.arg2)); |
| 531 | break; |
| 532 | default: |
| 533 | mLog.log("Unknown event: " + msg.what); |
| 534 | break; |
| 535 | } |
| 536 | } |
| 537 | } |
| 538 | |
| 539 | private static boolean toBool(int encodedBoolean) { |
| 540 | return encodedBoolean != 0; |
| 541 | } |
| 542 | |
| 543 | private static int encodeBool(boolean b) { |
| 544 | return b ? 1 : 0; |
| 545 | } |
| 546 | |
| 547 | private static boolean isValidDownstreamType(int type) { |
| 548 | switch (type) { |
| 549 | case TETHERING_BLUETOOTH: |
| 550 | case TETHERING_USB: |
| 551 | case TETHERING_WIFI: |
| 552 | return true; |
| 553 | default: |
| 554 | return false; |
| 555 | } |
| 556 | } |
| 557 | |
| 558 | /** |
| 559 | * Dump the infromation of EntitlementManager. |
| 560 | * @param pw {@link PrintWriter} is used to print formatted |
| 561 | */ |
| 562 | public void dump(PrintWriter pw) { |
| 563 | pw.print("mCellularUpstreamPermitted: "); |
| 564 | pw.println(mCellularUpstreamPermitted); |
| 565 | for (Integer type : mCurrentTethers) { |
| 566 | pw.print("Type: "); |
| 567 | pw.print(typeString(type)); |
| 568 | if (mCellularPermitted.indexOfKey(type) > -1) { |
| 569 | pw.print(", Value: "); |
| 570 | pw.println(errorString(mCellularPermitted.get(type))); |
| 571 | } else { |
| 572 | pw.println(", Value: empty"); |
| 573 | } |
| 574 | } |
| 575 | } |
| 576 | |
| 577 | private static String typeString(int type) { |
| 578 | switch (type) { |
| 579 | case TETHERING_BLUETOOTH: return "TETHERING_BLUETOOTH"; |
| 580 | case TETHERING_INVALID: return "TETHERING_INVALID"; |
| 581 | case TETHERING_USB: return "TETHERING_USB"; |
| 582 | case TETHERING_WIFI: return "TETHERING_WIFI"; |
| 583 | default: |
| 584 | return String.format("TETHERING UNKNOWN TYPE (%d)", type); |
| 585 | } |
| 586 | } |
| 587 | |
| 588 | private static String errorString(int value) { |
| 589 | switch (value) { |
| 590 | case TETHER_ERROR_ENTITLEMENT_UNKONWN: return "TETHER_ERROR_ENTITLEMENT_UNKONWN"; |
| 591 | case TETHER_ERROR_NO_ERROR: return "TETHER_ERROR_NO_ERROR"; |
| 592 | case TETHER_ERROR_PROVISION_FAILED: return "TETHER_ERROR_PROVISION_FAILED"; |
| 593 | default: |
| 594 | return String.format("UNKNOWN ERROR (%d)", value); |
markchien | b6eb2c2 | 2018-07-18 14:29:20 +0800 | [diff] [blame] | 595 | } |
| 596 | } |
markchien | f273127 | 2019-01-16 17:44:13 +0800 | [diff] [blame] | 597 | |
markchien | 29b7014 | 2019-03-26 21:41:59 +0800 | [diff] [blame] | 598 | private ResultReceiver buildProxyReceiver(int type, boolean notifyFail, |
| 599 | final ResultReceiver receiver) { |
markchien | 3b51963 | 2018-09-07 16:19:12 +0800 | [diff] [blame] | 600 | ResultReceiver rr = new ResultReceiver(mHandler) { |
markchien | f273127 | 2019-01-16 17:44:13 +0800 | [diff] [blame] | 601 | @Override |
| 602 | protected void onReceiveResult(int resultCode, Bundle resultData) { |
| 603 | int updatedCacheValue = updateEntitlementCacheValue(type, resultCode); |
markchien | 3b51963 | 2018-09-07 16:19:12 +0800 | [diff] [blame] | 604 | addDownstreamMapping(type, updatedCacheValue); |
markchien | 29b7014 | 2019-03-26 21:41:59 +0800 | [diff] [blame] | 605 | if (updatedCacheValue == TETHER_ERROR_PROVISION_FAILED && notifyFail) { |
| 606 | mListener.onUiEntitlementFailed(type); |
| 607 | } |
markchien | 3b51963 | 2018-09-07 16:19:12 +0800 | [diff] [blame] | 608 | if (receiver != null) receiver.send(updatedCacheValue, null); |
markchien | f273127 | 2019-01-16 17:44:13 +0800 | [diff] [blame] | 609 | } |
| 610 | }; |
| 611 | |
| 612 | return writeToParcel(rr); |
| 613 | } |
| 614 | |
markchien | 3b51963 | 2018-09-07 16:19:12 +0800 | [diff] [blame] | 615 | // Instances of ResultReceiver need to be public classes for remote processes to be able |
| 616 | // to load them (otherwise, ClassNotFoundException). For private classes, this method |
| 617 | // performs a trick : round-trip parceling any instance of ResultReceiver will return a |
| 618 | // vanilla instance of ResultReceiver sharing the binder token with the original receiver. |
| 619 | // The binder token has a reference to the original instance of the private class and will |
| 620 | // still call its methods, and can be sent over. However it cannot be used for anything |
| 621 | // else than sending over a Binder call. |
| 622 | // While round-trip parceling is not great, there is currently no other way of generating |
| 623 | // a vanilla instance of ResultReceiver because all its fields are private. |
markchien | f273127 | 2019-01-16 17:44:13 +0800 | [diff] [blame] | 624 | private ResultReceiver writeToParcel(final ResultReceiver receiver) { |
markchien | f273127 | 2019-01-16 17:44:13 +0800 | [diff] [blame] | 625 | Parcel parcel = Parcel.obtain(); |
| 626 | receiver.writeToParcel(parcel, 0); |
| 627 | parcel.setDataPosition(0); |
| 628 | ResultReceiver receiverForSending = ResultReceiver.CREATOR.createFromParcel(parcel); |
| 629 | parcel.recycle(); |
| 630 | return receiverForSending; |
| 631 | } |
| 632 | |
| 633 | /** |
| 634 | * Update the last entitlement value to internal cache |
| 635 | * |
| 636 | * @param type tethering type from ConnectivityManager.TETHERING_{@code *} |
| 637 | * @param resultCode last entitlement value |
| 638 | * @return the last updated entitlement value |
| 639 | */ |
markchien | 3b51963 | 2018-09-07 16:19:12 +0800 | [diff] [blame] | 640 | private int updateEntitlementCacheValue(int type, int resultCode) { |
markchien | f273127 | 2019-01-16 17:44:13 +0800 | [diff] [blame] | 641 | if (DBG) { |
markchien | 9f246bd | 2019-04-03 10:43:09 +0800 | [diff] [blame] | 642 | mLog.i("updateEntitlementCacheValue: " + type + ", result: " + resultCode); |
markchien | f273127 | 2019-01-16 17:44:13 +0800 | [diff] [blame] | 643 | } |
markchien | 3b51963 | 2018-09-07 16:19:12 +0800 | [diff] [blame] | 644 | if (resultCode == TETHER_ERROR_NO_ERROR) { |
| 645 | mEntitlementCacheValue.put(type, resultCode); |
| 646 | return resultCode; |
| 647 | } else { |
| 648 | mEntitlementCacheValue.put(type, TETHER_ERROR_PROVISION_FAILED); |
| 649 | return TETHER_ERROR_PROVISION_FAILED; |
markchien | f273127 | 2019-01-16 17:44:13 +0800 | [diff] [blame] | 650 | } |
| 651 | } |
| 652 | |
| 653 | /** Get the last value of the tethering entitlement check. */ |
markchien | 9554abf | 2019-03-06 16:25:00 +0800 | [diff] [blame] | 654 | public void getLatestTetheringEntitlementResult(int downstream, ResultReceiver receiver, |
markchien | f273127 | 2019-01-16 17:44:13 +0800 | [diff] [blame] | 655 | boolean showEntitlementUi) { |
markchien | 3b51963 | 2018-09-07 16:19:12 +0800 | [diff] [blame] | 656 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_GET_ENTITLEMENT_VALUE, |
| 657 | downstream, encodeBool(showEntitlementUi), receiver)); |
| 658 | |
| 659 | } |
| 660 | |
| 661 | private void handleGetLatestTetheringEntitlementValue(int downstream, ResultReceiver receiver, |
| 662 | boolean showEntitlementUi) { |
markchien | 3394e14 | 2019-04-09 15:53:24 +0800 | [diff] [blame] | 663 | final TetheringConfiguration config = mFetcher.fetchTetheringConfiguration(); |
| 664 | if (!isTetherProvisioningRequired(config)) { |
markchien | f273127 | 2019-01-16 17:44:13 +0800 | [diff] [blame] | 665 | receiver.send(TETHER_ERROR_NO_ERROR, null); |
| 666 | return; |
| 667 | } |
| 668 | |
markchien | 3b51963 | 2018-09-07 16:19:12 +0800 | [diff] [blame] | 669 | final int cacheValue = mEntitlementCacheValue.get( |
markchien | f273127 | 2019-01-16 17:44:13 +0800 | [diff] [blame] | 670 | downstream, TETHER_ERROR_ENTITLEMENT_UNKONWN); |
markchien | f273127 | 2019-01-16 17:44:13 +0800 | [diff] [blame] | 671 | if (cacheValue == TETHER_ERROR_NO_ERROR || !showEntitlementUi) { |
| 672 | receiver.send(cacheValue, null); |
| 673 | } else { |
markchien | 29b7014 | 2019-03-26 21:41:59 +0800 | [diff] [blame] | 674 | ResultReceiver proxy = buildProxyReceiver(downstream, false/* notifyFail */, receiver); |
markchien | 3394e14 | 2019-04-09 15:53:24 +0800 | [diff] [blame] | 675 | runUiTetherProvisioning(downstream, config.subId, proxy); |
markchien | f273127 | 2019-01-16 17:44:13 +0800 | [diff] [blame] | 676 | } |
| 677 | } |
markchien | b6eb2c2 | 2018-07-18 14:29:20 +0800 | [diff] [blame] | 678 | } |