hughchen | bd3e5de | 2018-03-23 16:14:13 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 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 | package com.android.settings.connecteddevice; |
| 17 | |
hughchen | f431088 | 2018-04-17 16:36:58 +0800 | [diff] [blame] | 18 | import static com.android.settingslib.Utils.isAudioModeOngoingCall; |
| 19 | |
jasonwshsu | acd3f94 | 2022-03-20 16:12:18 +0800 | [diff] [blame] | 20 | import android.bluetooth.BluetoothProfile; |
hughchen | bd3e5de | 2018-03-23 16:14:13 +0800 | [diff] [blame] | 21 | import android.content.Context; |
| 22 | import android.content.pm.PackageManager; |
hughchen | 34066fa | 2018-06-25 11:06:45 +0800 | [diff] [blame] | 23 | import android.util.Log; |
Fan Zhang | c7162cd | 2018-06-18 15:21:41 -0700 | [diff] [blame] | 24 | |
Yiyi Shen | bf6e273 | 2023-12-13 18:14:42 +0800 | [diff] [blame] | 25 | import androidx.annotation.NonNull; |
Yiyi Shen | 42b8fbb | 2024-02-23 15:11:11 +0800 | [diff] [blame] | 26 | import androidx.annotation.Nullable; |
Fan Zhang | 23f8d59 | 2018-08-28 15:11:40 -0700 | [diff] [blame] | 27 | import androidx.annotation.VisibleForTesting; |
jasonwshsu | acd3f94 | 2022-03-20 16:12:18 +0800 | [diff] [blame] | 28 | import androidx.fragment.app.FragmentManager; |
Yiyi Shen | 42b8fbb | 2024-02-23 15:11:11 +0800 | [diff] [blame] | 29 | import androidx.lifecycle.DefaultLifecycleObserver; |
| 30 | import androidx.lifecycle.LifecycleOwner; |
Fan Zhang | 23f8d59 | 2018-08-28 15:11:40 -0700 | [diff] [blame] | 31 | import androidx.preference.Preference; |
| 32 | import androidx.preference.PreferenceGroup; |
| 33 | import androidx.preference.PreferenceScreen; |
| 34 | |
Fan Zhang | c7162cd | 2018-06-18 15:21:41 -0700 | [diff] [blame] | 35 | import com.android.settings.R; |
jasonwshsu | acd3f94 | 2022-03-20 16:12:18 +0800 | [diff] [blame] | 36 | import com.android.settings.accessibility.HearingAidUtils; |
hughchen | bd3e5de | 2018-03-23 16:14:13 +0800 | [diff] [blame] | 37 | import com.android.settings.bluetooth.AvailableMediaBluetoothDeviceUpdater; |
| 38 | import com.android.settings.bluetooth.BluetoothDeviceUpdater; |
hughchen | f431088 | 2018-04-17 16:36:58 +0800 | [diff] [blame] | 39 | import com.android.settings.bluetooth.Utils; |
hughchen | bd3e5de | 2018-03-23 16:14:13 +0800 | [diff] [blame] | 40 | import com.android.settings.core.BasePreferenceController; |
| 41 | import com.android.settings.dashboard.DashboardFragment; |
hughchen | f431088 | 2018-04-17 16:36:58 +0800 | [diff] [blame] | 42 | import com.android.settingslib.bluetooth.BluetoothCallback; |
Yiyi Shen | bf6e273 | 2023-12-13 18:14:42 +0800 | [diff] [blame] | 43 | import com.android.settingslib.bluetooth.BluetoothUtils; |
jasonwshsu | acd3f94 | 2022-03-20 16:12:18 +0800 | [diff] [blame] | 44 | import com.android.settingslib.bluetooth.CachedBluetoothDevice; |
hughchen | f431088 | 2018-04-17 16:36:58 +0800 | [diff] [blame] | 45 | import com.android.settingslib.bluetooth.LocalBluetoothManager; |
Yiyi Shen | 42b8fbb | 2024-02-23 15:11:11 +0800 | [diff] [blame] | 46 | import com.android.settingslib.core.lifecycle.Lifecycle; |
Yiyi Shen | bf6e273 | 2023-12-13 18:14:42 +0800 | [diff] [blame] | 47 | |
hughchen | bd3e5de | 2018-03-23 16:14:13 +0800 | [diff] [blame] | 48 | /** |
Yiyi Shen | bf6e273 | 2023-12-13 18:14:42 +0800 | [diff] [blame] | 49 | * Controller to maintain the {@link androidx.preference.PreferenceGroup} for all available media |
| 50 | * devices. It uses {@link DevicePreferenceCallback} to add/remove {@link Preference} |
hughchen | bd3e5de | 2018-03-23 16:14:13 +0800 | [diff] [blame] | 51 | */ |
| 52 | public class AvailableMediaDeviceGroupController extends BasePreferenceController |
Yiyi Shen | 42b8fbb | 2024-02-23 15:11:11 +0800 | [diff] [blame] | 53 | implements DefaultLifecycleObserver, DevicePreferenceCallback, BluetoothCallback { |
Yiyi Shen | bf6e273 | 2023-12-13 18:14:42 +0800 | [diff] [blame] | 54 | private static final boolean DEBUG = BluetoothUtils.D; |
hughchen | bd3e5de | 2018-03-23 16:14:13 +0800 | [diff] [blame] | 55 | |
hughchen | 34066fa | 2018-06-25 11:06:45 +0800 | [diff] [blame] | 56 | private static final String TAG = "AvailableMediaDeviceGroupController"; |
hughchen | bd3e5de | 2018-03-23 16:14:13 +0800 | [diff] [blame] | 57 | private static final String KEY = "available_device_list"; |
| 58 | |
Yiyi Shen | 42b8fbb | 2024-02-23 15:11:11 +0800 | [diff] [blame] | 59 | @VisibleForTesting @Nullable PreferenceGroup mPreferenceGroup; |
Yiyi Shen | bf6e273 | 2023-12-13 18:14:42 +0800 | [diff] [blame] | 60 | @VisibleForTesting LocalBluetoothManager mLocalBluetoothManager; |
Yiyi Shen | 42b8fbb | 2024-02-23 15:11:11 +0800 | [diff] [blame] | 61 | @Nullable private BluetoothDeviceUpdater mBluetoothDeviceUpdater; |
| 62 | @Nullable private FragmentManager mFragmentManager; |
Yiyi Shen | bf6e273 | 2023-12-13 18:14:42 +0800 | [diff] [blame] | 63 | |
Yiyi Shen | 42b8fbb | 2024-02-23 15:11:11 +0800 | [diff] [blame] | 64 | public AvailableMediaDeviceGroupController( |
| 65 | Context context, |
| 66 | @Nullable DashboardFragment fragment, |
| 67 | @Nullable Lifecycle lifecycle) { |
hughchen | bd3e5de | 2018-03-23 16:14:13 +0800 | [diff] [blame] | 68 | super(context, KEY); |
Yiyi Shen | 42b8fbb | 2024-02-23 15:11:11 +0800 | [diff] [blame] | 69 | if (fragment != null) { |
| 70 | init(fragment); |
| 71 | } |
| 72 | if (lifecycle != null) { |
| 73 | lifecycle.addObserver(this); |
| 74 | } |
hughchen | f431088 | 2018-04-17 16:36:58 +0800 | [diff] [blame] | 75 | mLocalBluetoothManager = Utils.getLocalBtManager(mContext); |
hughchen | bd3e5de | 2018-03-23 16:14:13 +0800 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | @Override |
Yiyi Shen | 42b8fbb | 2024-02-23 15:11:11 +0800 | [diff] [blame] | 79 | public void onStart(@NonNull LifecycleOwner owner) { |
hughchen | 34066fa | 2018-06-25 11:06:45 +0800 | [diff] [blame] | 80 | if (mLocalBluetoothManager == null) { |
| 81 | Log.e(TAG, "onStart() Bluetooth is not supported on this device"); |
| 82 | return; |
| 83 | } |
hughchen | f431088 | 2018-04-17 16:36:58 +0800 | [diff] [blame] | 84 | mLocalBluetoothManager.getEventManager().registerCallback(this); |
Yiyi Shen | 42b8fbb | 2024-02-23 15:11:11 +0800 | [diff] [blame] | 85 | if (mBluetoothDeviceUpdater != null) { |
| 86 | mBluetoothDeviceUpdater.registerCallback(); |
| 87 | mBluetoothDeviceUpdater.refreshPreference(); |
| 88 | } |
hughchen | bd3e5de | 2018-03-23 16:14:13 +0800 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | @Override |
Yiyi Shen | 42b8fbb | 2024-02-23 15:11:11 +0800 | [diff] [blame] | 92 | public void onStop(@NonNull LifecycleOwner owner) { |
hughchen | 34066fa | 2018-06-25 11:06:45 +0800 | [diff] [blame] | 93 | if (mLocalBluetoothManager == null) { |
| 94 | Log.e(TAG, "onStop() Bluetooth is not supported on this device"); |
| 95 | return; |
| 96 | } |
Yiyi Shen | 42b8fbb | 2024-02-23 15:11:11 +0800 | [diff] [blame] | 97 | if (mBluetoothDeviceUpdater != null) { |
| 98 | mBluetoothDeviceUpdater.unregisterCallback(); |
Yiyi Shen | bf6e273 | 2023-12-13 18:14:42 +0800 | [diff] [blame] | 99 | } |
hughchen | f431088 | 2018-04-17 16:36:58 +0800 | [diff] [blame] | 100 | mLocalBluetoothManager.getEventManager().unregisterCallback(this); |
hughchen | bd3e5de | 2018-03-23 16:14:13 +0800 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public void displayPreference(PreferenceScreen screen) { |
| 105 | super.displayPreference(screen); |
hughchen | ef2c1a1 | 2019-04-17 15:43:22 +0800 | [diff] [blame] | 106 | |
| 107 | mPreferenceGroup = screen.findPreference(KEY); |
Yiyi Shen | 42b8fbb | 2024-02-23 15:11:11 +0800 | [diff] [blame] | 108 | if (mPreferenceGroup != null) { |
| 109 | mPreferenceGroup.setVisible(false); |
| 110 | } |
hughchen | ef2c1a1 | 2019-04-17 15:43:22 +0800 | [diff] [blame] | 111 | |
hughchen | bd3e5de | 2018-03-23 16:14:13 +0800 | [diff] [blame] | 112 | if (isAvailable()) { |
hughchen | f431088 | 2018-04-17 16:36:58 +0800 | [diff] [blame] | 113 | updateTitle(); |
Yiyi Shen | 42b8fbb | 2024-02-23 15:11:11 +0800 | [diff] [blame] | 114 | if (mBluetoothDeviceUpdater != null) { |
| 115 | mBluetoothDeviceUpdater.setPrefContext(screen.getContext()); |
| 116 | mBluetoothDeviceUpdater.forceUpdate(); |
| 117 | } |
hughchen | bd3e5de | 2018-03-23 16:14:13 +0800 | [diff] [blame] | 118 | } |
| 119 | } |
| 120 | |
| 121 | @Override |
| 122 | public int getAvailabilityStatus() { |
| 123 | return mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH) |
Fan Zhang | 9ce4a1f | 2018-08-15 12:55:56 -0700 | [diff] [blame] | 124 | ? AVAILABLE_UNSEARCHABLE |
Matthew Fritze | f87a1f3 | 2018-05-03 16:46:51 -0700 | [diff] [blame] | 125 | : UNSUPPORTED_ON_DEVICE; |
hughchen | bd3e5de | 2018-03-23 16:14:13 +0800 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | @Override |
| 129 | public String getPreferenceKey() { |
| 130 | return KEY; |
| 131 | } |
| 132 | |
| 133 | @Override |
| 134 | public void onDeviceAdded(Preference preference) { |
Yiyi Shen | 42b8fbb | 2024-02-23 15:11:11 +0800 | [diff] [blame] | 135 | if (mPreferenceGroup != null) { |
| 136 | if (mPreferenceGroup.getPreferenceCount() == 0) { |
| 137 | mPreferenceGroup.setVisible(true); |
| 138 | } |
| 139 | mPreferenceGroup.addPreference(preference); |
hughchen | bd3e5de | 2018-03-23 16:14:13 +0800 | [diff] [blame] | 140 | } |
hughchen | bd3e5de | 2018-03-23 16:14:13 +0800 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | @Override |
| 144 | public void onDeviceRemoved(Preference preference) { |
Yiyi Shen | 42b8fbb | 2024-02-23 15:11:11 +0800 | [diff] [blame] | 145 | if (mPreferenceGroup != null) { |
| 146 | mPreferenceGroup.removePreference(preference); |
| 147 | if (mPreferenceGroup.getPreferenceCount() == 0) { |
| 148 | mPreferenceGroup.setVisible(false); |
| 149 | } |
hughchen | bd3e5de | 2018-03-23 16:14:13 +0800 | [diff] [blame] | 150 | } |
| 151 | } |
| 152 | |
| 153 | public void init(DashboardFragment fragment) { |
jasonwshsu | acd3f94 | 2022-03-20 16:12:18 +0800 | [diff] [blame] | 154 | mFragmentManager = fragment.getParentFragmentManager(); |
Yiyi Shen | bf6e273 | 2023-12-13 18:14:42 +0800 | [diff] [blame] | 155 | mBluetoothDeviceUpdater = |
| 156 | new AvailableMediaBluetoothDeviceUpdater( |
| 157 | fragment.getContext(), |
| 158 | AvailableMediaDeviceGroupController.this, |
| 159 | fragment.getMetricsCategory()); |
hughchen | bd3e5de | 2018-03-23 16:14:13 +0800 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | @VisibleForTesting |
Angela Wang | bb1fe04 | 2023-09-08 11:35:03 +0000 | [diff] [blame] | 163 | public void setFragmentManager(FragmentManager fragmentManager) { |
| 164 | mFragmentManager = fragmentManager; |
| 165 | } |
| 166 | |
| 167 | @VisibleForTesting |
hughchen | bd3e5de | 2018-03-23 16:14:13 +0800 | [diff] [blame] | 168 | public void setBluetoothDeviceUpdater(BluetoothDeviceUpdater bluetoothDeviceUpdater) { |
Fan Zhang | 7db118e | 2019-02-14 12:25:29 -0800 | [diff] [blame] | 169 | mBluetoothDeviceUpdater = bluetoothDeviceUpdater; |
hughchen | bd3e5de | 2018-03-23 16:14:13 +0800 | [diff] [blame] | 170 | } |
hughchen | f431088 | 2018-04-17 16:36:58 +0800 | [diff] [blame] | 171 | |
| 172 | @Override |
hughchen | f431088 | 2018-04-17 16:36:58 +0800 | [diff] [blame] | 173 | public void onAudioModeChanged() { |
| 174 | updateTitle(); |
| 175 | } |
| 176 | |
jasonwshsu | acd3f94 | 2022-03-20 16:12:18 +0800 | [diff] [blame] | 177 | @Override |
| 178 | public void onActiveDeviceChanged(CachedBluetoothDevice activeDevice, int bluetoothProfile) { |
| 179 | // exclude inactive device |
| 180 | if (activeDevice == null) { |
| 181 | return; |
| 182 | } |
| 183 | |
| 184 | if (bluetoothProfile == BluetoothProfile.HEARING_AID) { |
Yiyi Shen | bf6e273 | 2023-12-13 18:14:42 +0800 | [diff] [blame] | 185 | HearingAidUtils.launchHearingAidPairingDialog( |
| 186 | mFragmentManager, activeDevice, getMetricsCategory()); |
jasonwshsu | acd3f94 | 2022-03-20 16:12:18 +0800 | [diff] [blame] | 187 | } |
| 188 | } |
| 189 | |
hughchen | f431088 | 2018-04-17 16:36:58 +0800 | [diff] [blame] | 190 | private void updateTitle() { |
Yiyi Shen | 42b8fbb | 2024-02-23 15:11:11 +0800 | [diff] [blame] | 191 | if (mPreferenceGroup != null) { |
| 192 | if (isAudioModeOngoingCall(mContext)) { |
| 193 | // in phone call |
| 194 | mPreferenceGroup.setTitle( |
| 195 | mContext.getString(R.string.connected_device_call_device_title)); |
| 196 | } else { |
| 197 | // without phone call |
| 198 | mPreferenceGroup.setTitle( |
| 199 | mContext.getString(R.string.connected_device_media_device_title)); |
| 200 | } |
hughchen | f431088 | 2018-04-17 16:36:58 +0800 | [diff] [blame] | 201 | } |
| 202 | } |
hughchen | bd3e5de | 2018-03-23 16:14:13 +0800 | [diff] [blame] | 203 | } |