UsbManager.java revision 40bbf9295d5245d3917629ce15f7b37670aef1ac
1/*
2 * Copyright (C) 2010 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 android.hardware.usb;
19
20import android.os.Bundle;
21import android.os.ParcelFileDescriptor;
22import android.os.RemoteException;
23import android.util.Log;
24
25import java.io.File;
26import java.io.FileInputStream;
27import java.io.FileOutputStream;
28import java.io.IOException;
29import java.util.HashMap;
30
31/**
32 * This class allows you to access the state of USB.
33 *
34 * <p>You can obtain an instance of this class by calling
35 * {@link android.content.Context#getSystemService(java.lang.String) Context.getSystemService()}.
36 *
37 * {@samplecode
38 * UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
39 * }
40 * @hide
41 */
42public class UsbManager {
43    private static final String TAG = "UsbManager";
44
45   /**
46     * Broadcast Action:  A sticky broadcast for USB state change events when in device mode.
47     *
48     * This is a sticky broadcast for clients that includes USB connected/disconnected state,
49     * <ul>
50     * <li> {@link #USB_CONNECTED} boolean indicating whether USB is connected or disconnected.
51     * <li> {@link #USB_CONFIGURATION} a Bundle containing name/value pairs where the name
52     * is the name of a USB function and the value is either {@link #USB_FUNCTION_ENABLED}
53     * or {@link #USB_FUNCTION_DISABLED}.  The possible function names include
54     * {@link #USB_FUNCTION_MASS_STORAGE}, {@link #USB_FUNCTION_ADB}, {@link #USB_FUNCTION_RNDIS},
55     * {@link #USB_FUNCTION_MTP} and {@link #USB_FUNCTION_ACCESSORY}.
56     * </ul>
57     */
58    public static final String ACTION_USB_STATE =
59            "android.hardware.usb.action.USB_STATE";
60
61   /**
62     * Broadcast Action:  A broadcast for USB accessory attached event.
63     *
64     * This intent is sent when a USB accessory is attached.
65     * <ul>
66     * <li> {@link #EXTRA_ACCESSORY} containing the {@link android.hardware.usb.UsbAccessory}
67     * for the attached accessory
68     * </ul>
69     */
70    public static final String ACTION_USB_ACCESSORY_ATTACHED =
71            "android.hardware.usb.action.USB_ACCESSORY_ATTACHED";
72
73   /**
74     * Broadcast Action:  A broadcast for USB accessory detached event.
75     *
76     * This intent is sent when a USB accessory is detached.
77     * <ul>
78     * <li> {@link #EXTRA_ACCESSORY} containing the {@link android.hardware.usb.UsbAccessory}
79     * for the attached accessory that was detached
80     * </ul>
81     */
82    public static final String ACTION_USB_ACCESSORY_DETACHED =
83            "android.hardware.usb.action.USB_ACCESSORY_DETACHED";
84
85    /**
86     * Boolean extra indicating whether USB is connected or disconnected.
87     * Used in extras for the {@link #ACTION_USB_STATE} broadcast.
88     */
89    public static final String USB_CONNECTED = "connected";
90
91    /**
92     * Integer extra containing currently set USB configuration.
93     * Used in extras for the {@link #ACTION_USB_STATE} broadcast.
94     */
95    public static final String USB_CONFIGURATION = "configuration";
96
97    /**
98     * Name of the USB mass storage USB function.
99     * Used in extras for the {@link #ACTION_USB_STATE} broadcast
100     */
101    public static final String USB_FUNCTION_MASS_STORAGE = "mass_storage";
102
103    /**
104     * Name of the adb USB function.
105     * Used in extras for the {@link #ACTION_USB_STATE} broadcast
106     */
107    public static final String USB_FUNCTION_ADB = "adb";
108
109    /**
110     * Name of the RNDIS ethernet USB function.
111     * Used in extras for the {@link #ACTION_USB_STATE} broadcast
112     */
113    public static final String USB_FUNCTION_RNDIS = "rndis";
114
115    /**
116     * Name of the MTP USB function.
117     * Used in extras for the {@link #ACTION_USB_STATE} broadcast
118     */
119    public static final String USB_FUNCTION_MTP = "mtp";
120
121    /**
122     * Name of the Accessory USB function.
123     * Used in extras for the {@link #ACTION_USB_STATE} broadcast
124     */
125    public static final String USB_FUNCTION_ACCESSORY = "accessory";
126
127    /**
128     * Value indicating that a USB function is enabled.
129     * Used in {@link #USB_CONFIGURATION} extras bundle for the
130     * {@link #ACTION_USB_STATE} broadcast
131     */
132    public static final String USB_FUNCTION_ENABLED = "enabled";
133
134    /**
135     * Value indicating that a USB function is disabled.
136     * Used in {@link #USB_CONFIGURATION} extras bundle for the
137     * {@link #ACTION_USB_STATE} broadcast
138     */
139    public static final String USB_FUNCTION_DISABLED = "disabled";
140
141    /**
142     * Name of extra for {@link #ACTION_USB_ACCESSORY_ATTACHED} and
143     * {@link #ACTION_USB_ACCESSORY_DETACHED} broadcasts
144     * containing the UsbAccessory object for the accessory.
145     */
146    public static final String EXTRA_ACCESSORY = "accessory";
147
148    private IUsbManager mService;
149
150    /**
151     * {@hide}
152     */
153    public UsbManager(IUsbManager service) {
154        mService = service;
155    }
156
157    /**
158     * Returns a list of currently attached USB accessories.
159     * (in the current implementation there can be at most one)
160     *
161     * @return list of USB accessories, or null if none are attached.
162     */
163    public UsbAccessory[] getAccessoryList() {
164        try {
165            UsbAccessory accessory = mService.getCurrentAccessory();
166            if (accessory == null) {
167                return null;
168            } else {
169                return new UsbAccessory[] { accessory };
170            }
171        } catch (RemoteException e) {
172            Log.e(TAG, "RemoteException in getAccessoryList" , e);
173            return null;
174        }
175    }
176
177    /**
178     * Opens a file descriptor for reading and writing data to the USB accessory.
179     *
180     * @param accessory the USB accessory to open
181     * @return file descriptor, or null if the accessor could not be opened.
182     */
183    public ParcelFileDescriptor openAccessory(UsbAccessory accessory) {
184        try {
185            return mService.openAccessory(accessory);
186        } catch (RemoteException e) {
187            Log.e(TAG, "RemoteException in openAccessory" , e);
188            return null;
189        }
190    }
191
192    private static File getFunctionEnableFile(String function) {
193        return new File("/sys/class/usb_composite/" + function + "/enable");
194    }
195
196    /**
197     * Returns true if the specified USB function is supported by the kernel.
198     * Note that a USB function maybe supported but disabled.
199     *
200     * @param function name of the USB function
201     * @return true if the USB function is supported.
202     */
203    public static boolean isFunctionSupported(String function) {
204        return getFunctionEnableFile(function).exists();
205    }
206
207    /**
208     * Returns true if the specified USB function is currently enabled.
209     *
210     * @param function name of the USB function
211     * @return true if the USB function is enabled.
212     */
213    public static boolean isFunctionEnabled(String function) {
214        try {
215            FileInputStream stream = new FileInputStream(getFunctionEnableFile(function));
216            boolean enabled = (stream.read() == '1');
217            stream.close();
218            return enabled;
219        } catch (IOException e) {
220            return false;
221        }
222    }
223
224    /**
225     * Enables or disables a USB function.
226     *
227     * @hide
228     */
229    public static boolean setFunctionEnabled(String function, boolean enable) {
230        try {
231            FileOutputStream stream = new FileOutputStream(getFunctionEnableFile(function));
232            stream.write(enable ? '1' : '0');
233            stream.close();
234            return true;
235        } catch (IOException e) {
236            return false;
237        }
238    }
239}
240