HidProfile.java revision 79be0b3e6b5639c4cbe2bbcd9adb0ec4c28716ed
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
17package com.android.settings.bluetooth;
18
19import android.bluetooth.BluetoothClass;
20import android.bluetooth.BluetoothDevice;
21import android.bluetooth.BluetoothInputDevice;
22import android.bluetooth.BluetoothProfile;
23import android.content.Context;
24
25import com.android.settings.R;
26
27import java.util.List;
28
29/**
30 * HidProfile handles Bluetooth HID profile.
31 */
32final class HidProfile implements LocalBluetoothProfile {
33    private BluetoothInputDevice mService;
34    private boolean mProfileReady;
35
36    static final String NAME = "HID";
37
38    // Order of this profile in device profiles list
39    private static final int ORDINAL = 3;
40
41    // These callbacks run on the main thread.
42    private final class InputDeviceServiceListener
43            implements BluetoothProfile.ServiceListener {
44
45        public void onServiceConnected(int profile, BluetoothProfile proxy) {
46            mService = (BluetoothInputDevice) proxy;
47            mProfileReady = true;
48        }
49
50        public void onServiceDisconnected(int profile) {
51            mProfileReady = false;
52            mService = null;
53        }
54    }
55
56    HidProfile(Context context, LocalBluetoothAdapter adapter) {
57        adapter.getProfileProxy(context, new InputDeviceServiceListener(),
58                BluetoothProfile.INPUT_DEVICE);
59    }
60
61    public boolean isConnectable() {
62        return true;
63    }
64
65    public boolean isAutoConnectable() {
66        return true;
67    }
68
69    public boolean connect(BluetoothDevice device) {
70        return mService.connect(device);
71    }
72
73    public boolean disconnect(BluetoothDevice device) {
74        return mService.disconnect(device);
75    }
76
77    public int getConnectionStatus(BluetoothDevice device) {
78        List<BluetoothDevice> deviceList = mService.getConnectedDevices();
79
80        return !deviceList.isEmpty() && deviceList.get(0).equals(device)
81                ? mService.getConnectionState(device)
82                : BluetoothProfile.STATE_DISCONNECTED;
83    }
84
85    public boolean isPreferred(BluetoothDevice device) {
86        return mService.getPriority(device) > BluetoothProfile.PRIORITY_OFF;
87    }
88
89    public int getPreferred(BluetoothDevice device) {
90        return mService.getPriority(device);
91    }
92
93    public void setPreferred(BluetoothDevice device, boolean preferred) {
94        if (preferred) {
95            if (mService.getPriority(device) < BluetoothProfile.PRIORITY_ON) {
96                mService.setPriority(device, BluetoothProfile.PRIORITY_ON);
97            }
98        } else {
99            mService.setPriority(device, BluetoothProfile.PRIORITY_OFF);
100        }
101    }
102
103    public boolean isProfileReady() {
104        return mProfileReady;
105    }
106
107    public String toString() {
108        return NAME;
109    }
110
111    public int getOrdinal() {
112        return ORDINAL;
113    }
114
115    public int getNameResource(BluetoothDevice device) {
116        // TODO: distinguish between keyboard and mouse?
117        return R.string.bluetooth_profile_hid;
118    }
119
120    public int getSummaryResourceForDevice(BluetoothDevice device) {
121        int state = mService.getConnectionState(device);
122        switch (state) {
123            case BluetoothProfile.STATE_DISCONNECTED:
124                return R.string.bluetooth_hid_profile_summary_use_for;
125
126            case BluetoothProfile.STATE_CONNECTED:
127                return R.string.bluetooth_hid_profile_summary_connected;
128
129            default:
130                return Utils.getConnectionStateSummary(state);
131        }
132    }
133
134    public int getDrawableResource(BluetoothClass btClass) {
135        if (btClass == null) {
136            return R.drawable.ic_bt_keyboard_hid;
137        }
138        return getHidClassDrawable(btClass);
139    }
140
141    static int getHidClassDrawable(BluetoothClass btClass) {
142        switch (btClass.getDeviceClass()) {
143            case BluetoothClass.Device.PERIPHERAL_KEYBOARD:
144            case BluetoothClass.Device.PERIPHERAL_KEYBOARD_POINTING:
145                return R.drawable.ic_bt_keyboard_hid;
146            case BluetoothClass.Device.PERIPHERAL_POINTING:
147                return R.drawable.ic_bt_pointing_hid;
148            default:
149                return R.drawable.ic_bt_misc_hid;
150        }
151    }
152}
153