Kyunglyul Hyun | 0332e9a2 | 2019-11-20 01:39:25 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 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 | |
| 17 | package com.android.server.media; |
| 18 | |
Hyundo Moon | 42bef14 | 2020-01-14 14:16:30 +0900 | [diff] [blame] | 19 | import static android.media.MediaRoute2Info.FEATURE_LIVE_AUDIO; |
| 20 | import static android.media.MediaRoute2Info.FEATURE_LIVE_VIDEO; |
Kyunglyul Hyun | 8828c89 | 2020-02-17 20:49:58 +0900 | [diff] [blame] | 21 | import static android.media.MediaRoute2Info.TYPE_BUILTIN_SPEAKER; |
Hyundo Moon | 42bef14 | 2020-01-14 14:16:30 +0900 | [diff] [blame] | 22 | |
Hyundo Moon | 717ef72 | 2020-02-06 18:44:13 +0900 | [diff] [blame] | 23 | import android.content.BroadcastReceiver; |
Kyunglyul Hyun | 0332e9a2 | 2019-11-20 01:39:25 +0000 | [diff] [blame] | 24 | import android.content.ComponentName; |
| 25 | import android.content.Context; |
| 26 | import android.content.Intent; |
Hyundo Moon | 717ef72 | 2020-02-06 18:44:13 +0900 | [diff] [blame] | 27 | import android.content.IntentFilter; |
Kyunglyul Hyun | 0332e9a2 | 2019-11-20 01:39:25 +0000 | [diff] [blame] | 28 | import android.media.AudioManager; |
| 29 | import android.media.AudioRoutesInfo; |
| 30 | import android.media.IAudioRoutesObserver; |
| 31 | import android.media.IAudioService; |
| 32 | import android.media.MediaRoute2Info; |
| 33 | import android.media.MediaRoute2ProviderInfo; |
Kyunglyul Hyun | 1866d8a | 2020-01-31 11:56:34 +0900 | [diff] [blame] | 34 | import android.media.RouteDiscoveryPreference; |
Hyundo Moon | 67c41fd | 2020-01-17 14:22:42 +0900 | [diff] [blame] | 35 | import android.media.RoutingSessionInfo; |
Hyundo Moon | 84e027d | 2020-01-16 17:39:05 +0900 | [diff] [blame] | 36 | import android.os.Bundle; |
Kyunglyul Hyun | 0332e9a2 | 2019-11-20 01:39:25 +0000 | [diff] [blame] | 37 | import android.os.Handler; |
| 38 | import android.os.Looper; |
| 39 | import android.os.RemoteException; |
| 40 | import android.os.ServiceManager; |
Hyundo Moon | 67c41fd | 2020-01-17 14:22:42 +0900 | [diff] [blame] | 41 | import android.text.TextUtils; |
Kyunglyul Hyun | 0332e9a2 | 2019-11-20 01:39:25 +0000 | [diff] [blame] | 42 | import android.util.Log; |
| 43 | |
| 44 | import com.android.internal.R; |
| 45 | |
Hyundo Moon | 67c41fd | 2020-01-17 14:22:42 +0900 | [diff] [blame] | 46 | import java.util.Objects; |
Kyunglyul Hyun | efe4374 | 2019-12-31 18:32:28 +0900 | [diff] [blame] | 47 | |
Kyunglyul Hyun | 0332e9a2 | 2019-11-20 01:39:25 +0000 | [diff] [blame] | 48 | /** |
| 49 | * Provides routes for local playbacks such as phone speaker, wired headset, or Bluetooth speakers. |
| 50 | */ |
Sungsoo Lim | ea1eaf7 | 2020-02-12 11:00:06 +0900 | [diff] [blame] | 51 | // TODO: check thread safety. We may need to use lock to protect variables. |
Kyunglyul Hyun | 0332e9a2 | 2019-11-20 01:39:25 +0000 | [diff] [blame] | 52 | class SystemMediaRoute2Provider extends MediaRoute2Provider { |
| 53 | private static final String TAG = "MR2SystemProvider"; |
| 54 | private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG); |
| 55 | |
Hyundo Moon | 5736a61 | 2019-11-19 15:08:32 +0900 | [diff] [blame] | 56 | static final String DEFAULT_ROUTE_ID = "DEFAULT_ROUTE"; |
Sungsoo Lim | ddf140d | 2020-03-25 08:53:55 +0900 | [diff] [blame] | 57 | static final String DEVICE_ROUTE_ID = "DEVICE_ROUTE"; |
Hyundo Moon | 67c41fd | 2020-01-17 14:22:42 +0900 | [diff] [blame] | 58 | static final String SYSTEM_SESSION_ID = "SYSTEM_SESSION"; |
Kyunglyul Hyun | 0332e9a2 | 2019-11-20 01:39:25 +0000 | [diff] [blame] | 59 | |
Kyunglyul Hyun | 0332e9a2 | 2019-11-20 01:39:25 +0000 | [diff] [blame] | 60 | private final AudioManager mAudioManager; |
| 61 | private final IAudioService mAudioService; |
| 62 | private final Handler mHandler; |
| 63 | private final Context mContext; |
Sungsoo Lim | bb3ee6e | 2019-12-23 17:47:24 +0900 | [diff] [blame] | 64 | private final BluetoothRouteProvider mBtRouteProvider; |
Kyunglyul Hyun | 0332e9a2 | 2019-11-20 01:39:25 +0000 | [diff] [blame] | 65 | |
| 66 | private static ComponentName sComponentName = new ComponentName( |
Kyunglyul Hyun | d9bb4d7 | 2020-02-18 15:05:26 +0900 | [diff] [blame] | 67 | SystemMediaRoute2Provider.class.getPackage().getName(), |
Kyunglyul Hyun | 0332e9a2 | 2019-11-20 01:39:25 +0000 | [diff] [blame] | 68 | SystemMediaRoute2Provider.class.getName()); |
| 69 | |
Kyunglyul Hyun | 5161b37 | 2020-02-05 18:45:35 +0900 | [diff] [blame] | 70 | private String mSelectedRouteId; |
Sungsoo Lim | ddf140d | 2020-03-25 08:53:55 +0900 | [diff] [blame] | 71 | // For apps without MODIFYING_AUDIO_ROUTING permission. |
| 72 | // This should be the currently selected route. |
Kyunglyul Hyun | 0332e9a2 | 2019-11-20 01:39:25 +0000 | [diff] [blame] | 73 | MediaRoute2Info mDefaultRoute; |
Sungsoo Lim | ddf140d | 2020-03-25 08:53:55 +0900 | [diff] [blame] | 74 | MediaRoute2Info mDeviceRoute; |
Kyunglyul Hyun | 0332e9a2 | 2019-11-20 01:39:25 +0000 | [diff] [blame] | 75 | final AudioRoutesInfo mCurAudioRoutesInfo = new AudioRoutesInfo(); |
| 76 | |
| 77 | final IAudioRoutesObserver.Stub mAudioRoutesObserver = new IAudioRoutesObserver.Stub() { |
| 78 | @Override |
| 79 | public void dispatchAudioRoutesChanged(final AudioRoutesInfo newRoutes) { |
Sungsoo Lim | ea1eaf7 | 2020-02-12 11:00:06 +0900 | [diff] [blame] | 80 | mHandler.post(() -> { |
Sungsoo Lim | ddf140d | 2020-03-25 08:53:55 +0900 | [diff] [blame] | 81 | updateDeviceRoute(newRoutes); |
Sungsoo Lim | ea1eaf7 | 2020-02-12 11:00:06 +0900 | [diff] [blame] | 82 | notifyProviderState(); |
Kyunglyul Hyun | 0332e9a2 | 2019-11-20 01:39:25 +0000 | [diff] [blame] | 83 | }); |
| 84 | } |
| 85 | }; |
| 86 | |
| 87 | SystemMediaRoute2Provider(Context context, Callback callback) { |
| 88 | super(sComponentName); |
| 89 | setCallback(callback); |
| 90 | |
Kyunglyul Hyun | 96d36ce | 2020-01-29 19:59:33 +0900 | [diff] [blame] | 91 | mIsSystemRouteProvider = true; |
| 92 | |
Kyunglyul Hyun | 0332e9a2 | 2019-11-20 01:39:25 +0000 | [diff] [blame] | 93 | mContext = context; |
| 94 | mHandler = new Handler(Looper.getMainLooper()); |
| 95 | |
| 96 | mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); |
| 97 | mAudioService = IAudioService.Stub.asInterface( |
| 98 | ServiceManager.getService(Context.AUDIO_SERVICE)); |
Sungsoo Lim | ea1eaf7 | 2020-02-12 11:00:06 +0900 | [diff] [blame] | 99 | AudioRoutesInfo newAudioRoutes = null; |
| 100 | try { |
| 101 | newAudioRoutes = mAudioService.startWatchingRoutes(mAudioRoutesObserver); |
| 102 | } catch (RemoteException e) { |
| 103 | } |
Sungsoo Lim | ddf140d | 2020-03-25 08:53:55 +0900 | [diff] [blame] | 104 | updateDeviceRoute(newAudioRoutes); |
Kyunglyul Hyun | 0332e9a2 | 2019-11-20 01:39:25 +0000 | [diff] [blame] | 105 | |
Roman Kiryanov | fe87df9 | 2020-03-27 17:40:27 -0700 | [diff] [blame] | 106 | // .getInstance returns null if there is no bt adapter available |
Sungsoo Lim | bb3ee6e | 2019-12-23 17:47:24 +0900 | [diff] [blame] | 107 | mBtRouteProvider = BluetoothRouteProvider.getInstance(context, (routes) -> { |
Sungsoo Lim | ea1eaf7 | 2020-02-12 11:00:06 +0900 | [diff] [blame] | 108 | publishProviderState(); |
Hyundo Moon | 67c41fd | 2020-01-17 14:22:42 +0900 | [diff] [blame] | 109 | |
| 110 | boolean sessionInfoChanged; |
Sungsoo Lim | 613a77a | 2020-03-16 15:44:51 +0900 | [diff] [blame] | 111 | sessionInfoChanged = updateSessionInfosIfNeeded(); |
Hyundo Moon | 67c41fd | 2020-01-17 14:22:42 +0900 | [diff] [blame] | 112 | if (sessionInfoChanged) { |
| 113 | notifySessionInfoUpdated(); |
| 114 | } |
Sungsoo Lim | bb3ee6e | 2019-12-23 17:47:24 +0900 | [diff] [blame] | 115 | }); |
Sungsoo Lim | 613a77a | 2020-03-16 15:44:51 +0900 | [diff] [blame] | 116 | updateSessionInfosIfNeeded(); |
Hyundo Moon | 717ef72 | 2020-02-06 18:44:13 +0900 | [diff] [blame] | 117 | mContext.registerReceiver(new VolumeChangeReceiver(), |
| 118 | new IntentFilter(AudioManager.VOLUME_CHANGED_ACTION)); |
Sungsoo Lim | 613a77a | 2020-03-16 15:44:51 +0900 | [diff] [blame] | 119 | |
Roman Kiryanov | fe87df9 | 2020-03-27 17:40:27 -0700 | [diff] [blame] | 120 | if (mBtRouteProvider != null) { |
| 121 | mHandler.post(() -> { |
| 122 | mBtRouteProvider.start(); |
| 123 | notifyProviderState(); |
| 124 | }); |
| 125 | } |
Kyunglyul Hyun | 0332e9a2 | 2019-11-20 01:39:25 +0000 | [diff] [blame] | 126 | } |
| 127 | |
Hyundo Moon | 63a0540 | 2019-12-19 20:13:56 +0900 | [diff] [blame] | 128 | @Override |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 129 | public void requestCreateSession(long requestId, String packageName, String routeId, |
Hyundo Moon | 84e027d | 2020-01-16 17:39:05 +0900 | [diff] [blame] | 130 | Bundle sessionHints) { |
Kyunglyul Hyun | 923ef0d | 2020-03-13 20:55:11 +0900 | [diff] [blame] | 131 | |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 132 | transferToRoute(requestId, SYSTEM_SESSION_ID, routeId); |
Kyunglyul Hyun | 923ef0d | 2020-03-13 20:55:11 +0900 | [diff] [blame] | 133 | mCallback.onSessionCreated(this, requestId, mSessionInfos.get(0)); |
| 134 | //TODO: We should call after the session info is changed. |
Hyundo Moon | 63a0540 | 2019-12-19 20:13:56 +0900 | [diff] [blame] | 135 | } |
| 136 | |
Kyunglyul Hyun | 0332e9a2 | 2019-11-20 01:39:25 +0000 | [diff] [blame] | 137 | @Override |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 138 | public void releaseSession(long requestId, String sessionId) { |
Kyunglyul Hyun | cb8894d | 2019-12-27 14:24:46 +0900 | [diff] [blame] | 139 | // Do nothing |
Kyunglyul Hyun | 0332e9a2 | 2019-11-20 01:39:25 +0000 | [diff] [blame] | 140 | } |
Hyundo Moon | 0fa60e8 | 2020-02-14 11:44:45 +0900 | [diff] [blame] | 141 | |
Kyunglyul Hyun | 1866d8a | 2020-01-31 11:56:34 +0900 | [diff] [blame] | 142 | @Override |
| 143 | public void updateDiscoveryPreference(RouteDiscoveryPreference discoveryPreference) { |
| 144 | // Do nothing |
| 145 | } |
Kyunglyul Hyun | 0332e9a2 | 2019-11-20 01:39:25 +0000 | [diff] [blame] | 146 | |
Kyunglyul Hyun | 0332e9a2 | 2019-11-20 01:39:25 +0000 | [diff] [blame] | 147 | @Override |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 148 | public void selectRoute(long requestId, String sessionId, String routeId) { |
Kyunglyul Hyun | 581fc98 | 2020-01-21 16:30:28 +0900 | [diff] [blame] | 149 | // Do nothing since we don't support multiple BT yet. |
Kyunglyul Hyun | cb8894d | 2019-12-27 14:24:46 +0900 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | @Override |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 153 | public void deselectRoute(long requestId, String sessionId, String routeId) { |
Kyunglyul Hyun | 581fc98 | 2020-01-21 16:30:28 +0900 | [diff] [blame] | 154 | // Do nothing since we don't support multiple BT yet. |
Kyunglyul Hyun | cb8894d | 2019-12-27 14:24:46 +0900 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | @Override |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 158 | public void transferToRoute(long requestId, String sessionId, String routeId) { |
Roman Kiryanov | fe87df9 | 2020-03-27 17:40:27 -0700 | [diff] [blame] | 159 | if (mBtRouteProvider != null) { |
| 160 | if (TextUtils.equals(routeId, mDeviceRoute.getId())) { |
| 161 | mBtRouteProvider.transferTo(null); |
| 162 | } else { |
| 163 | mBtRouteProvider.transferTo(routeId); |
| 164 | } |
Kyunglyul Hyun | 581fc98 | 2020-01-21 16:30:28 +0900 | [diff] [blame] | 165 | } |
Kyunglyul Hyun | 0332e9a2 | 2019-11-20 01:39:25 +0000 | [diff] [blame] | 166 | } |
| 167 | |
Kyunglyul Hyun | 0332e9a2 | 2019-11-20 01:39:25 +0000 | [diff] [blame] | 168 | @Override |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 169 | public void setRouteVolume(long requestId, String routeId, int volume) { |
Kyunglyul Hyun | 5161b37 | 2020-02-05 18:45:35 +0900 | [diff] [blame] | 170 | if (!TextUtils.equals(routeId, mSelectedRouteId)) { |
| 171 | return; |
| 172 | } |
| 173 | mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, volume, 0); |
| 174 | } |
| 175 | |
| 176 | @Override |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 177 | public void setSessionVolume(long requestId, String sessionId, int volume) { |
Kyunglyul Hyun | 5161b37 | 2020-02-05 18:45:35 +0900 | [diff] [blame] | 178 | // Do nothing since we don't support grouping volume yet. |
Kyunglyul Hyun | 0332e9a2 | 2019-11-20 01:39:25 +0000 | [diff] [blame] | 179 | } |
| 180 | |
Sungsoo Lim | ddf140d | 2020-03-25 08:53:55 +0900 | [diff] [blame] | 181 | public MediaRoute2Info getDefaultRoute() { |
| 182 | return mDefaultRoute; |
| 183 | } |
| 184 | |
| 185 | private void updateDeviceRoute(AudioRoutesInfo newRoutes) { |
Kyunglyul Hyun | 0332e9a2 | 2019-11-20 01:39:25 +0000 | [diff] [blame] | 186 | int name = R.string.default_audio_route_name; |
Sungsoo Lim | ea1eaf7 | 2020-02-12 11:00:06 +0900 | [diff] [blame] | 187 | if (newRoutes != null) { |
| 188 | mCurAudioRoutesInfo.mainType = newRoutes.mainType; |
| 189 | if ((newRoutes.mainType & AudioRoutesInfo.MAIN_HEADPHONES) != 0 |
| 190 | || (newRoutes.mainType & AudioRoutesInfo.MAIN_HEADSET) != 0) { |
| 191 | name = com.android.internal.R.string.default_audio_route_name_headphones; |
| 192 | } else if ((newRoutes.mainType & AudioRoutesInfo.MAIN_DOCK_SPEAKERS) != 0) { |
| 193 | name = com.android.internal.R.string.default_audio_route_name_dock_speakers; |
| 194 | } else if ((newRoutes.mainType & AudioRoutesInfo.MAIN_HDMI) != 0) { |
| 195 | name = com.android.internal.R.string.default_audio_route_name_hdmi; |
| 196 | } else if ((newRoutes.mainType & AudioRoutesInfo.MAIN_USB) != 0) { |
| 197 | name = com.android.internal.R.string.default_audio_route_name_usb; |
| 198 | } |
Kyunglyul Hyun | 0332e9a2 | 2019-11-20 01:39:25 +0000 | [diff] [blame] | 199 | } |
Sungsoo Lim | ddf140d | 2020-03-25 08:53:55 +0900 | [diff] [blame] | 200 | mDeviceRoute = new MediaRoute2Info.Builder( |
| 201 | DEVICE_ROUTE_ID, mContext.getResources().getText(name).toString()) |
Kyunglyul Hyun | 0332e9a2 | 2019-11-20 01:39:25 +0000 | [diff] [blame] | 202 | .setVolumeHandling(mAudioManager.isVolumeFixed() |
| 203 | ? MediaRoute2Info.PLAYBACK_VOLUME_FIXED |
| 204 | : MediaRoute2Info.PLAYBACK_VOLUME_VARIABLE) |
| 205 | .setVolumeMax(mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC)) |
| 206 | .setVolume(mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC)) |
Kyunglyul Hyun | 8828c89 | 2020-02-17 20:49:58 +0900 | [diff] [blame] | 207 | //TODO: Guess the exact type using AudioDevice |
| 208 | .setType(TYPE_BUILTIN_SPEAKER) |
Hyundo Moon | 42bef14 | 2020-01-14 14:16:30 +0900 | [diff] [blame] | 209 | .addFeature(FEATURE_LIVE_AUDIO) |
| 210 | .addFeature(FEATURE_LIVE_VIDEO) |
Sungsoo Lim | ea1eaf7 | 2020-02-12 11:00:06 +0900 | [diff] [blame] | 211 | .setConnectionState(MediaRoute2Info.CONNECTION_STATE_CONNECTED) |
Kyunglyul Hyun | 0332e9a2 | 2019-11-20 01:39:25 +0000 | [diff] [blame] | 212 | .build(); |
Sungsoo Lim | ea1eaf7 | 2020-02-12 11:00:06 +0900 | [diff] [blame] | 213 | updateProviderState(); |
| 214 | } |
Kyunglyul Hyun | 0332e9a2 | 2019-11-20 01:39:25 +0000 | [diff] [blame] | 215 | |
Sungsoo Lim | ea1eaf7 | 2020-02-12 11:00:06 +0900 | [diff] [blame] | 216 | private void updateProviderState() { |
| 217 | MediaRoute2ProviderInfo.Builder builder = new MediaRoute2ProviderInfo.Builder(); |
Sungsoo Lim | ddf140d | 2020-03-25 08:53:55 +0900 | [diff] [blame] | 218 | builder.addRoute(mDeviceRoute); |
Sungsoo Lim | ea1eaf7 | 2020-02-12 11:00:06 +0900 | [diff] [blame] | 219 | if (mBtRouteProvider != null) { |
| 220 | for (MediaRoute2Info route : mBtRouteProvider.getAllBluetoothRoutes()) { |
| 221 | builder.addRoute(route); |
| 222 | } |
| 223 | } |
| 224 | setProviderState(builder.build()); |
Kyunglyul Hyun | 0332e9a2 | 2019-11-20 01:39:25 +0000 | [diff] [blame] | 225 | } |
Hyundo Moon | 5736a61 | 2019-11-19 15:08:32 +0900 | [diff] [blame] | 226 | |
| 227 | /** |
Hyundo Moon | 67c41fd | 2020-01-17 14:22:42 +0900 | [diff] [blame] | 228 | * Updates the mSessionInfo. Returns true if the session info is changed. |
| 229 | */ |
Sungsoo Lim | 613a77a | 2020-03-16 15:44:51 +0900 | [diff] [blame] | 230 | boolean updateSessionInfosIfNeeded() { |
| 231 | synchronized (mLock) { |
| 232 | // Prevent to execute this method before mBtRouteProvider is created. |
| 233 | if (mBtRouteProvider == null) return false; |
| 234 | RoutingSessionInfo oldSessionInfo = mSessionInfos.isEmpty() ? null : mSessionInfos.get( |
| 235 | 0); |
Hyundo Moon | 67c41fd | 2020-01-17 14:22:42 +0900 | [diff] [blame] | 236 | |
Sungsoo Lim | 613a77a | 2020-03-16 15:44:51 +0900 | [diff] [blame] | 237 | RoutingSessionInfo.Builder builder = new RoutingSessionInfo.Builder( |
| 238 | SYSTEM_SESSION_ID, "" /* clientPackageName */) |
| 239 | .setSystemSession(true); |
Hyundo Moon | 67c41fd | 2020-01-17 14:22:42 +0900 | [diff] [blame] | 240 | |
Sungsoo Lim | 613a77a | 2020-03-16 15:44:51 +0900 | [diff] [blame] | 241 | MediaRoute2Info selectedRoute = mBtRouteProvider.getSelectedRoute(); |
| 242 | if (selectedRoute == null) { |
Sungsoo Lim | ddf140d | 2020-03-25 08:53:55 +0900 | [diff] [blame] | 243 | selectedRoute = mDeviceRoute; |
Sungsoo Lim | 613a77a | 2020-03-16 15:44:51 +0900 | [diff] [blame] | 244 | } else { |
Sungsoo Lim | ddf140d | 2020-03-25 08:53:55 +0900 | [diff] [blame] | 245 | builder.addTransferableRoute(mDeviceRoute.getId()); |
Sungsoo Lim | 613a77a | 2020-03-16 15:44:51 +0900 | [diff] [blame] | 246 | } |
| 247 | mSelectedRouteId = selectedRoute.getId(); |
Sungsoo Lim | ddf140d | 2020-03-25 08:53:55 +0900 | [diff] [blame] | 248 | mDefaultRoute = new MediaRoute2Info.Builder(DEFAULT_ROUTE_ID, selectedRoute).build(); |
Sungsoo Lim | 613a77a | 2020-03-16 15:44:51 +0900 | [diff] [blame] | 249 | builder.addSelectedRoute(mSelectedRouteId); |
Hyundo Moon | 67c41fd | 2020-01-17 14:22:42 +0900 | [diff] [blame] | 250 | |
Sungsoo Lim | 613a77a | 2020-03-16 15:44:51 +0900 | [diff] [blame] | 251 | for (MediaRoute2Info route : mBtRouteProvider.getTransferableRoutes()) { |
| 252 | builder.addTransferableRoute(route.getId()); |
| 253 | } |
Kyunglyul Hyun | 581fc98 | 2020-01-21 16:30:28 +0900 | [diff] [blame] | 254 | |
Sungsoo Lim | 613a77a | 2020-03-16 15:44:51 +0900 | [diff] [blame] | 255 | RoutingSessionInfo newSessionInfo = builder.setProviderId(mUniqueId).build(); |
| 256 | if (Objects.equals(oldSessionInfo, newSessionInfo)) { |
| 257 | return false; |
| 258 | } else { |
| 259 | mSessionInfos.clear(); |
| 260 | mSessionInfos.add(newSessionInfo); |
| 261 | return true; |
| 262 | } |
Hyundo Moon | 67c41fd | 2020-01-17 14:22:42 +0900 | [diff] [blame] | 263 | } |
| 264 | } |
| 265 | |
Sungsoo Lim | ea1eaf7 | 2020-02-12 11:00:06 +0900 | [diff] [blame] | 266 | void publishProviderState() { |
| 267 | updateProviderState(); |
| 268 | notifyProviderState(); |
Kyunglyul Hyun | 0332e9a2 | 2019-11-20 01:39:25 +0000 | [diff] [blame] | 269 | } |
Hyundo Moon | 67c41fd | 2020-01-17 14:22:42 +0900 | [diff] [blame] | 270 | |
| 271 | void notifySessionInfoUpdated() { |
| 272 | RoutingSessionInfo sessionInfo; |
| 273 | synchronized (mLock) { |
| 274 | sessionInfo = mSessionInfos.get(0); |
| 275 | } |
Kyunglyul Hyun | 923ef0d | 2020-03-13 20:55:11 +0900 | [diff] [blame] | 276 | |
Hyundo Moon | 67c41fd | 2020-01-17 14:22:42 +0900 | [diff] [blame] | 277 | mCallback.onSessionUpdated(this, sessionInfo); |
| 278 | } |
Hyundo Moon | 717ef72 | 2020-02-06 18:44:13 +0900 | [diff] [blame] | 279 | |
| 280 | private class VolumeChangeReceiver extends BroadcastReceiver { |
| 281 | // This will be called in the main thread. |
| 282 | @Override |
| 283 | public void onReceive(Context context, Intent intent) { |
| 284 | if (!intent.getAction().equals(AudioManager.VOLUME_CHANGED_ACTION)) { |
| 285 | return; |
| 286 | } |
| 287 | |
| 288 | final int streamType = intent.getIntExtra(AudioManager.EXTRA_VOLUME_STREAM_TYPE, -1); |
| 289 | if (streamType != AudioManager.STREAM_MUSIC) { |
| 290 | return; |
| 291 | } |
| 292 | |
| 293 | final int newVolume = intent.getIntExtra(AudioManager.EXTRA_VOLUME_STREAM_VALUE, 0); |
| 294 | final int oldVolume = intent.getIntExtra( |
| 295 | AudioManager.EXTRA_PREV_VOLUME_STREAM_VALUE, 0); |
| 296 | |
| 297 | if (newVolume != oldVolume) { |
Sungsoo Lim | ddf140d | 2020-03-25 08:53:55 +0900 | [diff] [blame] | 298 | if (TextUtils.equals(mDeviceRoute.getId(), mSelectedRouteId)) { |
| 299 | mDeviceRoute = new MediaRoute2Info.Builder(mDeviceRoute) |
Hyundo Moon | 717ef72 | 2020-02-06 18:44:13 +0900 | [diff] [blame] | 300 | .setVolume(newVolume) |
| 301 | .build(); |
Roman Kiryanov | fe87df9 | 2020-03-27 17:40:27 -0700 | [diff] [blame] | 302 | } else if (mBtRouteProvider != null) { |
Sungsoo Lim | ea1eaf7 | 2020-02-12 11:00:06 +0900 | [diff] [blame] | 303 | mBtRouteProvider.setSelectedRouteVolume(newVolume); |
Hyundo Moon | 717ef72 | 2020-02-06 18:44:13 +0900 | [diff] [blame] | 304 | } |
Sungsoo Lim | ea1eaf7 | 2020-02-12 11:00:06 +0900 | [diff] [blame] | 305 | publishProviderState(); |
Hyundo Moon | 717ef72 | 2020-02-06 18:44:13 +0900 | [diff] [blame] | 306 | } |
| 307 | } |
| 308 | } |
Kyunglyul Hyun | 0332e9a2 | 2019-11-20 01:39:25 +0000 | [diff] [blame] | 309 | } |