Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit f182674d authored by bengris32's avatar bengris32 Committed by Daniel Jacob Chittoor
Browse files

2e: 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: default avatarbengris32 <bengris32@protonmail.ch>
parent 4bfb6af2
Loading
Loading
Loading
Loading
+16 −0
Original line number Diff line number Diff line
package org.lineageos.mediatek.incallservice;

import android.media.AudioSystem;

import android.util.Log;

public class GainUtils {
    public static final String LOG_TAG = "MtkInCallService";

    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);
    }
}
+12 −17
Original line number Diff line number Diff line
@@ -15,36 +15,31 @@ 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();
            if (callDevice.getInternalType() != AudioDeviceInfo.TYPE_BUILTIN_EARPIECE) {
            if (callDevice.getType() != AudioDeviceInfo.TYPE_BUILTIN_EARPIECE) {
                // Device is not the built in earpiece, we don't need to do anything.
                return;
            }

            // 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);
    }
}
+4 −1
Original line number Diff line number Diff line
@@ -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;