UsbManager.java revision 1110748b2df664f9c5066819c1f0616eae3394a7
1/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17
18package com.android.future.usb;
19
20import android.content.Context;
21import android.content.Intent;
22import android.hardware.usb.IUsbManager;
23import android.os.IBinder;
24import android.os.ParcelFileDescriptor;
25import android.os.RemoteException;
26import android.os.ServiceManager;
27import android.util.Log;
28
29/**
30 * This class allows you to access the state of USB, both in host and device mode.
31 *
32 * <p>You can obtain an instance of this class by calling {@link #getInstance}
33 *
34 */
35public class UsbManager {
36    private static final String TAG = "UsbManager";
37
38   /**
39     * Broadcast Action:  A broadcast for USB accessory attached event.
40     *
41     * This intent is sent when a USB accessory is attached.
42     * Call {@link #getAccessory(android.content.Intent)} to retrieve the
43     * {@link com.google.android.usb.UsbAccessory} for the attached accessory.
44     */
45    public static final String ACTION_USB_ACCESSORY_ATTACHED =
46            "android.hardware.usb.action.USB_ACCESSORY_ATTACHED";
47
48   /**
49     * Broadcast Action:  A broadcast for USB accessory detached event.
50     *
51     * This intent is sent when a USB accessory is detached.
52     * Call {@link #getAccessory(android.content.Intent)} to retrieve the
53     * {@link com.google.android.usb.UsbAccessory} for the attached accessory that was detached.
54     */
55    public static final String ACTION_USB_ACCESSORY_DETACHED =
56            "android.hardware.usb.action.USB_ACCESSORY_DETACHED";
57
58    private final IUsbManager mService;
59
60    private UsbManager(IUsbManager service) {
61        mService = service;
62    }
63
64    /**
65     * Returns a new instance of this class.
66     *
67     * @return UsbManager instance.
68     */
69    public static UsbManager getInstance() {
70        IBinder b = ServiceManager.getService(Context.USB_SERVICE);
71        return new UsbManager(IUsbManager.Stub.asInterface(b));
72    }
73
74    /**
75     * Returns the {@link com.google.android.usb.UsbAccessory} for
76     * a {@link #ACTION_USB_ACCESSORY_ATTACHED} or {@link #ACTION_USB_ACCESSORY_ATTACHED}
77     * broadcast Intent
78     *
79     * @return UsbAccessory for the broadcast.
80     */
81    public static UsbAccessory getAccessory(Intent intent) {
82        android.hardware.usb.UsbAccessory accessory =
83            intent.getParcelableExtra(android.hardware.usb.UsbManager.EXTRA_ACCESSORY);
84        if (accessory == null) {
85            return null;
86        } else {
87            return new UsbAccessory(accessory);
88        }
89    }
90
91    /**
92     * Returns a list of currently attached USB accessories.
93     * (in the current implementation there can be at most one)
94     *
95     * @return list of USB accessories, or null if none are attached.
96     */
97    public UsbAccessory[] getAccessoryList() {
98        try {
99            android.hardware.usb.UsbAccessory accessory = mService.getCurrentAccessory();
100            if (accessory == null) {
101                return null;
102            } else {
103                return new UsbAccessory[] { new UsbAccessory(accessory) };
104            }
105        } catch (RemoteException e) {
106            Log.e(TAG, "RemoteException in getAccessoryList" , e);
107            return null;
108        }
109    }
110
111    /**
112     * Opens a file descriptor for reading and writing data to the USB accessory.
113     *
114     * @param accessory the USB accessory to open
115     * @return file descriptor, or null if the accessor could not be opened.
116     */
117    public ParcelFileDescriptor openAccessory(UsbAccessory accessory) {
118        try {
119            return mService.openAccessory(new android.hardware.usb.UsbAccessory(
120                    accessory.getManufacturer(),accessory.getModel(),
121                    accessory.getType(), accessory.getVersion()));
122        } catch (RemoteException e) {
123            Log.e(TAG, "RemoteException in openAccessory" , e);
124            return null;
125        }
126    }
127}
128