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 | |
hughchen | 6283741 | 2020-02-24 17:03:26 +0800 | [diff] [blame] | 18 | import static android.media.MediaRoute2Info.TYPE_BLUETOOTH_A2DP; |
| 19 | import static android.media.MediaRoute2Info.TYPE_BUILTIN_SPEAKER; |
hughchen | e8c79b0 | 2020-04-21 17:26:21 +0800 | [diff] [blame] | 20 | import static android.media.MediaRoute2Info.TYPE_DOCK; |
hughchen | 6283741 | 2020-02-24 17:03:26 +0800 | [diff] [blame] | 21 | import static android.media.MediaRoute2Info.TYPE_GROUP; |
hughchen | e8c79b0 | 2020-04-21 17:26:21 +0800 | [diff] [blame] | 22 | import static android.media.MediaRoute2Info.TYPE_HDMI; |
hughchen | 6283741 | 2020-02-24 17:03:26 +0800 | [diff] [blame] | 23 | import static android.media.MediaRoute2Info.TYPE_HEARING_AID; |
| 24 | import static android.media.MediaRoute2Info.TYPE_REMOTE_SPEAKER; |
| 25 | import static android.media.MediaRoute2Info.TYPE_REMOTE_TV; |
| 26 | import static android.media.MediaRoute2Info.TYPE_UNKNOWN; |
hughchen | e8c79b0 | 2020-04-21 17:26:21 +0800 | [diff] [blame] | 27 | import static android.media.MediaRoute2Info.TYPE_USB_ACCESSORY; |
| 28 | import static android.media.MediaRoute2Info.TYPE_USB_DEVICE; |
| 29 | import static android.media.MediaRoute2Info.TYPE_USB_HEADSET; |
hughchen | 6283741 | 2020-02-24 17:03:26 +0800 | [diff] [blame] | 30 | import static android.media.MediaRoute2Info.TYPE_WIRED_HEADPHONES; |
| 31 | import static android.media.MediaRoute2Info.TYPE_WIRED_HEADSET; |
hughchen | 063a35a | 2020-03-04 17:51:57 +0800 | [diff] [blame] | 32 | import static android.media.MediaRoute2ProviderService.REASON_UNKNOWN_ERROR; |
hughchen | 491b9fc | 2020-02-05 18:31:36 +0800 | [diff] [blame] | 33 | |
hughchen | a73686f | 2018-11-12 17:42:08 +0800 | [diff] [blame] | 34 | import android.app.Notification; |
hughchen | 491b9fc | 2020-02-05 18:31:36 +0800 | [diff] [blame] | 35 | import android.bluetooth.BluetoothAdapter; |
| 36 | import android.bluetooth.BluetoothDevice; |
hughchen | a73686f | 2018-11-12 17:42:08 +0800 | [diff] [blame] | 37 | import android.content.Context; |
hughchen | e6a4f48 | 2019-10-18 17:34:15 +0800 | [diff] [blame] | 38 | import android.media.MediaRoute2Info; |
| 39 | import android.media.MediaRouter2Manager; |
hughchen | df7b838 | 2020-02-03 17:48:49 +0800 | [diff] [blame] | 40 | import android.media.RoutingSessionInfo; |
hughchen | e6a4f48 | 2019-10-18 17:34:15 +0800 | [diff] [blame] | 41 | import android.text.TextUtils; |
hughchen | 491b9fc | 2020-02-05 18:31:36 +0800 | [diff] [blame] | 42 | import android.util.Log; |
hughchen | a73686f | 2018-11-12 17:42:08 +0800 | [diff] [blame] | 43 | |
hughchen | bd0dd49 | 2019-01-08 14:34:10 +0800 | [diff] [blame] | 44 | import com.android.internal.annotations.VisibleForTesting; |
hughchen | 491b9fc | 2020-02-05 18:31:36 +0800 | [diff] [blame] | 45 | import com.android.settingslib.bluetooth.CachedBluetoothDevice; |
| 46 | import com.android.settingslib.bluetooth.LocalBluetoothManager; |
hughchen | bd0dd49 | 2019-01-08 14:34:10 +0800 | [diff] [blame] | 47 | |
hughchen | 6ab62ca | 2020-02-12 10:40:04 +0800 | [diff] [blame] | 48 | import java.util.ArrayList; |
hughchen | e6a4f48 | 2019-10-18 17:34:15 +0800 | [diff] [blame] | 49 | import java.util.List; |
| 50 | import java.util.concurrent.Executor; |
| 51 | import java.util.concurrent.Executors; |
| 52 | |
hughchen | a73686f | 2018-11-12 17:42:08 +0800 | [diff] [blame] | 53 | /** |
| 54 | * InfoMediaManager provide interface to get InfoMediaDevice list. |
| 55 | */ |
| 56 | public class InfoMediaManager extends MediaManager { |
| 57 | |
| 58 | private static final String TAG = "InfoMediaManager"; |
hughchen | 063a35a | 2020-03-04 17:51:57 +0800 | [diff] [blame] | 59 | private static final boolean DEBUG = false; |
hughchen | bd0dd49 | 2019-01-08 14:34:10 +0800 | [diff] [blame] | 60 | @VisibleForTesting |
hughchen | e6a4f48 | 2019-10-18 17:34:15 +0800 | [diff] [blame] | 61 | final RouterManagerCallback mMediaRouterCallback = new RouterManagerCallback(); |
hughchen | bd0dd49 | 2019-01-08 14:34:10 +0800 | [diff] [blame] | 62 | @VisibleForTesting |
hughchen | e6a4f48 | 2019-10-18 17:34:15 +0800 | [diff] [blame] | 63 | final Executor mExecutor = Executors.newSingleThreadExecutor(); |
hughchen | bd0dd49 | 2019-01-08 14:34:10 +0800 | [diff] [blame] | 64 | @VisibleForTesting |
hughchen | e6a4f48 | 2019-10-18 17:34:15 +0800 | [diff] [blame] | 65 | MediaRouter2Manager mRouterManager; |
hughchen | 0d20204 | 2020-01-17 18:30:03 +0800 | [diff] [blame] | 66 | @VisibleForTesting |
| 67 | String mPackageName; |
hughchen | a73686f | 2018-11-12 17:42:08 +0800 | [diff] [blame] | 68 | |
hughchen | e6a4f48 | 2019-10-18 17:34:15 +0800 | [diff] [blame] | 69 | private MediaDevice mCurrentConnectedDevice; |
hughchen | 491b9fc | 2020-02-05 18:31:36 +0800 | [diff] [blame] | 70 | private LocalBluetoothManager mBluetoothManager; |
hughchen | a73686f | 2018-11-12 17:42:08 +0800 | [diff] [blame] | 71 | |
hughchen | 491b9fc | 2020-02-05 18:31:36 +0800 | [diff] [blame] | 72 | public InfoMediaManager(Context context, String packageName, Notification notification, |
| 73 | LocalBluetoothManager localBluetoothManager) { |
hughchen | a73686f | 2018-11-12 17:42:08 +0800 | [diff] [blame] | 74 | super(context, notification); |
| 75 | |
hughchen | e6a4f48 | 2019-10-18 17:34:15 +0800 | [diff] [blame] | 76 | mRouterManager = MediaRouter2Manager.getInstance(context); |
hughchen | 491b9fc | 2020-02-05 18:31:36 +0800 | [diff] [blame] | 77 | mBluetoothManager = localBluetoothManager; |
hughchen | 0d20204 | 2020-01-17 18:30:03 +0800 | [diff] [blame] | 78 | if (!TextUtils.isEmpty(packageName)) { |
hughchen | e6a4f48 | 2019-10-18 17:34:15 +0800 | [diff] [blame] | 79 | mPackageName = packageName; |
| 80 | } |
hughchen | a73686f | 2018-11-12 17:42:08 +0800 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public void startScan() { |
| 85 | mMediaDevices.clear(); |
hughchen | e6a4f48 | 2019-10-18 17:34:15 +0800 | [diff] [blame] | 86 | mRouterManager.registerCallback(mExecutor, mMediaRouterCallback); |
hughchen | 0d20204 | 2020-01-17 18:30:03 +0800 | [diff] [blame] | 87 | refreshDevices(); |
hughchen | a73686f | 2018-11-12 17:42:08 +0800 | [diff] [blame] | 88 | } |
| 89 | |
hughchen | a73686f | 2018-11-12 17:42:08 +0800 | [diff] [blame] | 90 | @Override |
| 91 | public void stopScan() { |
hughchen | e6a4f48 | 2019-10-18 17:34:15 +0800 | [diff] [blame] | 92 | mRouterManager.unregisterCallback(mMediaRouterCallback); |
hughchen | a73686f | 2018-11-12 17:42:08 +0800 | [diff] [blame] | 93 | } |
| 94 | |
hughchen | e6a4f48 | 2019-10-18 17:34:15 +0800 | [diff] [blame] | 95 | /** |
| 96 | * Get current device that played media. |
| 97 | * @return MediaDevice |
| 98 | */ |
hughchen | 6ab62ca | 2020-02-12 10:40:04 +0800 | [diff] [blame] | 99 | MediaDevice getCurrentConnectedDevice() { |
hughchen | e6a4f48 | 2019-10-18 17:34:15 +0800 | [diff] [blame] | 100 | return mCurrentConnectedDevice; |
| 101 | } |
| 102 | |
hughchen | 6ab62ca | 2020-02-12 10:40:04 +0800 | [diff] [blame] | 103 | /** |
| 104 | * Transfer MediaDevice for media without package name. |
| 105 | */ |
| 106 | boolean connectDeviceWithoutPackageName(MediaDevice device) { |
| 107 | boolean isConnected = false; |
| 108 | final List<RoutingSessionInfo> infos = mRouterManager.getActiveSessions(); |
| 109 | if (infos.size() > 0) { |
| 110 | final RoutingSessionInfo info = infos.get(0); |
Kyunglyul Hyun | fa6f06d | 2020-04-30 02:56:40 +0900 | [diff] [blame] | 111 | mRouterManager.transfer(info, device.mRouteInfo); |
hughchen | 6ab62ca | 2020-02-12 10:40:04 +0800 | [diff] [blame] | 112 | |
hughchen | 6ab62ca | 2020-02-12 10:40:04 +0800 | [diff] [blame] | 113 | isConnected = true; |
| 114 | } |
| 115 | return isConnected; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Add a MediaDevice to let it play current media. |
| 120 | * |
| 121 | * @param device MediaDevice |
| 122 | * @return If add device successful return {@code true}, otherwise return {@code false} |
| 123 | */ |
| 124 | boolean addDeviceToPlayMedia(MediaDevice device) { |
| 125 | if (TextUtils.isEmpty(mPackageName)) { |
| 126 | Log.w(TAG, "addDeviceToPlayMedia() package name is null or empty!"); |
| 127 | return false; |
| 128 | } |
| 129 | |
| 130 | final RoutingSessionInfo info = getRoutingSessionInfo(); |
| 131 | if (info != null && info.getSelectableRoutes().contains(device.mRouteInfo.getId())) { |
Kyunglyul Hyun | fa6f06d | 2020-04-30 02:56:40 +0900 | [diff] [blame] | 132 | mRouterManager.selectRoute(info, device.mRouteInfo); |
hughchen | 6ab62ca | 2020-02-12 10:40:04 +0800 | [diff] [blame] | 133 | return true; |
| 134 | } |
| 135 | |
| 136 | Log.w(TAG, "addDeviceToPlayMedia() Ignoring selecting a non-selectable device : " |
| 137 | + device.getName()); |
| 138 | |
| 139 | return false; |
| 140 | } |
| 141 | |
| 142 | private RoutingSessionInfo getRoutingSessionInfo() { |
hughchen | 8e9e4b4 | 2020-03-23 18:59:58 +0800 | [diff] [blame] | 143 | final List<RoutingSessionInfo> sessionInfos = |
| 144 | mRouterManager.getRoutingSessions(mPackageName); |
hughchen | 6ab62ca | 2020-02-12 10:40:04 +0800 | [diff] [blame] | 145 | |
hughchen | 8e9e4b4 | 2020-03-23 18:59:58 +0800 | [diff] [blame] | 146 | return sessionInfos.get(sessionInfos.size() - 1); |
hughchen | 6ab62ca | 2020-02-12 10:40:04 +0800 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Remove a {@code device} from current media. |
| 151 | * |
| 152 | * @param device MediaDevice |
| 153 | * @return If device stop successful return {@code true}, otherwise return {@code false} |
| 154 | */ |
| 155 | boolean removeDeviceFromPlayMedia(MediaDevice device) { |
| 156 | if (TextUtils.isEmpty(mPackageName)) { |
| 157 | Log.w(TAG, "removeDeviceFromMedia() package name is null or empty!"); |
| 158 | return false; |
| 159 | } |
| 160 | |
| 161 | final RoutingSessionInfo info = getRoutingSessionInfo(); |
| 162 | if (info != null && info.getSelectedRoutes().contains(device.mRouteInfo.getId())) { |
Kyunglyul Hyun | fa6f06d | 2020-04-30 02:56:40 +0900 | [diff] [blame] | 163 | mRouterManager.deselectRoute(info, device.mRouteInfo); |
hughchen | 6ab62ca | 2020-02-12 10:40:04 +0800 | [diff] [blame] | 164 | return true; |
| 165 | } |
| 166 | |
| 167 | Log.w(TAG, "removeDeviceFromMedia() Ignoring deselecting a non-deselectable device : " |
| 168 | + device.getName()); |
| 169 | |
| 170 | return false; |
| 171 | } |
| 172 | |
| 173 | /** |
hughchen | 61bced0 | 2020-02-20 18:46:32 +0800 | [diff] [blame] | 174 | * Release session to stop playing media on MediaDevice. |
| 175 | */ |
| 176 | boolean releaseSession() { |
| 177 | if (TextUtils.isEmpty(mPackageName)) { |
| 178 | Log.w(TAG, "releaseSession() package name is null or empty!"); |
| 179 | return false; |
| 180 | } |
| 181 | |
hughchen | 8e9e4b4 | 2020-03-23 18:59:58 +0800 | [diff] [blame] | 182 | final RoutingSessionInfo sessionInfo = getRoutingSessionInfo(); |
hughchen | eed82d5 | 2020-03-20 14:54:10 +0800 | [diff] [blame] | 183 | |
| 184 | if (sessionInfo != null) { |
| 185 | mRouterManager.releaseSession(sessionInfo); |
hughchen | 61bced0 | 2020-02-20 18:46:32 +0800 | [diff] [blame] | 186 | return true; |
| 187 | } |
| 188 | |
| 189 | Log.w(TAG, "releaseSession() Ignoring release session : " + mPackageName); |
| 190 | |
| 191 | return false; |
| 192 | } |
| 193 | |
| 194 | /** |
hughchen | 6ab62ca | 2020-02-12 10:40:04 +0800 | [diff] [blame] | 195 | * Get the MediaDevice list that can be added to current media. |
| 196 | * |
| 197 | * @return list of MediaDevice |
| 198 | */ |
| 199 | List<MediaDevice> getSelectableMediaDevice() { |
| 200 | final List<MediaDevice> deviceList = new ArrayList<>(); |
| 201 | if (TextUtils.isEmpty(mPackageName)) { |
| 202 | Log.w(TAG, "getSelectableMediaDevice() package name is null or empty!"); |
| 203 | return deviceList; |
| 204 | } |
| 205 | |
| 206 | final RoutingSessionInfo info = getRoutingSessionInfo(); |
| 207 | if (info != null) { |
Kyunglyul Hyun | fa6f06d | 2020-04-30 02:56:40 +0900 | [diff] [blame] | 208 | for (MediaRoute2Info route : mRouterManager.getSelectableRoutes(info)) { |
hughchen | 6ab62ca | 2020-02-12 10:40:04 +0800 | [diff] [blame] | 209 | deviceList.add(new InfoMediaDevice(mContext, mRouterManager, |
| 210 | route, mPackageName)); |
| 211 | } |
| 212 | return deviceList; |
| 213 | } |
| 214 | |
| 215 | Log.w(TAG, "getSelectableMediaDevice() cannot found selectable MediaDevice from : " |
| 216 | + mPackageName); |
| 217 | |
| 218 | return deviceList; |
| 219 | } |
| 220 | |
| 221 | /** |
timhypeng | 6bbd555 | 2020-02-17 14:53:45 +0800 | [diff] [blame] | 222 | * Get the MediaDevice list that has been selected to current media. |
| 223 | * |
| 224 | * @return list of MediaDevice |
| 225 | */ |
| 226 | List<MediaDevice> getSelectedMediaDevice() { |
| 227 | final List<MediaDevice> deviceList = new ArrayList<>(); |
| 228 | if (TextUtils.isEmpty(mPackageName)) { |
| 229 | Log.w(TAG, "getSelectedMediaDevice() package name is null or empty!"); |
| 230 | return deviceList; |
| 231 | } |
| 232 | |
| 233 | final RoutingSessionInfo info = getRoutingSessionInfo(); |
| 234 | if (info != null) { |
Kyunglyul Hyun | fa6f06d | 2020-04-30 02:56:40 +0900 | [diff] [blame] | 235 | for (MediaRoute2Info route : mRouterManager.getSelectedRoutes(info)) { |
timhypeng | 6bbd555 | 2020-02-17 14:53:45 +0800 | [diff] [blame] | 236 | deviceList.add(new InfoMediaDevice(mContext, mRouterManager, |
| 237 | route, mPackageName)); |
| 238 | } |
| 239 | return deviceList; |
| 240 | } |
| 241 | |
| 242 | Log.w(TAG, "getSelectedMediaDevice() cannot found selectable MediaDevice from : " |
| 243 | + mPackageName); |
| 244 | |
| 245 | return deviceList; |
| 246 | } |
| 247 | |
Tim Peng | 8a0f361 | 2020-04-30 12:33:20 +0800 | [diff] [blame] | 248 | void adjustSessionVolume(RoutingSessionInfo info, int volume) { |
| 249 | if (info == null) { |
| 250 | Log.w(TAG, "Unable to adjust session volume. RoutingSessionInfo is empty"); |
| 251 | return; |
| 252 | } |
| 253 | |
| 254 | mRouterManager.setSessionVolume(info, volume); |
| 255 | } |
| 256 | |
timhypeng | 6bbd555 | 2020-02-17 14:53:45 +0800 | [diff] [blame] | 257 | /** |
hughchen | 6ab62ca | 2020-02-12 10:40:04 +0800 | [diff] [blame] | 258 | * Adjust the volume of {@link android.media.RoutingSessionInfo}. |
| 259 | * |
| 260 | * @param volume the value of volume |
| 261 | */ |
| 262 | void adjustSessionVolume(int volume) { |
| 263 | if (TextUtils.isEmpty(mPackageName)) { |
| 264 | Log.w(TAG, "adjustSessionVolume() package name is null or empty!"); |
| 265 | return; |
| 266 | } |
| 267 | |
| 268 | final RoutingSessionInfo info = getRoutingSessionInfo(); |
| 269 | if (info != null) { |
| 270 | Log.d(TAG, "adjustSessionVolume() adjust volume : " + volume + ", with : " |
| 271 | + mPackageName); |
| 272 | mRouterManager.setSessionVolume(info, volume); |
| 273 | return; |
| 274 | } |
| 275 | |
| 276 | Log.w(TAG, "adjustSessionVolume() can't found corresponding RoutingSession with : " |
| 277 | + mPackageName); |
| 278 | } |
| 279 | |
| 280 | /** |
| 281 | * Gets the maximum volume of the {@link android.media.RoutingSessionInfo}. |
| 282 | * |
| 283 | * @return maximum volume of the session, and return -1 if not found. |
| 284 | */ |
| 285 | public int getSessionVolumeMax() { |
| 286 | if (TextUtils.isEmpty(mPackageName)) { |
| 287 | Log.w(TAG, "getSessionVolumeMax() package name is null or empty!"); |
| 288 | return -1; |
| 289 | } |
| 290 | |
| 291 | final RoutingSessionInfo info = getRoutingSessionInfo(); |
| 292 | if (info != null) { |
| 293 | return info.getVolumeMax(); |
| 294 | } |
| 295 | |
| 296 | Log.w(TAG, "getSessionVolumeMax() can't found corresponding RoutingSession with : " |
| 297 | + mPackageName); |
| 298 | return -1; |
| 299 | } |
| 300 | |
| 301 | /** |
| 302 | * Gets the current volume of the {@link android.media.RoutingSessionInfo}. |
| 303 | * |
| 304 | * @return current volume of the session, and return -1 if not found. |
| 305 | */ |
| 306 | public int getSessionVolume() { |
| 307 | if (TextUtils.isEmpty(mPackageName)) { |
| 308 | Log.w(TAG, "getSessionVolume() package name is null or empty!"); |
| 309 | return -1; |
| 310 | } |
| 311 | |
| 312 | final RoutingSessionInfo info = getRoutingSessionInfo(); |
| 313 | if (info != null) { |
| 314 | return info.getVolume(); |
| 315 | } |
| 316 | |
| 317 | Log.w(TAG, "getSessionVolume() can't found corresponding RoutingSession with : " |
| 318 | + mPackageName); |
| 319 | return -1; |
| 320 | } |
| 321 | |
Tim Peng | 635b04a | 2020-03-05 13:08:02 +0800 | [diff] [blame] | 322 | CharSequence getSessionName() { |
| 323 | if (TextUtils.isEmpty(mPackageName)) { |
| 324 | Log.w(TAG, "Unable to get session name. The package name is null or empty!"); |
| 325 | return null; |
| 326 | } |
| 327 | |
| 328 | final RoutingSessionInfo info = getRoutingSessionInfo(); |
| 329 | if (info != null) { |
| 330 | return info.getName(); |
| 331 | } |
| 332 | |
| 333 | Log.w(TAG, "Unable to get session name for package: " + mPackageName); |
| 334 | return null; |
| 335 | } |
| 336 | |
hughchen | 0d20204 | 2020-01-17 18:30:03 +0800 | [diff] [blame] | 337 | private void refreshDevices() { |
| 338 | mMediaDevices.clear(); |
| 339 | mCurrentConnectedDevice = null; |
| 340 | if (TextUtils.isEmpty(mPackageName)) { |
| 341 | buildAllRoutes(); |
| 342 | } else { |
| 343 | buildAvailableRoutes(); |
hughchen | a73686f | 2018-11-12 17:42:08 +0800 | [diff] [blame] | 344 | } |
hughchen | 0d20204 | 2020-01-17 18:30:03 +0800 | [diff] [blame] | 345 | dispatchDeviceListAdded(); |
| 346 | } |
| 347 | |
| 348 | private void buildAllRoutes() { |
| 349 | for (MediaRoute2Info route : mRouterManager.getAllRoutes()) { |
hughchen | 063a35a | 2020-03-04 17:51:57 +0800 | [diff] [blame] | 350 | if (DEBUG) { |
hughchen | f961f2c | 2020-03-25 17:14:02 +0800 | [diff] [blame] | 351 | Log.d(TAG, "buildAllRoutes() route : " + route.getName() + ", volume : " |
hughchen | e8c79b0 | 2020-04-21 17:26:21 +0800 | [diff] [blame] | 352 | + route.getVolume() + ", type : " + route.getType()); |
hughchen | 063a35a | 2020-03-04 17:51:57 +0800 | [diff] [blame] | 353 | } |
hughchen | 61bced0 | 2020-02-20 18:46:32 +0800 | [diff] [blame] | 354 | if (route.isSystemRoute()) { |
| 355 | addMediaDevice(route); |
| 356 | } |
hughchen | 0d20204 | 2020-01-17 18:30:03 +0800 | [diff] [blame] | 357 | } |
| 358 | } |
| 359 | |
Tim Peng | 8a0f361 | 2020-04-30 12:33:20 +0800 | [diff] [blame] | 360 | List<RoutingSessionInfo> getActiveMediaSession() { |
| 361 | return mRouterManager.getActiveSessions(); |
| 362 | } |
| 363 | |
hughchen | 0d20204 | 2020-01-17 18:30:03 +0800 | [diff] [blame] | 364 | private void buildAvailableRoutes() { |
| 365 | for (MediaRoute2Info route : mRouterManager.getAvailableRoutes(mPackageName)) { |
hughchen | 063a35a | 2020-03-04 17:51:57 +0800 | [diff] [blame] | 366 | if (DEBUG) { |
hughchen | e8c79b0 | 2020-04-21 17:26:21 +0800 | [diff] [blame] | 367 | Log.d(TAG, "buildAvailableRoutes() route : " + route.getName() |
| 368 | + ", type : " + route.getType()); |
hughchen | 063a35a | 2020-03-04 17:51:57 +0800 | [diff] [blame] | 369 | } |
hughchen | 491b9fc | 2020-02-05 18:31:36 +0800 | [diff] [blame] | 370 | addMediaDevice(route); |
| 371 | } |
| 372 | } |
| 373 | |
hughchen | e8c79b0 | 2020-04-21 17:26:21 +0800 | [diff] [blame] | 374 | @VisibleForTesting |
| 375 | void addMediaDevice(MediaRoute2Info route) { |
hughchen | 6283741 | 2020-02-24 17:03:26 +0800 | [diff] [blame] | 376 | final int deviceType = route.getType(); |
hughchen | 491b9fc | 2020-02-05 18:31:36 +0800 | [diff] [blame] | 377 | MediaDevice mediaDevice = null; |
| 378 | switch (deviceType) { |
hughchen | 6283741 | 2020-02-24 17:03:26 +0800 | [diff] [blame] | 379 | case TYPE_UNKNOWN: |
| 380 | case TYPE_REMOTE_TV: |
| 381 | case TYPE_REMOTE_SPEAKER: |
| 382 | case TYPE_GROUP: |
hughchen | 491b9fc | 2020-02-05 18:31:36 +0800 | [diff] [blame] | 383 | //TODO(b/148765806): use correct device type once api is ready. |
hughchen | 6283741 | 2020-02-24 17:03:26 +0800 | [diff] [blame] | 384 | mediaDevice = new InfoMediaDevice(mContext, mRouterManager, route, |
| 385 | mPackageName); |
| 386 | if (!TextUtils.isEmpty(mPackageName) |
hughchen | 8e9e4b4 | 2020-03-23 18:59:58 +0800 | [diff] [blame] | 387 | && getRoutingSessionInfo().getSelectedRoutes().contains(route.getId()) |
hughchen | 063a35a | 2020-03-04 17:51:57 +0800 | [diff] [blame] | 388 | && mCurrentConnectedDevice == null) { |
hughchen | 6283741 | 2020-02-24 17:03:26 +0800 | [diff] [blame] | 389 | mCurrentConnectedDevice = mediaDevice; |
hughchen | 491b9fc | 2020-02-05 18:31:36 +0800 | [diff] [blame] | 390 | } |
| 391 | break; |
hughchen | 6283741 | 2020-02-24 17:03:26 +0800 | [diff] [blame] | 392 | case TYPE_BUILTIN_SPEAKER: |
hughchen | e8c79b0 | 2020-04-21 17:26:21 +0800 | [diff] [blame] | 393 | case TYPE_USB_DEVICE: |
| 394 | case TYPE_USB_HEADSET: |
| 395 | case TYPE_USB_ACCESSORY: |
| 396 | case TYPE_DOCK: |
| 397 | case TYPE_HDMI: |
hughchen | 6283741 | 2020-02-24 17:03:26 +0800 | [diff] [blame] | 398 | case TYPE_WIRED_HEADSET: |
| 399 | case TYPE_WIRED_HEADPHONES: |
| 400 | mediaDevice = |
| 401 | new PhoneMediaDevice(mContext, mRouterManager, route, mPackageName); |
hughchen | 491b9fc | 2020-02-05 18:31:36 +0800 | [diff] [blame] | 402 | break; |
hughchen | 6283741 | 2020-02-24 17:03:26 +0800 | [diff] [blame] | 403 | case TYPE_HEARING_AID: |
| 404 | case TYPE_BLUETOOTH_A2DP: |
hughchen | 491b9fc | 2020-02-05 18:31:36 +0800 | [diff] [blame] | 405 | final BluetoothDevice device = |
| 406 | BluetoothAdapter.getDefaultAdapter().getRemoteDevice(route.getOriginalId()); |
| 407 | final CachedBluetoothDevice cachedDevice = |
| 408 | mBluetoothManager.getCachedDeviceManager().findDevice(device); |
hughchen | 71a7f82 | 2020-04-24 14:59:05 +0800 | [diff] [blame] | 409 | if (cachedDevice != null) { |
| 410 | mediaDevice = new BluetoothMediaDevice(mContext, cachedDevice, mRouterManager, |
| 411 | route, mPackageName); |
| 412 | } |
hughchen | 491b9fc | 2020-02-05 18:31:36 +0800 | [diff] [blame] | 413 | break; |
| 414 | default: |
| 415 | Log.w(TAG, "addMediaDevice() unknown device type : " + deviceType); |
| 416 | break; |
| 417 | |
| 418 | } |
| 419 | |
| 420 | if (mediaDevice != null) { |
| 421 | mMediaDevices.add(mediaDevice); |
hughchen | 0d20204 | 2020-01-17 18:30:03 +0800 | [diff] [blame] | 422 | } |
| 423 | } |
| 424 | |
| 425 | class RouterManagerCallback extends MediaRouter2Manager.Callback { |
hughchen | a73686f | 2018-11-12 17:42:08 +0800 | [diff] [blame] | 426 | |
| 427 | @Override |
hughchen | e6a4f48 | 2019-10-18 17:34:15 +0800 | [diff] [blame] | 428 | public void onRoutesAdded(List<MediaRoute2Info> routes) { |
| 429 | refreshDevices(); |
| 430 | } |
| 431 | |
| 432 | @Override |
Kyunglyul Hyun | fa6f06d | 2020-04-30 02:56:40 +0900 | [diff] [blame] | 433 | public void onPreferredFeaturesChanged(String packageName, List<String> preferredFeatures) { |
hughchen | e6a4f48 | 2019-10-18 17:34:15 +0800 | [diff] [blame] | 434 | if (TextUtils.equals(mPackageName, packageName)) { |
| 435 | refreshDevices(); |
hughchen | a73686f | 2018-11-12 17:42:08 +0800 | [diff] [blame] | 436 | } |
| 437 | } |
hughchen | df7b838 | 2020-02-03 17:48:49 +0800 | [diff] [blame] | 438 | |
| 439 | @Override |
| 440 | public void onRoutesChanged(List<MediaRoute2Info> routes) { |
hughchen | f961f2c | 2020-03-25 17:14:02 +0800 | [diff] [blame] | 441 | refreshDevices(); |
hughchen | df7b838 | 2020-02-03 17:48:49 +0800 | [diff] [blame] | 442 | } |
hughchen | b3d68f6 | 2020-02-11 14:30:14 +0800 | [diff] [blame] | 443 | |
| 444 | @Override |
| 445 | public void onRoutesRemoved(List<MediaRoute2Info> routes) { |
| 446 | refreshDevices(); |
| 447 | } |
hughchen | 063a35a | 2020-03-04 17:51:57 +0800 | [diff] [blame] | 448 | |
| 449 | @Override |
| 450 | public void onTransferred(RoutingSessionInfo oldSession, RoutingSessionInfo newSession) { |
| 451 | if (DEBUG) { |
| 452 | Log.d(TAG, "onTransferred() oldSession : " + oldSession.getName() |
| 453 | + ", newSession : " + newSession.getName()); |
| 454 | } |
hughchen | f961f2c | 2020-03-25 17:14:02 +0800 | [diff] [blame] | 455 | mMediaDevices.clear(); |
| 456 | mCurrentConnectedDevice = null; |
| 457 | if (TextUtils.isEmpty(mPackageName)) { |
| 458 | buildAllRoutes(); |
| 459 | } else { |
| 460 | buildAvailableRoutes(); |
| 461 | } |
| 462 | |
| 463 | final String id = mCurrentConnectedDevice != null |
| 464 | ? mCurrentConnectedDevice.getId() |
| 465 | : null; |
| 466 | dispatchConnectedDeviceChanged(id); |
hughchen | 063a35a | 2020-03-04 17:51:57 +0800 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | @Override |
| 470 | public void onTransferFailed(RoutingSessionInfo session, MediaRoute2Info route) { |
| 471 | dispatchOnRequestFailed(REASON_UNKNOWN_ERROR); |
| 472 | } |
| 473 | |
| 474 | @Override |
| 475 | public void onRequestFailed(int reason) { |
| 476 | dispatchOnRequestFailed(reason); |
| 477 | } |
hughchen | a73686f | 2018-11-12 17:42:08 +0800 | [diff] [blame] | 478 | } |
| 479 | } |