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