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