am 278aaed1: Merge "Backport audio ducking from Honeycomb" into gingerbread

* commit '278aaed1eb37763f7dcb2364523db591014a9210':
  Backport audio ducking from Honeycomb
diff --git a/src/com/android/music/MediaPlaybackService.java b/src/com/android/music/MediaPlaybackService.java
index 9b1fd58..c4d7474 100644
--- a/src/com/android/music/MediaPlaybackService.java
+++ b/src/com/android/music/MediaPlaybackService.java
@@ -97,8 +97,9 @@
     private static final int TRACK_ENDED = 1;
     private static final int RELEASE_WAKELOCK = 2;
     private static final int SERVER_DIED = 3;
-    private static final int FADEIN = 4;
-    private static final int FOCUSCHANGE = 5;
+    private static final int FOCUSCHANGE = 4;
+    private static final int FADEDOWN = 5;
+    private static final int FADEUP = 6;
     private static final int MAX_HISTORY_SIZE = 100;
     
     private MultiPlayer mPlayer;
@@ -157,10 +158,19 @@
         public void handleMessage(Message msg) {
             MusicUtils.debugLog("mMediaplayerHandler.handleMessage " + msg.what);
             switch (msg.what) {
-                case FADEIN:
-                    mCurrentVolume += 0.01f;
+                case FADEDOWN:
+                    mCurrentVolume -= .05f;
+                    if (mCurrentVolume > .2f) {
+                        mMediaplayerHandler.sendEmptyMessageDelayed(FADEDOWN, 10);
+                    } else {
+                        mCurrentVolume = .2f;
+                    }
+                    mPlayer.setVolume(mCurrentVolume);
+                    break;
+                case FADEUP:
+                    mCurrentVolume += .01f;
                     if (mCurrentVolume < 1.0f) {
-                        mMediaplayerHandler.sendEmptyMessageDelayed(FADEIN, 10);
+                        mMediaplayerHandler.sendEmptyMessageDelayed(FADEUP, 10);
                     } else {
                         mCurrentVolume = 1.0f;
                     }
@@ -192,7 +202,6 @@
                 case FOCUSCHANGE:
                     // This code is here so we can better synchronize it with the code that
                     // handles fade-in
-                    // AudioFocus is a new feature: focus updates are made verbose on purpose
                     switch (msg.arg1) {
                         case AudioManager.AUDIOFOCUS_LOSS:
                             Log.v(LOGTAG, "AudioFocus: received AUDIOFOCUS_LOSS");
@@ -201,8 +210,11 @@
                             }
                             pause();
                             break;
-                        case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
                         case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
+                            mMediaplayerHandler.removeMessages(FADEUP);
+                            mMediaplayerHandler.sendEmptyMessage(FADEDOWN);
+                            break;
+                        case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
                             Log.v(LOGTAG, "AudioFocus: received AUDIOFOCUS_LOSS_TRANSIENT");
                             if(isPlaying()) {
                                 mPausedByTransientLossOfFocus = true;
@@ -216,6 +228,9 @@
                                 mCurrentVolume = 0f;
                                 mPlayer.setVolume(mCurrentVolume);
                                 play(); // also queues a fade-in
+                            } else {
+                                mMediaplayerHandler.removeMessages(FADEDOWN);
+                                mMediaplayerHandler.sendEmptyMessage(FADEUP);
                             }
                             break;
                         default:
@@ -1048,7 +1063,8 @@
             mPlayer.start();
             // make sure we fade in, in case a previous fadein was stopped because
             // of another focus loss
-            mMediaplayerHandler.sendEmptyMessage(FADEIN);
+            mMediaplayerHandler.removeMessages(FADEDOWN);
+            mMediaplayerHandler.sendEmptyMessage(FADEUP);
 
             RemoteViews views = new RemoteViews(getPackageName(), R.layout.statusbar);
             views.setImageViewResource(R.id.icon, R.drawable.stat_notify_musicplayer);
@@ -1124,7 +1140,7 @@
      */
     public void pause() {
         synchronized(this) {
-            mMediaplayerHandler.removeMessages(FADEIN);
+            mMediaplayerHandler.removeMessages(FADEUP);
             if (isPlaying()) {
                 mPlayer.pause();
                 gotoIdleState();