add native SoundTrigger modes in battery saver

Control over the SoundTrigger service behavior in battery saver mode is
expanded to from a boolean to multiple modes. Modes include enabled,
disabled, and privileged. Adding the privedged mode allows for the
SoundTrigger service to selectively control clients which are deemed
esential to the Android system.

Bug: 172294448
Test: atest libpowermanager_test
Test: build and verify backward compatibility with SoundTrigger system
service behavior

Change-Id: I087a5817c832e194fc8ba670d5c90506d548544e
diff --git a/services/powermanager/BatterySaverPolicyConfig.cpp b/services/powermanager/BatterySaverPolicyConfig.cpp
index ee55b6b..49557bc 100644
--- a/services/powermanager/BatterySaverPolicyConfig.cpp
+++ b/services/powermanager/BatterySaverPolicyConfig.cpp
@@ -55,7 +55,6 @@
         ?: parcel->readBool(&mDisableAod)
         ?: parcel->readBool(&mDisableLaunchBoost)
         ?: parcel->readBool(&mDisableOptionalSensors)
-        ?: parcel->readBool(&mDisableSoundTrigger)
         ?: parcel->readBool(&mDisableVibration)
         ?: parcel->readBool(&mEnableAdjustBrightness)
         ?: parcel->readBool(&mEnableDataSaver)
@@ -64,7 +63,8 @@
         ?: parcel->readBool(&mEnableQuickDoze)
         ?: parcel->readBool(&mForceAllAppsStandby)
         ?: parcel->readBool(&mForceBackgroundCheck)
-        ?: parcel->readInt32(reinterpret_cast<int32_t *>(&mLocationMode));
+        ?: parcel->readInt32(reinterpret_cast<int32_t *>(&mLocationMode))
+        ?: parcel->readInt32(reinterpret_cast<int32_t *>(&mSoundTriggerMode));
 }
 
 status_t BatterySaverPolicyConfig::writeDeviceSpecificSettings(android::Parcel *parcel) const {
@@ -97,7 +97,6 @@
         ?: parcel->writeBool(mDisableAod)
         ?: parcel->writeBool(mDisableLaunchBoost)
         ?: parcel->writeBool(mDisableOptionalSensors)
-        ?: parcel->writeBool(mDisableSoundTrigger)
         ?: parcel->writeBool(mDisableVibration)
         ?: parcel->writeBool(mEnableAdjustBrightness)
         ?: parcel->writeBool(mEnableDataSaver)
@@ -106,7 +105,8 @@
         ?: parcel->writeBool(mEnableQuickDoze)
         ?: parcel->writeBool(mForceAllAppsStandby)
         ?: parcel->writeBool(mForceBackgroundCheck)
-        ?: parcel->writeInt32(static_cast<int32_t>(mLocationMode));
+        ?: parcel->writeInt32(static_cast<int32_t>(mLocationMode))
+        ?: parcel->writeInt32(static_cast<int32_t>(mSoundTriggerMode));
 }
 
 } // namespace android::os
diff --git a/services/powermanager/PowerSaveState.cpp b/services/powermanager/PowerSaveState.cpp
index 6d1830a..d705e91 100644
--- a/services/powermanager/PowerSaveState.cpp
+++ b/services/powermanager/PowerSaveState.cpp
@@ -31,6 +31,7 @@
     return parcel->readBool(&mBatterySaverEnabled)
         ?: parcel->readBool(&mGlobalBatterySaverEnabled)
         ?: parcel->readInt32(reinterpret_cast<int32_t *>(&mLocationMode))
+        ?: parcel->readInt32(reinterpret_cast<int32_t *>(&mSoundTriggerMode))
         ?: parcel->readFloat(&mBrightnessFactor);
 }
 
@@ -43,6 +44,7 @@
     return parcel->writeBool(mBatterySaverEnabled)
         ?: parcel->writeBool(mGlobalBatterySaverEnabled)
         ?: parcel->writeInt32(static_cast<int32_t>(mLocationMode))
