Replace ConnectivityManager as TetheringManager
Tethering APIs are all move to TetheringManager from Android R.
1. Replace ConnectivityManager tethering API usage as TetheringManager.
2. Use TetheringManager#stopTethering to disable usb tethering instead
of using deprecated ConnectivityService#setUsbTethering
3. Use TetheringManager#stopTethering to disable bluetooth tethering
instead of directly use BluetoothPan#setBlueoothTethering. So bluetooth
getProfileProxy is not needed in TetherService because tethering would
do that when calling #stopTethering.
4. Also support TETHERING_ETHERNET entitlement check that
TETHERING_ETHERNET is new added from Android R.
Bug: 146918263
Test: atest TetherServiceTest
Change-Id: Id969f29d7210f2ee32719c76439049bbc86cd4f6
Merged-In: Id969f29d7210f2ee32719c76439049bbc86cd4f6
diff --git a/src/com/android/settings/network/TetherProvisioningActivity.java b/src/com/android/settings/network/TetherProvisioningActivity.java
index 09efe48..bb61546 100644
--- a/src/com/android/settings/network/TetherProvisioningActivity.java
+++ b/src/com/android/settings/network/TetherProvisioningActivity.java
@@ -16,13 +16,19 @@
package com.android.settings.network;
+import static android.net.TetheringConstants.EXTRA_ADD_TETHER_TYPE;
+import static android.net.TetheringConstants.EXTRA_PROVISION_CALLBACK;
+import static android.net.TetheringConstants.EXTRA_RUN_PROVISION;
+import static android.net.TetheringManager.TETHERING_INVALID;
+import static android.net.TetheringManager.TETHER_ERROR_NO_ERROR;
+import static android.net.TetheringManager.TETHER_ERROR_PROVISIONING_FAILED;
+import static android.telephony.SubscriptionManager.EXTRA_SUBSCRIPTION_INDEX;
+import static android.telephony.SubscriptionManager.INVALID_SUBSCRIPTION_ID;
+
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.res.Resources;
-import android.net.ConnectivityManager;
-import android.net.TetheringConstants;
-import android.net.TetheringManager;
import android.os.Bundle;
import android.os.ResultReceiver;
import android.os.UserHandle;
@@ -52,23 +58,19 @@
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
- mResultReceiver = (ResultReceiver)getIntent().getParcelableExtra(
- ConnectivityManager.EXTRA_PROVISION_CALLBACK);
+ mResultReceiver = (ResultReceiver) getIntent().getParcelableExtra(EXTRA_PROVISION_CALLBACK);
- final int tetherType = getIntent().getIntExtra(ConnectivityManager.EXTRA_ADD_TETHER_TYPE,
- ConnectivityManager.TETHERING_INVALID);
+ final int tetherType = getIntent().getIntExtra(EXTRA_ADD_TETHER_TYPE, TETHERING_INVALID);
- final int tetherSubId = getIntent().getIntExtra(EXTRA_SUBID,
- SubscriptionManager.INVALID_SUBSCRIPTION_ID);
+ final int tetherSubId = getIntent().getIntExtra(EXTRA_SUBID, INVALID_SUBSCRIPTION_ID);
final int subId = SubscriptionManager.getActiveDataSubscriptionId();
if (tetherSubId != subId) {
Log.e(TAG, "This Provisioning request is outdated, current subId: " + subId);
- mResultReceiver.send(TetheringManager.TETHER_ERROR_PROVISIONING_FAILED, null);
+ mResultReceiver.send(TETHER_ERROR_PROVISIONING_FAILED, null);
finish();
return;
}
- String[] provisionApp = getIntent().getStringArrayExtra(
- TetheringConstants.EXTRA_RUN_PROVISION);
+ String[] provisionApp = getIntent().getStringArrayExtra(EXTRA_RUN_PROVISION);
if (provisionApp == null || provisionApp.length < 2) {
final Resources res = Utils.getResourcesForSubId(this, subId);
provisionApp = res.getStringArray(
@@ -77,8 +79,8 @@
final Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setClassName(provisionApp[0], provisionApp[1]);
intent.putExtra(EXTRA_TETHER_TYPE, tetherType);
- intent.putExtra(SubscriptionManager.EXTRA_SUBSCRIPTION_INDEX, subId);
- intent.putExtra(ConnectivityManager.EXTRA_PROVISION_CALLBACK, mResultReceiver);
+ intent.putExtra(EXTRA_SUBSCRIPTION_INDEX, subId);
+ intent.putExtra(EXTRA_PROVISION_CALLBACK, mResultReceiver);
if (DEBUG) {
Log.d(TAG, "Starting provisioning app: " + provisionApp[0] + "." + provisionApp[1]);
}
@@ -86,7 +88,7 @@
if (getPackageManager().queryIntentActivities(intent,
PackageManager.MATCH_DEFAULT_ONLY).isEmpty()) {
Log.e(TAG, "Provisioning app is configured, but not available.");
- mResultReceiver.send(TetheringManager.TETHER_ERROR_PROVISIONING_FAILED, null);
+ mResultReceiver.send(TETHER_ERROR_PROVISIONING_FAILED, null);
finish();
return;
}
@@ -99,9 +101,8 @@
super.onActivityResult(requestCode, resultCode, intent);
if (requestCode == PROVISION_REQUEST) {
if (DEBUG) Log.d(TAG, "Got result from app: " + resultCode);
- int result = resultCode == Activity.RESULT_OK ?
- TetheringManager.TETHER_ERROR_NO_ERROR :
- TetheringManager.TETHER_ERROR_PROVISIONING_FAILED;
+ int result = resultCode == Activity.RESULT_OK
+ ? TETHER_ERROR_NO_ERROR : TETHER_ERROR_PROVISIONING_FAILED;
mResultReceiver.send(result, null);
finish();
}
diff --git a/src/com/android/settings/wifi/tether/TetherService.java b/src/com/android/settings/wifi/tether/TetherService.java
index 7252960..72ea1a9 100644
--- a/src/com/android/settings/wifi/tether/TetherService.java
+++ b/src/com/android/settings/wifi/tether/TetherService.java
@@ -16,13 +16,24 @@
package com.android.settings.wifi.tether;
+import static android.net.TetheringConstants.EXTRA_ADD_TETHER_TYPE;
+import static android.net.TetheringConstants.EXTRA_PROVISION_CALLBACK;
+import static android.net.TetheringConstants.EXTRA_REM_TETHER_TYPE;
+import static android.net.TetheringConstants.EXTRA_RUN_PROVISION;
+import static android.net.TetheringManager.TETHERING_BLUETOOTH;
+import static android.net.TetheringManager.TETHERING_ETHERNET;
+import static android.net.TetheringManager.TETHERING_INVALID;
+import static android.net.TetheringManager.TETHERING_USB;
+import static android.net.TetheringManager.TETHERING_WIFI;
+import static android.net.TetheringManager.TETHER_ERROR_NO_ERROR;
+import static android.net.TetheringManager.TETHER_ERROR_PROVISIONING_FAILED;
+import static android.net.TetheringManager.TETHER_ERROR_UNKNOWN_IFACE;
+import static android.telephony.SubscriptionManager.EXTRA_SUBSCRIPTION_INDEX;
+import static android.telephony.SubscriptionManager.INVALID_SUBSCRIPTION_ID;
+
import android.app.Activity;
import android.app.Service;
import android.app.usage.UsageStatsManager;
-import android.bluetooth.BluetoothAdapter;
-import android.bluetooth.BluetoothPan;
-import android.bluetooth.BluetoothProfile;
-import android.bluetooth.BluetoothProfile.ServiceListener;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
@@ -31,7 +42,7 @@
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.content.res.Resources;
-import android.net.ConnectivityManager;
+import android.net.TetheringManager;
import android.os.IBinder;
import android.os.ResultReceiver;
import android.telephony.SubscriptionManager;
@@ -89,17 +100,16 @@
mCurrentTethers = stringToTethers(prefs.getString(KEY_TETHERS, ""));
mCurrentTypeIndex = 0;
mPendingCallbacks = new ArrayMap<>(3);
- mPendingCallbacks.put(ConnectivityManager.TETHERING_WIFI, new ArrayList<ResultReceiver>());
- mPendingCallbacks.put(ConnectivityManager.TETHERING_USB, new ArrayList<ResultReceiver>());
- mPendingCallbacks.put(
- ConnectivityManager.TETHERING_BLUETOOTH, new ArrayList<ResultReceiver>());
+ mPendingCallbacks.put(TETHERING_WIFI, new ArrayList<ResultReceiver>());
+ mPendingCallbacks.put(TETHERING_USB, new ArrayList<ResultReceiver>());
+ mPendingCallbacks.put(TETHERING_BLUETOOTH, new ArrayList<ResultReceiver>());
+ mPendingCallbacks.put(TETHERING_ETHERNET, new ArrayList<ResultReceiver>());
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (intent.hasExtra(EXTRA_SUBID)) {
- final int tetherSubId = intent.getIntExtra(EXTRA_SUBID,
- SubscriptionManager.INVALID_SUBSCRIPTION_ID);
+ final int tetherSubId = intent.getIntExtra(EXTRA_SUBID, INVALID_SUBSCRIPTION_ID);
final int subId = getTetherServiceWrapper().getActiveDataSubscriptionId();
if (tetherSubId != subId) {
Log.e(TAG, "This Provisioning request is outdated, current subId: " + subId);
@@ -109,18 +119,16 @@
return START_NOT_STICKY;
}
}
- if (intent.hasExtra(ConnectivityManager.EXTRA_ADD_TETHER_TYPE)) {
- int type = intent.getIntExtra(ConnectivityManager.EXTRA_ADD_TETHER_TYPE,
- ConnectivityManager.TETHERING_INVALID);
- ResultReceiver callback =
- intent.getParcelableExtra(ConnectivityManager.EXTRA_PROVISION_CALLBACK);
+ if (intent.hasExtra(EXTRA_ADD_TETHER_TYPE)) {
+ int type = intent.getIntExtra(EXTRA_ADD_TETHER_TYPE, TETHERING_INVALID);
+ ResultReceiver callback = intent.getParcelableExtra(EXTRA_PROVISION_CALLBACK);
if (callback != null) {
List<ResultReceiver> callbacksForType = mPendingCallbacks.get(type);
if (callbacksForType != null) {
callbacksForType.add(callback);
} else {
// Invalid tether type. Just ignore this request and report failure.
- callback.send(ConnectivityManager.TETHER_ERROR_UNKNOWN_IFACE, null);
+ callback.send(TETHER_ERROR_UNKNOWN_IFACE, null);
stopSelf();
return START_NOT_STICKY;
}
@@ -132,10 +140,9 @@
}
}
- if (intent.hasExtra(ConnectivityManager.EXTRA_REM_TETHER_TYPE)) {
+ if (intent.hasExtra(EXTRA_REM_TETHER_TYPE)) {
if (!mInProvisionCheck) {
- int type = intent.getIntExtra(ConnectivityManager.EXTRA_REM_TETHER_TYPE,
- ConnectivityManager.TETHERING_INVALID);
+ int type = intent.getIntExtra(EXTRA_REM_TETHER_TYPE, TETHERING_INVALID);
int index = mCurrentTethers.indexOf(type);
if (DEBUG) Log.d(TAG, "Removing tether " + type + ", index " + index);
if (index >= 0) {
@@ -146,7 +153,7 @@
}
}
- if (intent.getBooleanExtra(ConnectivityManager.EXTRA_RUN_PROVISION, false)) {
+ if (intent.getBooleanExtra(EXTRA_RUN_PROVISION, false)) {
startProvisioning(mCurrentTypeIndex);
} else if (!mInProvisionCheck) {
// If we aren't running any provisioning, no reason to stay alive.
@@ -207,32 +214,9 @@
return buffer.toString();
}
- private void disableWifiTethering() {
- ConnectivityManager cm =
- (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
- cm.stopTethering(ConnectivityManager.TETHERING_WIFI);
- }
-
- private void disableUsbTethering() {
- ConnectivityManager cm =
- (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
- cm.setUsbTethering(false);
- }
-
- private void disableBtTethering() {
- final BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
- if (adapter != null) {
- adapter.getProfileProxy(this, new ServiceListener() {
- @Override
- public void onServiceDisconnected(int profile) { }
-
- @Override
- public void onServiceConnected(int profile, BluetoothProfile proxy) {
- ((BluetoothPan) proxy).setBluetoothTethering(false);
- adapter.closeProfileProxy(BluetoothProfile.PAN, proxy);
- }
- }, BluetoothProfile.PAN);
- }
+ private void disableTethering(final int tetheringType) {
+ final TetheringManager tm = (TetheringManager) getSystemService(Context.TETHERING_SERVICE);
+ tm.stopTethering(tetheringType);
}
private void startProvisioning(int index) {
@@ -255,7 +239,7 @@
Intent intent = new Intent(provisionAction);
int type = mCurrentTethers.get(index);
intent.putExtra(TETHER_CHOICE, type);
- intent.putExtra(SubscriptionManager.EXTRA_SUBSCRIPTION_INDEX, subId);
+ intent.putExtra(EXTRA_SUBSCRIPTION_INDEX, subId);
intent.setFlags(Intent.FLAG_RECEIVER_FOREGROUND
| Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND);
@@ -285,8 +269,8 @@
if (callbacksForType == null) {
return;
}
- int errorCode = result == RESULT_OK ? ConnectivityManager.TETHER_ERROR_NO_ERROR :
- ConnectivityManager.TETHER_ERROR_PROVISION_FAILED;
+ int errorCode = result == RESULT_OK ? TETHER_ERROR_NO_ERROR :
+ TETHER_ERROR_PROVISIONING_FAILED;
for (ResultReceiver callback : callbacksForType) {
if (DEBUG) Log.d(TAG, "Firing result: " + errorCode + " to callback");
callback.send(errorCode, null);
@@ -309,19 +293,7 @@
int checkType = mCurrentTethers.get(mCurrentTypeIndex);
mInProvisionCheck = false;
int result = intent.getIntExtra(EXTRA_RESULT, RESULT_DEFAULT);
- if (result != RESULT_OK) {
- switch (checkType) {
- case ConnectivityManager.TETHERING_WIFI:
- disableWifiTethering();
- break;
- case ConnectivityManager.TETHERING_BLUETOOTH:
- disableBtTethering();
- break;
- case ConnectivityManager.TETHERING_USB:
- disableUsbTethering();
- break;
- }
- }
+ if (result != RESULT_OK) disableTethering(checkType);
fireCallbacksForType(checkType, result);
if (++mCurrentTypeIndex >= mCurrentTethers.size()) {
diff --git a/tests/unit/src/com/android/settings/wifi/tether/TetherServiceTest.java b/tests/unit/src/com/android/settings/wifi/tether/TetherServiceTest.java
index 514755b..19f29c0 100644
--- a/tests/unit/src/com/android/settings/wifi/tether/TetherServiceTest.java
+++ b/tests/unit/src/com/android/settings/wifi/tether/TetherServiceTest.java
@@ -16,15 +16,15 @@
package com.android.settings.wifi.tether;
-import static android.net.ConnectivityManager.EXTRA_ADD_TETHER_TYPE;
-import static android.net.ConnectivityManager.EXTRA_PROVISION_CALLBACK;
-import static android.net.ConnectivityManager.EXTRA_RUN_PROVISION;
-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.ConnectivityManager.TETHER_ERROR_NO_ERROR;
-import static android.net.ConnectivityManager.TETHER_ERROR_PROVISION_FAILED;
+import static android.net.TetheringConstants.EXTRA_ADD_TETHER_TYPE;
+import static android.net.TetheringConstants.EXTRA_PROVISION_CALLBACK;
+import static android.net.TetheringConstants.EXTRA_RUN_PROVISION;
+import static android.net.TetheringManager.TETHERING_BLUETOOTH;
+import static android.net.TetheringManager.TETHERING_INVALID;
+import static android.net.TetheringManager.TETHERING_USB;
+import static android.net.TetheringManager.TETHERING_WIFI;
+import static android.net.TetheringManager.TETHER_ERROR_NO_ERROR;
+import static android.net.TetheringManager.TETHER_ERROR_PROVISIONING_FAILED;
import static android.telephony.SubscriptionManager.INVALID_SUBSCRIPTION_ID;
import static org.mockito.Matchers.any;
@@ -47,7 +47,7 @@
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.content.res.Resources;
-import android.net.ConnectivityManager;
+import android.net.TetheringManager;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.ResultReceiver;
@@ -88,7 +88,7 @@
private ProvisionReceiver mProvisionReceiver;
private Receiver mResultReceiver;
- @Mock private ConnectivityManager mConnectivityManager;
+ @Mock private TetheringManager mTetheringManager;
@Mock private PackageManager mPackageManager;
@Mock private WifiManager mWifiManager;
@Mock private SharedPreferences mPrefs;
@@ -208,9 +208,9 @@
runProvisioningForType(TETHERING_WIFI);
assertTrue(waitForProvisionRequest(TETHERING_WIFI));
- assertTrue(waitForProvisionResponse(TETHER_ERROR_PROVISION_FAILED));
+ assertTrue(waitForProvisionResponse(TETHER_ERROR_PROVISIONING_FAILED));
- verify(mConnectivityManager).stopTethering(ConnectivityManager.TETHERING_WIFI);
+ verify(mTetheringManager).stopTethering(TETHERING_WIFI);
}
public void testFailureStopsTethering_Usb() {
@@ -219,9 +219,9 @@
runProvisioningForType(TETHERING_USB);
assertTrue(waitForProvisionRequest(TETHERING_USB));
- assertTrue(waitForProvisionResponse(TETHER_ERROR_PROVISION_FAILED));
+ assertTrue(waitForProvisionResponse(TETHER_ERROR_PROVISIONING_FAILED));
- verify(mConnectivityManager).setUsbTethering(eq(false));
+ verify(mTetheringManager).stopTethering(TETHERING_USB);
}
public void testIgnoreOutdatedRequest() {
@@ -345,8 +345,8 @@
@Override
public Object getSystemService(String name) {
- if (CONNECTIVITY_SERVICE.equals(name)) {
- return mConnectivityManager;
+ if (TETHERING_SERVICE.equals(name)) {
+ return mTetheringManager;
} else if (WIFI_SERVICE.equals(name)) {
return mWifiManager;
}