From 465ff3a0c1da8afd5cb13b25ed9a3c95ee0dd2c4 Mon Sep 17 00:00:00 2001 From: Erik Kline Date: Fri, 9 Mar 2018 14:18:02 +0900 Subject: [PATCH] Refactor isTetheringSupported() calls Tethering currently wants access to complex isTetheringSupported logic that is only available in ConnectivityService. Instead of trying to access that via ConnectivityManager, pass this capability in to Tethering directly, in the TetheringDependencies object. Also: - ConnectivityManager is only a source of static constants now, so "import static" all the constants that are actually used. Test: as follows - built - flashed - booted - runtest frameworks-net works - manual USB towards WiFi tethering works Bug: 68951715 Change-Id: Ia64faaadefb4a5d84a50da98bdebd544b6fda101 --- .../android/server/ConnectivityService.java | 7 +- .../server/connectivity/Tethering.java | 166 ++++++++++-------- .../tethering/TetheringDependencies.java | 4 + .../server/connectivity/TetheringTest.java | 82 +++++---- 4 files changed, 138 insertions(+), 121 deletions(-) diff --git a/services/core/java/com/android/server/ConnectivityService.java b/services/core/java/com/android/server/ConnectivityService.java index ae7ac8f04165a..995f926479820 100644 --- a/services/core/java/com/android/server/ConnectivityService.java +++ b/services/core/java/com/android/server/ConnectivityService.java @@ -878,7 +878,12 @@ public class ConnectivityService extends IConnectivityManager.Stub private Tethering makeTethering() { // TODO: Move other elements into @Overridden getters. - final TetheringDependencies deps = new TetheringDependencies(); + final TetheringDependencies deps = new TetheringDependencies() { + @Override + public boolean isTetheringSupported() { + return ConnectivityService.this.isTetheringSupported(); + } + }; return new Tethering(mContext, mNetd, mStatsService, mPolicyManager, IoThread.get().getLooper(), new MockableSystemProperties(), deps); diff --git a/services/core/java/com/android/server/connectivity/Tethering.java b/services/core/java/com/android/server/connectivity/Tethering.java index cac191cc99b34..e520d0cba06ec 100644 --- a/services/core/java/com/android/server/connectivity/Tethering.java +++ b/services/core/java/com/android/server/connectivity/Tethering.java @@ -19,6 +19,27 @@ package com.android.server.connectivity; import static android.hardware.usb.UsbManager.USB_CONFIGURED; import static android.hardware.usb.UsbManager.USB_CONNECTED; import static android.hardware.usb.UsbManager.USB_FUNCTION_RNDIS; +import static android.net.ConnectivityManager.ACTION_TETHER_STATE_CHANGED; +import static android.net.ConnectivityManager.CONNECTIVITY_ACTION; +import static android.net.ConnectivityManager.EXTRA_ACTIVE_LOCAL_ONLY; +import static android.net.ConnectivityManager.EXTRA_ACTIVE_TETHER; +import static android.net.ConnectivityManager.EXTRA_ADD_TETHER_TYPE; +import static android.net.ConnectivityManager.EXTRA_AVAILABLE_TETHER; +import static android.net.ConnectivityManager.EXTRA_ERRORED_TETHER; +import static android.net.ConnectivityManager.EXTRA_NETWORK_INFO; +import static android.net.ConnectivityManager.EXTRA_PROVISION_CALLBACK; +import static android.net.ConnectivityManager.EXTRA_REM_TETHER_TYPE; +import static android.net.ConnectivityManager.EXTRA_RUN_PROVISION; +import static android.net.ConnectivityManager.EXTRA_SET_ALARM; +import static android.net.ConnectivityManager.TETHER_ERROR_MASTER_ERROR; +import static android.net.ConnectivityManager.TETHER_ERROR_NO_ERROR; +import static android.net.ConnectivityManager.TETHER_ERROR_SERVICE_UNAVAIL; +import static android.net.ConnectivityManager.TETHER_ERROR_UNKNOWN_IFACE; +import static android.net.ConnectivityManager.TETHER_ERROR_UNAVAIL_IFACE; +import static android.net.ConnectivityManager.TETHERING_BLUETOOTH; +import static android.net.ConnectivityManager.TETHERING_INVALID; +import static android.net.ConnectivityManager.TETHERING_USB; +import static android.net.ConnectivityManager.TETHERING_WIFI; import static android.net.NetworkCapabilities.TRANSPORT_CELLULAR; import static android.net.wifi.WifiManager.EXTRA_WIFI_AP_INTERFACE_NAME; import static android.net.wifi.WifiManager.EXTRA_WIFI_AP_MODE; @@ -45,7 +66,6 @@ import android.content.Intent; import android.content.IntentFilter; import android.content.res.Resources; import android.hardware.usb.UsbManager; -import android.net.ConnectivityManager; import android.net.INetworkPolicyManager; import android.net.INetworkStatsService; import android.net.IpPrefix; @@ -148,7 +168,7 @@ public class Tethering extends BaseNetworkObserver { stateMachine = sm; // Assume all state machines start out available and with no errors. lastState = IControlsTethering.STATE_AVAILABLE; - lastError = ConnectivityManager.TETHER_ERROR_NO_ERROR; + lastError = TETHER_ERROR_NO_ERROR; } public boolean isCurrentlyServing() { @@ -216,7 +236,7 @@ public class Tethering extends BaseNetworkObserver { final Handler smHandler = mTetherMasterSM.getHandler(); mOffloadController = new OffloadController(smHandler, - deps.getOffloadHardwareInterface(smHandler, mLog), + mDeps.getOffloadHardwareInterface(smHandler, mLog), mContext.getContentResolver(), mNMService, mLog); mUpstreamNetworkMonitor = deps.getUpstreamNetworkMonitor(mContext, mTetherMasterSM, mLog, @@ -243,7 +263,7 @@ public class Tethering extends BaseNetworkObserver { mStateReceiver = new StateReceiver(); filter = new IntentFilter(); filter.addAction(UsbManager.ACTION_USB_STATE); - filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION); + filter.addAction(CONNECTIVITY_ACTION); filter.addAction(WifiManager.WIFI_AP_STATE_CHANGED_ACTION); filter.addAction(Intent.ACTION_CONFIGURATION_CHANGED); mContext.registerReceiver(mStateReceiver, filter, null, smHandler); @@ -265,12 +285,6 @@ public class Tethering extends BaseNetworkObserver { updateConfiguration(); } - // We can't do this once in the Tethering() constructor and cache the value, because the - // CONNECTIVITY_SERVICE is registered only after the Tethering() constructor has completed. - private ConnectivityManager getConnectivityManager() { - return (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE); - } - private WifiManager getWifiManager() { return (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE); } @@ -296,7 +310,7 @@ public class Tethering extends BaseNetworkObserver { if (up) { maybeTrackNewInterfaceLocked(iface); } else { - if (ifaceNameToType(iface) == ConnectivityManager.TETHERING_BLUETOOTH) { + if (ifaceNameToType(iface) == TETHERING_BLUETOOTH) { stopTrackingInterfaceLocked(iface); } else { // Ignore usb0 down after enabling RNDIS. @@ -318,13 +332,13 @@ public class Tethering extends BaseNetworkObserver { final TetheringConfiguration cfg = mConfig; if (cfg.isWifi(iface)) { - return ConnectivityManager.TETHERING_WIFI; + return TETHERING_WIFI; } else if (cfg.isUsb(iface)) { - return ConnectivityManager.TETHERING_USB; + return TETHERING_USB; } else if (cfg.isBluetooth(iface)) { - return ConnectivityManager.TETHERING_BLUETOOTH; + return TETHERING_BLUETOOTH; } - return ConnectivityManager.TETHERING_INVALID; + return TETHERING_INVALID; } @Override @@ -426,26 +440,26 @@ public class Tethering extends BaseNetworkObserver { boolean isProvisioningRequired = enable && isTetherProvisioningRequired(); int result; switch (type) { - case ConnectivityManager.TETHERING_WIFI: + case TETHERING_WIFI: result = setWifiTethering(enable); - if (isProvisioningRequired && result == ConnectivityManager.TETHER_ERROR_NO_ERROR) { + if (isProvisioningRequired && result == TETHER_ERROR_NO_ERROR) { scheduleProvisioningRechecks(type); } sendTetherResult(receiver, result); break; - case ConnectivityManager.TETHERING_USB: + case TETHERING_USB: result = setUsbTethering(enable); - if (isProvisioningRequired && result == ConnectivityManager.TETHER_ERROR_NO_ERROR) { + if (isProvisioningRequired && result == TETHER_ERROR_NO_ERROR) { scheduleProvisioningRechecks(type); } sendTetherResult(receiver, result); break; - case ConnectivityManager.TETHERING_BLUETOOTH: + case TETHERING_BLUETOOTH: setBluetoothTethering(enable, receiver); break; default: Log.w(TAG, "Invalid tether type."); - sendTetherResult(receiver, ConnectivityManager.TETHER_ERROR_UNKNOWN_IFACE); + sendTetherResult(receiver, TETHER_ERROR_UNKNOWN_IFACE); } } @@ -456,7 +470,7 @@ public class Tethering extends BaseNetworkObserver { } private int setWifiTethering(final boolean enable) { - int rval = ConnectivityManager.TETHER_ERROR_MASTER_ERROR; + int rval = TETHER_ERROR_MASTER_ERROR; final long ident = Binder.clearCallingIdentity(); try { synchronized (mPublicSync) { @@ -464,7 +478,7 @@ public class Tethering extends BaseNetworkObserver { final WifiManager mgr = getWifiManager(); if ((enable && mgr.startSoftAp(null /* use existing wifi config */)) || (!enable && mgr.stopSoftAp())) { - rval = ConnectivityManager.TETHER_ERROR_NO_ERROR; + rval = TETHER_ERROR_NO_ERROR; } } } finally { @@ -478,7 +492,7 @@ public class Tethering extends BaseNetworkObserver { if (adapter == null || !adapter.isEnabled()) { Log.w(TAG, "Tried to enable bluetooth tethering with null or disabled adapter. null: " + (adapter == null)); - sendTetherResult(receiver, ConnectivityManager.TETHER_ERROR_SERVICE_UNAVAIL); + sendTetherResult(receiver, TETHER_ERROR_SERVICE_UNAVAIL); return; } @@ -491,12 +505,12 @@ public class Tethering extends BaseNetworkObserver { ((BluetoothPan) proxy).setBluetoothTethering(enable); // TODO: Enabling bluetooth tethering can fail asynchronously here. // We should figure out a way to bubble up that failure instead of sending success. - int result = ((BluetoothPan) proxy).isTetheringOn() == enable ? - ConnectivityManager.TETHER_ERROR_NO_ERROR : - ConnectivityManager.TETHER_ERROR_MASTER_ERROR; + final int result = (((BluetoothPan) proxy).isTetheringOn() == enable) + ? TETHER_ERROR_NO_ERROR + : TETHER_ERROR_MASTER_ERROR; sendTetherResult(receiver, result); if (enable && isTetherProvisioningRequired()) { - scheduleProvisioningRechecks(ConnectivityManager.TETHERING_BLUETOOTH); + scheduleProvisioningRechecks(TETHERING_BLUETOOTH); } adapter.closeProfileProxy(BluetoothProfile.PAN, proxy); } @@ -510,8 +524,8 @@ public class Tethering extends BaseNetworkObserver { private void sendUiTetherProvisionIntent(int type, ResultReceiver receiver) { Intent intent = new Intent(Settings.ACTION_TETHER_PROVISIONING); - intent.putExtra(ConnectivityManager.EXTRA_ADD_TETHER_TYPE, type); - intent.putExtra(ConnectivityManager.EXTRA_PROVISION_CALLBACK, receiver); + intent.putExtra(EXTRA_ADD_TETHER_TYPE, type); + intent.putExtra(EXTRA_PROVISION_CALLBACK, receiver); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); final long ident = Binder.clearCallingIdentity(); try { @@ -534,7 +548,7 @@ public class Tethering extends BaseNetworkObserver { @Override protected void onReceiveResult(int resultCode, Bundle resultData) { // If provisioning is successful, enable tethering, otherwise just send the error. - if (resultCode == ConnectivityManager.TETHER_ERROR_NO_ERROR) { + if (resultCode == TETHER_ERROR_NO_ERROR) { enableTetheringInternal(type, true, receiver); } else { sendTetherResult(receiver, resultCode); @@ -554,8 +568,8 @@ public class Tethering extends BaseNetworkObserver { private void scheduleProvisioningRechecks(int type) { Intent intent = new Intent(); - intent.putExtra(ConnectivityManager.EXTRA_ADD_TETHER_TYPE, type); - intent.putExtra(ConnectivityManager.EXTRA_SET_ALARM, true); + intent.putExtra(EXTRA_ADD_TETHER_TYPE, type); + intent.putExtra(EXTRA_SET_ALARM, true); intent.setComponent(TETHER_SERVICE); final long ident = Binder.clearCallingIdentity(); try { @@ -572,9 +586,9 @@ public class Tethering extends BaseNetworkObserver { private void sendSilentTetherProvisionIntent(int type, ResultReceiver receiver) { Intent intent = new Intent(); - intent.putExtra(ConnectivityManager.EXTRA_ADD_TETHER_TYPE, type); - intent.putExtra(ConnectivityManager.EXTRA_RUN_PROVISION, true); - intent.putExtra(ConnectivityManager.EXTRA_PROVISION_CALLBACK, receiver); + intent.putExtra(EXTRA_ADD_TETHER_TYPE, type); + intent.putExtra(EXTRA_RUN_PROVISION, true); + intent.putExtra(EXTRA_PROVISION_CALLBACK, receiver); intent.setComponent(TETHER_SERVICE); final long ident = Binder.clearCallingIdentity(); try { @@ -585,9 +599,9 @@ public class Tethering extends BaseNetworkObserver { } private void cancelTetherProvisioningRechecks(int type) { - if (getConnectivityManager().isTetheringSupported()) { + if (mDeps.isTetheringSupported()) { Intent intent = new Intent(); - intent.putExtra(ConnectivityManager.EXTRA_REM_TETHER_TYPE, type); + intent.putExtra(EXTRA_REM_TETHER_TYPE, type); intent.setComponent(TETHER_SERVICE); final long ident = Binder.clearCallingIdentity(); try { @@ -602,8 +616,8 @@ public class Tethering extends BaseNetworkObserver { // TODO: De-duplicate with above code, where possible. private void startProvisionIntent(int tetherType) { final Intent startProvIntent = new Intent(); - startProvIntent.putExtra(ConnectivityManager.EXTRA_ADD_TETHER_TYPE, tetherType); - startProvIntent.putExtra(ConnectivityManager.EXTRA_RUN_PROVISION, true); + startProvIntent.putExtra(EXTRA_ADD_TETHER_TYPE, tetherType); + startProvIntent.putExtra(EXTRA_RUN_PROVISION, true); startProvIntent.setComponent(TETHER_SERVICE); mContext.startServiceAsUser(startProvIntent, UserHandle.CURRENT); } @@ -618,13 +632,13 @@ public class Tethering extends BaseNetworkObserver { TetherState tetherState = mTetherStates.get(iface); if (tetherState == null) { Log.e(TAG, "Tried to Tether an unknown iface: " + iface + ", ignoring"); - return ConnectivityManager.TETHER_ERROR_UNKNOWN_IFACE; + return TETHER_ERROR_UNKNOWN_IFACE; } // Ignore the error status of the interface. If the interface is available, // the errors are referring to past tethering attempts anyway. if (tetherState.lastState != IControlsTethering.STATE_AVAILABLE) { Log.e(TAG, "Tried to Tether an unavailable iface: " + iface + ", ignoring"); - return ConnectivityManager.TETHER_ERROR_UNAVAIL_IFACE; + return TETHER_ERROR_UNAVAIL_IFACE; } // NOTE: If a CMD_TETHER_REQUESTED message is already in the TISM's // queue but not yet processed, this will be a no-op and it will not @@ -633,7 +647,7 @@ public class Tethering extends BaseNetworkObserver { // TODO: reexamine the threading and messaging model. tetherState.stateMachine.sendMessage( TetherInterfaceStateMachine.CMD_TETHER_REQUESTED, requestedState); - return ConnectivityManager.TETHER_ERROR_NO_ERROR; + return TETHER_ERROR_NO_ERROR; } } @@ -643,22 +657,22 @@ public class Tethering extends BaseNetworkObserver { TetherState tetherState = mTetherStates.get(iface); if (tetherState == null) { Log.e(TAG, "Tried to Untether an unknown iface :" + iface + ", ignoring"); - return ConnectivityManager.TETHER_ERROR_UNKNOWN_IFACE; + return TETHER_ERROR_UNKNOWN_IFACE; } if (!tetherState.isCurrentlyServing()) { Log.e(TAG, "Tried to untether an inactive iface :" + iface + ", ignoring"); - return ConnectivityManager.TETHER_ERROR_UNAVAIL_IFACE; + return TETHER_ERROR_UNAVAIL_IFACE; } tetherState.stateMachine.sendMessage( TetherInterfaceStateMachine.CMD_TETHER_UNREQUESTED); - return ConnectivityManager.TETHER_ERROR_NO_ERROR; + return TETHER_ERROR_NO_ERROR; } } public void untetherAll() { - stopTethering(ConnectivityManager.TETHERING_WIFI); - stopTethering(ConnectivityManager.TETHERING_USB); - stopTethering(ConnectivityManager.TETHERING_BLUETOOTH); + stopTethering(TETHERING_WIFI); + stopTethering(TETHERING_USB); + stopTethering(TETHERING_BLUETOOTH); } public int getLastTetherError(String iface) { @@ -667,7 +681,7 @@ public class Tethering extends BaseNetworkObserver { if (tetherState == null) { Log.e(TAG, "Tried to getLastTetherError on an unknown iface :" + iface + ", ignoring"); - return ConnectivityManager.TETHER_ERROR_UNKNOWN_IFACE; + return TETHER_ERROR_UNKNOWN_IFACE; } return tetherState.lastError; } @@ -675,7 +689,7 @@ public class Tethering extends BaseNetworkObserver { // TODO: Figure out how to update for local hotspot mode interfaces. private void sendTetherStateChangedBroadcast() { - if (!getConnectivityManager().isTetheringSupported()) return; + if (!mDeps.isTetheringSupported()) return; final ArrayList availableList = new ArrayList<>(); final ArrayList tetherList = new ArrayList<>(); @@ -692,7 +706,7 @@ public class Tethering extends BaseNetworkObserver { for (int i = 0; i < mTetherStates.size(); i++) { TetherState tetherState = mTetherStates.valueAt(i); String iface = mTetherStates.keyAt(i); - if (tetherState.lastError != ConnectivityManager.TETHER_ERROR_NO_ERROR) { + if (tetherState.lastError != TETHER_ERROR_NO_ERROR) { erroredList.add(iface); } else if (tetherState.lastState == IControlsTethering.STATE_AVAILABLE) { availableList.add(iface); @@ -710,13 +724,13 @@ public class Tethering extends BaseNetworkObserver { } } } - final Intent bcast = new Intent(ConnectivityManager.ACTION_TETHER_STATE_CHANGED); + final Intent bcast = new Intent(ACTION_TETHER_STATE_CHANGED); bcast.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING | Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); - bcast.putStringArrayListExtra(ConnectivityManager.EXTRA_AVAILABLE_TETHER, availableList); - bcast.putStringArrayListExtra(ConnectivityManager.EXTRA_ACTIVE_LOCAL_ONLY, localOnlyList); - bcast.putStringArrayListExtra(ConnectivityManager.EXTRA_ACTIVE_TETHER, tetherList); - bcast.putStringArrayListExtra(ConnectivityManager.EXTRA_ERRORED_TETHER, erroredList); + bcast.putStringArrayListExtra(EXTRA_AVAILABLE_TETHER, availableList); + bcast.putStringArrayListExtra(EXTRA_ACTIVE_LOCAL_ONLY, localOnlyList); + bcast.putStringArrayListExtra(EXTRA_ACTIVE_TETHER, tetherList); + bcast.putStringArrayListExtra(EXTRA_ERRORED_TETHER, erroredList); mContext.sendStickyBroadcastAsUser(bcast, UserHandle.ALL); if (DBG) { Log.d(TAG, String.format( @@ -839,7 +853,7 @@ public class Tethering extends BaseNetworkObserver { if (action.equals(UsbManager.ACTION_USB_STATE)) { handleUsbAction(intent); - } else if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) { + } else if (action.equals(CONNECTIVITY_ACTION)) { handleConnectivityAction(intent); } else if (action.equals(WifiManager.WIFI_AP_STATE_CHANGED_ACTION)) { handleWifiApAction(intent); @@ -850,8 +864,8 @@ public class Tethering extends BaseNetworkObserver { } private void handleConnectivityAction(Intent intent) { - final NetworkInfo networkInfo = (NetworkInfo)intent.getParcelableExtra( - ConnectivityManager.EXTRA_NETWORK_INFO); + final NetworkInfo networkInfo = + (NetworkInfo) intent.getParcelableExtra(EXTRA_NETWORK_INFO); if (networkInfo == null || networkInfo.getDetailedState() == NetworkInfo.DetailedState.FAILED) { return; @@ -887,14 +901,10 @@ public class Tethering extends BaseNetworkObserver { synchronized (Tethering.this.mPublicSync) { if (!usbConnected && mRndisEnabled) { // Turn off tethering if it was enabled and there is a disconnect. - tetherMatchingInterfaces( - IControlsTethering.STATE_AVAILABLE, - ConnectivityManager.TETHERING_USB); + tetherMatchingInterfaces(IControlsTethering.STATE_AVAILABLE, TETHERING_USB); } else if (usbConfigured && rndisEnabled) { // Tether if rndis is enabled and usb is configured. - tetherMatchingInterfaces( - IControlsTethering.STATE_TETHERED, - ConnectivityManager.TETHERING_USB); + tetherMatchingInterfaces(IControlsTethering.STATE_TETHERED, TETHERING_USB); } mRndisEnabled = usbConfigured && rndisEnabled; } @@ -975,7 +985,7 @@ public class Tethering extends BaseNetworkObserver { for (int i = 0; i < mTetherStates.size(); i++) { TetherInterfaceStateMachine tism = mTetherStates.valueAt(i).stateMachine; - if (tism.interfaceType() == ConnectivityManager.TETHERING_WIFI) { + if (tism.interfaceType() == TETHERING_WIFI) { tism.unwanted(); return; } @@ -1003,7 +1013,7 @@ public class Tethering extends BaseNetworkObserver { } if (!TextUtils.isEmpty(ifname)) { - maybeTrackNewInterfaceLocked(ifname, ConnectivityManager.TETHERING_WIFI); + maybeTrackNewInterfaceLocked(ifname, TETHERING_WIFI); changeInterfaceState(ifname, ipServingMode); } else { mLog.e(String.format( @@ -1062,7 +1072,7 @@ public class Tethering extends BaseNetworkObserver { Log.wtf(TAG, "Unknown interface state: " + requestedState); return; } - if (result != ConnectivityManager.TETHER_ERROR_NO_ERROR) { + if (result != TETHER_ERROR_NO_ERROR) { Log.e(TAG, "unable start or stop tethering on iface " + ifname); return; } @@ -1103,7 +1113,7 @@ public class Tethering extends BaseNetworkObserver { synchronized (mPublicSync) { usbManager.setCurrentFunction(enable ? UsbManager.USB_FUNCTION_RNDIS : null, false); } - return ConnectivityManager.TETHER_ERROR_NO_ERROR; + return TETHER_ERROR_NO_ERROR; } // TODO review API - figure out how to delete these entirely. @@ -1142,7 +1152,7 @@ public class Tethering extends BaseNetworkObserver { synchronized (mPublicSync) { for (int i = 0; i < mTetherStates.size(); i++) { TetherState tetherState = mTetherStates.valueAt(i); - if (tetherState.lastError != ConnectivityManager.TETHER_ERROR_NO_ERROR) { + if (tetherState.lastError != TETHER_ERROR_NO_ERROR) { list.add(mTetherStates.keyAt(i)); } } @@ -1188,7 +1198,7 @@ public class Tethering extends BaseNetworkObserver { } String iface = mTetherStates.keyAt(i); int interfaceType = ifaceNameToType(iface); - if (interfaceType != ConnectivityManager.TETHERING_INVALID) { + if (interfaceType != TETHERING_INVALID) { tethered.add(interfaceType); } } @@ -1438,7 +1448,7 @@ public class Tethering extends BaseNetworkObserver { } // If this is a Wi-Fi interface, notify WifiManager of the active serving state. - if (who.interfaceType() == ConnectivityManager.TETHERING_WIFI) { + if (who.interfaceType() == TETHERING_WIFI) { final WifiManager mgr = getWifiManager(); final String iface = who.interfaceName(); switch (mode) { @@ -1462,8 +1472,8 @@ public class Tethering extends BaseNetworkObserver { mForwardedDownstreams.remove(who); // If this is a Wi-Fi interface, tell WifiManager of any errors. - if (who.interfaceType() == ConnectivityManager.TETHERING_WIFI) { - if (who.lastError() != ConnectivityManager.TETHER_ERROR_NO_ERROR) { + if (who.interfaceType() == TETHERING_WIFI) { + if (who.lastError() != TETHER_ERROR_NO_ERROR) { getWifiManager().updateInterfaceIpState( who.interfaceName(), IFACE_IP_MODE_CONFIGURATION_ERROR); } @@ -1668,7 +1678,7 @@ public class Tethering extends BaseNetworkObserver { who.sendMessage(mErrorNotification); break; case CMD_CLEAR_ERROR: - mErrorNotification = ConnectivityManager.TETHER_ERROR_NO_ERROR; + mErrorNotification = TETHER_ERROR_NO_ERROR; transitionTo(mInitialState); break; default: @@ -1930,7 +1940,7 @@ public class Tethering extends BaseNetworkObserver { // If TetherMasterSM is in ErrorState, TetherMasterSM stays there. // Thus we give a chance for TetherMasterSM to recover to InitialState // by sending CMD_CLEAR_ERROR - if (error == ConnectivityManager.TETHER_ERROR_MASTER_ERROR) { + if (error == TETHER_ERROR_MASTER_ERROR) { mTetherMasterSM.sendMessage(TetherMasterSM.CMD_CLEAR_ERROR, who); } int which; @@ -1974,7 +1984,7 @@ public class Tethering extends BaseNetworkObserver { private void maybeTrackNewInterfaceLocked(final String iface) { // If we don't care about this type of interface, ignore. final int interfaceType = ifaceNameToType(iface); - if (interfaceType == ConnectivityManager.TETHERING_INVALID) { + if (interfaceType == TETHERING_INVALID) { mLog.log(iface + " is not a tetherable iface, ignoring"); return; } diff --git a/services/core/java/com/android/server/connectivity/tethering/TetheringDependencies.java b/services/core/java/com/android/server/connectivity/tethering/TetheringDependencies.java index 66afb0f2cb145..0ac7a36e3ffc1 100644 --- a/services/core/java/com/android/server/connectivity/tethering/TetheringDependencies.java +++ b/services/core/java/com/android/server/connectivity/tethering/TetheringDependencies.java @@ -60,4 +60,8 @@ public class TetheringDependencies { public INetd getNetdService() { return NetdService.getInstance(); } + + public boolean isTetheringSupported() { + return true; + } } diff --git a/tests/net/java/com/android/server/connectivity/TetheringTest.java b/tests/net/java/com/android/server/connectivity/TetheringTest.java index d010f14ffa29d..8fb87f1277a38 100644 --- a/tests/net/java/com/android/server/connectivity/TetheringTest.java +++ b/tests/net/java/com/android/server/connectivity/TetheringTest.java @@ -19,8 +19,14 @@ package com.android.server.connectivity; import static android.hardware.usb.UsbManager.USB_CONFIGURED; import static android.hardware.usb.UsbManager.USB_CONNECTED; import static android.hardware.usb.UsbManager.USB_FUNCTION_RNDIS; +import static android.net.ConnectivityManager.ACTION_TETHER_STATE_CHANGED; +import static android.net.ConnectivityManager.EXTRA_ACTIVE_LOCAL_ONLY; +import static android.net.ConnectivityManager.EXTRA_ACTIVE_TETHER; +import static android.net.ConnectivityManager.EXTRA_AVAILABLE_TETHER; +import static android.net.ConnectivityManager.TETHER_ERROR_UNKNOWN_IFACE; import static android.net.ConnectivityManager.TETHERING_WIFI; import static android.net.ConnectivityManager.TETHERING_USB; +import static android.net.ConnectivityManager.TYPE_MOBILE; import static android.net.wifi.WifiManager.IFACE_IP_MODE_LOCAL_ONLY; import static android.net.wifi.WifiManager.IFACE_IP_MODE_TETHERED; import static android.net.wifi.WifiManager.EXTRA_WIFI_AP_INTERFACE_NAME; @@ -54,7 +60,6 @@ import android.content.IntentFilter; import android.content.pm.ApplicationInfo; import android.content.res.Resources; import android.hardware.usb.UsbManager; -import android.net.ConnectivityManager; import android.net.INetd; import android.net.INetworkPolicyManager; import android.net.INetworkStatsService; @@ -125,7 +130,6 @@ public class TetheringTest { @Mock private ApplicationInfo mApplicationInfo; @Mock private Context mContext; - @Mock private ConnectivityManager mConnectivityManager; @Mock private INetworkManagementService mNMService; @Mock private INetworkStatsService mStatsService; @Mock private INetworkPolicyManager mPolicyManager; @@ -173,7 +177,6 @@ public class TetheringTest { @Override public Object getSystemService(String name) { - if (Context.CONNECTIVITY_SERVICE.equals(name)) return mConnectivityManager; if (Context.WIFI_SERVICE.equals(name)) return mWifiManager; if (Context.USB_SERVICE.equals(name)) return mUsbManager; return super.getSystemService(name); @@ -181,8 +184,15 @@ public class TetheringTest { } public class MockTetheringDependencies extends TetheringDependencies { - private StateMachine upstreamNetworkMonitorMasterSM; - private ArrayList ipv6CoordinatorNotifyList; + StateMachine upstreamNetworkMonitorMasterSM; + ArrayList ipv6CoordinatorNotifyList; + int isTetheringSupportedCalls; + + public void reset() { + upstreamNetworkMonitorMasterSM = null; + ipv6CoordinatorNotifyList = null; + isTetheringSupportedCalls = 0; + } @Override public OffloadHardwareInterface getOffloadHardwareInterface(Handler h, SharedLog log) { @@ -222,11 +232,17 @@ public class TetheringTest { return new InterfaceParams(ifName, index + IFINDEX_OFFSET, MacAddress.ALL_ZEROS_ADDRESS); } + + @Override + public boolean isTetheringSupported() { + isTetheringSupportedCalls++; + return true; + } } private static NetworkState buildMobileUpstreamState(boolean withIPv4, boolean withIPv6, boolean with464xlat) { - final NetworkInfo info = new NetworkInfo(ConnectivityManager.TYPE_MOBILE, 0, null, null); + final NetworkInfo info = new NetworkInfo(TYPE_MOBILE, 0, null, null); info.setDetailedState(NetworkInfo.DetailedState.CONNECTED, null, null); final LinkProperties prop = new LinkProperties(); prop.setInterfaceName(TEST_MOBILE_IFNAME); @@ -308,7 +324,8 @@ public class TetheringTest { } }; mServiceContext.registerReceiver(mBroadcastReceiver, - new IntentFilter(ConnectivityManager.ACTION_TETHER_STATE_CHANGED)); + new IntentFilter(ACTION_TETHER_STATE_CHANGED)); + mTetheringDependencies.reset(); mTethering = new Tethering(mServiceContext, mNMService, mStatsService, mPolicyManager, mLooper.getLooper(), mSystemProperties, mTetheringDependencies); @@ -404,7 +421,7 @@ public class TetheringTest { private void verifyTetheringBroadcast(String ifname, String whichExtra) { // Verify that ifname is in the whichExtra array of the tether state changed broadcast. final Intent bcast = mIntents.get(0); - assertEquals(ConnectivityManager.ACTION_TETHER_STATE_CHANGED, bcast.getAction()); + assertEquals(ACTION_TETHER_STATE_CHANGED, bcast.getAction()); final ArrayList ifnames = bcast.getStringArrayListExtra(whichExtra); assertTrue(ifnames.contains(ifname)); mIntents.remove(bcast); @@ -412,8 +429,6 @@ public class TetheringTest { public void failingLocalOnlyHotspotLegacyApBroadcast( boolean emulateInterfaceStatusChanged) throws Exception { - when(mConnectivityManager.isTetheringSupported()).thenReturn(true); - // Emulate externally-visible WifiManager effects, causing the // per-interface state machine to start up, and telling us that // hotspot mode is to be started. @@ -427,16 +442,14 @@ public class TetheringTest { // then it creates a TetherInterfaceStateMachine and sends out a // broadcast indicating that the interface is "available". if (emulateInterfaceStatusChanged) { - verify(mConnectivityManager, atLeastOnce()).isTetheringSupported(); - verifyTetheringBroadcast(TEST_WLAN_IFNAME, ConnectivityManager.EXTRA_AVAILABLE_TETHER); + assertEquals(1, mTetheringDependencies.isTetheringSupportedCalls); + verifyTetheringBroadcast(TEST_WLAN_IFNAME, EXTRA_AVAILABLE_TETHER); } - verifyNoMoreInteractions(mConnectivityManager); verifyNoMoreInteractions(mNMService); verifyNoMoreInteractions(mWifiManager); } private void prepareUsbTethering(NetworkState upstreamState) { - when(mConnectivityManager.isTetheringSupported()).thenReturn(true); when(mUpstreamNetworkMonitor.selectPreferredUpstreamType(any())) .thenReturn(upstreamState); @@ -454,7 +467,6 @@ public class TetheringTest { prepareUsbTethering(upstreamState); // This should produce no activity of any kind. - verifyNoMoreInteractions(mConnectivityManager); verifyNoMoreInteractions(mNMService); // Pretend we then receive USB configured broadcast. @@ -481,8 +493,6 @@ public class TetheringTest { public void workingLocalOnlyHotspotEnrichedApBroadcast( boolean emulateInterfaceStatusChanged) throws Exception { - when(mConnectivityManager.isTetheringSupported()).thenReturn(true); - // Emulate externally-visible WifiManager effects, causing the // per-interface state machine to start up, and telling us that // hotspot mode is to be started. @@ -493,18 +503,17 @@ public class TetheringTest { mLooper.dispatchAll(); verifyInterfaceServingModeStarted(); - verifyTetheringBroadcast(TEST_WLAN_IFNAME, ConnectivityManager.EXTRA_AVAILABLE_TETHER); + verifyTetheringBroadcast(TEST_WLAN_IFNAME, EXTRA_AVAILABLE_TETHER); verify(mNMService, times(1)).setIpForwardingEnabled(true); verify(mNMService, times(1)).startTethering(any(String[].class)); verifyNoMoreInteractions(mNMService); verify(mWifiManager).updateInterfaceIpState( TEST_WLAN_IFNAME, WifiManager.IFACE_IP_MODE_LOCAL_ONLY); verifyNoMoreInteractions(mWifiManager); - verifyTetheringBroadcast(TEST_WLAN_IFNAME, ConnectivityManager.EXTRA_ACTIVE_LOCAL_ONLY); + verifyTetheringBroadcast(TEST_WLAN_IFNAME, EXTRA_ACTIVE_LOCAL_ONLY); verify(mUpstreamNetworkMonitor, times(1)).start(); // TODO: Figure out why this isn't exactly once, for sendTetherStateChangedBroadcast(). - verify(mConnectivityManager, atLeastOnce()).isTetheringSupported(); - verifyNoMoreInteractions(mConnectivityManager); + assertTrue(1 <= mTetheringDependencies.isTetheringSupportedCalls); // Emulate externally-visible WifiManager effects, when hotspot mode // is being torn down. @@ -523,8 +532,7 @@ public class TetheringTest { verifyNoMoreInteractions(mWifiManager); // Asking for the last error after the per-interface state machine // has been reaped yields an unknown interface error. - assertEquals(ConnectivityManager.TETHER_ERROR_UNKNOWN_IFACE, - mTethering.getLastTetherError(TEST_WLAN_IFNAME)); + assertEquals(TETHER_ERROR_UNKNOWN_IFACE, mTethering.getLastTetherError(TEST_WLAN_IFNAME)); } /** @@ -652,7 +660,6 @@ public class TetheringTest { // TODO: Test with and without interfaceStatusChanged(). @Test public void failingWifiTetheringLegacyApBroadcast() throws Exception { - when(mConnectivityManager.isTetheringSupported()).thenReturn(true); when(mWifiManager.startSoftAp(any(WifiConfiguration.class))).thenReturn(true); // Emulate pressing the WiFi tethering button. @@ -660,7 +667,6 @@ public class TetheringTest { mLooper.dispatchAll(); verify(mWifiManager, times(1)).startSoftAp(null); verifyNoMoreInteractions(mWifiManager); - verifyNoMoreInteractions(mConnectivityManager); verifyNoMoreInteractions(mNMService); // Emulate externally-visible WifiManager effects, causing the @@ -670,9 +676,8 @@ public class TetheringTest { sendWifiApStateChanged(WIFI_AP_STATE_ENABLED); mLooper.dispatchAll(); - verify(mConnectivityManager, atLeastOnce()).isTetheringSupported(); - verifyTetheringBroadcast(TEST_WLAN_IFNAME, ConnectivityManager.EXTRA_AVAILABLE_TETHER); - verifyNoMoreInteractions(mConnectivityManager); + assertEquals(1, mTetheringDependencies.isTetheringSupportedCalls); + verifyTetheringBroadcast(TEST_WLAN_IFNAME, EXTRA_AVAILABLE_TETHER); verifyNoMoreInteractions(mNMService); verifyNoMoreInteractions(mWifiManager); } @@ -680,7 +685,6 @@ public class TetheringTest { // TODO: Test with and without interfaceStatusChanged(). @Test public void workingWifiTetheringEnrichedApBroadcast() throws Exception { - when(mConnectivityManager.isTetheringSupported()).thenReturn(true); when(mWifiManager.startSoftAp(any(WifiConfiguration.class))).thenReturn(true); // Emulate pressing the WiFi tethering button. @@ -688,7 +692,6 @@ public class TetheringTest { mLooper.dispatchAll(); verify(mWifiManager, times(1)).startSoftAp(null); verifyNoMoreInteractions(mWifiManager); - verifyNoMoreInteractions(mConnectivityManager); verifyNoMoreInteractions(mNMService); // Emulate externally-visible WifiManager effects, causing the @@ -699,21 +702,20 @@ public class TetheringTest { mLooper.dispatchAll(); verifyInterfaceServingModeStarted(); - verifyTetheringBroadcast(TEST_WLAN_IFNAME, ConnectivityManager.EXTRA_AVAILABLE_TETHER); + verifyTetheringBroadcast(TEST_WLAN_IFNAME, EXTRA_AVAILABLE_TETHER); verify(mNMService, times(1)).setIpForwardingEnabled(true); verify(mNMService, times(1)).startTethering(any(String[].class)); verifyNoMoreInteractions(mNMService); verify(mWifiManager).updateInterfaceIpState( TEST_WLAN_IFNAME, WifiManager.IFACE_IP_MODE_TETHERED); verifyNoMoreInteractions(mWifiManager); - verifyTetheringBroadcast(TEST_WLAN_IFNAME, ConnectivityManager.EXTRA_ACTIVE_TETHER); + verifyTetheringBroadcast(TEST_WLAN_IFNAME, EXTRA_ACTIVE_TETHER); verify(mUpstreamNetworkMonitor, times(1)).start(); // In tethering mode, in the default configuration, an explicit request // for a mobile network is also made. verify(mUpstreamNetworkMonitor, times(1)).registerMobileNetworkRequest(); // TODO: Figure out why this isn't exactly once, for sendTetherStateChangedBroadcast(). - verify(mConnectivityManager, atLeastOnce()).isTetheringSupported(); - verifyNoMoreInteractions(mConnectivityManager); + assertTrue(1 <= mTetheringDependencies.isTetheringSupportedCalls); ///// // We do not currently emulate any upstream being found. @@ -727,7 +729,6 @@ public class TetheringTest { mLooper.dispatchAll(); verify(mWifiManager, times(1)).stopSoftAp(); verifyNoMoreInteractions(mWifiManager); - verifyNoMoreInteractions(mConnectivityManager); verifyNoMoreInteractions(mNMService); // Emulate externally-visible WifiManager effects, when tethering mode @@ -747,14 +748,12 @@ public class TetheringTest { verifyNoMoreInteractions(mWifiManager); // Asking for the last error after the per-interface state machine // has been reaped yields an unknown interface error. - assertEquals(ConnectivityManager.TETHER_ERROR_UNKNOWN_IFACE, - mTethering.getLastTetherError(TEST_WLAN_IFNAME)); + assertEquals(TETHER_ERROR_UNKNOWN_IFACE, mTethering.getLastTetherError(TEST_WLAN_IFNAME)); } // TODO: Test with and without interfaceStatusChanged(). @Test public void failureEnablingIpForwarding() throws Exception { - when(mConnectivityManager.isTetheringSupported()).thenReturn(true); when(mWifiManager.startSoftAp(any(WifiConfiguration.class))).thenReturn(true); doThrow(new RemoteException()).when(mNMService).setIpForwardingEnabled(true); @@ -763,7 +762,6 @@ public class TetheringTest { mLooper.dispatchAll(); verify(mWifiManager, times(1)).startSoftAp(null); verifyNoMoreInteractions(mWifiManager); - verifyNoMoreInteractions(mConnectivityManager); verifyNoMoreInteractions(mNMService); // Emulate externally-visible WifiManager effects, causing the @@ -782,8 +780,9 @@ public class TetheringTest { verify(mNMService, times(1)).tetherInterface(TEST_WLAN_IFNAME); verify(mWifiManager).updateInterfaceIpState( TEST_WLAN_IFNAME, WifiManager.IFACE_IP_MODE_TETHERED); - verify(mConnectivityManager, atLeastOnce()).isTetheringSupported(); - verifyTetheringBroadcast(TEST_WLAN_IFNAME, ConnectivityManager.EXTRA_AVAILABLE_TETHER); + // TODO: Figure out why this isn't exactly once, for sendTetherStateChangedBroadcast(). + assertTrue(1 <= mTetheringDependencies.isTetheringSupportedCalls); + verifyTetheringBroadcast(TEST_WLAN_IFNAME, EXTRA_AVAILABLE_TETHER); // This is called, but will throw. verify(mNMService, times(1)).setIpForwardingEnabled(true); // This never gets called because of the exception thrown above. @@ -796,7 +795,6 @@ public class TetheringTest { TEST_WLAN_IFNAME, WifiManager.IFACE_IP_MODE_CONFIGURATION_ERROR); verifyNoMoreInteractions(mWifiManager); - verifyNoMoreInteractions(mConnectivityManager); verifyNoMoreInteractions(mNMService); }