Class ShadowUsbManager


  • @Implements(value=android.hardware.usb.UsbManager.class,
                looseSignatures=true)
    public class ShadowUsbManager
    extends java.lang.Object
    Robolectric implementation of UsbManager.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void addOrUpdateUsbDevice​(android.hardware.usb.UsbDevice usbDevice, boolean hasPermission)
      Adds a USB device into available USB devices map with permission value.
      void addPort​(java.lang.String portId)
      Adds a USB port with given ID to UsbManager.
      void addPort​(java.lang.String portId, int statusCurrentMode, int statusCurrentPowerRole, int statusCurrentDataRole, int statusSupportedRoleCombinations)
      Adds a USB port with given ID and UsbPortStatus parameters to UsbManager for Q+.
      void clearPorts()
      Remove all added ports from UsbManager.
      protected android.hardware.usb.UsbAccessory[] getAccessoryList()  
      protected java.util.HashMap<java.lang.String,​android.hardware.usb.UsbDevice> getDeviceList()
      Returns a HashMap containing all USB devices currently attached.
      protected java.lang.Object getPorts()  
      protected java.lang.Object getPortStatus​(java.lang.Object port)  
      java.lang.Object getPortStatus​(java.lang.String portId)
      Returns the UsbPortStatus corresponding to the UsbPort with given portId if present; otherwise returns null.
      protected void grantPermission​(android.hardware.usb.UsbDevice device)  
      protected void grantPermission​(android.hardware.usb.UsbDevice device, java.lang.String packageName)  
      protected boolean hasPermission​(android.hardware.usb.UsbDevice device)
      Returns true if the caller has permission to access the device.
      boolean hasPermissionForPackage​(android.hardware.usb.UsbDevice device, java.lang.String packageName)
      Returns true if the given package has permission to access the device.
      protected android.os.ParcelFileDescriptor openAccessory​(android.hardware.usb.UsbAccessory accessory)
      Opens a file descriptor from a temporary file.
      void removeUsbDevice​(android.hardware.usb.UsbDevice usbDevice)
      Removes a USB device from available USB devices map.
      void revokePermission​(android.hardware.usb.UsbDevice device, java.lang.String packageName)
      Revokes permission to a USB device granted to a package.
      void setAttachedUsbAccessory​(android.hardware.usb.UsbAccessory usbAccessory)
      Sets the currently attached Usb accessory returned in #getAccessoryList.
      protected void setPortRoles​(java.lang.Object port, java.lang.Object powerRole, java.lang.Object dataRole)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • ShadowUsbManager

        public ShadowUsbManager()
    • Method Detail

      • hasPermission

        @Implementation
        protected boolean hasPermission​(android.hardware.usb.UsbDevice device)
        Returns true if the caller has permission to access the device.
      • hasPermissionForPackage

        public boolean hasPermissionForPackage​(android.hardware.usb.UsbDevice device,
                                               java.lang.String packageName)
        Returns true if the given package has permission to access the device.
      • grantPermission

        @Implementation(minSdk=24)
        @HiddenApi
        protected void grantPermission​(android.hardware.usb.UsbDevice device)
      • grantPermission

        @Implementation(minSdk=25)
        @HiddenApi
        protected void grantPermission​(android.hardware.usb.UsbDevice device,
                                       java.lang.String packageName)
      • revokePermission

        public void revokePermission​(android.hardware.usb.UsbDevice device,
                                     java.lang.String packageName)
        Revokes permission to a USB device granted to a package. This method does nothing if the package doesn't have permission to access the device.
      • getDeviceList

        @Implementation
        protected java.util.HashMap<java.lang.String,​android.hardware.usb.UsbDevice> getDeviceList()
        Returns a HashMap containing all USB devices currently attached. USB device name is the key for the returned HashMap. The result will be empty if no devices are attached, or if USB host mode is inactive or unsupported.
      • getAccessoryList

        @Implementation
        protected android.hardware.usb.UsbAccessory[] getAccessoryList()
      • setAttachedUsbAccessory

        public void setAttachedUsbAccessory​(android.hardware.usb.UsbAccessory usbAccessory)
        Sets the currently attached Usb accessory returned in #getAccessoryList.
      • addOrUpdateUsbDevice

        public void addOrUpdateUsbDevice​(android.hardware.usb.UsbDevice usbDevice,
                                         boolean hasPermission)
        Adds a USB device into available USB devices map with permission value. If the USB device already exists, updates the USB device with new permission value.
      • removeUsbDevice

        public void removeUsbDevice​(android.hardware.usb.UsbDevice usbDevice)
        Removes a USB device from available USB devices map.
      • getPorts

        @Implementation(minSdk=23)
        @HiddenApi
        protected java.lang.Object getPorts()
      • clearPorts

        public void clearPorts()
        Remove all added ports from UsbManager.
      • addPort

        public void addPort​(java.lang.String portId)
        Adds a USB port with given ID to UsbManager.
      • addPort

        public void addPort​(java.lang.String portId,
                            int statusCurrentMode,
                            int statusCurrentPowerRole,
                            int statusCurrentDataRole,
                            int statusSupportedRoleCombinations)
        Adds a USB port with given ID and UsbPortStatus parameters to UsbManager for Q+.
      • getPortStatus

        public java.lang.Object getPortStatus​(java.lang.String portId)
        Returns the UsbPortStatus corresponding to the UsbPort with given portId if present; otherwise returns null.
      • getPortStatus

        @Implementation(minSdk=23)
        @HiddenApi
        protected java.lang.Object getPortStatus​(java.lang.Object port)
      • setPortRoles

        @Implementation(minSdk=23)
        @HiddenApi
        protected void setPortRoles​(java.lang.Object port,
                                    java.lang.Object powerRole,
                                    java.lang.Object dataRole)
      • openAccessory

        @Implementation
        protected android.os.ParcelFileDescriptor openAccessory​(android.hardware.usb.UsbAccessory accessory)
        Opens a file descriptor from a temporary file.