1 /* 2 * Copyright (C) 2024 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 21 import androidx.annotation.Nullable; 22 import androidx.annotation.VisibleForTesting; 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.overlay.FeatureFactory; 29 import com.android.settingslib.bluetooth.CachedBluetoothDevice; 30 import com.android.settingslib.core.lifecycle.Lifecycle; 31 import com.android.settingslib.utils.ThreadUtils; 32 33 import dagger.internal.Preconditions; 34 35 import java.util.List; 36 37 public class BluetoothDetailsExtraOptionsController extends BluetoothDetailsController { 38 39 private static final String KEY_BLUETOOTH_EXTRA_OPTIONS = "bt_extra_options"; 40 41 @VisibleForTesting @Nullable 42 PreferenceCategory mOptionsContainer; 43 @Nullable PreferenceScreen mPreferenceScreen; 44 BluetoothDetailsExtraOptionsController( Context context, PreferenceFragmentCompat fragment, CachedBluetoothDevice device, Lifecycle lifecycle)45 public BluetoothDetailsExtraOptionsController( 46 Context context, 47 PreferenceFragmentCompat fragment, 48 CachedBluetoothDevice device, 49 Lifecycle lifecycle) { 50 super(context, fragment, device, lifecycle); 51 } 52 53 @Override getPreferenceKey()54 public String getPreferenceKey() { 55 return KEY_BLUETOOTH_EXTRA_OPTIONS; 56 } 57 58 @Override init(PreferenceScreen screen)59 protected void init(PreferenceScreen screen) { 60 mPreferenceScreen = screen; 61 mOptionsContainer = screen.findPreference(getPreferenceKey()); 62 refresh(); 63 } 64 65 @Override refresh()66 protected void refresh() { 67 ThreadUtils.postOnBackgroundThread( 68 () -> { 69 List<Preference> options = 70 FeatureFactory.getFeatureFactory() 71 .getBluetoothFeatureProvider() 72 .getBluetoothExtraOptions(mContext, mCachedDevice); 73 ThreadUtils.postOnMainThread( 74 () -> { 75 if (mOptionsContainer != null) { 76 mOptionsContainer.removeAll(); 77 for (Preference option : options) { 78 mOptionsContainer.addPreference(option); 79 } 80 setVisible( 81 Preconditions.checkNotNull(mPreferenceScreen), 82 getPreferenceKey(), 83 !options.isEmpty()); 84 } 85 }); 86 }); 87 } 88 } 89