+        ?: parcel->writeInt32(static_cast<int32_t>(mSoundTriggerMode))
         ?: parcel->writeFloat(mBrightnessFactor);
 }
 
diff --git a/services/powermanager/include/android/BatterySaverPolicyConfig.h b/services/powermanager/include/android/BatterySaverPolicyConfig.h
index 728c8a0..3a0c9d0 100644
--- a/services/powermanager/include/android/BatterySaverPolicyConfig.h
+++ b/services/powermanager/include/android/BatterySaverPolicyConfig.h
@@ -24,6 +24,7 @@
 namespace android::os {
 
 enum class LocationMode : int32_t;
+enum class SoundTriggerMode : int32_t;
 /**
  * BatterySaverPolicyConfig is a structure of configs to set Battery Saver policy flags.
  * This file needs to be kept in sync with
@@ -40,7 +41,6 @@
                              bool disableAod = false,
                              bool disableLaunchBoost = false,
                              bool disableOptionalSensors = false,
-                             bool disableSoundTrigger = false,
                              bool disableVibration = false,
                              bool enableAdjustBrightness = false,
                              bool enableDataSaver = false,
@@ -49,7 +49,8 @@
                              bool enableQuickDoze = false,
                              bool forceAllAppsStandby = false,
                              bool forceBackgroundCheck = false,
-                             LocationMode locationMode = static_cast<LocationMode>(0))
+                             LocationMode locationMode = static_cast<LocationMode>(0),
+                             SoundTriggerMode soundTriggerMode = static_cast<SoundTriggerMode>(0))
         : mAdjustBrightnessFactor(adjustBrightnessFactor),
           mAdvertiseIsEnabled(advertiseIsEnabled),
           mDeferFullBackup(deferFullBackup),
@@ -59,7 +60,6 @@
           mDisableAod(disableAod),
           mDisableLaunchBoost(disableLaunchBoost),
           mDisableOptionalSensors(disableOptionalSensors),
-          mDisableSoundTrigger(disableSoundTrigger),
           mDisableVibration(disableVibration),
           mEnableAdjustBrightness(enableAdjustBrightness),
           mEnableDataSaver(enableDataSaver),
@@ -68,7 +68,8 @@
           mEnableQuickDoze(enableQuickDoze),
           mForceAllAppsStandby(forceAllAppsStandby),
           mForceBackgroundCheck(forceBackgroundCheck),
-          mLocationMode(locationMode) {
+          mLocationMode(locationMode),
+          mSoundTriggerMode(soundTriggerMode) {
     }
 
     status_t readFromParcel(const android::Parcel* parcel) override;
@@ -83,7 +84,6 @@
                mDisableAod == bsp.mDisableAod &&
                mDisableLaunchBoost == bsp.mDisableLaunchBoost &&
                mDisableOptionalSensors == bsp.mDisableOptionalSensors &&
-               mDisableSoundTrigger == bsp.mDisableSoundTrigger &&
                mDisableVibration == bsp.mDisableVibration &&
                mEnableAdjustBrightness == bsp.mEnableAdjustBrightness &&
                mEnableDataSaver == bsp.mEnableDataSaver &&
@@ -92,7 +92,8 @@
                mEnableQuickDoze == bsp.mEnableQuickDoze &&
                mForceAllAppsStandby == bsp.mForceAllAppsStandby &&
                mForceBackgroundCheck == bsp.mForceBackgroundCheck &&
-               mLocationMode == bsp.mLocationMode;
+               mLocationMode == bsp.mLocationMode &&
+               mSoundTriggerMode == bsp.mSoundTriggerMode;
     }
 
 private:
@@ -116,8 +117,6 @@
     bool mDisableLaunchBoost;
     /** Disable optional sensors */
     bool mDisableOptionalSensors;
-    /** Disable sound trigger */
-    bool mDisableSoundTrigger;
     /** Disable vibration */
     bool mDisableVibration;
     /** Enable adjust brightness */
@@ -136,6 +135,8 @@
     bool mForceBackgroundCheck;
     /** Location mode */
     LocationMode mLocationMode;
+    /** SoundTrigger mode */
+    SoundTriggerMode mSoundTriggerMode;
 };
 
 } // namespace android::os
