diff options
Diffstat (limited to 'core/java/android/net/ConnectivityManager.java')
| -rw-r--r-- | core/java/android/net/ConnectivityManager.java | 174 |
1 files changed, 43 insertions, 131 deletions
diff --git a/core/java/android/net/ConnectivityManager.java b/core/java/android/net/ConnectivityManager.java index d95da91d2009..3ed51d7f13e4 100644 --- a/core/java/android/net/ConnectivityManager.java +++ b/core/java/android/net/ConnectivityManager.java @@ -50,6 +50,7 @@ import android.os.RemoteException; import android.os.ResultReceiver; import android.os.ServiceManager; import android.os.ServiceSpecificException; +import android.os.SystemClock; import android.provider.Settings; import android.telephony.SubscriptionManager; import android.telephony.TelephonyManager; @@ -57,7 +58,6 @@ import android.util.ArrayMap; import android.util.Log; import android.util.SparseIntArray; -import com.android.internal.annotations.GuardedBy; import com.android.internal.util.Preconditions; import com.android.internal.util.Protocol; @@ -802,6 +802,7 @@ public class ConnectivityManager { private INetworkManagementService mNMService; private INetworkPolicyManager mNPManager; + private TetheringManager mTetheringManager; /** * Tests if a given integer represents a valid network type. @@ -2339,6 +2340,28 @@ public class ConnectivityManager { return getInstanceOrNull(); } + private static final int TETHERING_TIMEOUT_MS = 60_000; + private final Object mTetheringLock = new Object(); + + private TetheringManager getTetheringManager() { + synchronized (mTetheringLock) { + if (mTetheringManager != null) { + return mTetheringManager; + } + final long before = System.currentTimeMillis(); + while ((mTetheringManager = (TetheringManager) mContext.getSystemService( + Context.TETHERING_SERVICE)) == null) { + if (System.currentTimeMillis() - before > TETHERING_TIMEOUT_MS) { + Log.e(TAG, "Timeout waiting tethering service not ready yet"); + throw new IllegalStateException("No tethering service yet"); + } + SystemClock.sleep(100); + } + + return mTetheringManager; + } + } + /** * Get the set of tetherable, available interfaces. This list is limited by * device configuration and current interface existence. @@ -2350,11 +2373,7 @@ public class ConnectivityManager { @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) @UnsupportedAppUsage public String[] getTetherableIfaces() { - try { - return mService.getTetherableIfaces(); - } catch (RemoteException e) { - throw e.rethrowFromSystemServer(); - } + return getTetheringManager().getTetherableIfaces(); } /** @@ -2367,11 +2386,7 @@ public class ConnectivityManager { @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) @UnsupportedAppUsage public String[] getTetheredIfaces() { - try { - return mService.getTetheredIfaces(); - } catch (RemoteException e) { - throw e.rethrowFromSystemServer(); - } + return getTetheringManager().getTetheredIfaces(); } /** @@ -2390,11 +2405,7 @@ public class ConnectivityManager { @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) @UnsupportedAppUsage public String[] getTetheringErroredIfaces() { - try { - return mService.getTetheringErroredIfaces(); - } catch (RemoteException e) { - throw e.rethrowFromSystemServer(); - } + return getTetheringManager().getTetheringErroredIfaces(); } /** @@ -2405,11 +2416,7 @@ public class ConnectivityManager { */ @RequiresPermission(android.Manifest.permission.NETWORK_SETTINGS) public String[] getTetheredDhcpRanges() { - try { - return mService.getTetheredDhcpRanges(); - } catch (RemoteException e) { - throw e.rethrowFromSystemServer(); - } + return getTetheringManager().getTetheredDhcpRanges(); } /** @@ -2438,13 +2445,7 @@ public class ConnectivityManager { */ @UnsupportedAppUsage public int tether(String iface) { - try { - String pkgName = mContext.getOpPackageName(); - Log.i(TAG, "tether caller:" + pkgName); - return mService.tether(iface, pkgName); - } catch (RemoteException e) { - throw e.rethrowFromSystemServer(); - } + return getTetheringManager().tether(iface); } /** @@ -2467,13 +2468,7 @@ public class ConnectivityManager { */ @UnsupportedAppUsage public int untether(String iface) { - try { - String pkgName = mContext.getOpPackageName(); - Log.i(TAG, "untether caller:" + pkgName); - return mService.untether(iface, pkgName); - } catch (RemoteException e) { - throw e.rethrowFromSystemServer(); - } + return getTetheringManager().untether(iface); } /** @@ -2498,16 +2493,7 @@ public class ConnectivityManager { @RequiresPermission(anyOf = {android.Manifest.permission.TETHER_PRIVILEGED, android.Manifest.permission.WRITE_SETTINGS}) public boolean isTetheringSupported() { - String pkgName = mContext.getOpPackageName(); - try { - return mService.isTetheringSupported(pkgName); - } catch (SecurityException e) { - // This API is not available to this caller, but for backward-compatibility - // this will just return false instead of throwing. - return false; - } catch (RemoteException e) { - throw e.rethrowFromSystemServer(); - } + return getTetheringManager().isTetheringSupported(); } /** @@ -2576,14 +2562,7 @@ public class ConnectivityManager { } }; - try { - String pkgName = mContext.getOpPackageName(); - Log.i(TAG, "startTethering caller:" + pkgName); - mService.startTethering(type, wrappedCallback, showProvisioningUi, pkgName); - } catch (RemoteException e) { - Log.e(TAG, "Exception trying to start tethering.", e); - wrappedCallback.send(TETHER_ERROR_SERVICE_UNAVAIL, null); - } + getTetheringManager().startTethering(type, wrappedCallback, showProvisioningUi); } /** @@ -2599,13 +2578,7 @@ public class ConnectivityManager { @SystemApi @RequiresPermission(android.Manifest.permission.TETHER_PRIVILEGED) public void stopTethering(int type) { - try { - String pkgName = mContext.getOpPackageName(); - Log.i(TAG, "stopTethering caller:" + pkgName); - mService.stopTethering(type, pkgName); - } catch (RemoteException e) { - throw e.rethrowFromSystemServer(); - } + getTetheringManager().stopTethering(type); } /** @@ -2627,10 +2600,6 @@ public class ConnectivityManager { public void onUpstreamChanged(@Nullable Network network) {} } - @GuardedBy("mTetheringEventCallbacks") - private final ArrayMap<OnTetheringEventCallback, ITetheringEventCallback> - mTetheringEventCallbacks = new ArrayMap<>(); - /** * Start listening to tethering change events. Any new added callback will receive the last * tethering status right away. If callback is registered when tethering has no upstream or @@ -2648,27 +2617,7 @@ public class ConnectivityManager { @NonNull final OnTetheringEventCallback callback) { Preconditions.checkNotNull(callback, "OnTetheringEventCallback cannot be null."); - synchronized (mTetheringEventCallbacks) { - Preconditions.checkArgument(!mTetheringEventCallbacks.containsKey(callback), - "callback was already registered."); - ITetheringEventCallback remoteCallback = new ITetheringEventCallback.Stub() { - @Override - public void onUpstreamChanged(Network network) throws RemoteException { - Binder.withCleanCallingIdentity(() -> - executor.execute(() -> { - callback.onUpstreamChanged(network); - })); - } - }; - try { - String pkgName = mContext.getOpPackageName(); - Log.i(TAG, "registerTetheringUpstreamCallback:" + pkgName); - mService.registerTetheringEventCallback(remoteCallback, pkgName); - mTetheringEventCallbacks.put(callback, remoteCallback); - } catch (RemoteException e) { - throw e.rethrowFromSystemServer(); - } - } + getTetheringManager().registerTetheringEventCallback(executor, callback); } /** @@ -2682,17 +2631,7 @@ public class ConnectivityManager { @RequiresPermission(android.Manifest.permission.TETHER_PRIVILEGED) public void unregisterTetheringEventCallback( @NonNull final OnTetheringEventCallback callback) { - synchronized (mTetheringEventCallbacks) { - ITetheringEventCallback remoteCallback = mTetheringEventCallbacks.remove(callback); - Preconditions.checkNotNull(remoteCallback, "callback was not registered."); - try { - String pkgName = mContext.getOpPackageName(); - Log.i(TAG, "unregisterTetheringEventCallback:" + pkgName); - mService.unregisterTetheringEventCallback(remoteCallback, pkgName); - } catch (RemoteException e) { - throw e.rethrowFromSystemServer(); - } - } + getTetheringManager().unregisterTetheringEventCallback(callback); } @@ -2709,11 +2648,7 @@ public class ConnectivityManager { @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) @UnsupportedAppUsage public String[] getTetherableUsbRegexs() { - try { - return mService.getTetherableUsbRegexs(); - } catch (RemoteException e) { - throw e.rethrowFromSystemServer(); - } + return getTetheringManager().getTetherableUsbRegexs(); } /** @@ -2729,11 +2664,7 @@ public class ConnectivityManager { @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) @UnsupportedAppUsage public String[] getTetherableWifiRegexs() { - try { - return mService.getTetherableWifiRegexs(); - } catch (RemoteException e) { - throw e.rethrowFromSystemServer(); - } + return getTetheringManager().getTetherableWifiRegexs(); } /** @@ -2749,11 +2680,7 @@ public class ConnectivityManager { @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) @UnsupportedAppUsage public String[] getTetherableBluetoothRegexs() { - try { - return mService.getTetherableBluetoothRegexs(); - } catch (RemoteException e) { - throw e.rethrowFromSystemServer(); - } + return getTetheringManager().getTetherableBluetoothRegexs(); } /** @@ -2775,13 +2702,7 @@ public class ConnectivityManager { */ @UnsupportedAppUsage public int setUsbTethering(boolean enable) { - try { - String pkgName = mContext.getOpPackageName(); - Log.i(TAG, "setUsbTethering caller:" + pkgName); - return mService.setUsbTethering(enable, pkgName); - } catch (RemoteException e) { - throw e.rethrowFromSystemServer(); - } + return getTetheringManager().setUsbTethering(enable); } /** {@hide} */ @@ -2829,11 +2750,7 @@ public class ConnectivityManager { @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) @UnsupportedAppUsage public int getLastTetherError(String iface) { - try { - return mService.getLastTetherError(iface); - } catch (RemoteException e) { - throw e.rethrowFromSystemServer(); - } + return getTetheringManager().getLastTetherError(iface); } /** @hide */ @@ -2899,14 +2816,8 @@ public class ConnectivityManager { } }; - try { - String pkgName = mContext.getOpPackageName(); - Log.i(TAG, "getLatestTetheringEntitlementResult:" + pkgName); - mService.getLatestTetheringEntitlementResult(type, wrappedListener, - showEntitlementUi, pkgName); - } catch (RemoteException e) { - throw e.rethrowFromSystemServer(); - } + getTetheringManager().requestLatestTetheringEntitlementResult(type, wrappedListener, + showEntitlementUi); } /** @@ -4331,6 +4242,7 @@ public class ConnectivityManager { public void factoryReset() { try { mService.factoryReset(); + getTetheringManager().stopAllTethering(); } catch (RemoteException e) { throw e.rethrowFromSystemServer(); } |
