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.bluetooth; 18 19 import static com.android.settings.bluetooth.BluetoothDetailsHearingDeviceController.KEY_HEARING_DEVICE_GROUP; 20 import static com.android.settings.bluetooth.BluetoothDetailsHearingDeviceController.ORDER_HEARING_DEVICE_SETTINGS; 21 22 import android.content.Context; 23 import android.text.TextUtils; 24 25 import androidx.preference.Preference; 26 import androidx.preference.PreferenceCategory; 27 import androidx.preference.PreferenceFragmentCompat; 28 import androidx.preference.PreferenceScreen; 29 30 import com.android.settings.R; 31 import com.android.settings.accessibility.AccessibilityHearingAidsFragment; 32 import com.android.settings.accessibility.ArrowPreference; 33 import com.android.settings.core.SubSettingLauncher; 34 import com.android.settingslib.bluetooth.CachedBluetoothDevice; 35 import com.android.settingslib.core.lifecycle.Lifecycle; 36 37 import com.google.common.annotations.VisibleForTesting; 38 39 /** 40 * The controller of the hearing device settings to launch Hearing device page. 41 */ 42 public class BluetoothDetailsHearingDeviceSettingsController extends BluetoothDetailsController 43 implements Preference.OnPreferenceClickListener { 44 45 @VisibleForTesting 46 static final String KEY_HEARING_DEVICE_SETTINGS = "hearing_device_settings"; 47 BluetoothDetailsHearingDeviceSettingsController(Context context, PreferenceFragmentCompat fragment, CachedBluetoothDevice device, Lifecycle lifecycle)48 public BluetoothDetailsHearingDeviceSettingsController(Context context, 49 PreferenceFragmentCompat fragment, CachedBluetoothDevice device, Lifecycle lifecycle) { 50 super(context, fragment, device, lifecycle); 51 lifecycle.addObserver(this); 52 } 53 54 @Override isAvailable()55 public boolean isAvailable() { 56 return mCachedDevice.isHearingAidDevice(); 57 } 58 59 @Override init(PreferenceScreen screen)60 protected void init(PreferenceScreen screen) { 61 if (!isAvailable()) { 62 return; 63 } 64 final PreferenceCategory group = screen.findPreference(KEY_HEARING_DEVICE_GROUP); 65 final Preference pref = createHearingDeviceSettingsPreference(group.getContext()); 66 group.addPreference(pref); 67 } 68 69 @Override refresh()70 protected void refresh() { 71 72 } 73 74 @Override getPreferenceKey()75 public String getPreferenceKey() { 76 return KEY_HEARING_DEVICE_SETTINGS; 77 } 78 79 @Override onPreferenceClick(Preference preference)80 public boolean onPreferenceClick(Preference preference) { 81 if (TextUtils.equals(preference.getKey(), KEY_HEARING_DEVICE_SETTINGS)) { 82 launchAccessibilityHearingDeviceSettings(); 83 return true; 84 } 85 return false; 86 } 87 createHearingDeviceSettingsPreference(Context context)88 private Preference createHearingDeviceSettingsPreference(Context context) { 89 final ArrowPreference preference = new ArrowPreference(context); 90 preference.setKey(KEY_HEARING_DEVICE_SETTINGS); 91 preference.setOrder(ORDER_HEARING_DEVICE_SETTINGS); 92 preference.setTitle(context.getString(R.string.bluetooth_hearing_device_settings_title)); 93 preference.setSummary( 94 context.getString(R.string.bluetooth_hearing_device_settings_summary)); 95 preference.setOnPreferenceClickListener(this); 96 97 return preference; 98 } 99 launchAccessibilityHearingDeviceSettings()100 private void launchAccessibilityHearingDeviceSettings() { 101 new SubSettingLauncher(mContext) 102 .setDestination(AccessibilityHearingAidsFragment.class.getName()) 103 .setSourceMetricsCategory( 104 ((BluetoothDeviceDetailsFragment) mFragment).getMetricsCategory()) 105 .launch(); 106 } 107 } 108