1 /* 2 * Copyright 2021 HIMSA II K/S - www.himsa.com. 3 * Represented by EHIMA - www.ehima.com 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package com.android.bluetooth.vc; 19 20 import static java.util.Objects.requireNonNull; 21 22 import android.bluetooth.BluetoothDevice; 23 24 import com.android.bluetooth.Utils; 25 26 import java.lang.annotation.Native; 27 28 public class VolumeControlNativeInterface { 29 private static final String TAG = VolumeControlNativeInterface.class.getSimpleName(); 30 31 // Value access from native, see com_android_bluetooth_vc.cpp 32 @Native private final VolumeControlNativeCallback mNativeCallback; 33 VolumeControlNativeInterface(VolumeControlNativeCallback nativeCallback)34 VolumeControlNativeInterface(VolumeControlNativeCallback nativeCallback) { 35 mNativeCallback = requireNonNull(nativeCallback); 36 } 37 init()38 void init() { 39 initNative(); 40 } 41 cleanup()42 void cleanup() { 43 cleanupNative(); 44 } 45 getByteAddress(BluetoothDevice device)46 private static byte[] getByteAddress(BluetoothDevice device) { 47 if (device == null) { 48 return Utils.getBytesFromAddress("00:00:00:00:00:00"); 49 } 50 return Utils.getBytesFromAddress(device.getAddress()); 51 } 52 connectVolumeControl(BluetoothDevice device)53 boolean connectVolumeControl(BluetoothDevice device) { 54 return connectVolumeControlNative(getByteAddress(device)); 55 } 56 disconnectVolumeControl(BluetoothDevice device)57 boolean disconnectVolumeControl(BluetoothDevice device) { 58 return disconnectVolumeControlNative(getByteAddress(device)); 59 } 60 setVolume(BluetoothDevice device, int volume)61 void setVolume(BluetoothDevice device, int volume) { 62 setVolumeNative(getByteAddress(device), volume); 63 } 64 setGroupVolume(int groupId, int volume)65 void setGroupVolume(int groupId, int volume) { 66 setGroupVolumeNative(groupId, volume); 67 } 68 mute(BluetoothDevice device)69 void mute(BluetoothDevice device) { 70 muteNative(getByteAddress(device)); 71 } 72 muteGroup(int groupId)73 void muteGroup(int groupId) { 74 muteGroupNative(groupId); 75 } 76 unmute(BluetoothDevice device)77 void unmute(BluetoothDevice device) { 78 unmuteNative(getByteAddress(device)); 79 } 80 unmuteGroup(int groupId)81 void unmuteGroup(int groupId) { 82 unmuteGroupNative(groupId); 83 } 84 getExtAudioOutVolumeOffset(BluetoothDevice device, int externalOutputId)85 boolean getExtAudioOutVolumeOffset(BluetoothDevice device, int externalOutputId) { 86 return getExtAudioOutVolumeOffsetNative(getByteAddress(device), externalOutputId); 87 } 88 setExtAudioOutVolumeOffset(BluetoothDevice device, int externalOutputId, int offset)89 boolean setExtAudioOutVolumeOffset(BluetoothDevice device, int externalOutputId, int offset) { 90 if (Utils.isPtsTestMode()) { 91 setVolumeNative(getByteAddress(device), offset); 92 return true; 93 } 94 return setExtAudioOutVolumeOffsetNative(getByteAddress(device), externalOutputId, offset); 95 } 96 getExtAudioOutLocation(BluetoothDevice device, int externalOutputId)97 boolean getExtAudioOutLocation(BluetoothDevice device, int externalOutputId) { 98 return getExtAudioOutLocationNative(getByteAddress(device), externalOutputId); 99 } 100 setExtAudioOutLocation(BluetoothDevice device, int externalOutputId, int location)101 boolean setExtAudioOutLocation(BluetoothDevice device, int externalOutputId, int location) { 102 return setExtAudioOutLocationNative(getByteAddress(device), externalOutputId, location); 103 } 104 getExtAudioOutDescription(BluetoothDevice device, int externalOutputId)105 boolean getExtAudioOutDescription(BluetoothDevice device, int externalOutputId) { 106 return getExtAudioOutDescriptionNative(getByteAddress(device), externalOutputId); 107 } 108 setExtAudioOutDescription(BluetoothDevice device, int externalOutputId, String descr)109 boolean setExtAudioOutDescription(BluetoothDevice device, int externalOutputId, String descr) { 110 return setExtAudioOutDescriptionNative(getByteAddress(device), externalOutputId, descr); 111 } 112 getExtAudioInState(BluetoothDevice device, int externalInputId)113 boolean getExtAudioInState(BluetoothDevice device, int externalInputId) { 114 return getExtAudioInStateNative(getByteAddress(device), externalInputId); 115 } 116 getExtAudioInStatus(BluetoothDevice device, int externalInputId)117 boolean getExtAudioInStatus(BluetoothDevice device, int externalInputId) { 118 return getExtAudioInStatusNative(getByteAddress(device), externalInputId); 119 } 120 getExtAudioInType(BluetoothDevice device, int externalInputId)121 boolean getExtAudioInType(BluetoothDevice device, int externalInputId) { 122 return getExtAudioInTypeNative(getByteAddress(device), externalInputId); 123 } 124 getExtAudioInGainProps(BluetoothDevice device, int externalInputId)125 boolean getExtAudioInGainProps(BluetoothDevice device, int externalInputId) { 126 return getExtAudioInGainPropsNative(getByteAddress(device), externalInputId); 127 } 128 getExtAudioInDescription(BluetoothDevice device, int externalInputId)129 boolean getExtAudioInDescription(BluetoothDevice device, int externalInputId) { 130 return getExtAudioInDescriptionNative(getByteAddress(device), externalInputId); 131 } 132 setExtAudioInDescription(BluetoothDevice device, int externalInputId, String descr)133 boolean setExtAudioInDescription(BluetoothDevice device, int externalInputId, String descr) { 134 return setExtAudioInDescriptionNative(getByteAddress(device), externalInputId, descr); 135 } 136 setExtAudioInGainSetting(BluetoothDevice device, int externalInputId, int gainSetting)137 boolean setExtAudioInGainSetting(BluetoothDevice device, int externalInputId, int gainSetting) { 138 return setExtAudioInGainSettingNative(getByteAddress(device), externalInputId, gainSetting); 139 } 140 setExtAudioInGainMode(BluetoothDevice device, int externalInputId, int gainMode)141 boolean setExtAudioInGainMode(BluetoothDevice device, int externalInputId, int gainMode) { 142 return setExtAudioInGainModeNative(getByteAddress(device), externalInputId, gainMode); 143 } 144 setExtAudioInMute(BluetoothDevice device, int externalInputId, int mute)145 boolean setExtAudioInMute(BluetoothDevice device, int externalInputId, int mute) { 146 return setExtAudioInMuteNative(getByteAddress(device), externalInputId, mute); 147 } 148 149 // Native methods that call into the JNI interface initNative()150 private native void initNative(); 151 cleanupNative()152 private native void cleanupNative(); 153 connectVolumeControlNative(byte[] address)154 private native boolean connectVolumeControlNative(byte[] address); 155 disconnectVolumeControlNative(byte[] address)156 private native boolean disconnectVolumeControlNative(byte[] address); 157 setVolumeNative(byte[] address, int volume)158 private native void setVolumeNative(byte[] address, int volume); 159 setGroupVolumeNative(int groupId, int volume)160 private native void setGroupVolumeNative(int groupId, int volume); 161 muteNative(byte[] address)162 private native void muteNative(byte[] address); 163 muteGroupNative(int groupId)164 private native void muteGroupNative(int groupId); 165 unmuteNative(byte[] address)166 private native void unmuteNative(byte[] address); 167 unmuteGroupNative(int groupId)168 private native void unmuteGroupNative(int groupId); 169 getExtAudioOutVolumeOffsetNative(byte[] address, int externalOutputId)170 private native boolean getExtAudioOutVolumeOffsetNative(byte[] address, int externalOutputId); 171 setExtAudioOutVolumeOffsetNative( byte[] address, int externalOutputId, int offset)172 private native boolean setExtAudioOutVolumeOffsetNative( 173 byte[] address, int externalOutputId, int offset); 174 getExtAudioOutLocationNative(byte[] address, int externalOutputId)175 private native boolean getExtAudioOutLocationNative(byte[] address, int externalOutputId); 176 setExtAudioOutLocationNative( byte[] address, int externalOutputId, int location)177 private native boolean setExtAudioOutLocationNative( 178 byte[] address, int externalOutputId, int location); 179 getExtAudioOutDescriptionNative(byte[] address, int externalOutputId)180 private native boolean getExtAudioOutDescriptionNative(byte[] address, int externalOutputId); 181 setExtAudioOutDescriptionNative( byte[] address, int externalOutputId, String descr)182 private native boolean setExtAudioOutDescriptionNative( 183 byte[] address, int externalOutputId, String descr); 184 185 /* Native methods for audio inputs control service */ getExtAudioInStateNative(byte[] address, int externalInputId)186 private native boolean getExtAudioInStateNative(byte[] address, int externalInputId); 187 getExtAudioInStatusNative(byte[] address, int externalInputId)188 private native boolean getExtAudioInStatusNative(byte[] address, int externalInputId); 189 getExtAudioInTypeNative(byte[] address, int externalInputId)190 private native boolean getExtAudioInTypeNative(byte[] address, int externalInputId); 191 getExtAudioInGainPropsNative(byte[] address, int externalInputId)192 private native boolean getExtAudioInGainPropsNative(byte[] address, int externalInputId); 193 getExtAudioInDescriptionNative(byte[] address, int externalInputId)194 private native boolean getExtAudioInDescriptionNative(byte[] address, int externalInputId); 195 setExtAudioInDescriptionNative( byte[] address, int externalInputId, String descr)196 private native boolean setExtAudioInDescriptionNative( 197 byte[] address, int externalInputId, String descr); 198 setExtAudioInGainSettingNative( byte[] address, int externalInputId, int gainSetting)199 private native boolean setExtAudioInGainSettingNative( 200 byte[] address, int externalInputId, int gainSetting); 201 setExtAudioInGainModeNative( byte[] address, int externalInputId, int gainMode)202 private native boolean setExtAudioInGainModeNative( 203 byte[] address, int externalInputId, int gainMode); 204 setExtAudioInMuteNative(byte[] address, int externalInputId, int mute)205 private native boolean setExtAudioInMuteNative(byte[] address, int externalInputId, int mute); 206 } 207