hughchen | a73686f | 2018-11-12 17:42:08 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package com.android.settingslib.media; |
| 17 | |
| 18 | import android.app.Notification; |
hughchen | b3d68f6 | 2020-02-11 14:30:14 +0800 | [diff] [blame] | 19 | import android.bluetooth.BluetoothAdapter; |
| 20 | import android.bluetooth.BluetoothDevice; |
hughchen | bd0dd49 | 2019-01-08 14:34:10 +0800 | [diff] [blame] | 21 | import android.bluetooth.BluetoothProfile; |
hughchen | a73686f | 2018-11-12 17:42:08 +0800 | [diff] [blame] | 22 | import android.content.Context; |
Tim Peng | 8a0f361 | 2020-04-30 12:33:20 +0800 | [diff] [blame] | 23 | import android.media.RoutingSessionInfo; |
hughchen | df7b838 | 2020-02-03 17:48:49 +0800 | [diff] [blame] | 24 | import android.text.TextUtils; |
hughchen | a73686f | 2018-11-12 17:42:08 +0800 | [diff] [blame] | 25 | import android.util.Log; |
| 26 | |
| 27 | import androidx.annotation.IntDef; |
hughchen | 5bd3968 | 2020-05-08 10:56:30 +0800 | [diff] [blame] | 28 | import androidx.annotation.Nullable; |
hughchen | a73686f | 2018-11-12 17:42:08 +0800 | [diff] [blame] | 29 | |
hughchen | bd0dd49 | 2019-01-08 14:34:10 +0800 | [diff] [blame] | 30 | import com.android.internal.annotations.VisibleForTesting; |
hughchen | 81efaa5 | 2020-05-06 15:08:29 +0800 | [diff] [blame] | 31 | import com.android.settingslib.bluetooth.A2dpProfile; |
hughchen | a73686f | 2018-11-12 17:42:08 +0800 | [diff] [blame] | 32 | import com.android.settingslib.bluetooth.BluetoothCallback; |
hughchen | bd0dd49 | 2019-01-08 14:34:10 +0800 | [diff] [blame] | 33 | import com.android.settingslib.bluetooth.CachedBluetoothDevice; |
hughchen | b3d68f6 | 2020-02-11 14:30:14 +0800 | [diff] [blame] | 34 | import com.android.settingslib.bluetooth.CachedBluetoothDeviceManager; |
hughchen | 81efaa5 | 2020-05-06 15:08:29 +0800 | [diff] [blame] | 35 | import com.android.settingslib.bluetooth.HearingAidProfile; |
hughchen | a73686f | 2018-11-12 17:42:08 +0800 | [diff] [blame] | 36 | import com.android.settingslib.bluetooth.LocalBluetoothManager; |
hughchen | 81efaa5 | 2020-05-06 15:08:29 +0800 | [diff] [blame] | 37 | import com.android.settingslib.bluetooth.LocalBluetoothProfile; |
hughchen | a73686f | 2018-11-12 17:42:08 +0800 | [diff] [blame] | 38 | |
| 39 | import java.lang.annotation.Retention; |
| 40 | import java.lang.annotation.RetentionPolicy; |
| 41 | import java.util.ArrayList; |
| 42 | import java.util.Collection; |
hughchen | 31901c0 | 2020-04-22 19:31:28 +0800 | [diff] [blame] | 43 | import java.util.Collections; |
timhypeng | a8b826c | 2018-11-14 15:33:53 +0800 | [diff] [blame] | 44 | import java.util.Comparator; |
hughchen | a73686f | 2018-11-12 17:42:08 +0800 | [diff] [blame] | 45 | import java.util.List; |
hughchen | 8175bf2 | 2020-02-12 18:52:34 +0800 | [diff] [blame] | 46 | import java.util.concurrent.CopyOnWriteArrayList; |
hughchen | a73686f | 2018-11-12 17:42:08 +0800 | [diff] [blame] | 47 | |
| 48 | /** |
| 49 | * LocalMediaManager provide interface to get MediaDevice list and transfer media to MediaDevice. |
| 50 | */ |
| 51 | public class LocalMediaManager implements BluetoothCallback { |
timhypeng | a8b826c | 2018-11-14 15:33:53 +0800 | [diff] [blame] | 52 | private static final Comparator<MediaDevice> COMPARATOR = Comparator.naturalOrder(); |
hughchen | a73686f | 2018-11-12 17:42:08 +0800 | [diff] [blame] | 53 | private static final String TAG = "LocalMediaManager"; |
hughchen | 11264e2 | 2020-03-30 14:11:18 +0800 | [diff] [blame] | 54 | private static final int MAX_DISCONNECTED_DEVICE_NUM = 5; |
hughchen | a73686f | 2018-11-12 17:42:08 +0800 | [diff] [blame] | 55 | |
| 56 | @Retention(RetentionPolicy.SOURCE) |
| 57 | @IntDef({MediaDeviceState.STATE_CONNECTED, |
| 58 | MediaDeviceState.STATE_CONNECTING, |
Tim Peng | dbdfaef | 2020-03-11 11:30:01 +0800 | [diff] [blame] | 59 | MediaDeviceState.STATE_DISCONNECTED, |
| 60 | MediaDeviceState.STATE_CONNECTING_FAILED}) |
hughchen | a73686f | 2018-11-12 17:42:08 +0800 | [diff] [blame] | 61 | public @interface MediaDeviceState { |
Tim Peng | dbdfaef | 2020-03-11 11:30:01 +0800 | [diff] [blame] | 62 | int STATE_CONNECTED = 0; |
| 63 | int STATE_CONNECTING = 1; |
| 64 | int STATE_DISCONNECTED = 2; |
| 65 | int STATE_CONNECTING_FAILED = 3; |
hughchen | a73686f | 2018-11-12 17:42:08 +0800 | [diff] [blame] | 66 | } |
| 67 | |
hughchen | 8175bf2 | 2020-02-12 18:52:34 +0800 | [diff] [blame] | 68 | private final Collection<DeviceCallback> mCallbacks = new CopyOnWriteArrayList<>(); |
hughchen | 5bd3968 | 2020-05-08 10:56:30 +0800 | [diff] [blame] | 69 | private final Object mMediaDevicesLock = new Object(); |
hughchen | bd0dd49 | 2019-01-08 14:34:10 +0800 | [diff] [blame] | 70 | @VisibleForTesting |
| 71 | final MediaDeviceCallback mMediaDeviceCallback = new MediaDeviceCallback(); |
hughchen | a73686f | 2018-11-12 17:42:08 +0800 | [diff] [blame] | 72 | |
| 73 | private Context mContext; |
hughchen | a73686f | 2018-11-12 17:42:08 +0800 | [diff] [blame] | 74 | private LocalBluetoothManager mLocalBluetoothManager; |
hughchen | e6a4f48 | 2019-10-18 17:34:15 +0800 | [diff] [blame] | 75 | private InfoMediaManager mInfoMediaManager; |
| 76 | private String mPackageName; |
Tim Peng | 48c2367 | 2020-03-24 12:53:16 +0800 | [diff] [blame] | 77 | private MediaDevice mOnTransferBluetoothDevice; |
hughchen | bd0dd49 | 2019-01-08 14:34:10 +0800 | [diff] [blame] | 78 | |
| 79 | @VisibleForTesting |
hughchen | 37282f0 | 2020-03-25 18:12:52 +0800 | [diff] [blame] | 80 | List<MediaDevice> mMediaDevices = new CopyOnWriteArrayList<>(); |
hughchen | bd0dd49 | 2019-01-08 14:34:10 +0800 | [diff] [blame] | 81 | @VisibleForTesting |
hughchen | 37282f0 | 2020-03-25 18:12:52 +0800 | [diff] [blame] | 82 | List<MediaDevice> mDisconnectedMediaDevices = new CopyOnWriteArrayList<>(); |
hughchen | b3d68f6 | 2020-02-11 14:30:14 +0800 | [diff] [blame] | 83 | @VisibleForTesting |
hughchen | bd0dd49 | 2019-01-08 14:34:10 +0800 | [diff] [blame] | 84 | MediaDevice mPhoneDevice; |
| 85 | @VisibleForTesting |
| 86 | MediaDevice mCurrentConnectedDevice; |
hughchen | b3d68f6 | 2020-02-11 14:30:14 +0800 | [diff] [blame] | 87 | @VisibleForTesting |
| 88 | DeviceAttributeChangeCallback mDeviceAttributeChangeCallback = |
| 89 | new DeviceAttributeChangeCallback(); |
| 90 | @VisibleForTesting |
| 91 | BluetoothAdapter mBluetoothAdapter; |
hughchen | a73686f | 2018-11-12 17:42:08 +0800 | [diff] [blame] | 92 | |
| 93 | /** |
| 94 | * Register to start receiving callbacks for MediaDevice events. |
| 95 | */ |
| 96 | public void registerCallback(DeviceCallback callback) { |
hughchen | 8175bf2 | 2020-02-12 18:52:34 +0800 | [diff] [blame] | 97 | mCallbacks.add(callback); |
hughchen | a73686f | 2018-11-12 17:42:08 +0800 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Unregister to stop receiving callbacks for MediaDevice events |
| 102 | */ |
| 103 | public void unregisterCallback(DeviceCallback callback) { |
hughchen | 8175bf2 | 2020-02-12 18:52:34 +0800 | [diff] [blame] | 104 | mCallbacks.remove(callback); |
hughchen | a73686f | 2018-11-12 17:42:08 +0800 | [diff] [blame] | 105 | } |
| 106 | |
Fabian Kozynski | e731634 | 2020-03-16 13:31:06 -0400 | [diff] [blame] | 107 | /** |
| 108 | * Creates a LocalMediaManager with references to given managers. |
| 109 | * |
| 110 | * It will obtain a {@link LocalBluetoothManager} by calling |
| 111 | * {@link LocalBluetoothManager#getInstance} and create an {@link InfoMediaManager} passing |
| 112 | * that bluetooth manager. |
| 113 | * |
| 114 | * It will use {@link BluetoothAdapter#getDefaultAdapter()] for setting the bluetooth adapter. |
| 115 | */ |
hughchen | a73686f | 2018-11-12 17:42:08 +0800 | [diff] [blame] | 116 | public LocalMediaManager(Context context, String packageName, Notification notification) { |
| 117 | mContext = context; |
hughchen | e6a4f48 | 2019-10-18 17:34:15 +0800 | [diff] [blame] | 118 | mPackageName = packageName; |
hughchen | a73686f | 2018-11-12 17:42:08 +0800 | [diff] [blame] | 119 | mLocalBluetoothManager = |
| 120 | LocalBluetoothManager.getInstance(context, /* onInitCallback= */ null); |
hughchen | b3d68f6 | 2020-02-11 14:30:14 +0800 | [diff] [blame] | 121 | mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); |
hughchen | a73686f | 2018-11-12 17:42:08 +0800 | [diff] [blame] | 122 | if (mLocalBluetoothManager == null) { |
| 123 | Log.e(TAG, "Bluetooth is not supported on this device"); |
| 124 | return; |
| 125 | } |
| 126 | |
hughchen | 491b9fc | 2020-02-05 18:31:36 +0800 | [diff] [blame] | 127 | mInfoMediaManager = |
| 128 | new InfoMediaManager(context, packageName, notification, mLocalBluetoothManager); |
hughchen | a73686f | 2018-11-12 17:42:08 +0800 | [diff] [blame] | 129 | } |
| 130 | |
Fabian Kozynski | e731634 | 2020-03-16 13:31:06 -0400 | [diff] [blame] | 131 | /** |
| 132 | * Creates a LocalMediaManager with references to given managers. |
| 133 | * |
| 134 | * It will use {@link BluetoothAdapter#getDefaultAdapter()] for setting the bluetooth adapter. |
| 135 | */ |
Fabian Kozynski | cc4c6c3 | 2020-03-09 15:11:33 -0400 | [diff] [blame] | 136 | public LocalMediaManager(Context context, LocalBluetoothManager localBluetoothManager, |
hughchen | df7b838 | 2020-02-03 17:48:49 +0800 | [diff] [blame] | 137 | InfoMediaManager infoMediaManager, String packageName) { |
hughchen | bd0dd49 | 2019-01-08 14:34:10 +0800 | [diff] [blame] | 138 | mContext = context; |
| 139 | mLocalBluetoothManager = localBluetoothManager; |
hughchen | e6a4f48 | 2019-10-18 17:34:15 +0800 | [diff] [blame] | 140 | mInfoMediaManager = infoMediaManager; |
hughchen | df7b838 | 2020-02-03 17:48:49 +0800 | [diff] [blame] | 141 | mPackageName = packageName; |
Fabian Kozynski | e731634 | 2020-03-16 13:31:06 -0400 | [diff] [blame] | 142 | mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); |
hughchen | bd0dd49 | 2019-01-08 14:34:10 +0800 | [diff] [blame] | 143 | } |
| 144 | |
hughchen | a73686f | 2018-11-12 17:42:08 +0800 | [diff] [blame] | 145 | /** |
| 146 | * Connect the MediaDevice to transfer media |
| 147 | * @param connectDevice the MediaDevice |
| 148 | */ |
| 149 | public void connectDevice(MediaDevice connectDevice) { |
hughchen | 5bd3968 | 2020-05-08 10:56:30 +0800 | [diff] [blame] | 150 | MediaDevice device = null; |
| 151 | synchronized (mMediaDevicesLock) { |
| 152 | device = getMediaDeviceById(mMediaDevices, connectDevice.getId()); |
| 153 | } |
| 154 | if (device == null) { |
| 155 | Log.w(TAG, "connectDevice() connectDevice not in the list!"); |
| 156 | return; |
| 157 | } |
hughchen | c0c1163 | 2019-03-07 16:21:17 +0800 | [diff] [blame] | 158 | if (device instanceof BluetoothMediaDevice) { |
| 159 | final CachedBluetoothDevice cachedDevice = |
| 160 | ((BluetoothMediaDevice) device).getCachedDevice(); |
| 161 | if (!cachedDevice.isConnected() && !cachedDevice.isBusy()) { |
Tim Peng | 48c2367 | 2020-03-24 12:53:16 +0800 | [diff] [blame] | 162 | mOnTransferBluetoothDevice = connectDevice; |
Tim Peng | 92fb219 | 2020-03-31 11:34:20 +0800 | [diff] [blame] | 163 | device.setState(MediaDeviceState.STATE_CONNECTING); |
hughchen | 4ae2186 | 2020-01-14 15:55:29 +0800 | [diff] [blame] | 164 | cachedDevice.connect(); |
hughchen | c0c1163 | 2019-03-07 16:21:17 +0800 | [diff] [blame] | 165 | return; |
| 166 | } |
| 167 | } |
| 168 | |
hughchen | bd0dd49 | 2019-01-08 14:34:10 +0800 | [diff] [blame] | 169 | if (device == mCurrentConnectedDevice) { |
| 170 | Log.d(TAG, "connectDevice() this device all ready connected! : " + device.getName()); |
hughchen | a73686f | 2018-11-12 17:42:08 +0800 | [diff] [blame] | 171 | return; |
| 172 | } |
| 173 | |
hughchen | e6a4f48 | 2019-10-18 17:34:15 +0800 | [diff] [blame] | 174 | if (mCurrentConnectedDevice != null) { |
hughchen | bd0dd49 | 2019-01-08 14:34:10 +0800 | [diff] [blame] | 175 | mCurrentConnectedDevice.disconnect(); |
hughchen | a73686f | 2018-11-12 17:42:08 +0800 | [diff] [blame] | 176 | } |
| 177 | |
Tim Peng | dbdfaef | 2020-03-11 11:30:01 +0800 | [diff] [blame] | 178 | device.setState(MediaDeviceState.STATE_CONNECTING); |
hughchen | df7b838 | 2020-02-03 17:48:49 +0800 | [diff] [blame] | 179 | if (TextUtils.isEmpty(mPackageName)) { |
hughchen | 063a35a | 2020-03-04 17:51:57 +0800 | [diff] [blame] | 180 | mInfoMediaManager.connectDeviceWithoutPackageName(device); |
hughchen | df7b838 | 2020-02-03 17:48:49 +0800 | [diff] [blame] | 181 | } else { |
hughchen | 063a35a | 2020-03-04 17:51:57 +0800 | [diff] [blame] | 182 | device.connect(); |
hughchen | df7b838 | 2020-02-03 17:48:49 +0800 | [diff] [blame] | 183 | } |
hughchen | a73686f | 2018-11-12 17:42:08 +0800 | [diff] [blame] | 184 | } |
| 185 | |
timhypeng | 97c6cf6 | 2018-12-27 15:40:32 +0800 | [diff] [blame] | 186 | void dispatchSelectedDeviceStateChanged(MediaDevice device, @MediaDeviceState int state) { |
hughchen | 8175bf2 | 2020-02-12 18:52:34 +0800 | [diff] [blame] | 187 | for (DeviceCallback callback : getCallbacks()) { |
| 188 | callback.onSelectedDeviceStateChanged(device, state); |
hughchen | a73686f | 2018-11-12 17:42:08 +0800 | [diff] [blame] | 189 | } |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * Start scan connected MediaDevice |
| 194 | */ |
| 195 | public void startScan() { |
hughchen | 5bd3968 | 2020-05-08 10:56:30 +0800 | [diff] [blame] | 196 | synchronized (mMediaDevicesLock) { |
| 197 | mMediaDevices.clear(); |
| 198 | } |
hughchen | 0d20204 | 2020-01-17 18:30:03 +0800 | [diff] [blame] | 199 | mInfoMediaManager.registerCallback(mMediaDeviceCallback); |
| 200 | mInfoMediaManager.startScan(); |
hughchen | a73686f | 2018-11-12 17:42:08 +0800 | [diff] [blame] | 201 | } |
| 202 | |
hughchen | a73686f | 2018-11-12 17:42:08 +0800 | [diff] [blame] | 203 | void dispatchDeviceListUpdate() { |
hughchen | 5bd3968 | 2020-05-08 10:56:30 +0800 | [diff] [blame] | 204 | final List<MediaDevice> mediaDevices = new ArrayList<>(mMediaDevices); |
| 205 | Collections.sort(mediaDevices, COMPARATOR); |
hughchen | 8175bf2 | 2020-02-12 18:52:34 +0800 | [diff] [blame] | 206 | for (DeviceCallback callback : getCallbacks()) { |
hughchen | 5bd3968 | 2020-05-08 10:56:30 +0800 | [diff] [blame] | 207 | callback.onDeviceListUpdate(mediaDevices); |
hughchen | a73686f | 2018-11-12 17:42:08 +0800 | [diff] [blame] | 208 | } |
| 209 | } |
| 210 | |
hughchen | e572b83 | 2020-01-08 18:07:18 +0800 | [diff] [blame] | 211 | void dispatchDeviceAttributesChanged() { |
hughchen | 8175bf2 | 2020-02-12 18:52:34 +0800 | [diff] [blame] | 212 | for (DeviceCallback callback : getCallbacks()) { |
| 213 | callback.onDeviceAttributesChanged(); |
hughchen | e572b83 | 2020-01-08 18:07:18 +0800 | [diff] [blame] | 214 | } |
| 215 | } |
| 216 | |
hughchen | 063a35a | 2020-03-04 17:51:57 +0800 | [diff] [blame] | 217 | void dispatchOnRequestFailed(int reason) { |
| 218 | for (DeviceCallback callback : getCallbacks()) { |
| 219 | callback.onRequestFailed(reason); |
| 220 | } |
| 221 | } |
| 222 | |
hughchen | a73686f | 2018-11-12 17:42:08 +0800 | [diff] [blame] | 223 | /** |
| 224 | * Stop scan MediaDevice |
| 225 | */ |
| 226 | public void stopScan() { |
hughchen | 0d20204 | 2020-01-17 18:30:03 +0800 | [diff] [blame] | 227 | mInfoMediaManager.unregisterCallback(mMediaDeviceCallback); |
| 228 | mInfoMediaManager.stopScan(); |
hughchen | 37282f0 | 2020-03-25 18:12:52 +0800 | [diff] [blame] | 229 | unRegisterDeviceAttributeChangeCallback(); |
hughchen | a73686f | 2018-11-12 17:42:08 +0800 | [diff] [blame] | 230 | } |
| 231 | |
hughchen | 0487263 | 2018-12-24 14:41:20 +0800 | [diff] [blame] | 232 | /** |
| 233 | * Find the MediaDevice through id. |
| 234 | * |
| 235 | * @param devices the list of MediaDevice |
| 236 | * @param id the unique id of MediaDevice |
| 237 | * @return MediaDevice |
| 238 | */ |
| 239 | public MediaDevice getMediaDeviceById(List<MediaDevice> devices, String id) { |
| 240 | for (MediaDevice mediaDevice : devices) { |
hughchen | 4e6e524 | 2020-04-06 11:42:33 +0800 | [diff] [blame] | 241 | if (TextUtils.equals(mediaDevice.getId(), id)) { |
hughchen | 0487263 | 2018-12-24 14:41:20 +0800 | [diff] [blame] | 242 | return mediaDevice; |
| 243 | } |
| 244 | } |
| 245 | Log.i(TAG, "getMediaDeviceById() can't found device"); |
| 246 | return null; |
| 247 | } |
| 248 | |
hughchen | bd0dd49 | 2019-01-08 14:34:10 +0800 | [diff] [blame] | 249 | /** |
timhypeng | d37d70b | 2020-02-05 13:52:37 +0800 | [diff] [blame] | 250 | * Find the MediaDevice from all media devices by id. |
| 251 | * |
| 252 | * @param id the unique id of MediaDevice |
| 253 | * @return MediaDevice |
| 254 | */ |
| 255 | public MediaDevice getMediaDeviceById(String id) { |
hughchen | 5bd3968 | 2020-05-08 10:56:30 +0800 | [diff] [blame] | 256 | synchronized (mMediaDevicesLock) { |
| 257 | for (MediaDevice mediaDevice : mMediaDevices) { |
| 258 | if (TextUtils.equals(mediaDevice.getId(), id)) { |
| 259 | return mediaDevice; |
| 260 | } |
timhypeng | d37d70b | 2020-02-05 13:52:37 +0800 | [diff] [blame] | 261 | } |
| 262 | } |
| 263 | Log.i(TAG, "Unable to find device " + id); |
| 264 | return null; |
| 265 | } |
| 266 | |
| 267 | /** |
hughchen | bd0dd49 | 2019-01-08 14:34:10 +0800 | [diff] [blame] | 268 | * Find the current connected MediaDevice. |
| 269 | * |
| 270 | * @return MediaDevice |
| 271 | */ |
hughchen | 5bd3968 | 2020-05-08 10:56:30 +0800 | [diff] [blame] | 272 | @Nullable |
hughchen | bd0dd49 | 2019-01-08 14:34:10 +0800 | [diff] [blame] | 273 | public MediaDevice getCurrentConnectedDevice() { |
| 274 | return mCurrentConnectedDevice; |
| 275 | } |
| 276 | |
timhypeng | a812e88 | 2020-02-04 17:15:48 +0800 | [diff] [blame] | 277 | /** |
hughchen | 6ab62ca | 2020-02-12 10:40:04 +0800 | [diff] [blame] | 278 | * Add a MediaDevice to let it play current media. |
| 279 | * |
| 280 | * @param device MediaDevice |
| 281 | * @return If add device successful return {@code true}, otherwise return {@code false} |
| 282 | */ |
| 283 | public boolean addDeviceToPlayMedia(MediaDevice device) { |
| 284 | return mInfoMediaManager.addDeviceToPlayMedia(device); |
| 285 | } |
| 286 | |
| 287 | /** |
| 288 | * Remove a {@code device} from current media. |
| 289 | * |
| 290 | * @param device MediaDevice |
| 291 | * @return If device stop successful return {@code true}, otherwise return {@code false} |
| 292 | */ |
| 293 | public boolean removeDeviceFromPlayMedia(MediaDevice device) { |
| 294 | return mInfoMediaManager.removeDeviceFromPlayMedia(device); |
| 295 | } |
| 296 | |
| 297 | /** |
| 298 | * Get the MediaDevice list that can be added to current media. |
| 299 | * |
| 300 | * @return list of MediaDevice |
| 301 | */ |
| 302 | public List<MediaDevice> getSelectableMediaDevice() { |
| 303 | return mInfoMediaManager.getSelectableMediaDevice(); |
| 304 | } |
| 305 | |
| 306 | /** |
hughchen | 61bced0 | 2020-02-20 18:46:32 +0800 | [diff] [blame] | 307 | * Release session to stop playing media on MediaDevice. |
| 308 | */ |
| 309 | public boolean releaseSession() { |
| 310 | return mInfoMediaManager.releaseSession(); |
| 311 | } |
| 312 | |
| 313 | /** |
timhypeng | 6bbd555 | 2020-02-17 14:53:45 +0800 | [diff] [blame] | 314 | * Get the MediaDevice list that has been selected to current media. |
| 315 | * |
| 316 | * @return list of MediaDevice |
| 317 | */ |
| 318 | public List<MediaDevice> getSelectedMediaDevice() { |
| 319 | return mInfoMediaManager.getSelectedMediaDevice(); |
| 320 | } |
| 321 | |
| 322 | /** |
hughchen | 6ab62ca | 2020-02-12 10:40:04 +0800 | [diff] [blame] | 323 | * Adjust the volume of session. |
| 324 | * |
Tim Peng | 8a0f361 | 2020-04-30 12:33:20 +0800 | [diff] [blame] | 325 | * @param sessionId the value of media session id |
| 326 | * @param volume the value of volume |
| 327 | */ |
| 328 | public void adjustSessionVolume(String sessionId, int volume) { |
| 329 | final List<RoutingSessionInfo> infos = getActiveMediaSession(); |
| 330 | for (RoutingSessionInfo info : infos) { |
| 331 | if (TextUtils.equals(sessionId, info.getId())) { |
| 332 | mInfoMediaManager.adjustSessionVolume(info, volume); |
| 333 | return; |
| 334 | } |
| 335 | } |
| 336 | Log.w(TAG, "adjustSessionVolume: Unable to find session: " + sessionId); |
| 337 | } |
| 338 | |
| 339 | /** |
| 340 | * Adjust the volume of session. |
| 341 | * |
hughchen | 6ab62ca | 2020-02-12 10:40:04 +0800 | [diff] [blame] | 342 | * @param volume the value of volume |
| 343 | */ |
| 344 | public void adjustSessionVolume(int volume) { |
| 345 | mInfoMediaManager.adjustSessionVolume(volume); |
| 346 | } |
| 347 | |
| 348 | /** |
| 349 | * Gets the maximum volume of the {@link android.media.RoutingSessionInfo}. |
| 350 | * |
| 351 | * @return maximum volume of the session, and return -1 if not found. |
| 352 | */ |
| 353 | public int getSessionVolumeMax() { |
| 354 | return mInfoMediaManager.getSessionVolumeMax(); |
| 355 | } |
| 356 | |
| 357 | /** |
| 358 | * Gets the current volume of the {@link android.media.RoutingSessionInfo}. |
| 359 | * |
| 360 | * @return current volume of the session, and return -1 if not found. |
| 361 | */ |
| 362 | public int getSessionVolume() { |
| 363 | return mInfoMediaManager.getSessionVolume(); |
| 364 | } |
| 365 | |
Tim Peng | 635b04a | 2020-03-05 13:08:02 +0800 | [diff] [blame] | 366 | /** |
| 367 | * Gets the user-visible name of the {@link android.media.RoutingSessionInfo}. |
| 368 | * |
| 369 | * @return current name of the session, and return {@code null} if not found. |
| 370 | */ |
| 371 | public CharSequence getSessionName() { |
| 372 | return mInfoMediaManager.getSessionName(); |
| 373 | } |
| 374 | |
Tim Peng | 8a0f361 | 2020-04-30 12:33:20 +0800 | [diff] [blame] | 375 | /** |
| 376 | * Gets the current active session. |
| 377 | * |
| 378 | * @return current active session list{@link android.media.RoutingSessionInfo} |
| 379 | */ |
| 380 | public List<RoutingSessionInfo> getActiveMediaSession() { |
| 381 | return mInfoMediaManager.getActiveMediaSession(); |
| 382 | } |
| 383 | |
hughchen | bd0dd49 | 2019-01-08 14:34:10 +0800 | [diff] [blame] | 384 | private MediaDevice updateCurrentConnectedDevice() { |
hughchen | 5bd3968 | 2020-05-08 10:56:30 +0800 | [diff] [blame] | 385 | synchronized (mMediaDevicesLock) { |
| 386 | for (MediaDevice device : mMediaDevices) { |
| 387 | if (device instanceof BluetoothMediaDevice) { |
| 388 | if (isActiveDevice(((BluetoothMediaDevice) device).getCachedDevice())) { |
| 389 | return device; |
| 390 | } |
| 391 | } else if (device instanceof PhoneMediaDevice) { |
hughchen | bd0dd49 | 2019-01-08 14:34:10 +0800 | [diff] [blame] | 392 | return device; |
| 393 | } |
| 394 | } |
| 395 | } |
hughchen | 5bd3968 | 2020-05-08 10:56:30 +0800 | [diff] [blame] | 396 | Log.w(TAG, "updateCurrentConnectedDevice() can't found current connected device"); |
| 397 | return null; |
hughchen | bd0dd49 | 2019-01-08 14:34:10 +0800 | [diff] [blame] | 398 | } |
| 399 | |
hughchen | 063a35a | 2020-03-04 17:51:57 +0800 | [diff] [blame] | 400 | private boolean isActiveDevice(CachedBluetoothDevice device) { |
hughchen | bd0dd49 | 2019-01-08 14:34:10 +0800 | [diff] [blame] | 401 | return device.isActiveDevice(BluetoothProfile.A2DP) |
| 402 | || device.isActiveDevice(BluetoothProfile.HEARING_AID); |
| 403 | } |
| 404 | |
hughchen | 8175bf2 | 2020-02-12 18:52:34 +0800 | [diff] [blame] | 405 | private Collection<DeviceCallback> getCallbacks() { |
| 406 | return new CopyOnWriteArrayList<>(mCallbacks); |
| 407 | } |
| 408 | |
hughchen | a73686f | 2018-11-12 17:42:08 +0800 | [diff] [blame] | 409 | class MediaDeviceCallback implements MediaManager.MediaDeviceCallback { |
| 410 | @Override |
| 411 | public void onDeviceAdded(MediaDevice device) { |
hughchen | 5bd3968 | 2020-05-08 10:56:30 +0800 | [diff] [blame] | 412 | boolean isAdded = false; |
| 413 | synchronized (mMediaDevicesLock) { |
| 414 | if (!mMediaDevices.contains(device)) { |
| 415 | mMediaDevices.add(device); |
| 416 | isAdded = true; |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | if (isAdded) { |
hughchen | a73686f | 2018-11-12 17:42:08 +0800 | [diff] [blame] | 421 | dispatchDeviceListUpdate(); |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | @Override |
| 426 | public void onDeviceListAdded(List<MediaDevice> devices) { |
hughchen | 5bd3968 | 2020-05-08 10:56:30 +0800 | [diff] [blame] | 427 | synchronized (mMediaDevicesLock) { |
| 428 | mMediaDevices.clear(); |
| 429 | mMediaDevices.addAll(devices); |
| 430 | mMediaDevices.addAll(buildDisconnectedBluetoothDevice()); |
| 431 | } |
hughchen | b3d68f6 | 2020-02-11 14:30:14 +0800 | [diff] [blame] | 432 | |
hughchen | e6a4f48 | 2019-10-18 17:34:15 +0800 | [diff] [blame] | 433 | final MediaDevice infoMediaDevice = mInfoMediaManager.getCurrentConnectedDevice(); |
| 434 | mCurrentConnectedDevice = infoMediaDevice != null |
| 435 | ? infoMediaDevice : updateCurrentConnectedDevice(); |
hughchen | a73686f | 2018-11-12 17:42:08 +0800 | [diff] [blame] | 436 | dispatchDeviceListUpdate(); |
Tim Peng | 48c2367 | 2020-03-24 12:53:16 +0800 | [diff] [blame] | 437 | if (mOnTransferBluetoothDevice != null && mOnTransferBluetoothDevice.isConnected()) { |
| 438 | connectDevice(mOnTransferBluetoothDevice); |
Tim Peng | 92fb219 | 2020-03-31 11:34:20 +0800 | [diff] [blame] | 439 | mOnTransferBluetoothDevice.setState(MediaDeviceState.STATE_CONNECTED); |
Tim Peng | 48c2367 | 2020-03-24 12:53:16 +0800 | [diff] [blame] | 440 | mOnTransferBluetoothDevice = null; |
| 441 | } |
hughchen | a73686f | 2018-11-12 17:42:08 +0800 | [diff] [blame] | 442 | } |
| 443 | |
hughchen | b3d68f6 | 2020-02-11 14:30:14 +0800 | [diff] [blame] | 444 | private List<MediaDevice> buildDisconnectedBluetoothDevice() { |
hughchen | be51400 | 2020-04-14 11:50:55 +0800 | [diff] [blame] | 445 | if (mBluetoothAdapter == null) { |
| 446 | Log.w(TAG, "buildDisconnectedBluetoothDevice() BluetoothAdapter is null"); |
| 447 | return new ArrayList<>(); |
| 448 | } |
| 449 | |
hughchen | b3d68f6 | 2020-02-11 14:30:14 +0800 | [diff] [blame] | 450 | final List<BluetoothDevice> bluetoothDevices = |
| 451 | mBluetoothAdapter.getMostRecentlyConnectedDevices(); |
| 452 | final CachedBluetoothDeviceManager cachedDeviceManager = |
| 453 | mLocalBluetoothManager.getCachedDeviceManager(); |
| 454 | |
hughchen | 37282f0 | 2020-03-25 18:12:52 +0800 | [diff] [blame] | 455 | final List<CachedBluetoothDevice> cachedBluetoothDeviceList = new ArrayList<>(); |
hughchen | 11264e2 | 2020-03-30 14:11:18 +0800 | [diff] [blame] | 456 | int deviceCount = 0; |
hughchen | b3d68f6 | 2020-02-11 14:30:14 +0800 | [diff] [blame] | 457 | for (BluetoothDevice device : bluetoothDevices) { |
| 458 | final CachedBluetoothDevice cachedDevice = |
| 459 | cachedDeviceManager.findDevice(device); |
| 460 | if (cachedDevice != null) { |
| 461 | if (cachedDevice.getBondState() == BluetoothDevice.BOND_BONDED |
hughchen | 81efaa5 | 2020-05-06 15:08:29 +0800 | [diff] [blame] | 462 | && !cachedDevice.isConnected() |
| 463 | && isA2dpOrHearingAidDevice(cachedDevice)) { |
hughchen | 11264e2 | 2020-03-30 14:11:18 +0800 | [diff] [blame] | 464 | deviceCount++; |
hughchen | 37282f0 | 2020-03-25 18:12:52 +0800 | [diff] [blame] | 465 | cachedBluetoothDeviceList.add(cachedDevice); |
hughchen | 11264e2 | 2020-03-30 14:11:18 +0800 | [diff] [blame] | 466 | if (deviceCount >= MAX_DISCONNECTED_DEVICE_NUM) { |
| 467 | break; |
| 468 | } |
hughchen | b3d68f6 | 2020-02-11 14:30:14 +0800 | [diff] [blame] | 469 | } |
| 470 | } |
| 471 | } |
hughchen | 37282f0 | 2020-03-25 18:12:52 +0800 | [diff] [blame] | 472 | |
| 473 | unRegisterDeviceAttributeChangeCallback(); |
| 474 | mDisconnectedMediaDevices.clear(); |
| 475 | for (CachedBluetoothDevice cachedDevice : cachedBluetoothDeviceList) { |
| 476 | final MediaDevice mediaDevice = new BluetoothMediaDevice(mContext, |
| 477 | cachedDevice, |
| 478 | null, null, mPackageName); |
| 479 | if (!mMediaDevices.contains(mediaDevice)) { |
| 480 | cachedDevice.registerCallback(mDeviceAttributeChangeCallback); |
| 481 | mDisconnectedMediaDevices.add(mediaDevice); |
| 482 | } |
| 483 | } |
hughchen | b3d68f6 | 2020-02-11 14:30:14 +0800 | [diff] [blame] | 484 | return new ArrayList<>(mDisconnectedMediaDevices); |
| 485 | } |
| 486 | |
hughchen | 81efaa5 | 2020-05-06 15:08:29 +0800 | [diff] [blame] | 487 | private boolean isA2dpOrHearingAidDevice(CachedBluetoothDevice device) { |
| 488 | for (LocalBluetoothProfile profile : device.getConnectableProfiles()) { |
| 489 | if (profile instanceof A2dpProfile || profile instanceof HearingAidProfile) { |
| 490 | return true; |
| 491 | } |
| 492 | } |
| 493 | return false; |
| 494 | } |
| 495 | |
hughchen | a73686f | 2018-11-12 17:42:08 +0800 | [diff] [blame] | 496 | @Override |
| 497 | public void onDeviceRemoved(MediaDevice device) { |
hughchen | 5bd3968 | 2020-05-08 10:56:30 +0800 | [diff] [blame] | 498 | boolean isRemoved = false; |
| 499 | synchronized (mMediaDevicesLock) { |
| 500 | if (mMediaDevices.contains(device)) { |
| 501 | mMediaDevices.remove(device); |
| 502 | isRemoved = true; |
| 503 | } |
| 504 | } |
| 505 | if (isRemoved) { |
hughchen | a73686f | 2018-11-12 17:42:08 +0800 | [diff] [blame] | 506 | dispatchDeviceListUpdate(); |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | @Override |
| 511 | public void onDeviceListRemoved(List<MediaDevice> devices) { |
hughchen | 5bd3968 | 2020-05-08 10:56:30 +0800 | [diff] [blame] | 512 | synchronized (mMediaDevicesLock) { |
| 513 | mMediaDevices.removeAll(devices); |
| 514 | } |
hughchen | a73686f | 2018-11-12 17:42:08 +0800 | [diff] [blame] | 515 | dispatchDeviceListUpdate(); |
| 516 | } |
| 517 | |
| 518 | @Override |
hughchen | bd0dd49 | 2019-01-08 14:34:10 +0800 | [diff] [blame] | 519 | public void onConnectedDeviceChanged(String id) { |
hughchen | 5bd3968 | 2020-05-08 10:56:30 +0800 | [diff] [blame] | 520 | MediaDevice connectDevice = null; |
| 521 | synchronized (mMediaDevicesLock) { |
| 522 | connectDevice = getMediaDeviceById(mMediaDevices, id); |
| 523 | } |
hughchen | 063a35a | 2020-03-04 17:51:57 +0800 | [diff] [blame] | 524 | connectDevice = connectDevice != null |
| 525 | ? connectDevice : updateCurrentConnectedDevice(); |
hughchen | 9710435 | 2020-05-04 13:51:06 +0800 | [diff] [blame] | 526 | |
hughchen | bd0dd49 | 2019-01-08 14:34:10 +0800 | [diff] [blame] | 527 | mCurrentConnectedDevice = connectDevice; |
hughchen | 5bd3968 | 2020-05-08 10:56:30 +0800 | [diff] [blame] | 528 | if (connectDevice != null) { |
| 529 | connectDevice.setState(MediaDeviceState.STATE_CONNECTED); |
| 530 | |
| 531 | dispatchSelectedDeviceStateChanged(mCurrentConnectedDevice, |
| 532 | MediaDeviceState.STATE_CONNECTED); |
| 533 | } |
hughchen | c0c1163 | 2019-03-07 16:21:17 +0800 | [diff] [blame] | 534 | } |
hughchen | bd0dd49 | 2019-01-08 14:34:10 +0800 | [diff] [blame] | 535 | |
hughchen | c0c1163 | 2019-03-07 16:21:17 +0800 | [diff] [blame] | 536 | @Override |
| 537 | public void onDeviceAttributesChanged() { |
hughchen | e572b83 | 2020-01-08 18:07:18 +0800 | [diff] [blame] | 538 | dispatchDeviceAttributesChanged(); |
hughchen | bd0dd49 | 2019-01-08 14:34:10 +0800 | [diff] [blame] | 539 | } |
hughchen | 063a35a | 2020-03-04 17:51:57 +0800 | [diff] [blame] | 540 | |
| 541 | @Override |
| 542 | public void onRequestFailed(int reason) { |
hughchen | 5bd3968 | 2020-05-08 10:56:30 +0800 | [diff] [blame] | 543 | synchronized (mMediaDevicesLock) { |
| 544 | for (MediaDevice device : mMediaDevices) { |
| 545 | if (device.getState() == MediaDeviceState.STATE_CONNECTING) { |
| 546 | device.setState(MediaDeviceState.STATE_CONNECTING_FAILED); |
| 547 | } |
Tim Peng | dbdfaef | 2020-03-11 11:30:01 +0800 | [diff] [blame] | 548 | } |
| 549 | } |
hughchen | 063a35a | 2020-03-04 17:51:57 +0800 | [diff] [blame] | 550 | dispatchOnRequestFailed(reason); |
| 551 | } |
hughchen | a73686f | 2018-11-12 17:42:08 +0800 | [diff] [blame] | 552 | } |
| 553 | |
hughchen | 37282f0 | 2020-03-25 18:12:52 +0800 | [diff] [blame] | 554 | private void unRegisterDeviceAttributeChangeCallback() { |
| 555 | for (MediaDevice device : mDisconnectedMediaDevices) { |
| 556 | ((BluetoothMediaDevice) device).getCachedDevice() |
| 557 | .unregisterCallback(mDeviceAttributeChangeCallback); |
| 558 | } |
| 559 | } |
hughchen | a73686f | 2018-11-12 17:42:08 +0800 | [diff] [blame] | 560 | |
| 561 | /** |
| 562 | * Callback for notifying device information updating |
| 563 | */ |
| 564 | public interface DeviceCallback { |
| 565 | /** |
| 566 | * Callback for notifying device list updated. |
| 567 | * |
| 568 | * @param devices MediaDevice list |
| 569 | */ |
hughchen | e572b83 | 2020-01-08 18:07:18 +0800 | [diff] [blame] | 570 | default void onDeviceListUpdate(List<MediaDevice> devices) {}; |
hughchen | a73686f | 2018-11-12 17:42:08 +0800 | [diff] [blame] | 571 | |
| 572 | /** |
| 573 | * Callback for notifying the connected device is changed. |
| 574 | * |
| 575 | * @param device the changed connected MediaDevice |
| 576 | * @param state the current MediaDevice state, the possible values are: |
| 577 | * {@link MediaDeviceState#STATE_CONNECTED}, |
| 578 | * {@link MediaDeviceState#STATE_CONNECTING}, |
| 579 | * {@link MediaDeviceState#STATE_DISCONNECTED} |
| 580 | */ |
hughchen | e572b83 | 2020-01-08 18:07:18 +0800 | [diff] [blame] | 581 | default void onSelectedDeviceStateChanged(MediaDevice device, |
| 582 | @MediaDeviceState int state) {}; |
| 583 | |
| 584 | /** |
| 585 | * Callback for notifying the device attributes is changed. |
| 586 | */ |
| 587 | default void onDeviceAttributesChanged() {}; |
hughchen | 063a35a | 2020-03-04 17:51:57 +0800 | [diff] [blame] | 588 | |
| 589 | /** |
| 590 | * Callback for notifying that transferring is failed. |
| 591 | * |
| 592 | * @param reason the reason that the request has failed. Can be one of followings: |
| 593 | * {@link android.media.MediaRoute2ProviderService#REASON_UNKNOWN_ERROR}, |
| 594 | * {@link android.media.MediaRoute2ProviderService#REASON_REJECTED}, |
| 595 | * {@link android.media.MediaRoute2ProviderService#REASON_NETWORK_ERROR}, |
| 596 | * {@link android.media.MediaRoute2ProviderService#REASON_ROUTE_NOT_AVAILABLE}, |
| 597 | * {@link android.media.MediaRoute2ProviderService#REASON_INVALID_COMMAND}, |
| 598 | */ |
| 599 | default void onRequestFailed(int reason){}; |
hughchen | a73686f | 2018-11-12 17:42:08 +0800 | [diff] [blame] | 600 | } |
hughchen | b3d68f6 | 2020-02-11 14:30:14 +0800 | [diff] [blame] | 601 | |
| 602 | /** |
| 603 | * This callback is for update {@link BluetoothMediaDevice} summary when |
| 604 | * {@link CachedBluetoothDevice} connection state is changed. |
| 605 | */ |
| 606 | @VisibleForTesting |
| 607 | class DeviceAttributeChangeCallback implements CachedBluetoothDevice.Callback { |
| 608 | |
| 609 | @Override |
| 610 | public void onDeviceAttributesChanged() { |
Tim Peng | 92fb219 | 2020-03-31 11:34:20 +0800 | [diff] [blame] | 611 | if (mOnTransferBluetoothDevice != null |
| 612 | && !((BluetoothMediaDevice) mOnTransferBluetoothDevice).getCachedDevice() |
| 613 | .isBusy() |
| 614 | && !mOnTransferBluetoothDevice.isConnected()) { |
| 615 | // Failed to connect |
| 616 | mOnTransferBluetoothDevice.setState(MediaDeviceState.STATE_DISCONNECTED); |
| 617 | mOnTransferBluetoothDevice = null; |
| 618 | } |
hughchen | b3d68f6 | 2020-02-11 14:30:14 +0800 | [diff] [blame] | 619 | dispatchDeviceAttributesChanged(); |
| 620 | } |
| 621 | } |
hughchen | a73686f | 2018-11-12 17:42:08 +0800 | [diff] [blame] | 622 | } |