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

Commit 7b0f8870 authored by Tim Peng's avatar Tim Peng Committed by tim peng
Browse files

Update "Play media to" display rule in SoundSetting

- show under Casting mode
- hide under no connected Bluetooth devices
- update test case

Bug: 131143025
Test: make -j42 RunSettingsRoboTests
Change-Id: I0eac856970043ecd9a1975fbe382241078ece924
parent 7a237a40
Loading
Loading
Loading
Loading
+2 −78
Original line number Diff line number Diff line
@@ -17,7 +17,6 @@
package com.android.settings.sound;

import static android.media.AudioManager.STREAM_DEVICES_CHANGED_ACTION;
import static android.media.MediaRouter.ROUTE_TYPE_REMOTE_DISPLAY;

import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
@@ -29,7 +28,6 @@ import android.media.AudioDeviceCallback;
import android.media.AudioDeviceInfo;
import android.media.AudioManager;
import android.media.MediaRouter;
import android.media.MediaRouter.Callback;
import android.os.Handler;
import android.os.Looper;
import android.util.FeatureFlagUtils;
@@ -76,7 +74,6 @@ public abstract class AudioSwitchPreferenceController extends BasePreferenceCont
    protected AudioSwitchCallback mAudioSwitchPreferenceCallback;

    private final AudioManagerAudioDeviceCallback mAudioManagerAudioDeviceCallback;
    private final MediaRouterCallback mMediaRouterCallback;
    private final WiredHeadsetBroadcastReceiver mReceiver;
    private final Handler mHandler;
    private LocalBluetoothManager mLocalBluetoothManager;
@@ -92,7 +89,6 @@ public abstract class AudioSwitchPreferenceController extends BasePreferenceCont
        mHandler = new Handler(Looper.getMainLooper());
        mAudioManagerAudioDeviceCallback = new AudioManagerAudioDeviceCallback();
        mReceiver = new WiredHeadsetBroadcastReceiver();
        mMediaRouterCallback = new MediaRouterCallback();
        mConnectedDevices = new ArrayList<>();
        final FutureTask<LocalBluetoothManager> localBtManagerFutureTask = new FutureTask<>(
                // Avoid StrictMode ThreadPolicy violation
@@ -210,12 +206,12 @@ public abstract class AudioSwitchPreferenceController extends BasePreferenceCont
     * get A2dp devices on all states
     * (STATE_DISCONNECTED, STATE_CONNECTING, STATE_CONNECTED,  STATE_DISCONNECTING)
     */
    protected List<BluetoothDevice> getConnectableA2dpDevices() {
    protected List<BluetoothDevice> getConnectedA2dpDevices() {
        final A2dpProfile a2dpProfile = mProfileManager.getA2dpProfile();
        if (a2dpProfile == null) {
            return new ArrayList<>();
        }
        return a2dpProfile.getConnectableDevices();
        return a2dpProfile.getConnectedDevices();
    }

    /**
@@ -241,31 +237,6 @@ public abstract class AudioSwitchPreferenceController extends BasePreferenceCont
        return connectedDevices;
    }

    /**
     * get hearing aid profile devices on all states
     * (STATE_DISCONNECTED, STATE_CONNECTING, STATE_CONNECTED,  STATE_DISCONNECTING)
     * exclude other devices with same hiSyncId.
     */
    protected List<BluetoothDevice> getConnectableHearingAidDevices() {
        final List<BluetoothDevice> connectedDevices = new ArrayList<>();
        final HearingAidProfile hapProfile = mProfileManager.getHearingAidProfile();
        if (hapProfile == null) {
            return connectedDevices;
        }
        final List<Long> devicesHiSyncIds = new ArrayList<>();
        final List<BluetoothDevice> devices = hapProfile.getConnectableDevices();
        for (BluetoothDevice device : devices) {
            final long hiSyncId = hapProfile.getHiSyncId(device);
            // device with same hiSyncId should not be shown in the UI.
            // So do not add it into connectedDevices.
            if (!devicesHiSyncIds.contains(hiSyncId)) {
                devicesHiSyncIds.add(hiSyncId);
                connectedDevices.add(device);
            }
        }
        return connectedDevices;
    }

    /**
     * Find active hearing aid device
     */
@@ -299,7 +270,6 @@ public abstract class AudioSwitchPreferenceController extends BasePreferenceCont
    private void register() {
        mLocalBluetoothManager.getEventManager().registerCallback(this);
        mAudioManager.registerAudioDeviceCallback(mAudioManagerAudioDeviceCallback, mHandler);
        mMediaRouter.addCallback(ROUTE_TYPE_REMOTE_DISPLAY, mMediaRouterCallback);

        // Register for misc other intent broadcasts.
        IntentFilter intentFilter = new IntentFilter(Intent.ACTION_HEADSET_PLUG);
@@ -310,7 +280,6 @@ public abstract class AudioSwitchPreferenceController extends BasePreferenceCont
    private void unregister() {
        mLocalBluetoothManager.getEventManager().unregisterCallback(this);
        mAudioManager.unregisterAudioDeviceCallback(mAudioManagerAudioDeviceCallback);
        mMediaRouter.removeCallback(mMediaRouterCallback);
        mContext.unregisterReceiver(mReceiver);
    }

@@ -338,49 +307,4 @@ public abstract class AudioSwitchPreferenceController extends BasePreferenceCont
            }
        }
    }

    /** Callback for cast device events. */
    private class MediaRouterCallback extends Callback {
        @Override
        public void onRouteSelected(MediaRouter router, int type, MediaRouter.RouteInfo info) {
        }

        @Override
        public void onRouteUnselected(MediaRouter router, int type, MediaRouter.RouteInfo info) {
        }

        @Override
        public void onRouteAdded(MediaRouter router, MediaRouter.RouteInfo info) {
            if (info != null && !info.isDefault()) {
                // cast mode
                updateState(mPreference);
            }
        }

        @Override
        public void onRouteRemoved(MediaRouter router, MediaRouter.RouteInfo info) {
        }

        @Override
        public void onRouteChanged(MediaRouter router, MediaRouter.RouteInfo info) {
            if (info != null && !info.isDefault()) {
                // cast mode
                updateState(mPreference);
            }
        }

        @Override
        public void onRouteGrouped(MediaRouter router, MediaRouter.RouteInfo info,
                MediaRouter.RouteGroup group, int index) {
        }

        @Override
        public void onRouteUngrouped(MediaRouter router, MediaRouter.RouteInfo info,
                MediaRouter.RouteGroup group) {
        }

        @Override
        public void onRouteVolumeChanged(MediaRouter router, MediaRouter.RouteInfo info) {
        }
    }
}
+7 −17
Original line number Diff line number Diff line
@@ -16,9 +16,6 @@

