FileDocCategorySizeDatePackage
UsbService.javaAPI DocAndroid 5.1 API11545Thu Mar 12 22:22:42 GMT 2015com.android.server.usb

UsbService

public class UsbService extends IUsbManager.Stub
UsbService manages all USB related state, including both host and device support. Host related events and calls are delegated to UsbHostManager, and device related support is delegated to UsbDeviceManager.

Fields Summary
private static final String
TAG
private final android.content.Context
mContext
private UsbDeviceManager
mDeviceManager
private UsbHostManager
mHostManager
private final Object
mLock
private final android.util.SparseArray
mSettingsByUser
Map from {@link UserHandle} to {@link UsbSettingsManager}
private android.content.BroadcastReceiver
mUserReceiver
Constructors Summary
public UsbService(android.content.Context context)

        mContext = context;

        final PackageManager pm = mContext.getPackageManager();
        if (pm.hasSystemFeature(PackageManager.FEATURE_USB_HOST)) {
            mHostManager = new UsbHostManager(context);
        }
        if (new File("/sys/class/android_usb").exists()) {
            mDeviceManager = new UsbDeviceManager(context);
        }

        setCurrentUser(UserHandle.USER_OWNER);

        final IntentFilter userFilter = new IntentFilter();
        userFilter.addAction(Intent.ACTION_USER_SWITCHED);
        userFilter.addAction(Intent.ACTION_USER_STOPPED);
        mContext.registerReceiver(mUserReceiver, userFilter, null, null);
    
Methods Summary
public voidallowUsbDebugging(boolean alwaysAllow, java.lang.String publicKey)

        mContext.enforceCallingOrSelfPermission(android.Manifest.permission.MANAGE_USB, null);
        mDeviceManager.allowUsbDebugging(alwaysAllow, publicKey);
    
public voidclearDefaults(java.lang.String packageName, int userId)

        mContext.enforceCallingOrSelfPermission(android.Manifest.permission.MANAGE_USB, null);
        getSettingsForUser(userId).clearDefaults(packageName);
    
public voidclearUsbDebuggingKeys()

        mContext.enforceCallingOrSelfPermission(android.Manifest.permission.MANAGE_USB, null);
        mDeviceManager.clearUsbDebuggingKeys();
    
public voiddenyUsbDebugging()

        mContext.enforceCallingOrSelfPermission(android.Manifest.permission.MANAGE_USB, null);
        mDeviceManager.denyUsbDebugging();
    
public voiddump(java.io.FileDescriptor fd, java.io.PrintWriter writer, java.lang.String[] args)

        mContext.enforceCallingOrSelfPermission(android.Manifest.permission.DUMP, TAG);
        final IndentingPrintWriter pw = new IndentingPrintWriter(writer, "  ");

        pw.println("USB Manager State:");
        if (mDeviceManager != null) {
            mDeviceManager.dump(fd, pw);
        }
        if (mHostManager != null) {
            mHostManager.dump(fd, pw);
        }

        synchronized (mLock) {
            for (int i = 0; i < mSettingsByUser.size(); i++) {
                final int userId = mSettingsByUser.keyAt(i);
                final UsbSettingsManager settings = mSettingsByUser.valueAt(i);
                pw.increaseIndent();
                pw.println("Settings for user " + userId + ":");
                settings.dump(fd, pw);
                pw.decreaseIndent();
            }
        }
        pw.decreaseIndent();
    
public android.hardware.usb.UsbAccessorygetCurrentAccessory()

        if (mDeviceManager != null) {
            return mDeviceManager.getCurrentAccessory();
        } else {
            return null;
        }
    
public voidgetDeviceList(android.os.Bundle devices)

        if (mHostManager != null) {
            mHostManager.getDeviceList(devices);
        }
    
private UsbSettingsManagergetSettingsForUser(int userId)


        
        synchronized (mLock) {
            UsbSettingsManager settings = mSettingsByUser.get(userId);
            if (settings == null) {
                settings = new UsbSettingsManager(mContext, new UserHandle(userId));
                mSettingsByUser.put(userId, settings);
            }
            return settings;
        }
    
