Merge "Send offload status changed callback" into rvc-dev
diff --git a/api/module-lib-current.txt b/api/module-lib-current.txt
index 93ad787..69b52693 100644
--- a/api/module-lib-current.txt
+++ b/api/module-lib-current.txt
@@ -47,6 +47,7 @@
     method @NonNull public String[] getTetheredIfaces();
     method @NonNull public String[] getTetheringErroredIfaces();
     method public boolean isTetheringSupported();
+    method public boolean isTetheringSupported(@NonNull String);
     method @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) public void registerTetheringEventCallback(@NonNull java.util.concurrent.Executor, @NonNull android.net.TetheringManager.TetheringEventCallback);
     method @RequiresPermission(anyOf={"android.permission.TETHER_PRIVILEGED", android.Manifest.permission.WRITE_SETTINGS}) public void requestLatestTetheringEntitlementResult(int, boolean, @NonNull java.util.concurrent.Executor, @NonNull android.net.TetheringManager.OnTetheringEntitlementResultListener);
     method public void requestLatestTetheringEntitlementResult(int, @NonNull android.os.ResultReceiver, boolean);
diff --git a/core/java/android/net/ConnectivityManager.java b/core/java/android/net/ConnectivityManager.java
index 38ef814..fc6954f 100644
--- a/core/java/android/net/ConnectivityManager.java
+++ b/core/java/android/net/ConnectivityManager.java
@@ -53,7 +53,6 @@
 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;
@@ -808,7 +807,7 @@
 
     private INetworkManagementService mNMService;
     private INetworkPolicyManager mNPManager;
-    private TetheringManager mTetheringManager;
+    private final TetheringManager mTetheringManager;
 
     /**
      * Tests if a given integer represents a valid network type.
@@ -2274,6 +2273,7 @@
     public ConnectivityManager(Context context, IConnectivityManager service) {
         mContext = Preconditions.checkNotNull(context, "missing context");
         mService = Preconditions.checkNotNull(service, "missing IConnectivityManager");
+        mTetheringManager = (TetheringManager) mContext.getSystemService(Context.TETHERING_SERVICE);
         sInstance = this;
     }
 
@@ -2347,28 +2347,6 @@
         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.
@@ -2382,7 +2360,7 @@
     @UnsupportedAppUsage
     @Deprecated
     public String[] getTetherableIfaces() {
-        return getTetheringManager().getTetherableIfaces();
+        return mTetheringManager.getTetherableIfaces();
     }
 
     /**
@@ -2397,7 +2375,7 @@
     @UnsupportedAppUsage
     @Deprecated
     public String[] getTetheredIfaces() {
-        return getTetheringManager().getTetheredIfaces();
+        return mTetheringManager.getTetheredIfaces();
     }
 
     /**
@@ -2418,7 +2396,7 @@
     @UnsupportedAppUsage
     @Deprecated
     public String[] getTetheringErroredIfaces() {
-        return getTetheringManager().getTetheringErroredIfaces();
+        return mTetheringManager.getTetheringErroredIfaces();
     }
 
     /**
@@ -2462,7 +2440,7 @@
     @UnsupportedAppUsage
     @Deprecated
     public int tether(String iface) {
-        return getTetheringManager().tether(iface);
+        return mTetheringManager.tether(iface);
     }
 
     /**
@@ -2486,7 +2464,7 @@
     @UnsupportedAppUsage
     @Deprecated
     public int untether(String iface) {
-        return getTetheringManager().untether(iface);
+        return mTetheringManager.untether(iface);
     }
 
     /**
@@ -2512,7 +2490,7 @@
     @RequiresPermission(anyOf = {android.Manifest.permission.TETHER_PRIVILEGED,
             android.Manifest.permission.WRITE_SETTINGS})
     public boolean isTetheringSupported() {
-        return getTetheringManager().isTetheringSupported();
+        return mTetheringManager.isTetheringSupported();
     }
 
     /**
@@ -2605,7 +2583,7 @@
         final TetheringRequest request = new TetheringRequest.Builder(type)
                 .setSilentProvisioning(!showProvisioningUi).build();
 
-        getTetheringManager().startTethering(request, executor, tetheringCallback);
+        mTetheringManager.startTethering(request, executor, tetheringCallback);
     }
 
     /**
@@ -2624,7 +2602,7 @@
     @Deprecated
     @RequiresPermission(android.Manifest.permission.TETHER_PRIVILEGED)
     public void stopTethering(int type) {
-        getTetheringManager().stopTethering(type);
+        mTetheringManager.stopTethering(type);
     }
 
     /**
@@ -2682,7 +2660,7 @@
 
         synchronized (mTetheringEventCallbacks) {
             mTetheringEventCallbacks.put(callback, tetherCallback);
-            getTetheringManager().registerTetheringEventCallback(executor, tetherCallback);
+            mTetheringManager.registerTetheringEventCallback(executor, tetherCallback);
         }
     }
 
@@ -2704,7 +2682,7 @@
         synchronized (mTetheringEventCallbacks) {
             final TetheringEventCallback tetherCallback =
                     mTetheringEventCallbacks.remove(callback);
-            getTetheringManager().unregisterTetheringEventCallback(tetherCallback);
+            mTetheringManager.unregisterTetheringEventCallback(tetherCallback);
         }
     }
 
@@ -2724,7 +2702,7 @@
     @UnsupportedAppUsage
     @Deprecated
     public String[] getTetherableUsbRegexs() {
-        return getTetheringManager().getTetherableUsbRegexs();
+        return mTetheringManager.getTetherableUsbRegexs();
     }
 
     /**
@@ -2742,7 +2720,7 @@
     @UnsupportedAppUsage
     @Deprecated
     public String[] getTetherableWifiRegexs() {
-        return getTetheringManager().getTetherableWifiRegexs();
+        return mTetheringManager.getTetherableWifiRegexs();
     }
 
     /**
@@ -2761,7 +2739,7 @@
     @UnsupportedAppUsage
     @Deprecated
     public String[] getTetherableBluetoothRegexs() {
-        return getTetheringManager().getTetherableBluetoothRegexs();
+        return mTetheringManager.getTetherableBluetoothRegexs();
     }
 
     /**
@@ -2785,7 +2763,7 @@
     @UnsupportedAppUsage
     @Deprecated
     public int setUsbTethering(boolean enable) {
-        return getTetheringManager().setUsbTethering(enable);
+        return mTetheringManager.setUsbTethering(enable);
     }
 
     /**
@@ -2902,7 +2880,7 @@
     @UnsupportedAppUsage
     @Deprecated
     public int getLastTetherError(String iface) {
-        return getTetheringManager().getLastTetherError(iface);
+        return mTetheringManager.getLastTetherError(iface);
     }
 
     /** @hide */
