2 * This file is part of the sigrok-androidutils project.
4 * Copyright (C) 2014 Marcus Comstedt <marcus@mc.pp.se>
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.
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.
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/>.
20 package org.sigrok.androidutils;
22 import android.content.Context;
23 import android.hardware.usb.UsbDevice;
24 import android.hardware.usb.UsbDeviceConnection;
25 import android.hardware.usb.UsbManager;
26 import android.util.Log;
27 import java.util.HashMap;
29 public final class UsbHelper
31 private static UsbManager manager;
32 private static Context context;
33 private static UsbEventMonitor eventMonitor;
35 public static void setContext(Context ctx)
41 manager = (UsbManager) ctx.getSystemService(Context.USB_SERVICE);
44 private static int open(UsbManager manager, String name, int mode)
46 if (manager == null) {
47 Log.w("UsbHelper", "no manager");
50 HashMap<String,UsbDevice> devlist = manager.getDeviceList();
51 UsbDevice dev = (devlist == null ? null : devlist.get(name));
54 if (!manager.hasPermission(dev))
56 UsbDeviceConnection conn = manager.openDevice(dev);
57 return (conn == null ? -1 : conn.getFileDescriptor());
60 private static synchronized void startEventMonitor(Context context, UsbManager manager, UsbEventListener listener)
62 if (eventMonitor != null) {
66 if (context == null) {
67 Log.w("UsbHelper", "no context");
70 if (manager == null) {
71 Log.w("UsbHelper", "no manager");
74 eventMonitor = new UsbEventMonitor(context, manager, listener);
78 private static synchronized void stopEventMonitor(Context context)
80 if (eventMonitor != null) {
86 private static String[] scanDevices(UsbManager manager)
88 if (manager == null) {
89 Log.w("UsbHelper", "no manager");
92 HashMap<String,UsbDevice> devlist = manager.getDeviceList();
95 String[] list = devlist.keySet().toArray(new String[devlist.size()]);
99 public static int open(String name, int mode)
102 return open(manager, name, mode);
103 } catch (Exception e) {
104 Log.w("UsbHelper", "caught exception " + e);
109 public static void startEventMonitor(UsbEventListener listener)
112 startEventMonitor(context, manager, listener);
113 } catch (Exception e) {
114 Log.w("UsbHelper", "caught exception " + e);
118 public static void stopEventMonitor()
121 stopEventMonitor(context);
122 } catch (Exception e) {
123 Log.w("UsbHelper", "caught exception " + e);
127 public static String[] scanDevices()
130 return scanDevices(manager);
131 } catch (Exception e) {
132 Log.w("UsbHelper", "caught exception " + e);