Skip to content

Commit

Permalink
Allow unlocked USB data access (1/2)
Browse files Browse the repository at this point in the history
* By @KreAch3R

This bypasses the marshmallow "regression" of blocking and resetting USB data access
after disconnecting. It also allows saving the default configuration and it survives a reboot.
Originally, this "feature" was added by Google for security reasons
so it should be advised to use the below setting under caution.

Change-Id: Ia7902472119cc6f443ac3b56b5de37f6bd93b657
  • Loading branch information
KreAch3R authored and rc420head committed Jul 19, 2016
1 parent 712fa1c commit badf7fa
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
12 changes: 12 additions & 0 deletions core/java/android/provider/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -3829,6 +3829,18 @@ public boolean validate(String value) {

/**
* Alternative recent apps integration using OmniSwitch
* USB data automatic unlock
*/
public static final String USB_DATA_AUTO_UNLOCK = "usb_data_auto_unlock";

/**
* Settings to backup. This is here so that it's in the same place as the settings
* keys and easy to update.
*
* NOTE: Settings are backed up and restored in the order they appear
* in this array. If you have one setting depending on another,
* make sure that they are ordered appropriately.
*
* @hide
*/
public static final String RECENTS_USE_OMNISWITCH = "recents_use_omniswitch";
Expand Down
34 changes: 29 additions & 5 deletions services/usb/java/com/android/server/usb/UsbDeviceManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public class UsbDeviceManager {
private static final boolean DEBUG = false;

/**
* The persistent property which stores whether adb is enabled or not.
* The persistent property which stores whether adb is enabled or not, and the user default USB state.
* May also contain vendor-specific default functions for testing purposes.
*/
private static final String USB_PERSISTENT_CONFIG_PROPERTY = "persist.sys.usb.config";
Expand Down Expand Up @@ -161,6 +161,19 @@ public void onChange(boolean selfChange) {
}
}

private class UsbDataSettingsObserver extends ContentObserver {
public UsbDataSettingsObserver() {
super(null);
}
@Override
public void onChange(boolean selfChange) {
boolean unlocked = (Settings.System.getInt(mContentResolver,
Settings.System.USB_DATA_AUTO_UNLOCK, 0) > 0);
mHandler.sendMessage(MSG_SET_USB_DATA_UNLOCKED, unlocked);
Slog.d(TAG, "AUTO_UNLOCK IS CHANGED.");
}
}

/*
* Listens for uevent messages from the kernel to monitor the USB state
*/
Expand Down Expand Up @@ -318,7 +331,7 @@ private final class UsbHandler extends Handler {
private boolean mConnected;
private boolean mHostConnected;
private boolean mConfigured;
private boolean mUsbDataUnlocked;
private boolean mUsbDataUnlocked = isUsbDataSetToUnlocked();
private String mCurrentFunctions;
private boolean mCurrentFunctionsApplied;
private UsbAccessory mCurrentAccessory;
Expand All @@ -344,7 +357,7 @@ public UsbHandler(Looper looper) {
String state = FileUtils.readTextFile(new File(STATE_PATH), 0, null).trim();
updateState(state);

// register observer to listen for settings changes
// register observer to listen for adb settings changes
mContentResolver.registerContentObserver(
Settings.Global.getUriFor(Settings.Global.ADB_ENABLED),
false, new AdbSettingsObserver());
Expand All @@ -362,6 +375,10 @@ public void onChange(boolean selfChange) {
mContentResolver.registerContentObserver(
Settings.Secure.getUriFor(Settings.Secure.ADB_NOTIFY),
false, adbNotificationObserver);
// register observer to listen for USB data settings changes
mContentResolver.registerContentObserver(
Settings.System.getUriFor(Settings.System.USB_DATA_AUTO_UNLOCK),
false, new UsbDataSettingsObserver());

// Watch for USB configuration changes
mUEventObserver.startObserving(USB_STATE_MATCH);
Expand Down Expand Up @@ -434,6 +451,8 @@ private boolean setUsbConfig(String config) {
// we always set it due to b/23631400, where adbd was getting killed
// and not restarted due to property timeouts on some devices
SystemProperties.set(USB_CONFIG_PROPERTY, config);
// set the persistent value too (to survive reboots)
SystemProperties.set(USB_PERSISTENT_CONFIG_PROPERTY, config);
return waitForState(config);
}

Expand Down Expand Up @@ -670,7 +689,7 @@ public void handleMessage(Message msg) {
case MSG_UPDATE_STATE:
mConnected = (msg.arg1 == 1);
mConfigured = (msg.arg2 == 1);
if (!mConnected) {
if (!mConnected && !isUsbDataSetToUnlocked()) {
// When a disconnect occurs, relock access to sensitive user data
mUsbDataUnlocked = false;
}
Expand All @@ -679,7 +698,7 @@ public void handleMessage(Message msg) {
if (UsbManager.containsFunction(mCurrentFunctions,
UsbManager.USB_FUNCTION_ACCESSORY)) {
updateCurrentAccessory();
} else if (!mConnected) {
} else if (!mConnected && !isUsbDataSetToUnlocked()) {
// restore defaults when USB is disconnected
setEnabledFunctions(null, false);
}
Expand Down Expand Up @@ -920,6 +939,11 @@ public boolean isFunctionEnabled(String function) {
return UsbManager.containsFunction(SystemProperties.get(USB_CONFIG_PROPERTY), function);
}

public boolean isUsbDataSetToUnlocked() {
return (Settings.System.getInt(mContentResolver,
Settings.System.USB_DATA_AUTO_UNLOCK, 0) > 0);
}

public void setCurrentFunctions(String functions) {
if (DEBUG) Slog.d(TAG, "setCurrentFunctions(" + functions + ")");
mHandler.sendMessage(MSG_SET_CURRENT_FUNCTIONS, functions);
Expand Down

0 comments on commit badf7fa

Please sign in to comment.