use of android.hardware.usb.UsbDevice in project android_frameworks_base by ParanoidAndroid.
the class UsbHostManager method usbDeviceAdded.
/* Called from JNI in monitorUsbHostBus() to report new USB devices */
private void usbDeviceAdded(String deviceName, int vendorID, int productID, int deviceClass, int deviceSubclass, int deviceProtocol, /* array of quintuples containing id, class, subclass, protocol
and number of endpoints for each interface */
int[] interfaceValues, /* array of quadruples containing address, attributes, max packet size
and interval for each endpoint */
int[] endpointValues) {
if (isBlackListed(deviceName) || isBlackListed(deviceClass, deviceSubclass, deviceProtocol)) {
return;
}
synchronized (mLock) {
if (mDevices.get(deviceName) != null) {
Slog.w(TAG, "device already on mDevices list: " + deviceName);
return;
}
int numInterfaces = interfaceValues.length / 5;
Parcelable[] interfaces = new UsbInterface[numInterfaces];
try {
// repackage interfaceValues as an array of UsbInterface
int intf, endp, ival = 0, eval = 0;
for (intf = 0; intf < numInterfaces; intf++) {
int interfaceId = interfaceValues[ival++];
int interfaceClass = interfaceValues[ival++];
int interfaceSubclass = interfaceValues[ival++];
int interfaceProtocol = interfaceValues[ival++];
int numEndpoints = interfaceValues[ival++];
Parcelable[] endpoints = new UsbEndpoint[numEndpoints];
for (endp = 0; endp < numEndpoints; endp++) {
int address = endpointValues[eval++];
int attributes = endpointValues[eval++];
int maxPacketSize = endpointValues[eval++];
int interval = endpointValues[eval++];
endpoints[endp] = new UsbEndpoint(address, attributes, maxPacketSize, interval);
}
// don't allow if any interfaces are blacklisted
if (isBlackListed(interfaceClass, interfaceSubclass, interfaceProtocol)) {
return;
}
interfaces[intf] = new UsbInterface(interfaceId, interfaceClass, interfaceSubclass, interfaceProtocol, endpoints);
}
} catch (Exception e) {
// beware of index out of bound exceptions, which might happen if
// a device does not set bNumEndpoints correctly
Slog.e(TAG, "error parsing USB descriptors", e);
return;
}
UsbDevice device = new UsbDevice(deviceName, vendorID, productID, deviceClass, deviceSubclass, deviceProtocol, interfaces);
mDevices.put(deviceName, device);
getCurrentSettings().deviceAttached(device);
}
}
use of android.hardware.usb.UsbDevice in project platform_frameworks_base by android.
the class DeviceDisconnectedReceiver method onReceive.
@Override
public void onReceive(Context context, Intent intent) {
UsbDevice device = (UsbDevice) intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
String deviceName = device.getDeviceName();
Log.d(TAG, "ACTION_USB_DEVICE_DETACHED " + deviceName);
// close our activity if the device it is displaying is disconnected
if (deviceName.equals(mDeviceName)) {
mActivity.finish();
}
}
use of android.hardware.usb.UsbDevice in project platform_frameworks_base by android.
the class MtpManager method openDevice.
synchronized MtpDeviceRecord openDevice(int deviceId) throws IOException {
UsbDevice rawDevice = null;
for (final UsbDevice candidate : mManager.getDeviceList().values()) {
if (candidate.getDeviceId() == deviceId) {
rawDevice = candidate;
break;
}
}
ensureNotNull(rawDevice, "Not found USB device: " + deviceId);
if (!mManager.hasPermission(rawDevice)) {
mManager.grantPermission(rawDevice);
if (!mManager.hasPermission(rawDevice)) {
throw new IOException("Failed to grant a device permission.");
}
}
final MtpDevice device = new MtpDevice(rawDevice);
final UsbDeviceConnection connection = ensureNotNull(mManager.openDevice(rawDevice), "Failed to open a USB connection.");
if (!device.open(connection)) {
// We cannot open connection when another application use the device.
throw new BusyDeviceException();
}
// Handle devices that fail to obtain storages just after opening a MTP session.
final int[] storageIds = ensureNotNull(device.getStorageIds(), "Not found MTP storages in the device.");
mDevices.put(deviceId, device);
return createDeviceRecord(rawDevice);
}
use of android.hardware.usb.UsbDevice in project platform_frameworks_base by android.
the class ReceiverActivity method onCreate.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (UsbManager.ACTION_USB_DEVICE_ATTACHED.equals(getIntent().getAction())) {
final UsbDevice device = getIntent().getParcelableExtra(UsbManager.EXTRA_DEVICE);
try {
final MtpDocumentsProvider provider = MtpDocumentsProvider.getInstance();
provider.openDevice(device.getDeviceId());
final String deviceRootId = provider.getDeviceDocumentId(device.getDeviceId());
final Uri uri = DocumentsContract.buildRootUri(MtpDocumentsProvider.AUTHORITY, deviceRootId);
final Intent intent = new Intent(DocumentsContract.ACTION_BROWSE);
intent.setData(uri);
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.putExtra(DocumentsContract.EXTRA_FANCY_FEATURES, true);
this.startActivity(intent);
} catch (IOException exception) {
Log.e(MtpDocumentsProvider.TAG, "Failed to open device", exception);
}
}
finish();
}
use of android.hardware.usb.UsbDevice in project platform_frameworks_base by android.
the class TestUtil method findMtpDevice.
private static UsbDevice findMtpDevice(UsbManager usbManager, MtpManager manager) throws IOException {
final HashMap<String, UsbDevice> devices = usbManager.getDeviceList();
if (devices.size() == 0) {
throw new IOException("Device not found.");
}
final UsbDevice device = devices.values().iterator().next();
// Tries to get ownership of the device in case that another application use it.
if (usbManager.hasPermission(device)) {
final UsbDeviceConnection connection = usbManager.openDevice(device);
for (int i = 0; i < device.getInterfaceCount(); i++) {
// Since the test runs real environment, we need to call claim interface with
// force = true to rob interfaces from other applications.
connection.claimInterface(device.getInterface(i), true);
connection.releaseInterface(device.getInterface(i));
}
connection.close();
}
manager.openDevice(device.getDeviceId());
return device;
}
Aggregations