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