Audio service: always unduck focus requester when granted

When an app is requesting focus, it might
  have been in the focus stack before, and ducked. Always
  unduck the associated players when the app requests audio
  focus and it is granted.
When an app gains focus from an another abandoning focus,
  do not make unducking conditional to having a focus
  dispatcher (== app has a focus listener).

Test: test app made to duck, re-request focus and verify not ducked
Bug: 37433811
Change-Id: I7e5b29c9148c923e7510957b971e28b6139d6156
(cherry picked from commit 270f1c9d8ff38be25b8972f7d158ca178da82eb7)
diff --git a/services/core/java/com/android/server/audio/FocusRequester.java b/services/core/java/com/android/server/audio/FocusRequester.java
index f6fc724..125095c 100644
--- a/services/core/java/com/android/server/audio/FocusRequester.java
+++ b/services/core/java/com/android/server/audio/FocusRequester.java
@@ -322,8 +322,8 @@
                 if (mFocusLossWasNotified) {
                     fd.dispatchAudioFocusChange(focusGain, mClientId);
                 }
-                mFocusController.unduckPlayers(this);
             }
+            mFocusController.unduckPlayers(this);
             mFocusLossWasNotified = false;
         } catch (android.os.RemoteException e) {
             Log.e(TAG, "Failure to signal gain of audio focus due to: ", e);
@@ -333,6 +333,15 @@
     /**
      * Called synchronized on MediaFocusControl.mAudioFocusLock
      */
+    void handleFocusGainFromRequest(int focusRequestResult) {
+        if (focusRequestResult == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
+            mFocusController.unduckPlayers(this);
+        }
+    }
+
+    /**
+     * Called synchronized on MediaFocusControl.mAudioFocusLock
+     */
     void handleFocusLoss(int focusLoss, @Nullable final FocusRequester fr) {
         try {
             if (focusLoss != mFocusLossReceived) {
diff --git a/services/core/java/com/android/server/audio/MediaFocusControl.java b/services/core/java/com/android/server/audio/MediaFocusControl.java
index f5c13c1..7d742ff 100644
--- a/services/core/java/com/android/server/audio/MediaFocusControl.java
+++ b/services/core/java/com/android/server/audio/MediaFocusControl.java
@@ -752,6 +752,7 @@
 
                 // push focus requester at the top of the audio focus stack
                 mFocusStack.push(nfr);
+                nfr.handleFocusGainFromRequest(AudioManager.AUDIOFOCUS_REQUEST_GRANTED);
             }
             notifyExtPolicyFocusGrant_syncAf(nfr.toAudioFocusInfo(),
                     AudioManager.AUDIOFOCUS_REQUEST_GRANTED);