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

Commit 3c45c291 authored by RoboErik's avatar RoboErik Committed by Erik Pasternak
Browse files

Pipe volume keys to adjustVolume instead of sendMediaKeyEvent

We were calling sendMediaKeyEvent with a KEYCODE_VOLUME key, which was being
ignored because it's not a media key. This redirects the volume keys to use
the adjustVolume methods instead. It also sends the appropriate flags to
make the lock screen consistent with the home screen and the volume keys
only affect active playback when the screen is off.

bug:15900519
Change-Id: I9f3853a2385869353a58debae6e6ca9933ba06ce
parent b5ef7f8c
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -345,6 +345,12 @@ public class AudioManager {
     */
    public static final int FLAG_HDMI_SYSTEM_AUDIO_VOLUME = 1 << 8;

    /**
     * Indicates that this should only be handled if media is actively playing.
     * @hide
     */
    public static final int FLAG_ACTIVE_MEDIA_ONLY = 1 << 9;

    /**
     * Ringer mode that will be silent and will not vibrate. (This overrides the
     * vibrate setting.)
+19 −0
Original line number Diff line number Diff line
@@ -698,6 +698,25 @@ public final class MediaSession {
        }
    }

    /**
     * Return true if this is considered an active playback state.
     *
     * @hide
     */
    public static boolean isActiveState(int state) {
        switch (state) {
            case PlaybackState.STATE_FAST_FORWARDING:
            case PlaybackState.STATE_REWINDING:
            case PlaybackState.STATE_SKIPPING_TO_PREVIOUS:
            case PlaybackState.STATE_SKIPPING_TO_NEXT:
            case PlaybackState.STATE_BUFFERING:
            case PlaybackState.STATE_CONNECTING:
            case PlaybackState.STATE_PLAYING:
                return true;
        }
        return false;
    }

    /**
     * Represents an ongoing session. This may be passed to apps by the session
     * owner to allow them to create a {@link MediaController} to communicate with
+48 −0
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@ import android.app.PendingIntent.CanceledException;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaMetadata;
import android.media.MediaMetadataEditor;
import android.media.MediaMetadataRetriever;
@@ -157,12 +158,59 @@ public class MediaSessionLegacyHelper {
    }

    public void sendMediaButtonEvent(KeyEvent keyEvent, boolean needWakeLock) {
        if (keyEvent == null) {
            Log.w(TAG, "Tried to send a null key event. Ignoring.");
            return;
        }
        mSessionManager.dispatchMediaKeyEvent(keyEvent, needWakeLock);
        if (DEBUG) {
            Log.d(TAG, "dispatched media key " + keyEvent);
        }
    }

    public void sendVolumeKeyEvent(KeyEvent keyEvent, boolean musicOnly) {
        if (keyEvent == null) {
            Log.w(TAG, "Tried to send a null key event. Ignoring.");
            return;
        }
        boolean down = keyEvent.getAction() == KeyEvent.ACTION_DOWN;
        boolean up = keyEvent.getAction() == KeyEvent.ACTION_UP;
        int direction = 0;
        switch (keyEvent.getKeyCode()) {
            case KeyEvent.KEYCODE_VOLUME_UP:
                direction = AudioManager.ADJUST_RAISE;
                break;
            case KeyEvent.KEYCODE_VOLUME_DOWN:
                direction = AudioManager.ADJUST_LOWER;
                break;
            case KeyEvent.KEYCODE_VOLUME_MUTE:
                // TODO
                break;
        }
        if ((down || up) && direction != 0) {
            int flags;
            // If this is action up we want to send a beep for non-music events
            if (up) {
                direction = 0;
            }
            if (musicOnly) {
                // This flag is used when the screen is off to only affect
                // active media
                flags = AudioManager.FLAG_ACTIVE_MEDIA_ONLY;
            } else {
                // These flags are consistent with the home screen
                if (up) {
                    flags = AudioManager.FLAG_PLAY_SOUND | AudioManager.FLAG_VIBRATE;
                } else {
                    flags = AudioManager.FLAG_SHOW_UI;
                }
            }

            mSessionManager.dispatchAdjustVolumeBy(AudioManager.USE_DEFAULT_STREAM_TYPE,
                    direction, flags);
        }
    }

    public void sendAdjustVolumeBy(int suggestedStream, int delta, int flags) {
        mSessionManager.dispatchAdjustVolumeBy(suggestedStream, delta, flags);
        if (DEBUG) {
+2 −6
Original line number Diff line number Diff line
@@ -83,7 +83,7 @@ public class PhoneFallbackEventHandler implements FallbackEventHandler {
            case KeyEvent.KEYCODE_VOLUME_UP:
            case KeyEvent.KEYCODE_VOLUME_DOWN:
            case KeyEvent.KEYCODE_VOLUME_MUTE: {
                getAudioManager().handleKeyDown(event, AudioManager.USE_DEFAULT_STREAM_TYPE);
                MediaSessionLegacyHelper.getHelper(mContext).sendVolumeKeyEvent(event, false);
                return true;
            }

@@ -198,11 +198,7 @@ public class PhoneFallbackEventHandler implements FallbackEventHandler {
            case KeyEvent.KEYCODE_VOLUME_DOWN:
            case KeyEvent.KEYCODE_VOLUME_MUTE: {
                if (!event.isCanceled()) {
                    AudioManager audioManager = (AudioManager)mContext.getSystemService(
                            Context.AUDIO_SERVICE);
                    if (audioManager != null) {
                        getAudioManager().handleKeyUp(event, AudioManager.USE_DEFAULT_STREAM_TYPE);
                    }
                    MediaSessionLegacyHelper.getHelper(mContext).sendVolumeKeyEvent(event, false);
                }
                return true;
            }
+7 −17
Original line number Diff line number Diff line
@@ -45,7 +45,11 @@ import android.media.AudioManager;
import android.media.IAudioService;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.media.session.MediaController;
import android.media.session.MediaSession;
import android.media.session.MediaSessionLegacyHelper;
import android.media.session.MediaSessionManager;
import android.media.session.PlaybackState;
import android.os.Bundle;
import android.os.FactoryTest;
import android.os.Handler;
@@ -113,6 +117,7 @@ import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

import static android.view.WindowManager.LayoutParams.*;
import static android.view.WindowManagerPolicy.WindowManagerFuncs.LID_ABSENT;
@@ -4006,21 +4011,6 @@ public class PhoneWindowManager implements WindowManagerPolicy {
        setHdmiPlugged(!mHdmiPlugged);
    }

    /**
     * @return Whether music is being played right now "locally" (e.g. on the device's speakers
     *    or wired headphones) or "remotely" (e.g. on a device using the Cast protocol and
     *    controlled by this device, or through remote submix).
     */
    boolean isMusicActive() {

        final AudioManager am = (AudioManager)mContext.getSystemService(Context.AUDIO_SERVICE);
        if (am == null) {
            Log.w(TAG, "isMusicActive: couldn't get AudioManager reference");
            return false;
        }
        return am.isLocalOrRemoteMusicActive();
    }

    final Object mScreenshotLock = new Object();
    ServiceConnection mScreenshotConnection = null;

@@ -4210,7 +4200,7 @@ public class PhoneWindowManager implements WindowManagerPolicy {
                            // the application, just pass it to the session service.

                            MediaSessionLegacyHelper.getHelper(mContext)
                                    .sendMediaButtonEvent(event, true);
                                    .sendVolumeKeyEvent(event, false);
                            break;
                        }
                    }
@@ -4220,7 +4210,7 @@ public class PhoneWindowManager implements WindowManagerPolicy {
                        // handled it send it to the session manager to figure
                        // out.
                        MediaSessionLegacyHelper.getHelper(mContext)
                                .sendMediaButtonEvent(event, true);
                                .sendVolumeKeyEvent(event, true);
                        break;
                    }
                }
Loading