summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--packages/SettingsLib/src/com/android/settingslib/bluetooth/A2dpProfile.java1
-rw-r--r--packages/SettingsLib/src/com/android/settingslib/bluetooth/HearingAidProfile.java2
-rw-r--r--packages/SettingsLib/src/com/android/settingslib/media/BluetoothMediaDevice.java10
-rw-r--r--packages/SettingsLib/src/com/android/settingslib/media/BluetoothMediaManager.java81
-rw-r--r--packages/SettingsLib/src/com/android/settingslib/media/InfoMediaDevice.java15
-rw-r--r--packages/SettingsLib/src/com/android/settingslib/media/InfoMediaManager.java26
-rw-r--r--packages/SettingsLib/src/com/android/settingslib/media/LocalMediaManager.java91
-rw-r--r--packages/SettingsLib/src/com/android/settingslib/media/MediaDevice.java16
-rw-r--r--packages/SettingsLib/src/com/android/settingslib/media/MediaManager.java13
-rw-r--r--packages/SettingsLib/src/com/android/settingslib/media/PhoneMediaDevice.java12
-rw-r--r--packages/SettingsLib/tests/robotests/src/com/android/settingslib/media/MediaDeviceTest.java15
11 files changed, 195 insertions, 87 deletions
diff --git a/packages/SettingsLib/src/com/android/settingslib/bluetooth/A2dpProfile.java b/packages/SettingsLib/src/com/android/settingslib/bluetooth/A2dpProfile.java
index 24d7011d9a35..c05915685e83 100644
--- a/packages/SettingsLib/src/com/android/settingslib/bluetooth/A2dpProfile.java
+++ b/packages/SettingsLib/src/com/android/settingslib/bluetooth/A2dpProfile.java
@@ -74,6 +74,7 @@ public class A2dpProfile implements LocalBluetoothProfile {
device.refresh();
}
mIsProfileReady=true;
+ mProfileManager.callServiceConnectedListeners();
}
public void onServiceDisconnected(int profile) {
diff --git a/packages/SettingsLib/src/com/android/settingslib/bluetooth/HearingAidProfile.java b/packages/SettingsLib/src/com/android/settingslib/bluetooth/HearingAidProfile.java
index 577d98d93405..77dfbe994488 100644
--- a/packages/SettingsLib/src/com/android/settingslib/bluetooth/HearingAidProfile.java
+++ b/packages/SettingsLib/src/com/android/settingslib/bluetooth/HearingAidProfile.java
@@ -71,8 +71,8 @@ public class HearingAidProfile implements LocalBluetoothProfile {
// Check current list of CachedDevices to see if any are Hearing Aid devices.
mDeviceManager.updateHearingAidsDevices();
-
mIsProfileReady=true;
+ mProfileManager.callServiceConnectedListeners();
}
public void onServiceDisconnected(int profile) {
diff --git a/packages/SettingsLib/src/com/android/settingslib/media/BluetoothMediaDevice.java b/packages/SettingsLib/src/com/android/settingslib/media/BluetoothMediaDevice.java
index 959f9b24133c..a5c6f0c28e78 100644
--- a/packages/SettingsLib/src/com/android/settingslib/media/BluetoothMediaDevice.java
+++ b/packages/SettingsLib/src/com/android/settingslib/media/BluetoothMediaDevice.java
@@ -54,17 +54,17 @@ public class BluetoothMediaDevice extends MediaDevice {
}
@Override
- public void connect() {
+ public boolean connect() {
//TODO(b/117129183): add callback to notify LocalMediaManager connection state.
- mIsConnected = mCachedDevice.setActive();
- super.connect();
- Log.d(TAG, "connect() device : " + getName() + ", is selected : " + mIsConnected);
+ final boolean isConnected = mCachedDevice.setActive();
+ setConnectedRecord();
+ Log.d(TAG, "connect() device : " + getName() + ", is selected : " + isConnected);
+ return isConnected;
}
@Override
public void disconnect() {
//TODO(b/117129183): disconnected last select device
- mIsConnected = false;
}
/**
diff --git a/packages/SettingsLib/src/com/android/settingslib/media/BluetoothMediaManager.java b/packages/SettingsLib/src/com/android/settingslib/media/BluetoothMediaManager.java
index ab1cca0084c4..fa2dd887bd3d 100644
--- a/packages/SettingsLib/src/com/android/settingslib/media/BluetoothMediaManager.java
+++ b/packages/SettingsLib/src/com/android/settingslib/media/BluetoothMediaManager.java
@@ -18,6 +18,7 @@ package com.android.settingslib.media;
import android.app.Notification;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
+import android.bluetooth.BluetoothProfile;
import android.content.Context;
import android.util.Log;
@@ -35,36 +36,48 @@ import java.util.List;
/**
* BluetoothMediaManager provide interface to get Bluetooth device list.
*/
-public class BluetoothMediaManager extends MediaManager implements BluetoothCallback {
+public class BluetoothMediaManager extends MediaManager implements BluetoothCallback,
+ LocalBluetoothProfileManager.ServiceListener {
private static final String TAG = "BluetoothMediaManager";
- private final DeviceAttributeChangeCallback mCachedDeviceCallback =
- new DeviceAttributeChangeCallback();
-
private LocalBluetoothManager mLocalBluetoothManager;
private LocalBluetoothProfileManager mProfileManager;
+ private CachedBluetoothDeviceManager mCachedBluetoothDeviceManager;
private MediaDevice mLastAddedDevice;
private MediaDevice mLastRemovedDevice;
+ private boolean mIsA2dpProfileReady = false;
+ private boolean mIsHearingAidProfileReady = false;
+
BluetoothMediaManager(Context context, LocalBluetoothManager localBluetoothManager,
Notification notification) {
super(context, notification);
mLocalBluetoothManager = localBluetoothManager;
mProfileManager = mLocalBluetoothManager.getProfileManager();
+ mCachedBluetoothDeviceManager = mLocalBluetoothManager.getCachedDeviceManager();
}
@Override
public void startScan() {
- mMediaDevices.clear();
mLocalBluetoothManager.getEventManager().registerCallback(this);
buildBluetoothDeviceList();
dispatchDeviceListAdded();
+
+ // The profile may not ready when calling startScan().
+ // Device status are all disconnected since profiles are not ready to connected.
+ // In this case, we observe onServiceConnected() in LocalBluetoothProfileManager.
+ // When A2dpProfile or HearingAidProfile is connected will call buildBluetoothDeviceList()
+ // again to find the connected devices.
+ if (!mIsA2dpProfileReady || !mIsHearingAidProfileReady) {
+ mProfileManager.addServiceListener(this);
+ }
}
private void buildBluetoothDeviceList() {
+ mMediaDevices.clear();
addConnectedA2dpDevices();
addConnectedHearingAidDevices();
}
@@ -77,12 +90,10 @@ public class BluetoothMediaManager extends MediaManager implements BluetoothCall
}
final List<BluetoothDevice> devices = a2dpProfile.getConnectedDevices();
- final CachedBluetoothDeviceManager cachedBluetoothDeviceManager =
- mLocalBluetoothManager.getCachedDeviceManager();
for (BluetoothDevice device : devices) {
final CachedBluetoothDevice cachedDevice =
- cachedBluetoothDeviceManager.findDevice(device);
+ mCachedBluetoothDeviceManager.findDevice(device);
if (cachedDevice == null) {
Log.w(TAG, "Can't found CachedBluetoothDevice : " + device.getName());
@@ -96,6 +107,8 @@ public class BluetoothMediaManager extends MediaManager implements BluetoothCall
addMediaDevice(cachedDevice);
}
}
+
+ mIsA2dpProfileReady = a2dpProfile.isProfileReady();
}
private void addConnectedHearingAidDevices() {
@@ -107,12 +120,10 @@ public class BluetoothMediaManager extends MediaManager implements BluetoothCall
final List<Long> devicesHiSyncIds = new ArrayList<>();
final List<BluetoothDevice> devices = hapProfile.getConnectedDevices();
- final CachedBluetoothDeviceManager cachedBluetoothDeviceManager =
- mLocalBluetoothManager.getCachedDeviceManager();
for (BluetoothDevice device : devices) {
final CachedBluetoothDevice cachedDevice =
- cachedBluetoothDeviceManager.findDevice(device);
+ mCachedBluetoothDeviceManager.findDevice(device);
if (cachedDevice == null) {
Log.w(TAG, "Can't found CachedBluetoothDevice : " + device.getName());
@@ -130,13 +141,14 @@ public class BluetoothMediaManager extends MediaManager implements BluetoothCall
addMediaDevice(cachedDevice);
}
}
+
+ mIsHearingAidProfileReady = hapProfile.isProfileReady();
}
private void addMediaDevice(CachedBluetoothDevice cachedDevice) {
MediaDevice mediaDevice = findMediaDevice(MediaDeviceUtils.getId(cachedDevice));
if (mediaDevice == null) {
mediaDevice = new BluetoothMediaDevice(mContext, cachedDevice);
- cachedDevice.registerCallback(mCachedDeviceCallback);
mLastAddedDevice = mediaDevice;
mMediaDevices.add(mediaDevice);
}
@@ -145,16 +157,6 @@ public class BluetoothMediaManager extends MediaManager implements BluetoothCall
@Override
public void stopScan() {
mLocalBluetoothManager.getEventManager().unregisterCallback(this);
- unregisterCachedDeviceCallback();
- }
-
- private void unregisterCachedDeviceCallback() {
- for (MediaDevice device : mMediaDevices) {
- if (device instanceof BluetoothMediaDevice) {
- ((BluetoothMediaDevice) device).getCachedDevice()
- .unregisterCallback(mCachedDeviceCallback);
- }
- }
}
@Override
@@ -166,8 +168,6 @@ public class BluetoothMediaManager extends MediaManager implements BluetoothCall
final List<MediaDevice> removeDevicesList = new ArrayList<>();
for (MediaDevice device : mMediaDevices) {
if (device instanceof BluetoothMediaDevice) {
- ((BluetoothMediaDevice) device).getCachedDevice()
- .unregisterCallback(mCachedDeviceCallback);
removeDevicesList.add(device);
}
}
@@ -212,7 +212,6 @@ public class BluetoothMediaManager extends MediaManager implements BluetoothCall
private void removeMediaDevice(CachedBluetoothDevice cachedDevice) {
final MediaDevice mediaDevice = findMediaDevice(MediaDeviceUtils.getId(cachedDevice));
if (mediaDevice != null) {
- cachedDevice.unregisterCallback(mCachedDeviceCallback);
mLastRemovedDevice = mediaDevice;
mMediaDevices.remove(mediaDevice);
}
@@ -252,10 +251,34 @@ public class BluetoothMediaManager extends MediaManager implements BluetoothCall
dispatchDeviceRemoved(cachedDevice);
}
}
- class DeviceAttributeChangeCallback implements CachedBluetoothDevice.Callback {
- @Override
- public void onDeviceAttributesChanged() {
- dispatchDeviceAttributesChanged();
+
+ @Override
+ public void onActiveDeviceChanged(CachedBluetoothDevice activeDevice, int bluetoothProfile) {
+ Log.d(TAG, "onActiveDeviceChanged : device : "
+ + activeDevice + ", profile : " + bluetoothProfile);
+ if (BluetoothProfile.HEARING_AID == bluetoothProfile
+ || BluetoothProfile.A2DP == bluetoothProfile) {
+ final String id = activeDevice == null
+ ? PhoneMediaDevice.ID : MediaDeviceUtils.getId(activeDevice);
+ dispatchConnectedDeviceChanged(id);
+ }
+ }
+
+ @Override
+ public void onServiceConnected() {
+ if (!mIsA2dpProfileReady || !mIsHearingAidProfileReady) {
+ buildBluetoothDeviceList();
+ dispatchDeviceListAdded();
}
+
+ //Remove the listener once a2dpProfile and hearingAidProfile are ready.
+ if (mIsA2dpProfileReady && mIsHearingAidProfileReady) {
+ mProfileManager.removeServiceListener(this);
+ }
+ }
+
+ @Override
+ public void onServiceDisconnected() {
+
}
}
diff --git a/packages/SettingsLib/src/com/android/settingslib/media/InfoMediaDevice.java b/packages/SettingsLib/src/com/android/settingslib/media/InfoMediaDevice.java
index 21a81e03037a..04f70cc676d9 100644
--- a/packages/SettingsLib/src/com/android/settingslib/media/InfoMediaDevice.java
+++ b/packages/SettingsLib/src/com/android/settingslib/media/InfoMediaDevice.java
@@ -16,6 +16,7 @@
package com.android.settingslib.media;
import android.content.Context;
+import android.widget.Toast;
import androidx.mediarouter.media.MediaRouter;
@@ -43,7 +44,7 @@ public class InfoMediaDevice extends MediaDevice {
@Override
public int getIcon() {
- //TODO(b/117129183): This is not final icon for cast device, just for demo.
+ //TODO(b/121083246): This is not final icon for cast device, just for demo.
return R.drawable.ic_settings_print;
}
@@ -53,15 +54,15 @@ public class InfoMediaDevice extends MediaDevice {
}
@Override
- public void connect() {
- //TODO(b/117129183): use MediaController2 to transfer media
- mIsConnected = true;
- super.connect();
+ public boolean connect() {
+ //TODO(b/121083246): use SystemApi to transfer media
+ setConnectedRecord();
+ Toast.makeText(mContext, "This is cast device !", Toast.LENGTH_SHORT).show();
+ return false;
}
@Override
public void disconnect() {
- //TODO(b/117129183): disconnected last select device
- mIsConnected = false;
+ //TODO(b/121083246): disconnected last select device
}
}
diff --git a/packages/SettingsLib/src/com/android/settingslib/media/InfoMediaManager.java b/packages/SettingsLib/src/com/android/settingslib/media/InfoMediaManager.java
index 6907238082fc..bc8e2c35291d 100644
--- a/packages/SettingsLib/src/com/android/settingslib/media/InfoMediaManager.java
+++ b/packages/SettingsLib/src/com/android/settingslib/media/InfoMediaManager.java
@@ -22,6 +22,8 @@ import android.util.Log;
import androidx.mediarouter.media.MediaRouteSelector;
import androidx.mediarouter.media.MediaRouter;
+import com.android.internal.annotations.VisibleForTesting;
+
/**
* InfoMediaManager provide interface to get InfoMediaDevice list.
*/
@@ -29,9 +31,13 @@ public class InfoMediaManager extends MediaManager {
private static final String TAG = "InfoMediaManager";
- private final MediaRouterCallback mMediaRouterCallback = new MediaRouterCallback();
+ @VisibleForTesting
+ final MediaRouterCallback mMediaRouterCallback = new MediaRouterCallback();
+ @VisibleForTesting
+ MediaRouteSelector mSelector;
+ @VisibleForTesting
+ MediaRouter mMediaRouter;
- private MediaRouter mMediaRouter;
private String mPackageName;
InfoMediaManager(Context context, String packageName, Notification notification) {
@@ -39,24 +45,20 @@ public class InfoMediaManager extends MediaManager {
mMediaRouter = MediaRouter.getInstance(context);
mPackageName = packageName;
+ mSelector = new MediaRouteSelector.Builder()
+ .addControlCategory(getControlCategoryByPackageName(mPackageName))
+ .build();
}
@Override
public void startScan() {
mMediaDevices.clear();
- startScanCastDevice();
- }
-
- private void startScanCastDevice() {
- final MediaRouteSelector selector = new MediaRouteSelector.Builder()
- .addControlCategory(getControlCategoryByPackageName(mPackageName))
- .build();
-
- mMediaRouter.addCallback(selector, mMediaRouterCallback,
+ mMediaRouter.addCallback(mSelector, mMediaRouterCallback,
MediaRouter.CALLBACK_FLAG_REQUEST_DISCOVERY);
}
- private String getControlCategoryByPackageName(String packageName) {
+ @VisibleForTesting
+ String getControlCategoryByPackageName(String packageName) {
//TODO(b/117129183): Use package name to get ControlCategory.
//Since api not ready, return fixed ControlCategory for prototype.
return "com.google.android.gms.cast.CATEGORY_CAST/4F8B3483";
diff --git a/packages/SettingsLib/src/com/android/settingslib/media/LocalMediaManager.java b/packages/SettingsLib/src/com/android/settingslib/media/LocalMediaManager.java
index c9479d44f791..44d945a9ee0e 100644
--- a/packages/SettingsLib/src/com/android/settingslib/media/LocalMediaManager.java
+++ b/packages/SettingsLib/src/com/android/settingslib/media/LocalMediaManager.java
@@ -16,12 +16,15 @@
package com.android.settingslib.media;
import android.app.Notification;
+import android.bluetooth.BluetoothProfile;
import android.content.Context;
import android.util.Log;
import androidx.annotation.IntDef;
+import com.android.internal.annotations.VisibleForTesting;
import com.android.settingslib.bluetooth.BluetoothCallback;
+import com.android.settingslib.bluetooth.CachedBluetoothDevice;
import com.android.settingslib.bluetooth.LocalBluetoothManager;
import java.lang.annotation.Retention;
@@ -50,16 +53,20 @@ public class LocalMediaManager implements BluetoothCallback {
}
private final Collection<DeviceCallback> mCallbacks = new ArrayList<>();
- private final MediaDeviceCallback mMediaDeviceCallback = new MediaDeviceCallback();
+ @VisibleForTesting
+ final MediaDeviceCallback mMediaDeviceCallback = new MediaDeviceCallback();
private Context mContext;
- private List<MediaDevice> mMediaDevices = new ArrayList<>();
private BluetoothMediaManager mBluetoothMediaManager;
private InfoMediaManager mInfoMediaManager;
-
private LocalBluetoothManager mLocalBluetoothManager;
- private MediaDevice mLastConnectedDevice;
- private MediaDevice mPhoneDevice;
+
+ @VisibleForTesting
+ List<MediaDevice> mMediaDevices = new ArrayList<>();
+ @VisibleForTesting
+ MediaDevice mPhoneDevice;
+ @VisibleForTesting
+ MediaDevice mCurrentConnectedDevice;
/**
* Register to start receiving callbacks for MediaDevice events.
@@ -93,28 +100,40 @@ public class LocalMediaManager implements BluetoothCallback {
mInfoMediaManager = new InfoMediaManager(context, packageName, notification);
}
+ @VisibleForTesting
+ LocalMediaManager(Context context, LocalBluetoothManager localBluetoothManager,
+ BluetoothMediaManager bluetoothMediaManager, InfoMediaManager infoMediaManager) {
+ mContext = context;
+ mLocalBluetoothManager = localBluetoothManager;
+ mBluetoothMediaManager = bluetoothMediaManager;
+ mInfoMediaManager = infoMediaManager;
+ }
+
/**
* Connect the MediaDevice to transfer media
* @param connectDevice the MediaDevice
*/
public void connectDevice(MediaDevice connectDevice) {
- if (connectDevice == mLastConnectedDevice) {
+ final MediaDevice device = getMediaDeviceById(mMediaDevices, connectDevice.getId());
+ if (device == mCurrentConnectedDevice) {
+ Log.d(TAG, "connectDevice() this device all ready connected! : " + device.getName());
return;
}
- if (mLastConnectedDevice != null) {
- mLastConnectedDevice.disconnect();
+ //TODO(b/121083246): Update it once remote media API is ready.
+ if (mCurrentConnectedDevice != null && !(connectDevice instanceof InfoMediaDevice)) {
+ mCurrentConnectedDevice.disconnect();
}
- connectDevice.connect();
- if (connectDevice.isConnected()) {
- mLastConnectedDevice = connectDevice;
+ final boolean isConnected = device.connect();
+ if (isConnected) {
+ mCurrentConnectedDevice = device;
}
- final int state = connectDevice.isConnected()
+ final int state = isConnected
? MediaDeviceState.STATE_CONNECTED
: MediaDeviceState.STATE_DISCONNECTED;
- dispatchSelectedDeviceStateChanged(connectDevice, state);
+ dispatchSelectedDeviceStateChanged(device, state);
}
void dispatchSelectedDeviceStateChanged(MediaDevice device, @MediaDeviceState int state) {
@@ -189,6 +208,31 @@ public class LocalMediaManager implements BluetoothCallback {
return null;
}
+ /**
+ * Find the current connected MediaDevice.
+ *
+ * @return MediaDevice
+ */
+ public MediaDevice getCurrentConnectedDevice() {
+ return mCurrentConnectedDevice;
+ }
+
+ private MediaDevice updateCurrentConnectedDevice() {
+ for (MediaDevice device : mMediaDevices) {
+ if (device instanceof BluetoothMediaDevice) {
+ if (isConnected(((BluetoothMediaDevice) device).getCachedDevice())) {
+ return device;
+ }
+ }
+ }
+ return mMediaDevices.contains(mPhoneDevice) ? mPhoneDevice : null;
+ }
+
+ private boolean isConnected(CachedBluetoothDevice device) {
+ return device.isActiveDevice(BluetoothProfile.A2DP)
+ || device.isActiveDevice(BluetoothProfile.HEARING_AID);
+ }
+
class MediaDeviceCallback implements MediaManager.MediaDeviceCallback {
@Override
public void onDeviceAdded(MediaDevice device) {
@@ -201,8 +245,13 @@ public class LocalMediaManager implements BluetoothCallback {
@Override
public void onDeviceListAdded(List<MediaDevice> devices) {
- mMediaDevices.addAll(devices);
+ for (MediaDevice device : devices) {
+ if (getMediaDeviceById(mMediaDevices, device.getId()) == null) {
+ mMediaDevices.add(device);
+ }
+ }
addPhoneDeviceIfNecessary();
+ mCurrentConnectedDevice = updateCurrentConnectedDevice();
dispatchDeviceListUpdate();
}
@@ -226,6 +275,20 @@ public class LocalMediaManager implements BluetoothCallback {
public void onDeviceAttributesChanged() {
dispatchDeviceListUpdate();
}
+
+ @Override
+ public void onConnectedDeviceChanged(String id) {
+ final MediaDevice connectDevice = getMediaDeviceById(mMediaDevices, id);
+
+ if (connectDevice == mCurrentConnectedDevice) {
+ Log.d(TAG, "onConnectedDeviceChanged() this device all ready connected! : "
+ + connectDevice.getName());
+ return;
+ }
+ mCurrentConnectedDevice = connectDevice;
+
+ dispatchDeviceListUpdate();
+ }
}
diff --git a/packages/SettingsLib/src/com/android/settingslib/media/MediaDevice.java b/packages/SettingsLib/src/com/android/settingslib/media/MediaDevice.java
index 33b621cb28da..f35c30edd16a 100644
--- a/packages/SettingsLib/src/com/android/settingslib/media/MediaDevice.java
+++ b/packages/SettingsLib/src/com/android/settingslib/media/MediaDevice.java
@@ -41,7 +41,6 @@ public abstract class MediaDevice implements Comparable<MediaDevice> {
private int mConnectedRecord;
- protected boolean mIsConnected = false;
protected Context mContext;
protected int mType;
@@ -57,15 +56,6 @@ public abstract class MediaDevice implements Comparable<MediaDevice> {
}
/**
- * Check the MediaDevice is be connected to transfer.
- *
- * @return true if the MediaDevice is be connected to transfer, false otherwise.
- */
- public boolean isConnected() {
- return mIsConnected;
- }
-
- /**
* Get name from MediaDevice.
*
* @return name of MediaDevice.
@@ -87,8 +77,12 @@ public abstract class MediaDevice implements Comparable<MediaDevice> {
/**
* Transfer MediaDevice for media
+ *
+ * @return result of transfer media
*/
- public void connect() {
+ public abstract boolean connect();
+
+ void setConnectedRecord() {
mConnectedRecord++;
ConnectionRecordManager.getInstance().setConnectionRecord(mContext, getId(),
mConnectedRecord);
diff --git a/packages/SettingsLib/src/com/android/settingslib/media/MediaManager.java b/packages/SettingsLib/src/com/android/settingslib/media/MediaManager.java
index 72b6b09d637e..2c3a96cafa56 100644
--- a/packages/SettingsLib/src/com/android/settingslib/media/MediaManager.java
+++ b/packages/SettingsLib/src/com/android/settingslib/media/MediaManager.java
@@ -96,7 +96,7 @@ public abstract class MediaManager {
protected void dispatchDeviceListAdded() {
synchronized (mCallbacks) {
for (MediaDeviceCallback callback : mCallbacks) {
- callback.onDeviceListAdded(mMediaDevices);
+ callback.onDeviceListAdded(new ArrayList<>(mMediaDevices));
}
}
}
@@ -109,10 +109,10 @@ public abstract class MediaManager {
}
}
- protected void dispatchDeviceAttributesChanged() {
+ protected void dispatchConnectedDeviceChanged(String id) {
synchronized (mCallbacks) {
for (MediaDeviceCallback callback : mCallbacks) {
- callback.onDeviceAttributesChanged();
+ callback.onConnectedDeviceChanged(id);
}
}
}
@@ -153,5 +153,12 @@ public abstract class MediaManager {
* Callback for notifying MediaDevice attributes is changed.
*/
void onDeviceAttributesChanged();
+
+ /**
+ * Callback for notifying connected MediaDevice is changed.
+ *
+ * @param id the id of MediaDevice
+ */
+ void onConnectedDeviceChanged(String id);
}
}
diff --git a/packages/SettingsLib/src/com/android/settingslib/media/PhoneMediaDevice.java b/packages/SettingsLib/src/com/android/settingslib/media/PhoneMediaDevice.java
index e0f3c2ff7692..c808214dbcf4 100644
--- a/packages/SettingsLib/src/com/android/settingslib/media/PhoneMediaDevice.java
+++ b/packages/SettingsLib/src/com/android/settingslib/media/PhoneMediaDevice.java
@@ -62,20 +62,22 @@ public class PhoneMediaDevice extends MediaDevice {
}
@Override
- public void connect() {
+ public boolean connect() {
final HearingAidProfile hapProfile = mProfileManager.getHearingAidProfile();
final A2dpProfile a2dpProfile = mProfileManager.getA2dpProfile();
+ boolean isConnected = false;
+
if (hapProfile != null && a2dpProfile != null) {
- mIsConnected = hapProfile.setActiveDevice(null) && a2dpProfile.setActiveDevice(null);
- super.connect();
+ isConnected = hapProfile.setActiveDevice(null) && a2dpProfile.setActiveDevice(null);
+ setConnectedRecord();
}
- Log.d(TAG, "connect() device : " + getName() + ", is selected : " + mIsConnected);
+ Log.d(TAG, "connect() device : " + getName() + ", is selected : " + isConnected);
+ return isConnected;
}
@Override
public void disconnect() {
//TODO(b/117129183): disconnected last select device
- mIsConnected = false;
}
}
diff --git a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/media/MediaDeviceTest.java b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/media/MediaDeviceTest.java
index 9bbdd0102fad..fc514f0ac25c 100644
--- a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/media/MediaDeviceTest.java
+++ b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/media/MediaDeviceTest.java
@@ -25,8 +25,11 @@ import android.content.Context;
import androidx.mediarouter.media.MediaRouter;
+import com.android.settingslib.bluetooth.A2dpProfile;
import com.android.settingslib.bluetooth.CachedBluetoothDevice;
+import com.android.settingslib.bluetooth.HearingAidProfile;
import com.android.settingslib.bluetooth.LocalBluetoothManager;
+import com.android.settingslib.bluetooth.LocalBluetoothProfileManager;
import org.junit.Before;
import org.junit.Test;
@@ -78,6 +81,14 @@ public class MediaDeviceTest {
private MediaRouter.RouteInfo mRouteInfo2;
@Mock
private MediaRouter.RouteInfo mRouteInfo3;
+ @Mock
+ private LocalBluetoothProfileManager mProfileManager;
+ @Mock
+ private HearingAidProfile mHapProfile;
+ @Mock
+ private A2dpProfile mA2dpProfile;
+ @Mock
+ private BluetoothDevice mDevice;
private BluetoothMediaDevice mBluetoothMediaDevice1;
private BluetoothMediaDevice mBluetoothMediaDevice2;
@@ -109,6 +120,10 @@ public class MediaDeviceTest {
when(mRouteInfo1.getName()).thenReturn(DEVICE_NAME_1);
when(mRouteInfo2.getName()).thenReturn(DEVICE_NAME_2);
when(mRouteInfo3.getName()).thenReturn(DEVICE_NAME_3);
+ when(mLocalBluetoothManager.getProfileManager()).thenReturn(mProfileManager);
+ when(mProfileManager.getA2dpProfile()).thenReturn(mA2dpProfile);
+ when(mProfileManager.getHearingAidProfile()).thenReturn(mHapProfile);
+ when(mA2dpProfile.getActiveDevice()).thenReturn(mDevice);
mBluetoothMediaDevice1 = new BluetoothMediaDevice(mContext, mCachedDevice1);
mBluetoothMediaDevice2 = new BluetoothMediaDevice(mContext, mCachedDevice2);