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

Commit 0dc37cce authored by Mike Lockwood's avatar Mike Lockwood
Browse files

AudioService: Send broadcasts when master volume and mute state change

parent c3a7bb65
Loading
Loading
Loading
Loading
+44 −1
Original line number Diff line number Diff line
@@ -91,7 +91,8 @@ public class AudioManager {
     * @see #EXTRA_VIBRATE_SETTING
     */
    @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
    public static final String VIBRATE_SETTING_CHANGED_ACTION = "android.media.VIBRATE_SETTING_CHANGED";
    public static final String VIBRATE_SETTING_CHANGED_ACTION =
        "android.media.VIBRATE_SETTING_CHANGED";

    /**
     * @hide Broadcast intent when the volume for a particular stream type changes.
@@ -104,6 +105,27 @@ public class AudioManager {
    @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
    public static final String VOLUME_CHANGED_ACTION = "android.media.VOLUME_CHANGED_ACTION";

    /**
     * @hide Broadcast intent when the master volume changes.
     * Includes the new volume
     *
     * @see #EXTRA_MASTER_VOLUME_VALUE
     * @see #EXTRA_PREV_MASTER_VOLUME_VALUE
     */
    @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
    public static final String MASTER_VOLUME_CHANGED_ACTION =
        "android.media.MASTER_VOLUME_CHANGED_ACTION";

    /**
     * @hide Broadcast intent when the master mute state changes.
     * Includes the the new volume
     *
     * @see #EXTRA_MASTER_VOLUME_MUTED
     */
    @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
    public static final String MASTER_MUTE_CHANGED_ACTION =
        "android.media.MASTER_MUTE_CHANGED_ACTION";

    /**
     * The new vibrate setting for a particular type.
     *
@@ -141,6 +163,27 @@ public class AudioManager {
    public static final String EXTRA_PREV_VOLUME_STREAM_VALUE =
        "android.media.EXTRA_PREV_VOLUME_STREAM_VALUE";

    /**
     * @hide The new master volume value for the master volume changed intent.
     * Value is integer between 0 and 100 inclusive.
     */
    public static final String EXTRA_MASTER_VOLUME_VALUE =
        "android.media.EXTRA_MASTER_VOLUME_VALUE";

    /**
     * @hide The previous master volume value for the master volume changed intent.
     * Value is integer between 0 and 100 inclusive.
     */
    public static final String EXTRA_PREV_MASTER_VOLUME_VALUE =
        "android.media.EXTRA_PREV_MASTER_VOLUME_VALUE";

    /**
     * @hide The new master volume mute state for the master mute changed intent.
     * Value is boolean
     */
    public static final String EXTRA_MASTER_VOLUME_MUTED =
        "android.media.EXTRA_MASTER_VOLUME_MUTED";

    /** The audio stream for phone calls */
    public static final int STREAM_VOICE_CALL = AudioSystem.STREAM_VOICE_CALL;
    /** The audio stream for system sounds */
+37 −5
Original line number Diff line number Diff line
@@ -283,6 +283,9 @@ public class AudioService extends IAudioService.Stub {
    // Forced device usage for communications
    private int mForcedUseForComm;

    // True if we have master volume support
    private final boolean mUseMasterVolume;

    // List of binder death handlers for setMode() client processes.
    // The last process to have called setMode() is at the top of the list.
    private final ArrayList <SetModeDeathHandler> mSetModeDeathHandlers = new ArrayList <SetModeDeathHandler>();
@@ -410,8 +413,9 @@ public class AudioService extends IAudioService.Stub {
                context.getSystemService(Context.TELEPHONY_SERVICE);
        tmgr.listen(mPhoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);

        if (context.getResources().getBoolean(
                com.android.internal.R.bool.config_useMasterVolume)) {
        mUseMasterVolume = context.getResources().getBoolean(
                com.android.internal.R.bool.config_useMasterVolume);
        if (mUseMasterVolume) {
            float volume = Settings.System.getFloat(mContentResolver,
                    Settings.System.VOLUME_MASTER, -1.0f);
            if (volume >= 0.0f) {
@@ -626,6 +630,8 @@ public class AudioService extends IAudioService.Stub {

        float volume = AudioSystem.getMasterVolume();
        if (volume >= 0.0) {
            // get current master volume adjusted to 0 to 100
            int oldVolume = getMasterVolume();
            if (direction == AudioManager.ADJUST_RAISE) {
                volume += MASTER_VOLUME_INCREMENT;
                if (volume > 1.0f) volume = 1.0f;
@@ -637,7 +643,7 @@ public class AudioService extends IAudioService.Stub {
            long origCallerIdentityToken = Binder.clearCallingIdentity();
            Settings.System.putFloat(mContentResolver, Settings.System.VOLUME_MASTER, volume);
            Binder.restoreCallingIdentity(origCallerIdentityToken);
            mVolumePanel.postMasterVolumeChanged(flags);
            sendMasterVolumeUpdate(flags, oldVolume, getMasterVolume());
        }
    }

@@ -696,6 +702,27 @@ public class AudioService extends IAudioService.Stub {
        mContext.sendBroadcast(intent);
    }

    // UI update and Broadcast Intent
    private void sendMasterVolumeUpdate(int flags, int oldVolume, int newVolume) {
        mVolumePanel.postMasterVolumeChanged(flags);

        Intent intent = new Intent(AudioManager.MASTER_VOLUME_CHANGED_ACTION);
        intent.putExtra(AudioManager.EXTRA_PREV_MASTER_VOLUME_VALUE, oldVolume);
        intent.putExtra(AudioManager.EXTRA_MASTER_VOLUME_VALUE, newVolume);
        mContext.sendBroadcast(intent);
    }

    // UI update and Broadcast Intent
    private void sendMasterMuteUpdate(boolean muted, int flags) {
        mVolumePanel.postMasterMuteChanged(flags);

        Intent intent = new Intent(AudioManager.MASTER_MUTE_CHANGED_ACTION);
        intent.putExtra(AudioManager.EXTRA_MASTER_VOLUME_MUTED, muted);
        long origCallerIdentityToken = Binder.clearCallingIdentity();
        mContext.sendStickyBroadcast(intent);
        Binder.restoreCallingIdentity(origCallerIdentityToken);
    }

    /**
     * Sets the stream state's index, and posts a message to set system volume.
     * This will not call out to the UI. Assumes a valid stream type.
@@ -2288,7 +2315,7 @@ public class AudioService extends IAudioService.Stub {
                                // If the stream is not yet muted by any client, set lvel to 0
                                if (muteCount() == 0) {
                                    AudioSystem.setMasterMute(true);
                                    mVolumePanel.postMasterMuteChanged(AudioManager.FLAG_SHOW_UI);
                                    sendMasterMuteUpdate(true, AudioManager.FLAG_SHOW_UI);
                                }
                            } catch (RemoteException e) {
                                // Client has died!
@@ -2314,7 +2341,7 @@ public class AudioService extends IAudioService.Stub {
                                }
                                if (muteCount() == 0) {
                                    AudioSystem.setMasterMute(false);
                                    mVolumePanel.postMasterMuteChanged(AudioManager.FLAG_SHOW_UI);
                                    sendMasterMuteUpdate(false, AudioManager.FLAG_SHOW_UI);
                                }
                            }
                        }
@@ -3021,6 +3048,11 @@ public class AudioService extends IAudioService.Stub {
                    adapter.getProfileProxy(mContext, mBluetoothProfileServiceListener,
                                            BluetoothProfile.A2DP);
                }

                if (mUseMasterVolume) {
                    // Send sticky broadcast for initial master mute state
                    sendMasterMuteUpdate(false, 0);
                }
            } else if (action.equals(Intent.ACTION_PACKAGE_REMOVED)) {
                if (!intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)) {
                    // a package is being removed, not replaced