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.bluetooth.BluetoothDevice; 20 import android.content.Context; 21 22 import androidx.preference.PreferenceFragmentCompat; 23 import androidx.preference.PreferenceScreen; 24 25 import com.android.settings.R; 26 import com.android.settings.applications.SpacePreference; 27 import com.android.settings.core.SubSettingLauncher; 28 import com.android.settingslib.bluetooth.CachedBluetoothDevice; 29 import com.android.settingslib.bluetooth.HearingAidInfo; 30 import com.android.settingslib.core.lifecycle.Lifecycle; 31 import com.android.settingslib.widget.ButtonPreference; 32 33 import com.google.common.annotations.VisibleForTesting; 34 35 import java.util.Set; 36 37 /** 38 * This class handles button preference logic to display for hearing aid device. 39 */ 40 public class BluetoothDetailsPairOtherController extends BluetoothDetailsController { 41 private static final String KEY_PAIR_OTHER = "hearing_aid_pair_other_button"; 42 @VisibleForTesting 43 static final String KEY_SPACE = "hearing_aid_space_layout"; 44 45 private ButtonPreference mPreference; 46 private SpacePreference mSpacePreference; 47 BluetoothDetailsPairOtherController(Context context, PreferenceFragmentCompat fragment, CachedBluetoothDevice device, Lifecycle lifecycle)48 public BluetoothDetailsPairOtherController(Context context, 49 PreferenceFragmentCompat fragment, 50 CachedBluetoothDevice device, 51 Lifecycle lifecycle) { 52 super(context, fragment, device, lifecycle); 53 } 54 55 @Override isAvailable()56 public boolean isAvailable() { 57 return getButtonPreferenceVisibility(mCachedDevice); 58 } 59 60 @Override getPreferenceKey()61 public String getPreferenceKey() { 62 return KEY_PAIR_OTHER; 63 } 64 65 @Override init(PreferenceScreen screen)66 protected void init(PreferenceScreen screen) { 67 mPreference = screen.findPreference(getPreferenceKey()); 68 mSpacePreference = screen.findPreference(KEY_SPACE); 69 updateButtonPreferenceTitle(mPreference); 70 setPreferencesVisibility(getButtonPreferenceVisibility(mCachedDevice)); 71 mPreference.setOnClickListener(v -> launchPairingDetail()); 72 } 73 74 @Override refresh()75 protected void refresh() { 76 updateButtonPreferenceTitle(mPreference); 77 setPreferencesVisibility(getButtonPreferenceVisibility(mCachedDevice)); 78 } 79 updateButtonPreferenceTitle(ButtonPreference preference)80 private void updateButtonPreferenceTitle(ButtonPreference preference) { 81 final int side = mCachedDevice.getDeviceSide(); 82 final int stringRes = (side == HearingAidInfo.DeviceSide.SIDE_LEFT) 83 ? R.string.bluetooth_pair_right_ear_button 84 : R.string.bluetooth_pair_left_ear_button; 85 86 preference.setTitle(stringRes); 87 } 88 setPreferencesVisibility(boolean visible)89 private void setPreferencesVisibility(boolean visible) { 90 mPreference.setVisible(visible); 91 mSpacePreference.setVisible(visible); 92 } 93 getButtonPreferenceVisibility(CachedBluetoothDevice cachedDevice)94 private boolean getButtonPreferenceVisibility(CachedBluetoothDevice cachedDevice) { 95 // The device is not connected yet. Don't show the button. 96 if (!cachedDevice.isConnectedHearingAidDevice()) { 97 return false; 98 } 99 return isBinauralMode(cachedDevice) && !isOtherSideBonded(cachedDevice); 100 } 101 launchPairingDetail()102 private void launchPairingDetail() { 103 new SubSettingLauncher(mContext) 104 .setDestination(BluetoothPairingDetail.class.getName()) 105 .setSourceMetricsCategory( 106 ((BluetoothDeviceDetailsFragment) mFragment).getMetricsCategory()) 107 .launch(); 108 } 109 isBinauralMode(CachedBluetoothDevice cachedDevice)110 private boolean isBinauralMode(CachedBluetoothDevice cachedDevice) { 111 return cachedDevice.getDeviceMode() == HearingAidInfo.DeviceMode.MODE_BINAURAL; 112 } 113 isOtherSideBonded(CachedBluetoothDevice cachedDevice)114 private boolean isOtherSideBonded(CachedBluetoothDevice cachedDevice) { 115 final CachedBluetoothDevice subDevice = cachedDevice.getSubDevice(); 116 final boolean subDeviceBonded = 117 subDevice != null && subDevice.getBondState() == BluetoothDevice.BOND_BONDED; 118 119 final Set<CachedBluetoothDevice> memberDevice = cachedDevice.getMemberDevice(); 120 final boolean allMemberDevicesBonded = 121 !memberDevice.isEmpty() && memberDevice.stream().allMatch( 122 device -> device.getBondState() == BluetoothDevice.BOND_BONDED); 123 124 return subDeviceBonded || allMemberDevicesBonded; 125 } 126 } 127