diff options
Diffstat (limited to 'services/usb/java')
5 files changed, 58 insertions, 158 deletions
diff --git a/services/usb/java/com/android/server/usb/UsbAlsaManager.java b/services/usb/java/com/android/server/usb/UsbAlsaManager.java index daccf95..27c97d0 100644 --- a/services/usb/java/com/android/server/usb/UsbAlsaManager.java +++ b/services/usb/java/com/android/server/usb/UsbAlsaManager.java @@ -401,6 +401,7 @@ public final class UsbAlsaManager { Bundle properties = new Bundle(); String manufacturer = usbDevice.getManufacturerName(); String product = usbDevice.getProductName(); + String version = usbDevice.getVersion(); String name; if (manufacturer == null || manufacturer.isEmpty()) { name = product; @@ -412,6 +413,7 @@ public final class UsbAlsaManager { properties.putString(MidiDeviceInfo.PROPERTY_NAME, name); properties.putString(MidiDeviceInfo.PROPERTY_MANUFACTURER, manufacturer); properties.putString(MidiDeviceInfo.PROPERTY_PRODUCT, product); + properties.putString(MidiDeviceInfo.PROPERTY_VERSION, version); properties.putString(MidiDeviceInfo.PROPERTY_SERIAL_NUMBER, usbDevice.getSerialNumber()); properties.putInt(MidiDeviceInfo.PROPERTY_ALSA_CARD, alsaDevice.mCard); diff --git a/services/usb/java/com/android/server/usb/UsbDeviceManager.java b/services/usb/java/com/android/server/usb/UsbDeviceManager.java index 6adb8be..cb8f938 100644 --- a/services/usb/java/com/android/server/usb/UsbDeviceManager.java +++ b/services/usb/java/com/android/server/usb/UsbDeviceManager.java @@ -78,8 +78,6 @@ public class UsbDeviceManager { "/sys/class/android_usb/android0/functions"; private static final String STATE_PATH = "/sys/class/android_usb/android0/state"; - private static final String MASS_STORAGE_FILE_PATH = - "/sys/class/android_usb/android0/f_mass_storage/lun/file"; private static final String RNDIS_ETH_ADDR_PATH = "/sys/class/android_usb/android0/f_rndis/ethaddr"; private static final String AUDIO_SOURCE_PCM_PATH = @@ -94,7 +92,6 @@ public class UsbDeviceManager { private static final int MSG_BOOT_COMPLETED = 4; private static final int MSG_USER_SWITCHED = 5; - private static final int AUDIO_MODE_NONE = 0; private static final int AUDIO_MODE_SOURCE = 1; // Delay for debouncing USB disconnects. @@ -109,8 +106,6 @@ public class UsbDeviceManager { // Request is cancelled if host does not configure device within 10 seconds. private static final int ACCESSORY_REQUEST_TIMEOUT = 10 * 1000; - private static final String BOOT_MODE_PROPERTY = "ro.bootmode"; - private UsbHandler mHandler; private boolean mBootCompleted; @@ -247,7 +242,7 @@ public class UsbDeviceManager { if (functions != null) { mAccessoryModeRequestTime = SystemClock.elapsedRealtime(); - setCurrentFunctions(functions, false); + setCurrentFunctions(functions); } } @@ -320,7 +315,6 @@ public class UsbDeviceManager { private boolean mConnected; private boolean mConfigured; private String mCurrentFunctions; - private String mDefaultFunctions; private UsbAccessory mCurrentAccessory; private int mUsbNotificationId; private boolean mAdbNotificationShown; @@ -345,25 +339,20 @@ public class UsbDeviceManager { public UsbHandler(Looper looper) { super(looper); try { + // Special note about persist.sys.usb.config: We only ever look at the adb value + // from that property. Other values are ignored. persist.sys.usb.config is now + // only used to determine if adb is enabled or not. + // TODO: rename persist.sys.usb.config to something more descriptive. // persist.sys.usb.config should never be unset. But if it is, set it to "adb" // so we have a chance of debugging what happened. - mDefaultFunctions = SystemProperties.get("persist.sys.usb.config", "adb"); - - // Check if USB mode needs to be overridden depending on OEM specific bootmode. - mDefaultFunctions = processOemUsbOverride(mDefaultFunctions); - // sanity check the sys.usb.config system property - // this may be necessary if we crashed while switching USB configurations - String config = SystemProperties.get("sys.usb.config", "none"); - if (!config.equals(mDefaultFunctions)) { - Slog.w(TAG, "resetting config to persistent property: " + mDefaultFunctions); - SystemProperties.set("sys.usb.config", mDefaultFunctions); - } + mAdbEnabled = containsFunction( + SystemProperties.get(UsbManager.ADB_PERSISTENT_PROPERTY, "adb"), + UsbManager.USB_FUNCTION_ADB); - mCurrentFunctions = getDefaultFunctions(); + mCurrentFunctions = mAdbEnabled ? "adb" : "none"; String state = FileUtils.readTextFile(new File(STATE_PATH), 0, null).trim(); updateState(state); - mAdbEnabled = containsFunction(mCurrentFunctions, UsbManager.USB_FUNCTION_ADB); // register observer to listen for settings changes mContentResolver.registerContentObserver( @@ -398,14 +387,6 @@ public class UsbDeviceManager { sendMessage(m); } - public void sendMessage(int what, Object arg0, boolean arg1) { - removeMessages(what); - Message m = Message.obtain(this, what); - m.obj = arg0; - m.arg1 = (arg1 ? 1 : 0); - sendMessage(m); - } - public void updateState(String state) { int connected, configured; @@ -445,7 +426,7 @@ public class UsbDeviceManager { private boolean setUsbConfig(String config) { if (DEBUG) Slog.d(TAG, "setUsbConfig(" + config + ")"); // set the new configuration - SystemProperties.set("sys.usb.config", config); + SystemProperties.set(UsbManager.USB_SETTINGS_PROPERTY, config); return waitForState(config); } @@ -455,9 +436,9 @@ public class UsbDeviceManager { mAdbEnabled = enable; // Due to the persist.sys.usb.config property trigger, changing adb state requires // persisting default function - setEnabledFunctions(mDefaultFunctions, true); + SystemProperties.set(UsbManager.ADB_PERSISTENT_PROPERTY, mAdbEnabled ? "adb" : "none"); // After persisting them use the lock-down aware function set - setEnabledFunctions(getDefaultFunctions(), false); + setEnabledFunctions(getDefaultFunctions()); updateAdbNotification(); } if (mDebuggingManager != null) { @@ -465,65 +446,31 @@ public class UsbDeviceManager { } } - private void setEnabledFunctions(String functions, boolean makeDefault) { - if (DEBUG) Slog.d(TAG, "setEnabledFunctions " + functions - + " makeDefault: " + makeDefault); + private void setEnabledFunctions(String functions) { + if (DEBUG) Slog.d(TAG, "setEnabledFunctions " + functions); - // Do not update persystent.sys.usb.config if the device is booted up - // with OEM specific mode. - if (functions != null && makeDefault && !needsOemUsbOverride()) { + if (functions == null) { + functions = "none"; + } - if (mAdbEnabled) { - functions = addFunction(functions, UsbManager.USB_FUNCTION_ADB); - } else { - functions = removeFunction(functions, UsbManager.USB_FUNCTION_ADB); - } - if (!mDefaultFunctions.equals(functions)) { - if (!setUsbConfig("none")) { - Slog.e(TAG, "Failed to disable USB"); - // revert to previous configuration if we fail - setUsbConfig(mCurrentFunctions); - return; - } - // setting this property will also change the current USB state - // via a property trigger - SystemProperties.set("persist.sys.usb.config", functions); - if (waitForState(functions)) { - mCurrentFunctions = functions; - mDefaultFunctions = functions; - } else { - Slog.e(TAG, "Failed to switch persistent USB config to " + functions); - // revert to previous configuration if we fail - SystemProperties.set("persist.sys.usb.config", mDefaultFunctions); - } - } + if (mAdbEnabled) { + functions = addFunction(functions, UsbManager.USB_FUNCTION_ADB); } else { - if (functions == null) { - functions = mDefaultFunctions; + functions = removeFunction(functions, UsbManager.USB_FUNCTION_ADB); + } + if (!mCurrentFunctions.equals(functions)) { + if (!setUsbConfig("none")) { + Slog.e(TAG, "Failed to disable USB"); + // revert to previous configuration if we fail + setUsbConfig(mCurrentFunctions); + return; } - - // Override with bootmode specific usb mode if needed - functions = processOemUsbOverride(functions); - - if (mAdbEnabled) { - functions = addFunction(functions, UsbManager.USB_FUNCTION_ADB); + if (setUsbConfig(functions)) { + mCurrentFunctions = functions; } else { - functions = removeFunction(functions, UsbManager.USB_FUNCTION_ADB); - } - if (!mCurrentFunctions.equals(functions)) { - if (!setUsbConfig("none")) { - Slog.e(TAG, "Failed to disable USB"); - // revert to previous configuration if we fail - setUsbConfig(mCurrentFunctions); - return; - } - if (setUsbConfig(functions)) { - mCurrentFunctions = functions; - } else { - Slog.e(TAG, "Failed to switch USB config to " + functions); - // revert to previous configuration if we fail - setUsbConfig(mCurrentFunctions); - } + Slog.e(TAG, "Failed to switch USB config to " + functions); + // revert to previous configuration if we fail + setUsbConfig(mCurrentFunctions); } } } @@ -553,7 +500,7 @@ public class UsbDeviceManager { // make sure accessory mode is off // and restore default functions Slog.d(TAG, "exited USB accessory mode"); - setEnabledFunctions(getDefaultFunctions(), false); + setEnabledFunctions(getDefaultFunctions()); if (mCurrentAccessory != null) { if (mBootCompleted) { @@ -646,7 +593,7 @@ public class UsbDeviceManager { updateCurrentAccessory(); } else if (!mConnected) { // restore defaults when USB is disconnected - setEnabledFunctions(getDefaultFunctions(), false); + setEnabledFunctions(getDefaultFunctions()); } if (mBootCompleted) { updateUsbState(); @@ -659,10 +606,11 @@ public class UsbDeviceManager { break; case MSG_SET_CURRENT_FUNCTIONS: String functions = (String)msg.obj; - boolean makeDefault = (msg.arg1 == 1); - setEnabledFunctions(functions, makeDefault); + setEnabledFunctions(functions); break; case MSG_SYSTEM_READY: + setUsbConfig(mCurrentFunctions); + SystemProperties.set(UsbManager.ADB_PERSISTENT_PROPERTY, mAdbEnabled ? "adb" : "none"); updateUsbNotification(); updateAdbNotification(); updateUsbState(); @@ -671,6 +619,7 @@ public class UsbDeviceManager { break; case MSG_BOOT_COMPLETED: mBootCompleted = true; + setUsbConfig(mCurrentFunctions); if (mCurrentAccessory != null) { getCurrentSettings().accessoryAttached(mCurrentAccessory); } @@ -726,10 +675,7 @@ public class UsbDeviceManager { } else if (containsFunction(mCurrentFunctions, UsbManager.USB_FUNCTION_ACCESSORY)) { id = com.android.internal.R.string.usb_accessory_notification_title; } else { - // There is a different notification for USB tethering so we don't need one here - //if (!containsFunction(mCurrentFunctions, UsbManager.USB_FUNCTION_RNDIS)) { - // Slog.e(TAG, "No known USB function in updateUsbNotification"); - //} + id = com.android.internal.R.string.usb_charging_notification_title; } } if (id != mUsbNotificationId) { @@ -756,7 +702,7 @@ public class UsbDeviceManager { Intent intent = Intent.makeRestartActivityTask( new ComponentName("com.android.settings", - "com.android.settings.UsbSettings")); + "com.android.settings.deviceinfo.UsbModeChooserActivity")); PendingIntent pi = PendingIntent.getActivityAsUser(mContext, 0, intent, 0, null, UserHandle.CURRENT); notification.color = mContext.getColor( @@ -812,18 +758,12 @@ public class UsbDeviceManager { } private String getDefaultFunctions() { - UserManager userManager = (UserManager) mContext.getSystemService(Context.USER_SERVICE); - if (userManager.hasUserRestriction(UserManager.DISALLOW_USB_FILE_TRANSFER, - new UserHandle(mCurrentUser))) { - return "none"; - } - return mDefaultFunctions; + return "none"; } public void dump(FileDescriptor fd, PrintWriter pw) { pw.println(" USB Device State:"); pw.println(" Current Functions: " + mCurrentFunctions); - pw.println(" Default Functions: " + mDefaultFunctions); pw.println(" mConnected: " + mConnected); pw.println(" mConfigured: " + mConfigured); pw.println(" mCurrentAccessory: " + mCurrentAccessory); @@ -832,8 +772,6 @@ public class UsbDeviceManager { + FileUtils.readTextFile(new File(STATE_PATH), 0, null).trim()); pw.println(" Kernel function list: " + FileUtils.readTextFile(new File(FUNCTIONS_PATH), 0, null).trim()); - pw.println(" Mass storage backing file: " - + FileUtils.readTextFile(new File(MASS_STORAGE_FILE_PATH), 0, null).trim()); } catch (IOException e) { pw.println("IOException: " + e); } @@ -861,18 +799,9 @@ public class UsbDeviceManager { return nativeOpenAccessory(); } - public void setCurrentFunctions(String functions, boolean makeDefault) { - if (DEBUG) Slog.d(TAG, "setCurrentFunctions(" + functions + ") default: " + makeDefault); - mHandler.sendMessage(MSG_SET_CURRENT_FUNCTIONS, functions, makeDefault); - } - - public void setMassStorageBackingFile(String path) { - if (path == null) path = ""; - try { - FileUtils.stringToFile(MASS_STORAGE_FILE_PATH, path); - } catch (IOException e) { - Slog.e(TAG, "failed to write to " + MASS_STORAGE_FILE_PATH); - } + public void setCurrentFunctions(String functions) { + if (DEBUG) Slog.d(TAG, "setCurrentFunctions(" + functions + ")"); + mHandler.sendMessage(MSG_SET_CURRENT_FUNCTIONS, functions); } private void readOemUsbOverrideConfig() { @@ -897,31 +826,6 @@ public class UsbDeviceManager { } } - private boolean needsOemUsbOverride() { - if (mOemModeMap == null) return false; - - String bootMode = SystemProperties.get(BOOT_MODE_PROPERTY, "unknown"); - return (mOemModeMap.get(bootMode) != null) ? true : false; - } - - private String processOemUsbOverride(String usbFunctions) { - if ((usbFunctions == null) || (mOemModeMap == null)) return usbFunctions; - - String bootMode = SystemProperties.get(BOOT_MODE_PROPERTY, "unknown"); - - List<Pair<String, String>> overrides = mOemModeMap.get(bootMode); - if (overrides != null) { - for (Pair<String, String> pair: overrides) { - if (pair.first.equals(usbFunctions)) { - Slog.d(TAG, "OEM USB override: " + pair.first + " ==> " + pair.second); - return pair.second; - } - } - } - // return passed in functions as is. - return usbFunctions; - } - public void allowUsbDebugging(boolean alwaysAllow, String publicKey) { if (mDebuggingManager != null) { mDebuggingManager.allowUsbDebugging(alwaysAllow, publicKey); diff --git a/services/usb/java/com/android/server/usb/UsbHostManager.java b/services/usb/java/com/android/server/usb/UsbHostManager.java index 5b58051..f5f2b07 100644 --- a/services/usb/java/com/android/server/usb/UsbHostManager.java +++ b/services/usb/java/com/android/server/usb/UsbHostManager.java @@ -112,7 +112,7 @@ public class UsbHostManager { */ private boolean beginUsbDeviceAdded(String deviceName, int vendorID, int productID, int deviceClass, int deviceSubclass, int deviceProtocol, - String manufacturerName, String productName, String serialNumber) { + String manufacturerName, String productName, int version, String serialNumber) { if (DEBUG) { Slog.d(TAG, "usb:UsbHostManager.beginUsbDeviceAdded(" + deviceName + ")"); @@ -149,9 +149,12 @@ public class UsbHostManager { return false; } + // Create version string in "%.%" format + String versionString = Integer.toString(version >> 8) + "." + (version & 0xFF); + mNewDevice = new UsbDevice(deviceName, vendorID, productID, deviceClass, deviceSubclass, deviceProtocol, - manufacturerName, productName, serialNumber); + manufacturerName, productName, versionString, serialNumber); mNewConfigurations = new ArrayList<UsbConfiguration>(); mNewInterfaces = new ArrayList<UsbInterface>(); diff --git a/services/usb/java/com/android/server/usb/UsbService.java b/services/usb/java/com/android/server/usb/UsbService.java index fda8076..51281a2 100644 --- a/services/usb/java/com/android/server/usb/UsbService.java +++ b/services/usb/java/com/android/server/usb/UsbService.java @@ -252,29 +252,19 @@ public class UsbService extends IUsbManager.Stub { } @Override - public void setCurrentFunction(String function, boolean makeDefault) { + public void setCurrentFunction(String function) { mContext.enforceCallingOrSelfPermission(android.Manifest.permission.MANAGE_USB, null); // If attempt to change USB function while file transfer is restricted, ensure that // the current function is set to "none", and return. UserManager userManager = (UserManager) mContext.getSystemService(Context.USER_SERVICE); if (userManager.hasUserRestriction(UserManager.DISALLOW_USB_FILE_TRANSFER)) { - if (mDeviceManager != null) mDeviceManager.setCurrentFunctions("none", false); + if (mDeviceManager != null) mDeviceManager.setCurrentFunctions("none"); return; } if (mDeviceManager != null) { - mDeviceManager.setCurrentFunctions(function, makeDefault); - } else { - throw new IllegalStateException("USB device mode not supported"); - } - } - - @Override - public void setMassStorageBackingFile(String path) { - mContext.enforceCallingOrSelfPermission(android.Manifest.permission.MANAGE_USB, null); - if (mDeviceManager != null) { - mDeviceManager.setMassStorageBackingFile(path); + mDeviceManager.setCurrentFunctions(function); } else { throw new IllegalStateException("USB device mode not supported"); } diff --git a/services/usb/java/com/android/server/usb/UsbSettingsManager.java b/services/usb/java/com/android/server/usb/UsbSettingsManager.java index bfd1f13..2331a8b 100644 --- a/services/usb/java/com/android/server/usb/UsbSettingsManager.java +++ b/services/usb/java/com/android/server/usb/UsbSettingsManager.java @@ -57,6 +57,7 @@ import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.PrintWriter; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -558,7 +559,7 @@ class UsbSettingsManager { try { fis = new FileInputStream(sSingleUserSettingsFile); XmlPullParser parser = Xml.newPullParser(); - parser.setInput(fis, null); + parser.setInput(fis, StandardCharsets.UTF_8.name()); XmlUtils.nextElement(parser); while (parser.getEventType() != XmlPullParser.END_DOCUMENT) { @@ -594,7 +595,7 @@ class UsbSettingsManager { try { stream = mSettingsFile.openRead(); XmlPullParser parser = Xml.newPullParser(); - parser.setInput(stream, null); + parser.setInput(stream, StandardCharsets.UTF_8.name()); XmlUtils.nextElement(parser); while (parser.getEventType() != XmlPullParser.END_DOCUMENT) { @@ -623,7 +624,7 @@ class UsbSettingsManager { fos = mSettingsFile.startWrite(); FastXmlSerializer serializer = new FastXmlSerializer(); - serializer.setOutput(fos, "utf-8"); + serializer.setOutput(fos, StandardCharsets.UTF_8.name()); serializer.startDocument(null, true); serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true); serializer.startTag(null, "settings"); |