Merge "Add scale factor fields to HapticGenerator AIDL" into main
diff --git a/audio/aidl/aidl_api/android.hardware.audio.effect/current/android/hardware/audio/effect/HapticGenerator.aidl b/audio/aidl/aidl_api/android.hardware.audio.effect/current/android/hardware/audio/effect/HapticGenerator.aidl
index 8addab7..aa1a86f 100644
--- a/audio/aidl/aidl_api/android.hardware.audio.effect/current/android/hardware/audio/effect/HapticGenerator.aidl
+++ b/audio/aidl/aidl_api/android.hardware.audio.effect/current/android/hardware/audio/effect/HapticGenerator.aidl
@@ -55,6 +55,9 @@
   parcelable HapticScale {
     int id;
     android.hardware.audio.effect.HapticGenerator.VibratorScale scale = android.hardware.audio.effect.HapticGenerator.VibratorScale.MUTE;
+    float scaleFactor = (-1.0f) /* -1.000000f */;
+    float adaptiveScaleFactor = (-1.0f) /* -1.000000f */;
+    const float UNDEFINED_SCALE_FACTOR = (-1.0f) /* -1.000000f */;
   }
   @VintfStability
   parcelable VibratorInformation {
diff --git a/audio/aidl/android/hardware/audio/effect/HapticGenerator.aidl b/audio/aidl/android/hardware/audio/effect/HapticGenerator.aidl
index 3cc5acb..f882d63 100644
--- a/audio/aidl/android/hardware/audio/effect/HapticGenerator.aidl
+++ b/audio/aidl/android/hardware/audio/effect/HapticGenerator.aidl
@@ -56,13 +56,51 @@
     @VintfStability
     parcelable HapticScale {
         /**
+         * Representation of undefined scale factor, applied by default for backwards compatibility.
+         */
+        const float UNDEFINED_SCALE_FACTOR = -1.0f;
+
+        /**
          * Audio track ID.
          */
         int id;
+
         /**
          * Haptic intensity.
+         *
+         * This represents haptics scale as fixed levels defined by VibrationScale. If the field
+         * scaleFactor is defined then this will be ignored in favor of scaleFactor, otherwise this
+         * will be used to define the intensity for the haptics.
          */
         VibratorScale scale = VibratorScale.MUTE;
+
+        /**
+         * Haptic scale factor.
+         *
+         * This is a continuous scale representation of VibratorScale, allowing flexible number of
+         * scale levels. If this field is defined then it will be used to define the intensity of
+         * the haptics, instead of the old VibratorScale field. If this field is undefined then the
+         * old VibratorScale field will be used.
+         *
+         * The value zero represents the same as VibratorScale.MUTE and the value one represents
+         * VibratorScale.NONE. Values in (0,1) should scale down, and values > 1 should scale up
+         * within hardware bounds. Negative values will be ignored.
+         */
+        float scaleFactor = -1.0f; // UNDEFINED_SCALE_FACTOR
+
+        /**
+         * Haptic adaptive scale factor.
+         *
+         * This is an additional scale value that should be applied on top of the vibrator scale to
+         * adapt to the device current state. This should be applied to linearly scale the haptic
+         * data after scale/scaleFactor is applied.
+         *
+         * The value zero mutes the haptics, even if the scale/scaleFactor are not set to MUTE/zero.
+         * The value one will not scale the haptics, and can be used as a constant for no-op.
+         * Values in (0,1) should scale down. Values > 1 should scale up within hardware bounds.
+         * Negative values will be ignored.
+         */
+        float adaptiveScaleFactor = -1.0f; // UNDEFINED_SCALE_FACTOR
     }
 
     /**
diff --git a/audio/aidl/vts/VtsHalHapticGeneratorTargetTest.cpp b/audio/aidl/vts/VtsHalHapticGeneratorTargetTest.cpp
index 6af326d..d8332aa 100644
--- a/audio/aidl/vts/VtsHalHapticGeneratorTargetTest.cpp
+++ b/audio/aidl/vts/VtsHalHapticGeneratorTargetTest.cpp
@@ -42,13 +42,15 @@
     PARAM_INSTANCE_NAME,
     PARAM_HAPTIC_SCALE_ID,
     PARAM_HAPTIC_SCALE_VIBRATOR_SCALE,
+    PARAM_HAPTIC_SCALE_SCALE_FACTOR,
+    PARAM_HAPTIC_SCALE_ADAPTIVE_SCALE_FACTOR,
     PARAM_VIBRATION_INFORMATION_RESONANT_FREQUENCY,
     PARAM_VIBRATION_INFORMATION_Q_FACTOR,
     PARAM_VIBRATION_INFORMATION_MAX_AMPLITUDE,
 };
 using HapticGeneratorParamTestParam =
         std::tuple<std::pair<std::shared_ptr<IFactory>, Descriptor>, int,
-                   HapticGenerator::VibratorScale, float, float, float>;
+                   HapticGenerator::VibratorScale, float, float, float, float, float>;
 
 /*
  * Testing parameter range, assuming the parameter supported by effect is in this range.
@@ -67,6 +69,10 @@
 const std::vector<HapticGenerator::VibratorScale> kVibratorScaleValues = {
         ndk::enum_range<HapticGenerator::VibratorScale>().begin(),
         ndk::enum_range<HapticGenerator::VibratorScale>().end()};
+const std::vector<float> kScaleFactorValues = {HapticGenerator::HapticScale::UNDEFINED_SCALE_FACTOR,
+                                               0.0f, 0.5f, 1.0f, MAX_FLOAT};
+const std::vector<float> kAdaptiveScaleFactorValues = {
+        HapticGenerator::HapticScale::UNDEFINED_SCALE_FACTOR, 0.0f, 0.5f, 1.0f, MAX_FLOAT};
 
 const std::vector<float> kResonantFrequencyValues = {MIN_FLOAT, 100, MAX_FLOAT};
 const std::vector<float> kQFactorValues = {MIN_FLOAT, 100, MAX_FLOAT};
@@ -78,6 +84,8 @@
     HapticGeneratorParamTest()
         : mParamHapticScaleId(std::get<PARAM_HAPTIC_SCALE_ID>(GetParam())),
           mParamVibratorScale(std::get<PARAM_HAPTIC_SCALE_VIBRATOR_SCALE>(GetParam())),
+          mParamScaleFactor(std::get<PARAM_HAPTIC_SCALE_SCALE_FACTOR>(GetParam())),
+          mParamAdaptiveScaleFactor(std::get<PARAM_HAPTIC_SCALE_ADAPTIVE_SCALE_FACTOR>(GetParam())),
           mParamResonantFrequency(
                   std::get<PARAM_VIBRATION_INFORMATION_RESONANT_FREQUENCY>(GetParam())),
           mParamQFactor(std::get<PARAM_VIBRATION_INFORMATION_Q_FACTOR>(GetParam())),
@@ -107,6 +115,8 @@
     Descriptor mDescriptor;
     int mParamHapticScaleId = 0;
     HapticGenerator::VibratorScale mParamVibratorScale = HapticGenerator::VibratorScale::MUTE;
+    float mParamScaleFactor = HapticGenerator::HapticScale::UNDEFINED_SCALE_FACTOR;
+    float mParamAdaptiveScaleFactor = HapticGenerator::HapticScale::UNDEFINED_SCALE_FACTOR;
     float mParamResonantFrequency = 0;
     float mParamQFactor = 0;
     float mParamMaxAmplitude = 0;
@@ -135,9 +145,14 @@
         }
     }
 
-    void addHapticScaleParam(int id, HapticGenerator::VibratorScale scale) {
+    void addHapticScaleParam(int id, HapticGenerator::VibratorScale scale, float scaleFactor,
+                             float adaptiveScaleFactor) {
         HapticGenerator setHg;
-        std::vector<HapticGenerator::HapticScale> hapticScales = {{.id = id, .scale = scale}};
+        std::vector<HapticGenerator::HapticScale> hapticScales = {
+                {.id = id,
+                 .scale = scale,
+                 .scaleFactor = scaleFactor,
+                 .adaptiveScaleFactor = adaptiveScaleFactor}};
         setHg.set<HapticGenerator::hapticScales>(hapticScales);
         mTags.push_back({HapticGenerator::hapticScales, setHg});
     }
@@ -160,13 +175,16 @@
 };
 
 TEST_P(HapticGeneratorParamTest, SetAndGetHapticScale) {
-    EXPECT_NO_FATAL_FAILURE(addHapticScaleParam(mParamHapticScaleId, mParamVibratorScale));
+    EXPECT_NO_FATAL_FAILURE(addHapticScaleParam(mParamHapticScaleId, mParamVibratorScale,
+                                                mParamScaleFactor, mParamAdaptiveScaleFactor));
     SetAndGetHapticGeneratorParameters();
 }
 
 TEST_P(HapticGeneratorParamTest, SetAndGetMultipleHapticScales) {
-    EXPECT_NO_FATAL_FAILURE(addHapticScaleParam(mParamHapticScaleId, mParamVibratorScale));
-    EXPECT_NO_FATAL_FAILURE(addHapticScaleParam(mParamHapticScaleId, mParamVibratorScale));
+    EXPECT_NO_FATAL_FAILURE(addHapticScaleParam(mParamHapticScaleId, mParamVibratorScale,
+                                                mParamScaleFactor, mParamAdaptiveScaleFactor));
+    EXPECT_NO_FATAL_FAILURE(addHapticScaleParam(mParamHapticScaleId, mParamVibratorScale,
+                                                mParamScaleFactor, mParamAdaptiveScaleFactor));
     SetAndGetHapticGeneratorParameters();
 }
 
@@ -182,6 +200,8 @@
                                    IFactory::descriptor, getEffectTypeUuidHapticGenerator())),
                            testing::ValuesIn(kHapticScaleIdValues),
                            testing::ValuesIn(kVibratorScaleValues),
+                           testing::ValuesIn(kScaleFactorValues),
+                           testing::ValuesIn(kAdaptiveScaleFactorValues),
                            testing::ValuesIn(kResonantFrequencyValues),
                            testing::ValuesIn(kQFactorValues), testing::ValuesIn(kMaxAmplitude)),
         [](const testing::TestParamInfo<HapticGeneratorParamTest::ParamType>& info) {
@@ -189,6 +209,10 @@
             std::string hapticScaleID = std::to_string(std::get<PARAM_HAPTIC_SCALE_ID>(info.param));
             std::string hapticScaleVibScale = std::to_string(
                     static_cast<int>(std::get<PARAM_HAPTIC_SCALE_VIBRATOR_SCALE>(info.param)));
+            std::string hapticScaleFactor =
+                    std::to_string(std::get<PARAM_HAPTIC_SCALE_SCALE_FACTOR>(info.param));
+            std::string hapticAdaptiveScaleFactor =
+                    std::to_string(std::get<PARAM_HAPTIC_SCALE_ADAPTIVE_SCALE_FACTOR>(info.param));
             std::string resonantFrequency = std::to_string(
                     std::get<PARAM_VIBRATION_INFORMATION_RESONANT_FREQUENCY>(info.param));
             std::string qFactor =
@@ -196,7 +220,9 @@
             std::string maxAmplitude =
                     std::to_string(std::get<PARAM_VIBRATION_INFORMATION_MAX_AMPLITUDE>(info.param));
             std::string name = getPrefix(descriptor) + "_hapticScaleId" + hapticScaleID +
-                               "_hapticScaleVibScale" + hapticScaleVibScale + "_resonantFrequency" +
+                               "_hapticScaleVibScale" + hapticScaleVibScale + "_hapticScaleFactor" +
+                               hapticScaleFactor + "_hapticAdaptiveScaleFactor" +
+                               hapticAdaptiveScaleFactor + "_resonantFrequency" +
                                resonantFrequency + "_qFactor" + qFactor + "_maxAmplitude" +
                                maxAmplitude;
             std::replace_if(
@@ -210,6 +236,8 @@
                                    IFactory::descriptor, getEffectTypeUuidHapticGenerator())),
                            testing::Values(MIN_ID),
                            testing::Values(HapticGenerator::VibratorScale::NONE),
+                           testing::Values(HapticGenerator::HapticScale::UNDEFINED_SCALE_FACTOR),
+                           testing::Values(HapticGenerator::HapticScale::UNDEFINED_SCALE_FACTOR),
                            testing::Values(MIN_FLOAT), testing::Values(MIN_FLOAT),
                            testing::Values(MIN_FLOAT)),
         [](const testing::TestParamInfo<HapticGeneratorParamTest::ParamType>& info) {
@@ -217,6 +245,10 @@
             std::string hapticScaleID = std::to_string(std::get<PARAM_HAPTIC_SCALE_ID>(info.param));
             std::string hapticScaleVibScale = std::to_string(
                     static_cast<int>(std::get<PARAM_HAPTIC_SCALE_VIBRATOR_SCALE>(info.param)));
+            std::string hapticScaleFactor =
+                    std::to_string(std::get<PARAM_HAPTIC_SCALE_SCALE_FACTOR>(info.param));
+            std::string hapticAdaptiveScaleFactor =
+                    std::to_string(std::get<PARAM_HAPTIC_SCALE_ADAPTIVE_SCALE_FACTOR>(info.param));
             std::string resonantFrequency = std::to_string(
                     std::get<PARAM_VIBRATION_INFORMATION_RESONANT_FREQUENCY>(info.param));
             std::string qFactor =
@@ -227,6 +259,8 @@
                                descriptor.common.name + "_UUID_" +
                                toString(descriptor.common.id.uuid) + "_hapticScaleId" +
                                hapticScaleID + "_hapticScaleVibScale" + hapticScaleVibScale +
+                               "_hapticScaleFactor" + hapticScaleFactor +
+                               "_hapticAdaptiveScaleFactor" + hapticAdaptiveScaleFactor +
                                "_resonantFrequency" + resonantFrequency + "_qFactor" + qFactor +
                                "_maxAmplitude" + maxAmplitude;
             std::replace_if(