Enumerator.java
Go to the documentation of this file.
1 package com.intel.realsense.librealsense;
2 
3 import android.content.BroadcastReceiver;
4 import android.content.Context;
5 import android.content.Intent;
6 import android.content.IntentFilter;
7 import android.hardware.usb.UsbManager;
8 import android.os.Handler;
9 import android.os.HandlerThread;
10 import android.os.Looper;
11 import android.os.Message;
12 import android.util.Log;
13 
18 class Enumerator {
19  private static final String TAG = "librs Enumerator";
20 
21  private Context mContext;
22  private DeviceListener mListener;
23  private HandlerThread mMessagesHandler;
24  private Handler mHandler;
25 
26  private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
27  @Override
28  public void onReceive(Context context, Intent intent) {
29  String action = intent.getAction();
30  Log.i(TAG, "onReceive: " + action);
31 
32  switch(action){
33  case UsbManager.ACTION_USB_DEVICE_ATTACHED:
34  case UsbUtilities.ACTION_USB_PERMISSION:
35  onDeviceAttach(context);
36  break;
37  case UsbManager.ACTION_USB_DEVICE_DETACHED:
38  onDeviceDetach();
39  break;
40  }
41  }
42  };
43 
50  public Enumerator(Context context, DeviceListener listener){
51  if(listener == null) {
52  Log.e(TAG, "Enumerator: provided listener is null");
53  throw new NullPointerException("provided listener is null");
54  }
55  if(context == null) {
56  Log.e(TAG, "Enumerator: provided context is null");
57  throw new NullPointerException("provided context is null");
58  }
59 
60  mMessagesHandler = new HandlerThread("DeviceManager device availability message thread");
61  mMessagesHandler.start();
62  mHandler = new MessagesHandler(mMessagesHandler.getLooper());
63 
64  mListener = listener;
65  mContext = context;
66 
67  context.registerReceiver(mBroadcastReceiver, new IntentFilter(UsbUtilities.ACTION_USB_PERMISSION));
68  context.registerReceiver(mBroadcastReceiver, new IntentFilter(UsbManager.ACTION_USB_DEVICE_ATTACHED));
69  context.registerReceiver(mBroadcastReceiver, new IntentFilter(UsbManager.ACTION_USB_DEVICE_DETACHED));
70 
71  onDeviceAttach(context);
72  }
73 
77  public synchronized void close() {
78  if(mContext != null)
79  mContext.unregisterReceiver(mBroadcastReceiver);
80  mMessagesHandler.quitSafely();
81  Log.v(TAG, "joining message handler");
82  try {
83  mMessagesHandler.join();
84  } catch (InterruptedException e) {
85  Log.e(TAG, "close: " + e.getMessage());
86  }
87  Log.v(TAG, "joined message handler");
88  mMessagesHandler = null;
89  }
90 
91  @Override
92  protected void finalize() throws Throwable {
93  if(mMessagesHandler != null)
94  close();
95  super.finalize();
96  }
97 
98  private synchronized void notifyOnAttach() throws Exception {
99  if(mListener != null) {
100  Log.i(TAG, "notifyOnAttach");
101 
102  mListener.onDeviceAttach();
103  }
104  }
105 
106  private synchronized void notifyOnDetach() throws Exception {
107  if(mListener != null) {
108  Log.i(TAG, "notifyOnDetach");
109 
110  mListener.onDeviceDetach();
111  }
112  }
113 
114  private void onDeviceAttach(Context context){
115  Message msg = Message.obtain();
116  UsbUtilities.grantUsbPermissionIfNeeded(context);
117  msg.what = MessagesHandler.ON_DEVICE_AVAILABLE;
118  mHandler.sendMessage(msg);
119  }
120 
121  private void onDeviceDetach() {
122  Message msg = Message.obtain();
123  msg.what = MessagesHandler.ON_DEVICE_UNAVAILABLE;
124  mHandler.sendMessage(msg);
125  }
126 
127  private class MessagesHandler extends Handler {
128  private static final String TAG = "librs MessagesHandler";
129 
130  public static final int ON_DEVICE_AVAILABLE = 0;
131  public static final int ON_DEVICE_UNAVAILABLE = 1;
132 
133 
134  public MessagesHandler(Looper looper) {
135  super(looper);
136  }
137 
138  @Override
139  public void handleMessage(android.os.Message msg) {
140  try{
141  switch(msg.what) {
142  case ON_DEVICE_AVAILABLE:
143  Log.i(TAG, "handleMessage: realsense device attached");
144  notifyOnAttach();
145  break;
146  case ON_DEVICE_UNAVAILABLE:
147  Log.i(TAG, "handleMessage: realsense device detached");
148  notifyOnDetach();
149  break;
150  }
151  }
152  catch (Exception e) {
153  Log.e(TAG, "handleMessage: failed to open device, error: " + e.getMessage());
154  }
155  }
156  }
157 }
::rosgraph_msgs::Log_< std::allocator< void > > Log
Definition: Log.h:88
uvc_xu_option< int > super
Definition: l500-options.h:32
Exception::ExceptionMessageMatcher Message(std::string const &message)
e
Definition: rmse.py:177
::std_msgs::String_< std::allocator< void > > String
Definition: String.h:47
action
Definition: enums.py:62


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