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
diff --git a/services/core/java/com/android/server/media/MediaSessionRecord.java b/services/core/java/com/android/server/media/MediaSessionRecord.java
index aeade50..0fbcd7e 100644
--- a/services/core/java/com/android/server/media/MediaSessionRecord.java
+++ b/services/core/java/com/android/server/media/MediaSessionRecord.java
@@ -68,18 +68,6 @@
     private static final boolean DEBUG = false;
 
     /**
-     * These are the playback states that count as currently active.
-     */
-    private static final int[] ACTIVE_STATES = {
-            PlaybackState.STATE_FAST_FORWARDING,
-            PlaybackState.STATE_REWINDING,
-            PlaybackState.STATE_SKIPPING_TO_PREVIOUS,
-            PlaybackState.STATE_SKIPPING_TO_NEXT,
-            PlaybackState.STATE_BUFFERING,
-            PlaybackState.STATE_CONNECTING,
-            PlaybackState.STATE_PLAYING };
-
-    /**
      * The length of time a session will still be considered active after
      * pausing in ms.
      */
@@ -402,7 +390,7 @@
      */
     public boolean isPlaybackActive(boolean includeRecentlyActive) {
         int state = mPlaybackState == null ? 0 : mPlaybackState.getState();
-        if (isActiveState(state)) {
+        if (MediaSession.isActiveState(state)) {
             return true;
         }
         if (includeRecentlyActive && state == mPlaybackState.STATE_PAUSED) {
@@ -555,15 +543,6 @@
         pw.println(indent + "params=" + (mRequest == null ? null : mRequest.toString()));
     }
 
-    private boolean isActiveState(int state) {
-        for (int i = 0; i < ACTIVE_STATES.length; i++) {
-            if (ACTIVE_STATES[i] == state) {
-                return true;
-            }
-        }
-        return false;
-    }
-
     private String getShortMetadataString() {
         int fields = mMetadata == null ? 0 : mMetadata.size();
         String title = mMetadata == null ? null : mMetadata
@@ -804,7 +783,7 @@
         public void setPlaybackState(PlaybackState state) {
             int oldState = mPlaybackState == null ? 0 : mPlaybackState.getState();
             int newState = state == null ? 0 : state.getState();
-            if (isActiveState(oldState) && newState == PlaybackState.STATE_PAUSED) {
+            if (MediaSession.isActiveState(oldState) && newState == PlaybackState.STATE_PAUSED) {
                 mLastActiveTime = SystemClock.elapsedRealtime();
             }
             mPlaybackState = state;
diff --git a/services/core/java/com/android/server/media/MediaSessionService.java b/services/core/java/com/android/server/media/MediaSessionService.java
index 5a16e4d..310f3e9 100644
--- a/services/core/java/com/android/server/media/MediaSessionService.java
+++ b/services/core/java/com/android/server/media/MediaSessionService.java
@@ -27,6 +27,7 @@
 import android.content.Context;
 import android.content.Intent;
 import android.content.pm.PackageManager;
+import android.media.AudioManager;
 import android.media.IAudioService;
 import android.media.IRemoteVolumeController;
 import android.media.routeprovider.RouteRequest;
@@ -948,6 +949,12 @@
 
             }
             if (session == null) {
+                if ((flags & AudioManager.FLAG_ACTIVE_MEDIA_ONLY) != 0) {
+                    if (DEBUG) {
+                        Log.d(TAG, "No active session to adjust, skipping media only volume event");
+                        return;
+                    }
+                }
                 try {
                     if (delta == 0) {
                         mAudioService.adjustSuggestedStreamVolume(delta, suggestedStream, flags,