Loading core/java/android/hardware/usb/IUsbManager.aidl +9 −0 Original line number Diff line number Diff line Loading @@ -81,4 +81,13 @@ interface IUsbManager /* Clears default preferences and permissions for the package */ void clearDefaults(String packageName); /* Sets the current primary USB function. */ void setPrimaryFunction(String functions); /* Sets the default primary USB function. */ void setDefaultFunction(String functions); /* Sets the file path for USB mass storage backing file. */ void setMassStorageBackingFile(String path); } core/java/android/hardware/usb/UsbManager.java +43 −31 Original line number Diff line number Diff line Loading @@ -22,12 +22,9 @@ import android.content.Context; import android.os.Bundle; import android.os.ParcelFileDescriptor; import android.os.RemoteException; import android.os.SystemProperties; import android.util.Log; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.HashMap; /** Loading @@ -50,7 +47,7 @@ public class UsbManager { * This is a sticky broadcast for clients that includes USB connected/disconnected state, * <ul> * <li> {@link #USB_CONNECTED} boolean indicating whether USB is connected or disconnected. * <li> {@link #USB_CONFIGURATION} integer containing current USB configuration * <li> {@link #USB_CONFIGURED} boolean indicating whether USB is configured. * currently zero if not configured, one for configured. * <li> {@link #USB_FUNCTION_MASS_STORAGE} boolean extra indicating whether the * mass storage function is enabled Loading Loading @@ -128,12 +125,12 @@ public class UsbManager { public static final String USB_CONNECTED = "connected"; /** * Integer extra containing currently set USB configuration. * Boolean extra indicating whether USB is configured. * Used in extras for the {@link #ACTION_USB_STATE} broadcast. * * {@hide} */ public static final String USB_CONFIGURATION = "configuration"; public static final String USB_CONFIGURED = "configured"; /** * Name of the USB mass storage USB function. Loading Loading @@ -388,55 +385,70 @@ public class UsbManager { } } private static File getFunctionEnableFile(String function) { return new File("/sys/class/usb_composite/" + function + "/enable"); private static boolean propertyContainsFunction(String property, String function) { String functions = SystemProperties.get(property, ""); int index = functions.indexOf(function); if (index < 0) return false; if (index > 0 && functions.charAt(index - 1) != ',') return false; int charAfter = index + function.length(); if (charAfter < functions.length() && functions.charAt(charAfter) != ',') return false; return true; } /** * Returns true if the specified USB function is supported by the kernel. * Note that a USB function maybe supported but disabled. * Returns true if the specified USB function is currently enabled. * * @param function name of the USB function * @return true if the USB function is supported. * @return true if the USB function is enabled. * * {@hide} */ public static boolean isFunctionSupported(String function) { return getFunctionEnableFile(function).exists(); public boolean isFunctionEnabled(String function) { return propertyContainsFunction("sys.usb.config", function); } /** * Returns true if the specified USB function is currently enabled. * Sets the primary USB function. * * @param function name of the USB function * @return true if the USB function is enabled. * * {@hide} */ public static boolean isFunctionEnabled(String function) { public void setPrimaryFunction(String function) { try { FileInputStream stream = new FileInputStream(getFunctionEnableFile(function)); boolean enabled = (stream.read() == '1'); stream.close(); return enabled; } catch (IOException e) { return false; mService.setPrimaryFunction(function); } catch (RemoteException e) { Log.e(TAG, "RemoteException in setPrimaryFunction", e); } } /** * Enables or disables a USB function. * Sets the default primary USB function. * * @param function name of the USB function * * {@hide} */ public static boolean setFunctionEnabled(String function, boolean enable) { public void setDefaultFunction(String function) { try { FileOutputStream stream = new FileOutputStream(getFunctionEnableFile(function)); stream.write(enable ? '1' : '0'); stream.close(); return true; } catch (IOException e) { return false; mService.setDefaultFunction(function); } catch (RemoteException e) { Log.e(TAG, "RemoteException in setDefaultFunction", e); } } /** * Sets the file path for USB mass storage backing file. * * @param path backing file path * * {@hide} */ public void setMassStorageBackingFile(String path) { try { mService.setMassStorageBackingFile(path); } catch (RemoteException e) { Log.e(TAG, "RemoteException in setDefaultFunction", e); } } } services/java/com/android/server/SystemServer.java +2 −2 Original line number Diff line number Diff line Loading @@ -406,8 +406,8 @@ class ServerThread extends Thread { } try { Slog.i(TAG, "USB Observer"); // Listen for USB changes Slog.i(TAG, "USB Service"); // Manage USB host and device support usb = new UsbService(context); ServiceManager.addService(Context.USB_SERVICE, usb); } catch (Throwable e) { Loading services/java/com/android/server/usb/UsbDeviceManager.java +357 −305 File changed.Preview size limit exceeded, changes collapsed. Show changes services/java/com/android/server/usb/UsbService.java +29 −2 Original line number Diff line number Diff line Loading @@ -50,7 +50,7 @@ public class UsbService extends IUsbManager.Stub { if (pm.hasSystemFeature(PackageManager.FEATURE_USB_HOST)) { mHostManager = new UsbHostManager(context, mSettingsManager); } if (new File("/sys/class/usb_composite").exists()) { if (new File("/sys/class/android_usb").exists()) { mDeviceManager = new UsbDeviceManager(context, mSettingsManager); } } Loading Loading @@ -92,7 +92,7 @@ public class UsbService extends IUsbManager.Stub { /* opens the currently attached USB accessory (device mode) */ public ParcelFileDescriptor openAccessory(UsbAccessory accessory) { if (mDeviceManager != null) { return openAccessory(accessory); return mDeviceManager.openAccessory(accessory); } else { return null; } Loading Loading @@ -146,6 +146,33 @@ public class UsbService extends IUsbManager.Stub { mSettingsManager.clearDefaults(packageName); } public void setPrimaryFunction(String function) { mContext.enforceCallingOrSelfPermission(android.Manifest.permission.MANAGE_USB, null); if (mDeviceManager != null) { mDeviceManager.setPrimaryFunction(function); } else { throw new IllegalStateException("USB device mode not supported"); } } public void setDefaultFunction(String function) { mContext.enforceCallingOrSelfPermission(android.Manifest.permission.MANAGE_USB, null); if (mDeviceManager != null) { mDeviceManager.setDefaultFunction(function); } else { throw new IllegalStateException("USB device mode not supported"); } } public void setMassStorageBackingFile(String path) { mContext.enforceCallingOrSelfPermission(android.Manifest.permission.MANAGE_USB, null); if (mDeviceManager != null) { mDeviceManager.setMassStorageBackingFile(path); } else { throw new IllegalStateException("USB device mode not supported"); } } @Override public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP) Loading Loading
core/java/android/hardware/usb/IUsbManager.aidl +9 −0 Original line number Diff line number Diff line Loading @@ -81,4 +81,13 @@ interface IUsbManager /* Clears default preferences and permissions for the package */ void clearDefaults(String packageName); /* Sets the current primary USB function. */ void setPrimaryFunction(String functions); /* Sets the default primary USB function. */ void setDefaultFunction(String functions); /* Sets the file path for USB mass storage backing file. */ void setMassStorageBackingFile(String path); }
core/java/android/hardware/usb/UsbManager.java +43 −31 Original line number Diff line number Diff line Loading @@ -22,12 +22,9 @@ import android.content.Context; import android.os.Bundle; import android.os.ParcelFileDescriptor; import android.os.RemoteException; import android.os.SystemProperties; import android.util.Log; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.HashMap; /** Loading @@ -50,7 +47,7 @@ public class UsbManager { * This is a sticky broadcast for clients that includes USB connected/disconnected state, * <ul> * <li> {@link #USB_CONNECTED} boolean indicating whether USB is connected or disconnected. * <li> {@link #USB_CONFIGURATION} integer containing current USB configuration * <li> {@link #USB_CONFIGURED} boolean indicating whether USB is configured. * currently zero if not configured, one for configured. * <li> {@link #USB_FUNCTION_MASS_STORAGE} boolean extra indicating whether the * mass storage function is enabled Loading Loading @@ -128,12 +125,12 @@ public class UsbManager { public static final String USB_CONNECTED = "connected"; /** * Integer extra containing currently set USB configuration. * Boolean extra indicating whether USB is configured. * Used in extras for the {@link #ACTION_USB_STATE} broadcast. * * {@hide} */ public static final String USB_CONFIGURATION = "configuration"; public static final String USB_CONFIGURED = "configured"; /** * Name of the USB mass storage USB function. Loading Loading @@ -388,55 +385,70 @@ public class UsbManager { } } private static File getFunctionEnableFile(String function) { return new File("/sys/class/usb_composite/" + function + "/enable"); private static boolean propertyContainsFunction(String property, String function) { String functions = SystemProperties.get(property, ""); int index = functions.indexOf(function); if (index < 0) return false; if (index > 0 && functions.charAt(index - 1) != ',') return false; int charAfter = index + function.length(); if (charAfter < functions.length() && functions.charAt(charAfter) != ',') return false; return true; } /** * Returns true if the specified USB function is supported by the kernel. * Note that a USB function maybe supported but disabled. * Returns true if the specified USB function is currently enabled. * * @param function name of the USB function * @return true if the USB function is supported. * @return true if the USB function is enabled. * * {@hide} */ public static boolean isFunctionSupported(String function) { return getFunctionEnableFile(function).exists(); public boolean isFunctionEnabled(String function) { return propertyContainsFunction("sys.usb.config", function); } /** * Returns true if the specified USB function is currently enabled. * Sets the primary USB function. * * @param function name of the USB function * @return true if the USB function is enabled. * * {@hide} */ public static boolean isFunctionEnabled(String function) { public void setPrimaryFunction(String function) { try { FileInputStream stream = new FileInputStream(getFunctionEnableFile(function)); boolean enabled = (stream.read() == '1'); stream.close(); return enabled; } catch (IOException e) { return false; mService.setPrimaryFunction(function); } catch (RemoteException e) { Log.e(TAG, "RemoteException in setPrimaryFunction", e); } } /** * Enables or disables a USB function. * Sets the default primary USB function. * * @param function name of the USB function * * {@hide} */ public static boolean setFunctionEnabled(String function, boolean enable) { public void setDefaultFunction(String function) { try { FileOutputStream stream = new FileOutputStream(getFunctionEnableFile(function)); stream.write(enable ? '1' : '0'); stream.close(); return true; } catch (IOException e) { return false; mService.setDefaultFunction(function); } catch (RemoteException e) { Log.e(TAG, "RemoteException in setDefaultFunction", e); } } /** * Sets the file path for USB mass storage backing file. * * @param path backing file path * * {@hide} */ public void setMassStorageBackingFile(String path) { try { mService.setMassStorageBackingFile(path); } catch (RemoteException e) { Log.e(TAG, "RemoteException in setDefaultFunction", e); } } }
services/java/com/android/server/SystemServer.java +2 −2 Original line number Diff line number Diff line Loading @@ -406,8 +406,8 @@ class ServerThread extends Thread { } try { Slog.i(TAG, "USB Observer"); // Listen for USB changes Slog.i(TAG, "USB Service"); // Manage USB host and device support usb = new UsbService(context); ServiceManager.addService(Context.USB_SERVICE, usb); } catch (Throwable e) { Loading
services/java/com/android/server/usb/UsbDeviceManager.java +357 −305 File changed.Preview size limit exceeded, changes collapsed. Show changes
services/java/com/android/server/usb/UsbService.java +29 −2 Original line number Diff line number Diff line Loading @@ -50,7 +50,7 @@ public class UsbService extends IUsbManager.Stub { if (pm.hasSystemFeature(PackageManager.FEATURE_USB_HOST)) { mHostManager = new UsbHostManager(context, mSettingsManager); } if (new File("/sys/class/usb_composite").exists()) { if (new File("/sys/class/android_usb").exists()) { mDeviceManager = new UsbDeviceManager(context, mSettingsManager); } } Loading Loading @@ -92,7 +92,7 @@ public class UsbService extends IUsbManager.Stub { /* opens the currently attached USB accessory (device mode) */ public ParcelFileDescriptor openAccessory(UsbAccessory accessory) { if (mDeviceManager != null) { return openAccessory(accessory); return mDeviceManager.openAccessory(accessory); } else { return null; } Loading Loading @@ -146,6 +146,33 @@ public class UsbService extends IUsbManager.Stub { mSettingsManager.clearDefaults(packageName); } public void setPrimaryFunction(String function) { mContext.enforceCallingOrSelfPermission(android.Manifest.permission.MANAGE_USB, null); if (mDeviceManager != null) { mDeviceManager.setPrimaryFunction(function); } else { throw new IllegalStateException("USB device mode not supported"); } } public void setDefaultFunction(String function) { mContext.enforceCallingOrSelfPermission(android.Manifest.permission.MANAGE_USB, null); if (mDeviceManager != null) { mDeviceManager.setDefaultFunction(function); } else { throw new IllegalStateException("USB device mode not supported"); } } public void setMassStorageBackingFile(String path) { mContext.enforceCallingOrSelfPermission(android.Manifest.permission.MANAGE_USB, null); if (mDeviceManager != null) { mDeviceManager.setMassStorageBackingFile(path); } else { throw new IllegalStateException("USB device mode not supported"); } } @Override public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP) Loading