markchien | 0df2ebc4 | 2019-09-30 14:40:57 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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 | |
markchien | 503be61 | 2020-04-12 21:41:29 +0800 | [diff] [blame] | 17 | package com.android.networkstack.tethering; |
markchien | 0df2ebc4 | 2019-09-30 14:40:57 +0800 | [diff] [blame] | 18 | |
markchien | 5788f2a | 2020-05-08 18:55:26 +0800 | [diff] [blame] | 19 | import static android.Manifest.permission.ACCESS_NETWORK_STATE; |
| 20 | import static android.Manifest.permission.TETHER_PRIVILEGED; |
markchien | 6d06f6d | 2019-12-16 20:15:20 +0800 | [diff] [blame] | 21 | import static android.content.pm.PackageManager.PERMISSION_GRANTED; |
| 22 | import static android.net.TetheringManager.TETHER_ERROR_NO_ACCESS_TETHERING_PERMISSION; |
| 23 | import static android.net.TetheringManager.TETHER_ERROR_NO_CHANGE_TETHERING_PERMISSION; |
| 24 | import static android.net.TetheringManager.TETHER_ERROR_NO_ERROR; |
| 25 | import static android.net.TetheringManager.TETHER_ERROR_UNSUPPORTED; |
markchien | 986750b | 2019-12-06 15:24:53 +0800 | [diff] [blame] | 26 | import static android.net.dhcp.IDhcpServer.STATUS_UNKNOWN_ERROR; |
markchien | 6d06f6d | 2019-12-16 20:15:20 +0800 | [diff] [blame] | 27 | |
markchien | 0df2ebc4 | 2019-09-30 14:40:57 +0800 | [diff] [blame] | 28 | import android.app.Service; |
markchien | 3fe660b | 2019-12-05 12:04:59 +0800 | [diff] [blame] | 29 | import android.bluetooth.BluetoothAdapter; |
markchien | 0df2ebc4 | 2019-09-30 14:40:57 +0800 | [diff] [blame] | 30 | import android.content.Context; |
| 31 | import android.content.Intent; |
markchien | 6d06f6d | 2019-12-16 20:15:20 +0800 | [diff] [blame] | 32 | import android.net.IIntResultListener; |
| 33 | import android.net.INetworkStackConnector; |
markchien | 0df2ebc4 | 2019-09-30 14:40:57 +0800 | [diff] [blame] | 34 | import android.net.ITetheringConnector; |
markchien | 6d06f6d | 2019-12-16 20:15:20 +0800 | [diff] [blame] | 35 | import android.net.ITetheringEventCallback; |
markchien | 87d3f7d | 2020-01-08 20:58:23 +0800 | [diff] [blame] | 36 | import android.net.NetworkCapabilities; |
markchien | 0df2ebc4 | 2019-09-30 14:40:57 +0800 | [diff] [blame] | 37 | import android.net.NetworkRequest; |
Remi NGUYEN VAN | 438018d | 2020-03-18 18:31:39 +0900 | [diff] [blame] | 38 | import android.net.NetworkStack; |
markchien | 0688917 | 2020-01-20 19:31:56 +0800 | [diff] [blame] | 39 | import android.net.TetheringRequestParcel; |
markchien | 6d06f6d | 2019-12-16 20:15:20 +0800 | [diff] [blame] | 40 | import android.net.dhcp.DhcpServerCallbacks; |
| 41 | import android.net.dhcp.DhcpServingParamsParcel; |
| 42 | import android.net.ip.IpServer; |
markchien | 0df2ebc4 | 2019-09-30 14:40:57 +0800 | [diff] [blame] | 43 | import android.net.util.SharedLog; |
markchien | 6d06f6d | 2019-12-16 20:15:20 +0800 | [diff] [blame] | 44 | import android.os.Binder; |
markchien | 0df2ebc4 | 2019-09-30 14:40:57 +0800 | [diff] [blame] | 45 | import android.os.HandlerThread; |
| 46 | import android.os.IBinder; |
| 47 | import android.os.Looper; |
markchien | 6d06f6d | 2019-12-16 20:15:20 +0800 | [diff] [blame] | 48 | import android.os.RemoteException; |
markchien | 0df2ebc4 | 2019-09-30 14:40:57 +0800 | [diff] [blame] | 49 | import android.os.ResultReceiver; |
| 50 | import android.os.SystemProperties; |
markchien | 6d06f6d | 2019-12-16 20:15:20 +0800 | [diff] [blame] | 51 | import android.os.UserManager; |
markchien | 0df2ebc4 | 2019-09-30 14:40:57 +0800 | [diff] [blame] | 52 | import android.provider.Settings; |
markchien | 6d06f6d | 2019-12-16 20:15:20 +0800 | [diff] [blame] | 53 | import android.util.Log; |
markchien | 0df2ebc4 | 2019-09-30 14:40:57 +0800 | [diff] [blame] | 54 | |
| 55 | import androidx.annotation.NonNull; |
| 56 | import androidx.annotation.Nullable; |
| 57 | |
| 58 | import com.android.internal.annotations.VisibleForTesting; |
| 59 | |
| 60 | import java.io.FileDescriptor; |
| 61 | import java.io.PrintWriter; |
| 62 | |
| 63 | /** |
| 64 | * Android service used to manage tethering. |
| 65 | * |
| 66 | * <p>The service returns a binder for the system server to communicate with the tethering. |
| 67 | */ |
| 68 | public class TetheringService extends Service { |
| 69 | private static final String TAG = TetheringService.class.getSimpleName(); |
| 70 | |
| 71 | private final SharedLog mLog = new SharedLog(TAG); |
| 72 | private TetheringConnector mConnector; |
| 73 | private Context mContext; |
| 74 | private TetheringDependencies mDeps; |
| 75 | private Tethering mTethering; |
markchien | 6d06f6d | 2019-12-16 20:15:20 +0800 | [diff] [blame] | 76 | private UserManager mUserManager; |
markchien | 0df2ebc4 | 2019-09-30 14:40:57 +0800 | [diff] [blame] | 77 | |
| 78 | @Override |
| 79 | public void onCreate() { |
| 80 | mLog.mark("onCreate"); |
| 81 | mDeps = getTetheringDependencies(); |
| 82 | mContext = mDeps.getContext(); |
markchien | 6d06f6d | 2019-12-16 20:15:20 +0800 | [diff] [blame] | 83 | mUserManager = (UserManager) mContext.getSystemService(Context.USER_SERVICE); |
markchien | 5f7b3be | 2020-01-31 14:04:36 +0800 | [diff] [blame] | 84 | mTethering = makeTethering(mDeps); |
Mark Chien | 674a012 | 2020-03-27 09:37:01 +0000 | [diff] [blame] | 85 | mTethering.startStateMachineUpdaters(); |
markchien | 0df2ebc4 | 2019-09-30 14:40:57 +0800 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Make a reference to Tethering object. |
| 90 | */ |
| 91 | @VisibleForTesting |
| 92 | public Tethering makeTethering(TetheringDependencies deps) { |
markchien | 771c3e9 | 2019-12-07 22:02:28 +0800 | [diff] [blame] | 93 | System.loadLibrary("tetherutilsjni"); |
markchien | 0df2ebc4 | 2019-09-30 14:40:57 +0800 | [diff] [blame] | 94 | return new Tethering(deps); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Create a binder connector for the system server to communicate with the tethering. |
| 99 | */ |
| 100 | private synchronized IBinder makeConnector() { |
| 101 | if (mConnector == null) { |
markchien | 6d06f6d | 2019-12-16 20:15:20 +0800 | [diff] [blame] | 102 | mConnector = new TetheringConnector(mTethering, TetheringService.this); |
markchien | 0df2ebc4 | 2019-09-30 14:40:57 +0800 | [diff] [blame] | 103 | } |
| 104 | return mConnector; |
| 105 | } |
| 106 | |
| 107 | @NonNull |
| 108 | @Override |
| 109 | public IBinder onBind(Intent intent) { |
| 110 | mLog.mark("onBind"); |
| 111 | return makeConnector(); |
| 112 | } |
| 113 | |
| 114 | private static class TetheringConnector extends ITetheringConnector.Stub { |
markchien | 6d06f6d | 2019-12-16 20:15:20 +0800 | [diff] [blame] | 115 | private final TetheringService mService; |
| 116 | private final Tethering mTethering; |
markchien | 0df2ebc4 | 2019-09-30 14:40:57 +0800 | [diff] [blame] | 117 | |
markchien | 6d06f6d | 2019-12-16 20:15:20 +0800 | [diff] [blame] | 118 | TetheringConnector(Tethering tether, TetheringService service) { |
| 119 | mTethering = tether; |
| 120 | mService = service; |
markchien | 0df2ebc4 | 2019-09-30 14:40:57 +0800 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | @Override |
markchien | 6d06f6d | 2019-12-16 20:15:20 +0800 | [diff] [blame] | 124 | public void tether(String iface, String callerPkg, IIntResultListener listener) { |
| 125 | if (checkAndNotifyCommonError(callerPkg, listener)) return; |
| 126 | |
| 127 | try { |
| 128 | listener.onResult(mTethering.tether(iface)); |
| 129 | } catch (RemoteException e) { } |
markchien | 0df2ebc4 | 2019-09-30 14:40:57 +0800 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | @Override |
markchien | 6d06f6d | 2019-12-16 20:15:20 +0800 | [diff] [blame] | 133 | public void untether(String iface, String callerPkg, IIntResultListener listener) { |
| 134 | if (checkAndNotifyCommonError(callerPkg, listener)) return; |
| 135 | |
| 136 | try { |
| 137 | listener.onResult(mTethering.untether(iface)); |
| 138 | } catch (RemoteException e) { } |
markchien | 0df2ebc4 | 2019-09-30 14:40:57 +0800 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | @Override |
markchien | 6d06f6d | 2019-12-16 20:15:20 +0800 | [diff] [blame] | 142 | public void setUsbTethering(boolean enable, String callerPkg, IIntResultListener listener) { |
| 143 | if (checkAndNotifyCommonError(callerPkg, listener)) return; |
| 144 | |
| 145 | try { |
| 146 | listener.onResult(mTethering.setUsbTethering(enable)); |
| 147 | } catch (RemoteException e) { } |
markchien | 0df2ebc4 | 2019-09-30 14:40:57 +0800 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | @Override |
markchien | 0688917 | 2020-01-20 19:31:56 +0800 | [diff] [blame] | 151 | public void startTethering(TetheringRequestParcel request, String callerPkg, |
| 152 | IIntResultListener listener) { |
markchien | 5788f2a | 2020-05-08 18:55:26 +0800 | [diff] [blame] | 153 | if (checkAndNotifyCommonError(callerPkg, |
| 154 | request.exemptFromEntitlementCheck /* onlyAllowPrivileged */, |
| 155 | listener)) { |
| 156 | return; |
| 157 | } |
markchien | 6d06f6d | 2019-12-16 20:15:20 +0800 | [diff] [blame] | 158 | |
markchien | 0688917 | 2020-01-20 19:31:56 +0800 | [diff] [blame] | 159 | mTethering.startTethering(request, listener); |
markchien | 0df2ebc4 | 2019-09-30 14:40:57 +0800 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | @Override |
markchien | 6d06f6d | 2019-12-16 20:15:20 +0800 | [diff] [blame] | 163 | public void stopTethering(int type, String callerPkg, IIntResultListener listener) { |
| 164 | if (checkAndNotifyCommonError(callerPkg, listener)) return; |
| 165 | |
| 166 | try { |
| 167 | mTethering.stopTethering(type); |
| 168 | listener.onResult(TETHER_ERROR_NO_ERROR); |
| 169 | } catch (RemoteException e) { } |
markchien | 0df2ebc4 | 2019-09-30 14:40:57 +0800 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | @Override |
| 173 | public void requestLatestTetheringEntitlementResult(int type, ResultReceiver receiver, |
markchien | 6d06f6d | 2019-12-16 20:15:20 +0800 | [diff] [blame] | 174 | boolean showEntitlementUi, String callerPkg) { |
| 175 | if (checkAndNotifyCommonError(callerPkg, receiver)) return; |
| 176 | |
| 177 | mTethering.requestLatestTetheringEntitlementResult(type, receiver, showEntitlementUi); |
markchien | 0df2ebc4 | 2019-09-30 14:40:57 +0800 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | @Override |
markchien | 6d06f6d | 2019-12-16 20:15:20 +0800 | [diff] [blame] | 181 | public void registerTetheringEventCallback(ITetheringEventCallback callback, |
| 182 | String callerPkg) { |
| 183 | try { |
markchien | 5788f2a | 2020-05-08 18:55:26 +0800 | [diff] [blame] | 184 | if (!hasTetherAccessPermission()) { |
markchien | 6d06f6d | 2019-12-16 20:15:20 +0800 | [diff] [blame] | 185 | callback.onCallbackStopped(TETHER_ERROR_NO_ACCESS_TETHERING_PERMISSION); |
| 186 | return; |
| 187 | } |
| 188 | mTethering.registerTetheringEventCallback(callback); |
| 189 | } catch (RemoteException e) { } |
markchien | 0df2ebc4 | 2019-09-30 14:40:57 +0800 | [diff] [blame] | 190 | } |
markchien | 6d06f6d | 2019-12-16 20:15:20 +0800 | [diff] [blame] | 191 | |
| 192 | @Override |
| 193 | public void unregisterTetheringEventCallback(ITetheringEventCallback callback, |
| 194 | String callerPkg) { |
| 195 | try { |
markchien | 5788f2a | 2020-05-08 18:55:26 +0800 | [diff] [blame] | 196 | if (!hasTetherAccessPermission()) { |
markchien | 6d06f6d | 2019-12-16 20:15:20 +0800 | [diff] [blame] | 197 | callback.onCallbackStopped(TETHER_ERROR_NO_ACCESS_TETHERING_PERMISSION); |
| 198 | return; |
| 199 | } |
| 200 | mTethering.unregisterTetheringEventCallback(callback); |
| 201 | } catch (RemoteException e) { } |
| 202 | } |
| 203 | |
| 204 | @Override |
| 205 | public void stopAllTethering(String callerPkg, IIntResultListener listener) { |
| 206 | if (checkAndNotifyCommonError(callerPkg, listener)) return; |
| 207 | |
| 208 | try { |
| 209 | mTethering.untetherAll(); |
| 210 | listener.onResult(TETHER_ERROR_NO_ERROR); |
| 211 | } catch (RemoteException e) { } |
| 212 | } |
| 213 | |
| 214 | @Override |
| 215 | public void isTetheringSupported(String callerPkg, IIntResultListener listener) { |
| 216 | if (checkAndNotifyCommonError(callerPkg, listener)) return; |
| 217 | |
| 218 | try { |
| 219 | listener.onResult(TETHER_ERROR_NO_ERROR); |
| 220 | } catch (RemoteException e) { } |
| 221 | } |
| 222 | |
| 223 | @Override |
| 224 | protected void dump(@NonNull FileDescriptor fd, @NonNull PrintWriter writer, |
| 225 | @Nullable String[] args) { |
| 226 | mTethering.dump(fd, writer, args); |
| 227 | } |
| 228 | |
| 229 | private boolean checkAndNotifyCommonError(String callerPkg, IIntResultListener listener) { |
markchien | 5788f2a | 2020-05-08 18:55:26 +0800 | [diff] [blame] | 230 | return checkAndNotifyCommonError(callerPkg, false /* onlyAllowPrivileged */, listener); |
| 231 | } |
| 232 | |
| 233 | private boolean checkAndNotifyCommonError(final String callerPkg, |
| 234 | final boolean onlyAllowPrivileged, final IIntResultListener listener) { |
markchien | 6d06f6d | 2019-12-16 20:15:20 +0800 | [diff] [blame] | 235 | try { |
markchien | 5788f2a | 2020-05-08 18:55:26 +0800 | [diff] [blame] | 236 | if (!hasTetherChangePermission(callerPkg, onlyAllowPrivileged)) { |
markchien | 6d06f6d | 2019-12-16 20:15:20 +0800 | [diff] [blame] | 237 | listener.onResult(TETHER_ERROR_NO_CHANGE_TETHERING_PERMISSION); |
| 238 | return true; |
| 239 | } |
| 240 | if (!mService.isTetheringSupported()) { |
| 241 | listener.onResult(TETHER_ERROR_UNSUPPORTED); |
| 242 | return true; |
| 243 | } |
| 244 | } catch (RemoteException e) { |
| 245 | return true; |
| 246 | } |
| 247 | |
| 248 | return false; |
| 249 | } |
| 250 | |
| 251 | private boolean checkAndNotifyCommonError(String callerPkg, ResultReceiver receiver) { |
markchien | 5788f2a | 2020-05-08 18:55:26 +0800 | [diff] [blame] | 252 | if (!hasTetherChangePermission(callerPkg, false /* onlyAllowPrivileged */)) { |
markchien | 6d06f6d | 2019-12-16 20:15:20 +0800 | [diff] [blame] | 253 | receiver.send(TETHER_ERROR_NO_CHANGE_TETHERING_PERMISSION, null); |
| 254 | return true; |
| 255 | } |
| 256 | if (!mService.isTetheringSupported()) { |
| 257 | receiver.send(TETHER_ERROR_UNSUPPORTED, null); |
| 258 | return true; |
| 259 | } |
| 260 | |
| 261 | return false; |
| 262 | } |
| 263 | |
markchien | 5788f2a | 2020-05-08 18:55:26 +0800 | [diff] [blame] | 264 | private boolean hasTetherPrivilegedPermission() { |
| 265 | return mService.checkCallingOrSelfPermission(TETHER_PRIVILEGED) == PERMISSION_GRANTED; |
| 266 | } |
| 267 | |
| 268 | private boolean hasTetherChangePermission(final String callerPkg, |
| 269 | final boolean onlyAllowPrivileged) { |
| 270 | if (hasTetherPrivilegedPermission()) return true; |
| 271 | |
| 272 | if (onlyAllowPrivileged || mTethering.isTetherProvisioningRequired()) return false; |
| 273 | |
| 274 | int uid = Binder.getCallingUid(); |
| 275 | // If callerPkg's uid is not same as Binder.getCallingUid(), |
| 276 | // checkAndNoteWriteSettingsOperation will return false and the operation will be |
| 277 | // denied. |
| 278 | return Settings.checkAndNoteWriteSettingsOperation(mService, uid, callerPkg, |
| 279 | false /* throwException */); |
| 280 | } |
| 281 | |
| 282 | private boolean hasTetherAccessPermission() { |
| 283 | if (hasTetherPrivilegedPermission()) return true; |
| 284 | |
| 285 | return mService.checkCallingOrSelfPermission( |
| 286 | ACCESS_NETWORK_STATE) == PERMISSION_GRANTED; |
| 287 | } |
markchien | 0df2ebc4 | 2019-09-30 14:40:57 +0800 | [diff] [blame] | 288 | } |
| 289 | |
markchien | 6d06f6d | 2019-12-16 20:15:20 +0800 | [diff] [blame] | 290 | // if ro.tether.denied = true we default to no tethering |
| 291 | // gservices could set the secure setting to 1 though to enable it on a build where it |
| 292 | // had previously been turned off. |
| 293 | private boolean isTetheringSupported() { |
| 294 | final int defaultVal = |
| 295 | SystemProperties.get("ro.tether.denied").equals("true") ? 0 : 1; |
| 296 | final boolean tetherSupported = Settings.Global.getInt(mContext.getContentResolver(), |
| 297 | Settings.Global.TETHER_SUPPORTED, defaultVal) != 0; |
| 298 | final boolean tetherEnabledInSettings = tetherSupported |
| 299 | && !mUserManager.hasUserRestriction(UserManager.DISALLOW_CONFIG_TETHERING); |
| 300 | |
| 301 | return tetherEnabledInSettings && mTethering.hasTetherableConfiguration(); |
markchien | 0df2ebc4 | 2019-09-30 14:40:57 +0800 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | /** |
| 305 | * An injection method for testing. |
| 306 | */ |
| 307 | @VisibleForTesting |
| 308 | public TetheringDependencies getTetheringDependencies() { |
| 309 | if (mDeps == null) { |
| 310 | mDeps = new TetheringDependencies() { |
| 311 | @Override |
| 312 | public NetworkRequest getDefaultNetworkRequest() { |
markchien | 87d3f7d | 2020-01-08 20:58:23 +0800 | [diff] [blame] | 313 | // TODO: b/147280869, add a proper system API to replace this. |
| 314 | final NetworkRequest trackDefaultRequest = new NetworkRequest.Builder() |
| 315 | .clearCapabilities() |
| 316 | .addCapability(NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED) |
| 317 | .addCapability(NetworkCapabilities.NET_CAPABILITY_TRUSTED) |
| 318 | .addCapability(NetworkCapabilities.NET_CAPABILITY_NOT_VPN) |
| 319 | .addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) |
| 320 | .build(); |
| 321 | return trackDefaultRequest; |
markchien | 0df2ebc4 | 2019-09-30 14:40:57 +0800 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | @Override |
| 325 | public Looper getTetheringLooper() { |
| 326 | final HandlerThread tetherThread = new HandlerThread("android.tethering"); |
| 327 | tetherThread.start(); |
| 328 | return tetherThread.getLooper(); |
| 329 | } |
| 330 | |
| 331 | @Override |
| 332 | public boolean isTetheringSupported() { |
markchien | 6d06f6d | 2019-12-16 20:15:20 +0800 | [diff] [blame] | 333 | return TetheringService.this.isTetheringSupported(); |
markchien | 0df2ebc4 | 2019-09-30 14:40:57 +0800 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | @Override |
| 337 | public Context getContext() { |
| 338 | return TetheringService.this; |
| 339 | } |
markchien | 6d06f6d | 2019-12-16 20:15:20 +0800 | [diff] [blame] | 340 | |
| 341 | @Override |
| 342 | public IpServer.Dependencies getIpServerDependencies() { |
| 343 | return new IpServer.Dependencies() { |
| 344 | @Override |
| 345 | public void makeDhcpServer(String ifName, DhcpServingParamsParcel params, |
| 346 | DhcpServerCallbacks cb) { |
| 347 | try { |
| 348 | final INetworkStackConnector service = getNetworkStackConnector(); |
| 349 | if (service == null) return; |
| 350 | |
| 351 | service.makeDhcpServer(ifName, params, cb); |
| 352 | } catch (RemoteException e) { |
markchien | 986750b | 2019-12-06 15:24:53 +0800 | [diff] [blame] | 353 | Log.e(TAG, "Fail to make dhcp server"); |
| 354 | try { |
| 355 | cb.onDhcpServerCreated(STATUS_UNKNOWN_ERROR, null); |
| 356 | } catch (RemoteException re) { } |
markchien | 6d06f6d | 2019-12-16 20:15:20 +0800 | [diff] [blame] | 357 | } |
| 358 | } |
| 359 | }; |
| 360 | } |
| 361 | |
| 362 | // TODO: replace this by NetworkStackClient#getRemoteConnector after refactoring |
| 363 | // networkStackClient. |
| 364 | static final int NETWORKSTACK_TIMEOUT_MS = 60_000; |
| 365 | private INetworkStackConnector getNetworkStackConnector() { |
| 366 | IBinder connector; |
| 367 | try { |
| 368 | final long before = System.currentTimeMillis(); |
Remi NGUYEN VAN | 438018d | 2020-03-18 18:31:39 +0900 | [diff] [blame] | 369 | while ((connector = NetworkStack.getService()) == null) { |
markchien | 6d06f6d | 2019-12-16 20:15:20 +0800 | [diff] [blame] | 370 | if (System.currentTimeMillis() - before > NETWORKSTACK_TIMEOUT_MS) { |
| 371 | Log.wtf(TAG, "Timeout, fail to get INetworkStackConnector"); |
| 372 | return null; |
| 373 | } |
| 374 | Thread.sleep(200); |
| 375 | } |
| 376 | } catch (InterruptedException e) { |
| 377 | Log.wtf(TAG, "Interrupted, fail to get INetworkStackConnector"); |
| 378 | return null; |
| 379 | } |
| 380 | return INetworkStackConnector.Stub.asInterface(connector); |
| 381 | } |
markchien | 3fe660b | 2019-12-05 12:04:59 +0800 | [diff] [blame] | 382 | |
| 383 | @Override |
| 384 | public BluetoothAdapter getBluetoothAdapter() { |
| 385 | return BluetoothAdapter.getDefaultAdapter(); |
| 386 | } |
markchien | 0df2ebc4 | 2019-09-30 14:40:57 +0800 | [diff] [blame] | 387 | }; |
| 388 | } |
markchien | 0df2ebc4 | 2019-09-30 14:40:57 +0800 | [diff] [blame] | 389 | return mDeps; |
| 390 | } |
| 391 | } |