policy-hal: make sure policy-hal is in sync with AOSP

 This change makes sure that changes in the base function
 are moved to custom policy if the function is derived in
 custom policy.

 Below changes are taken care

1) audio policy: fix capture indication to sound trigger service.

   Only indicate active capture to sound trigger service
   when capturing from primay HW module.

   Bug: 31826453
   Bug: 31771879
   Change-Id: Idf92393d3e7dbf926b847ab23f670e7ab5dd5248

2) audio policy: fix media volume after ringtone

   Fix regression introduced by commit 11c499a5 causing
   music volume not to be restored after ringtone playback
   over duplicated output (speaker + A2DP).

   Bug: 31539910
   Change-Id: I1c4eebea1f1a8f82ae2acfeecb9a61c437a74e25

3) audio policy: fix call volume over USB

   Make sure the cache of output device selection per strategy is updated
   also when an input device is connected or disconnected as the list of
   input devices sometimes impacts the output device selection in
   getDeviceForStrategy().

   Bug: 30659619
   Change-Id: Ie9f7bafda0074afcc803f309379c35b538f50170

4) audio policy: workaround for truncated touch sounds

   Implement workaround to avoid touch sound truncation when
   audio path or volume changes are triggered by user actions.

   Also:
      - Fix regression introduced by commit c75307b7 causing failure
      to honor playback start delay in case of path change.
      - Fix failure to report path switch delay in updateCallRouting().
      - Make sure we delay temporary mute according to requested device
      switch delay in checkDeviceMuteStrategies().

   Bug: 29528161
   Bug: 27535199
   Change-Id: Id729579b6623ee67a767055b9aca7d2e2dde8491

Change-Id: I2f0b0e51ec725a462e913432f648cb7b3cf125fe
diff --git a/policy_hal/AudioPolicyManager.cpp b/policy_hal/AudioPolicyManager.cpp
index 77cbf5a..87ba53c 100644
--- a/policy_hal/AudioPolicyManager.cpp
+++ b/policy_hal/AudioPolicyManager.cpp
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2013-2016, The Linux Foundation. All rights reserved.
+ * Copyright (c) 2013-2017 The Linux Foundation. All rights reserved.
  * Not a contribution.
  *
  * Copyright (C) 2009 The Android Open Source Project
@@ -69,6 +69,10 @@
 #endif // DOLBY_END
 
 namespace android {
+/*audio policy: workaround for truncated touch sounds*/
+//FIXME: workaround for truncated touch sounds
+// to be removed when the problem is handled by system UI
+#define TOUCH_SOUND_FIXED_DELAY_MS 100
 #ifdef VOICE_CONCURRENCY
 audio_output_flags_t AudioPolicyManagerCustom::getFallBackPath()
 {
@@ -417,6 +421,10 @@
         }
 
         closeAllInputs();
+        /*audio policy: fix call volume over USB*/
+        // As the input device list can impact the output device selection, update
+        // getDeviceForStrategy() cache
+        updateDevicesAndOutputs();
 
         if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
             audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, false /*fromCache*/);
@@ -1068,9 +1076,17 @@
     checkOutputForAllStrategies();
     updateDevicesAndOutputs();
 
+    /*audio policy: workaround for truncated touch sounds*/
+    //FIXME: workaround for truncated touch sounds
+    // to be removed when the problem is handled by system UI
+    uint32_t delayMs = 0;
+    uint32_t waitMs = 0;
+    if (usage == AUDIO_POLICY_FORCE_FOR_COMMUNICATION) {
+        delayMs = TOUCH_SOUND_FIXED_DELAY_MS;
+    }
     if (mEngine->getPhoneState() == AUDIO_MODE_IN_CALL && hasPrimaryOutput()) {
         audio_devices_t newDevice = getNewOutputDevice(mPrimaryOutput, true /*fromCache*/);
-        updateCallRouting(newDevice);
+        waitMs = updateCallRouting(newDevice, delayMs);
     }
     // Use reverse loop to make sure any low latency usecases (generally tones)
     // are not routed before non LL usecases (generally music).
@@ -1082,11 +1098,12 @@
     for (size_t i = mOutputs.size(); i > 0; i--) {
         sp<SwAudioOutputDescriptor> outputDesc = mOutputs.valueAt(i-1);
         audio_devices_t newDevice = getNewOutputDevice(outputDesc, true /*fromCache*/);
-        if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || outputDesc != mPrimaryOutput) {
-            setOutputDevice(outputDesc, newDevice, (newDevice != AUDIO_DEVICE_NONE));
+        if ((mEngine->getPhoneState() != AUDIO_MODE_IN_CALL) || (outputDesc != mPrimaryOutput)) {
+            waitMs = setOutputDevice(outputDesc, newDevice, (newDevice != AUDIO_DEVICE_NONE),
+                                     delayMs);
         }
         if (forceVolumeReeval && (newDevice != AUDIO_DEVICE_NONE)) {
-            applyStreamVolumes(outputDesc, newDevice, 0, true);
+            applyStreamVolumes(outputDesc, newDevice, waitMs, true);
         }
     }
 
@@ -1160,6 +1177,11 @@
                                     dev,
                                     force,
                                     delayMs);
+                    /*audio policy: fix media volume after ringtone*/
+                    // re-apply device specific volume if not done by setOutputDevice()
+                     if (!force) {
+                         applyStreamVolumes(desc, dev, delayMs);
+                     }
                 }
             }
             // update the outputs if stopping one with a stream that can affect notification routing
@@ -1266,6 +1288,10 @@
         if (strategy == STRATEGY_SONIFICATION) {
             mpClientInterface->invalidateStream(AUDIO_STREAM_ACCESSIBILITY);
         }
+        if (waitMs > muteWaitMs) {
+            *delayMs = waitMs - muteWaitMs;
+        }
+
     }
     else {
         // handle special case for sonification while in call
@@ -2160,11 +2186,16 @@
             mpClientInterface->onDynamicPolicyMixStateUpdate(inputDesc->mPolicyMix->mDeviceAddress,
                     MIX_STATE_MIXING);
         }
-
-        if (mInputs.activeInputsCountOnDevices() == 0) {
+        /*audio policy: fix capture indication to sound trigger service*/
+        // indicate active capture to sound trigger service if starting capture from a mic on
+        // primary HW module
+        audio_devices_t device = getNewInputDevice(input);
+        audio_devices_t primaryInputDevices = availablePrimaryInputDevices();
+        if (((device & primaryInputDevices & ~AUDIO_DEVICE_BIT_IN) != 0) &&
+                mInputs.activeInputsCountOnDevices(primaryInputDevices) == 0) {
             SoundTrigger::setCaptureState(true);
         }
-        setInputDevice(input, getNewInputDevice(input), true /* force */);
+        setInputDevice(input, device, true /* force */);
 
         // automatically enable the remote submix output when input is started if not
         // used by a policy mix of type MIX_TYPE_RECORDERS