blob: 6c5ecbf2db40b3f3953861057a597ba4df4f1503 [file] [log] [blame]
Jakub Pawlowskiea580fa2017-11-22 11:02:34 -08001/*
2 * Copyright (C) 2018 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.BluetoothHearingAid;
20import android.bluetooth.BluetoothAdapter;
21import android.bluetooth.BluetoothClass;
22import android.bluetooth.BluetoothCodecConfig;
23import android.bluetooth.BluetoothCodecStatus;
24import android.bluetooth.BluetoothDevice;
25import android.bluetooth.BluetoothProfile;
26import android.bluetooth.BluetoothUuid;
27import android.content.Context;
28import android.os.ParcelUuid;
29import android.util.Log;
30
31import com.android.internal.annotations.VisibleForTesting;
32import com.android.settingslib.R;
33
34import java.util.ArrayList;
35import java.util.Arrays;
36import java.util.List;
37
38public class HearingAidProfile implements LocalBluetoothProfile {
39 private static final String TAG = "HearingAidProfile";
40 private static boolean V = true;
41
42 private Context mContext;
43
44 private BluetoothHearingAid mService;
45 private boolean mIsProfileReady;
46
47 private final LocalBluetoothAdapter mLocalAdapter;
48 private final CachedBluetoothDeviceManager mDeviceManager;
49
50 static final String NAME = "HearingAid";
51 private final LocalBluetoothProfileManager mProfileManager;
52
53 // Order of this profile in device profiles list
54 private static final int ORDINAL = 1;
55
56 // These callbacks run on the main thread.
57 private final class HearingAidServiceListener
58 implements BluetoothProfile.ServiceListener {
59
60 public void onServiceConnected(int profile, BluetoothProfile proxy) {
61 if (V) Log.d(TAG,"Bluetooth service connected");
62 mService = (BluetoothHearingAid) proxy;
63 // We just bound to the service, so refresh the UI for any connected HearingAid devices.
64 List<BluetoothDevice> deviceList = mService.getConnectedDevices();
65 while (!deviceList.isEmpty()) {
66 BluetoothDevice nextDevice = deviceList.remove(0);
67 CachedBluetoothDevice device = mDeviceManager.findDevice(nextDevice);
68 // we may add a new device here, but generally this should not happen
69 if (device == null) {
70 Log.w(TAG, "HearingAidProfile found new device: " + nextDevice);
71 device = mDeviceManager.addDevice(mLocalAdapter, mProfileManager, nextDevice);
72 }
73 device.onProfileStateChanged(HearingAidProfile.this, BluetoothProfile.STATE_CONNECTED);
74 device.refresh();
75 }
76 mIsProfileReady=true;
77 }
78
79 public void onServiceDisconnected(int profile) {
80 if (V) Log.d(TAG,"Bluetooth service disconnected");
81 mIsProfileReady=false;
82 }
83 }
84
85 public boolean isProfileReady() {
86 return mIsProfileReady;
87 }
88
89 HearingAidProfile(Context context, LocalBluetoothAdapter adapter,
90 CachedBluetoothDeviceManager deviceManager,
91 LocalBluetoothProfileManager profileManager) {
92 mContext = context;
93 mLocalAdapter = adapter;
94 mDeviceManager = deviceManager;
95 mProfileManager = profileManager;
96 mLocalAdapter.getProfileProxy(context, new HearingAidServiceListener(),
97 BluetoothProfile.HEARING_AID);
98 }
99
100 public boolean isConnectable() {
101 return true;
102 }
103
104 public boolean isAutoConnectable() {
105 return true;
106 }
107
108 public List<BluetoothDevice> getConnectedDevices() {
109 if (mService == null) return new ArrayList<BluetoothDevice>(0);
110 return mService.getDevicesMatchingConnectionStates(
111 new int[] {BluetoothProfile.STATE_CONNECTED,
112 BluetoothProfile.STATE_CONNECTING,
113 BluetoothProfile.STATE_DISCONNECTING});
114 }
115
116 public boolean connect(BluetoothDevice device) {
117 if (mService == null) return false;
118 return mService.connect(device);
119 }
120
121 public boolean disconnect(BluetoothDevice device) {
122 if (mService == null) return false;
123 // Downgrade priority as user is disconnecting the hearing aid.
124 if (mService.getPriority(device) > BluetoothProfile.PRIORITY_ON){
125 mService.setPriority(device, BluetoothProfile.PRIORITY_ON);
126 }
127 return mService.disconnect(device);
128 }
129
130 public int getConnectionStatus(BluetoothDevice device) {
131 if (mService == null) {
132 return BluetoothProfile.STATE_DISCONNECTED;
133 }
134 return mService.getConnectionState(device);
135 }
136
Hansong Zhang7ca303c2018-03-16 09:15:48 -0700137 public boolean setActiveDevice(BluetoothDevice device) {
138 if (mService == null) return false;
Hansong Zhang8d799f82018-03-28 16:53:10 -0700139 return mService.setActiveDevice(device);
Hansong Zhang7ca303c2018-03-16 09:15:48 -0700140 }
141
Hansong Zhang8d799f82018-03-28 16:53:10 -0700142 public List<BluetoothDevice> getActiveDevices() {
143 if (mService == null) return new ArrayList<>();
144 return mService.getActiveDevices();
Hansong Zhang7ca303c2018-03-16 09:15:48 -0700145 }
146
Jakub Pawlowskiea580fa2017-11-22 11:02:34 -0800147 public boolean isPreferred(BluetoothDevice device) {
148 if (mService == null) return false;
149 return mService.getPriority(device) > BluetoothProfile.PRIORITY_OFF;
150 }
151
152 public int getPreferred(BluetoothDevice device) {
153 if (mService == null) return BluetoothProfile.PRIORITY_OFF;
154 return mService.getPriority(device);
155 }
156
157 public void setPreferred(BluetoothDevice device, boolean preferred) {
158 if (mService == null) return;
159 if (preferred) {
160 if (mService.getPriority(device) < BluetoothProfile.PRIORITY_ON) {
161 mService.setPriority(device, BluetoothProfile.PRIORITY_ON);
162 }
163 } else {
164 mService.setPriority(device, BluetoothProfile.PRIORITY_OFF);
165 }
166 }
167
168 public int getVolume() {
169 if (mService == null) {
170 return 0;
171 }
172 return mService.getVolume();
173 }
174
175 public void setVolume(int volume) {
176 if (mService == null) {
177 return;
178 }
179 mService.setVolume(volume);
180 }
181
182 public long getHiSyncId(BluetoothDevice device) {
183 if (mService == null) {
184 return BluetoothHearingAid.HI_SYNC_ID_INVALID;
185 }
186 return mService.getHiSyncId(device);
187 }
188
189 public int getDeviceSide(BluetoothDevice device) {
190 if (mService == null) {
191 return BluetoothHearingAid.SIDE_LEFT;
192 }
193 return mService.getDeviceSide(device);
194 }
195
196 public int getDeviceMode(BluetoothDevice device) {
197 if (mService == null) {
198 return BluetoothHearingAid.MODE_MONAURAL;
199 }
200 return mService.getDeviceMode(device);
201 }
202
203 public String toString() {
204 return NAME;
205 }
206
207 public int getOrdinal() {
208 return ORDINAL;
209 }
210
211 public int getNameResource(BluetoothDevice device) {
212 return R.string.bluetooth_profile_hearing_aid;
213 }
214
215 public int getSummaryResourceForDevice(BluetoothDevice device) {
216 int state = getConnectionStatus(device);
217 switch (state) {
218 case BluetoothProfile.STATE_DISCONNECTED:
219 return R.string.bluetooth_hearing_aid_profile_summary_use_for;
220
221 case BluetoothProfile.STATE_CONNECTED:
222 return R.string.bluetooth_hearing_aid_profile_summary_connected;
223
224 default:
225 return Utils.getConnectionStateSummary(state);
226 }
227 }
228
229 public int getDrawableResource(BluetoothClass btClass) {
230 return R.drawable.ic_bt_hearing_aid;
231 }
232
233 protected void finalize() {
234 if (V) Log.d(TAG, "finalize()");
235 if (mService != null) {
236 try {
237 BluetoothAdapter.getDefaultAdapter().closeProfileProxy(BluetoothProfile.HEARING_AID,
238 mService);
239 mService = null;
240 }catch (Throwable t) {
241 Log.w(TAG, "Error cleaning up Hearing Aid proxy", t);
242 }
243 }
244 }
245}