@@ -2973,7 +2951,7 @@
             }
         };
 
-        getTetheringManager().requestLatestTetheringEntitlementResult(type, wrappedListener,
+        mTetheringManager.requestLatestTetheringEntitlementResult(type, wrappedListener,
                     showEntitlementUi);
     }
 
@@ -4469,7 +4447,7 @@
     public void factoryReset() {
         try {
             mService.factoryReset();
-            getTetheringManager().stopAllTethering();
+            mTetheringManager.stopAllTethering();
         } catch (RemoteException e) {
             throw e.rethrowFromSystemServer();
         }
diff --git a/packages/Tethering/common/TetheringLib/src/android/net/TetheringManager.java b/packages/Tethering/common/TetheringLib/src/android/net/TetheringManager.java
index 183e7ff..7f831ce 100644
--- a/packages/Tethering/common/TetheringLib/src/android/net/TetheringManager.java
+++ b/packages/Tethering/common/TetheringLib/src/android/net/TetheringManager.java
@@ -1168,6 +1168,25 @@
     public boolean isTetheringSupported() {
         final String callerPkg = mContext.getOpPackageName();
 
+        return isTetheringSupported(callerPkg);
+    }
+
+    /**
+     * Check if the device allows for tethering. It may be disabled via {@code ro.tether.denied}
+     * system property, Settings.TETHER_SUPPORTED or due to device configuration. This is useful
+     * for system components that query this API on behalf of an app. In particular, Bluetooth
+     * has @UnsupportedAppUsage calls that will let apps turn on bluetooth tethering if they have
+     * the right permissions, but such an app needs to know whether it can (permissions as well
+     * as support from the device) turn on tethering in the first place to show the appropriate UI.
+     *
+     * @param callerPkg The caller package name, if it is not matching the calling uid,
+     *       SecurityException would be thrown.
+     * @return a boolean - {@code true} indicating Tethering is supported.
+     * @hide
+     */
+    @SystemApi(client = MODULE_LIBRARIES)
+    public boolean isTetheringSupported(@NonNull final String callerPkg) {
+
         final RequestDispatcher dispatcher = new RequestDispatcher();
         final int ret = dispatcher.waitForResult((connector, listener) -> {
             try {