Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 6b17d510 authored by Yanting Yang's avatar Yanting Yang Committed by Android (Google) Code Review
Browse files

Merge "Limit displayable rows in BluetoothDevicesSlice"

parents 2beb5442 0fc817bd
Loading
Loading
Loading
Loading
+16 −12
Original line number Diff line number Diff line
@@ -63,6 +63,13 @@ public class BluetoothDevicesSlice implements CustomSliceable {
    @VisibleForTesting
    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
     * Bluetooth devices by {@link CachedBluetoothDevice}.
@@ -70,12 +77,6 @@ public class BluetoothDevicesSlice implements CustomSliceable {
    private static final Comparator<CachedBluetoothDevice> COMPARATOR
            = 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 final Context mContext;
@@ -127,15 +128,18 @@ public class BluetoothDevicesSlice implements CustomSliceable {
                    .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()
                .setTitle(title)
                .setSubtitle(getSubTitle(rows.size()))
                .setSubtitle(getSubTitle(deviceCount))
                .setPrimaryAction(primarySliceAction));

        // Add Bluetooth device rows.
        for (ListBuilder.RowBuilder rowBuilder : rows) {
            listBuilder.addRow(rowBuilder);
        // According to the displayable device count to add bluetooth device rows.
        for (int i = 0; i < deviceCount; i++) {
            listBuilder.addRow(rows.get(i));
        }

        // Add "Pair new device" if need.
@@ -238,7 +242,7 @@ public class BluetoothDevicesSlice implements CustomSliceable {
    }

    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<CachedBluetoothDevice> bluetoothDevices = getConnectedBluetoothDevices();
        for (CachedBluetoothDevice bluetoothDevice : bluetoothDevices) {
+38 −6
Original line number Diff line number Diff line
@@ -16,6 +16,9 @@

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 org.mockito.ArgumentMatchers.any;
@@ -34,6 +37,7 @@ import androidx.slice.Slice;
import androidx.slice.SliceItem;
import androidx.slice.SliceMetadata;
import androidx.slice.SliceProvider;
import androidx.slice.core.SliceQuery;
import androidx.slice.widget.SliceLiveData;

import com.android.settings.R;
@@ -97,7 +101,7 @@ public class BluetoothDevicesSliceTest {

    @Test
    public void getSlice_hasBluetoothDevices_shouldHaveBluetoothDevicesTitle() {
        mockBluetoothDeviceList();
        mockBluetoothDeviceList(1);
        doReturn(mBluetoothDeviceList).when(mBluetoothDevicesSlice).getConnectedBluetoothDevices();

        final Slice slice = mBluetoothDevicesSlice.getSlice();
@@ -108,7 +112,7 @@ public class BluetoothDevicesSliceTest {

    @Test
    public void getSlice_hasBluetoothDevices_shouldMatchBluetoothMockTitle() {
        mockBluetoothDeviceList();
        mockBluetoothDeviceList(1);
        doReturn(mBluetoothDeviceList).when(mBluetoothDevicesSlice).getConnectedBluetoothDevices();

        final Slice slice = mBluetoothDevicesSlice.getSlice();
@@ -119,7 +123,7 @@ public class BluetoothDevicesSliceTest {

    @Test
    public void getSlice_hasBluetoothDevices_shouldHavePairNewDevice() {
        mockBluetoothDeviceList();
        mockBluetoothDeviceList(1);
        doReturn(mBluetoothDeviceList).when(mBluetoothDevicesSlice).getConnectedBluetoothDevices();

        final Slice slice = mBluetoothDevicesSlice.getSlice();
@@ -150,9 +154,35 @@ public class BluetoothDevicesSliceTest {
                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
    public void onNotifyChange_mediaDevice_shouldActivateDevice() {
        mockBluetoothDeviceList();
        mockBluetoothDeviceList(1);
        doReturn(mBluetoothDeviceList).when(mBluetoothDevicesSlice).getConnectedBluetoothDevices();
        final Intent intent = new Intent().putExtra(
                BluetoothDevicesSlice.BLUETOOTH_DEVICE_HASH_CODE,
@@ -163,13 +193,15 @@ public class BluetoothDevicesSliceTest {
        verify(mCachedBluetoothDevice).setActive();
    }

    private void mockBluetoothDeviceList() {
    private void mockBluetoothDeviceList(int deviceCount) {
        doReturn(BLUETOOTH_MOCK_TITLE).when(mCachedBluetoothDevice).getName();
        doReturn(BLUETOOTH_MOCK_SUMMARY).when(mCachedBluetoothDevice).getConnectionSummary();
        doReturn(BLUETOOTH_MOCK_ADDRESS).when(mCachedBluetoothDevice).getAddress();
        doReturn(true).when(mCachedBluetoothDevice).isConnectedA2dpDevice();
        for (int i = 0; i < deviceCount; i++) {
            mBluetoothDeviceList.add(mCachedBluetoothDevice);
        }
    }

    private boolean hasTitle(SliceMetadata metadata, String title) {
        final CharSequence sliceTitle = metadata.getTitle();