UsbUtilities.java
Go to the documentation of this file.
1 package com.intel.realsense.librealsense;
2 
3 import android.app.PendingIntent;
4 import android.content.Context;
5 import android.content.Intent;
6 import android.hardware.usb.UsbDevice;
7 import android.hardware.usb.UsbManager;
8 import android.util.Log;
9 
10 import java.lang.reflect.Array;
11 import java.util.ArrayList;
12 import java.util.HashMap;
13 import java.util.List;
14 import java.util.Map;
15 
16 public class UsbUtilities {
17  private static final String TAG = "librs UsbUtilities";
18  public static final String ACTION_USB_PERMISSION = "USB_CONTROL_PERMISSION";
19 
20  public static boolean isIntel(UsbDevice usbDevice){
21  if (usbDevice.getVendorId() == 0x8086)
22  return true;
23  return false;
24  }
25 
26  private static List<UsbDevice> getUsbDevices(Context context, Integer vId) {
27  return getUsbDevices(context, vId, 0);
28  }
29 
30  private static List<UsbDevice> getUsbDevices(Context context, Integer vId, Integer pId) {
31  ArrayList<UsbDevice> res = new ArrayList<>();
32  UsbManager usbManager = (UsbManager) context.getSystemService(Context.USB_SERVICE);
33 
34  HashMap<String, UsbDevice> devicesMap = usbManager.getDeviceList();
35  for (Map.Entry<String, UsbDevice> entry : devicesMap.entrySet()) {
36  UsbDevice usbDevice = entry.getValue();
37  if (usbDevice.getVendorId() == vId && (usbDevice.getProductId() == pId || pId == 0)) {
38  res.add(usbDevice);
39  }
40  }
41  if (res.isEmpty())
42  Log.w(TAG, "getUsbDevice: failed to locate USB device, " + "VID: " + String.format("0x%04x", vId) + ", PID: " + String.format("0x%04x", pId));
43  return res;
44  }
45 
46  private static boolean hasUsbPermission(Context context, UsbDevice usbDevice){
47  Log.d(TAG, "hasUsbPermission");
48 
49  if(usbDevice == null){
50  Log.w(TAG, "hasUsbPermission: null USB device");
51  return false;
52  }
53 
54  UsbManager usbManager = (UsbManager) context.getSystemService(Context.USB_SERVICE);
55  return usbManager.hasPermission(usbDevice);
56  }
57 
58  private static void grantUsbPermissions(Context context, UsbDevice usbDevice){
59  Log.d(TAG, "grantUsbPermissions");
60 
61  if(usbDevice == null){
62  Log.w(TAG, "grantUsbPermissions: null USB device");
63  return;
64  }
65 
66  UsbManager usbManager = (UsbManager) context.getSystemService(Context.USB_SERVICE);
67  boolean permission = usbManager.hasPermission(usbDevice);
68 
69  if(!permission) {
70  Log.i(TAG, "grantUsbPermissions:\ndevice: " + usbDevice.toString());
71  PendingIntent pi = PendingIntent.getBroadcast(context, 0, new Intent(UsbUtilities.ACTION_USB_PERMISSION), 0);
72  usbManager.requestPermission(usbDevice, pi);
73  }
74  }
75 
76  private static List<UsbDevice> getDevices(Context context) {
77  return getUsbDevices(context, 0x8086);
78  }
79 
80  public static void grantUsbPermissionIfNeeded(Context context) {
81  List<UsbDevice> usbDevices = getDevices(context);
82  for (UsbDevice usbDevice : usbDevices) {
83  if (!hasUsbPermission(context, usbDevice)) {
84  grantUsbPermissions(context, usbDevice);
85  }
86  }
87  }
88 }
static List< UsbDevice > getUsbDevices(Context context, Integer vId, Integer pId)
::rosgraph_msgs::Log_< std::allocator< void > > Log
Definition: Log.h:88
GLuint entry
Definition: glext.h:10991
static void grantUsbPermissions(Context context, UsbDevice usbDevice)
::std_msgs::String_< std::allocator< void > > String
Definition: String.h:47
static List< UsbDevice > getUsbDevices(Context context, Integer vId)
static List< UsbDevice > getDevices(Context context)
static boolean hasUsbPermission(Context context, UsbDevice usbDevice)
static const double pi
Definition: src/types.h:57
static boolean isIntel(UsbDevice usbDevice)
static void grantUsbPermissionIfNeeded(Context context)
GLuint res
Definition: glext.h:8856


librealsense2
Author(s): Sergey Dorodnicov , Doron Hirshberg , Mark Horn , Reagan Lopez , Itay Carpis
autogenerated on Mon May 3 2021 02:50:13