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