| Mike Lockwood | 517b04f | 2014-06-02 16:20:37 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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 android.bluetooth; |
| 18 | |
| William Escande | 9a4b7c1 | 2021-12-16 16:07:55 +0100 | [diff] [blame] | 19 | import static android.bluetooth.BluetoothUtils.getSyncTimeout; |
| 20 | |
| Jeff Sharkey | 8f80e4a | 2021-04-02 08:06:09 -0600 | [diff] [blame] | 21 | import android.annotation.RequiresPermission; |
| Jeff Sharkey | d7c5566 | 2021-04-20 12:30:37 -0600 | [diff] [blame] | 22 | import android.annotation.SdkConstant; |
| Jeff Sharkey | d7c5566 | 2021-04-20 12:30:37 -0600 | [diff] [blame] | 23 | import android.annotation.SdkConstant.SdkConstantType; |
| Jeff Sharkey | 8f80e4a | 2021-04-02 08:06:09 -0600 | [diff] [blame] | 24 | import android.bluetooth.annotations.RequiresBluetoothConnectPermission; |
| 25 | import android.bluetooth.annotations.RequiresLegacyBluetoothPermission; |
| Jeff Sharkey | f9e176c | 2021-04-22 16:01:29 -0600 | [diff] [blame] | 26 | import android.content.AttributionSource; |
| Mike Lockwood | 517b04f | 2014-06-02 16:20:37 -0700 | [diff] [blame] | 27 | import android.content.Context; |
| Mike Lockwood | 517b04f | 2014-06-02 16:20:37 -0700 | [diff] [blame] | 28 | import android.os.IBinder; |
| 29 | import android.os.RemoteException; |
| 30 | import android.util.Log; |
| 31 | |
| William Escande | 9a4b7c1 | 2021-12-16 16:07:55 +0100 | [diff] [blame] | 32 | import com.android.modules.utils.SynchronousResultReceiver; |
| 33 | |
| Mike Lockwood | 517b04f | 2014-06-02 16:20:37 -0700 | [diff] [blame] | 34 | import java.util.ArrayList; |
| 35 | import java.util.List; |
| William Escande | 9a4b7c1 | 2021-12-16 16:07:55 +0100 | [diff] [blame] | 36 | import java.util.concurrent.TimeoutException; |
| Mike Lockwood | 517b04f | 2014-06-02 16:20:37 -0700 | [diff] [blame] | 37 | |
| 38 | /** |
| Sanket Agarwal | 25e84d4 | 2015-10-21 18:23:27 -0700 | [diff] [blame] | 39 | * This class provides the public APIs to control the Bluetooth AVRCP Controller. It currently |
| 40 | * supports player information, playback support and track metadata. |
| Mike Lockwood | 517b04f | 2014-06-02 16:20:37 -0700 | [diff] [blame] | 41 | * |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 42 | * <p>BluetoothAvrcpController is a proxy object for controlling the Bluetooth AVRCP |
| Mike Lockwood | 517b04f | 2014-06-02 16:20:37 -0700 | [diff] [blame] | 43 | * Service via IPC. Use {@link BluetoothAdapter#getProfileProxy} to get |
| 44 | * the BluetoothAvrcpController proxy object. |
| 45 | * |
| 46 | * {@hide} |
| 47 | */ |
| 48 | public final class BluetoothAvrcpController implements BluetoothProfile { |
| 49 | private static final String TAG = "BluetoothAvrcpController"; |
| Sanket Agarwal | 25e84d4 | 2015-10-21 18:23:27 -0700 | [diff] [blame] | 50 | private static final boolean DBG = false; |
| Mike Lockwood | 517b04f | 2014-06-02 16:20:37 -0700 | [diff] [blame] | 51 | private static final boolean VDBG = false; |
| 52 | |
| 53 | /** |
| 54 | * Intent used to broadcast the change in connection state of the AVRCP Controller |
| 55 | * profile. |
| 56 | * |
| 57 | * <p>This intent will have 3 extras: |
| 58 | * <ul> |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 59 | * <li> {@link #EXTRA_STATE} - The current state of the profile. </li> |
| 60 | * <li> {@link #EXTRA_PREVIOUS_STATE}- The previous state of the profile.</li> |
| 61 | * <li> {@link BluetoothDevice#EXTRA_DEVICE} - The remote device. </li> |
| Mike Lockwood | 517b04f | 2014-06-02 16:20:37 -0700 | [diff] [blame] | 62 | * </ul> |
| 63 | * |
| 64 | * <p>{@link #EXTRA_STATE} or {@link #EXTRA_PREVIOUS_STATE} can be any of |
| 65 | * {@link #STATE_DISCONNECTED}, {@link #STATE_CONNECTING}, |
| 66 | * {@link #STATE_CONNECTED}, {@link #STATE_DISCONNECTING}. |
| Mike Lockwood | 517b04f | 2014-06-02 16:20:37 -0700 | [diff] [blame] | 67 | */ |
| Jeff Sharkey | 8f80e4a | 2021-04-02 08:06:09 -0600 | [diff] [blame] | 68 | @RequiresLegacyBluetoothPermission |
| 69 | @RequiresBluetoothConnectPermission |
| 70 | @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT) |
| Jeff Sharkey | d7c5566 | 2021-04-20 12:30:37 -0600 | [diff] [blame] | 71 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
| Mike Lockwood | 517b04f | 2014-06-02 16:20:37 -0700 | [diff] [blame] | 72 | public static final String ACTION_CONNECTION_STATE_CHANGED = |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 73 | "android.bluetooth.avrcp-controller.profile.action.CONNECTION_STATE_CHANGED"; |
| Sanket Agarwal | 25e84d4 | 2015-10-21 18:23:27 -0700 | [diff] [blame] | 74 | |
| 75 | /** |
| Sanket Agarwal | 25e84d4 | 2015-10-21 18:23:27 -0700 | [diff] [blame] | 76 | * Intent used to broadcast the change in player application setting state on AVRCP AG. |
| 77 | * |
| 78 | * <p>This intent will have the following extras: |
| 79 | * <ul> |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 80 | * <li> {@link #EXTRA_PLAYER_SETTING} - {@link BluetoothAvrcpPlayerSettings} containing the |
| 81 | * most recent player setting. </li> |
| Sanket Agarwal | 25e84d4 | 2015-10-21 18:23:27 -0700 | [diff] [blame] | 82 | * </ul> |
| 83 | */ |
| Jeff Sharkey | d7c5566 | 2021-04-20 12:30:37 -0600 | [diff] [blame] | 84 | @RequiresBluetoothConnectPermission |
| 85 | @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT) |
| 86 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
| Sanket Agarwal | 25e84d4 | 2015-10-21 18:23:27 -0700 | [diff] [blame] | 87 | public static final String ACTION_PLAYER_SETTING = |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 88 | "android.bluetooth.avrcp-controller.profile.action.PLAYER_SETTING"; |
| Sanket Agarwal | 25e84d4 | 2015-10-21 18:23:27 -0700 | [diff] [blame] | 89 | |
| Sanket Agarwal | 25e84d4 | 2015-10-21 18:23:27 -0700 | [diff] [blame] | 90 | public static final String EXTRA_PLAYER_SETTING = |
| 91 | "android.bluetooth.avrcp-controller.profile.extra.PLAYER_SETTING"; |
| 92 | |
| Jeff Sharkey | f9e176c | 2021-04-22 16:01:29 -0600 | [diff] [blame] | 93 | private final BluetoothAdapter mAdapter; |
| 94 | private final AttributionSource mAttributionSource; |
| Ugo Yu | 70d7659 | 2019-03-26 21:38:08 +0800 | [diff] [blame] | 95 | private final BluetoothProfileConnector<IBluetoothAvrcpController> mProfileConnector = |
| 96 | new BluetoothProfileConnector(this, BluetoothProfile.AVRCP_CONTROLLER, |
| 97 | "BluetoothAvrcpController", IBluetoothAvrcpController.class.getName()) { |
| 98 | @Override |
| 99 | public IBluetoothAvrcpController getServiceInterface(IBinder service) { |
| William Escande | 9a4b7c1 | 2021-12-16 16:07:55 +0100 | [diff] [blame] | 100 | return IBluetoothAvrcpController.Stub.asInterface(service); |
| Mike Lockwood | 517b04f | 2014-06-02 16:20:37 -0700 | [diff] [blame] | 101 | } |
| Ugo Yu | 70d7659 | 2019-03-26 21:38:08 +0800 | [diff] [blame] | 102 | }; |
| Mike Lockwood | 517b04f | 2014-06-02 16:20:37 -0700 | [diff] [blame] | 103 | |
| 104 | /** |
| 105 | * Create a BluetoothAvrcpController proxy object for interacting with the local |
| 106 | * Bluetooth AVRCP service. |
| Mike Lockwood | 517b04f | 2014-06-02 16:20:37 -0700 | [diff] [blame] | 107 | */ |
| Jeff Sharkey | f9e176c | 2021-04-22 16:01:29 -0600 | [diff] [blame] | 108 | /* package */ BluetoothAvrcpController(Context context, ServiceListener listener, |
| 109 | BluetoothAdapter adapter) { |
| 110 | mAdapter = adapter; |
| 111 | mAttributionSource = adapter.getAttributionSource(); |
| Ugo Yu | 70d7659 | 2019-03-26 21:38:08 +0800 | [diff] [blame] | 112 | mProfileConnector.connect(context, listener); |
| Mike Lockwood | 517b04f | 2014-06-02 16:20:37 -0700 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | /*package*/ void close() { |
| Ugo Yu | 70d7659 | 2019-03-26 21:38:08 +0800 | [diff] [blame] | 116 | mProfileConnector.disconnect(); |
| 117 | } |
| Mike Lockwood | 517b04f | 2014-06-02 16:20:37 -0700 | [diff] [blame] | 118 | |
| Ugo Yu | 70d7659 | 2019-03-26 21:38:08 +0800 | [diff] [blame] | 119 | private IBluetoothAvrcpController getService() { |
| 120 | return mProfileConnector.getService(); |
| Mike Lockwood | 517b04f | 2014-06-02 16:20:37 -0700 | [diff] [blame] | 121 | } |
| 122 | |
| Jack He | 9e045d2 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 123 | @Override |
| Mike Lockwood | 517b04f | 2014-06-02 16:20:37 -0700 | [diff] [blame] | 124 | public void finalize() { |
| 125 | close(); |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * {@inheritDoc} |
| 130 | */ |
| Jack He | 9e045d2 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 131 | @Override |
| Jeff Sharkey | 3614e0a | 2021-04-16 15:34:54 -0600 | [diff] [blame] | 132 | @RequiresBluetoothConnectPermission |
| 133 | @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT) |
| Mike Lockwood | 517b04f | 2014-06-02 16:20:37 -0700 | [diff] [blame] | 134 | public List<BluetoothDevice> getConnectedDevices() { |
| 135 | if (VDBG) log("getConnectedDevices()"); |
| William Escande | 9a4b7c1 | 2021-12-16 16:07:55 +0100 | [diff] [blame] | 136 | final IBluetoothAvrcpController service = getService(); |
| 137 | final List<BluetoothDevice> defaultValue = new ArrayList<BluetoothDevice>(); |
| 138 | if (service == null) { |
| 139 | Log.w(TAG, "Proxy not attached to service"); |
| 140 | if (DBG) log(Log.getStackTraceString(new Throwable())); |
| 141 | } else if (isEnabled()) { |
| Mike Lockwood | 517b04f | 2014-06-02 16:20:37 -0700 | [diff] [blame] | 142 | try { |
| William Escande | 9a4b7c1 | 2021-12-16 16:07:55 +0100 | [diff] [blame] | 143 | final SynchronousResultReceiver<List<BluetoothDevice>> recv = |
| Etienne Ruffieux | 02dcbe7 | 2022-06-22 13:54:25 -0700 | [diff] [blame] | 144 | SynchronousResultReceiver.get(); |
| William Escande | 9a4b7c1 | 2021-12-16 16:07:55 +0100 | [diff] [blame] | 145 | service.getConnectedDevices(mAttributionSource, recv); |
| Jeff Sharkey | 98f3044 | 2021-06-03 09:26:53 -0600 | [diff] [blame] | 146 | return Attributable.setAttributionSource( |
| William Escande | 9a4b7c1 | 2021-12-16 16:07:55 +0100 | [diff] [blame] | 147 | recv.awaitResultNoInterrupt(getSyncTimeout()).getValue(defaultValue), |
| 148 | mAttributionSource); |
| 149 | } catch (RemoteException | TimeoutException e) { |
| 150 | Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable())); |
| Mike Lockwood | 517b04f | 2014-06-02 16:20:37 -0700 | [diff] [blame] | 151 | } |
| 152 | } |
| William Escande | 9a4b7c1 | 2021-12-16 16:07:55 +0100 | [diff] [blame] | 153 | return defaultValue; |
| Mike Lockwood | 517b04f | 2014-06-02 16:20:37 -0700 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | /** |
| 157 | * {@inheritDoc} |
| 158 | */ |
| Jack He | 9e045d2 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 159 | @Override |
| Jeff Sharkey | 3614e0a | 2021-04-16 15:34:54 -0600 | [diff] [blame] | 160 | @RequiresBluetoothConnectPermission |
| 161 | @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT) |
| Mike Lockwood | 517b04f | 2014-06-02 16:20:37 -0700 | [diff] [blame] | 162 | public List<BluetoothDevice> getDevicesMatchingConnectionStates(int[] states) { |
| 163 | if (VDBG) log("getDevicesMatchingStates()"); |
| William Escande | 9a4b7c1 | 2021-12-16 16:07:55 +0100 | [diff] [blame] | 164 | final IBluetoothAvrcpController service = getService(); |
| 165 | final List<BluetoothDevice> defaultValue = new ArrayList<BluetoothDevice>(); |
| 166 | if (service == null) { |
| 167 | Log.w(TAG, "Proxy not attached to service"); |
| 168 | if (DBG) log(Log.getStackTraceString(new Throwable())); |
| 169 | } else if (isEnabled()) { |
| Mike Lockwood | 517b04f | 2014-06-02 16:20:37 -0700 | [diff] [blame] | 170 | try { |
| William Escande | 9a4b7c1 | 2021-12-16 16:07:55 +0100 | [diff] [blame] | 171 | final SynchronousResultReceiver<List<BluetoothDevice>> recv = |
| Etienne Ruffieux | 02dcbe7 | 2022-06-22 13:54:25 -0700 | [diff] [blame] | 172 | SynchronousResultReceiver.get(); |
| William Escande | 9a4b7c1 | 2021-12-16 16:07:55 +0100 | [diff] [blame] | 173 | service.getDevicesMatchingConnectionStates(states, mAttributionSource, recv); |
| Jeff Sharkey | 98f3044 | 2021-06-03 09:26:53 -0600 | [diff] [blame] | 174 | return Attributable.setAttributionSource( |
| William Escande | 9a4b7c1 | 2021-12-16 16:07:55 +0100 | [diff] [blame] | 175 | recv.awaitResultNoInterrupt(getSyncTimeout()).getValue(defaultValue), |
| Jeff Sharkey | 43ee69e | 2021-04-23 14:13:57 -0600 | [diff] [blame] | 176 | mAttributionSource); |
| William Escande | 9a4b7c1 | 2021-12-16 16:07:55 +0100 | [diff] [blame] | 177 | } catch (RemoteException | TimeoutException e) { |
| 178 | Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable())); |
| Mike Lockwood | 517b04f | 2014-06-02 16:20:37 -0700 | [diff] [blame] | 179 | } |
| 180 | } |
| William Escande | 9a4b7c1 | 2021-12-16 16:07:55 +0100 | [diff] [blame] | 181 | return defaultValue; |
| Mike Lockwood | 517b04f | 2014-06-02 16:20:37 -0700 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | /** |
| 185 | * {@inheritDoc} |
| 186 | */ |
| Jack He | 9e045d2 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 187 | @Override |
| Jeff Sharkey | 3614e0a | 2021-04-16 15:34:54 -0600 | [diff] [blame] | 188 | @RequiresBluetoothConnectPermission |
| 189 | @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT) |
| Mike Lockwood | 517b04f | 2014-06-02 16:20:37 -0700 | [diff] [blame] | 190 | public int getConnectionState(BluetoothDevice device) { |
| 191 | if (VDBG) log("getState(" + device + ")"); |
| William Escande | 9a4b7c1 | 2021-12-16 16:07:55 +0100 | [diff] [blame] | 192 | final IBluetoothAvrcpController service = getService(); |
| 193 | final int defaultValue = BluetoothProfile.STATE_DISCONNECTED; |
| 194 | if (service == null) { |
| 195 | Log.w(TAG, "Proxy not attached to service"); |
| 196 | if (DBG) log(Log.getStackTraceString(new Throwable())); |
| 197 | } else if (isEnabled() && isValidDevice(device)) { |
| Mike Lockwood | 517b04f | 2014-06-02 16:20:37 -0700 | [diff] [blame] | 198 | try { |
| Etienne Ruffieux | 02dcbe7 | 2022-06-22 13:54:25 -0700 | [diff] [blame] | 199 | final SynchronousResultReceiver<Integer> recv = SynchronousResultReceiver.get(); |
| William Escande | 9a4b7c1 | 2021-12-16 16:07:55 +0100 | [diff] [blame] | 200 | service.getConnectionState(device, mAttributionSource, recv); |
| 201 | return recv.awaitResultNoInterrupt(getSyncTimeout()).getValue(defaultValue); |
| 202 | } catch (RemoteException | TimeoutException e) { |
| 203 | Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable())); |
| Mike Lockwood | 517b04f | 2014-06-02 16:20:37 -0700 | [diff] [blame] | 204 | } |
| 205 | } |
| William Escande | 9a4b7c1 | 2021-12-16 16:07:55 +0100 | [diff] [blame] | 206 | return defaultValue; |
| Mike Lockwood | 517b04f | 2014-06-02 16:20:37 -0700 | [diff] [blame] | 207 | } |
| 208 | |
| Sanket Agarwal | 25e84d4 | 2015-10-21 18:23:27 -0700 | [diff] [blame] | 209 | /** |
| 210 | * Gets the player application settings. |
| 211 | * |
| 212 | * @return the {@link BluetoothAvrcpPlayerSettings} or {@link null} if there is an error. |
| 213 | */ |
| Jeff Sharkey | 3614e0a | 2021-04-16 15:34:54 -0600 | [diff] [blame] | 214 | @RequiresBluetoothConnectPermission |
| 215 | @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT) |
| Sanket Agarwal | 25e84d4 | 2015-10-21 18:23:27 -0700 | [diff] [blame] | 216 | public BluetoothAvrcpPlayerSettings getPlayerSettings(BluetoothDevice device) { |
| 217 | if (DBG) Log.d(TAG, "getPlayerSettings"); |
| 218 | BluetoothAvrcpPlayerSettings settings = null; |
| William Escande | 9a4b7c1 | 2021-12-16 16:07:55 +0100 | [diff] [blame] | 219 | final IBluetoothAvrcpController service = getService(); |
| 220 | final BluetoothAvrcpPlayerSettings defaultValue = null; |
| 221 | if (service == null) { |
| 222 | Log.w(TAG, "Proxy not attached to service"); |
| 223 | if (DBG) log(Log.getStackTraceString(new Throwable())); |
| 224 | } else if (isEnabled()) { |
| Sanket Agarwal | 25e84d4 | 2015-10-21 18:23:27 -0700 | [diff] [blame] | 225 | try { |
| William Escande | 9a4b7c1 | 2021-12-16 16:07:55 +0100 | [diff] [blame] | 226 | final SynchronousResultReceiver<BluetoothAvrcpPlayerSettings> recv = |
| Etienne Ruffieux | 02dcbe7 | 2022-06-22 13:54:25 -0700 | [diff] [blame] | 227 | SynchronousResultReceiver.get(); |
| William Escande | 9a4b7c1 | 2021-12-16 16:07:55 +0100 | [diff] [blame] | 228 | service.getPlayerSettings(device, mAttributionSource, recv); |
| 229 | settings = recv.awaitResultNoInterrupt(getSyncTimeout()).getValue(defaultValue); |
| 230 | } catch (RemoteException | TimeoutException e) { |
| 231 | Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable())); |
| Sanket Agarwal | 25e84d4 | 2015-10-21 18:23:27 -0700 | [diff] [blame] | 232 | } |
| 233 | } |
| William Escande | 9a4b7c1 | 2021-12-16 16:07:55 +0100 | [diff] [blame] | 234 | return defaultValue; |
| Sanket Agarwal | 25e84d4 | 2015-10-21 18:23:27 -0700 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | /** |
| Sanket Agarwal | 25e84d4 | 2015-10-21 18:23:27 -0700 | [diff] [blame] | 238 | * Sets the player app setting for current player. |
| 239 | * returns true in case setting is supported by remote, false otherwise |
| 240 | */ |
| Jeff Sharkey | 3614e0a | 2021-04-16 15:34:54 -0600 | [diff] [blame] | 241 | @RequiresBluetoothConnectPermission |
| 242 | @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT) |
| Sanket Agarwal | 25e84d4 | 2015-10-21 18:23:27 -0700 | [diff] [blame] | 243 | public boolean setPlayerApplicationSetting(BluetoothAvrcpPlayerSettings plAppSetting) { |
| 244 | if (DBG) Log.d(TAG, "setPlayerApplicationSetting"); |
| William Escande | 9a4b7c1 | 2021-12-16 16:07:55 +0100 | [diff] [blame] | 245 | final IBluetoothAvrcpController service = getService(); |
| 246 | final boolean defaultValue = false; |
| 247 | if (service == null) { |
| 248 | Log.w(TAG, "Proxy not attached to service"); |
| 249 | if (DBG) log(Log.getStackTraceString(new Throwable())); |
| 250 | } else if (isEnabled()) { |
| Sanket Agarwal | 25e84d4 | 2015-10-21 18:23:27 -0700 | [diff] [blame] | 251 | try { |
| Etienne Ruffieux | 02dcbe7 | 2022-06-22 13:54:25 -0700 | [diff] [blame] | 252 | final SynchronousResultReceiver<Boolean> recv = SynchronousResultReceiver.get(); |
| William Escande | 9a4b7c1 | 2021-12-16 16:07:55 +0100 | [diff] [blame] | 253 | service.setPlayerApplicationSetting(plAppSetting, mAttributionSource, recv); |
| 254 | return recv.awaitResultNoInterrupt(getSyncTimeout()).getValue(defaultValue); |
| 255 | } catch (RemoteException | TimeoutException e) { |
| 256 | Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable())); |
| Sanket Agarwal | 25e84d4 | 2015-10-21 18:23:27 -0700 | [diff] [blame] | 257 | } |
| 258 | } |
| William Escande | 9a4b7c1 | 2021-12-16 16:07:55 +0100 | [diff] [blame] | 259 | return defaultValue; |
| Sanket Agarwal | 25e84d4 | 2015-10-21 18:23:27 -0700 | [diff] [blame] | 260 | } |
| 261 | |
| Jack He | 9e045d2 | 2017-08-22 21:21:23 -0700 | [diff] [blame] | 262 | /** |
| Sanket Agarwal | 25e84d4 | 2015-10-21 18:23:27 -0700 | [diff] [blame] | 263 | * Send Group Navigation Command to Remote. |
| 264 | * possible keycode values: next_grp, previous_grp defined above |
| 265 | */ |
| Jeff Sharkey | 3614e0a | 2021-04-16 15:34:54 -0600 | [diff] [blame] | 266 | @RequiresBluetoothConnectPermission |
| 267 | @RequiresPermission(android.Manifest.permission.BLUETOOTH_CONNECT) |
| Sanket Agarwal | 25e84d4 | 2015-10-21 18:23:27 -0700 | [diff] [blame] | 268 | public void sendGroupNavigationCmd(BluetoothDevice device, int keyCode, int keyState) { |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 269 | Log.d(TAG, "sendGroupNavigationCmd dev = " + device + " key " + keyCode + " State = " |
| 270 | + keyState); |
| William Escande | 9a4b7c1 | 2021-12-16 16:07:55 +0100 | [diff] [blame] | 271 | final IBluetoothAvrcpController service = getService(); |
| 272 | if (service == null) { |
| 273 | Log.w(TAG, "Proxy not attached to service"); |
| 274 | if (DBG) log(Log.getStackTraceString(new Throwable())); |
| 275 | } else if (isEnabled()) { |
| Sanket Agarwal | 25e84d4 | 2015-10-21 18:23:27 -0700 | [diff] [blame] | 276 | try { |
| Etienne Ruffieux | 02dcbe7 | 2022-06-22 13:54:25 -0700 | [diff] [blame] | 277 | final SynchronousResultReceiver recv = SynchronousResultReceiver.get(); |
| William Escande | 9a4b7c1 | 2021-12-16 16:07:55 +0100 | [diff] [blame] | 278 | service.sendGroupNavigationCmd(device, keyCode, keyState, mAttributionSource, recv); |
| 279 | recv.awaitResultNoInterrupt(getSyncTimeout()).getValue(null); |
| Sanket Agarwal | 25e84d4 | 2015-10-21 18:23:27 -0700 | [diff] [blame] | 280 | return; |
| William Escande | 9a4b7c1 | 2021-12-16 16:07:55 +0100 | [diff] [blame] | 281 | } catch (RemoteException | TimeoutException e) { |
| 282 | Log.e(TAG, e.toString() + "\n" + Log.getStackTraceString(new Throwable())); |
| Sanket Agarwal | 25e84d4 | 2015-10-21 18:23:27 -0700 | [diff] [blame] | 283 | } |
| 284 | } |
| Sanket Agarwal | 25e84d4 | 2015-10-21 18:23:27 -0700 | [diff] [blame] | 285 | } |
| 286 | |
| Mike Lockwood | 517b04f | 2014-06-02 16:20:37 -0700 | [diff] [blame] | 287 | private boolean isEnabled() { |
| Jack He | 1f686f6 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 288 | return mAdapter.getState() == BluetoothAdapter.STATE_ON; |
| Mike Lockwood | 517b04f | 2014-06-02 16:20:37 -0700 | [diff] [blame] | 289 | } |
| 290 | |
| Jack He | 1f686f6 | 2017-08-17 12:11:18 -0700 | [diff] [blame] | 291 | private static boolean isValidDevice(BluetoothDevice device) { |
| 292 | return device != null && BluetoothAdapter.checkBluetoothAddress(device.getAddress()); |
| Mike Lockwood | 517b04f | 2014-06-02 16:20:37 -0700 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | private static void log(String msg) { |
| Jack He | 910201b | 2017-08-22 16:06:54 -0700 | [diff] [blame] | 296 | Log.d(TAG, msg); |
| Mike Lockwood | 517b04f | 2014-06-02 16:20:37 -0700 | [diff] [blame] | 297 | } |
| 298 | } |