package com.android.settings.sound;

import static android.media.AudioManager.STREAM_MUSIC;
import static android.media.AudioSystem.DEVICE_OUT_REMOTE_SUBMIX;

import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.content.Intent;
@@ -56,13 +53,6 @@ public class MediaOutputPreferenceController extends AudioSwitchPreferenceContro
            return;
        }

        if (isStreamFromOutputDevice(STREAM_MUSIC, DEVICE_OUT_REMOTE_SUBMIX)) {
            // In cast mode, disable switch entry.
            mPreference.setVisible(false);
            preference.setSummary(mContext.getText(R.string.media_output_summary_unavailable));
            return;
        }

        if (Utils.isAudioModeOngoingCall(mContext)) {
            // Ongoing call status, switch entry for media will be disabled.
            mPreference.setVisible(false);
@@ -71,19 +61,19 @@ public class MediaOutputPreferenceController extends AudioSwitchPreferenceContro
            return;
        }

        boolean deviceConnectable = false;
        boolean deviceConnected = false;
        BluetoothDevice activeDevice = null;
        // Show preference if there is connected or previously connected device
        // Find active device and set its name as the preference's summary
        List<BluetoothDevice> connectableA2dpDevices = getConnectableA2dpDevices();
        List<BluetoothDevice> connectableHADevices = getConnectableHearingAidDevices();
        List<BluetoothDevice> connectedA2dpDevices = getConnectedA2dpDevices();
        List<BluetoothDevice> connectedHADevices = getConnectedHearingAidDevices();
        if (mAudioManager.getMode() == AudioManager.MODE_NORMAL
                && ((connectableA2dpDevices != null && !connectableA2dpDevices.isEmpty())
                || (connectableHADevices != null && !connectableHADevices.isEmpty()))) {
            deviceConnectable = true;
                && ((connectedA2dpDevices != null && !connectedA2dpDevices.isEmpty())
                || (connectedHADevices != null && !connectedHADevices.isEmpty()))) {
            deviceConnected = true;
            activeDevice = findActiveDevice();
        }
        mPreference.setVisible(deviceConnectable);
        mPreference.setVisible(deviceConnected);
        mPreference.setSummary((activeDevice == null) ?
                mContext.getText(R.string.media_output_default_summary) :
                activeDevice.getAliasName());
