Add entitlement check for usb tethering
Before this CL, usb tethering in detail page doesn't have entitlement check. This CL fix it by using the correct API Also polish it by updating the UI before entitlement check to align hotspot page. So in UI it will first check "usb tethering". If entitlement check fail, it will revoke and go back to previous selection. Bug: 115707279 Test: RunSettingsRoboTests Change-Id: I3d2ebad2879479a870bcdfe596bb88b83c424389
This commit is contained in:
@@ -16,9 +16,14 @@
|
||||
|
||||
package com.android.settings.connecteddevice.usb;
|
||||
|
||||
import static android.net.ConnectivityManager.TETHERING_USB;
|
||||
|
||||
import android.content.Context;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.hardware.usb.UsbManager;
|
||||
import android.net.ConnectivityManager;
|
||||
import android.os.Bundle;
|
||||
import android.util.Log;
|
||||
|
||||
import androidx.annotation.VisibleForTesting;
|
||||
|
||||
@@ -40,11 +45,18 @@ import java.util.List;
|
||||
public class UsbDefaultFragment extends RadioButtonPickerFragment {
|
||||
@VisibleForTesting
|
||||
UsbBackend mUsbBackend;
|
||||
@VisibleForTesting
|
||||
ConnectivityManager mConnectivityManager;
|
||||
@VisibleForTesting
|
||||
OnStartTetheringCallback mOnStartTetheringCallback = new OnStartTetheringCallback();
|
||||
@VisibleForTesting
|
||||
long mPreviousFunctions;
|
||||
|
||||
@Override
|
||||
public void onAttach(Context context) {
|
||||
super.onAttach(context);
|
||||
mUsbBackend = new UsbBackend(context);
|
||||
mConnectivityManager = context.getSystemService(ConnectivityManager.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -105,9 +117,37 @@ public class UsbDefaultFragment extends RadioButtonPickerFragment {
|
||||
@Override
|
||||
protected boolean setDefaultKey(String key) {
|
||||
long functions = UsbBackend.usbFunctionsFromString(key);
|
||||
mPreviousFunctions = mUsbBackend.getCurrentFunctions();
|
||||
if (!Utils.isMonkeyRunning()) {
|
||||
mUsbBackend.setDefaultUsbFunctions(functions);
|
||||
if (functions == UsbManager.FUNCTION_RNDIS) {
|
||||
// We need to have entitlement check for usb tethering, so use API in
|
||||
// ConnectivityManager.
|
||||
mConnectivityManager.startTethering(TETHERING_USB, true /* showProvisioningUi */,
|
||||
mOnStartTetheringCallback);
|
||||
} else {
|
||||
mUsbBackend.setDefaultUsbFunctions(functions);
|
||||
}
|
||||
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
final class OnStartTetheringCallback extends
|
||||
ConnectivityManager.OnStartTetheringCallback {
|
||||
|
||||
@Override
|
||||
public void onTetheringStarted() {
|
||||
super.onTetheringStarted();
|
||||
// Set default usb functions again to make internal data persistent
|
||||
mUsbBackend.setDefaultUsbFunctions(UsbManager.FUNCTION_RNDIS);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTetheringFailed() {
|
||||
super.onTetheringFailed();
|
||||
mUsbBackend.setDefaultUsbFunctions(mPreviousFunctions);
|
||||
updateCandidates();
|
||||
}
|
||||
}
|
||||
}
|
@@ -16,10 +16,14 @@
|
||||
|
||||
package com.android.settings.connecteddevice.usb;
|
||||
|
||||
import static android.net.ConnectivityManager.TETHERING_USB;
|
||||
|
||||
import android.content.Context;
|
||||
import android.hardware.usb.UsbManager;
|
||||
import android.hardware.usb.UsbPort;
|
||||
import android.net.ConnectivityManager;
|
||||
|
||||
import androidx.annotation.VisibleForTesting;
|
||||
import androidx.preference.PreferenceCategory;
|
||||
import androidx.preference.PreferenceScreen;
|
||||
|
||||
@@ -47,10 +51,18 @@ public class UsbDetailsFunctionsController extends UsbDetailsController
|
||||
}
|
||||
|
||||
private PreferenceCategory mProfilesContainer;
|
||||
private ConnectivityManager mConnectivityManager;
|
||||
@VisibleForTesting
|
||||
OnStartTetheringCallback mOnStartTetheringCallback;
|
||||
@VisibleForTesting
|
||||
long mPreviousFunction;
|
||||
|
||||
public UsbDetailsFunctionsController(Context context, UsbDetailsFragment fragment,
|
||||
UsbBackend backend) {
|
||||
super(context, fragment, backend);
|
||||
mConnectivityManager = context.getSystemService(ConnectivityManager.class);
|
||||
mOnStartTetheringCallback = new OnStartTetheringCallback();
|
||||
mPreviousFunction = mUsbBackend.getCurrentFunctions();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -97,9 +109,28 @@ public class UsbDetailsFunctionsController extends UsbDetailsController
|
||||
|
||||
@Override
|
||||
public void onRadioButtonClicked(RadioButtonPreference preference) {
|
||||
long function = UsbBackend.usbFunctionsFromString(preference.getKey());
|
||||
if (function != mUsbBackend.getCurrentFunctions() && !Utils.isMonkeyRunning()) {
|
||||
mUsbBackend.setCurrentFunctions(function);
|
||||
final long function = UsbBackend.usbFunctionsFromString(preference.getKey());
|
||||
final long previousFunction = mUsbBackend.getCurrentFunctions();
|
||||
if (function != previousFunction && !Utils.isMonkeyRunning()) {
|
||||
mPreviousFunction = previousFunction;
|
||||
|
||||
if (function == UsbManager.FUNCTION_RNDIS) {
|
||||
//Update the UI in advance to make it looks smooth
|
||||
final RadioButtonPreference prevPref =
|
||||
(RadioButtonPreference) mProfilesContainer.findPreference(
|
||||
UsbBackend.usbFunctionsToString(mPreviousFunction));
|
||||
if (prevPref != null) {
|
||||
prevPref.setChecked(false);
|
||||
preference.setChecked(true);
|
||||
}
|
||||
|
||||
// We need to have entitlement check for usb tethering, so use API in
|
||||
// ConnectivityManager.
|
||||
mConnectivityManager.startTethering(TETHERING_USB, true /* showProvisioningUi */,
|
||||
mOnStartTetheringCallback);
|
||||
} else {
|
||||
mUsbBackend.setCurrentFunctions(function);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -112,4 +143,15 @@ public class UsbDetailsFunctionsController extends UsbDetailsController
|
||||
public String getPreferenceKey() {
|
||||
return "usb_details_functions";
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
final class OnStartTetheringCallback extends
|
||||
ConnectivityManager.OnStartTetheringCallback {
|
||||
|
||||
@Override
|
||||
public void onTetheringFailed() {
|
||||
super.onTetheringFailed();
|
||||
mUsbBackend.setCurrentFunctions(mPreviousFunction);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user