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

Commit 4767690f authored by Mike Lockwood's avatar Mike Lockwood Committed by Mike Lockwood
Browse files

AudioManager: transparently convert volume settings for other streams to...


AudioManager: transparently convert volume settings for other streams to master volume if config_useMasterVolume is set.

This allows Music2 and other media apps to control master volume without changing their code

Bug: 5567694

Signed-off-by: default avatarMike Lockwood <lockwood@android.com>
parent 79824179
Loading
Loading
Loading
Loading
+4 −8
Original line number Diff line number Diff line
@@ -93,7 +93,7 @@ public class VolumePanel extends Handler implements OnSeekBarChangeListener, Vie
    private static final int MSG_RINGER_MODE_CHANGED = 6;

    // Pseudo stream type for master volume
    private static final int STREAM_MASTER = -1;
    private static final int STREAM_MASTER = -100;

    protected Context mContext;
    private AudioManager mAudioManager;
@@ -303,34 +303,30 @@ public class VolumePanel extends Handler implements OnSeekBarChangeListener, Vie
    }

    private int getStreamMaxVolume(int streamType) {
        // master volume is 0.0f - 1.0f, but we will use 0 - 100 internally
        if (streamType == STREAM_MASTER) {
            return 100;
            return mAudioService.getMasterMaxVolume();
        } else {
            return mAudioService.getStreamMaxVolume(streamType);
        }
    }

    private int getStreamVolume(int streamType) {
         // master volume is 0.0f - 1.0f, but we will use 0 - 100 internally
        if (streamType == STREAM_MASTER) {
            return Math.round(mAudioService.getMasterVolume() * 100);
            return mAudioService.getMasterVolume();
        } else {
            return mAudioService.getStreamVolume(streamType);
        }
    }

    private void setStreamVolume(int streamType, int index, int flags) {
         // master volume is 0.0f - 1.0f, but we will use 0 - 100 internally
        if (streamType == STREAM_MASTER) {
            mAudioService.setMasterVolume((float)index / 100.0f);
            mAudioService.setMasterVolume(index, flags);
        } else {
            mAudioService.setStreamVolume(streamType, index, flags);
        }
    }

    private int getLastAudibleStreamVolume(int streamType) {
         // master volume is 0.0f - 1.0f, but we will use 0 - 100 internally
        if (streamType == STREAM_MASTER) {
            // master volume mute not yet supported
            return getStreamVolume(STREAM_MASTER);
+35 −7
Original line number Diff line number Diff line
@@ -485,7 +485,11 @@ public class AudioManager {
    public void adjustStreamVolume(int streamType, int direction, int flags) {
        IAudioService service = getService();
        try {
            if (mUseMasterVolume) {
                service.adjustMasterVolume(direction, flags);
            } else {
                service.adjustStreamVolume(streamType, direction, flags);
            }
        } catch (RemoteException e) {
            Log.e(TAG, "Dead object in adjustStreamVolume", e);
        }
@@ -511,7 +515,11 @@ public class AudioManager {
    public void adjustVolume(int direction, int flags) {
        IAudioService service = getService();
        try {
            if (mUseMasterVolume) {
                service.adjustMasterVolume(direction, flags);
            } else {
                service.adjustVolume(direction, flags);
            }
        } catch (RemoteException e) {
            Log.e(TAG, "Dead object in adjustVolume", e);
        }
@@ -537,7 +545,11 @@ public class AudioManager {
    public void adjustSuggestedStreamVolume(int direction, int suggestedStreamType, int flags) {
        IAudioService service = getService();
        try {
            if (mUseMasterVolume) {
                service.adjustMasterVolume(direction, flags);
            } else {
                service.adjustSuggestedStreamVolume(direction, suggestedStreamType, flags);
            }
        } catch (RemoteException e) {
            Log.e(TAG, "Dead object in adjustSuggestedStreamVolume", e);
        }
@@ -603,7 +615,11 @@ public class AudioManager {
    public int getStreamMaxVolume(int streamType) {
        IAudioService service = getService();
        try {
            if (mUseMasterVolume) {
                return service.getMasterMaxVolume();
            } else {
                return service.getStreamMaxVolume(streamType);
            }
        } catch (RemoteException e) {
            Log.e(TAG, "Dead object in getStreamMaxVolume", e);
            return 0;
@@ -621,7 +637,11 @@ public class AudioManager {
    public int getStreamVolume(int streamType) {
        IAudioService service = getService();
        try {
            if (mUseMasterVolume) {
                return service.getMasterVolume();
            } else {
                return service.getStreamVolume(streamType);
            }
        } catch (RemoteException e) {
            Log.e(TAG, "Dead object in getStreamVolume", e);
            return 0;
@@ -636,7 +656,11 @@ public class AudioManager {
    public int getLastAudibleStreamVolume(int streamType) {
        IAudioService service = getService();
        try {
            if (mUseMasterVolume) {
                return service.getMasterVolume();
            } else {
                return service.getLastAudibleStreamVolume(streamType);
            }
        } catch (RemoteException e) {
            Log.e(TAG, "Dead object in getLastAudibleStreamVolume", e);
            return 0;
@@ -679,7 +703,11 @@ public class AudioManager {
    public void setStreamVolume(int streamType, int index, int flags) {
        IAudioService service = getService();
        try {
            if (mUseMasterVolume) {
                service.setMasterVolume(index, flags);
            } else {
                service.setStreamVolume(streamType, index, flags);
            }
        } catch (RemoteException e) {
            Log.e(TAG, "Dead object in setStreamVolume", e);
        }
+11 −4
Original line number Diff line number Diff line
@@ -158,6 +158,10 @@ public class AudioService extends IAudioService.Stub {
    private static final int NUM_SOUNDPOOL_CHANNELS = 4;
    private static final int SOUND_EFFECT_VOLUME = 1000;

    // Internally master volume is a float in the 0.0 - 1.0 range,
    // but to support integer based AudioManager API we translate it to 0 - 100
    private static final int MAX_MASTER_VOLUME = 100;

    /* Sound effect file names  */
    private static final String SOUND_EFFECTS_PATH = "/media/audio/ui/";
    private static final String[] SOUND_EFFECT_FILES = new String[] {
@@ -765,12 +769,12 @@ public class AudioService extends IAudioService.Stub {
        return (mStreamStates[streamType].getIndex(device, false  /* lastAudible */) + 5) / 10;
    }

    public float getMasterVolume() {
        return AudioSystem.getMasterVolume();
    public int getMasterVolume() {
        return Math.round(AudioSystem.getMasterVolume() * MAX_MASTER_VOLUME);
    }

    public void setMasterVolume(float volume) {
        AudioSystem.setMasterVolume(volume);
    public void setMasterVolume(int volume, int flags) {
        AudioSystem.setMasterVolume((float)volume / MAX_MASTER_VOLUME);
    }

    /** @see AudioManager#getStreamMaxVolume(int) */
@@ -779,6 +783,9 @@ public class AudioService extends IAudioService.Stub {
        return (mStreamStates[streamType].getMaxIndex() + 5) / 10;
    }

    public int getMasterMaxVolume() {
        return MAX_MASTER_VOLUME;
    }

    /** Get last audible volume before stream was muted. */
    public int getLastAudibleStreamVolume(int streamType) {
+5 −1
Original line number Diff line number Diff line
@@ -37,6 +37,8 @@ interface IAudioService {

    void setStreamVolume(int streamType, int index, int flags);

    void setMasterVolume(int index, int flags);
    
    void setStreamSolo(int streamType, boolean state, IBinder cb);
   	
    void setStreamMute(int streamType, boolean state, IBinder cb);
@@ -45,10 +47,12 @@ interface IAudioService {

    int getStreamVolume(int streamType);

    float getMasterVolume();
    int getMasterVolume();

    int getStreamMaxVolume(int streamType);

    int getMasterMaxVolume();
    
    int getLastAudibleStreamVolume(int streamType);

    void setRingerMode(int ringerMode);