DeviceWatcher.java
Go to the documentation of this file.
1 package com.intel.realsense.librealsense;
2 
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;
8 
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;
14 import java.util.Map;
15 
16 class DeviceWatcher extends LrsClass {
17  private static final String TAG = "librs DeviceWatcher";
18 
19  private final List<DeviceListener> mAppDeviceListener;
20 
21  public synchronized void addListener(DeviceListener deviceListener){
22  if(!mAppDeviceListener.contains(deviceListener)) {
23  mAppDeviceListener.add(deviceListener);
24  }
25  }
26 
27  public synchronized void removeListener(DeviceListener deviceListener){
28  mAppDeviceListener.remove(deviceListener);
29  }
30 
31  public int getDeviceCount() {
32  return mDescriptors.size();
33  }
34 
35  private final Context mContext;
36  private final Enumerator mEnumerator;
37 
38  private HashMap<String, UsbDesc> mDescriptors = new LinkedHashMap<>();
39 
40 
41  private DeviceListener mListener = new DeviceListener() {
42  @Override
43  public void onDeviceAttach() {
44  invalidateDevices();
45  }
46 
47  @Override
48  public void onDeviceDetach() {
49  invalidateDevices();
50  }
51  };
52 
53  DeviceWatcher(Context context){
54  mContext = context;
55  mEnumerator = new Enumerator(mContext, mListener);
56  mAppDeviceListener = new ArrayList<>();
57  }
58 
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());
67  }
68  Iterator<Map.Entry<String, UsbDesc>> iter = mDescriptors.entrySet().iterator();
69  while (iter.hasNext()) {
70  Map.Entry<String, UsbDesc> entry = iter.next();
71  if(!intelDevices.contains(entry.getKey())) {
72  removeDevice(entry.getValue());
73  iter.remove();
74  }
75  }
76  for(String name : intelDevices) {
77  if(!mDescriptors.containsKey(name)) {
78  addDevice(devicesMap.get(name));
79  }
80  }
81  }
82 
83  private enum UpdateListenersType {
85  DETACH
86  }
87 
88  private void updateListeners(UpdateListenersType type){
89  for(DeviceListener listener : mAppDeviceListener) {
90  try {
91  if (type == UpdateListenersType.ATTACH)
92  listener.onDeviceAttach();
93  else
94  listener.onDeviceDetach();
95  } catch (Exception e) {
96  Log.e(TAG, e.getMessage());
97  }
98  }
99  }
100 
101  private void removeDevice(UsbDesc desc) {
102  Log.d(TAG, "Removing device: " + desc.name);
103 
104  nRemoveUsbDevice(desc.descriptor);
105  desc.connection.close();
106  updateListeners(UpdateListenersType.DETACH);
107  Log.d(TAG, "Device: " + desc.name + " removed successfully");
108  }
109 
110  private void addDevice(UsbDevice device) {
111  if (device == null)
112  return;
113 
114  UsbManager usbManager = (UsbManager) mContext.getSystemService(Context.USB_SERVICE);
115  UsbDeviceConnection conn = usbManager.openDevice(device);
116  if(conn == null)
117  return;
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);
122 
123  updateListeners(UpdateListenersType.ATTACH);
124  Log.d(TAG, "Device: " + desc.name + " added successfully");
125  }
126 
127  @Override
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());
133  iter.remove();
134  }
135  }
136 
137  private static native void nAddUsbDevice(String deviceName, int fileDescriptor);
138  private static native void nRemoveUsbDevice(int fileDescriptor);
139 }
::rosgraph_msgs::Log_< std::allocator< void > > Log
Definition: Log.h:88
GLuint const GLchar * name
GLuint entry
Definition: glext.h:10991
e
Definition: rmse.py:177
::std_msgs::String_< std::allocator< void > > String
Definition: String.h:47
GLenum type
auto device
Definition: pyrs_net.cpp:17


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