Add BT extra options in device details page

Bug: 319562236
Test: make RunSettingsRoboTests ROBOTEST_FILTER=BluetoothDetailsExtraOptionsControllerTest
Change-Id: Ifab39d2bd19044d60f090f4a0419d1e20b2ad925
This commit is contained in:
Haijie Hong
2024-01-15 17:30:04 +08:00
parent dea44ebf7b
commit 76d8145f15
6 changed files with 207 additions and 0 deletions

View File

@@ -80,6 +80,9 @@
<PreferenceCategory
android:key="bluetooth_profiles"/>
<PreferenceCategory
android:key="bt_extra_options"/>
<PreferenceCategory
android:key="bluetooth_related_tools"
android:title="@string/bluetooth_screen_related">

View File

@@ -0,0 +1,88 @@
/*
* Copyright (C) 2024 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.bluetooth;
import android.content.Context;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import androidx.preference.Preference;
import androidx.preference.PreferenceCategory;
import androidx.preference.PreferenceFragmentCompat;
import androidx.preference.PreferenceScreen;
import com.android.settings.overlay.FeatureFactory;
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
import com.android.settingslib.core.lifecycle.Lifecycle;
import com.android.settingslib.utils.ThreadUtils;
import dagger.internal.Preconditions;
import java.util.List;
public class BluetoothDetailsExtraOptionsController extends BluetoothDetailsController {
private static final String KEY_BLUETOOTH_EXTRA_OPTIONS = "bt_extra_options";
@VisibleForTesting @Nullable
PreferenceCategory mOptionsContainer;
@Nullable PreferenceScreen mPreferenceScreen;
public BluetoothDetailsExtraOptionsController(
Context context,
PreferenceFragmentCompat fragment,
CachedBluetoothDevice device,
Lifecycle lifecycle) {
super(context, fragment, device, lifecycle);
}
@Override
public String getPreferenceKey() {
return KEY_BLUETOOTH_EXTRA_OPTIONS;
}
@Override
protected void init(PreferenceScreen screen) {
mPreferenceScreen = screen;
mOptionsContainer = screen.findPreference(getPreferenceKey());
refresh();
}
@Override
protected void refresh() {
ThreadUtils.postOnBackgroundThread(
() -> {
List<Preference> options =
FeatureFactory.getFeatureFactory()
.getBluetoothFeatureProvider()
.getBluetoothExtraOptions(mContext, mCachedDevice);
ThreadUtils.postOnMainThread(
() -> {
if (mOptionsContainer != null) {
mOptionsContainer.removeAll();
for (Preference option : options) {
mOptionsContainer.addPreference(option);
}
setVisible(
Preconditions.checkNotNull(mPreferenceScreen),
getPreferenceKey(),
!options.isEmpty());
}
});
});
}
}

View File

@@ -328,6 +328,9 @@ public class BluetoothDeviceDetailsFragment extends RestrictedDashboardFragment
mCachedDevice, lifecycle));
controllers.add(new BluetoothDetailsDataSyncController(context, this,
mCachedDevice, lifecycle));
controllers.add(
new BluetoothDetailsExtraOptionsController(
context, this, mCachedDevice, lifecycle));
}
return controllers;
}

View File

@@ -22,6 +22,10 @@ import android.content.Context;
import android.media.Spatializer;
import android.net.Uri;
import androidx.preference.Preference;
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
import java.util.List;
/**
@@ -60,4 +64,13 @@ public interface BluetoothFeatureProvider {
* @return the Spatializer instance
*/
Spatializer getSpatializer(Context context);
/**
* Gets bluetooth device extra options
*
* @param context Context
* @param device the bluetooth device
* @return the extra bluetooth preference list
*/
List<Preference> getBluetoothExtraOptions(Context context, CachedBluetoothDevice device);
}

View File

@@ -23,7 +23,12 @@ import android.media.AudioManager;
import android.media.Spatializer;
import android.net.Uri;
import androidx.preference.Preference;
import com.android.settingslib.bluetooth.BluetoothUtils;
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
import com.google.common.collect.ImmutableList;
import java.util.List;
@@ -54,4 +59,10 @@ public class BluetoothFeatureProviderImpl implements BluetoothFeatureProvider {
AudioManager audioManager = context.getSystemService(AudioManager.class);
return audioManager.getSpatializer();
}
@Override
public List<Preference> getBluetoothExtraOptions(Context context,
CachedBluetoothDevice device) {
return ImmutableList.of();
}
}

View File

@@ -0,0 +1,89 @@
/*
* Copyright (C) 2024 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.bluetooth;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.bluetooth.BluetoothDevice;
import androidx.preference.Preference;
import androidx.preference.PreferenceCategory;
import androidx.preference.PreferenceScreen;
import androidx.preference.SwitchPreferenceCompat;
import com.android.settings.testutils.FakeFeatureFactory;
import com.android.settingslib.core.lifecycle.Lifecycle;
import com.google.common.collect.ImmutableList;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.shadows.ShadowLooper;
@RunWith(RobolectricTestRunner.class)
public class BluetoothDetailsExtraOptionsControllerTest extends BluetoothDetailsControllerTestBase {
private static final String MAC_ADDRESS = "04:52:C7:0B:D8:3C";
@Mock private BluetoothDevice mBluetoothDevice;
@Mock private Lifecycle mExtraOptionsLifecycle;
@Mock private PreferenceCategory mOptionsContainer;
@Mock private PreferenceScreen mPreferenceScreen;
private BluetoothDetailsExtraOptionsController mController;
private BluetoothFeatureProvider mFeatureProvider;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = spy(RuntimeEnvironment.application);
when(mCachedDevice.getAddress()).thenReturn(MAC_ADDRESS);
when(mCachedDevice.getDevice()).thenReturn(mBluetoothDevice);
when(mBluetoothDevice.getAnonymizedAddress()).thenReturn(MAC_ADDRESS);
final FakeFeatureFactory fakeFeatureFactory = FakeFeatureFactory.setupForTest();
mFeatureProvider = fakeFeatureFactory.getBluetoothFeatureProvider();
mController =
new BluetoothDetailsExtraOptionsController(
mContext, mFragment, mCachedDevice, mExtraOptionsLifecycle);
}
@Test
public void displayPreference_removeAndAddPreferences() {
Preference preference1 = new SwitchPreferenceCompat(mContext);
Preference preference2 = new SwitchPreferenceCompat(mContext);
when(mFeatureProvider.getBluetoothExtraOptions(mContext, mCachedDevice))
.thenReturn(ImmutableList.of(preference1, preference2));
when(mPreferenceScreen.findPreference(mController.getPreferenceKey()))
.thenReturn(mOptionsContainer);
mController.displayPreference(mPreferenceScreen);
ShadowLooper.idleMainLooper();
verify(mOptionsContainer).removeAll();
verify(mOptionsContainer).addPreference(preference1);
verify(mOptionsContainer).addPreference(preference2);
}
}