• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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.util.Log;
20 
21 import androidx.annotation.NonNull;
22 import androidx.fragment.app.FragmentManager;
23 
24 import com.android.settings.bluetooth.HearingAidPairingDialogFragment;
25 import com.android.settingslib.bluetooth.CachedBluetoothDevice;
26 import com.android.settingslib.bluetooth.CsipSetCoordinatorProfile;
27 import com.android.settingslib.bluetooth.HearingAidInfo;
28 
29 /** Provides utility methods related hearing aids. */
30 public final class HearingAidUtils {
31     private static final String TAG = "HearingAidUtils";
32 
HearingAidUtils()33     private HearingAidUtils(){}
34 
35     /**
36      * Launches pairing dialog when hearing aid device needs other side of hearing aid device to
37      * work.
38      *
39      * @param fragmentManager The {@link FragmentManager} used to show dialog fragment
40      * @param device The {@link CachedBluetoothDevice} need to be hearing aid device
41      */
launchHearingAidPairingDialog(FragmentManager fragmentManager, @NonNull CachedBluetoothDevice device)42     public static void launchHearingAidPairingDialog(FragmentManager fragmentManager,
43             @NonNull CachedBluetoothDevice device) {
44         // No need to show the pair another ear dialog if the device supports and enables CSIP.
45         // CSIP will pair other devices in the same set automatically.
46         if (isCsipSupportedAndEnabled(device)) {
47             return;
48         }
49         if (device.isConnectedAshaHearingAidDevice()
50                 && device.getDeviceMode() == HearingAidInfo.DeviceMode.MODE_BINAURAL
51                 && device.getSubDevice() == null) {
52             launchHearingAidPairingDialogInternal(fragmentManager, device);
53         }
54     }
55 
launchHearingAidPairingDialogInternal(FragmentManager fragmentManager, @NonNull CachedBluetoothDevice device)56     private static void launchHearingAidPairingDialogInternal(FragmentManager fragmentManager,
57             @NonNull CachedBluetoothDevice device) {
58         if (device.getDeviceSide() == HearingAidInfo.DeviceSide.SIDE_INVALID) {
59             Log.w(TAG, "Can not launch hearing aid pairing dialog for invalid side");
60             return;
61         }
62         HearingAidPairingDialogFragment.newInstance(device.getAddress()).show(fragmentManager,
63                 HearingAidPairingDialogFragment.TAG);
64     }
65 
isCsipSupportedAndEnabled(@onNull CachedBluetoothDevice device)66     private static boolean isCsipSupportedAndEnabled(@NonNull CachedBluetoothDevice device) {
67         return device.getProfiles().stream().anyMatch(
68                 profile -> (profile instanceof CsipSetCoordinatorProfile)
69                         && (profile.isEnabled(device.getDevice())));
70     }
71 }
72