diff --git a/services/powermanager/include/android/PowerSaveState.h b/services/powermanager/include/android/PowerSaveState.h
index b421f6a..1818db2 100644
--- a/services/powermanager/include/android/PowerSaveState.h
+++ b/services/powermanager/include/android/PowerSaveState.h
@@ -24,6 +24,7 @@
 namespace android::os {
 
 enum class LocationMode : int32_t;
+enum class SoundTriggerMode : int32_t;
 /**
  * PowerSaveState is a structure to encapsulate PowerSaveState status.
  * This file needs to be kept in sync with frameworks/base/core/java/android/os/PowerSaveState.java
@@ -33,16 +34,19 @@
     PowerSaveState(bool batterySaverEnabled = false,
                    bool globalBatterySaverEnabled = false,
                    LocationMode locationMode = static_cast<LocationMode>(0),
+                   SoundTriggerMode soundTriggerMode = static_cast<SoundTriggerMode>(0),
                    float brightnessFactor = 0.5f)
             : mBatterySaverEnabled(batterySaverEnabled),
               mGlobalBatterySaverEnabled(globalBatterySaverEnabled),
               mLocationMode(locationMode),
+              mSoundTriggerMode(soundTriggerMode),
               mBrightnessFactor(brightnessFactor) {
     }
 
     bool getBatterySaverEnabled() const { return mBatterySaverEnabled; }
     bool getGlobalBatterySaverEnabled() const { return mGlobalBatterySaverEnabled; }
     LocationMode getLocationMode() const { return mLocationMode; }
+    SoundTriggerMode getSoundTriggerMode() const { return mSoundTriggerMode; }
     float getBrightnessFactor() const { return mBrightnessFactor; }
     bool operator == (const PowerSaveState &ps) const {
         return mBatterySaverEnabled == ps.mBatterySaverEnabled &&
@@ -61,6 +65,8 @@
     bool mGlobalBatterySaverEnabled;
     /** Location mode */
     LocationMode mLocationMode;
+    /** SoundTrigger mode */
+    SoundTriggerMode mSoundTriggerMode;
     /** Screen brightness factor. */
     float mBrightnessFactor;
 };
diff --git a/services/powermanager/include/android/SoundTriggerMode.h b/services/powermanager/include/android/SoundTriggerMode.h
new file mode 100644
index 0000000..cee43e3
--- /dev/null
+++ b/services/powermanager/include/android/SoundTriggerMode.h
@@ -0,0 +1,32 @@
+/*
+ * Copyright (C) 2020 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef ANDROID_OS_SOUNDTRIGGER_MODE_H
+#define ANDROID_OS_SOUNDTRIGGER_MODE_H
+
+namespace android::os {
+
+enum class SoundTriggerMode : int32_t {
+    ALL_ENABLED = IPowerManager::SOUND_TRIGGER_MODE_ALL_ENABLED,
+    CRITICAL_ONLY = IPowerManager::SOUND_TRIGGER_MODE_CRITICAL_ONLY,
+    ALL_DISABLED = IPowerManager::SOUND_TRIGGER_MODE_ALL_DISABLED,
+    MIN = IPowerManager::SOUND_TRIGGER_MODE_ALL_ENABLED,
+    MAX = IPowerManager::SOUND_TRIGGER_MODE_ALL_DISABLED,
+};
+
+} // namespace android::os
+
+#endif /* ANDROID_OS_SOUNDTRIGGER_MODE_H */