Require authentication for changing USB preferences.

Test: atest SettingsRoboTests
Test: also tested manually
Bug: 317367746
Change-Id: I17daaad7c7772664cae6d12cf2322991827f33bb
This commit is contained in:
Jan Tomljanovic
2023-12-21 18:16:33 +00:00
parent 6ca4c0fb5a
commit db40deb26f
10 changed files with 271 additions and 71 deletions

View File

@@ -23,6 +23,8 @@ import androidx.annotation.UiThread;
import androidx.annotation.VisibleForTesting;
import com.android.settings.core.PreferenceControllerMixin;
import com.android.settings.flags.Flags;
import com.android.settings.wifi.dpp.WifiDppUtils;
import com.android.settingslib.core.AbstractPreferenceController;
/**
@@ -61,4 +63,16 @@ public abstract class UsbDetailsController extends AbstractPreferenceController
*/
@UiThread
protected abstract void refresh(boolean connected, long functions, int powerRole, int dataRole);
/** Protects given action with an auth challenge. */
protected final void requireAuthAndExecute(Runnable action) {
if (Flags.enableAuthChallengeForUsbPreferences() && !mFragment.isUserAuthenticated()) {
WifiDppUtils.showLockScreen(mContext, () -> {
mFragment.setUserAuthenticated(true);
action.run();
});
} else {
action.run();
}
}
}

View File

@@ -98,17 +98,19 @@ public class UsbDetailsDataRoleController extends UsbDetailsController
@Override
public void onRadioButtonClicked(SelectorWithWidgetPreference preference) {
int role = UsbBackend.dataRoleFromString(preference.getKey());
if (role != mUsbBackend.getDataRole() && mNextRolePref == null
&& !Utils.isMonkeyRunning()) {
mUsbBackend.setDataRole(role);
mNextRolePref = preference;
preference.setSummary(R.string.usb_switching);
requireAuthAndExecute(() -> {
int role = UsbBackend.dataRoleFromString(preference.getKey());
if (role != mUsbBackend.getDataRole() && mNextRolePref == null
&& !Utils.isMonkeyRunning()) {
mUsbBackend.setDataRole(role);
mNextRolePref = preference;
preference.setSummary(R.string.usb_switching);
mHandler.postDelayed(mFailureCallback,
mUsbBackend.areAllRolesSupported() ? UsbBackend.PD_ROLE_SWAP_TIMEOUT_MS
: UsbBackend.NONPD_ROLE_SWAP_TIMEOUT_MS);
}
mHandler.postDelayed(mFailureCallback,
mUsbBackend.areAllRolesSupported() ? UsbBackend.PD_ROLE_SWAP_TIMEOUT_MS
: UsbBackend.NONPD_ROLE_SWAP_TIMEOUT_MS);
}
});
}
@Override

View File

@@ -45,6 +45,7 @@ public class UsbDetailsFragment extends DashboardFragment {
private List<UsbDetailsController> mControllers;
private UsbBackend mUsbBackend;
private boolean mUserAuthenticated = false;
@VisibleForTesting
UsbConnectionBroadcastReceiver mUsbReceiver;
@@ -56,6 +57,20 @@ public class UsbDetailsFragment extends DashboardFragment {
}
};
boolean isUserAuthenticated() {
return mUserAuthenticated;
}
void setUserAuthenticated(boolean userAuthenticated) {
mUserAuthenticated = userAuthenticated;
}
@Override
public void onStart() {
super.onStart();
mUserAuthenticated = false;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);

View File

@@ -130,37 +130,39 @@ public class UsbDetailsFunctionsController extends UsbDetailsController
@Override
public void onRadioButtonClicked(SelectorWithWidgetPreference preference) {
final long function = UsbBackend.usbFunctionsFromString(preference.getKey());
final long previousFunction = mUsbBackend.getCurrentFunctions();
if (DEBUG) {
Log.d(TAG, "onRadioButtonClicked() function : " + function + ", toString() : "
+ UsbManager.usbFunctionsToString(function) + ", previousFunction : "
+ previousFunction + ", toString() : "
+ UsbManager.usbFunctionsToString(previousFunction));
}
if (function != previousFunction && !Utils.isMonkeyRunning()
&& !isClickEventIgnored(function, previousFunction)) {
mPreviousFunction = previousFunction;
//Update the UI in advance to make it looks smooth
final SelectorWithWidgetPreference prevPref =
(SelectorWithWidgetPreference) mProfilesContainer.findPreference(
UsbBackend.usbFunctionsToString(mPreviousFunction));
if (prevPref != null) {
prevPref.setChecked(false);
preference.setChecked(true);
requireAuthAndExecute(() -> {
final long function = UsbBackend.usbFunctionsFromString(preference.getKey());
final long previousFunction = mUsbBackend.getCurrentFunctions();
if (DEBUG) {
Log.d(TAG, "onRadioButtonClicked() function : " + function + ", toString() : "
+ UsbManager.usbFunctionsToString(function) + ", previousFunction : "
+ previousFunction + ", toString() : "
+ UsbManager.usbFunctionsToString(previousFunction));
}
if (function != previousFunction && !Utils.isMonkeyRunning()
&& !isClickEventIgnored(function, previousFunction)) {
mPreviousFunction = previousFunction;
if (function == UsbManager.FUNCTION_RNDIS || function == UsbManager.FUNCTION_NCM) {
// We need to have entitlement check for usb tethering, so use API in
// TetheringManager.
mTetheringManager.startTethering(
TetheringManager.TETHERING_USB, new HandlerExecutor(mHandler),
mOnStartTetheringCallback);
} else {
mUsbBackend.setCurrentFunctions(function);
//Update the UI in advance to make it looks smooth
final SelectorWithWidgetPreference prevPref =
(SelectorWithWidgetPreference) mProfilesContainer.findPreference(
UsbBackend.usbFunctionsToString(mPreviousFunction));
if (prevPref != null) {
prevPref.setChecked(false);
preference.setChecked(true);
}
if (function == UsbManager.FUNCTION_RNDIS || function == UsbManager.FUNCTION_NCM) {
// We need to have entitlement check for usb tethering, so use API in
// TetheringManager.
mTetheringManager.startTethering(
TetheringManager.TETHERING_USB, new HandlerExecutor(mHandler),
mOnStartTetheringCallback);
} else {
mUsbBackend.setCurrentFunctions(function);
}
}
}
});
}
private boolean isClickEventIgnored(long function, long previousFunction) {

View File

@@ -78,13 +78,15 @@ public class UsbDetailsTranscodeMtpController extends UsbDetailsController
@Override
public boolean onPreferenceClick(Preference preference) {
SystemProperties.set(TRANSCODE_MTP_SYS_PROP_KEY,
Boolean.toString(mSwitchPreference.isChecked()));
requireAuthAndExecute(() -> {
SystemProperties.set(TRANSCODE_MTP_SYS_PROP_KEY,
Boolean.toString(mSwitchPreference.isChecked()));
final long previousFunctions = mUsbBackend.getCurrentFunctions();
// Toggle the MTP connection to reload file sizes for files shared via MTP clients
mUsbBackend.setCurrentFunctions(previousFunctions & ~UsbManager.FUNCTION_MTP);
mUsbBackend.setCurrentFunctions(previousFunctions);
final long previousFunctions = mUsbBackend.getCurrentFunctions();
// Toggle the MTP connection to reload file sizes for files shared via MTP clients
mUsbBackend.setCurrentFunctions(previousFunctions & ~UsbManager.FUNCTION_MTP);
mUsbBackend.setCurrentFunctions(previousFunctions);
});
return true;
}