Skip to content

Commit 197fe26

Browse files
Wink SavilleAndroid (Google) Code Review
authored andcommitted
Merge "Add OEM specific USB mode enumeration based on ro.bootmode property" into ics-mr1
2 parents 650462b + afd8f18 commit 197fe26

File tree

2 files changed

+76
-1
lines changed

2 files changed

+76
-1
lines changed

core/res/res/values/config.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -745,4 +745,11 @@
745745
<!-- Base "touch slop" value used by ViewConfiguration as a
746746
movement threshold where scrolling should begin. -->
747747
<dimen name="config_viewConfigurationTouchSlop">8dp</dimen>
748+
749+
<!-- Array of OEM specific USB mode override config.
750+
OEM can override a certain USB mode depending on ro.bootmode.
751+
Specify an array of below items to set override rule.
752+
[bootmode]:[original USB mode]:[USB mode used]-->
753+
<integer-array translatable="false" name="config_oemUsbModeOverride">
754+
</integer-array>
748755
</resources>

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

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import android.os.SystemProperties;
4747
import android.os.UEventObserver;
4848
import android.provider.Settings;
49+
import android.util.Pair;
4950
import android.util.Slog;
5051

5152
import java.io.File;
@@ -54,7 +55,10 @@
5455
import java.io.IOException;
5556
import java.io.PrintWriter;
5657
import java.util.ArrayList;
58+
import java.util.LinkedList;
5759
import java.util.List;
60+
import java.util.HashMap;
61+
import java.util.Map;
5862

5963
/**
6064
* UsbDeviceManager manages USB state in device mode.
@@ -88,6 +92,8 @@ public class UsbDeviceManager {
8892
// which need debouncing.
8993
private static final int UPDATE_DELAY = 1000;
9094

95+
private static final String BOOT_MODE_PROPERTY = "ro.bootmode";
96+
9197
private UsbHandler mHandler;
9298
private boolean mBootCompleted;
9399

@@ -98,6 +104,7 @@ public class UsbDeviceManager {
98104
private final boolean mHasUsbAccessory;
99105
private boolean mUseUsbNotification;
100106
private boolean mAdbEnabled;
107+
private Map<String, List<Pair<String, String>>> mOemModeMap;
101108

102109
private class AdbSettingsObserver extends ContentObserver {
103110
public AdbSettingsObserver() {
@@ -138,6 +145,8 @@ public UsbDeviceManager(Context context, UsbSettingsManager settingsManager) {
138145
mHasUsbAccessory = pm.hasSystemFeature(PackageManager.FEATURE_USB_ACCESSORY);
139146
initRndisAddress();
140147

148+
readOemUsbOverrideConfig();
149+
141150
// create a thread for our Handler
142151
HandlerThread thread = new HandlerThread("UsbDeviceManager",
143152
Process.THREAD_PRIORITY_BACKGROUND);
@@ -259,6 +268,10 @@ public UsbHandler(Looper looper) {
259268
// persist.sys.usb.config should never be unset. But if it is, set it to "adb"
260269
// so we have a chance of debugging what happened.
261270
mDefaultFunctions = SystemProperties.get("persist.sys.usb.config", "adb");
271+
272+
// Check if USB mode needs to be overridden depending on OEM specific bootmode.
273+
mDefaultFunctions = processOemUsbOverride(mDefaultFunctions);
274+
262275
// sanity check the sys.usb.config system property
263276
// this may be necessary if we crashed while switching USB configurations
264277
String config = SystemProperties.get("sys.usb.config", "none");
@@ -381,7 +394,11 @@ private void setAdbEnabled(boolean enable) {
381394
}
382395

383396
private void setEnabledFunctions(String functions, boolean makeDefault) {
384-
if (functions != null && makeDefault) {
397+
398+
// Do not update persystent.sys.usb.config if the device is booted up
399+
// with OEM specific mode.
400+
if (functions != null && makeDefault && !needsOemUsbOverride()) {
401+
385402
if (mAdbEnabled) {
386403
functions = addFunction(functions, UsbManager.USB_FUNCTION_ADB);
387404
} else {
@@ -410,6 +427,10 @@ private void setEnabledFunctions(String functions, boolean makeDefault) {
410427
if (functions == null) {
411428
functions = mDefaultFunctions;
412429
}
430+
431+
// Override with bootmode specific usb mode if needed
432+
functions = processOemUsbOverride(functions);
433+
413434
if (mAdbEnabled) {
414435
functions = addFunction(functions, UsbManager.USB_FUNCTION_ADB);
415436
} else {
@@ -671,6 +692,53 @@ public void setMassStorageBackingFile(String path) {
671692
}
672693
}
673694

695+
private void readOemUsbOverrideConfig() {
696+
String[] configList = mContext.getResources().getStringArray(
697+
com.android.internal.R.array.config_oemUsbModeOverride);
698+
699+
if (configList != null) {
700+
for (String config: configList) {
701+
String[] items = config.split(":");
702+
if (items.length == 3) {
703+
if (mOemModeMap == null) {
704+
mOemModeMap = new HashMap<String, List<Pair<String, String>>>();
705+
}
706+
List overrideList = mOemModeMap.get(items[0]);
707+
if (overrideList == null) {
708+
overrideList = new LinkedList<Pair<String, String>>();
709+
mOemModeMap.put(items[0], overrideList);
710+
}
711+
overrideList.add(new Pair<String, String>(items[1], items[2]));
712+
}
713+
}
714+
}
715+
}
716+
717+
private boolean needsOemUsbOverride() {
718+
if (mOemModeMap == null) return false;
719+
720+
String bootMode = SystemProperties.get(BOOT_MODE_PROPERTY, "unknown");
721+
return (mOemModeMap.get(bootMode) != null) ? true : false;
722+
}
723+
724+
private String processOemUsbOverride(String usbFunctions) {
725+
if ((usbFunctions == null) || (mOemModeMap == null)) return usbFunctions;
726+
727+
String bootMode = SystemProperties.get(BOOT_MODE_PROPERTY, "unknown");
728+
729+
List<Pair<String, String>> overrides = mOemModeMap.get(bootMode);
730+
if (overrides != null) {
731+
for (Pair<String, String> pair: overrides) {
732+
if (pair.first.equals(usbFunctions)) {
733+
Slog.d(TAG, "OEM USB override: " + pair.first + " ==> " + pair.second);
734+
return pair.second;
735+
}
736+
}
737+
}
738+
// return passed in functions as is.
739+
return usbFunctions;
740+
}
741+
674742
public void dump(FileDescriptor fd, PrintWriter pw) {
675743
if (mHandler != null) {
676744
mHandler.dump(fd, pw);

0 commit comments

Comments
 (0)