Sungsoo Lim | bb3ee6e | 2019-12-23 17:47:24 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2020 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 | |
Kyunglyul Hyun | e77fbcb | 2020-05-29 15:18:11 +0900 | [diff] [blame] | 19 | import static android.bluetooth.BluetoothAdapter.ACTIVE_DEVICE_AUDIO; |
| 20 | |
Sungsoo Lim | bb3ee6e | 2019-12-23 17:47:24 +0900 | [diff] [blame] | 21 | import android.annotation.NonNull; |
Hyundo Moon | 67c41fd | 2020-01-17 14:22:42 +0900 | [diff] [blame] | 22 | import android.annotation.Nullable; |
Sungsoo Lim | bb3ee6e | 2019-12-23 17:47:24 +0900 | [diff] [blame] | 23 | import android.bluetooth.BluetoothA2dp; |
| 24 | import android.bluetooth.BluetoothAdapter; |
| 25 | import android.bluetooth.BluetoothDevice; |
| 26 | import android.bluetooth.BluetoothHearingAid; |
| 27 | import android.bluetooth.BluetoothProfile; |
| 28 | import android.content.BroadcastReceiver; |
| 29 | import android.content.Context; |
| 30 | import android.content.Intent; |
| 31 | import android.content.IntentFilter; |
Hyundo Moon | 717ef72 | 2020-02-06 18:44:13 +0900 | [diff] [blame] | 32 | import android.media.AudioManager; |
Kyunglyul Hyun | f8f170c | 2020-05-13 21:31:26 +0900 | [diff] [blame] | 33 | import android.media.AudioSystem; |
Sungsoo Lim | bb3ee6e | 2019-12-23 17:47:24 +0900 | [diff] [blame] | 34 | import android.media.MediaRoute2Info; |
Sungsoo Lim | 2f19fd1 | 2020-05-07 09:53:09 +0900 | [diff] [blame] | 35 | import android.text.TextUtils; |
Kyunglyul Hyun | 581fc98 | 2020-01-21 16:30:28 +0900 | [diff] [blame] | 36 | import android.util.Slog; |
Sungsoo Lim | bb3ee6e | 2019-12-23 17:47:24 +0900 | [diff] [blame] | 37 | import android.util.SparseBooleanArray; |
Kyunglyul Hyun | f8f170c | 2020-05-13 21:31:26 +0900 | [diff] [blame] | 38 | import android.util.SparseIntArray; |
Sungsoo Lim | bb3ee6e | 2019-12-23 17:47:24 +0900 | [diff] [blame] | 39 | |
| 40 | import com.android.internal.R; |
| 41 | |
| 42 | import java.util.ArrayList; |
Kyunglyul Hyun | 581fc98 | 2020-01-21 16:30:28 +0900 | [diff] [blame] | 43 | import java.util.Collections; |
Sungsoo Lim | bb3ee6e | 2019-12-23 17:47:24 +0900 | [diff] [blame] | 44 | import java.util.HashMap; |
| 45 | import java.util.List; |
| 46 | import java.util.Map; |
| 47 | import java.util.Objects; |
| 48 | |
| 49 | class BluetoothRouteProvider { |
| 50 | private static final String TAG = "BTRouteProvider"; |
Kyunglyul Hyun | f6d5b3c | 2020-06-11 22:23:11 +0900 | [diff] [blame] | 51 | private static final String HEARING_AID_ROUTE_ID_PREFIX = "HEARING_AID_"; |
Sungsoo Lim | bb3ee6e | 2019-12-23 17:47:24 +0900 | [diff] [blame] | 52 | private static BluetoothRouteProvider sInstance; |
| 53 | |
| 54 | @SuppressWarnings("WeakerAccess") /* synthetic access */ |
Kyunglyul Hyun | a8f2a06 | 2020-06-17 15:46:38 +0900 | [diff] [blame] | 55 | // Maps hardware address to BluetoothRouteInfo |
Sungsoo Lim | bb3ee6e | 2019-12-23 17:47:24 +0900 | [diff] [blame] | 56 | final Map<String, BluetoothRouteInfo> mBluetoothRoutes = new HashMap<>(); |
| 57 | @SuppressWarnings("WeakerAccess") /* synthetic access */ |
Sungsoo Lim | cfa7abc | 2020-02-12 09:32:05 +0900 | [diff] [blame] | 58 | BluetoothRouteInfo mSelectedRoute = null; |
| 59 | @SuppressWarnings("WeakerAccess") /* synthetic access */ |
Sungsoo Lim | bb3ee6e | 2019-12-23 17:47:24 +0900 | [diff] [blame] | 60 | BluetoothA2dp mA2dpProfile; |
| 61 | @SuppressWarnings("WeakerAccess") /* synthetic access */ |
| 62 | BluetoothHearingAid mHearingAidProfile; |
| 63 | |
Kyunglyul Hyun | f8f170c | 2020-05-13 21:31:26 +0900 | [diff] [blame] | 64 | // Route type -> volume map |
| 65 | private final SparseIntArray mVolumeMap = new SparseIntArray(); |
| 66 | |
Sungsoo Lim | bb3ee6e | 2019-12-23 17:47:24 +0900 | [diff] [blame] | 67 | private final Context mContext; |
| 68 | private final BluetoothAdapter mBluetoothAdapter; |
| 69 | private final BluetoothRoutesUpdatedListener mListener; |
Hyundo Moon | 717ef72 | 2020-02-06 18:44:13 +0900 | [diff] [blame] | 70 | private final AudioManager mAudioManager; |
Sungsoo Lim | bb3ee6e | 2019-12-23 17:47:24 +0900 | [diff] [blame] | 71 | private final Map<String, BluetoothEventReceiver> mEventReceiverMap = new HashMap<>(); |
| 72 | private final IntentFilter mIntentFilter = new IntentFilter(); |
| 73 | private final BroadcastReceiver mBroadcastReceiver = new BluetoothBroadcastReceiver(); |
| 74 | private final BluetoothProfileListener mProfileListener = new BluetoothProfileListener(); |
| 75 | |
Sungsoo Lim | bb3ee6e | 2019-12-23 17:47:24 +0900 | [diff] [blame] | 76 | static synchronized BluetoothRouteProvider getInstance(@NonNull Context context, |
| 77 | @NonNull BluetoothRoutesUpdatedListener listener) { |
| 78 | Objects.requireNonNull(context); |
| 79 | Objects.requireNonNull(listener); |
| 80 | |
| 81 | if (sInstance == null) { |
| 82 | BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter(); |
| 83 | if (btAdapter == null) { |
| 84 | return null; |
| 85 | } |
| 86 | sInstance = new BluetoothRouteProvider(context, btAdapter, listener); |
| 87 | } |
| 88 | return sInstance; |
| 89 | } |
| 90 | |
| 91 | private BluetoothRouteProvider(Context context, BluetoothAdapter btAdapter, |
| 92 | BluetoothRoutesUpdatedListener listener) { |
| 93 | mContext = context; |
| 94 | mBluetoothAdapter = btAdapter; |
| 95 | mListener = listener; |
Hyundo Moon | 717ef72 | 2020-02-06 18:44:13 +0900 | [diff] [blame] | 96 | mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE); |
Sungsoo Lim | bb3ee6e | 2019-12-23 17:47:24 +0900 | [diff] [blame] | 97 | buildBluetoothRoutes(); |
Sungsoo Lim | 613a77a | 2020-03-16 15:44:51 +0900 | [diff] [blame] | 98 | } |
Sungsoo Lim | bb3ee6e | 2019-12-23 17:47:24 +0900 | [diff] [blame] | 99 | |
Sungsoo Lim | 613a77a | 2020-03-16 15:44:51 +0900 | [diff] [blame] | 100 | public void start() { |
Sungsoo Lim | bb3ee6e | 2019-12-23 17:47:24 +0900 | [diff] [blame] | 101 | mBluetoothAdapter.getProfileProxy(mContext, mProfileListener, BluetoothProfile.A2DP); |
| 102 | mBluetoothAdapter.getProfileProxy(mContext, mProfileListener, BluetoothProfile.HEARING_AID); |
| 103 | |
| 104 | // Bluetooth on/off broadcasts |
| 105 | addEventReceiver(BluetoothAdapter.ACTION_STATE_CHANGED, new AdapterStateChangedReceiver()); |
| 106 | |
Sungsoo Lim | bb3ee6e | 2019-12-23 17:47:24 +0900 | [diff] [blame] | 107 | DeviceStateChangedRecevier deviceStateChangedReceiver = new DeviceStateChangedRecevier(); |
| 108 | addEventReceiver(BluetoothA2dp.ACTION_ACTIVE_DEVICE_CHANGED, deviceStateChangedReceiver); |
| 109 | addEventReceiver(BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED, deviceStateChangedReceiver); |
| 110 | addEventReceiver(BluetoothHearingAid.ACTION_ACTIVE_DEVICE_CHANGED, |
| 111 | deviceStateChangedReceiver); |
| 112 | addEventReceiver(BluetoothHearingAid.ACTION_CONNECTION_STATE_CHANGED, |
| 113 | deviceStateChangedReceiver); |
| 114 | |
| 115 | mContext.registerReceiver(mBroadcastReceiver, mIntentFilter, null, null); |
| 116 | } |
| 117 | |
Kyunglyul Hyun | 581fc98 | 2020-01-21 16:30:28 +0900 | [diff] [blame] | 118 | /** |
Sungsoo Lim | cfa7abc | 2020-02-12 09:32:05 +0900 | [diff] [blame] | 119 | * Transfers to a given bluetooth route. |
| 120 | * The dedicated BT device with the route would be activated. |
| 121 | * |
| 122 | * @param routeId the id of the Bluetooth device. {@code null} denotes to clear the use of |
| 123 | * BT routes. |
Kyunglyul Hyun | 581fc98 | 2020-01-21 16:30:28 +0900 | [diff] [blame] | 124 | */ |
Sungsoo Lim | cfa7abc | 2020-02-12 09:32:05 +0900 | [diff] [blame] | 125 | public void transferTo(@Nullable String routeId) { |
| 126 | if (routeId == null) { |
| 127 | clearActiveDevices(); |
| 128 | return; |
Kyunglyul Hyun | 581fc98 | 2020-01-21 16:30:28 +0900 | [diff] [blame] | 129 | } |
Kyunglyul Hyun | 581fc98 | 2020-01-21 16:30:28 +0900 | [diff] [blame] | 130 | |
Kyunglyul Hyun | a8f2a06 | 2020-06-17 15:46:38 +0900 | [diff] [blame] | 131 | BluetoothRouteInfo btRouteInfo = findBluetoothRouteWithRouteId(routeId); |
| 132 | |
Kyunglyul Hyun | 581fc98 | 2020-01-21 16:30:28 +0900 | [diff] [blame] | 133 | if (btRouteInfo == null) { |
Kyunglyul Hyun | a8f2a06 | 2020-06-17 15:46:38 +0900 | [diff] [blame] | 134 | Slog.w(TAG, "transferTo: Unknown route. ID=" + routeId); |
Kyunglyul Hyun | 581fc98 | 2020-01-21 16:30:28 +0900 | [diff] [blame] | 135 | return; |
| 136 | } |
Kyunglyul Hyun | 581fc98 | 2020-01-21 16:30:28 +0900 | [diff] [blame] | 137 | |
Kyunglyul Hyun | e77fbcb | 2020-05-29 15:18:11 +0900 | [diff] [blame] | 138 | if (mBluetoothAdapter != null) { |
| 139 | mBluetoothAdapter.setActiveDevice(btRouteInfo.btDevice, ACTIVE_DEVICE_AUDIO); |
Kyunglyul Hyun | 581fc98 | 2020-01-21 16:30:28 +0900 | [diff] [blame] | 140 | } |
| 141 | } |
| 142 | |
Sungsoo Lim | cfa7abc | 2020-02-12 09:32:05 +0900 | [diff] [blame] | 143 | /** |
| 144 | * Clears the active device for all known profiles. |
| 145 | */ |
| 146 | private void clearActiveDevices() { |
Kyunglyul Hyun | e77fbcb | 2020-05-29 15:18:11 +0900 | [diff] [blame] | 147 | if (mBluetoothAdapter != null) { |
| 148 | mBluetoothAdapter.removeActiveDevice(ACTIVE_DEVICE_AUDIO); |
Sungsoo Lim | cfa7abc | 2020-02-12 09:32:05 +0900 | [diff] [blame] | 149 | } |
| 150 | } |
| 151 | |
Sungsoo Lim | bb3ee6e | 2019-12-23 17:47:24 +0900 | [diff] [blame] | 152 | private void addEventReceiver(String action, BluetoothEventReceiver eventReceiver) { |
| 153 | mEventReceiverMap.put(action, eventReceiver); |
| 154 | mIntentFilter.addAction(action); |
| 155 | } |
| 156 | |
| 157 | private void buildBluetoothRoutes() { |
| 158 | mBluetoothRoutes.clear(); |
| 159 | for (BluetoothDevice device : mBluetoothAdapter.getBondedDevices()) { |
| 160 | if (device.isConnected()) { |
| 161 | BluetoothRouteInfo newBtRoute = createBluetoothRoute(device); |
Kyunglyul Hyun | 6eefb5f | 2020-05-15 16:21:20 +0900 | [diff] [blame] | 162 | if (newBtRoute.connectedProfiles.size() > 0) { |
| 163 | mBluetoothRoutes.put(device.getAddress(), newBtRoute); |
| 164 | } |
Sungsoo Lim | bb3ee6e | 2019-12-23 17:47:24 +0900 | [diff] [blame] | 165 | } |
| 166 | } |
| 167 | } |
| 168 | |
Sungsoo Lim | ea1eaf7 | 2020-02-12 11:00:06 +0900 | [diff] [blame] | 169 | @Nullable |
| 170 | MediaRoute2Info getSelectedRoute() { |
| 171 | return (mSelectedRoute == null) ? null : mSelectedRoute.route; |
| 172 | } |
| 173 | |
| 174 | @NonNull |
| 175 | List<MediaRoute2Info> getTransferableRoutes() { |
| 176 | List<MediaRoute2Info> routes = getAllBluetoothRoutes(); |
| 177 | if (mSelectedRoute != null) { |
| 178 | routes.remove(mSelectedRoute.route); |
| 179 | } |
| 180 | return routes; |
| 181 | } |
| 182 | |
Sungsoo Lim | cfa7abc | 2020-02-12 09:32:05 +0900 | [diff] [blame] | 183 | @NonNull |
| 184 | List<MediaRoute2Info> getAllBluetoothRoutes() { |
Kyunglyul Hyun | f6d5b3c | 2020-06-11 22:23:11 +0900 | [diff] [blame] | 185 | List<MediaRoute2Info> routes = new ArrayList<>(); |
| 186 | List<String> routeIds = new ArrayList<>(); |
| 187 | |
Sungsoo Lim | bb3ee6e | 2019-12-23 17:47:24 +0900 | [diff] [blame] | 188 | for (BluetoothRouteInfo btRoute : mBluetoothRoutes.values()) { |
Kyunglyul Hyun | f6d5b3c | 2020-06-11 22:23:11 +0900 | [diff] [blame] | 189 | // A pair of hearing aid devices or the same hardware address |
| 190 | if (routeIds.contains(btRoute.route.getId())) { |
| 191 | continue; |
| 192 | } |
Sungsoo Lim | bb3ee6e | 2019-12-23 17:47:24 +0900 | [diff] [blame] | 193 | routes.add(btRoute.route); |
Kyunglyul Hyun | f6d5b3c | 2020-06-11 22:23:11 +0900 | [diff] [blame] | 194 | routeIds.add(btRoute.route.getId()); |
Sungsoo Lim | bb3ee6e | 2019-12-23 17:47:24 +0900 | [diff] [blame] | 195 | } |
| 196 | return routes; |
| 197 | } |
| 198 | |
Kyunglyul Hyun | a8f2a06 | 2020-06-17 15:46:38 +0900 | [diff] [blame] | 199 | BluetoothRouteInfo findBluetoothRouteWithRouteId(String routeId) { |
| 200 | if (routeId == null) { |
| 201 | return null; |
| 202 | } |
| 203 | for (BluetoothRouteInfo btRouteInfo : mBluetoothRoutes.values()) { |
| 204 | if (TextUtils.equals(btRouteInfo.route.getId(), routeId)) { |
| 205 | return btRouteInfo; |
| 206 | } |
| 207 | } |
| 208 | return null; |
| 209 | } |
| 210 | |
Kyunglyul Hyun | f8f170c | 2020-05-13 21:31:26 +0900 | [diff] [blame] | 211 | /** |
| 212 | * Updates the volume for {@link AudioManager#getDevicesForStream(int) devices}. |
| 213 | * |
| 214 | * @return true if devices can be handled by the provider. |
| 215 | */ |
| 216 | public boolean updateVolumeForDevices(int devices, int volume) { |
| 217 | int routeType; |
| 218 | if ((devices & (AudioSystem.DEVICE_OUT_HEARING_AID)) != 0) { |
| 219 | routeType = MediaRoute2Info.TYPE_HEARING_AID; |
| 220 | } else if ((devices & (AudioManager.DEVICE_OUT_BLUETOOTH_A2DP |
| 221 | | AudioManager.DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES |
| 222 | | AudioManager.DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER)) != 0) { |
| 223 | routeType = MediaRoute2Info.TYPE_BLUETOOTH_A2DP; |
| 224 | } else { |
| 225 | return false; |
| 226 | } |
| 227 | mVolumeMap.put(routeType, volume); |
| 228 | if (mSelectedRoute == null || mSelectedRoute.route.getType() != routeType) { |
| 229 | return true; |
| 230 | } |
Sungsoo Lim | ea1eaf7 | 2020-02-12 11:00:06 +0900 | [diff] [blame] | 231 | mSelectedRoute.route = new MediaRoute2Info.Builder(mSelectedRoute.route) |
| 232 | .setVolume(volume) |
| 233 | .build(); |
Kyunglyul Hyun | f8f170c | 2020-05-13 21:31:26 +0900 | [diff] [blame] | 234 | notifyBluetoothRoutesUpdated(); |
Sungsoo Lim | ea1eaf7 | 2020-02-12 11:00:06 +0900 | [diff] [blame] | 235 | return true; |
Hyundo Moon | 67c41fd | 2020-01-17 14:22:42 +0900 | [diff] [blame] | 236 | } |
| 237 | |
Sungsoo Lim | bb3ee6e | 2019-12-23 17:47:24 +0900 | [diff] [blame] | 238 | private void notifyBluetoothRoutesUpdated() { |
| 239 | if (mListener != null) { |
Sungsoo Lim | cfa7abc | 2020-02-12 09:32:05 +0900 | [diff] [blame] | 240 | mListener.onBluetoothRoutesUpdated(getAllBluetoothRoutes()); |
Sungsoo Lim | bb3ee6e | 2019-12-23 17:47:24 +0900 | [diff] [blame] | 241 | } |
| 242 | } |
| 243 | |
| 244 | private BluetoothRouteInfo createBluetoothRoute(BluetoothDevice device) { |
| 245 | BluetoothRouteInfo newBtRoute = new BluetoothRouteInfo(); |
| 246 | newBtRoute.btDevice = device; |
Kyunglyul Hyun | f6d5b3c | 2020-06-11 22:23:11 +0900 | [diff] [blame] | 247 | |
| 248 | String routeId = device.getAddress(); |
Sungsoo Lim | 2f19fd1 | 2020-05-07 09:53:09 +0900 | [diff] [blame] | 249 | String deviceName = device.getName(); |
| 250 | if (TextUtils.isEmpty(deviceName)) { |
| 251 | deviceName = mContext.getResources().getText(R.string.unknownName).toString(); |
| 252 | } |
Kyunglyul Hyun | 6eefb5f | 2020-05-15 16:21:20 +0900 | [diff] [blame] | 253 | int type = MediaRoute2Info.TYPE_BLUETOOTH_A2DP; |
| 254 | newBtRoute.connectedProfiles = new SparseBooleanArray(); |
| 255 | if (mA2dpProfile != null && mA2dpProfile.getConnectedDevices().contains(device)) { |
| 256 | newBtRoute.connectedProfiles.put(BluetoothProfile.A2DP, true); |
| 257 | } |
| 258 | if (mHearingAidProfile != null |
| 259 | && mHearingAidProfile.getConnectedDevices().contains(device)) { |
| 260 | newBtRoute.connectedProfiles.put(BluetoothProfile.HEARING_AID, true); |
Kyunglyul Hyun | f6d5b3c | 2020-06-11 22:23:11 +0900 | [diff] [blame] | 261 | // Intentionally assign the same ID for a pair of devices to publish only one of them. |
| 262 | routeId = HEARING_AID_ROUTE_ID_PREFIX + mHearingAidProfile.getHiSyncId(device); |
Kyunglyul Hyun | 6eefb5f | 2020-05-15 16:21:20 +0900 | [diff] [blame] | 263 | type = MediaRoute2Info.TYPE_HEARING_AID; |
| 264 | } |
| 265 | |
Kyunglyul Hyun | f6d5b3c | 2020-06-11 22:23:11 +0900 | [diff] [blame] | 266 | // Current volume will be set when connected. |
| 267 | newBtRoute.route = new MediaRoute2Info.Builder(routeId, deviceName) |
Hyundo Moon | 42bef14 | 2020-01-14 14:16:30 +0900 | [diff] [blame] | 268 | .addFeature(MediaRoute2Info.FEATURE_LIVE_AUDIO) |
Sungsoo Lim | bb3ee6e | 2019-12-23 17:47:24 +0900 | [diff] [blame] | 269 | .setConnectionState(MediaRoute2Info.CONNECTION_STATE_DISCONNECTED) |
| 270 | .setDescription(mContext.getResources().getText( |
| 271 | R.string.bluetooth_a2dp_audio_route_name).toString()) |
Kyunglyul Hyun | 6eefb5f | 2020-05-15 16:21:20 +0900 | [diff] [blame] | 272 | .setType(type) |
Hyundo Moon | 717ef72 | 2020-02-06 18:44:13 +0900 | [diff] [blame] | 273 | .setVolumeHandling(MediaRoute2Info.PLAYBACK_VOLUME_VARIABLE) |
Kyunglyul Hyun | f8f170c | 2020-05-13 21:31:26 +0900 | [diff] [blame] | 274 | .setVolumeMax(mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC)) |
Kyunglyul Hyun | 1ea19b1 | 2020-06-10 20:04:15 +0900 | [diff] [blame] | 275 | .setAddress(device.getAddress()) |
Sungsoo Lim | bb3ee6e | 2019-12-23 17:47:24 +0900 | [diff] [blame] | 276 | .build(); |
Sungsoo Lim | bb3ee6e | 2019-12-23 17:47:24 +0900 | [diff] [blame] | 277 | return newBtRoute; |
| 278 | } |
| 279 | |
Sungsoo Lim | cfa7abc | 2020-02-12 09:32:05 +0900 | [diff] [blame] | 280 | private void setRouteConnectionState(@NonNull BluetoothRouteInfo btRoute, |
Sungsoo Lim | bb3ee6e | 2019-12-23 17:47:24 +0900 | [diff] [blame] | 281 | @MediaRoute2Info.ConnectionState int state) { |
Sungsoo Lim | bb3ee6e | 2019-12-23 17:47:24 +0900 | [diff] [blame] | 282 | if (btRoute == null) { |
Sungsoo Lim | cfa7abc | 2020-02-12 09:32:05 +0900 | [diff] [blame] | 283 | Slog.w(TAG, "setRouteConnectionState: route shouldn't be null"); |
Sungsoo Lim | bb3ee6e | 2019-12-23 17:47:24 +0900 | [diff] [blame] | 284 | return; |
| 285 | } |
Hyundo Moon | 717ef72 | 2020-02-06 18:44:13 +0900 | [diff] [blame] | 286 | if (btRoute.route.getConnectionState() == state) { |
| 287 | return; |
Sungsoo Lim | bb3ee6e | 2019-12-23 17:47:24 +0900 | [diff] [blame] | 288 | } |
Hyundo Moon | 717ef72 | 2020-02-06 18:44:13 +0900 | [diff] [blame] | 289 | |
Hyundo Moon | 717ef72 | 2020-02-06 18:44:13 +0900 | [diff] [blame] | 290 | MediaRoute2Info.Builder builder = new MediaRoute2Info.Builder(btRoute.route) |
| 291 | .setConnectionState(state); |
Kyunglyul Hyun | f8f170c | 2020-05-13 21:31:26 +0900 | [diff] [blame] | 292 | builder.setType(btRoute.getRouteType()); |
Hyundo Moon | 717ef72 | 2020-02-06 18:44:13 +0900 | [diff] [blame] | 293 | |
| 294 | if (state == MediaRoute2Info.CONNECTION_STATE_CONNECTED) { |
Kyunglyul Hyun | f8f170c | 2020-05-13 21:31:26 +0900 | [diff] [blame] | 295 | builder.setVolume(mVolumeMap.get(btRoute.getRouteType(), 0)); |
Hyundo Moon | 717ef72 | 2020-02-06 18:44:13 +0900 | [diff] [blame] | 296 | } |
| 297 | btRoute.route = builder.build(); |
Sungsoo Lim | bb3ee6e | 2019-12-23 17:47:24 +0900 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | interface BluetoothRoutesUpdatedListener { |
| 301 | void onBluetoothRoutesUpdated(@NonNull List<MediaRoute2Info> routes); |
| 302 | } |
| 303 | |
| 304 | private class BluetoothRouteInfo { |
| 305 | public BluetoothDevice btDevice; |
| 306 | public MediaRoute2Info route; |
| 307 | public SparseBooleanArray connectedProfiles; |
Kyunglyul Hyun | f8f170c | 2020-05-13 21:31:26 +0900 | [diff] [blame] | 308 | |
| 309 | @MediaRoute2Info.Type |
| 310 | int getRouteType() { |
| 311 | // Let hearing aid profile have a priority. |
| 312 | if (connectedProfiles.get(BluetoothProfile.HEARING_AID, false)) { |
| 313 | return MediaRoute2Info.TYPE_HEARING_AID; |
| 314 | } |
| 315 | return MediaRoute2Info.TYPE_BLUETOOTH_A2DP; |
| 316 | } |
Sungsoo Lim | bb3ee6e | 2019-12-23 17:47:24 +0900 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | // These callbacks run on the main thread. |
| 320 | private final class BluetoothProfileListener implements BluetoothProfile.ServiceListener { |
| 321 | public void onServiceConnected(int profile, BluetoothProfile proxy) { |
Kyunglyul Hyun | 581fc98 | 2020-01-21 16:30:28 +0900 | [diff] [blame] | 322 | List<BluetoothDevice> activeDevices; |
Sungsoo Lim | bb3ee6e | 2019-12-23 17:47:24 +0900 | [diff] [blame] | 323 | switch (profile) { |
| 324 | case BluetoothProfile.A2DP: |
| 325 | mA2dpProfile = (BluetoothA2dp) proxy; |
Kyunglyul Hyun | 581fc98 | 2020-01-21 16:30:28 +0900 | [diff] [blame] | 326 | // It may contain null. |
| 327 | activeDevices = Collections.singletonList(mA2dpProfile.getActiveDevice()); |
Sungsoo Lim | bb3ee6e | 2019-12-23 17:47:24 +0900 | [diff] [blame] | 328 | break; |
| 329 | case BluetoothProfile.HEARING_AID: |
| 330 | mHearingAidProfile = (BluetoothHearingAid) proxy; |
Kyunglyul Hyun | 581fc98 | 2020-01-21 16:30:28 +0900 | [diff] [blame] | 331 | activeDevices = mHearingAidProfile.getActiveDevices(); |
Sungsoo Lim | bb3ee6e | 2019-12-23 17:47:24 +0900 | [diff] [blame] | 332 | break; |
| 333 | default: |
| 334 | return; |
| 335 | } |
Kyunglyul Hyun | e77fbcb | 2020-05-29 15:18:11 +0900 | [diff] [blame] | 336 | //TODO(b/157708273): Handle two active devices in the binaural case. |
Sungsoo Lim | bb3ee6e | 2019-12-23 17:47:24 +0900 | [diff] [blame] | 337 | for (BluetoothDevice device : proxy.getConnectedDevices()) { |
| 338 | BluetoothRouteInfo btRoute = mBluetoothRoutes.get(device.getAddress()); |
| 339 | if (btRoute == null) { |
| 340 | btRoute = createBluetoothRoute(device); |
| 341 | mBluetoothRoutes.put(device.getAddress(), btRoute); |
| 342 | } |
Kyunglyul Hyun | 581fc98 | 2020-01-21 16:30:28 +0900 | [diff] [blame] | 343 | if (activeDevices.contains(device)) { |
Sungsoo Lim | cfa7abc | 2020-02-12 09:32:05 +0900 | [diff] [blame] | 344 | mSelectedRoute = btRoute; |
| 345 | setRouteConnectionState(mSelectedRoute, |
Kyunglyul Hyun | 581fc98 | 2020-01-21 16:30:28 +0900 | [diff] [blame] | 346 | MediaRoute2Info.CONNECTION_STATE_CONNECTED); |
| 347 | } |
Sungsoo Lim | bb3ee6e | 2019-12-23 17:47:24 +0900 | [diff] [blame] | 348 | } |
Kyunglyul Hyun | 581fc98 | 2020-01-21 16:30:28 +0900 | [diff] [blame] | 349 | notifyBluetoothRoutesUpdated(); |
Sungsoo Lim | bb3ee6e | 2019-12-23 17:47:24 +0900 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | public void onServiceDisconnected(int profile) { |
| 353 | switch (profile) { |
| 354 | case BluetoothProfile.A2DP: |
| 355 | mA2dpProfile = null; |
| 356 | break; |
| 357 | case BluetoothProfile.HEARING_AID: |
| 358 | mHearingAidProfile = null; |
| 359 | break; |
| 360 | default: |
| 361 | return; |
| 362 | } |
| 363 | } |
| 364 | } |
| 365 | private class BluetoothBroadcastReceiver extends BroadcastReceiver { |
| 366 | @Override |
| 367 | public void onReceive(Context context, Intent intent) { |
| 368 | String action = intent.getAction(); |
| 369 | BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); |
| 370 | |
| 371 | BluetoothEventReceiver receiver = mEventReceiverMap.get(action); |
| 372 | if (receiver != null) { |
| 373 | receiver.onReceive(context, intent, device); |
| 374 | } |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | private interface BluetoothEventReceiver { |
| 379 | void onReceive(Context context, Intent intent, BluetoothDevice device); |
| 380 | } |
| 381 | |
| 382 | private class AdapterStateChangedReceiver implements BluetoothEventReceiver { |
| 383 | public void onReceive(Context context, Intent intent, BluetoothDevice device) { |
| 384 | int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1); |
| 385 | if (state == BluetoothAdapter.STATE_OFF |
| 386 | || state == BluetoothAdapter.STATE_TURNING_OFF) { |
| 387 | mBluetoothRoutes.clear(); |
| 388 | notifyBluetoothRoutesUpdated(); |
| 389 | } else if (state == BluetoothAdapter.STATE_ON) { |
| 390 | buildBluetoothRoutes(); |
| 391 | if (!mBluetoothRoutes.isEmpty()) { |
| 392 | notifyBluetoothRoutesUpdated(); |
| 393 | } |
| 394 | } |
| 395 | } |
| 396 | } |
| 397 | |
Sungsoo Lim | bb3ee6e | 2019-12-23 17:47:24 +0900 | [diff] [blame] | 398 | private class DeviceStateChangedRecevier implements BluetoothEventReceiver { |
| 399 | @Override |
| 400 | public void onReceive(Context context, Intent intent, BluetoothDevice device) { |
| 401 | switch (intent.getAction()) { |
| 402 | case BluetoothA2dp.ACTION_ACTIVE_DEVICE_CHANGED: |
Kyunglyul Hyun | e77fbcb | 2020-05-29 15:18:11 +0900 | [diff] [blame] | 403 | case BluetoothHearingAid.ACTION_ACTIVE_DEVICE_CHANGED: |
Sungsoo Lim | cfa7abc | 2020-02-12 09:32:05 +0900 | [diff] [blame] | 404 | if (mSelectedRoute == null |
| 405 | || !mSelectedRoute.btDevice.equals(device)) { |
| 406 | if (mSelectedRoute != null) { |
| 407 | setRouteConnectionState(mSelectedRoute, |
Sungsoo Lim | bb3ee6e | 2019-12-23 17:47:24 +0900 | [diff] [blame] | 408 | MediaRoute2Info.CONNECTION_STATE_DISCONNECTED); |
| 409 | } |
Sungsoo Lim | cfa7abc | 2020-02-12 09:32:05 +0900 | [diff] [blame] | 410 | mSelectedRoute = (device == null) ? null |
| 411 | : mBluetoothRoutes.get(device.getAddress()); |
| 412 | if (mSelectedRoute != null) { |
| 413 | setRouteConnectionState(mSelectedRoute, |
Sungsoo Lim | bb3ee6e | 2019-12-23 17:47:24 +0900 | [diff] [blame] | 414 | MediaRoute2Info.CONNECTION_STATE_CONNECTED); |
| 415 | } |
Hyundo Moon | 67c41fd | 2020-01-17 14:22:42 +0900 | [diff] [blame] | 416 | notifyBluetoothRoutesUpdated(); |
Sungsoo Lim | bb3ee6e | 2019-12-23 17:47:24 +0900 | [diff] [blame] | 417 | } |
| 418 | break; |
| 419 | case BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED: |
| 420 | handleConnectionStateChanged(BluetoothProfile.A2DP, intent, device); |
| 421 | break; |
Kyunglyul Hyun | e77fbcb | 2020-05-29 15:18:11 +0900 | [diff] [blame] | 422 | case BluetoothHearingAid.ACTION_CONNECTION_STATE_CHANGED: |
| 423 | handleConnectionStateChanged(BluetoothProfile.HEARING_AID, intent, device); |
| 424 | break; |
Sungsoo Lim | bb3ee6e | 2019-12-23 17:47:24 +0900 | [diff] [blame] | 425 | } |
| 426 | } |
| 427 | |
| 428 | private void handleConnectionStateChanged(int profile, Intent intent, |
| 429 | BluetoothDevice device) { |
| 430 | int state = intent.getIntExtra(BluetoothProfile.EXTRA_STATE, -1); |
| 431 | BluetoothRouteInfo btRoute = mBluetoothRoutes.get(device.getAddress()); |
| 432 | if (state == BluetoothProfile.STATE_CONNECTED) { |
| 433 | if (btRoute == null) { |
| 434 | btRoute = createBluetoothRoute(device); |
Kyunglyul Hyun | 6eefb5f | 2020-05-15 16:21:20 +0900 | [diff] [blame] | 435 | if (btRoute.connectedProfiles.size() > 0) { |
| 436 | mBluetoothRoutes.put(device.getAddress(), btRoute); |
| 437 | notifyBluetoothRoutesUpdated(); |
| 438 | } |
Sungsoo Lim | bb3ee6e | 2019-12-23 17:47:24 +0900 | [diff] [blame] | 439 | } else { |
| 440 | btRoute.connectedProfiles.put(profile, true); |
| 441 | } |
| 442 | } else if (state == BluetoothProfile.STATE_DISCONNECTING |
| 443 | || state == BluetoothProfile.STATE_DISCONNECTED) { |
Kyunglyul Hyun | db74f3d | 2020-01-15 11:12:14 +0900 | [diff] [blame] | 444 | if (btRoute != null) { |
| 445 | btRoute.connectedProfiles.delete(profile); |
| 446 | if (btRoute.connectedProfiles.size() == 0) { |
| 447 | mBluetoothRoutes.remove(device.getAddress()); |
Sungsoo Lim | cfa7abc | 2020-02-12 09:32:05 +0900 | [diff] [blame] | 448 | if (mSelectedRoute != null && mSelectedRoute.btDevice.equals(device)) { |
| 449 | mSelectedRoute = null; |
Kyunglyul Hyun | db74f3d | 2020-01-15 11:12:14 +0900 | [diff] [blame] | 450 | } |
| 451 | notifyBluetoothRoutesUpdated(); |
Sungsoo Lim | bb3ee6e | 2019-12-23 17:47:24 +0900 | [diff] [blame] | 452 | } |
Sungsoo Lim | bb3ee6e | 2019-12-23 17:47:24 +0900 | [diff] [blame] | 453 | } |
| 454 | } |
| 455 | } |
| 456 | } |
| 457 | } |