mirror of
https://github.com/Evolution-X-Devices/device_xiaomi_rosemary
synced 2026-01-27 13:35:09 +00:00
rosemary: InCallService: Bugfixes and refactor
* Refactor and cleanup code * Use proper way of getting audioDevice with getPort().type() * Stop mixing up values between AudioSystem and AudioManager, as values between AudioManager (java) and AudioSystem (native) could change and become different from eachother. Signed-off-by: bengris32 <bengris32@protonmail.ch> Change-Id: I2311ed02b88148b71b374b19aaaf458f82f23824
This commit is contained in:
committed by
Matsvei Niaverau
parent
510e3cda72
commit
ced33fe9d4
@@ -0,0 +1,16 @@
|
||||
package org.lineageos.mediatek.incallservice;
|
||||
|
||||
import android.media.AudioSystem;
|
||||
|
||||
import android.util.Log;
|
||||
|
||||
public class GainUtils {
|
||||
public static final String LOG_TAG = "MediatekInCallService";
|
||||
|
||||
public static void setGainLevel(int audioDevice, int gainIndex, int streamType) {
|
||||
String parameters = String.format("volumeDevice=%d;volumeIndex=%d;volumeStreamType=%d",
|
||||
audioDevice, Math.min(7, gainIndex), streamType);
|
||||
Log.d(LOG_TAG, "Setting audio parameters to: " + parameters);
|
||||
AudioSystem.setParameters(parameters);
|
||||
}
|
||||
}
|
||||
@@ -15,32 +15,27 @@ public class VolumeChangeReceiver extends BroadcastReceiver {
|
||||
|
||||
private AudioManager mAudioManager;
|
||||
|
||||
public VolumeChangeReceiver(Context context) {
|
||||
mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
|
||||
public VolumeChangeReceiver(AudioManager audioManager) {
|
||||
mAudioManager = audioManager;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
int streamType = intent.getIntExtra(AudioManager.EXTRA_VOLUME_STREAM_TYPE, -1);
|
||||
if (streamType == AudioSystem.STREAM_VOICE_CALL) {
|
||||
private void handleVolumeStateChange(Intent intent) {
|
||||
if (intent.getIntExtra(AudioManager.EXTRA_VOLUME_STREAM_TYPE, -1) == AudioManager.STREAM_VOICE_CALL) {
|
||||
AudioDeviceInfo callDevice = mAudioManager.getCommunicationDevice();
|
||||
|
||||
// Start building parameters
|
||||
String parameters = "volumeDevice=" + (callDevice.getId() - 1) + ";";
|
||||
// Try to get volumeIndex
|
||||
int volumeIndex = intent.getIntExtra(AudioManager.EXTRA_VOLUME_STREAM_VALUE, -1);
|
||||
if (volumeIndex < 0) {
|
||||
Log.w(LOG_TAG, "Could not get volumeIndex!");
|
||||
return;
|
||||
}
|
||||
|
||||
// Limit volumeIndex to a max of 7 since that's the size of
|
||||
// MediaTek's gain table.
|
||||
parameters += "volumeIndex=" + Math.min(7, volumeIndex) + ";";
|
||||
parameters += "volumeStreamType=" + streamType;
|
||||
|
||||
// Set gain parameters
|
||||
Log.d(LOG_TAG, "Setting audio parameters: " + parameters);
|
||||
AudioSystem.setParameters(parameters);
|
||||
GainUtils.setGainLevel(callDevice.getPort().type(), volumeIndex, AudioSystem.STREAM_VOICE_CALL);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
handleVolumeStateChange(intent);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,9 +29,12 @@ public class VolumeChangeService extends Service {
|
||||
@Override
|
||||
public int onStartCommand(Intent intent, int flags, int startid) {
|
||||
mContext = this;
|
||||
mVolumeChangeReceiver = new VolumeChangeReceiver(mContext);
|
||||
|
||||
AudioManager audioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
|
||||
mVolumeChangeReceiver = new VolumeChangeReceiver(audioManager);
|
||||
|
||||
Log.i(LOG_TAG, "Service is starting...");
|
||||
|
||||
this.registerReceiver(mVolumeChangeReceiver,
|
||||
new IntentFilter(AudioManager.VOLUME_CHANGED_ACTION));
|
||||
return START_STICKY;
|
||||
|
||||
Reference in New Issue
Block a user