UsbManager.java revision 580b64111788fd312697d4fe60017127877dadf4
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.app.PendingIntent;
21import android.content.Context;
22import android.content.Intent;
23import android.hardware.usb.IUsbManager;
24import android.os.IBinder;
25import android.os.ParcelFileDescriptor;
26import android.os.RemoteException;
27import android.os.ServiceManager;
28import android.util.Log;
29
30/**
31 * This class allows you to access the state of USB, both in host and device mode.
32 *
33 * <p>You can obtain an instance of this class by calling {@link #getInstance}
34 *
35 */
36public class UsbManager {
37    private static final String TAG = "UsbManager";
38
39   /**
40     * Broadcast Action:  A broadcast for USB accessory attached event.
41     *
42     * This intent is sent when a USB accessory is attached.
43     * Call {@link #getAccessory(android.content.Intent)} to retrieve the
44     * {@link com.google.android.usb.UsbAccessory} for the attached accessory.
45     */
46    public static final String ACTION_USB_ACCESSORY_ATTACHED =
47            "android.hardware.usb.action.USB_ACCESSORY_ATTACHED";
48
49   /**
50     * Broadcast Action:  A broadcast for USB accessory detached event.
51     *
52     * This intent is sent when a USB accessory is detached.
53     * Call {@link #getAccessory(android.content.Intent)} to retrieve the
54     * {@link com.google.android.usb.UsbAccessory} for the attached accessory that was detached.
55     */
56    public static final String ACTION_USB_ACCESSORY_DETACHED =
57            "android.hardware.usb.action.USB_ACCESSORY_DETACHED";
58
59    /**
60     * Name of extra added to the {@link android.app.PendingIntent}
61     * passed into {#requestPermission} or {#requestPermission}
62     * containing a boolean value indicating whether the user granted permission or not.
63     */
64    public static final String EXTRA_PERMISSION_GRANTED = "permission";
65
66    private final Context mContext;
67    private final IUsbManager mService;
68
69    private UsbManager(Context context, IUsbManager service) {
70        mContext = context;
71        mService = service;
72    }
73
74    /**
75     * Returns a new instance of this class.
76     *
77     * @param context the caller's {@link android.content.Context}
78     * @return UsbManager instance.
79     */
80    public static UsbManager getInstance(Context context) {
81        IBinder b = ServiceManager.getService(Context.USB_SERVICE);
82        return new UsbManager(context, IUsbManager.Stub.asInterface(b));
83    }
84
85    /**
86     * Returns the {@link com.google.android.usb.UsbAccessory} for
87     * a {@link #ACTION_USB_ACCESSORY_ATTACHED} or {@link #ACTION_USB_ACCESSORY_ATTACHED}
88     * broadcast Intent. This can also be used to retrieve the accessory from the result
89     * of a call to {#requestPermission}.
90     *
91     * @return UsbAccessory for the intent.
92     */
93    public static UsbAccessory getAccessory(Intent intent) {
94        android.hardware.usb.UsbAccessory accessory =
95            intent.getParcelableExtra(android.hardware.usb.UsbManager.EXTRA_ACCESSORY);
96        if (accessory == null) {
97            return null;
98        } else {
99            return new UsbAccessory(accessory);
100        }
101    }
102
103    /**
104     * Returns a list of currently attached USB accessories.
105     * (in the current implementation there can be at most one)
106     *
107     * @return list of USB accessories, or null if none are attached.
108     */
109    public UsbAccessory[] getAccessoryList() {
110        try {
111            android.hardware.usb.UsbAccessory accessory = mService.getCurrentAccessory();
112            if (accessory == null) {
113                return null;
114            } else {
115                return new UsbAccessory[] { new UsbAccessory(accessory) };
116            }
117        } catch (RemoteException e) {
118            Log.e(TAG, "RemoteException in getAccessoryList" , e);
119            return null;
120        }
121    }
122
123    /**
124     * Opens a file descriptor for reading and writing data to the USB accessory.
125     *
126     * @param accessory the USB accessory to open
127     * @return file descriptor, or null if the accessor could not be opened.
128     */
129    public ParcelFileDescriptor openAccessory(UsbAccessory accessory) {
130        try {
131            return mService.openAccessory(new android.hardware.usb.UsbAccessory(
132                    accessory.getManufacturer(),accessory.getModel(),
133                    accessory.getType(), accessory.getVersion()));
134        } catch (RemoteException e) {
135            Log.e(TAG, "RemoteException in openAccessory" , e);
136            return null;
137        }
138    }
139
140    /**
141     * Returns true if the caller has permission to access the accessory.
142     * Permission might have been granted temporarily via
143     * {@link #requestPermission(android.hardware.usb.UsbAccessory} or
144     * by the user choosing the caller as the default application for the accessory.
145     *
146     * @param accessory to check permissions for
147     * @return true if caller has permission
148     */
149    public boolean hasPermission(UsbAccessory accessory) {
150        try {
151            return mService.hasAccessoryPermission(new android.hardware.usb.UsbAccessory(
152                        accessory.getManufacturer(),accessory.getModel(),
153                        accessory.getType(), accessory.getVersion()));
154        } catch (RemoteException e) {
155            Log.e(TAG, "RemoteException in hasPermission", e);
156            return false;
157        }
158    }
159
160    /**
161     * Requests temporary permission for the given package to access the accessory.
162     * This may result in a system dialog being displayed to the user
163     * if permission had not already been granted.
164     * Success or failure is returned via the {@link android.app.PendingIntent} pi.
165     * The boolean extra {@link #EXTRA_PERMISSION_GRANTED} will be attached to the
166     * PendingIntent to indicate success or failure.
167     * If successful, this grants the caller permission to access the device only
168     * until the device is disconnected.
169     *
170     * @param accessory to request permissions for
171     * @param pi PendingIntent for returning result
172     */
173    public void requestPermission(UsbAccessory accessory, PendingIntent pi) {
174        try {
175            mService.requestAccessoryPermission(new android.hardware.usb.UsbAccessory(
176                        accessory.getManufacturer(),accessory.getModel(),
177                        accessory.getType(), accessory.getVersion()),
178                    mContext.getPackageName(), pi);
179        } catch (RemoteException e) {
180            Log.e(TAG, "RemoteException in requestPermission", e);
181        }
182    }
183}
184