1 package com.intel.realsense.librealsense;
3 import android.content.Context;
4 import android.hardware.usb.UsbDevice;
5 import android.hardware.usb.UsbDeviceConnection;
6 import android.hardware.usb.UsbManager;
7 import android.util.Log;
9 import java.util.ArrayList;
10 import java.util.HashMap;
11 import java.util.Iterator;
12 import java.util.LinkedHashMap;
13 import java.util.List;
16 class DeviceWatcher
extends LrsClass {
17 private static final String TAG =
"librs DeviceWatcher";
19 private final List<DeviceListener> mAppDeviceListener;
21 public synchronized void addListener(DeviceListener deviceListener){
22 if(!mAppDeviceListener.contains(deviceListener)) {
23 mAppDeviceListener.add(deviceListener);
27 public synchronized void removeListener(DeviceListener deviceListener){
28 mAppDeviceListener.remove(deviceListener);
31 public int getDeviceCount() {
32 return mDescriptors.size();
35 private final Context mContext;
36 private final Enumerator mEnumerator;
38 private HashMap<String, UsbDesc> mDescriptors =
new LinkedHashMap<>();
41 private DeviceListener mListener =
new DeviceListener() {
43 public void onDeviceAttach() {
48 public void onDeviceDetach() {
53 DeviceWatcher(Context context){
55 mEnumerator =
new Enumerator(mContext, mListener);
56 mAppDeviceListener =
new ArrayList<>();
59 private synchronized void invalidateDevices() {
60 UsbManager usbManager = (UsbManager) mContext.getSystemService(Context.USB_SERVICE);
61 HashMap<String, UsbDevice> devicesMap = usbManager.getDeviceList();
62 List<String> intelDevices =
new ArrayList<String>();
63 for (Map.Entry<
String, UsbDevice>
entry : devicesMap.entrySet()) {
64 UsbDevice usbDevice =
entry.getValue();
65 if (UsbUtilities.isIntel(usbDevice))
66 intelDevices.add(
entry.getKey());
68 Iterator<Map.Entry<
String, UsbDesc>> iter = mDescriptors.entrySet().iterator();
69 while (iter.hasNext()) {
71 if(!intelDevices.contains(entry.getKey())) {
72 removeDevice(entry.getValue());
77 if(!mDescriptors.containsKey(
name)) {
78 addDevice(devicesMap.get(
name));
92 listener.onDeviceAttach();
94 listener.onDeviceDetach();
95 }
catch (Exception
e) {
96 Log.e(TAG, e.getMessage());
101 private void removeDevice(UsbDesc desc) {
102 Log.d(TAG,
"Removing device: " + desc.name);
104 nRemoveUsbDevice(desc.descriptor);
105 desc.connection.close();
107 Log.d(TAG,
"Device: " + desc.name +
" removed successfully");
110 private void addDevice(UsbDevice
device) {
114 UsbManager usbManager = (UsbManager) mContext.getSystemService(Context.USB_SERVICE);
115 UsbDeviceConnection conn = usbManager.openDevice(device);
118 UsbDesc desc =
new UsbDesc(device.getDeviceName(), conn.getFileDescriptor(), conn);
119 Log.d(TAG,
"Adding device: " + desc.name);
120 mDescriptors.put(device.getDeviceName(), desc);
121 nAddUsbDevice(desc.name, desc.descriptor);
124 Log.d(TAG,
"Device: " + desc.name +
" added successfully");
128 public void close() {
129 Iterator<Map.Entry<
String, UsbDesc>> iter = mDescriptors.entrySet().iterator();
130 while (iter.hasNext()) {
131 Map.Entry<
String, UsbDesc> entry = iter.next();
132 removeDevice(entry.getValue());
137 private static native
void nAddUsbDevice(
String deviceName,
int fileDescriptor);
138 private static native
void nRemoveUsbDevice(
int fileDescriptor);
::rosgraph_msgs::Log_< std::allocator< void > > Log
GLuint const GLchar * name
::std_msgs::String_< std::allocator< void > > String