blob: 7f906f6c5b0653f1d903fe8ffa4e6bbfa7e1fdca [file] [log] [blame]
Jason Monk7ce96b92015-02-02 11:27:58 -05001/*
2 * Copyright (C) 2012 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.settingslib.bluetooth;
18
19import android.bluetooth.BluetoothAdapter;
20import android.bluetooth.BluetoothClass;
21import android.bluetooth.BluetoothDevice;
Hansong Zhangc26c76c2017-10-20 15:55:59 -070022import android.bluetooth.BluetoothHidHost;
Jason Monk7ce96b92015-02-02 11:27:58 -050023import android.bluetooth.BluetoothProfile;
24import android.content.Context;
25import android.util.Log;
26
27import com.android.settingslib.R;
28
29import java.util.List;
30
31/**
Chienyuanc129df82018-08-29 23:14:55 +080032 * HidProfile handles Bluetooth HID Host role.
Jason Monk7ce96b92015-02-02 11:27:58 -050033 */
Jack He6258aae2017-06-29 17:01:23 -070034public class HidProfile implements LocalBluetoothProfile {
Jason Monk7ce96b92015-02-02 11:27:58 -050035 private static final String TAG = "HidProfile";
Jason Monk7ce96b92015-02-02 11:27:58 -050036
Hansong Zhangc26c76c2017-10-20 15:55:59 -070037 private BluetoothHidHost mService;
Jason Monk7ce96b92015-02-02 11:27:58 -050038 private boolean mIsProfileReady;
39
Jason Monk7ce96b92015-02-02 11:27:58 -050040 private final CachedBluetoothDeviceManager mDeviceManager;
41 private final LocalBluetoothProfileManager mProfileManager;
42
43 static final String NAME = "HID";
44
45 // Order of this profile in device profiles list
46 private static final int ORDINAL = 3;
47
48 // These callbacks run on the main thread.
Hansong Zhangdcae2932017-11-13 10:56:23 -080049 private final class HidHostServiceListener
Jason Monk7ce96b92015-02-02 11:27:58 -050050 implements BluetoothProfile.ServiceListener {
51
52 public void onServiceConnected(int profile, BluetoothProfile proxy) {
Hansong Zhangc26c76c2017-10-20 15:55:59 -070053 mService = (BluetoothHidHost) proxy;
Jason Monk7ce96b92015-02-02 11:27:58 -050054 // We just bound to the service, so refresh the UI for any connected HID devices.
55 List<BluetoothDevice> deviceList = mService.getConnectedDevices();
56 while (!deviceList.isEmpty()) {
57 BluetoothDevice nextDevice = deviceList.remove(0);
58 CachedBluetoothDevice device = mDeviceManager.findDevice(nextDevice);
59 // we may add a new device here, but generally this should not happen
60 if (device == null) {
61 Log.w(TAG, "HidProfile found new device: " + nextDevice);
timhypengf64f6902018-07-31 15:40:15 +080062 device = mDeviceManager.addDevice(nextDevice);
Jason Monk7ce96b92015-02-02 11:27:58 -050063 }
64 device.onProfileStateChanged(HidProfile.this, BluetoothProfile.STATE_CONNECTED);
65 device.refresh();
66 }
67 mIsProfileReady=true;
68 }
69
70 public void onServiceDisconnected(int profile) {
Jason Monk7ce96b92015-02-02 11:27:58 -050071 mIsProfileReady=false;
72 }
73 }
74
75 public boolean isProfileReady() {
76 return mIsProfileReady;
77 }
78
ryanywlin44de3a02018-05-02 15:15:37 +080079 @Override
80 public int getProfileId() {
81 return BluetoothProfile.HID_HOST;
82 }
83
timhypengf64f6902018-07-31 15:40:15 +080084 HidProfile(Context context,
Jason Monk7ce96b92015-02-02 11:27:58 -050085 CachedBluetoothDeviceManager deviceManager,
86 LocalBluetoothProfileManager profileManager) {
Jason Monk7ce96b92015-02-02 11:27:58 -050087 mDeviceManager = deviceManager;
88 mProfileManager = profileManager;
timhypengf64f6902018-07-31 15:40:15 +080089 BluetoothAdapter.getDefaultAdapter().getProfileProxy(context, new HidHostServiceListener(),
Hansong Zhangc26c76c2017-10-20 15:55:59 -070090 BluetoothProfile.HID_HOST);
Jason Monk7ce96b92015-02-02 11:27:58 -050091 }
92
Leon Liao3b5e6bd2018-09-18 12:45:45 +080093 public boolean accessProfileEnabled() {
Jason Monk7ce96b92015-02-02 11:27:58 -050094 return true;
95 }
96
97 public boolean isAutoConnectable() {
98 return true;
99 }
100
101 public boolean connect(BluetoothDevice device) {
102 if (mService == null) return false;
103 return mService.connect(device);
104 }
105
106 public boolean disconnect(BluetoothDevice device) {
107 if (mService == null) return false;
108 return mService.disconnect(device);
109 }
110
111 public int getConnectionStatus(BluetoothDevice device) {
112 if (mService == null) {
113 return BluetoothProfile.STATE_DISCONNECTED;
114 }
Chienyuanc129df82018-08-29 23:14:55 +0800115 return mService.getConnectionState(device);
Jason Monk7ce96b92015-02-02 11:27:58 -0500116 }
117
118 public boolean isPreferred(BluetoothDevice device) {
119 if (mService == null) return false;
120 return mService.getPriority(device) > BluetoothProfile.PRIORITY_OFF;
121 }
122
123 public int getPreferred(BluetoothDevice device) {
124 if (mService == null) return BluetoothProfile.PRIORITY_OFF;
125 return mService.getPriority(device);
126 }
127
128 public void setPreferred(BluetoothDevice device, boolean preferred) {
129 if (mService == null) return;
130 if (preferred) {
131 if (mService.getPriority(device) < BluetoothProfile.PRIORITY_ON) {
132 mService.setPriority(device, BluetoothProfile.PRIORITY_ON);
133 }
134 } else {
135 mService.setPriority(device, BluetoothProfile.PRIORITY_OFF);
136 }
137 }
138
139 public String toString() {
140 return NAME;
141 }
142
143 public int getOrdinal() {
144 return ORDINAL;
145 }
146
147 public int getNameResource(BluetoothDevice device) {
148 // TODO: distinguish between keyboard and mouse?
149 return R.string.bluetooth_profile_hid;
150 }
151
152 public int getSummaryResourceForDevice(BluetoothDevice device) {
153 int state = getConnectionStatus(device);
154 switch (state) {
155 case BluetoothProfile.STATE_DISCONNECTED:
156 return R.string.bluetooth_hid_profile_summary_use_for;
157
158 case BluetoothProfile.STATE_CONNECTED:
159 return R.string.bluetooth_hid_profile_summary_connected;
160
161 default:
Kunhung Lie7b7cb82018-05-17 11:04:54 +0800162 return BluetoothUtils.getConnectionStateSummary(state);
Jason Monk7ce96b92015-02-02 11:27:58 -0500163 }
164 }
165
166 public int getDrawableResource(BluetoothClass btClass) {
167 if (btClass == null) {
Amin Shaikh10f363b2019-01-24 17:59:49 -0500168 return com.android.internal.R.drawable.ic_lockscreen_ime;
Jason Monk7ce96b92015-02-02 11:27:58 -0500169 }
170 return getHidClassDrawable(btClass);
171 }
172
173 public static int getHidClassDrawable(BluetoothClass btClass) {
174 switch (btClass.getDeviceClass()) {
175 case BluetoothClass.Device.PERIPHERAL_KEYBOARD:
176 case BluetoothClass.Device.PERIPHERAL_KEYBOARD_POINTING:
Amin Shaikh10f363b2019-01-24 17:59:49 -0500177 return com.android.internal.R.drawable.ic_lockscreen_ime;
Jason Monk7ce96b92015-02-02 11:27:58 -0500178 case BluetoothClass.Device.PERIPHERAL_POINTING:
Amin Shaikh10f363b2019-01-24 17:59:49 -0500179 return com.android.internal.R.drawable.ic_bt_pointing_hid;
Jason Monk7ce96b92015-02-02 11:27:58 -0500180 default:
Amin Shaikh10f363b2019-01-24 17:59:49 -0500181 return com.android.internal.R.drawable.ic_bt_misc_hid;
Jason Monk7ce96b92015-02-02 11:27:58 -0500182 }
183 }
184
185 protected void finalize() {
Chienyuan97e4b202019-01-03 20:02:27 +0800186 Log.d(TAG, "finalize()");
Jason Monk7ce96b92015-02-02 11:27:58 -0500187 if (mService != null) {
188 try {
Hansong Zhangc26c76c2017-10-20 15:55:59 -0700189 BluetoothAdapter.getDefaultAdapter().closeProfileProxy(BluetoothProfile.HID_HOST,
Jason Monk7ce96b92015-02-02 11:27:58 -0500190 mService);
191 mService = null;
192 }catch (Throwable t) {
193 Log.w(TAG, "Error cleaning up HID proxy", t);
194 }
195 }
196 }
197}