public voidgrantAccessoryPermission(android.hardware.usb.UsbAccessory accessory, int uid)

        mContext.enforceCallingOrSelfPermission(android.Manifest.permission.MANAGE_USB, null);
        final int userId = UserHandle.getUserId(uid);
        getSettingsForUser(userId).grantAccessoryPermission(accessory, uid);
    
public voidgrantDevicePermission(android.hardware.usb.UsbDevice device, int uid)

        mContext.enforceCallingOrSelfPermission(android.Manifest.permission.MANAGE_USB, null);
        final int userId = UserHandle.getUserId(uid);
        getSettingsForUser(userId).grantDevicePermission(device, uid);
    
public booleanhasAccessoryPermission(android.hardware.usb.UsbAccessory accessory)

        final int userId = UserHandle.getCallingUserId();
        return getSettingsForUser(userId).hasPermission(accessory);
    
public booleanhasDefaults(java.lang.String packageName, int userId)

        mContext.enforceCallingOrSelfPermission(android.Manifest.permission.MANAGE_USB, null);
        return getSettingsForUser(userId).hasDefaults(packageName);
    
public booleanhasDevicePermission(android.hardware.usb.UsbDevice device)

        final int userId = UserHandle.getCallingUserId();
        return getSettingsForUser(userId).hasPermission(device);
    
public android.os.ParcelFileDescriptoropenAccessory(android.hardware.usb.UsbAccessory accessory)

        if (mDeviceManager != null) {
            return mDeviceManager.openAccessory(accessory);
        } else {
            return null;
        }
    
public android.os.ParcelFileDescriptoropenDevice(java.lang.String deviceName)

        if (mHostManager != null) {
            return mHostManager.openDevice(deviceName);
        } else {
            return null;
        }
    
public voidrequestAccessoryPermission(android.hardware.usb.UsbAccessory accessory, java.lang.String packageName, android.app.PendingIntent pi)

        final int userId = UserHandle.getCallingUserId();
        getSettingsForUser(userId).requestPermission(accessory, packageName, pi);
    
public voidrequestDevicePermission(android.hardware.usb.UsbDevice device, java.lang.String packageName, android.app.PendingIntent pi)

        final int userId = UserHandle.getCallingUserId();
        getSettingsForUser(userId).requestPermission(device, packageName, pi);
    
public voidsetAccessoryPackage(android.hardware.usb.UsbAccessory accessory, java.lang.String packageName, int userId)

        mContext.enforceCallingOrSelfPermission(android.Manifest.permission.MANAGE_USB, null);
        getSettingsForUser(userId).setAccessoryPackage(accessory, packageName);
    
public voidsetCurrentFunction(java.lang.String function, boolean makeDefault)

        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);
            return;
        }

        if (mDeviceManager != null) {
            mDeviceManager.setCurrentFunctions(function, makeDefault);
        } else {
            throw new IllegalStateException("USB device mode not supported");
        }
    
private voidsetCurrentUser(int userId)


        
        final UsbSettingsManager userSettings = getSettingsForUser(userId);
        if (mHostManager != null) {
            mHostManager.setCurrentSettings(userSettings);
        }
        if (mDeviceManager != null) {
            mDeviceManager.setCurrentSettings(userSettings);
        }
    
public voidsetDevicePackage(android.hardware.usb.UsbDevice device, java.lang.String packageName, int userId)

        mContext.enforceCallingOrSelfPermission(android.Manifest.permission.MANAGE_USB, null);
        getSettingsForUser(userId).setDevicePackage(device, packageName);
    
public voidsetMassStorageBackingFile(java.lang.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");
        }
    
public voidsystemReady()

        if (mDeviceManager != null) {
            mDeviceManager.systemReady();
        }
        if (mHostManager != null) {
            mHostManager.systemReady();
        }