Update rule to display output switcher Slice

-Display when Cast device is available
-Add test case

Bug: 150907688
Test: make -j42 RunSettingsRoboTests
Change-Id: I1aa2fbe7b77a0274816af47bbc372eae9d7944c9
This commit is contained in:
Tim Peng
2020-03-06 11:22:08 +08:00
parent 5a4257deb4
commit 9c2968ab21
5 changed files with 151 additions and 195 deletions

View File

@@ -18,7 +18,6 @@ package com.android.settings.media;
import static android.media.AudioManager.STREAM_DEVICES_CHANGED_ACTION;
import android.bluetooth.BluetoothProfile;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
@@ -36,21 +35,33 @@ import androidx.annotation.Nullable;
import com.android.settings.bluetooth.Utils;
import com.android.settings.slices.SliceBackgroundWorker;
import com.android.settingslib.bluetooth.BluetoothCallback;
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
import com.android.settingslib.bluetooth.LocalBluetoothManager;
import com.android.settingslib.media.LocalMediaManager;
import com.android.settingslib.media.MediaDevice;
import com.google.common.annotations.VisibleForTesting;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
/**
* Listener for background change from {@code BluetoothCallback} to update media output indicator.
*/
public class MediaOutputIndicatorWorker extends SliceBackgroundWorker implements BluetoothCallback {
public class MediaOutputIndicatorWorker extends SliceBackgroundWorker implements BluetoothCallback,
LocalMediaManager.DeviceCallback {
private static final String TAG = "MediaOutputIndicatorWorker";
private static final String TAG = "MediaOutputIndWorker";
private final DevicesChangedBroadcastReceiver mReceiver;
private final Context mContext;
private final Collection<MediaDevice> mMediaDevices = new CopyOnWriteArrayList<>();
private LocalBluetoothManager mLocalBluetoothManager;
@VisibleForTesting
LocalMediaManager mLocalMediaManager;
public MediaOutputIndicatorWorker(Context context, Uri uri) {
super(context, uri);
mReceiver = new DevicesChangedBroadcastReceiver();
@@ -59,6 +70,7 @@ public class MediaOutputIndicatorWorker extends SliceBackgroundWorker implements
@Override
protected void onSlicePinned() {
mMediaDevices.clear();
mLocalBluetoothManager = Utils.getLocalBtManager(getContext());
if (mLocalBluetoothManager == null) {
Log.e(TAG, "Bluetooth is not supported on this device");
@@ -67,10 +79,25 @@ public class MediaOutputIndicatorWorker extends SliceBackgroundWorker implements
final IntentFilter intentFilter = new IntentFilter(STREAM_DEVICES_CHANGED_ACTION);
mContext.registerReceiver(mReceiver, intentFilter);
mLocalBluetoothManager.getEventManager().registerCallback(this);
if (mLocalMediaManager == null) {
final MediaController controller = getActiveLocalMediaController();
String packageName = null;
if (controller != null) {
packageName = controller.getPackageName();
}
mLocalMediaManager = new LocalMediaManager(mContext, packageName, null);
}
mLocalMediaManager.registerCallback(this);
mLocalMediaManager.startScan();
}
@Override
protected void onSliceUnpinned() {
mLocalMediaManager.unregisterCallback(this);
mLocalMediaManager.stopScan();
if (mLocalBluetoothManager == null) {
Log.e(TAG, "Bluetooth is not supported on this device");
return;
@@ -82,20 +109,7 @@ public class MediaOutputIndicatorWorker extends SliceBackgroundWorker implements
@Override
public void close() {
mLocalBluetoothManager = null;
}
@Override
public void onBluetoothStateChanged(int bluetoothState) {
// To handle the case that Bluetooth on and no connected devices
notifySliceChange();
}
@Override
public void onActiveDeviceChanged(CachedBluetoothDevice activeDevice, int bluetoothProfile) {
if (bluetoothProfile == BluetoothProfile.A2DP ||
bluetoothProfile == BluetoothProfile.HEARING_AID) {
notifySliceChange();
}
mLocalMediaManager = null;
}
@Override
@@ -124,6 +138,36 @@ public class MediaOutputIndicatorWorker extends SliceBackgroundWorker implements
}
return null;
}
@Override
public void onDeviceListUpdate(List<MediaDevice> devices) {
buildMediaDevices(devices);
notifySliceChange();
}
private void buildMediaDevices(List<MediaDevice> devices) {
mMediaDevices.clear();
mMediaDevices.addAll(devices);
}
@Override
public void onSelectedDeviceStateChanged(MediaDevice device, int state) {
notifySliceChange();
}
@Override
public void onDeviceAttributesChanged() {
notifySliceChange();
}
Collection<MediaDevice> getMediaDevices() {
return mMediaDevices;
}
MediaDevice getCurrentConnectedMediaDevice() {
return mLocalMediaManager.getCurrentConnectedDevice();
}
private class DevicesChangedBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {