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.BluetoothProfile; 20 import android.content.Context; 21 22 import androidx.fragment.app.FragmentManager; 23 import androidx.preference.Preference; 24 import androidx.preference.PreferenceScreen; 25 26 import com.android.settings.bluetooth.BluetoothDeviceUpdater; 27 import com.android.settings.connecteddevice.DevicePreferenceCallback; 28 import com.android.settings.dashboard.DashboardFragment; 29 import com.android.settingslib.bluetooth.BluetoothCallback; 30 import com.android.settingslib.bluetooth.CachedBluetoothDevice; 31 import com.android.settingslib.bluetooth.LocalBluetoothManager; 32 import com.android.settingslib.core.lifecycle.LifecycleObserver; 33 import com.android.settingslib.core.lifecycle.events.OnStart; 34 import com.android.settingslib.core.lifecycle.events.OnStop; 35 36 /** 37 * Controller to update the {@link androidx.preference.PreferenceCategory} for all 38 * connected hearing devices, including ASHA and HAP profile. 39 * Parent class {@link BaseBluetoothDevicePreferenceController} will use 40 * {@link DevicePreferenceCallback} to add/remove {@link Preference}. 41 */ 42 public class AvailableHearingDevicePreferenceController extends 43 BaseBluetoothDevicePreferenceController implements LifecycleObserver, OnStart, OnStop, 44 BluetoothCallback { 45 46 private static final String TAG = "AvailableHearingDevicePreferenceController"; 47 48 private BluetoothDeviceUpdater mAvailableHearingDeviceUpdater; 49 private final LocalBluetoothManager mLocalBluetoothManager; 50 private FragmentManager mFragmentManager; 51 AvailableHearingDevicePreferenceController(Context context, String preferenceKey)52 public AvailableHearingDevicePreferenceController(Context context, 53 String preferenceKey) { 54 super(context, preferenceKey); 55 mLocalBluetoothManager = com.android.settings.bluetooth.Utils.getLocalBluetoothManager( 56 context); 57 } 58 59 /** 60 * Initializes objects in this controller. Need to call this before onStart(). 61 * 62 * <p>Should not call this more than 1 time. 63 * 64 * @param fragment The {@link DashboardFragment} uses the controller. 65 */ init(DashboardFragment fragment)66 public void init(DashboardFragment fragment) { 67 if (mAvailableHearingDeviceUpdater != null) { 68 throw new IllegalStateException("Should not call init() more than 1 time."); 69 } 70 mAvailableHearingDeviceUpdater = new AvailableHearingDeviceUpdater(fragment.getContext(), 71 this, fragment.getMetricsCategory()); 72 mFragmentManager = fragment.getParentFragmentManager(); 73 } 74 75 @Override onStart()76 public void onStart() { 77 mAvailableHearingDeviceUpdater.registerCallback(); 78 mAvailableHearingDeviceUpdater.refreshPreference(); 79 mLocalBluetoothManager.getEventManager().registerCallback(this); 80 } 81 82 @Override onStop()83 public void onStop() { 84 mAvailableHearingDeviceUpdater.unregisterCallback(); 85 mLocalBluetoothManager.getEventManager().unregisterCallback(this); 86 } 87 88 @Override displayPreference(PreferenceScreen screen)89 public void displayPreference(PreferenceScreen screen) { 90 super.displayPreference(screen); 91 92 if (isAvailable()) { 93 final Context context = screen.getContext(); 94 mAvailableHearingDeviceUpdater.setPrefContext(context); 95 mAvailableHearingDeviceUpdater.forceUpdate(); 96 } 97 } 98 99 @Override onActiveDeviceChanged(CachedBluetoothDevice activeDevice, int bluetoothProfile)100 public void onActiveDeviceChanged(CachedBluetoothDevice activeDevice, int bluetoothProfile) { 101 if (activeDevice == null) { 102 return; 103 } 104 105 if (bluetoothProfile == BluetoothProfile.HEARING_AID) { 106 HearingAidUtils.launchHearingAidPairingDialog(mFragmentManager, activeDevice, 107 getMetricsCategory()); 108 } 109 } 110 } 111