1 package com.intel.realsense.librealsense;
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;
19 private static final String TAG =
"librs Enumerator";
21 private Context mContext;
22 private DeviceListener mListener;
23 private HandlerThread mMessagesHandler;
24 private Handler mHandler;
26 private BroadcastReceiver mBroadcastReceiver =
new BroadcastReceiver() {
28 public void onReceive(Context context, Intent intent) {
30 Log.i(TAG,
"onReceive: " + action);
33 case UsbManager.ACTION_USB_DEVICE_ATTACHED:
34 case UsbUtilities.ACTION_USB_PERMISSION:
35 onDeviceAttach(context);
37 case UsbManager.ACTION_USB_DEVICE_DETACHED:
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");
56 Log.e(TAG,
"Enumerator: provided context is null");
57 throw new NullPointerException(
"provided context is null");
60 mMessagesHandler =
new HandlerThread(
"DeviceManager device availability message thread");
61 mMessagesHandler.start();
62 mHandler =
new MessagesHandler(mMessagesHandler.getLooper());
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));
71 onDeviceAttach(context);
77 public synchronized void close() {
79 mContext.unregisterReceiver(mBroadcastReceiver);
80 mMessagesHandler.quitSafely();
81 Log.v(TAG,
"joining message handler");
83 mMessagesHandler.join();
84 }
catch (InterruptedException
e) {
85 Log.e(TAG,
"close: " + e.getMessage());
87 Log.v(TAG,
"joined message handler");
88 mMessagesHandler = null;
92 protected void finalize() throws Throwable {
93 if(mMessagesHandler != null)
98 private synchronized void notifyOnAttach() throws Exception {
99 if(mListener != null) {
100 Log.i(TAG,
"notifyOnAttach");
102 mListener.onDeviceAttach();
106 private synchronized void notifyOnDetach() throws Exception {
107 if(mListener != null) {
108 Log.i(TAG,
"notifyOnDetach");
110 mListener.onDeviceDetach();
114 private void onDeviceAttach(Context context){
116 UsbUtilities.grantUsbPermissionIfNeeded(context);
117 msg.what = MessagesHandler.ON_DEVICE_AVAILABLE;
118 mHandler.sendMessage(msg);
121 private void onDeviceDetach() {
123 msg.what = MessagesHandler.ON_DEVICE_UNAVAILABLE;
124 mHandler.sendMessage(msg);
128 private static final String TAG =
"librs MessagesHandler";
142 case ON_DEVICE_AVAILABLE:
143 Log.i(TAG,
"handleMessage: realsense device attached");
146 case ON_DEVICE_UNAVAILABLE:
147 Log.i(TAG,
"handleMessage: realsense device detached");
152 catch (Exception e) {
153 Log.e(TAG,
"handleMessage: failed to open device, error: " + e.getMessage());
::rosgraph_msgs::Log_< std::allocator< void > > Log
uvc_xu_option< int > super
Exception::ExceptionMessageMatcher Message(std::string const &message)
MessagesHandler(Looper looper)
::std_msgs::String_< std::allocator< void > > String
void handleMessage(android.os.Message msg)
static final int ON_DEVICE_UNAVAILABLE
static final int ON_DEVICE_AVAILABLE