summaryrefslogtreecommitdiffstats
path: root/libs/usb/src/com/android/future/usb/UsbManager.java
blob: f74b291e70358bbb2672f2687cf0ebab99f9db11 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


package com.android.future.usb;

import android.content.Context;
import android.content.Intent;
import android.hardware.usb.IUsbManager;
import android.os.IBinder;
import android.os.ParcelFileDescriptor;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.util.Log;

/**
 * This class allows you to access the state of USB, both in host and device mode.
 *
 * <p>You can obtain an instance of this class by calling {@link #getInstance}
 *
 */
public class UsbManager {
    private static final String TAG = "UsbManager";

   /**
     * Broadcast Action:  A broadcast for USB accessory attached event.
     *
     * This intent is sent when a USB accessory is attached.
     * Call {@link #getAccessory(android.content.Intent)} to retrieve the
     * {@link com.google.android.usb.UsbAccessory} for the attached accessory.
     */
    public static final String ACTION_USB_ACCESSORY_ATTACHED =
            "android.hardware.usb.action.USB_ACCESSORY_ATTACHED";

   /**
     * Broadcast Action:  A broadcast for USB accessory detached event.
     *
     * This intent is sent when a USB accessory is detached.
     * Call {@link #getAccessory(android.content.Intent)} to retrieve the
     * {@link com.google.android.usb.UsbAccessory} for the attached accessory that was detached.
     */
    public static final String ACTION_USB_ACCESSORY_DETACHED =
            "android.hardware.usb.action.USB_ACCESSORY_DETACHED";

    private final IUsbManager mService;

    private UsbManager(IUsbManager service) {
        mService = service;
    }

    /**
     * Returns a new instance of this class.
     *
     * @return UsbManager instance.
     */
    public static UsbManager getInstance() {
        IBinder b = ServiceManager.getService(Context.USB_SERVICE);
        return new UsbManager(IUsbManager.Stub.asInterface(b));
    }

    /**
     * Returns the {@link com.google.android.usb.UsbAccessory} for
     * a {@link #ACTION_USB_ACCESSORY_ATTACHED} or {@link #ACTION_USB_ACCESSORY_ATTACHED}
     * broadcast Intent
     *
     * @return UsbAccessory for the broadcast.
     */
    public static UsbAccessory getAccessory(Intent intent) {
        android.hardware.usb.UsbAccessory accessory =
            intent.getParcelableExtra(android.hardware.usb.UsbManager.EXTRA_ACCESSORY);
        if (accessory == null) {
            return null;
        } else {
            return new UsbAccessory(accessory);
        }
    }

    /**
     * Returns a list of currently attached USB accessories.
     * (in the current implementation there can be at most one)
     *
     * @return list of USB accessories, or null if none are attached.
     */
    public UsbAccessory[] getAccessoryList() {
        try {
            android.hardware.usb.UsbAccessory accessory = mService.getCurrentAccessory();
            if (accessory == null) {
                return null;
            } else {
                return new UsbAccessory[] { new UsbAccessory(accessory) };
            }
        } catch (RemoteException e) {
            Log.e(TAG, "RemoteException in getAccessoryList" , e);
            return null;
        }
    }

    /**
     * Opens a file descriptor for reading and writing data to the USB accessory.
     *
     * @param accessory the USB accessory to open
     * @return file descriptor, or null if the accessor could not be opened.
     */
    public ParcelFileDescriptor openAccessory(UsbAccessory accessory) {
        try {
            return mService.openAccessory(new android.hardware.usb.UsbAccessory(
                    accessory.getManufacturer(),accessory.getModel(),
                    accessory.getType(), accessory.getVersion()));
        } catch (RemoteException e) {
            Log.e(TAG, "RemoteException in openAccessory" , e);
            return null;
        }
    }
}