Skip to content

Commit 1a008c1

Browse files
committed
UsbDeviceManager: fix b/21429947 regression (try #2)
As discussed in b/21429947 (commit 6740190), MTP must always be enabled, even if access to the underlying MTP data is disabled. Otherwise, Android will not enumerate on the USB bus, and won't receive notifications from the kernel about USB state changes. This effectively prevents using MTP functionality on user builds, or on userdebug/eng builds with adb turned off. Always ensure that MTP is the default driver mode. Move the DISALLOW_USB_FILE_TRANSFER filtering of mUsbDataUnlocked from setting time to the time we post the sticky broadcast. Remove isUsbDataUnlocked(). It essentially duplicates data in the sticky broadcast. Bug: 22447614 Bug: 21429947 Change-Id: I9d0d94cadbf6db6281ebd77bfb7162f9d06520c2
1 parent 7469060 commit 1a008c1

4 files changed

Lines changed: 22 additions & 52 deletions

File tree

core/java/android/hardware/usb/IUsbManager.aidl

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,6 @@ interface IUsbManager
9797
*/
9898
void setUsbDataUnlocked(boolean unlock);
9999

100-
/* Returns true iff sensitive user data is exposed on the USB connection. */
101-
boolean isUsbDataUnlocked();
102-
103100
/* Allow USB debugging from the attached host. If alwaysAllow is true, add the
104101
* the public key to list of host keys that the user has approved.
105102
*/

core/java/android/hardware/usb/UsbManager.java

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -519,21 +519,6 @@ public void setUsbDataUnlocked(boolean unlocked) {
519519
}
520520
}
521521

