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; |
hughchen | df7b838 | 2020-02-03 17:48:49 +0800 | [diff] [blame] | 23 | import android.text.TextUtils; |
hughchen | a73686f | 2018-11-12 17:42:08 +0800 | [diff] [blame] | 24 | import android.util.Log; |
| 25 | |
| 26 | import androidx.annotation.IntDef; |
| 27 | |
hughchen | bd0dd49 | 2019-01-08 14:34:10 +0800 | [diff] [blame] | 28 | import com.android.internal.annotations.VisibleForTesting; |
hughchen | a73686f | 2018-11-12 17:42:08 +0800 | [diff] [blame] | 29 | import com.android.settingslib.bluetooth.BluetoothCallback; |
hughchen | bd0dd49 | 2019-01-08 14:34:10 +0800 | [diff] [blame] | 30 | import com.android.settingslib.bluetooth.CachedBluetoothDevice; |
hughchen | b3d68f6 | 2020-02-11 14:30:14 +0800 | [diff] [blame] | 31 | import com.android.settingslib.bluetooth.CachedBluetoothDeviceManager; |
hughchen | a73686f | 2018-11-12 17:42:08 +0800 | [diff] [blame] | 32 | import com.android.settingslib.bluetooth.LocalBluetoothManager; |
| 33 | |
| 34 | import java.lang.annotation.Retention; |
| 35 | import java.lang.annotation.RetentionPolicy; |
| 36 | import java.util.ArrayList; |
| 37 | import java.util.Collection; |
timhypeng | a8b826c | 2018-11-14 15:33:53 +0800 | [diff] [blame] | 38 | import java.util.Comparator; |
hughchen | a73686f | 2018-11-12 17:42:08 +0800 | [diff] [blame] | 39 | import java.util.List; |
hughchen | 8175bf2 | 2020-02-12 18:52:34 +0800 | [diff] [blame] | 40 | import java.util.concurrent.CopyOnWriteArrayList; |
hughchen | a73686f | 2018-11-12 17:42:08 +0800 | [diff] [blame] | 41 | |
| 42 | /** |
| 43 | * LocalMediaManager provide interface to get MediaDevice list and transfer media to MediaDevice. |
| 44 | */ |
| 45 | public class LocalMediaManager implements BluetoothCallback { |
timhypeng | a8b826c | 2018-11-14 15:33:53 +0800 | [diff] [blame] | 46 | private static final Comparator<MediaDevice> COMPARATOR = Comparator.naturalOrder(); |
hughchen | a73686f | 2018-11-12 17:42:08 +0800 | [diff] [blame] | 47 | private static final String TAG = "LocalMediaManager"; |
| 48 | |
| 49 | @Retention(RetentionPolicy.SOURCE) |
| 50 | @IntDef({MediaDeviceState.STATE_CONNECTED, |
| 51 | MediaDeviceState.STATE_CONNECTING, |
Tim Peng | dbdfaef | 2020-03-11 11:30:01 +0800 | [diff] [blame] | 52 | MediaDeviceState.STATE_DISCONNECTED, |
| 53 | MediaDeviceState.STATE_CONNECTING_FAILED}) |
hughchen | a73686f | 2018-11-12 17:42:08 +0800 | [diff] [blame] | 54 | public @interface MediaDeviceState { |
Tim Peng | dbdfaef | 2020-03-11 11:30:01 +0800 | [diff] [blame] | 55 | int STATE_CONNECTED = 0; |
| 56 | int STATE_CONNECTING = 1; |
| 57 | int STATE_DISCONNECTED = 2; |
| 58 | int STATE_CONNECTING_FAILED = 3; |
hughchen | a73686f | 2018-11-12 17:42:08 +0800 | [diff] [blame] | 59 | } |
| 60 | |
hughchen | 8175bf2 | 2020-02-12 18:52:34 +0800 | [diff] [blame] | 61 | private final Collection<DeviceCallback> mCallbacks = new CopyOnWriteArrayList<>(); |
hughchen | bd0dd49 | 2019-01-08 14:34:10 +0800 | [diff] [blame] | 62 | @VisibleForTesting |
| 63 | final MediaDeviceCallback mMediaDeviceCallback = new MediaDeviceCallback(); |
hughchen | a73686f | 2018-11-12 17:42:08 +0800 | [diff] [blame] | 64 | |
| 65 | private Context mContext; |
hughchen | a73686f | 2018-11-12 17:42:08 +0800 | [diff] [blame] | 66 | private LocalBluetoothManager mLocalBluetoothManager; |
hughchen | e6a4f48 | 2019-10-18 17:34:15 +0800 | [diff] [blame] | 67 | private InfoMediaManager mInfoMediaManager; |
| 68 | private String mPackageName; |
hughchen | bd0dd49 | 2019-01-08 14:34:10 +0800 | [diff] [blame] | 69 | |
| 70 | @VisibleForTesting |
| 71 | List<MediaDevice> mMediaDevices = new ArrayList<>(); |
| 72 | @VisibleForTesting |
hughchen | b3d68f6 | 2020-02-11 14:30:14 +0800 | [diff] [blame] | 73 | List<MediaDevice> mDisconnectedMediaDevices = new ArrayList<>(); |
| 74 | @VisibleForTesting |
hughchen | bd0dd49 | 2019-01-08 14:34:10 +0800 | [diff] [blame] | 75 | MediaDevice mPhoneDevice; |
| 76 | @VisibleForTesting |
| 77 | MediaDevice mCurrentConnectedDevice; |
hughchen | b3d68f6 | 2020-02-11 14:30:14 +0800 | [diff] [blame] | 78 | @VisibleForTesting |
| 79 | DeviceAttributeChangeCallback mDeviceAttributeChangeCallback = |
| 80 | new DeviceAttributeChangeCallback(); |
| 81 | @VisibleForTesting |
| 82 | BluetoothAdapter mBluetoothAdapter; |
hughchen | a73686f | 2018-11-12 17:42:08 +0800 | [diff] [blame] | 83 | |
| 84 | /** |
| 85 | * Register to start receiving callbacks for MediaDevice events. |
| 86 | */ |
| 87 | public void registerCallback(DeviceCallback callback) { |
hughchen | 8175bf2 | 2020-02-12 18:52:34 +0800 | [diff] [blame] | 88 | mCallbacks.add(callback); |
hughchen | a73686f | 2018-11-12 17:42:08 +0800 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Unregister to stop receiving callbacks for MediaDevice events |
| 93 | */ |
| 94 | public void unregisterCallback(DeviceCallback callback) { |
hughchen | 8175bf2 | 2020-02-12 18:52:34 +0800 | [diff] [blame] | 95 | mCallbacks.remove(callback); |
hughchen | a73686f | 2018-11-12 17:42:08 +0800 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | public LocalMediaManager(Context context, String packageName, Notification notification) { |
| 99 | mContext = context; |
hughchen | e6a4f48 | 2019-10-18 17:34:15 +0800 | [diff] [blame] | 100 | mPackageName = packageName; |
hughchen | a73686f | 2018-11-12 17:42:08 +0800 | [diff] [blame] | 101 | mLocalBluetoothManager = |
| 102 | LocalBluetoothManager.getInstance(context, /* onInitCallback= */ null); |
hughchen | b3d68f6 | 2020-02-11 14:30:14 +0800 | [diff] [blame] | 103 | mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); |
hughchen | a73686f | 2018-11-12 17:42:08 +0800 | [diff] [blame] | 104 | if (mLocalBluetoothManager == null) { |
| 105 | Log.e(TAG, "Bluetooth is not supported on this device"); |
| 106 | return; |
| 107 | } |
| 108 | |
hughchen | 491b9fc | 2020-02-05 18:31:36 +0800 | [diff] [blame] | 109 | mInfoMediaManager = |
| 110 | new InfoMediaManager(context, packageName, notification, mLocalBluetoothManager); |
hughchen | a73686f | 2018-11-12 17:42:08 +0800 | [diff] [blame] | 111 | } |
| 112 | |
Fabian Kozynski | cc4c6c3 | 2020-03-09 15:11:33 -0400 | [diff] [blame] | 113 | public LocalMediaManager(Context context, LocalBluetoothManager localBluetoothManager, |
hughchen | df7b838 | 2020-02-03 17:48:49 +0800 | [diff] [blame] | 114 | InfoMediaManager infoMediaManager, String packageName) { |
hughchen | bd0dd49 | 2019-01-08 14:34:10 +0800 | [diff] [blame] | 115 | mContext = context; |
| 116 | mLocalBluetoothManager = localBluetoothManager; |
hughchen | e6a4f48 | 2019-10-18 17:34:15 +0800 | [diff] [blame] | 117 | mInfoMediaManager = infoMediaManager; |
hughchen | df7b838 | 2020-02-03 17:48:49 +0800 | [diff] [blame] | 118 | mPackageName = packageName; |
hughchen | bd0dd49 | 2019-01-08 14:34:10 +0800 | [diff] [blame] | 119 | } |
| 120 | |
hughchen | a73686f | 2018-11-12 17:42:08 +0800 | [diff] [blame] | 121 | /** |
| 122 | * Connect the MediaDevice to transfer media |
| 123 | * @param connectDevice the MediaDevice |
| 124 | */ |
| 125 | public void connectDevice(MediaDevice connectDevice) { |
hughchen | bd0dd49 | 2019-01-08 14:34:10 +0800 | [diff] [blame] | 126 | final MediaDevice device = getMediaDeviceById(mMediaDevices, connectDevice.getId()); |
hughchen | c0c1163 | 2019-03-07 16:21:17 +0800 | [diff] [blame] | 127 | if (device instanceof BluetoothMediaDevice) { |
| 128 | final CachedBluetoothDevice cachedDevice = |
| 129 | ((BluetoothMediaDevice) device).getCachedDevice(); |
| 130 | if (!cachedDevice.isConnected() && !cachedDevice.isBusy()) { |
Tim Peng | dbdfaef | 2020-03-11 11:30:01 +0800 | [diff] [blame] | 131 | device.setState(MediaDeviceState.STATE_CONNECTING); |
hughchen | 4ae2186 | 2020-01-14 15:55:29 +0800 | [diff] [blame] | 132 | cachedDevice.connect(); |
hughchen | c0c1163 | 2019-03-07 16:21:17 +0800 | [diff] [blame] | 133 | return; |
| 134 | } |
| 135 | } |
| 136 | |
hughchen | bd0dd49 | 2019-01-08 14:34:10 +0800 | [diff] [blame] | 137 | if (device == mCurrentConnectedDevice) { |
| 138 | Log.d(TAG, "connectDevice() this device all ready connected! : " + device.getName()); |
hughchen | a73686f | 2018-11-12 17:42:08 +0800 | [diff] [blame] | 139 | return; |
| 140 | } |
| 141 | |
hughchen | e6a4f48 | 2019-10-18 17:34:15 +0800 | [diff] [blame] | 142 | if (mCurrentConnectedDevice != null) { |
hughchen | bd0dd49 | 2019-01-08 14:34:10 +0800 | [diff] [blame] | 143 | mCurrentConnectedDevice.disconnect(); |
hughchen | a73686f | 2018-11-12 17:42:08 +0800 | [diff] [blame] | 144 | } |
| 145 | |
Tim Peng | dbdfaef | 2020-03-11 11:30:01 +0800 | [diff] [blame] | 146 | device.setState(MediaDeviceState.STATE_CONNECTING); |
hughchen | df7b838 | 2020-02-03 17:48:49 +0800 | [diff] [blame] | 147 | if (TextUtils.isEmpty(mPackageName)) { |
hughchen | 063a35a | 2020-03-04 17:51:57 +0800 | [diff] [blame] | 148 | mInfoMediaManager.connectDeviceWithoutPackageName(device); |
hughchen | df7b838 | 2020-02-03 17:48:49 +0800 | [diff] [blame] | 149 | } else { |
hughchen | 063a35a | 2020-03-04 17:51:57 +0800 | [diff] [blame] | 150 | device.connect(); |
hughchen | df7b838 | 2020-02-03 17:48:49 +0800 | [diff] [blame] | 151 | } |
hughchen | a73686f | 2018-11-12 17:42:08 +0800 | [diff] [blame] | 152 | } |
| 153 | |
timhypeng | 97c6cf6 | 2018-12-27 15:40:32 +0800 | [diff] [blame] | 154 | void dispatchSelectedDeviceStateChanged(MediaDevice device, @MediaDeviceState int state) { |
hughchen | 8175bf2 | 2020-02-12 18:52:34 +0800 | [diff] [blame] | 155 | for (DeviceCallback callback : getCallbacks()) { |
| 156 | callback.onSelectedDeviceStateChanged(device, state); |
hughchen | a73686f | 2018-11-12 17:42:08 +0800 | [diff] [blame] | 157 | } |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Start scan connected MediaDevice |
| 162 | */ |
| 163 | public void startScan() { |
| 164 | mMediaDevices.clear(); |
hughchen | 0d20204 | 2020-01-17 18:30:03 +0800 | [diff] [blame] | 165 | mInfoMediaManager.registerCallback(mMediaDeviceCallback); |
| 166 | mInfoMediaManager.startScan(); |
hughchen | a73686f | 2018-11-12 17:42:08 +0800 | [diff] [blame] | 167 | } |
| 168 | |
hughchen | a73686f | 2018-11-12 17:42:08 +0800 | [diff] [blame] | 169 | void dispatchDeviceListUpdate() { |
hughchen | b3d68f6 | 2020-02-11 14:30:14 +0800 | [diff] [blame] | 170 | //TODO(b/149260820): Use new rule to rank device once device type api is ready. |
hughchen | 8175bf2 | 2020-02-12 18:52:34 +0800 | [diff] [blame] | 171 | for (DeviceCallback callback : getCallbacks()) { |
| 172 | callback.onDeviceListUpdate(new ArrayList<>(mMediaDevices)); |
hughchen | a73686f | 2018-11-12 17:42:08 +0800 | [diff] [blame] | 173 | } |
| 174 | } |
| 175 | |
hughchen | e572b83 | 2020-01-08 18:07:18 +0800 | [diff] [blame] | 176 | void dispatchDeviceAttributesChanged() { |
hughchen | 8175bf2 | 2020-02-12 18:52:34 +0800 | [diff] [blame] | 177 | for (DeviceCallback callback : getCallbacks()) { |
| 178 | callback.onDeviceAttributesChanged(); |
hughchen | e572b83 | 2020-01-08 18:07:18 +0800 | [diff] [blame] | 179 | } |
| 180 | } |
| 181 | |
hughchen | 063a35a | 2020-03-04 17:51:57 +0800 | [diff] [blame] | 182 | void dispatchOnRequestFailed(int reason) { |
| 183 | for (DeviceCallback callback : getCallbacks()) { |
| 184 | callback.onRequestFailed(reason); |
| 185 | } |
| 186 | } |
| 187 | |
hughchen | a73686f | 2018-11-12 17:42:08 +0800 | [diff] [blame] | 188 | /** |
| 189 | * Stop scan MediaDevice |
| 190 | */ |
| 191 | public void stopScan() { |
hughchen | 0d20204 | 2020-01-17 18:30:03 +0800 | [diff] [blame] | 192 | mInfoMediaManager.unregisterCallback(mMediaDeviceCallback); |
| 193 | mInfoMediaManager.stopScan(); |
hughchen | a73686f | 2018-11-12 17:42:08 +0800 | [diff] [blame] | 194 | } |
| 195 | |
hughchen | 0487263 | 2018-12-24 14:41:20 +0800 | [diff] [blame] | 196 | /** |
| 197 | * Find the MediaDevice through id. |
| 198 | * |
| 199 | * @param devices the list of MediaDevice |
| 200 | * @param id the unique id of MediaDevice |
| 201 | * @return MediaDevice |
| 202 | */ |
| 203 | public MediaDevice getMediaDeviceById(List<MediaDevice> devices, String id) { |
| 204 | for (MediaDevice mediaDevice : devices) { |
| 205 | if (mediaDevice.getId().equals(id)) { |
| 206 | return mediaDevice; |
| 207 | } |
| 208 | } |
| 209 | Log.i(TAG, "getMediaDeviceById() can't found device"); |
| 210 | return null; |
| 211 | } |
| 212 | |
hughchen | bd0dd49 | 2019-01-08 14:34:10 +0800 | [diff] [blame] | 213 | /** |
timhypeng | d37d70b | 2020-02-05 13:52:37 +0800 | [diff] [blame] | 214 | * Find the MediaDevice from all media devices by id. |
| 215 | * |
| 216 | * @param id the unique id of MediaDevice |
| 217 | * @return MediaDevice |
| 218 | */ |
| 219 | public MediaDevice getMediaDeviceById(String id) { |
| 220 | for (MediaDevice mediaDevice : mMediaDevices) { |
| 221 | if (mediaDevice.getId().equals(id)) { |
| 222 | return mediaDevice; |
| 223 | } |
| 224 | } |
| 225 | Log.i(TAG, "Unable to find device " + id); |
| 226 | return null; |
| 227 | } |
| 228 | |
| 229 | /** |
hughchen | bd0dd49 | 2019-01-08 14:34:10 +0800 | [diff] [blame] | 230 | * Find the current connected MediaDevice. |
| 231 | * |
| 232 | * @return MediaDevice |
| 233 | */ |
| 234 | public MediaDevice getCurrentConnectedDevice() { |
| 235 | return mCurrentConnectedDevice; |
| 236 | } |
| 237 | |
timhypeng | a812e88 | 2020-02-04 17:15:48 +0800 | [diff] [blame] | 238 | /** |
| 239 | * Find the active MediaDevice. |
| 240 | * |
| 241 | * @param type the media device type. |
| 242 | * @return MediaDevice list |
| 243 | */ |
| 244 | public List<MediaDevice> getActiveMediaDevice(@MediaDevice.MediaDeviceType int type) { |
| 245 | final List<MediaDevice> devices = new ArrayList<>(); |
| 246 | for (MediaDevice device : mMediaDevices) { |
| 247 | if (type == device.mType && device.getClientPackageName() != null) { |
| 248 | devices.add(device); |
| 249 | } |
| 250 | } |
| 251 | return devices; |
| 252 | } |
| 253 | |
hughchen | 6ab62ca | 2020-02-12 10:40:04 +0800 | [diff] [blame] | 254 | /** |
| 255 | * Add a MediaDevice to let it play current media. |
| 256 | * |
| 257 | * @param device MediaDevice |
| 258 | * @return If add device successful return {@code true}, otherwise return {@code false} |
| 259 | */ |
| 260 | public boolean addDeviceToPlayMedia(MediaDevice device) { |
| 261 | return mInfoMediaManager.addDeviceToPlayMedia(device); |
| 262 | } |
| 263 | |
| 264 | /** |
| 265 | * Remove a {@code device} from current media. |
| 266 | * |
| 267 | * @param device MediaDevice |
| 268 | * @return If device stop successful return {@code true}, otherwise return {@code false} |
| 269 | */ |
| 270 | public boolean removeDeviceFromPlayMedia(MediaDevice device) { |
| 271 | return mInfoMediaManager.removeDeviceFromPlayMedia(device); |
| 272 | } |
| 273 | |
| 274 | /** |
| 275 | * Get the MediaDevice list that can be added to current media. |
| 276 | * |
| 277 | * @return list of MediaDevice |
| 278 | */ |
| 279 | public List<MediaDevice> getSelectableMediaDevice() { |
| 280 | return mInfoMediaManager.getSelectableMediaDevice(); |
| 281 | } |
| 282 | |
| 283 | /** |
hughchen | 61bced0 | 2020-02-20 18:46:32 +0800 | [diff] [blame] | 284 | * Release session to stop playing media on MediaDevice. |
| 285 | */ |
| 286 | public boolean releaseSession() { |
| 287 | return mInfoMediaManager.releaseSession(); |
| 288 | } |
| 289 | |
| 290 | /** |
timhypeng | 6bbd555 | 2020-02-17 14:53:45 +0800 | [diff] [blame] | 291 | * Get the MediaDevice list that has been selected to current media. |
| 292 | * |
| 293 | * @return list of MediaDevice |
| 294 | */ |
| 295 | public List<MediaDevice> getSelectedMediaDevice() { |
| 296 | return mInfoMediaManager.getSelectedMediaDevice(); |
| 297 | } |
| 298 | |
| 299 | /** |
hughchen | 6ab62ca | 2020-02-12 10:40:04 +0800 | [diff] [blame] | 300 | * Adjust the volume of session. |
| 301 | * |
| 302 | * @param volume the value of volume |
| 303 | */ |
| 304 | public void adjustSessionVolume(int volume) { |
| 305 | mInfoMediaManager.adjustSessionVolume(volume); |
| 306 | } |
| 307 | |
| 308 | /** |
| 309 | * Gets the maximum volume of the {@link android.media.RoutingSessionInfo}. |
| 310 | * |
| 311 | * @return maximum volume of the session, and return -1 if not found. |
| 312 | */ |
| 313 | public int getSessionVolumeMax() { |
| 314 | return mInfoMediaManager.getSessionVolumeMax(); |
| 315 | } |
| 316 | |
| 317 | /** |
| 318 | * Gets the current volume of the {@link android.media.RoutingSessionInfo}. |
| 319 | * |
| 320 | * @return current volume of the session, and return -1 if not found. |
| 321 | */ |
| 322 | public int getSessionVolume() { |
| 323 | return mInfoMediaManager.getSessionVolume(); |
| 324 | } |
| 325 | |
Tim Peng | 635b04a | 2020-03-05 13:08:02 +0800 | [diff] [blame] | 326 | /** |
| 327 | * Gets the user-visible name of the {@link android.media.RoutingSessionInfo}. |
| 328 | * |
| 329 | * @return current name of the session, and return {@code null} if not found. |
| 330 | */ |
| 331 | public CharSequence getSessionName() { |
| 332 | return mInfoMediaManager.getSessionName(); |
| 333 | } |
| 334 | |
hughchen | bd0dd49 | 2019-01-08 14:34:10 +0800 | [diff] [blame] | 335 | private MediaDevice updateCurrentConnectedDevice() { |
hughchen | 491b9fc | 2020-02-05 18:31:36 +0800 | [diff] [blame] | 336 | MediaDevice phoneMediaDevice = null; |
hughchen | bd0dd49 | 2019-01-08 14:34:10 +0800 | [diff] [blame] | 337 | for (MediaDevice device : mMediaDevices) { |
| 338 | if (device instanceof BluetoothMediaDevice) { |
hughchen | 063a35a | 2020-03-04 17:51:57 +0800 | [diff] [blame] | 339 | if (isActiveDevice(((BluetoothMediaDevice) device).getCachedDevice())) { |
hughchen | bd0dd49 | 2019-01-08 14:34:10 +0800 | [diff] [blame] | 340 | return device; |
| 341 | } |
hughchen | 491b9fc | 2020-02-05 18:31:36 +0800 | [diff] [blame] | 342 | } else if (device instanceof PhoneMediaDevice) { |
| 343 | phoneMediaDevice = device; |
hughchen | bd0dd49 | 2019-01-08 14:34:10 +0800 | [diff] [blame] | 344 | } |
| 345 | } |
hughchen | 491b9fc | 2020-02-05 18:31:36 +0800 | [diff] [blame] | 346 | return mMediaDevices.contains(phoneMediaDevice) ? phoneMediaDevice : null; |
hughchen | bd0dd49 | 2019-01-08 14:34:10 +0800 | [diff] [blame] | 347 | } |
| 348 | |
hughchen | 063a35a | 2020-03-04 17:51:57 +0800 | [diff] [blame] | 349 | private boolean isActiveDevice(CachedBluetoothDevice device) { |
hughchen | bd0dd49 | 2019-01-08 14:34:10 +0800 | [diff] [blame] | 350 | return device.isActiveDevice(BluetoothProfile.A2DP) |
| 351 | || device.isActiveDevice(BluetoothProfile.HEARING_AID); |
| 352 | } |
| 353 | |
hughchen | 8175bf2 | 2020-02-12 18:52:34 +0800 | [diff] [blame] | 354 | private Collection<DeviceCallback> getCallbacks() { |
| 355 | return new CopyOnWriteArrayList<>(mCallbacks); |
| 356 | } |
| 357 | |
hughchen | a73686f | 2018-11-12 17:42:08 +0800 | [diff] [blame] | 358 | class MediaDeviceCallback implements MediaManager.MediaDeviceCallback { |
| 359 | @Override |
| 360 | public void onDeviceAdded(MediaDevice device) { |
| 361 | if (!mMediaDevices.contains(device)) { |
| 362 | mMediaDevices.add(device); |
hughchen | a73686f | 2018-11-12 17:42:08 +0800 | [diff] [blame] | 363 | dispatchDeviceListUpdate(); |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | @Override |
| 368 | public void onDeviceListAdded(List<MediaDevice> devices) { |
hughchen | df7b838 | 2020-02-03 17:48:49 +0800 | [diff] [blame] | 369 | mMediaDevices.clear(); |
| 370 | mMediaDevices.addAll(devices); |
hughchen | b3d68f6 | 2020-02-11 14:30:14 +0800 | [diff] [blame] | 371 | mMediaDevices.addAll(buildDisconnectedBluetoothDevice()); |
| 372 | |
hughchen | e6a4f48 | 2019-10-18 17:34:15 +0800 | [diff] [blame] | 373 | final MediaDevice infoMediaDevice = mInfoMediaManager.getCurrentConnectedDevice(); |
| 374 | mCurrentConnectedDevice = infoMediaDevice != null |
| 375 | ? infoMediaDevice : updateCurrentConnectedDevice(); |
hughchen | a73686f | 2018-11-12 17:42:08 +0800 | [diff] [blame] | 376 | dispatchDeviceListUpdate(); |
| 377 | } |
| 378 | |
hughchen | b3d68f6 | 2020-02-11 14:30:14 +0800 | [diff] [blame] | 379 | private List<MediaDevice> buildDisconnectedBluetoothDevice() { |
| 380 | for (MediaDevice device : mDisconnectedMediaDevices) { |
| 381 | ((BluetoothMediaDevice) device).getCachedDevice() |
| 382 | .unregisterCallback(mDeviceAttributeChangeCallback); |
| 383 | } |
| 384 | mDisconnectedMediaDevices.clear(); |
| 385 | final List<BluetoothDevice> bluetoothDevices = |
| 386 | mBluetoothAdapter.getMostRecentlyConnectedDevices(); |
| 387 | final CachedBluetoothDeviceManager cachedDeviceManager = |
| 388 | mLocalBluetoothManager.getCachedDeviceManager(); |
| 389 | |
| 390 | for (BluetoothDevice device : bluetoothDevices) { |
| 391 | final CachedBluetoothDevice cachedDevice = |
| 392 | cachedDeviceManager.findDevice(device); |
| 393 | if (cachedDevice != null) { |
| 394 | if (cachedDevice.getBondState() == BluetoothDevice.BOND_BONDED |
| 395 | && !cachedDevice.isConnected()) { |
| 396 | final MediaDevice mediaDevice = new BluetoothMediaDevice(mContext, |
| 397 | cachedDevice, |
| 398 | null, null, mPackageName); |
| 399 | if (!mMediaDevices.contains(mediaDevice)) { |
| 400 | cachedDevice.registerCallback(mDeviceAttributeChangeCallback); |
| 401 | mDisconnectedMediaDevices.add(mediaDevice); |
| 402 | } |
| 403 | } |
| 404 | } |
| 405 | } |
| 406 | return new ArrayList<>(mDisconnectedMediaDevices); |
| 407 | } |
| 408 | |
hughchen | a73686f | 2018-11-12 17:42:08 +0800 | [diff] [blame] | 409 | @Override |
| 410 | public void onDeviceRemoved(MediaDevice device) { |
| 411 | if (mMediaDevices.contains(device)) { |
| 412 | mMediaDevices.remove(device); |
hughchen | a73686f | 2018-11-12 17:42:08 +0800 | [diff] [blame] | 413 | dispatchDeviceListUpdate(); |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | @Override |
| 418 | public void onDeviceListRemoved(List<MediaDevice> devices) { |
| 419 | mMediaDevices.removeAll(devices); |
hughchen | a73686f | 2018-11-12 17:42:08 +0800 | [diff] [blame] | 420 | dispatchDeviceListUpdate(); |
| 421 | } |
| 422 | |
| 423 | @Override |
hughchen | bd0dd49 | 2019-01-08 14:34:10 +0800 | [diff] [blame] | 424 | public void onConnectedDeviceChanged(String id) { |
hughchen | 063a35a | 2020-03-04 17:51:57 +0800 | [diff] [blame] | 425 | MediaDevice connectDevice = getMediaDeviceById(mMediaDevices, id); |
| 426 | connectDevice = connectDevice != null |
| 427 | ? connectDevice : updateCurrentConnectedDevice(); |
Tim Peng | dbdfaef | 2020-03-11 11:30:01 +0800 | [diff] [blame] | 428 | connectDevice.setState(MediaDeviceState.STATE_CONNECTED); |
hughchen | bd0dd49 | 2019-01-08 14:34:10 +0800 | [diff] [blame] | 429 | |
| 430 | if (connectDevice == mCurrentConnectedDevice) { |
hughchen | c0c1163 | 2019-03-07 16:21:17 +0800 | [diff] [blame] | 431 | Log.d(TAG, "onConnectedDeviceChanged() this device all ready connected!"); |
hughchen | bd0dd49 | 2019-01-08 14:34:10 +0800 | [diff] [blame] | 432 | return; |
| 433 | } |
| 434 | mCurrentConnectedDevice = connectDevice; |
hughchen | 063a35a | 2020-03-04 17:51:57 +0800 | [diff] [blame] | 435 | dispatchSelectedDeviceStateChanged(mCurrentConnectedDevice, |
| 436 | MediaDeviceState.STATE_CONNECTED); |
hughchen | c0c1163 | 2019-03-07 16:21:17 +0800 | [diff] [blame] | 437 | } |
hughchen | bd0dd49 | 2019-01-08 14:34:10 +0800 | [diff] [blame] | 438 | |
hughchen | c0c1163 | 2019-03-07 16:21:17 +0800 | [diff] [blame] | 439 | @Override |
| 440 | public void onDeviceAttributesChanged() { |
hughchen | e572b83 | 2020-01-08 18:07:18 +0800 | [diff] [blame] | 441 | dispatchDeviceAttributesChanged(); |
hughchen | bd0dd49 | 2019-01-08 14:34:10 +0800 | [diff] [blame] | 442 | } |
hughchen | 063a35a | 2020-03-04 17:51:57 +0800 | [diff] [blame] | 443 | |
| 444 | @Override |
| 445 | public void onRequestFailed(int reason) { |
Tim Peng | dbdfaef | 2020-03-11 11:30:01 +0800 | [diff] [blame] | 446 | for (MediaDevice device : mMediaDevices) { |
| 447 | if (device.getState() == MediaDeviceState.STATE_CONNECTING) { |
| 448 | device.setState(MediaDeviceState.STATE_CONNECTING_FAILED); |
| 449 | } |
| 450 | } |
hughchen | 063a35a | 2020-03-04 17:51:57 +0800 | [diff] [blame] | 451 | dispatchOnRequestFailed(reason); |
| 452 | } |
hughchen | a73686f | 2018-11-12 17:42:08 +0800 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | |
| 456 | /** |
| 457 | * Callback for notifying device information updating |
| 458 | */ |
| 459 | public interface DeviceCallback { |
| 460 | /** |
| 461 | * Callback for notifying device list updated. |
| 462 | * |
| 463 | * @param devices MediaDevice list |
| 464 | */ |
hughchen | e572b83 | 2020-01-08 18:07:18 +0800 | [diff] [blame] | 465 | default void onDeviceListUpdate(List<MediaDevice> devices) {}; |
hughchen | a73686f | 2018-11-12 17:42:08 +0800 | [diff] [blame] | 466 | |
| 467 | /** |
| 468 | * Callback for notifying the connected device is changed. |
| 469 | * |
| 470 | * @param device the changed connected MediaDevice |
| 471 | * @param state the current MediaDevice state, the possible values are: |
| 472 | * {@link MediaDeviceState#STATE_CONNECTED}, |
| 473 | * {@link MediaDeviceState#STATE_CONNECTING}, |
| 474 | * {@link MediaDeviceState#STATE_DISCONNECTED} |
| 475 | */ |
hughchen | e572b83 | 2020-01-08 18:07:18 +0800 | [diff] [blame] | 476 | default void onSelectedDeviceStateChanged(MediaDevice device, |
| 477 | @MediaDeviceState int state) {}; |
| 478 | |
| 479 | /** |
| 480 | * Callback for notifying the device attributes is changed. |
| 481 | */ |
| 482 | default void onDeviceAttributesChanged() {}; |
hughchen | 063a35a | 2020-03-04 17:51:57 +0800 | [diff] [blame] | 483 | |
| 484 | /** |
| 485 | * Callback for notifying that transferring is failed. |
| 486 | * |
| 487 | * @param reason the reason that the request has failed. Can be one of followings: |
| 488 | * {@link android.media.MediaRoute2ProviderService#REASON_UNKNOWN_ERROR}, |
| 489 | * {@link android.media.MediaRoute2ProviderService#REASON_REJECTED}, |
| 490 | * {@link android.media.MediaRoute2ProviderService#REASON_NETWORK_ERROR}, |
| 491 | * {@link android.media.MediaRoute2ProviderService#REASON_ROUTE_NOT_AVAILABLE}, |
| 492 | * {@link android.media.MediaRoute2ProviderService#REASON_INVALID_COMMAND}, |
| 493 | */ |
| 494 | default void onRequestFailed(int reason){}; |
hughchen | a73686f | 2018-11-12 17:42:08 +0800 | [diff] [blame] | 495 | } |
hughchen | b3d68f6 | 2020-02-11 14:30:14 +0800 | [diff] [blame] | 496 | |
| 497 | /** |
| 498 | * This callback is for update {@link BluetoothMediaDevice} summary when |
| 499 | * {@link CachedBluetoothDevice} connection state is changed. |
| 500 | */ |
| 501 | @VisibleForTesting |
| 502 | class DeviceAttributeChangeCallback implements CachedBluetoothDevice.Callback { |
| 503 | |
| 504 | @Override |
| 505 | public void onDeviceAttributesChanged() { |
| 506 | dispatchDeviceAttributesChanged(); |
| 507 | } |
| 508 | } |
hughchen | a73686f | 2018-11-12 17:42:08 +0800 | [diff] [blame] | 509 | } |