OSDN Git Service

Move explicit mobile request handling into UpstreamNetworkMonitor
authorErik Kline <ek@google.com>
Thu, 12 Jan 2017 08:02:51 +0000 (17:02 +0900)
committerErik Kline <ek@google.com>
Fri, 13 Jan 2017 05:09:57 +0000 (14:09 +0900)
Also:
    - some minor typo fixes
    - some minor style fixes
    - variable renames for clarity

Test: as follows
    - built (bullhead)
    - flashed
    - booted
    - runtest frameworks-net passes
    - regular (no DUN) wifi tethering to mobile works
    - USB tethering with switching between mobile and wifi works
Bug: 32163131
Change-Id: I113536f24436f0b962f402f3b37386e29468dc0a

services/core/java/com/android/server/connectivity/Tethering.java

index e336283..57381f2 100644 (file)
@@ -520,8 +520,8 @@ public class Tethering extends BaseNetworkObserver implements IControlsTethering
     }
 
     /**
-     * Creates a proxy {@link ResultReceiver} which enables tethering if the provsioning result is
-     * successful before firing back up to the wrapped receiver.
+     * Creates a proxy {@link ResultReceiver} which enables tethering if the provisioning result
+     * is successful before firing back up to the wrapped receiver.
      *
      * @param type The type of tethering being enabled.
      * @param receiver A ResultReceiver which will be called back with an int resultCode.
@@ -890,7 +890,11 @@ public class Tethering extends BaseNetworkObserver implements IControlsTethering
 
     public int setUsbTethering(boolean enable) {
         if (VDBG) Log.d(TAG, "setUsbTethering(" + enable + ")");
-        UsbManager usbManager = (UsbManager)mContext.getSystemService(Context.USB_SERVICE);
+        UsbManager usbManager = mContext.getSystemService(UsbManager.class);
+        if (usbManager == null) {
+            return enable ? ConnectivityManager.TETHER_ERROR_MASTER_ERROR
+                          : ConnectivityManager.TETHER_ERROR_NO_ERROR;
+        }
 
         synchronized (mPublicSync) {
             if (enable) {
@@ -936,7 +940,7 @@ public class Tethering extends BaseNetworkObserver implements IControlsTethering
 
     private void checkDunRequired() {
         int secureSetting = 2;
-        TelephonyManager tm = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
+        TelephonyManager tm = mContext.getSystemService(TelephonyManager.class);
         if (tm != null) {
             secureSetting = tm.getTetherApnRequired();
         }
@@ -1054,6 +1058,8 @@ public class Tethering extends BaseNetworkObserver implements IControlsTethering
         private ConnectivityManager mCM;
         private NetworkCallback mDefaultNetworkCallback;
         private NetworkCallback mDunTetheringCallback;
+        private NetworkCallback mMobileNetworkCallback;
+        private boolean mDunRequired;
 
         public UpstreamNetworkMonitor(Context ctx, StateMachine tgt, int what) {
             mContext = ctx;
@@ -1077,6 +1083,8 @@ public class Tethering extends BaseNetworkObserver implements IControlsTethering
         }
 
         public void stop() {
+            releaseMobileNetworkRequest();
+
             releaseCallback(mDefaultNetworkCallback);
             mDefaultNetworkCallback = null;
 
@@ -1086,6 +1094,52 @@ public class Tethering extends BaseNetworkObserver implements IControlsTethering
             mNetworkMap.clear();
         }
 
+        public void mobileUpstreamRequiresDun(boolean dunRequired) {
+            final boolean valueChanged = (mDunRequired != dunRequired);
+            mDunRequired = dunRequired;
+            if (valueChanged && mobileNetworkRequested()) {
+                releaseMobileNetworkRequest();
+                registerMobileNetworkRequest();
+            }
+        }
+
+        public boolean mobileNetworkRequested() {
+            return (mMobileNetworkCallback != null);
+        }
+
+        public void registerMobileNetworkRequest() {
+            if (mMobileNetworkCallback != null) return;
+
+            final NetworkRequest.Builder builder = new NetworkRequest.Builder()
+                    .addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR);
+            if (mDunRequired) {
+                builder.removeCapability(NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED)
+                       .addCapability(NetworkCapabilities.NET_CAPABILITY_DUN);
+            } else {
+                builder.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET);
+            }
+            final NetworkRequest mobileUpstreamRequest = builder.build();
+
+            // The existing default network and DUN callbacks will be notified.
+            // Therefore, to avoid duplicate notifications, we only register a no-op.
+            mMobileNetworkCallback = new NetworkCallback();
+
+            // TODO: Change the timeout from 0 (no onUnavailable callback) to use some
+            // moderate callback time (once timeout callbacks are implemented). This might
+            // be useful for updating some UI. Additionally, we should definitely log a
+            // message to aid in any subsequent debugging
+            if (DBG) Log.d(TAG, "requesting mobile upstream network: " + mobileUpstreamRequest);
+
+            cm().requestNetwork(mobileUpstreamRequest, mMobileNetworkCallback);
+        }
+
+        public void releaseMobileNetworkRequest() {
+            if (mMobileNetworkCallback == null) return;
+
+            cm().unregisterNetworkCallback(mMobileNetworkCallback);
+            mMobileNetworkCallback = null;
+        }
+
         public NetworkState lookup(Network network) {
             return (network != null) ? mNetworkMap.get(network) : null;
         }
@@ -1272,8 +1326,7 @@ public class Tethering extends BaseNetworkObserver implements IControlsTethering
         private final ArrayList<TetherInterfaceStateMachine> mNotifyList;
         private final IPv6TetheringCoordinator mIPv6TetheringCoordinator;
 
-        private int mMobileApnReserved = ConnectivityManager.TYPE_NONE;
-        private NetworkCallback mMobileUpstreamCallback;
+        private int mPreviousMobileApn = ConnectivityManager.TYPE_NONE;
 
         private static final int UPSTREAM_SETTLE_TIME_MS     = 10000;
 
@@ -1311,13 +1364,13 @@ public class Tethering extends BaseNetworkObserver implements IControlsTethering
             protected boolean turnOnUpstreamMobileConnection(int apnType) {
                 if (apnType == ConnectivityManager.TYPE_NONE) { return false; }
 
-                if (apnType != mMobileApnReserved) {
+                if (apnType != mPreviousMobileApn) {
                     // Unregister any previous mobile upstream callback because
                     // this request, if any, will be different.
                     turnOffUpstreamMobileConnection();
                 }
 
-                if (mMobileUpstreamCallback != null) {
+                if (mUpstreamNetworkMonitor.mobileNetworkRequested()) {
                     // Looks like we already filed a request for this apnType.
                     return true;
                 }
@@ -1326,42 +1379,25 @@ public class Tethering extends BaseNetworkObserver implements IControlsTethering
                     case ConnectivityManager.TYPE_MOBILE_DUN:
                     case ConnectivityManager.TYPE_MOBILE:
                     case ConnectivityManager.TYPE_MOBILE_HIPRI:
-                        mMobileApnReserved = apnType;
+                        mPreviousMobileApn = apnType;
                         break;
                     default:
                         return false;
                 }
 
-                final NetworkRequest.Builder builder = new NetworkRequest.Builder()
-                        .addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR);
-                if (apnType == ConnectivityManager.TYPE_MOBILE_DUN) {
-                    builder.removeCapability(NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED)
-                           .addCapability(NetworkCapabilities.NET_CAPABILITY_DUN);
-                } else {
-                    builder.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET);
-                }
-                final NetworkRequest mobileUpstreamRequest = builder.build();
-
-                // The UpstreamNetworkMonitor's callback will be notified.
-                // Therefore, to avoid duplicate notifications, we only register a no-op.
-                mMobileUpstreamCallback = new NetworkCallback();
-
-                // TODO: Change the timeout from 0 (no onUnavailable callback) to use some
-                // moderate callback time (once timeout callbacks are implemented). This might
-                // be useful for updating some UI. Additionally, we should definitely log a
-                // message to aid in any subsequent debugging
-                if (DBG) Log.d(TAG, "requesting mobile upstream network: " + mobileUpstreamRequest);
-                getConnectivityManager().requestNetwork(
-                        mobileUpstreamRequest, mMobileUpstreamCallback, 0, apnType);
+                // TODO: This should be called by the code that observes
+                // configuration changes, once the above code in this function
+                // is simplified (i.e. eradicated).
+                mUpstreamNetworkMonitor.mobileUpstreamRequiresDun(
+                        apnType == ConnectivityManager.TYPE_MOBILE_DUN);
+
+                mUpstreamNetworkMonitor.registerMobileNetworkRequest();
                 return true;
             }
 
             protected void turnOffUpstreamMobileConnection() {
-                if (mMobileUpstreamCallback != null) {
-                    getConnectivityManager().unregisterNetworkCallback(mMobileUpstreamCallback);
-                    mMobileUpstreamCallback = null;
-                }
-                mMobileApnReserved = ConnectivityManager.TYPE_NONE;
+                mUpstreamNetworkMonitor.releaseMobileNetworkRequest();
+                mPreviousMobileApn = ConnectivityManager.TYPE_NONE;
             }
 
             protected boolean turnOnMasterTetherSettings() {
@@ -1677,7 +1713,6 @@ public class Tethering extends BaseNetworkObserver implements IControlsTethering
 
             @Override
             public void exit() {
-                // TODO: examine if we should check the return value.
                 turnOffUpstreamMobileConnection();
                 mUpstreamNetworkMonitor.stop();
                 stopListeningForSimChanges();