522-
/**
523-
* Returns {@code true} iff access to sensitive USB data is currently allowed when
524-
* in device mode.
525-
*
526-
* {@hide}
527-
*/
528-
public boolean isUsbDataUnlocked() {
529-
try {
530-
return mService.isUsbDataUnlocked();
531-
} catch (RemoteException e) {
532-
Log.e(TAG, "RemoteException in isUsbDataUnlocked", e);
533-
}
534-
return false;
535-
}
536-
537522
/**
538523
* Returns a list of physical USB ports on the device.
539524
* <p>

services/usb/java/com/android/server/usb/UsbDeviceManager.java

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,8 @@
4949
import com.android.server.FgThread;
5050

5151
import java.io.File;
52-
import java.io.FileDescriptor;
5352
import java.io.FileNotFoundException;
5453
import java.io.IOException;
55-
import java.io.PrintWriter;
5654
import java.util.HashMap;
5755
import java.util.LinkedList;
5856
import java.util.List;
@@ -316,6 +314,9 @@ public UsbHandler(Looper looper) {
316314
// Restore default functions.
317315
mCurrentFunctions = SystemProperties.get(USB_CONFIG_PROPERTY,
318316
UsbManager.USB_FUNCTION_NONE);
317+
if (UsbManager.USB_FUNCTION_NONE.equals(mCurrentFunctions)) {
318+
mCurrentFunctions = UsbManager.USB_FUNCTION_MTP;
319+
}
319320
mCurrentFunctionsApplied = mCurrentFunctions.equals(
320321
SystemProperties.get(USB_STATE_PROPERTY));
321322
mAdbEnabled = UsbManager.containsFunction(getDefaultFunctions(),
@@ -400,6 +401,14 @@ private boolean setUsbConfig(String config) {
400401
return waitForState(config);
401402
}
402403

404+
private void setUsbDataUnlocked(boolean enable) {
405+
if (DEBUG) Slog.d(TAG, "setUsbDataUnlocked: " + enable);
406+
mUsbDataUnlocked = enable;
407+
updateUsbNotification();
408+
updateUsbStateBroadcast();
409+
setEnabledFunctions(mCurrentFunctions, true);
410+
}
411+
403412
private void setAdbEnabled(boolean enable) {
404413
if (DEBUG) Slog.d(TAG, "setAdbEnabled: " + enable);
405414
if (enable != mAdbEnabled) {
@@ -471,7 +480,6 @@ private boolean trySetEnabledFunctions(String functions, boolean forceRestart) {
471480
}
472481
functions = applyAdbFunction(functions);
473482
functions = applyOemOverrideFunction(functions);
474-
functions = applyUserRestrictions(functions);
475483

476484
if (!mCurrentFunctions.equals(functions) || !mCurrentFunctionsApplied
477485
|| forceRestart) {
@@ -502,13 +510,9 @@ private String applyAdbFunction(String functions) {
502510
return functions;
503511
}
504512

505-
private String applyUserRestrictions(String functions) {
513+
private boolean isUsbTransferAllowed() {
506514
UserManager userManager = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
507-
if (userManager.hasUserRestriction(UserManager.DISALLOW_USB_FILE_TRANSFER)) {
508-
functions = UsbManager.removeFunction(functions, UsbManager.USB_FUNCTION_MTP);
509-
functions = UsbManager.removeFunction(functions, UsbManager.USB_FUNCTION_PTP);
510-
}
511-
return functions;
515+
return !userManager.hasUserRestriction(UserManager.DISALLOW_USB_FILE_TRANSFER);
512516
}
513517

514518
private void updateCurrentAccessory() {
@@ -555,7 +559,7 @@ private void updateUsbStateBroadcast() {
555559
| Intent.FLAG_RECEIVER_FOREGROUND);
556560
intent.putExtra(UsbManager.USB_CONNECTED, mConnected);
557561
intent.putExtra(UsbManager.USB_CONFIGURED, mConfigured);
558-
intent.putExtra(UsbManager.USB_DATA_UNLOCKED, mUsbDataUnlocked);
562+
intent.putExtra(UsbManager.USB_DATA_UNLOCKED, isUsbTransferAllowed() && mUsbDataUnlocked);
559563

560564
if (mCurrentFunctions != null) {
561565
String[] functions = mCurrentFunctions.split(",");
@@ -659,10 +663,7 @@ public void handleMessage(Message msg) {
659663
setEnabledFunctions(mCurrentFunctions, false);
660664
break;
661665
case MSG_SET_USB_DATA_UNLOCKED:
662-
mUsbDataUnlocked = (msg.arg1 == 1);
663-
updateUsbNotification();
664-
updateUsbStateBroadcast();
665-
setEnabledFunctions(mCurrentFunctions, true);
666+
setUsbDataUnlocked(msg.arg1 == 1);
666667
break;
667668
case MSG_SYSTEM_READY:
668669
updateUsbNotification();
@@ -807,8 +808,12 @@ private void updateAdbNotification() {
807808
}
808809

809810
private String getDefaultFunctions() {
810-
return SystemProperties.get(USB_PERSISTENT_CONFIG_PROPERTY,
811-
UsbManager.USB_FUNCTION_ADB);
811+
String func = SystemProperties.get(USB_PERSISTENT_CONFIG_PROPERTY,
812+
UsbManager.USB_FUNCTION_NONE);
813+
if (UsbManager.USB_FUNCTION_NONE.equals(func)) {
814+
func = UsbManager.USB_FUNCTION_MTP;
815+
}
816+
return func;
812817
}
813818

814819
public void dump(IndentingPrintWriter pw) {
@@ -817,6 +822,7 @@ public void dump(IndentingPrintWriter pw) {
817822
pw.println(" mCurrentFunctionsApplied: " + mCurrentFunctionsApplied);
818823
pw.println(" mConnected: " + mConnected);
819824
pw.println(" mConfigured: " + mConfigured);
825+
pw.println(" mUsbDataUnlocked: " + mUsbDataUnlocked);
820826
pw.println(" mCurrentAccessory: " + mCurrentAccessory);
821827
try {
822828
pw.println(" Kernel state: "
@@ -864,11 +870,6 @@ public void setUsbDataUnlocked(boolean unlocked) {
864870
mHandler.sendMessage(MSG_SET_USB_DATA_UNLOCKED, unlocked);
865871
}
866872

867-
public boolean isUsbDataUnlocked() {
868-
if (DEBUG) Slog.d(TAG, "isUsbDataUnlocked() -> " + mHandler.mUsbDataUnlocked);
869-
return mHandler.mUsbDataUnlocked;
870-
}
871-
872873
private void readOemUsbOverrideConfig() {
873874
String[] configList = mContext.getResources().getStringArray(
874875
com.android.internal.R.array.config_oemUsbModeOverride);

services/usb/java/com/android/server/usb/UsbService.java

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -322,22 +322,9 @@ private static boolean isSupportedCurrentFunction(String function) {
322322
@Override
323323
public void setUsbDataUnlocked(boolean unlocked) {
324324
mContext.enforceCallingOrSelfPermission(android.Manifest.permission.MANAGE_USB, null);
325-
// If attempt to change USB function while file transfer is restricted, ensure that
326-
// usb data is always locked, and return.
327-
UserManager userManager = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
328-
if (userManager.hasUserRestriction(UserManager.DISALLOW_USB_FILE_TRANSFER)) {
329-
if (mDeviceManager != null) mDeviceManager.setUsbDataUnlocked(false);
330-
return;
331-
}
332325
mDeviceManager.setUsbDataUnlocked(unlocked);
333326
}
334327

335-
@Override
336-
public boolean isUsbDataUnlocked() {
337-
mContext.enforceCallingOrSelfPermission(android.Manifest.permission.MANAGE_USB, null);
338-
return mDeviceManager.isUsbDataUnlocked();
339-
}
340-
341328
@Override
342329
public void allowUsbDebugging(boolean alwaysAllow, String publicKey) {
343330
mContext.enforceCallingOrSelfPermission(android.Manifest.permission.MANAGE_USB, null);

0 commit comments

Comments
 (0)