+24 −40
Original line number Diff line number Diff line
@@ -109,7 +109,7 @@ public class MediaOutputPreferenceControllerTest {
    private BluetoothDevice mRightBluetoothHapDevice;
    private LocalBluetoothManager mLocalBluetoothManager;
    private MediaOutputPreferenceController mController;
    private List<BluetoothDevice> mProfileConnectableDevices;
    private List<BluetoothDevice> mProfileConnectedDevices;
    private List<BluetoothDevice> mHearingAidActiveDevices;

    @Before
@@ -150,7 +150,7 @@ public class MediaOutputPreferenceControllerTest {
        mController = new MediaOutputPreferenceController(mContext, TEST_KEY);
        mScreen = spy(new PreferenceScreen(mContext, null));
        mPreference = new Preference(mContext);
        mProfileConnectableDevices = new ArrayList<>();
        mProfileConnectedDevices = new ArrayList<>();
        mHearingAidActiveDevices = new ArrayList<>(2);

        when(mScreen.getPreferenceManager()).thenReturn(mock(PreferenceManager.class));
@@ -172,11 +172,11 @@ public class MediaOutputPreferenceControllerTest {
     * Preference should be invisible
     */
    @Test
    public void updateState_withoutConnectableBtDevice_preferenceInvisible() {
    public void updateState_withoutConnectedBtDevice_preferenceInvisible() {
        mShadowAudioManager.setOutputDevice(DEVICE_OUT_EARPIECE);
        mAudioManager.setMode(AudioManager.MODE_NORMAL);
        mProfileConnectableDevices.clear();
        when(mA2dpProfile.getConnectableDevices()).thenReturn(mProfileConnectableDevices);
        mProfileConnectedDevices.clear();
        when(mA2dpProfile.getConnectedDevices()).thenReturn(mProfileConnectedDevices);
        mPreference.setVisible(true);

        assertThat(mPreference.isVisible()).isTrue();
@@ -185,16 +185,16 @@ public class MediaOutputPreferenceControllerTest {
    }

    /**
     * A2DP Bluetooth device(s) are connectable, no matter active or inactive
     * A2DP Bluetooth device(s) are connected, no matter active or inactive
     * Preference should be visible
     */
    @Test
    public void updateState_withConnectableBtDevice_preferenceVisible() {
    public void updateState_withConnectedBtDevice_preferenceVisible() {
        mShadowAudioManager.setOutputDevice(DEVICE_OUT_BLUETOOTH_A2DP);
        mAudioManager.setMode(AudioManager.MODE_NORMAL);
        mProfileConnectableDevices.clear();
        mProfileConnectableDevices.add(mBluetoothDevice);
        when(mA2dpProfile.getConnectableDevices()).thenReturn(mProfileConnectableDevices);
        mProfileConnectedDevices.clear();
        mProfileConnectedDevices.add(mBluetoothDevice);
        when(mA2dpProfile.getConnectedDevices()).thenReturn(mProfileConnectedDevices);
        assertThat(mPreference.isVisible()).isFalse();

        // Without Active Bluetooth Device
@@ -208,17 +208,17 @@ public class MediaOutputPreferenceControllerTest {
    }

    /**
     * A2DP Bluetooth device(s) are connectable, but no device is set as activated
     * A2DP Bluetooth device(s) are connected, but no device is set as activated
     * Preference summary should be "This device"
     */
    @Test
    public void updateState_withConnectableBtDevice_withoutActiveBtDevice_setDefaultSummary() {
    public void updateState_withConnectedBtDevice_withoutActiveBtDevice_setDefaultSummary() {
        mShadowAudioManager.setOutputDevice(DEVICE_OUT_EARPIECE);
        mAudioManager.setMode(AudioManager.MODE_NORMAL);
        mProfileConnectableDevices.clear();
        mProfileConnectableDevices.add(mBluetoothDevice);
        mProfileConnectableDevices.add(mSecondBluetoothDevice);
        when(mA2dpProfile.getConnectableDevices()).thenReturn(mProfileConnectableDevices);
        mProfileConnectedDevices.clear();
        mProfileConnectedDevices.add(mBluetoothDevice);
        mProfileConnectedDevices.add(mSecondBluetoothDevice);
        when(mA2dpProfile.getConnectedDevices()).thenReturn(mProfileConnectedDevices);
        when(mA2dpProfile.getActiveDevice()).thenReturn(null);

        assertThat(mPreference.getSummary()).isNull();
@@ -235,10 +235,10 @@ public class MediaOutputPreferenceControllerTest {
    public void updateState_withActiveBtDevice_setActivatedDeviceName() {
        mShadowAudioManager.setOutputDevice(DEVICE_OUT_BLUETOOTH_A2DP);
        mAudioManager.setMode(AudioManager.MODE_NORMAL);
        mProfileConnectableDevices.clear();
        mProfileConnectableDevices.add(mBluetoothDevice);
        mProfileConnectableDevices.add(mSecondBluetoothDevice);
        when(mA2dpProfile.getConnectableDevices()).thenReturn(mProfileConnectableDevices);
        mProfileConnectedDevices.clear();
        mProfileConnectedDevices.add(mBluetoothDevice);
        mProfileConnectedDevices.add(mSecondBluetoothDevice);
        when(mA2dpProfile.getConnectedDevices()).thenReturn(mProfileConnectedDevices);
        when(mA2dpProfile.getActiveDevice()).thenReturn(mBluetoothDevice);

        assertThat(mPreference.getSummary()).isNull();
@@ -248,16 +248,16 @@ public class MediaOutputPreferenceControllerTest {


    /**
     * Hearing Aid device(s) are connectable, no matter active or inactive
     * Hearing Aid device(s) are connected, no matter active or inactive
     * Preference should be visible
     */
    @Test
    public void updateState_withConnectableHADevice_preferenceVisible() {
    public void updateState_withConnectedHADevice_preferenceVisible() {
        mShadowAudioManager.setOutputDevice(DEVICE_OUT_HEARING_AID);
        mAudioManager.setMode(AudioManager.MODE_NORMAL);
        mHearingAidActiveDevices.clear();
        mHearingAidActiveDevices.add(mLeftBluetoothHapDevice);
        when(mHearingAidProfile.getConnectableDevices()).thenReturn(mHearingAidActiveDevices);
        when(mHearingAidProfile.getConnectedDevices()).thenReturn(mHearingAidActiveDevices);
        assertThat(mPreference.isVisible()).isFalse();

        // Without Active Hearing Aid Device
@@ -280,7 +280,7 @@ public class MediaOutputPreferenceControllerTest {
        mAudioManager.setMode(AudioManager.MODE_NORMAL);
        mHearingAidActiveDevices.clear();
        mHearingAidActiveDevices.add(mLeftBluetoothHapDevice);
        when(mHearingAidProfile.getConnectableDevices()).thenReturn(mHearingAidActiveDevices);
        when(mHearingAidProfile.getConnectedDevices()).thenReturn(mHearingAidActiveDevices);
        when(mHearingAidProfile.getActiveDevices()).thenReturn(mHearingAidActiveDevices);

        assertThat(mPreference.getSummary()).isNull();
@@ -332,22 +332,6 @@ public class MediaOutputPreferenceControllerTest {
                mContext.getText(R.string.media_out_summary_ongoing_call_state));
    }

    /**
     * Media stream is captured by something else (cast device):
     * Preference should be invisible
     * Preference summary should be "unavailable"
     */
    @Test
    public void updateState_mediaStreamIsCapturedByCast_shouldDisableAndSetDefaultSummary() {
        mShadowAudioManager.setOutputDevice(DEVICE_OUT_REMOTE_SUBMIX);

        mController.updateState(mPreference);

        assertThat(mPreference.isVisible()).isFalse();
        String defaultString = mContext.getString(R.string.media_output_summary_unavailable);
        assertThat(mPreference.getSummary()).isEqualTo(defaultString);
    }

    @Test
    public void findActiveDevice_onlyA2dpDeviceActive_returnA2dpDevice() {
        when(mLocalBluetoothProfileManager.getHearingAidProfile()).thenReturn(null);