Merge "Replace ConnectivityManager as TetheringManager" into rvc-dev

This commit is contained in:
Mark Chien
2020-06-15 06:23:28 +00:00
committed by Android (Google) Code Review
3 changed files with 71 additions and 98 deletions

View File

@@ -16,13 +16,19 @@
package com.android.settings.network; 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.app.Activity;
import android.content.Intent; import android.content.Intent;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.content.res.Resources; import android.content.res.Resources;
import android.net.ConnectivityManager;
import android.net.TetheringConstants;
import android.net.TetheringManager;
import android.os.Bundle; import android.os.Bundle;
import android.os.ResultReceiver; import android.os.ResultReceiver;
import android.os.UserHandle; import android.os.UserHandle;
@@ -52,23 +58,19 @@ public class TetherProvisioningActivity extends Activity {
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
mResultReceiver = (ResultReceiver)getIntent().getParcelableExtra( mResultReceiver = (ResultReceiver) getIntent().getParcelableExtra(EXTRA_PROVISION_CALLBACK);
ConnectivityManager.EXTRA_PROVISION_CALLBACK);
final int tetherType = getIntent().getIntExtra(ConnectivityManager.EXTRA_ADD_TETHER_TYPE, final int tetherType = getIntent().getIntExtra(EXTRA_ADD_TETHER_TYPE, TETHERING_INVALID);
ConnectivityManager.TETHERING_INVALID);
final int tetherSubId = getIntent().getIntExtra(EXTRA_SUBID, final int tetherSubId = getIntent().getIntExtra(EXTRA_SUBID, INVALID_SUBSCRIPTION_ID);
SubscriptionManager.INVALID_SUBSCRIPTION_ID);
final int subId = SubscriptionManager.getActiveDataSubscriptionId(); final int subId = SubscriptionManager.getActiveDataSubscriptionId();
if (tetherSubId != subId) { if (tetherSubId != subId) {
Log.e(TAG, "This Provisioning request is outdated, current subId: " + 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(); finish();
return; return;
} }
String[] provisionApp = getIntent().getStringArrayExtra( String[] provisionApp = getIntent().getStringArrayExtra(EXTRA_RUN_PROVISION);
TetheringConstants.EXTRA_RUN_PROVISION);
if (provisionApp == null || provisionApp.length < 2) { if (provisionApp == null || provisionApp.length < 2) {
final Resources res = Utils.getResourcesForSubId(this, subId); final Resources res = Utils.getResourcesForSubId(this, subId);
provisionApp = res.getStringArray( provisionApp = res.getStringArray(
@@ -77,8 +79,8 @@ public class TetherProvisioningActivity extends Activity {
final Intent intent = new Intent(Intent.ACTION_MAIN); final Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setClassName(provisionApp[0], provisionApp[1]); intent.setClassName(provisionApp[0], provisionApp[1]);
intent.putExtra(EXTRA_TETHER_TYPE, tetherType); intent.putExtra(EXTRA_TETHER_TYPE, tetherType);
intent.putExtra(SubscriptionManager.EXTRA_SUBSCRIPTION_INDEX, subId); intent.putExtra(EXTRA_SUBSCRIPTION_INDEX, subId);
intent.putExtra(ConnectivityManager.EXTRA_PROVISION_CALLBACK, mResultReceiver); intent.putExtra(EXTRA_PROVISION_CALLBACK, mResultReceiver);
if (DEBUG) { if (DEBUG) {
Log.d(TAG, "Starting provisioning app: " + provisionApp[0] + "." + provisionApp[1]); Log.d(TAG, "Starting provisioning app: " + provisionApp[0] + "." + provisionApp[1]);
} }
@@ -86,7 +88,7 @@ public class TetherProvisioningActivity extends Activity {
if (getPackageManager().queryIntentActivities(intent, if (getPackageManager().queryIntentActivities(intent,
PackageManager.MATCH_DEFAULT_ONLY).isEmpty()) { PackageManager.MATCH_DEFAULT_ONLY).isEmpty()) {
Log.e(TAG, "Provisioning app is configured, but not available."); 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(); finish();
return; return;
} }
@@ -99,9 +101,8 @@ public class TetherProvisioningActivity extends Activity {
super.onActivityResult(requestCode, resultCode, intent); super.onActivityResult(requestCode, resultCode, intent);
if (requestCode == PROVISION_REQUEST) { if (requestCode == PROVISION_REQUEST) {
if (DEBUG) Log.d(TAG, "Got result from app: " + resultCode); if (DEBUG) Log.d(TAG, "Got result from app: " + resultCode);
int result = resultCode == Activity.RESULT_OK ? int result = resultCode == Activity.RESULT_OK
TetheringManager.TETHER_ERROR_NO_ERROR : ? TETHER_ERROR_NO_ERROR : TETHER_ERROR_PROVISIONING_FAILED;
TetheringManager.TETHER_ERROR_PROVISIONING_FAILED;
mResultReceiver.send(result, null); mResultReceiver.send(result, null);
finish(); finish();
} }

View File

@@ -16,13 +16,24 @@
package com.android.settings.wifi.tether; 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.Activity;
import android.app.Service; import android.app.Service;
import android.app.usage.UsageStatsManager; 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.BroadcastReceiver;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
@@ -31,7 +42,7 @@ import android.content.SharedPreferences;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo; import android.content.pm.ResolveInfo;
import android.content.res.Resources; import android.content.res.Resources;
import android.net.ConnectivityManager; import android.net.TetheringManager;
import android.os.IBinder; import android.os.IBinder;
import android.os.ResultReceiver; import android.os.ResultReceiver;
import android.telephony.SubscriptionManager; import android.telephony.SubscriptionManager;
@@ -89,17 +100,16 @@ public class TetherService extends Service {
mCurrentTethers = stringToTethers(prefs.getString(KEY_TETHERS, "")); mCurrentTethers = stringToTethers(prefs.getString(KEY_TETHERS, ""));
mCurrentTypeIndex = 0; mCurrentTypeIndex = 0;
mPendingCallbacks = new ArrayMap<>(3); mPendingCallbacks = new ArrayMap<>(3);
mPendingCallbacks.put(ConnectivityManager.TETHERING_WIFI, new ArrayList<ResultReceiver>()); mPendingCallbacks.put(TETHERING_WIFI, new ArrayList<ResultReceiver>());
mPendingCallbacks.put(ConnectivityManager.TETHERING_USB, new ArrayList<ResultReceiver>()); mPendingCallbacks.put(TETHERING_USB, new ArrayList<ResultReceiver>());
mPendingCallbacks.put( mPendingCallbacks.put(TETHERING_BLUETOOTH, new ArrayList<ResultReceiver>());
ConnectivityManager.TETHERING_BLUETOOTH, new ArrayList<ResultReceiver>()); mPendingCallbacks.put(TETHERING_ETHERNET, new ArrayList<ResultReceiver>());
} }
@Override @Override
public int onStartCommand(Intent intent, int flags, int startId) { public int onStartCommand(Intent intent, int flags, int startId) {
if (intent.hasExtra(EXTRA_SUBID)) { if (intent.hasExtra(EXTRA_SUBID)) {
final int tetherSubId = intent.getIntExtra(EXTRA_SUBID, final int tetherSubId = intent.getIntExtra(EXTRA_SUBID, INVALID_SUBSCRIPTION_ID);
SubscriptionManager.INVALID_SUBSCRIPTION_ID);
final int subId = getTetherServiceWrapper().getActiveDataSubscriptionId(); final int subId = getTetherServiceWrapper().getActiveDataSubscriptionId();
if (tetherSubId != subId) { if (tetherSubId != subId) {
Log.e(TAG, "This Provisioning request is outdated, current subId: " + subId); Log.e(TAG, "This Provisioning request is outdated, current subId: " + subId);
@@ -109,18 +119,16 @@ public class TetherService extends Service {
return START_NOT_STICKY; return START_NOT_STICKY;
} }
} }
if (intent.hasExtra(ConnectivityManager.EXTRA_ADD_TETHER_TYPE)) { if (intent.hasExtra(EXTRA_ADD_TETHER_TYPE)) {
int type = intent.getIntExtra(ConnectivityManager.EXTRA_ADD_TETHER_TYPE, int type = intent.getIntExtra(EXTRA_ADD_TETHER_TYPE, TETHERING_INVALID);
ConnectivityManager.TETHERING_INVALID); ResultReceiver callback = intent.getParcelableExtra(EXTRA_PROVISION_CALLBACK);
ResultReceiver callback =
intent.getParcelableExtra(ConnectivityManager.EXTRA_PROVISION_CALLBACK);
if (callback != null) { if (callback != null) {
List<ResultReceiver> callbacksForType = mPendingCallbacks.get(type); List<ResultReceiver> callbacksForType = mPendingCallbacks.get(type);
if (callbacksForType != null) { if (callbacksForType != null) {
callbacksForType.add(callback); callbacksForType.add(callback);
} else { } else {
// Invalid tether type. Just ignore this request and report failure. // 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(); stopSelf();
return START_NOT_STICKY; return START_NOT_STICKY;
} }
@@ -132,10 +140,9 @@ public class TetherService extends Service {
} }
} }
if (intent.hasExtra(ConnectivityManager.EXTRA_REM_TETHER_TYPE)) { if (intent.hasExtra(EXTRA_REM_TETHER_TYPE)) {
if (!mInProvisionCheck) { if (!mInProvisionCheck) {
int type = intent.getIntExtra(ConnectivityManager.EXTRA_REM_TETHER_TYPE, int type = intent.getIntExtra(EXTRA_REM_TETHER_TYPE, TETHERING_INVALID);
ConnectivityManager.TETHERING_INVALID);
int index = mCurrentTethers.indexOf(type); int index = mCurrentTethers.indexOf(type);
if (DEBUG) Log.d(TAG, "Removing tether " + type + ", index " + index); if (DEBUG) Log.d(TAG, "Removing tether " + type + ", index " + index);
if (index >= 0) { if (index >= 0) {
@@ -146,7 +153,7 @@ public class TetherService extends Service {
} }
} }
if (intent.getBooleanExtra(ConnectivityManager.EXTRA_RUN_PROVISION, false)) { if (intent.getBooleanExtra(EXTRA_RUN_PROVISION, false)) {
startProvisioning(mCurrentTypeIndex); startProvisioning(mCurrentTypeIndex);
} else if (!mInProvisionCheck) { } else if (!mInProvisionCheck) {
// If we aren't running any provisioning, no reason to stay alive. // If we aren't running any provisioning, no reason to stay alive.
@@ -207,32 +214,9 @@ public class TetherService extends Service {
return buffer.toString(); return buffer.toString();
} }
private void disableWifiTethering() { private void disableTethering(final int tetheringType) {
ConnectivityManager cm = final TetheringManager tm = (TetheringManager) getSystemService(Context.TETHERING_SERVICE);
(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE); tm.stopTethering(tetheringType);
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 startProvisioning(int index) { private void startProvisioning(int index) {
@@ -255,7 +239,7 @@ public class TetherService extends Service {
Intent intent = new Intent(provisionAction); Intent intent = new Intent(provisionAction);
int type = mCurrentTethers.get(index); int type = mCurrentTethers.get(index);
intent.putExtra(TETHER_CHOICE, type); 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.setFlags(Intent.FLAG_RECEIVER_FOREGROUND
| Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND); | Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND);
@@ -285,8 +269,8 @@ public class TetherService extends Service {
if (callbacksForType == null) { if (callbacksForType == null) {
return; return;
} }
int errorCode = result == RESULT_OK ? ConnectivityManager.TETHER_ERROR_NO_ERROR : int errorCode = result == RESULT_OK ? TETHER_ERROR_NO_ERROR :
ConnectivityManager.TETHER_ERROR_PROVISION_FAILED; TETHER_ERROR_PROVISIONING_FAILED;
for (ResultReceiver callback : callbacksForType) { for (ResultReceiver callback : callbacksForType) {
if (DEBUG) Log.d(TAG, "Firing result: " + errorCode + " to callback"); if (DEBUG) Log.d(TAG, "Firing result: " + errorCode + " to callback");
callback.send(errorCode, null); callback.send(errorCode, null);
@@ -309,19 +293,7 @@ public class TetherService extends Service {
int checkType = mCurrentTethers.get(mCurrentTypeIndex); int checkType = mCurrentTethers.get(mCurrentTypeIndex);
mInProvisionCheck = false; mInProvisionCheck = false;
int result = intent.getIntExtra(EXTRA_RESULT, RESULT_DEFAULT); int result = intent.getIntExtra(EXTRA_RESULT, RESULT_DEFAULT);
if (result != RESULT_OK) { if (result != RESULT_OK) disableTethering(checkType);
switch (checkType) {
case ConnectivityManager.TETHERING_WIFI:
disableWifiTethering();
break;
case ConnectivityManager.TETHERING_BLUETOOTH:
disableBtTethering();
break;
case ConnectivityManager.TETHERING_USB:
disableUsbTethering();
break;
}
}
fireCallbacksForType(checkType, result); fireCallbacksForType(checkType, result);
if (++mCurrentTypeIndex >= mCurrentTethers.size()) { if (++mCurrentTypeIndex >= mCurrentTethers.size()) {

View File

@@ -16,15 +16,15 @@
package com.android.settings.wifi.tether; package com.android.settings.wifi.tether;
import static android.net.ConnectivityManager.EXTRA_ADD_TETHER_TYPE; import static android.net.TetheringConstants.EXTRA_ADD_TETHER_TYPE;
import static android.net.ConnectivityManager.EXTRA_PROVISION_CALLBACK; import static android.net.TetheringConstants.EXTRA_PROVISION_CALLBACK;
import static android.net.ConnectivityManager.EXTRA_RUN_PROVISION; import static android.net.TetheringConstants.EXTRA_RUN_PROVISION;
import static android.net.ConnectivityManager.TETHERING_BLUETOOTH; import static android.net.TetheringManager.TETHERING_BLUETOOTH;
import static android.net.ConnectivityManager.TETHERING_INVALID; import static android.net.TetheringManager.TETHERING_INVALID;
import static android.net.ConnectivityManager.TETHERING_USB; import static android.net.TetheringManager.TETHERING_USB;
import static android.net.ConnectivityManager.TETHERING_WIFI; import static android.net.TetheringManager.TETHERING_WIFI;
import static android.net.ConnectivityManager.TETHER_ERROR_NO_ERROR; import static android.net.TetheringManager.TETHER_ERROR_NO_ERROR;
import static android.net.ConnectivityManager.TETHER_ERROR_PROVISION_FAILED; import static android.net.TetheringManager.TETHER_ERROR_PROVISIONING_FAILED;
import static android.telephony.SubscriptionManager.INVALID_SUBSCRIPTION_ID; import static android.telephony.SubscriptionManager.INVALID_SUBSCRIPTION_ID;
import static org.mockito.Matchers.any; import static org.mockito.Matchers.any;
@@ -47,7 +47,7 @@ import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo; import android.content.pm.ResolveInfo;
import android.content.res.Resources; import android.content.res.Resources;
import android.net.ConnectivityManager; import android.net.TetheringManager;
import android.net.wifi.WifiManager; import android.net.wifi.WifiManager;
import android.os.Bundle; import android.os.Bundle;
import android.os.ResultReceiver; import android.os.ResultReceiver;
@@ -88,7 +88,7 @@ public class TetherServiceTest extends ServiceTestCase<TetherService> {
private ProvisionReceiver mProvisionReceiver; private ProvisionReceiver mProvisionReceiver;
private Receiver mResultReceiver; private Receiver mResultReceiver;
@Mock private ConnectivityManager mConnectivityManager; @Mock private TetheringManager mTetheringManager;
@Mock private PackageManager mPackageManager; @Mock private PackageManager mPackageManager;
@Mock private WifiManager mWifiManager; @Mock private WifiManager mWifiManager;
@Mock private SharedPreferences mPrefs; @Mock private SharedPreferences mPrefs;
@@ -208,9 +208,9 @@ public class TetherServiceTest extends ServiceTestCase<TetherService> {
runProvisioningForType(TETHERING_WIFI); runProvisioningForType(TETHERING_WIFI);
assertTrue(waitForProvisionRequest(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() { public void testFailureStopsTethering_Usb() {
@@ -219,9 +219,9 @@ public class TetherServiceTest extends ServiceTestCase<TetherService> {
runProvisioningForType(TETHERING_USB); runProvisioningForType(TETHERING_USB);
assertTrue(waitForProvisionRequest(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() { public void testIgnoreOutdatedRequest() {
@@ -345,8 +345,8 @@ public class TetherServiceTest extends ServiceTestCase<TetherService> {
@Override @Override
public Object getSystemService(String name) { public Object getSystemService(String name) {
if (CONNECTIVITY_SERVICE.equals(name)) { if (TETHERING_SERVICE.equals(name)) {
return mConnectivityManager; return mTetheringManager;
} else if (WIFI_SERVICE.equals(name)) { } else if (WIFI_SERVICE.equals(name)) {
return mWifiManager; return mWifiManager;
} }