1 /* 2 * Copyright (C) 2023 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 package com.android.settings.accessibility; 18 19 import android.bluetooth.BluetoothAdapter; 20 import android.bluetooth.BluetoothDevice; 21 import android.bluetooth.BluetoothProfile; 22 import android.content.Context; 23 24 import com.android.settingslib.bluetooth.CachedBluetoothDevice; 25 import com.android.settingslib.bluetooth.CachedBluetoothDeviceManager; 26 import com.android.settingslib.bluetooth.HapClientProfile; 27 import com.android.settingslib.bluetooth.HearingAidProfile; 28 import com.android.settingslib.bluetooth.LocalBluetoothManager; 29 import com.android.settingslib.bluetooth.LocalBluetoothProfileManager; 30 31 import java.util.ArrayList; 32 import java.util.List; 33 import java.util.stream.Collectors; 34 35 /** 36 * A helper class to get and check hearing aids and its status. 37 */ 38 public class HearingAidHelper { 39 40 private final BluetoothAdapter mBluetoothAdapter; 41 private final LocalBluetoothProfileManager mProfileManager; 42 private final CachedBluetoothDeviceManager mCachedDeviceManager; 43 HearingAidHelper(Context context)44 public HearingAidHelper(Context context) { 45 final LocalBluetoothManager localBluetoothManager = 46 com.android.settings.bluetooth.Utils.getLocalBluetoothManager(context); 47 mProfileManager = localBluetoothManager.getProfileManager(); 48 mCachedDeviceManager = localBluetoothManager.getCachedDeviceManager(); 49 mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 50 } 51 52 /** 53 * Gets the connected hearing aids device whose profiles are 54 * {@link BluetoothProfile#HEARING_AID} or {@link BluetoothProfile#HAP_CLIENT}. 55 * 56 * @return a list of hearing aids {@link BluetoothDevice} objects 57 */ getConnectedHearingAidDeviceList()58 public List<BluetoothDevice> getConnectedHearingAidDeviceList() { 59 if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled() 60 || !isHearingAidSupported()) { 61 return new ArrayList<>(); 62 } 63 final List<BluetoothDevice> deviceList = new ArrayList<>(); 64 final HapClientProfile hapClientProfile = mProfileManager.getHapClientProfile(); 65 if (hapClientProfile != null) { 66 deviceList.addAll(hapClientProfile.getConnectedDevices()); 67 } 68 final HearingAidProfile hearingAidProfile = mProfileManager.getHearingAidProfile(); 69 if (hearingAidProfile != null) { 70 deviceList.addAll(hearingAidProfile.getConnectedDevices()); 71 } 72 return deviceList.stream() 73 .distinct() 74 .filter(d -> !mCachedDeviceManager.isSubDevice(d)).collect(Collectors.toList()); 75 } 76 77 /** 78 * Gets the first connected hearing aids device. 79 * 80 * @return a {@link CachedBluetoothDevice} that is hearing aids device 81 */ getConnectedHearingAidDevice()82 public CachedBluetoothDevice getConnectedHearingAidDevice() { 83 final List<BluetoothDevice> deviceList = getConnectedHearingAidDeviceList(); 84 return deviceList.isEmpty() ? null : mCachedDeviceManager.findDevice(deviceList.get(0)); 85 } 86 87 /** 88 * Checks if {@link BluetoothProfile#HEARING_AID} or {@link BluetoothProfile#HAP_CLIENT} 89 * supported. 90 */ isHearingAidSupported()91 public boolean isHearingAidSupported() { 92 final List<Integer> supportedList = mBluetoothAdapter.getSupportedProfiles(); 93 return supportedList.contains(BluetoothProfile.HEARING_AID) 94 || supportedList.contains(BluetoothProfile.HAP_CLIENT); 95 } 96 97 /** 98 * Checks if {@link BluetoothProfile#HEARING_AID} or {@link BluetoothProfile#HAP_CLIENT} 99 * profiles all ready. 100 */ isAllHearingAidRelatedProfilesReady()101 public boolean isAllHearingAidRelatedProfilesReady() { 102 HearingAidProfile hearingAidProfile = mProfileManager.getHearingAidProfile(); 103 if (hearingAidProfile != null && !hearingAidProfile.isProfileReady()) { 104 return false; 105 } 106 HapClientProfile hapClientProfile = mProfileManager.getHapClientProfile(); 107 if (hapClientProfile != null && !hapClientProfile.isProfileReady()) { 108 return false; 109 } 110 return true; 111 } 112 } 113