]> sigrok.org Git - sigrok-androidutils.git/blame - src/org/sigrok/androidutils/UsbHelper.java
UsbHelper: Add additional methods for device enumeration and monitoring
[sigrok-androidutils.git] / src / org / sigrok / androidutils / UsbHelper.java
CommitLineData
ea3ce762 1/*
5de7ce63 2 * This file is part of the sigrok-androidutils project.
ea3ce762
MC
3 *
4 * Copyright (C) 2014 Marcus Comstedt <marcus@mc.pp.se>
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20package org.sigrok.androidutils;
21
22import android.content.Context;
23import android.hardware.usb.UsbDevice;
24import android.hardware.usb.UsbDeviceConnection;
25import android.hardware.usb.UsbManager;
26import android.util.Log;
27import java.util.HashMap;
28
29public final class UsbHelper
30{
6f1c3a93 31 private static UsbManager manager;
054991d3
MC
32 private static Context context;
33 private static UsbEventMonitor eventMonitor;
ea3ce762 34
6f1c3a93
UH
35 public static void setContext(Context ctx)
36 {
054991d3 37 context = ctx;
6f1c3a93
UH
38 if (ctx == null)
39 manager = null;
40 else
41 manager = (UsbManager) ctx.getSystemService(Context.USB_SERVICE);
ea3ce762 42 }
6f1c3a93
UH
43
44 private static int open(UsbManager manager, String name, int mode)
45 {
46 if (manager == null) {
47 Log.w("UsbHelper", "no manager");
48 return -1;
49 }
50 HashMap<String,UsbDevice> devlist = manager.getDeviceList();
51 UsbDevice dev = (devlist == null ? null : devlist.get(name));
52 if (dev == null)
53 return -1;
54 if (!manager.hasPermission(dev))
55 return -1;
56 UsbDeviceConnection conn = manager.openDevice(dev);
57 return (conn == null ? -1 : conn.getFileDescriptor());
ea3ce762 58 }
ea3ce762 59
054991d3
MC
60 private static synchronized void startEventMonitor(Context context, UsbManager manager, UsbEventListener listener)
61 {
62 if (eventMonitor != null) {
63 eventMonitor.stop();
64 eventMonitor = null;
65 }
66 if (context == null) {
67 Log.w("UsbHelper", "no context");
68 return;
69 }
70 if (manager == null) {
71 Log.w("UsbHelper", "no manager");
72 return;
73 }
74 eventMonitor = new UsbEventMonitor(context, manager, listener);
75 eventMonitor.start();
76 }
77
78 private static synchronized void stopEventMonitor(Context context)
79 {
80 if (eventMonitor != null) {
81 eventMonitor.stop();
82 eventMonitor = null;
83 }
84 }
85
86 private static String[] scanDevices(UsbManager manager)
87 {
88 if (manager == null) {
89 Log.w("UsbHelper", "no manager");
90 return null;
91 }
92 HashMap<String,UsbDevice> devlist = manager.getDeviceList();
93 if (devlist == null)
94 return null;
95 String[] list = devlist.keySet().toArray(new String[devlist.size()]);
96 return list;
97 }
98
6f1c3a93
UH
99 public static int open(String name, int mode)
100 {
101 try {
102 return open(manager, name, mode);
103 } catch (Exception e) {
104 Log.w("UsbHelper", "caught exception " + e);
105 return -1;
106 }
ea3ce762 107 }
054991d3
MC
108
109 public static void startEventMonitor(UsbEventListener listener)
110 {
111 try {
112 startEventMonitor(context, manager, listener);
113 } catch (Exception e) {
114 Log.w("UsbHelper", "caught exception " + e);
115 }
116 }
117
118 public static void stopEventMonitor()
119 {
120 try {
121 stopEventMonitor(context);
122 } catch (Exception e) {
123 Log.w("UsbHelper", "caught exception " + e);
124 }
125 }
126
127 public static String[] scanDevices()
128 {
129 try {
130 return scanDevices(manager);
131 } catch (Exception e) {
132 Log.w("UsbHelper", "caught exception " + e);
133 return null;
134 }
135 }
ea3ce762
MC
136}
137