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.content.Context; 20 21 import androidx.preference.Preference; 22 import androidx.preference.PreferenceScreen; 23 24 import com.android.settings.bluetooth.BluetoothDeviceUpdater; 25 import com.android.settings.connecteddevice.DevicePreferenceCallback; 26 import com.android.settings.dashboard.DashboardFragment; 27 import com.android.settingslib.core.lifecycle.LifecycleObserver; 28 import com.android.settingslib.core.lifecycle.events.OnResume; 29 import com.android.settingslib.core.lifecycle.events.OnStart; 30 import com.android.settingslib.core.lifecycle.events.OnStop; 31 32 /** 33 * Controller to update the {@link androidx.preference.PreferenceCategory} for all 34 * saved ((bonded but not connected)) hearing devices, including ASHA and HAP profile. 35 * Parent class {@link BaseBluetoothDevicePreferenceController} will use 36 * {@link DevicePreferenceCallback} to add/remove {@link Preference}. 37 */ 38 public class SavedHearingDevicePreferenceController extends 39 BaseBluetoothDevicePreferenceController implements LifecycleObserver, OnStart, OnResume, 40 OnStop { 41 42 private BluetoothDeviceUpdater mSavedHearingDeviceUpdater; 43 SavedHearingDevicePreferenceController(Context context, String preferenceKey)44 public SavedHearingDevicePreferenceController(Context context, 45 String preferenceKey) { 46 super(context, preferenceKey); 47 } 48 49 /** 50 * Initializes objects in this controller. Need to call this before onStart(). 51 * 52 * <p>Should not call this more than 1 time. 53 * 54 * @param fragment The {@link DashboardFragment} uses the controller. 55 */ init(DashboardFragment fragment)56 public void init(DashboardFragment fragment) { 57 if (mSavedHearingDeviceUpdater != null) { 58 throw new IllegalStateException("Should not call init() more than 1 time."); 59 } 60 mSavedHearingDeviceUpdater = new SavedHearingDeviceUpdater(fragment.getContext(), this, 61 fragment.getMetricsCategory()); 62 } 63 64 @Override onStart()65 public void onStart() { 66 mSavedHearingDeviceUpdater.registerCallback(); 67 } 68 69 @Override onResume()70 public void onResume() { 71 mSavedHearingDeviceUpdater.refreshPreference(); 72 } 73 74 @Override onStop()75 public void onStop() { 76 mSavedHearingDeviceUpdater.unregisterCallback(); 77 } 78 79 @Override displayPreference(PreferenceScreen screen)80 public void displayPreference(PreferenceScreen screen) { 81 super.displayPreference(screen); 82 83 if (isAvailable()) { 84 final Context context = screen.getContext(); 85 mSavedHearingDeviceUpdater.setPrefContext(context); 86 mSavedHearingDeviceUpdater.forceUpdate(); 87 } 88 } 89 } 90