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 | c036350 | 2020-06-19 13:33:16 +0900 | [diff] [blame] | 21 | import static android.media.MediaRoute2Info.FEATURE_LOCAL_PLAYBACK; |
Kyunglyul Hyun | 8828c89 | 2020-02-17 20:49:58 +0900 | [diff] [blame] | 22 | import static android.media.MediaRoute2Info.TYPE_BUILTIN_SPEAKER; |
Kyunglyul Hyun | 1080caf | 2020-04-20 13:48:15 +0900 | [diff] [blame] | 23 | import static android.media.MediaRoute2Info.TYPE_DOCK; |
| 24 | import static android.media.MediaRoute2Info.TYPE_HDMI; |
| 25 | import static android.media.MediaRoute2Info.TYPE_USB_DEVICE; |
| 26 | import static android.media.MediaRoute2Info.TYPE_WIRED_HEADPHONES; |
| 27 | import static android.media.MediaRoute2Info.TYPE_WIRED_HEADSET; |
Hyundo Moon | 42bef14 | 2020-01-14 14:16:30 +0900 | [diff] [blame] | 28 | |
Hyundo Moon | 717ef72 | 2020-02-06 18:44:13 +0900 | [diff] [blame] | 29 | import android.content.BroadcastReceiver; |
Kyunglyul Hyun | 0332e9a2 | 2019-11-20 01:39:25 +0000 | [diff] [blame] | 30 | import android.content.ComponentName; |
| 31 | import android.content.Context; |
| 32 | import android.content.Intent; |
Hyundo Moon | 717ef72 | 2020-02-06 18:44:13 +0900 | [diff] [blame] | 33 | import android.content.IntentFilter; |
Kyunglyul Hyun | 0332e9a2 | 2019-11-20 01:39:25 +0000 | [diff] [blame] | 34 | import android.media.AudioManager; |
| 35 | import android.media.AudioRoutesInfo; |
| 36 | import android.media.IAudioRoutesObserver; |
| 37 | import android.media.IAudioService; |
| 38 | import android.media.MediaRoute2Info; |
| 39 | import android.media.MediaRoute2ProviderInfo; |
Kyunglyul Hyun | 22d4e05 | 2020-04-21 10:10:48 +0900 | [diff] [blame] | 40 | import android.media.MediaRoute2ProviderService; |
Kyunglyul Hyun | 1866d8a | 2020-01-31 11:56:34 +0900 | [diff] [blame] | 41 | import android.media.RouteDiscoveryPreference; |
Hyundo Moon | 67c41fd | 2020-01-17 14:22:42 +0900 | [diff] [blame] | 42 | import android.media.RoutingSessionInfo; |
Hyundo Moon | 84e027d | 2020-01-16 17:39:05 +0900 | [diff] [blame] | 43 | import android.os.Bundle; |
Kyunglyul Hyun | 0332e9a2 | 2019-11-20 01:39:25 +0000 | [diff] [blame] | 44 | import android.os.Handler; |
| 45 | import android.os.Looper; |
| 46 | import android.os.RemoteException; |
| 47 | import android.os.ServiceManager; |
Hyundo Moon | 67c41fd | 2020-01-17 14:22:42 +0900 | [diff] [blame] | 48 | import android.text.TextUtils; |
Kyunglyul Hyun | a464837 | 2020-06-15 16:52:14 +0900 | [diff] [blame] | 49 | import android.util.Log; |
Kyunglyul Hyun | 5ff55ec | 2020-05-18 16:37:43 +0900 | [diff] [blame] | 50 | import android.util.Slog; |
Kyunglyul Hyun | 0332e9a2 | 2019-11-20 01:39:25 +0000 | [diff] [blame] | 51 | |
| 52 | import com.android.internal.R; |
Kyunglyul Hyun | 22d4e05 | 2020-04-21 10:10:48 +0900 | [diff] [blame] | 53 | import com.android.internal.annotations.GuardedBy; |
Kyunglyul Hyun | 0332e9a2 | 2019-11-20 01:39:25 +0000 | [diff] [blame] | 54 | |
Hyundo Moon | 67c41fd | 2020-01-17 14:22:42 +0900 | [diff] [blame] | 55 | import java.util.Objects; |
Kyunglyul Hyun | efe4374 | 2019-12-31 18:32:28 +0900 | [diff] [blame] | 56 | |
Kyunglyul Hyun | 0332e9a2 | 2019-11-20 01:39:25 +0000 | [diff] [blame] | 57 | /** |
| 58 | * Provides routes for local playbacks such as phone speaker, wired headset, or Bluetooth speakers. |
| 59 | */ |
Sungsoo Lim | ea1eaf7 | 2020-02-12 11:00:06 +0900 | [diff] [blame] | 60 | // 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] | 61 | class SystemMediaRoute2Provider extends MediaRoute2Provider { |
| 62 | private static final String TAG = "MR2SystemProvider"; |
Kyunglyul Hyun | a464837 | 2020-06-15 16:52:14 +0900 | [diff] [blame] | 63 | private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG); |
Kyunglyul Hyun | 0332e9a2 | 2019-11-20 01:39:25 +0000 | [diff] [blame] | 64 | |
Hyundo Moon | 5736a61 | 2019-11-19 15:08:32 +0900 | [diff] [blame] | 65 | static final String DEFAULT_ROUTE_ID = "DEFAULT_ROUTE"; |
Sungsoo Lim | ddf140d | 2020-03-25 08:53:55 +0900 | [diff] [blame] | 66 | static final String DEVICE_ROUTE_ID = "DEVICE_ROUTE"; |
Hyundo Moon | 67c41fd | 2020-01-17 14:22:42 +0900 | [diff] [blame] | 67 | static final String SYSTEM_SESSION_ID = "SYSTEM_SESSION"; |
Kyunglyul Hyun | 0332e9a2 | 2019-11-20 01:39:25 +0000 | [diff] [blame] | 68 | |
Kyunglyul Hyun | 0332e9a2 | 2019-11-20 01:39:25 +0000 | [diff] [blame] | 69 | private final AudioManager mAudioManager; |
| 70 | private final IAudioService mAudioService; |
| 71 | private final Handler mHandler; |
| 72 | private final Context mContext; |
Sungsoo Lim | bb3ee6e | 2019-12-23 17:47:24 +0900 | [diff] [blame] | 73 | private final BluetoothRouteProvider mBtRouteProvider; |
Kyunglyul Hyun | 0332e9a2 | 2019-11-20 01:39:25 +0000 | [diff] [blame] | 74 | |
| 75 | private static ComponentName sComponentName = new ComponentName( |
Kyunglyul Hyun | d9bb4d7 | 2020-02-18 15:05:26 +0900 | [diff] [blame] | 76 | SystemMediaRoute2Provider.class.getPackage().getName(), |
Kyunglyul Hyun | 0332e9a2 | 2019-11-20 01:39:25 +0000 | [diff] [blame] | 77 | SystemMediaRoute2Provider.class.getName()); |
| 78 | |
Kyunglyul Hyun | 5161b37 | 2020-02-05 18:45:35 +0900 | [diff] [blame] | 79 | private String mSelectedRouteId; |
Sungsoo Lim | ddf140d | 2020-03-25 08:53:55 +0900 | [diff] [blame] | 80 | // For apps without MODIFYING_AUDIO_ROUTING permission. |
| 81 | // This should be the currently selected route. |
Kyunglyul Hyun | 0332e9a2 | 2019-11-20 01:39:25 +0000 | [diff] [blame] | 82 | MediaRoute2Info mDefaultRoute; |
Sungsoo Lim | ddf140d | 2020-03-25 08:53:55 +0900 | [diff] [blame] | 83 | MediaRoute2Info mDeviceRoute; |
Sungsoo Lim | c27785a | 2020-03-27 16:57:47 +0900 | [diff] [blame] | 84 | RoutingSessionInfo mDefaultSessionInfo; |
Kyunglyul Hyun | 0332e9a2 | 2019-11-20 01:39:25 +0000 | [diff] [blame] | 85 | final AudioRoutesInfo mCurAudioRoutesInfo = new AudioRoutesInfo(); |
Kyunglyul Hyun | f8f170c | 2020-05-13 21:31:26 +0900 | [diff] [blame] | 86 | int mDeviceVolume; |
Kyunglyul Hyun | 0332e9a2 | 2019-11-20 01:39:25 +0000 | [diff] [blame] | 87 | |
Kyunglyul Hyun | 22d4e05 | 2020-04-21 10:10:48 +0900 | [diff] [blame] | 88 | private final Object mRequestLock = new Object(); |
| 89 | @GuardedBy("mRequestLock") |
| 90 | private volatile SessionCreationRequest mPendingSessionCreationRequest; |
| 91 | |
Kyunglyul Hyun | 0332e9a2 | 2019-11-20 01:39:25 +0000 | [diff] [blame] | 92 | final IAudioRoutesObserver.Stub mAudioRoutesObserver = new IAudioRoutesObserver.Stub() { |
| 93 | @Override |
| 94 | public void dispatchAudioRoutesChanged(final AudioRoutesInfo newRoutes) { |
Sungsoo Lim | ea1eaf7 | 2020-02-12 11:00:06 +0900 | [diff] [blame] | 95 | mHandler.post(() -> { |
Sungsoo Lim | ddf140d | 2020-03-25 08:53:55 +0900 | [diff] [blame] | 96 | updateDeviceRoute(newRoutes); |
Sungsoo Lim | ea1eaf7 | 2020-02-12 11:00:06 +0900 | [diff] [blame] | 97 | notifyProviderState(); |
Kyunglyul Hyun | 0332e9a2 | 2019-11-20 01:39:25 +0000 | [diff] [blame] | 98 | }); |
| 99 | } |
| 100 | }; |
| 101 | |
Hyundo Moon | aa80375 | 2020-06-17 23:02:49 +0900 | [diff] [blame] | 102 | SystemMediaRoute2Provider(Context context) { |
Kyunglyul Hyun | 0332e9a2 | 2019-11-20 01:39:25 +0000 | [diff] [blame] | 103 | super(sComponentName); |
Kyunglyul Hyun | 0332e9a2 | 2019-11-20 01:39:25 +0000 | [diff] [blame] | 104 | |
Kyunglyul Hyun | 96d36ce | 2020-01-29 19:59:33 +0900 | [diff] [blame] | 105 | mIsSystemRouteProvider = true; |
Kyunglyul Hyun | 0332e9a2 | 2019-11-20 01:39:25 +0000 | [diff] [blame] | 106 | mContext = context; |
| 107 | mHandler = new Handler(Looper.getMainLooper()); |
| 108 | |
| 109 | mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); |
| 110 | mAudioService = IAudioService.Stub.asInterface( |
| 111 | ServiceManager.getService(Context.AUDIO_SERVICE)); |
Sungsoo Lim | ea1eaf7 | 2020-02-12 11:00:06 +0900 | [diff] [blame] | 112 | AudioRoutesInfo newAudioRoutes = null; |
| 113 | try { |
| 114 | newAudioRoutes = mAudioService.startWatchingRoutes(mAudioRoutesObserver); |
| 115 | } catch (RemoteException e) { |
| 116 | } |
Sungsoo Lim | ddf140d | 2020-03-25 08:53:55 +0900 | [diff] [blame] | 117 | updateDeviceRoute(newAudioRoutes); |
Kyunglyul Hyun | 0332e9a2 | 2019-11-20 01:39:25 +0000 | [diff] [blame] | 118 | |
Roman Kiryanov | fe87df9 | 2020-03-27 17:40:27 -0700 | [diff] [blame] | 119 | // .getInstance returns null if there is no bt adapter available |
Sungsoo Lim | bb3ee6e | 2019-12-23 17:47:24 +0900 | [diff] [blame] | 120 | mBtRouteProvider = BluetoothRouteProvider.getInstance(context, (routes) -> { |
Sungsoo Lim | ea1eaf7 | 2020-02-12 11:00:06 +0900 | [diff] [blame] | 121 | publishProviderState(); |
Hyundo Moon | 67c41fd | 2020-01-17 14:22:42 +0900 | [diff] [blame] | 122 | |
| 123 | boolean sessionInfoChanged; |
Sungsoo Lim | 613a77a | 2020-03-16 15:44:51 +0900 | [diff] [blame] | 124 | sessionInfoChanged = updateSessionInfosIfNeeded(); |
Hyundo Moon | 67c41fd | 2020-01-17 14:22:42 +0900 | [diff] [blame] | 125 | if (sessionInfoChanged) { |
| 126 | notifySessionInfoUpdated(); |
| 127 | } |
Sungsoo Lim | bb3ee6e | 2019-12-23 17:47:24 +0900 | [diff] [blame] | 128 | }); |
Sungsoo Lim | 613a77a | 2020-03-16 15:44:51 +0900 | [diff] [blame] | 129 | updateSessionInfosIfNeeded(); |
Sungsoo Lim | c27785a | 2020-03-27 16:57:47 +0900 | [diff] [blame] | 130 | |
Kyunglyul Hyun | f8f170c | 2020-05-13 21:31:26 +0900 | [diff] [blame] | 131 | IntentFilter intentFilter = new IntentFilter(AudioManager.VOLUME_CHANGED_ACTION); |
| 132 | intentFilter.addAction(AudioManager.STREAM_DEVICES_CHANGED_ACTION); |
| 133 | mContext.registerReceiver(new AudioManagerBroadcastReceiver(), intentFilter); |
Sungsoo Lim | 613a77a | 2020-03-16 15:44:51 +0900 | [diff] [blame] | 134 | |
Roman Kiryanov | fe87df9 | 2020-03-27 17:40:27 -0700 | [diff] [blame] | 135 | if (mBtRouteProvider != null) { |
| 136 | mHandler.post(() -> { |
| 137 | mBtRouteProvider.start(); |
| 138 | notifyProviderState(); |
| 139 | }); |
| 140 | } |
Kyunglyul Hyun | f8f170c | 2020-05-13 21:31:26 +0900 | [diff] [blame] | 141 | updateVolume(); |
Kyunglyul Hyun | 0332e9a2 | 2019-11-20 01:39:25 +0000 | [diff] [blame] | 142 | } |
| 143 | |
Hyundo Moon | 63a0540 | 2019-12-19 20:13:56 +0900 | [diff] [blame] | 144 | @Override |
Hyundo Moon | aa80375 | 2020-06-17 23:02:49 +0900 | [diff] [blame] | 145 | public void setCallback(Callback callback) { |
| 146 | super.setCallback(callback); |
| 147 | notifyProviderState(); |
| 148 | notifySessionInfoUpdated(); |
| 149 | } |
| 150 | |
| 151 | @Override |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 152 | public void requestCreateSession(long requestId, String packageName, String routeId, |
Hyundo Moon | 84e027d | 2020-01-16 17:39:05 +0900 | [diff] [blame] | 153 | Bundle sessionHints) { |
Kyunglyul Hyun | 22d4e05 | 2020-04-21 10:10:48 +0900 | [diff] [blame] | 154 | // Assume a router without MODIFY_AUDIO_ROUTING permission can't request with |
| 155 | // a route ID different from the default route ID. The service should've filtered. |
| 156 | if (TextUtils.equals(routeId, DEFAULT_ROUTE_ID)) { |
| 157 | mCallback.onSessionCreated(this, requestId, mDefaultSessionInfo); |
| 158 | return; |
| 159 | } |
| 160 | if (TextUtils.equals(routeId, mSelectedRouteId)) { |
| 161 | mCallback.onSessionCreated(this, requestId, mSessionInfos.get(0)); |
| 162 | return; |
| 163 | } |
| 164 | |
| 165 | synchronized (mRequestLock) { |
| 166 | // Handle the previous request as a failure if exists. |
| 167 | if (mPendingSessionCreationRequest != null) { |
| 168 | mCallback.onRequestFailed(this, mPendingSessionCreationRequest.mRequestId, |
| 169 | MediaRoute2ProviderService.REASON_UNKNOWN_ERROR); |
| 170 | } |
| 171 | mPendingSessionCreationRequest = new SessionCreationRequest(requestId, routeId); |
| 172 | } |
Kyunglyul Hyun | 923ef0d | 2020-03-13 20:55:11 +0900 | [diff] [blame] | 173 | |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 174 | transferToRoute(requestId, SYSTEM_SESSION_ID, routeId); |
Hyundo Moon | 63a0540 | 2019-12-19 20:13:56 +0900 | [diff] [blame] | 175 | } |
| 176 | |
Kyunglyul Hyun | 0332e9a2 | 2019-11-20 01:39:25 +0000 | [diff] [blame] | 177 | @Override |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 178 | public void releaseSession(long requestId, String sessionId) { |
Kyunglyul Hyun | cb8894d | 2019-12-27 14:24:46 +0900 | [diff] [blame] | 179 | // Do nothing |
Kyunglyul Hyun | 0332e9a2 | 2019-11-20 01:39:25 +0000 | [diff] [blame] | 180 | } |
Hyundo Moon | 0fa60e8 | 2020-02-14 11:44:45 +0900 | [diff] [blame] | 181 | |
Kyunglyul Hyun | 1866d8a | 2020-01-31 11:56:34 +0900 | [diff] [blame] | 182 | @Override |
| 183 | public void updateDiscoveryPreference(RouteDiscoveryPreference discoveryPreference) { |
| 184 | // Do nothing |
| 185 | } |
Kyunglyul Hyun | 0332e9a2 | 2019-11-20 01:39:25 +0000 | [diff] [blame] | 186 | |
Kyunglyul Hyun | 0332e9a2 | 2019-11-20 01:39:25 +0000 | [diff] [blame] | 187 | @Override |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 188 | public void selectRoute(long requestId, String sessionId, String routeId) { |
Kyunglyul Hyun | 581fc98 | 2020-01-21 16:30:28 +0900 | [diff] [blame] | 189 | // Do nothing since we don't support multiple BT yet. |
Kyunglyul Hyun | cb8894d | 2019-12-27 14:24:46 +0900 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | @Override |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 193 | public void deselectRoute(long requestId, String sessionId, String routeId) { |
Kyunglyul Hyun | 581fc98 | 2020-01-21 16:30:28 +0900 | [diff] [blame] | 194 | // Do nothing since we don't support multiple BT yet. |
Kyunglyul Hyun | cb8894d | 2019-12-27 14:24:46 +0900 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | @Override |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 198 | public void transferToRoute(long requestId, String sessionId, String routeId) { |
Sungsoo Lim | c27785a | 2020-03-27 16:57:47 +0900 | [diff] [blame] | 199 | if (TextUtils.equals(routeId, DEFAULT_ROUTE_ID)) { |
| 200 | // The currently selected route is the default route. |
| 201 | return; |
| 202 | } |
Roman Kiryanov | fe87df9 | 2020-03-27 17:40:27 -0700 | [diff] [blame] | 203 | if (mBtRouteProvider != null) { |
| 204 | if (TextUtils.equals(routeId, mDeviceRoute.getId())) { |
| 205 | mBtRouteProvider.transferTo(null); |
| 206 | } else { |
| 207 | mBtRouteProvider.transferTo(routeId); |
| 208 | } |
Kyunglyul Hyun | 581fc98 | 2020-01-21 16:30:28 +0900 | [diff] [blame] | 209 | } |
Kyunglyul Hyun | 0332e9a2 | 2019-11-20 01:39:25 +0000 | [diff] [blame] | 210 | } |
| 211 | |
Kyunglyul Hyun | 0332e9a2 | 2019-11-20 01:39:25 +0000 | [diff] [blame] | 212 | @Override |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 213 | public void setRouteVolume(long requestId, String routeId, int volume) { |
Kyunglyul Hyun | 5161b37 | 2020-02-05 18:45:35 +0900 | [diff] [blame] | 214 | if (!TextUtils.equals(routeId, mSelectedRouteId)) { |
| 215 | return; |
| 216 | } |
| 217 | mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, volume, 0); |
| 218 | } |
| 219 | |
| 220 | @Override |
Hyundo Moon | f8e49f4b | 2020-03-06 17:19:42 +0900 | [diff] [blame] | 221 | public void setSessionVolume(long requestId, String sessionId, int volume) { |
Kyunglyul Hyun | 5161b37 | 2020-02-05 18:45:35 +0900 | [diff] [blame] | 222 | // Do nothing since we don't support grouping volume yet. |
Kyunglyul Hyun | 0332e9a2 | 2019-11-20 01:39:25 +0000 | [diff] [blame] | 223 | } |
| 224 | |
Kyunglyul Hyun | 6a2f1d6 | 2020-05-19 23:00:17 +0900 | [diff] [blame] | 225 | @Override |
| 226 | public void prepareReleaseSession(String sessionId) { |
| 227 | // Do nothing since the system session persists. |
| 228 | } |
| 229 | |
Sungsoo Lim | ddf140d | 2020-03-25 08:53:55 +0900 | [diff] [blame] | 230 | public MediaRoute2Info getDefaultRoute() { |
| 231 | return mDefaultRoute; |
| 232 | } |
| 233 | |
Sungsoo Lim | c27785a | 2020-03-27 16:57:47 +0900 | [diff] [blame] | 234 | public RoutingSessionInfo getDefaultSessionInfo() { |
| 235 | return mDefaultSessionInfo; |
| 236 | } |
| 237 | |
Sungsoo Lim | ddf140d | 2020-03-25 08:53:55 +0900 | [diff] [blame] | 238 | private void updateDeviceRoute(AudioRoutesInfo newRoutes) { |
Kyunglyul Hyun | 0332e9a2 | 2019-11-20 01:39:25 +0000 | [diff] [blame] | 239 | int name = R.string.default_audio_route_name; |
Kyunglyul Hyun | 1080caf | 2020-04-20 13:48:15 +0900 | [diff] [blame] | 240 | int type = TYPE_BUILTIN_SPEAKER; |
Sungsoo Lim | ea1eaf7 | 2020-02-12 11:00:06 +0900 | [diff] [blame] | 241 | if (newRoutes != null) { |
| 242 | mCurAudioRoutesInfo.mainType = newRoutes.mainType; |
Kyunglyul Hyun | 1080caf | 2020-04-20 13:48:15 +0900 | [diff] [blame] | 243 | if ((newRoutes.mainType & AudioRoutesInfo.MAIN_HEADPHONES) != 0) { |
| 244 | type = TYPE_WIRED_HEADPHONES; |
| 245 | name = com.android.internal.R.string.default_audio_route_name_headphones; |
| 246 | } else if ((newRoutes.mainType & AudioRoutesInfo.MAIN_HEADSET) != 0) { |
| 247 | type = TYPE_WIRED_HEADSET; |
Sungsoo Lim | ea1eaf7 | 2020-02-12 11:00:06 +0900 | [diff] [blame] | 248 | name = com.android.internal.R.string.default_audio_route_name_headphones; |
| 249 | } else if ((newRoutes.mainType & AudioRoutesInfo.MAIN_DOCK_SPEAKERS) != 0) { |
Kyunglyul Hyun | 1080caf | 2020-04-20 13:48:15 +0900 | [diff] [blame] | 250 | type = TYPE_DOCK; |
Sungsoo Lim | ea1eaf7 | 2020-02-12 11:00:06 +0900 | [diff] [blame] | 251 | name = com.android.internal.R.string.default_audio_route_name_dock_speakers; |
| 252 | } else if ((newRoutes.mainType & AudioRoutesInfo.MAIN_HDMI) != 0) { |
Kyunglyul Hyun | 1080caf | 2020-04-20 13:48:15 +0900 | [diff] [blame] | 253 | type = TYPE_HDMI; |
Sungsoo Lim | ea1eaf7 | 2020-02-12 11:00:06 +0900 | [diff] [blame] | 254 | name = com.android.internal.R.string.default_audio_route_name_hdmi; |
| 255 | } else if ((newRoutes.mainType & AudioRoutesInfo.MAIN_USB) != 0) { |
Kyunglyul Hyun | 1080caf | 2020-04-20 13:48:15 +0900 | [diff] [blame] | 256 | type = TYPE_USB_DEVICE; |
Sungsoo Lim | ea1eaf7 | 2020-02-12 11:00:06 +0900 | [diff] [blame] | 257 | name = com.android.internal.R.string.default_audio_route_name_usb; |
| 258 | } |
Kyunglyul Hyun | 0332e9a2 | 2019-11-20 01:39:25 +0000 | [diff] [blame] | 259 | } |
Kyunglyul Hyun | 1080caf | 2020-04-20 13:48:15 +0900 | [diff] [blame] | 260 | |
Sungsoo Lim | ddf140d | 2020-03-25 08:53:55 +0900 | [diff] [blame] | 261 | mDeviceRoute = new MediaRoute2Info.Builder( |
| 262 | DEVICE_ROUTE_ID, mContext.getResources().getText(name).toString()) |
Kyunglyul Hyun | 0332e9a2 | 2019-11-20 01:39:25 +0000 | [diff] [blame] | 263 | .setVolumeHandling(mAudioManager.isVolumeFixed() |
| 264 | ? MediaRoute2Info.PLAYBACK_VOLUME_FIXED |
| 265 | : MediaRoute2Info.PLAYBACK_VOLUME_VARIABLE) |
Kyunglyul Hyun | f8f170c | 2020-05-13 21:31:26 +0900 | [diff] [blame] | 266 | .setVolume(mDeviceVolume) |
Kyunglyul Hyun | 0332e9a2 | 2019-11-20 01:39:25 +0000 | [diff] [blame] | 267 | .setVolumeMax(mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC)) |
Kyunglyul Hyun | 1080caf | 2020-04-20 13:48:15 +0900 | [diff] [blame] | 268 | .setType(type) |
Hyundo Moon | 42bef14 | 2020-01-14 14:16:30 +0900 | [diff] [blame] | 269 | .addFeature(FEATURE_LIVE_AUDIO) |
| 270 | .addFeature(FEATURE_LIVE_VIDEO) |
Kyunglyul Hyun | c036350 | 2020-06-19 13:33:16 +0900 | [diff] [blame] | 271 | .addFeature(FEATURE_LOCAL_PLAYBACK) |
Sungsoo Lim | ea1eaf7 | 2020-02-12 11:00:06 +0900 | [diff] [blame] | 272 | .setConnectionState(MediaRoute2Info.CONNECTION_STATE_CONNECTED) |
Kyunglyul Hyun | 0332e9a2 | 2019-11-20 01:39:25 +0000 | [diff] [blame] | 273 | .build(); |
Sungsoo Lim | ea1eaf7 | 2020-02-12 11:00:06 +0900 | [diff] [blame] | 274 | updateProviderState(); |
| 275 | } |
Kyunglyul Hyun | 0332e9a2 | 2019-11-20 01:39:25 +0000 | [diff] [blame] | 276 | |
Sungsoo Lim | ea1eaf7 | 2020-02-12 11:00:06 +0900 | [diff] [blame] | 277 | private void updateProviderState() { |
| 278 | MediaRoute2ProviderInfo.Builder builder = new MediaRoute2ProviderInfo.Builder(); |
Sungsoo Lim | ddf140d | 2020-03-25 08:53:55 +0900 | [diff] [blame] | 279 | builder.addRoute(mDeviceRoute); |
Sungsoo Lim | ea1eaf7 | 2020-02-12 11:00:06 +0900 | [diff] [blame] | 280 | if (mBtRouteProvider != null) { |
| 281 | for (MediaRoute2Info route : mBtRouteProvider.getAllBluetoothRoutes()) { |
| 282 | builder.addRoute(route); |
| 283 | } |
| 284 | } |
Kyunglyul Hyun | 5ff55ec | 2020-05-18 16:37:43 +0900 | [diff] [blame] | 285 | MediaRoute2ProviderInfo providerInfo = builder.build(); |
| 286 | setProviderState(providerInfo); |
| 287 | if (DEBUG) { |
| 288 | Slog.d(TAG, "Updating system provider info : " + providerInfo); |
| 289 | } |
Kyunglyul Hyun | 0332e9a2 | 2019-11-20 01:39:25 +0000 | [diff] [blame] | 290 | } |
Hyundo Moon | 5736a61 | 2019-11-19 15:08:32 +0900 | [diff] [blame] | 291 | |
| 292 | /** |
Hyundo Moon | 67c41fd | 2020-01-17 14:22:42 +0900 | [diff] [blame] | 293 | * Updates the mSessionInfo. Returns true if the session info is changed. |
| 294 | */ |
Sungsoo Lim | 613a77a | 2020-03-16 15:44:51 +0900 | [diff] [blame] | 295 | boolean updateSessionInfosIfNeeded() { |
| 296 | synchronized (mLock) { |
Sungsoo Lim | 613a77a | 2020-03-16 15:44:51 +0900 | [diff] [blame] | 297 | RoutingSessionInfo oldSessionInfo = mSessionInfos.isEmpty() ? null : mSessionInfos.get( |
| 298 | 0); |
Hyundo Moon | 67c41fd | 2020-01-17 14:22:42 +0900 | [diff] [blame] | 299 | |
Sungsoo Lim | 613a77a | 2020-03-16 15:44:51 +0900 | [diff] [blame] | 300 | RoutingSessionInfo.Builder builder = new RoutingSessionInfo.Builder( |
| 301 | SYSTEM_SESSION_ID, "" /* clientPackageName */) |
| 302 | .setSystemSession(true); |
Hyundo Moon | 67c41fd | 2020-01-17 14:22:42 +0900 | [diff] [blame] | 303 | |
Sungsoo Lim | c27785a | 2020-03-27 16:57:47 +0900 | [diff] [blame] | 304 | MediaRoute2Info selectedRoute = mDeviceRoute; |
| 305 | if (mBtRouteProvider != null) { |
| 306 | MediaRoute2Info selectedBtRoute = mBtRouteProvider.getSelectedRoute(); |
| 307 | if (selectedBtRoute != null) { |
| 308 | selectedRoute = selectedBtRoute; |
| 309 | builder.addTransferableRoute(mDeviceRoute.getId()); |
| 310 | } |
Sungsoo Lim | 613a77a | 2020-03-16 15:44:51 +0900 | [diff] [blame] | 311 | } |
| 312 | mSelectedRouteId = selectedRoute.getId(); |
Sungsoo Lim | c27785a | 2020-03-27 16:57:47 +0900 | [diff] [blame] | 313 | mDefaultRoute = new MediaRoute2Info.Builder(DEFAULT_ROUTE_ID, selectedRoute) |
| 314 | .setSystemRoute(true) |
| 315 | .setProviderId(mUniqueId) |
| 316 | .build(); |
Sungsoo Lim | 613a77a | 2020-03-16 15:44:51 +0900 | [diff] [blame] | 317 | builder.addSelectedRoute(mSelectedRouteId); |
Hyundo Moon | 67c41fd | 2020-01-17 14:22:42 +0900 | [diff] [blame] | 318 | |
Roman Kiryanov | 3996130 | 2020-04-15 11:49:19 -0700 | [diff] [blame] | 319 | if (mBtRouteProvider != null) { |
| 320 | for (MediaRoute2Info route : mBtRouteProvider.getTransferableRoutes()) { |
| 321 | builder.addTransferableRoute(route.getId()); |
| 322 | } |
Sungsoo Lim | 613a77a | 2020-03-16 15:44:51 +0900 | [diff] [blame] | 323 | } |
Kyunglyul Hyun | 581fc98 | 2020-01-21 16:30:28 +0900 | [diff] [blame] | 324 | |
Sungsoo Lim | 613a77a | 2020-03-16 15:44:51 +0900 | [diff] [blame] | 325 | RoutingSessionInfo newSessionInfo = builder.setProviderId(mUniqueId).build(); |
Kyunglyul Hyun | 22d4e05 | 2020-04-21 10:10:48 +0900 | [diff] [blame] | 326 | |
| 327 | if (mPendingSessionCreationRequest != null) { |
| 328 | SessionCreationRequest sessionCreationRequest; |
| 329 | synchronized (mRequestLock) { |
| 330 | sessionCreationRequest = mPendingSessionCreationRequest; |
| 331 | mPendingSessionCreationRequest = null; |
| 332 | } |
| 333 | if (sessionCreationRequest != null) { |
| 334 | if (TextUtils.equals(mSelectedRouteId, sessionCreationRequest.mRouteId)) { |
| 335 | mCallback.onSessionCreated(this, |
| 336 | sessionCreationRequest.mRequestId, newSessionInfo); |
| 337 | } else { |
| 338 | mCallback.onRequestFailed(this, sessionCreationRequest.mRequestId, |
| 339 | MediaRoute2ProviderService.REASON_UNKNOWN_ERROR); |
| 340 | } |
| 341 | } |
| 342 | } |
| 343 | |
Sungsoo Lim | 613a77a | 2020-03-16 15:44:51 +0900 | [diff] [blame] | 344 | if (Objects.equals(oldSessionInfo, newSessionInfo)) { |
| 345 | return false; |
| 346 | } else { |
Kyunglyul Hyun | 5ff55ec | 2020-05-18 16:37:43 +0900 | [diff] [blame] | 347 | if (DEBUG) { |
| 348 | Slog.d(TAG, "Updating system routing session info : " + newSessionInfo); |
| 349 | } |
Sungsoo Lim | 613a77a | 2020-03-16 15:44:51 +0900 | [diff] [blame] | 350 | mSessionInfos.clear(); |
| 351 | mSessionInfos.add(newSessionInfo); |
Sungsoo Lim | c27785a | 2020-03-27 16:57:47 +0900 | [diff] [blame] | 352 | mDefaultSessionInfo = new RoutingSessionInfo.Builder( |
| 353 | SYSTEM_SESSION_ID, "" /* clientPackageName */) |
| 354 | .setProviderId(mUniqueId) |
| 355 | .setSystemSession(true) |
| 356 | .addSelectedRoute(DEFAULT_ROUTE_ID) |
| 357 | .build(); |
Sungsoo Lim | 613a77a | 2020-03-16 15:44:51 +0900 | [diff] [blame] | 358 | return true; |
| 359 | } |
Hyundo Moon | 67c41fd | 2020-01-17 14:22:42 +0900 | [diff] [blame] | 360 | } |
| 361 | } |
| 362 | |
Sungsoo Lim | ea1eaf7 | 2020-02-12 11:00:06 +0900 | [diff] [blame] | 363 | void publishProviderState() { |
| 364 | updateProviderState(); |
| 365 | notifyProviderState(); |
Kyunglyul Hyun | 0332e9a2 | 2019-11-20 01:39:25 +0000 | [diff] [blame] | 366 | } |
Hyundo Moon | 67c41fd | 2020-01-17 14:22:42 +0900 | [diff] [blame] | 367 | |
| 368 | void notifySessionInfoUpdated() { |
Hyundo Moon | aa80375 | 2020-06-17 23:02:49 +0900 | [diff] [blame] | 369 | if (mCallback == null) { |
| 370 | return; |
| 371 | } |
| 372 | |
Hyundo Moon | 67c41fd | 2020-01-17 14:22:42 +0900 | [diff] [blame] | 373 | RoutingSessionInfo sessionInfo; |
| 374 | synchronized (mLock) { |
| 375 | sessionInfo = mSessionInfos.get(0); |
| 376 | } |
Kyunglyul Hyun | 923ef0d | 2020-03-13 20:55:11 +0900 | [diff] [blame] | 377 | |
Hyundo Moon | 67c41fd | 2020-01-17 14:22:42 +0900 | [diff] [blame] | 378 | mCallback.onSessionUpdated(this, sessionInfo); |
| 379 | } |
Hyundo Moon | 717ef72 | 2020-02-06 18:44:13 +0900 | [diff] [blame] | 380 | |
Kyunglyul Hyun | 22d4e05 | 2020-04-21 10:10:48 +0900 | [diff] [blame] | 381 | private static class SessionCreationRequest { |
| 382 | final long mRequestId; |
| 383 | final String mRouteId; |
| 384 | |
| 385 | SessionCreationRequest(long requestId, String routeId) { |
| 386 | this.mRequestId = requestId; |
| 387 | this.mRouteId = routeId; |
| 388 | } |
| 389 | } |
| 390 | |
Kyunglyul Hyun | f8f170c | 2020-05-13 21:31:26 +0900 | [diff] [blame] | 391 | void updateVolume() { |
| 392 | int devices = mAudioManager.getDevicesForStream(AudioManager.STREAM_MUSIC); |
| 393 | int volume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC); |
| 394 | |
| 395 | if (mDefaultRoute.getVolume() != volume) { |
| 396 | mDefaultRoute = new MediaRoute2Info.Builder(mDefaultRoute) |
| 397 | .setVolume(volume) |
| 398 | .build(); |
| 399 | } |
| 400 | |
| 401 | if (mBtRouteProvider != null && mBtRouteProvider.updateVolumeForDevices(devices, volume)) { |
| 402 | return; |
| 403 | } |
| 404 | if (mDeviceVolume != volume) { |
| 405 | mDeviceVolume = volume; |
| 406 | mDeviceRoute = new MediaRoute2Info.Builder(mDeviceRoute) |
| 407 | .setVolume(volume) |
| 408 | .build(); |
| 409 | } |
| 410 | publishProviderState(); |
| 411 | } |
| 412 | |
| 413 | private class AudioManagerBroadcastReceiver extends BroadcastReceiver { |
Hyundo Moon | 717ef72 | 2020-02-06 18:44:13 +0900 | [diff] [blame] | 414 | // This will be called in the main thread. |
| 415 | @Override |
| 416 | public void onReceive(Context context, Intent intent) { |
Kyunglyul Hyun | f8f170c | 2020-05-13 21:31:26 +0900 | [diff] [blame] | 417 | if (!intent.getAction().equals(AudioManager.VOLUME_CHANGED_ACTION) |
| 418 | && !intent.getAction().equals(AudioManager.STREAM_DEVICES_CHANGED_ACTION)) { |
Hyundo Moon | 717ef72 | 2020-02-06 18:44:13 +0900 | [diff] [blame] | 419 | return; |
| 420 | } |
| 421 | |
Kyunglyul Hyun | f8f170c | 2020-05-13 21:31:26 +0900 | [diff] [blame] | 422 | int streamType = intent.getIntExtra(AudioManager.EXTRA_VOLUME_STREAM_TYPE, -1); |
Hyundo Moon | 717ef72 | 2020-02-06 18:44:13 +0900 | [diff] [blame] | 423 | if (streamType != AudioManager.STREAM_MUSIC) { |
| 424 | return; |
| 425 | } |
| 426 | |
Kyunglyul Hyun | f8f170c | 2020-05-13 21:31:26 +0900 | [diff] [blame] | 427 | updateVolume(); |
Hyundo Moon | 717ef72 | 2020-02-06 18:44:13 +0900 | [diff] [blame] | 428 | } |
| 429 | } |
Kyunglyul Hyun | 0332e9a2 | 2019-11-20 01:39:25 +0000 | [diff] [blame] | 430 | } |