Limit displayable rows in BluetoothDevicesSlice
The maximum displayable row count in current design is 3. i.e., There are 1 header and 3 rows in a Slice when the number of Bluetooth device exceeds 3. Change-Id: Ie585617da0665dde4cbe1ab60a2693e3e1199c27 Fixes: 121308955 Bug: 114807655 Test: visual, robotests
This commit is contained in:
@@ -63,6 +63,13 @@ public class BluetoothDevicesSlice implements CustomSliceable {
|
|||||||
@VisibleForTesting
|
@VisibleForTesting
|
||||||
static final String BLUETOOTH_DEVICE_HASH_CODE = "bluetooth_device_hash_code";
|
static final String BLUETOOTH_DEVICE_HASH_CODE = "bluetooth_device_hash_code";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add the "Pair new device" in the end of slice, when the number of Bluetooth devices is less
|
||||||
|
* than {@link #DEFAULT_EXPANDED_ROW_COUNT}.
|
||||||
|
*/
|
||||||
|
@VisibleForTesting
|
||||||
|
static final int DEFAULT_EXPANDED_ROW_COUNT = 3;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Refer {@link com.android.settings.bluetooth.BluetoothDevicePreference#compareTo} to sort the
|
* Refer {@link com.android.settings.bluetooth.BluetoothDevicePreference#compareTo} to sort the
|
||||||
* Bluetooth devices by {@link CachedBluetoothDevice}.
|
* Bluetooth devices by {@link CachedBluetoothDevice}.
|
||||||
@@ -70,12 +77,6 @@ public class BluetoothDevicesSlice implements CustomSliceable {
|
|||||||
private static final Comparator<CachedBluetoothDevice> COMPARATOR
|
private static final Comparator<CachedBluetoothDevice> COMPARATOR
|
||||||
= Comparator.naturalOrder();
|
= Comparator.naturalOrder();
|
||||||
|
|
||||||
/**
|
|
||||||
* Add the "Pair new device" in the end of slice, when the number of Bluetooth devices is less
|
|
||||||
* than {@link #DEFAULT_EXPANDED_ROW_COUNT}.
|
|
||||||
*/
|
|
||||||
private static final int DEFAULT_EXPANDED_ROW_COUNT = 3;
|
|
||||||
|
|
||||||
private static final String TAG = "BluetoothDevicesSlice";
|
private static final String TAG = "BluetoothDevicesSlice";
|
||||||
|
|
||||||
private final Context mContext;
|
private final Context mContext;
|
||||||
@@ -127,15 +128,18 @@ public class BluetoothDevicesSlice implements CustomSliceable {
|
|||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
// According the number of Bluetooth devices to set sub title of header.
|
// Get displayable device count.
|
||||||
|
final int deviceCount = Math.min(rows.size(), DEFAULT_EXPANDED_ROW_COUNT);
|
||||||
|
|
||||||
|
// According to the displayable device count to set sub title of header.
|
||||||
listBuilder.setHeader(new ListBuilder.HeaderBuilder()
|
listBuilder.setHeader(new ListBuilder.HeaderBuilder()
|
||||||
.setTitle(title)
|
.setTitle(title)
|
||||||
.setSubtitle(getSubTitle(rows.size()))
|
.setSubtitle(getSubTitle(deviceCount))
|
||||||
.setPrimaryAction(primarySliceAction));
|
.setPrimaryAction(primarySliceAction));
|
||||||
|
|
||||||
// Add Bluetooth device rows.
|
// According to the displayable device count to add bluetooth device rows.
|
||||||
for (ListBuilder.RowBuilder rowBuilder : rows) {
|
for (int i = 0; i < deviceCount; i++) {
|
||||||
listBuilder.addRow(rowBuilder);
|
listBuilder.addRow(rows.get(i));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add "Pair new device" if need.
|
// Add "Pair new device" if need.
|
||||||
@@ -238,7 +242,7 @@ public class BluetoothDevicesSlice implements CustomSliceable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private List<ListBuilder.RowBuilder> getBluetoothRowBuilder() {
|
private List<ListBuilder.RowBuilder> getBluetoothRowBuilder() {
|
||||||
// According Bluetooth devices to create row builders.
|
// According to Bluetooth devices to create row builders.
|
||||||
final List<ListBuilder.RowBuilder> bluetoothRows = new ArrayList<>();
|
final List<ListBuilder.RowBuilder> bluetoothRows = new ArrayList<>();
|
||||||
final List<CachedBluetoothDevice> bluetoothDevices = getConnectedBluetoothDevices();
|
final List<CachedBluetoothDevice> bluetoothDevices = getConnectedBluetoothDevices();
|
||||||
for (CachedBluetoothDevice bluetoothDevice : bluetoothDevices) {
|
for (CachedBluetoothDevice bluetoothDevice : bluetoothDevices) {
|
||||||
|
@@ -16,6 +16,9 @@
|
|||||||
|
|
||||||
package com.android.settings.homepage.contextualcards.slices;
|
package com.android.settings.homepage.contextualcards.slices;
|
||||||
|
|
||||||
|
import static android.app.slice.Slice.HINT_LIST_ITEM;
|
||||||
|
import static android.app.slice.SliceItem.FORMAT_SLICE;
|
||||||
|
|
||||||
import static com.google.common.truth.Truth.assertThat;
|
import static com.google.common.truth.Truth.assertThat;
|
||||||
|
|
||||||
import static org.mockito.ArgumentMatchers.any;
|
import static org.mockito.ArgumentMatchers.any;
|
||||||
@@ -34,6 +37,7 @@ import androidx.slice.Slice;
|
|||||||
import androidx.slice.SliceItem;
|
import androidx.slice.SliceItem;
|
||||||
import androidx.slice.SliceMetadata;
|
import androidx.slice.SliceMetadata;
|
||||||
import androidx.slice.SliceProvider;
|
import androidx.slice.SliceProvider;
|
||||||
|
import androidx.slice.core.SliceQuery;
|
||||||
import androidx.slice.widget.SliceLiveData;
|
import androidx.slice.widget.SliceLiveData;
|
||||||
|
|
||||||
import com.android.settings.R;
|
import com.android.settings.R;
|
||||||
@@ -97,7 +101,7 @@ public class BluetoothDevicesSliceTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void getSlice_hasBluetoothDevices_shouldHaveBluetoothDevicesTitle() {
|
public void getSlice_hasBluetoothDevices_shouldHaveBluetoothDevicesTitle() {
|
||||||
mockBluetoothDeviceList();
|
mockBluetoothDeviceList(1);
|
||||||
doReturn(mBluetoothDeviceList).when(mBluetoothDevicesSlice).getConnectedBluetoothDevices();
|
doReturn(mBluetoothDeviceList).when(mBluetoothDevicesSlice).getConnectedBluetoothDevices();
|
||||||
|
|
||||||
final Slice slice = mBluetoothDevicesSlice.getSlice();
|
final Slice slice = mBluetoothDevicesSlice.getSlice();
|
||||||
@@ -108,7 +112,7 @@ public class BluetoothDevicesSliceTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void getSlice_hasBluetoothDevices_shouldMatchBluetoothMockTitle() {
|
public void getSlice_hasBluetoothDevices_shouldMatchBluetoothMockTitle() {
|
||||||
mockBluetoothDeviceList();
|
mockBluetoothDeviceList(1);
|
||||||
doReturn(mBluetoothDeviceList).when(mBluetoothDevicesSlice).getConnectedBluetoothDevices();
|
doReturn(mBluetoothDeviceList).when(mBluetoothDevicesSlice).getConnectedBluetoothDevices();
|
||||||
|
|
||||||
final Slice slice = mBluetoothDevicesSlice.getSlice();
|
final Slice slice = mBluetoothDevicesSlice.getSlice();
|
||||||
@@ -119,7 +123,7 @@ public class BluetoothDevicesSliceTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void getSlice_hasBluetoothDevices_shouldHavePairNewDevice() {
|
public void getSlice_hasBluetoothDevices_shouldHavePairNewDevice() {
|
||||||
mockBluetoothDeviceList();
|
mockBluetoothDeviceList(1);
|
||||||
doReturn(mBluetoothDeviceList).when(mBluetoothDevicesSlice).getConnectedBluetoothDevices();
|
doReturn(mBluetoothDeviceList).when(mBluetoothDevicesSlice).getConnectedBluetoothDevices();
|
||||||
|
|
||||||
final Slice slice = mBluetoothDevicesSlice.getSlice();
|
final Slice slice = mBluetoothDevicesSlice.getSlice();
|
||||||
@@ -150,9 +154,35 @@ public class BluetoothDevicesSliceTest {
|
|||||||
mContext.getString(R.string.bluetooth_pairing_pref_title))).isFalse();
|
mContext.getString(R.string.bluetooth_pairing_pref_title))).isFalse();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void getSlice_exceedDefaultRowCount_shouldOnlyShowDefaultRows() {
|
||||||
|
mockBluetoothDeviceList(BluetoothDevicesSlice.DEFAULT_EXPANDED_ROW_COUNT + 1);
|
||||||
|
doReturn(mBluetoothDeviceList).when(mBluetoothDevicesSlice).getConnectedBluetoothDevices();
|
||||||
|
|
||||||
|
final Slice slice = mBluetoothDevicesSlice.getSlice();
|
||||||
|
|
||||||
|
// Get the number of RowBuilders from Slice.
|
||||||
|
final int rows = SliceQuery.findAll(slice, FORMAT_SLICE, HINT_LIST_ITEM, null).size();
|
||||||
|
assertThat(rows).isEqualTo(BluetoothDevicesSlice.DEFAULT_EXPANDED_ROW_COUNT);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void getSlice_exceedDefaultRowCount_shouldContainDefaultCountInSubTitle() {
|
||||||
|
mockBluetoothDeviceList(BluetoothDevicesSlice.DEFAULT_EXPANDED_ROW_COUNT + 1);
|
||||||
|
doReturn(mBluetoothDeviceList).when(mBluetoothDevicesSlice).getConnectedBluetoothDevices();
|
||||||
|
|
||||||
|
final Slice slice = mBluetoothDevicesSlice.getSlice();
|
||||||
|
|
||||||
|
final SliceMetadata metadata = SliceMetadata.from(mContext, slice);
|
||||||
|
assertThat(metadata.getSubtitle()).isEqualTo(
|
||||||
|
mContext.getResources().getQuantityString(R.plurals.show_bluetooth_devices,
|
||||||
|
BluetoothDevicesSlice.DEFAULT_EXPANDED_ROW_COUNT,
|
||||||
|
BluetoothDevicesSlice.DEFAULT_EXPANDED_ROW_COUNT));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void onNotifyChange_mediaDevice_shouldActivateDevice() {
|
public void onNotifyChange_mediaDevice_shouldActivateDevice() {
|
||||||
mockBluetoothDeviceList();
|
mockBluetoothDeviceList(1);
|
||||||
doReturn(mBluetoothDeviceList).when(mBluetoothDevicesSlice).getConnectedBluetoothDevices();
|
doReturn(mBluetoothDeviceList).when(mBluetoothDevicesSlice).getConnectedBluetoothDevices();
|
||||||
final Intent intent = new Intent().putExtra(
|
final Intent intent = new Intent().putExtra(
|
||||||
BluetoothDevicesSlice.BLUETOOTH_DEVICE_HASH_CODE,
|
BluetoothDevicesSlice.BLUETOOTH_DEVICE_HASH_CODE,
|
||||||
@@ -163,12 +193,14 @@ public class BluetoothDevicesSliceTest {
|
|||||||
verify(mCachedBluetoothDevice).setActive();
|
verify(mCachedBluetoothDevice).setActive();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void mockBluetoothDeviceList() {
|
private void mockBluetoothDeviceList(int deviceCount) {
|
||||||
doReturn(BLUETOOTH_MOCK_TITLE).when(mCachedBluetoothDevice).getName();
|
doReturn(BLUETOOTH_MOCK_TITLE).when(mCachedBluetoothDevice).getName();
|
||||||
doReturn(BLUETOOTH_MOCK_SUMMARY).when(mCachedBluetoothDevice).getConnectionSummary();
|
doReturn(BLUETOOTH_MOCK_SUMMARY).when(mCachedBluetoothDevice).getConnectionSummary();
|
||||||
doReturn(BLUETOOTH_MOCK_ADDRESS).when(mCachedBluetoothDevice).getAddress();
|
doReturn(BLUETOOTH_MOCK_ADDRESS).when(mCachedBluetoothDevice).getAddress();
|
||||||
doReturn(true).when(mCachedBluetoothDevice).isConnectedA2dpDevice();
|
doReturn(true).when(mCachedBluetoothDevice).isConnectedA2dpDevice();
|
||||||
mBluetoothDeviceList.add(mCachedBluetoothDevice);
|
for (int i = 0; i < deviceCount; i++) {
|
||||||
|
mBluetoothDeviceList.add(mCachedBluetoothDevice);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean hasTitle(SliceMetadata metadata, String title) {
|
private boolean hasTitle(SliceMetadata metadata, String title) {
|
||||||
|
Reference in New Issue
Block a user