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

Commit ecea932a authored by Alex Shabalin's avatar Alex Shabalin
Browse files

Prefer the name from the route for Bluetooth devices.

Whenever available use the name from the MediaRoute2Info object because
`CachedBluetoothDevice#getName` results in an IPC call.

Bug: 414668703
Flag: com.android.media.flags.avoid_binder_calls_during_render
Test: atest SettingsLibRoboTests:com.android.settingslib.media.BluetoothMediaDeviceTest,
 on a device.
Change-Id: I15b872410cf72987d8f4938116a9a273776aa952
parent f992c647
Loading
Loading
Loading
Loading
+11 −0
Original line number Diff line number Diff line
@@ -15,6 +15,7 @@
 */
package com.android.settingslib.media;

import static com.android.media.flags.Flags.avoidBinderCallsDuringRender;
import static com.android.settingslib.media.MediaDevice.SelectionBehavior.SELECTION_BEHAVIOR_TRANSFER;

import android.annotation.NonNull;
@@ -56,6 +57,16 @@ public class BluetoothMediaDevice extends MediaDevice {

    @Override
    public String getName() {
        if (avoidBinderCallsDuringRender()) {
            if (mRouteInfo != null) {
                // Prefer name from route info since CachedBluetoothDevice#getName results in an
                // IPC call.
                return mRouteInfo.getName().toString();
            } else {
                return mCachedDevice.getName();
            }
        }

        return mCachedDevice.getName();
    }

+34 −0
Original line number Diff line number Diff line
@@ -25,10 +25,15 @@ import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothProfile;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.media.MediaRoute2Info;
import android.platform.test.annotations.EnableFlags;
import android.platform.test.flag.junit.SetFlagsRule;

import com.android.media.flags.Flags;
import com.android.settingslib.bluetooth.CachedBluetoothDevice;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
@@ -40,10 +45,18 @@ import org.robolectric.RuntimeEnvironment;
public class BluetoothMediaDeviceTest {

    private static final String TEST_ADDRESS = "11:22:33:44:55:66";
    private static final String TEST_ROUTE_NAME = "Name from route";
    private static final String TEST_CACHED_DEVICE_NAME = "Name from cached device";

    @Rule
    public final SetFlagsRule mSetFlagsRule = new SetFlagsRule();

    @Mock
    private CachedBluetoothDevice mDevice;

    @Mock
    private MediaRoute2Info mRouteInfo;

    private Context mContext;
    private BluetoothMediaDevice mBluetoothMediaDevice;

@@ -120,4 +133,25 @@ public class BluetoothMediaDeviceTest {
        when(mDevice.getAddress()).thenReturn(TEST_ADDRESS);
        assertThat(mBluetoothMediaDevice.getId()).isEqualTo(TEST_ADDRESS);
    }

    @EnableFlags(Flags.FLAG_AVOID_BINDER_CALLS_DURING_RENDER)
    @Test
    public void getName_hasRouteInfo_usesNameFromRoute() {
        when(mRouteInfo.getName()).thenReturn(TEST_ROUTE_NAME);
        when(mDevice.getName()).thenReturn(TEST_CACHED_DEVICE_NAME);
        MediaDevice bluetoothMediaDevice = new BluetoothMediaDevice(mContext, mDevice,
                mRouteInfo, /* dynamicRouteAttributes= */ null, null);

        assertThat(bluetoothMediaDevice.getName()).isEqualTo(TEST_ROUTE_NAME);
    }

    @EnableFlags(Flags.FLAG_AVOID_BINDER_CALLS_DURING_RENDER)
    @Test
    public void getName_noRouteInfo_usesNameFromCachedDevice() {
        when(mDevice.getName()).thenReturn(TEST_CACHED_DEVICE_NAME);

        MediaDevice bluetoothMediaDevice = new BluetoothMediaDevice(mContext, mDevice,
                null, /* dynamicRouteAttributes= */ null, null);
        assertThat(bluetoothMediaDevice.getName()).isEqualTo(TEST_CACHED_DEVICE_NAME);
    }
}