Add support for thermal threshold callback

Bug: 360486877
Flag: android.os.allow_thermal_thresholds_callback
Test: atest ThermalManagerServiceTest ThermalManagerServiceMockingTest
Change-Id: Ic8e0a53b2a306ae74e75b9c3a5dafda723840448
diff --git a/Android.bp b/Android.bp
index d4776f5..cd56c85 100644
--- a/Android.bp
+++ b/Android.bp
@@ -109,7 +109,7 @@
         ":android.hardware.radio.voice-V3-java-source",
         ":android.hardware.security.keymint-V3-java-source",
         ":android.hardware.security.secureclock-V1-java-source",
-        ":android.hardware.thermal-V2-java-source",
+        ":android.hardware.thermal-V3-java-source",
         ":android.hardware.tv.tuner-V3-java-source",
         ":android.security.apc-java-source",
         ":android.security.authorization-java-source",
diff --git a/core/java/android/os/flags.aconfig b/core/java/android/os/flags.aconfig
index c7cc653..9c83bc2 100644
--- a/core/java/android/os/flags.aconfig
+++ b/core/java/android/os/flags.aconfig
@@ -91,6 +91,14 @@
 }
 
 flag {
+    name: "allow_thermal_thresholds_callback"
+    is_exported: true
+    namespace: "game"
+    description: "Enable thermal threshold callback"
+    bug: "360486877"
+}
+
+flag {
     name: "android_os_build_vanilla_ice_cream"
     is_exported: true
     namespace: "build"
diff --git a/services/core/java/com/android/server/power/ThermalManagerService.java b/services/core/java/com/android/server/power/ThermalManagerService.java
index dc6b164..78bc06c 100644
--- a/services/core/java/com/android/server/power/ThermalManagerService.java
+++ b/services/core/java/com/android/server/power/ThermalManagerService.java
@@ -31,6 +31,7 @@
 import android.hardware.thermal.IThermal;
 import android.hardware.thermal.IThermalChangedCallback;
 import android.hardware.thermal.TemperatureThreshold;
+import android.hardware.thermal.TemperatureType;
 import android.hardware.thermal.ThrottlingSeverity;
 import android.hardware.thermal.V1_0.ThermalStatus;
 import android.hardware.thermal.V1_0.ThermalStatusCode;
@@ -134,6 +135,31 @@
     @VisibleForTesting
     final TemperatureWatcher mTemperatureWatcher = new TemperatureWatcher();
 
+    private final ThermalHalWrapper.WrapperThermalChangedCallback mWrapperCallback =
+            new ThermalHalWrapper.WrapperThermalChangedCallback() {
+                @Override
+                public void onTemperatureChanged(Temperature temperature) {
+                    final long token = Binder.clearCallingIdentity();
+                    try {
+                        ThermalManagerService.this.onTemperatureChanged(temperature, true);
+                    } finally {
+                        Binder.restoreCallingIdentity(token);
+                    }
+                }
+
+                @Override
+                public void onThresholdChanged(TemperatureThreshold threshold) {
+                    final long token = Binder.clearCallingIdentity();
+                    try {
+                        synchronized (mTemperatureWatcher.mSamples) {
+                            mTemperatureWatcher.updateTemperatureThresholdLocked(threshold, true);
+                        }
+                    } finally {
+                        Binder.restoreCallingIdentity(token);
+                    }
+                }
+            };
+
     private final Context mContext;
 
     public ThermalManagerService(Context context) {
@@ -146,7 +172,7 @@
         mContext = context;
         mHalWrapper = halWrapper;
         if (halWrapper != null) {
-            halWrapper.setCallback(this::onTemperatureChangedCallback);
+            halWrapper.setCallback(mWrapperCallback);
         }
         mStatus = Temperature.THROTTLING_NONE;
     }
@@ -171,19 +197,19 @@
             // Connect to HAL and post to listeners.
             boolean halConnected = (mHalWrapper != null);
             if (!halConnected) {
-                mHalWrapper = new ThermalHalAidlWrapper(this::onTemperatureChangedCallback);
+                mHalWrapper = new ThermalHalAidlWrapper(mWrapperCallback);
                 halConnected = mHalWrapper.connectToHal();
             }
             if (!halConnected) {
-                mHalWrapper = new ThermalHal20Wrapper(this::onTemperatureChangedCallback);
+                mHalWrapper = new ThermalHal20Wrapper(mWrapperCallback);
                 halConnected = mHalWrapper.connectToHal();
             }
             if (!halConnected) {
-                mHalWrapper = new ThermalHal11Wrapper(this::onTemperatureChangedCallback);
+                mHalWrapper = new ThermalHal11Wrapper(mWrapperCallback);
                 halConnected = mHalWrapper.connectToHal();
             }
             if (!halConnected) {
-                mHalWrapper = new ThermalHal10Wrapper(this::onTemperatureChangedCallback);
+                mHalWrapper = new ThermalHal10Wrapper(mWrapperCallback);
                 halConnected = mHalWrapper.connectToHal();
             }
             if (!halConnected) {
@@ -200,7 +226,7 @@
                 onTemperatureChanged(temperatures.get(i), false);
             }
             onTemperatureMapChangedLocked();
-            mTemperatureWatcher.updateThresholds();
+            mTemperatureWatcher.getAndUpdateThresholds();
             mHalReady.set(true);
         }
     }
@@ -335,16 +361,6 @@
         }
     }
 
-    /* HwBinder callback **/
-    private void onTemperatureChangedCallback(Temperature temperature) {
-        final long token = Binder.clearCallingIdentity();
-        try {
-            onTemperatureChanged(temperature, true);
-        } finally {
-            Binder.restoreCallingIdentity(token);
-        }
-    }
-
     private void registerStatsCallbacks() {
         final StatsManager statsManager = mContext.getSystemService(StatsManager.class);
         if (statsManager != null) {
@@ -924,19 +940,19 @@
         /** Lock to protect HAL handle. */
         protected final Object mHalLock = new Object();
 
-        @FunctionalInterface
-        interface TemperatureChangedCallback {
-            void onValues(Temperature temperature);
+        interface WrapperThermalChangedCallback {
+            void onTemperatureChanged(Temperature temperature);
+            void onThresholdChanged(TemperatureThreshold threshold);
         }
 
         /** Temperature callback. */
-        protected TemperatureChangedCallback mCallback;
+        protected WrapperThermalChangedCallback mCallback;
 
         /** Cookie for matching the right end point. */
         protected static final int THERMAL_HAL_DEATH_COOKIE = 5612;
 
         @VisibleForTesting
-        protected void setCallback(TemperatureChangedCallback cb) {
+        protected void setCallback(WrapperThermalChangedCallback cb) {
             mCallback = cb;
         }
 
@@ -959,7 +975,7 @@
                 List<Temperature> temperatures = getCurrentTemperatures(false, 0);
                 final int count = temperatures.size();
                 for (int i = 0; i < count; i++) {
-                    mCallback.onValues(temperatures.get(i));
+                    mCallback.onTemperatureChanged(temperatures.get(i));
                 }
             }
         }
@@ -985,31 +1001,42 @@
         private IThermal mInstance = null;
 
         /** Callback for Thermal HAL AIDL. */
-        private final IThermalChangedCallback mThermalChangedCallback =
+        private final IThermalChangedCallback mThermalCallbackAidl =
                 new IThermalChangedCallback.Stub() {
-                    @Override public void notifyThrottling(
-                            android.hardware.thermal.Temperature temperature)
-                            throws RemoteException {
+                    @Override
+                    public void notifyThrottling(
+                            android.hardware.thermal.Temperature temperature) {
                         Temperature svcTemperature = new Temperature(temperature.value,
                                 temperature.type, temperature.name, temperature.throttlingStatus);
                         final long token = Binder.clearCallingIdentity();
                         try {
-                            mCallback.onValues(svcTemperature);
+                            mCallback.onTemperatureChanged(svcTemperature);
                         } finally {
                             Binder.restoreCallingIdentity(token);
                         }
                     }
 
-            @Override public int getInterfaceVersion() throws RemoteException {
-                return this.VERSION;
-            }
+                    @Override
+                    public void notifyThresholdChanged(TemperatureThreshold threshold) {
+                        if (Flags.allowThermalThresholdsCallback()) {
+                            if (threshold.type == TemperatureType.SKIN) {
+                                mCallback.onThresholdChanged(threshold);
+                            }
+                        }
+                    }
 
-            @Override public String getInterfaceHash() throws RemoteException {
-                return this.HASH;
-            }
-        };
+                    @Override
+                    public int getInterfaceVersion() throws RemoteException {
+                        return this.VERSION;
+                    }
 
-        ThermalHalAidlWrapper(TemperatureChangedCallback callback) {
+                    @Override
+                    public String getInterfaceHash() throws RemoteException {
+                        return this.HASH;
+                    }
+                };
+
+        ThermalHalAidlWrapper(WrapperThermalChangedCallback callback) {
             mCallback = callback;
         }
 
@@ -1153,7 +1180,7 @@
         @VisibleForTesting
         void registerThermalChangedCallback() {
             try {
-                mInstance.registerThermalChangedCallback(mThermalChangedCallback);
+                mInstance.registerThermalChangedCallback(mThermalCallbackAidl);
             } catch (IllegalArgumentException | IllegalStateException e) {
                 Slog.e(TAG, "Couldn't registerThermalChangedCallback due to invalid status",
                         e);
@@ -1185,7 +1212,7 @@
         @GuardedBy("mHalLock")
         private android.hardware.thermal.V1_0.IThermal mThermalHal10 = null;
 
-        ThermalHal10Wrapper(TemperatureChangedCallback callback) {
+        ThermalHal10Wrapper(WrapperThermalChangedCallback callback) {
             mCallback = callback;
         }
 
@@ -1317,14 +1344,14 @@
                                         : Temperature.THROTTLING_NONE);
                         final long token = Binder.clearCallingIdentity();
                         try {
-                            mCallback.onValues(thermalSvcTemp);
+                            mCallback.onTemperatureChanged(thermalSvcTemp);
                         } finally {
                             Binder.restoreCallingIdentity(token);
                         }
                     }
                 };
 
-        ThermalHal11Wrapper(TemperatureChangedCallback callback) {
+        ThermalHal11Wrapper(WrapperThermalChangedCallback callback) {
             mCallback = callback;
         }
 
@@ -1455,14 +1482,14 @@
                                 temperature.throttlingStatus);
                         final long token = Binder.clearCallingIdentity();
                         try {
-                            mCallback.onValues(thermalSvcTemp);
+                            mCallback.onTemperatureChanged(thermalSvcTemp);
                         } finally {
                             Binder.restoreCallingIdentity(token);
                         }
                     }
                 };
 
-        ThermalHal20Wrapper(TemperatureChangedCallback callback) {
+        ThermalHal20Wrapper(WrapperThermalChangedCallback callback) {
             mCallback = callback;
         }
 
@@ -1627,52 +1654,57 @@
         @VisibleForTesting
         long mInactivityThresholdMillis = INACTIVITY_THRESHOLD_MILLIS;
 
-        void updateThresholds() {
+        void getAndUpdateThresholds() {
             List<TemperatureThreshold> thresholds =
                         mHalWrapper.getTemperatureThresholds(true, Temperature.TYPE_SKIN);
             synchronized (mSamples) {
                 if (Flags.allowThermalHeadroomThresholds()) {
                     Arrays.fill(mHeadroomThresholds, Float.NaN);
                 }
-                for (int t = 0; t < thresholds.size(); ++t) {
-                    TemperatureThreshold threshold = thresholds.get(t);
-                    if (threshold.hotThrottlingThresholds.length <= ThrottlingSeverity.SEVERE) {
-                        continue;
-                    }
-                    float severeThreshold =
-                            threshold.hotThrottlingThresholds[ThrottlingSeverity.SEVERE];
-                    if (!Float.isNaN(severeThreshold)) {
-                        mSevereThresholds.put(threshold.name, severeThreshold);
-                        if (Flags.allowThermalHeadroomThresholds()) {
-                            for (int severity = ThrottlingSeverity.LIGHT;
-                                    severity <= ThrottlingSeverity.SHUTDOWN; severity++) {
-                                if (threshold.hotThrottlingThresholds.length > severity) {
-                                    updateHeadroomThreshold(severity,
-                                            threshold.hotThrottlingThresholds[severity],
-                                            severeThreshold);
-                                }
-                            }
-                        }
-                    }
+                for (final TemperatureThreshold threshold : thresholds) {
+                    updateTemperatureThresholdLocked(threshold, false);
                 }
             }
         }
 
         // For an older device with multiple SKIN sensors, we will set a severity's headroom
-        // threshold based on the minimum value of all as a workaround.
-        void updateHeadroomThreshold(int severity, float threshold, float severeThreshold) {
-            if (!Float.isNaN(threshold)) {
-                synchronized (mSamples) {
-                    if (severity == ThrottlingSeverity.SEVERE) {
-                        mHeadroomThresholds[severity] = 1.0f;
-                        return;
+        // threshold based on the minimum value of all as a workaround, unless override.
+        @GuardedBy("mSamples")
+        void updateTemperatureThresholdLocked(TemperatureThreshold threshold, boolean override) {
+            if (threshold.hotThrottlingThresholds.length <= ThrottlingSeverity.SEVERE) {
+                return;
+            }
+            float severeThreshold =
+                    threshold.hotThrottlingThresholds[ThrottlingSeverity.SEVERE];
+            if (Float.isNaN(severeThreshold)) {
+                return;
+            }
+            mSevereThresholds.put(threshold.name, severeThreshold);
+            if (!Flags.allowThermalHeadroomThresholds()) {
+                return;
+            }
+            if (override) {
+                Arrays.fill(mHeadroomThresholds, Float.NaN);
+            }
+            for (int severity = ThrottlingSeverity.LIGHT;
+                    severity <= ThrottlingSeverity.SHUTDOWN; severity++) {
+                if (threshold.hotThrottlingThresholds.length > severity) {
+                    float t = threshold.hotThrottlingThresholds[severity];
+                    if (Float.isNaN(t)) {
+                        continue;
                     }
-                    float headroom = normalizeTemperature(threshold, severeThreshold);
-                    if (Float.isNaN(mHeadroomThresholds[severity])) {
-                        mHeadroomThresholds[severity] = headroom;
-                    } else {
-                        float lastHeadroom = mHeadroomThresholds[severity];
-                        mHeadroomThresholds[severity] = Math.min(lastHeadroom, headroom);
+                    synchronized (mSamples) {
+                        if (severity == ThrottlingSeverity.SEVERE) {
+                            mHeadroomThresholds[severity] = 1.0f;
+                            continue;
+                        }
+                        float headroom = normalizeTemperature(t, severeThreshold);
+                        if (Float.isNaN(mHeadroomThresholds[severity])) {
+                            mHeadroomThresholds[severity] = headroom;
+                        } else {
+                            float lastHeadroom = mHeadroomThresholds[severity];
+                            mHeadroomThresholds[severity] = Math.min(lastHeadroom, headroom);
+                        }
                     }
                 }
             }
diff --git a/services/core/jni/Android.bp b/services/core/jni/Android.bp
index a5085fc..ea0b02c 100644
--- a/services/core/jni/Android.bp
+++ b/services/core/jni/Android.bp
@@ -191,7 +191,7 @@
         "android.hardware.power.stats@1.0",
         "android.hardware.power.stats-V1-ndk",
         "android.hardware.thermal@1.0",
-        "android.hardware.thermal-V2-ndk",
+        "android.hardware.thermal-V3-ndk",
         "android.hardware.tv.input@1.0",
         "android.hardware.tv.input-V2-ndk",
         "android.hardware.vibrator-V3-ndk",
diff --git a/services/tests/mockingservicestests/src/com/android/server/power/ThermalManagerServiceMockingTest.java b/services/tests/mockingservicestests/src/com/android/server/power/ThermalManagerServiceMockingTest.java
index aa9d8c6..f1072da 100644
--- a/services/tests/mockingservicestests/src/com/android/server/power/ThermalManagerServiceMockingTest.java
+++ b/services/tests/mockingservicestests/src/com/android/server/power/ThermalManagerServiceMockingTest.java
@@ -18,6 +18,7 @@
 
 import static org.junit.Assert.assertArrayEquals;
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
 
@@ -29,10 +30,16 @@
 import android.hardware.thermal.ThrottlingSeverity;
 import android.os.Binder;
 import android.os.CoolingDevice;
+import android.os.Flags;
 import android.os.RemoteException;
 import android.os.Temperature;
+import android.platform.test.annotations.DisableFlags;
+import android.platform.test.annotations.EnableFlags;
+import android.platform.test.flag.junit.SetFlagsRule;
 
 import org.junit.Before;
+import org.junit.ClassRule;
+import org.junit.Rule;
 import org.junit.Test;
 import org.mockito.ArgumentCaptor;
 import org.mockito.Captor;
@@ -40,16 +47,36 @@
 import org.mockito.Mockito;
 import org.mockito.MockitoAnnotations;
 
+import java.util.Arrays;
 import java.util.List;
 import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.TimeUnit;
 
 
 public class ThermalManagerServiceMockingTest {
-    @Mock private IThermal mAidlHalMock;
+    @ClassRule
+    public static final SetFlagsRule.ClassRule mSetFlagsClassRule = new SetFlagsRule.ClassRule();
+    @Rule
+    public final SetFlagsRule mSetFlagsRule = mSetFlagsClassRule.createSetFlagsRule();
+
+    @Mock
+    private IThermal mAidlHalMock;
     private Binder mAidlBinder = new Binder();
     private CompletableFuture<Temperature> mTemperatureFuture;
-    private ThermalManagerService.ThermalHalWrapper.TemperatureChangedCallback mTemperatureCallback;
+    private CompletableFuture<TemperatureThreshold> mThresholdFuture;
+    private ThermalManagerService.ThermalHalWrapper.WrapperThermalChangedCallback
+            mTemperatureCallback =
+            new ThermalManagerService.ThermalHalWrapper.WrapperThermalChangedCallback() {
+                @Override
+                public void onTemperatureChanged(Temperature temperature) {
+                    mTemperatureFuture.complete(temperature);
+                }
+
+                @Override
+                public void onThresholdChanged(TemperatureThreshold threshold) {
+                    mThresholdFuture.complete(threshold);
+                }
+            };
     private ThermalManagerService.ThermalHalAidlWrapper mAidlWrapper;
     @Captor
     ArgumentCaptor<IThermalChangedCallback> mAidlCallbackCaptor;
@@ -60,27 +87,63 @@
         Mockito.when(mAidlHalMock.asBinder()).thenReturn(mAidlBinder);
         mAidlBinder.attachInterface(mAidlHalMock, IThermal.class.getName());
         mTemperatureFuture = new CompletableFuture<>();
-        mTemperatureCallback = temperature -> mTemperatureFuture.complete(temperature);
+        mThresholdFuture = new CompletableFuture<>();
         mAidlWrapper = new ThermalManagerService.ThermalHalAidlWrapper(mTemperatureCallback);
         mAidlWrapper.initProxyAndRegisterCallback(mAidlBinder);
     }
 
     @Test
+    @EnableFlags({Flags.FLAG_ALLOW_THERMAL_THRESHOLDS_CALLBACK})
     public void setCallback_aidl() throws Exception {
         Mockito.verify(mAidlHalMock, Mockito.times(1)).registerThermalChangedCallback(
                 mAidlCallbackCaptor.capture());
-        android.hardware.thermal.Temperature halT =
+        android.hardware.thermal.Temperature halTemperature =
                 new android.hardware.thermal.Temperature();
-        halT.type = TemperatureType.SOC;
-        halT.name = "test";
-        halT.throttlingStatus = ThrottlingSeverity.SHUTDOWN;
-        halT.value = 99.0f;
-        mAidlCallbackCaptor.getValue().notifyThrottling(halT);
+        halTemperature.type = TemperatureType.SOC;
+        halTemperature.name = "test";
+        halTemperature.throttlingStatus = ThrottlingSeverity.SHUTDOWN;
+        halTemperature.value = 99.0f;
+
+        android.hardware.thermal.TemperatureThreshold halThreshold =
+                new android.hardware.thermal.TemperatureThreshold();
+        halThreshold.type = TemperatureType.SKIN;
+        halThreshold.name = "test";
+        halThreshold.hotThrottlingThresholds = new float[ThrottlingSeverity.SHUTDOWN + 1];
+        Arrays.fill(halThreshold.hotThrottlingThresholds, Float.NaN);
+        halThreshold.hotThrottlingThresholds[ThrottlingSeverity.SEVERE] = 44.0f;
+
+        mAidlCallbackCaptor.getValue().notifyThrottling(halTemperature);
+        mAidlCallbackCaptor.getValue().notifyThresholdChanged(halThreshold);
+
         Temperature temperature = mTemperatureFuture.get(100, TimeUnit.MILLISECONDS);
-        assertEquals(halT.name, temperature.getName());
-        assertEquals(halT.type, temperature.getType());
-        assertEquals(halT.value, temperature.getValue(), 0.1f);
-        assertEquals(halT.throttlingStatus, temperature.getStatus());
+        assertEquals(halTemperature.name, temperature.getName());
+        assertEquals(halTemperature.type, temperature.getType());
+        assertEquals(halTemperature.value, temperature.getValue(), 0.1f);
+        assertEquals(halTemperature.throttlingStatus, temperature.getStatus());
+
+        TemperatureThreshold threshold = mThresholdFuture.get(100, TimeUnit.MILLISECONDS);
+        assertEquals(halThreshold.name, threshold.name);
+        assertEquals(halThreshold.type, threshold.type);
+        assertArrayEquals(halThreshold.hotThrottlingThresholds, threshold.hotThrottlingThresholds,
+                0.01f);
+    }
+
+    @Test
+    @DisableFlags({Flags.FLAG_ALLOW_THERMAL_THRESHOLDS_CALLBACK})
+    public void setCallback_aidl_allow_thermal_thresholds_callback_false() throws Exception {
+        Mockito.verify(mAidlHalMock, Mockito.times(1)).registerThermalChangedCallback(
+                mAidlCallbackCaptor.capture());
+        android.hardware.thermal.TemperatureThreshold halThreshold =
+                new android.hardware.thermal.TemperatureThreshold();
+        halThreshold.type = TemperatureType.SOC;
+        halThreshold.name = "test";
+        halThreshold.hotThrottlingThresholds = new float[ThrottlingSeverity.SHUTDOWN + 1];
+        Arrays.fill(halThreshold.hotThrottlingThresholds, Float.NaN);
+        halThreshold.hotThrottlingThresholds[ThrottlingSeverity.SEVERE] = 44.0f;
+
+        mAidlCallbackCaptor.getValue().notifyThresholdChanged(halThreshold);
+        Thread.sleep(1000);
+        assertFalse(mThresholdFuture.isDone());
     }
 
     @Test
diff --git a/services/tests/servicestests/src/com/android/server/power/ThermalManagerServiceTest.java b/services/tests/servicestests/src/com/android/server/power/ThermalManagerServiceTest.java
index 6d79ae4..cfe3d84 100644
--- a/services/tests/servicestests/src/com/android/server/power/ThermalManagerServiceTest.java
+++ b/services/tests/servicestests/src/com/android/server/power/ThermalManagerServiceTest.java
@@ -296,11 +296,11 @@
     }
 
     @Test
-    public void testNotify() throws RemoteException {
+    public void testNotifyThrottling() throws RemoteException {
         int status = Temperature.THROTTLING_SEVERE;
         // Should only notify event not status
         Temperature newBattery = new Temperature(50, Temperature.TYPE_BATTERY, "batt", status);
-        mFakeHal.mCallback.onValues(newBattery);
+        mFakeHal.mCallback.onTemperatureChanged(newBattery);
         verify(mEventListener1, timeout(CALLBACK_TIMEOUT_MILLI_SEC)
                 .times(1)).notifyThrottling(newBattery);
         verify(mStatusListener1, timeout(CALLBACK_TIMEOUT_MILLI_SEC)
@@ -312,7 +312,7 @@
         resetListenerMock();
         // Notify both event and status
         Temperature newSkin = new Temperature(50, Temperature.TYPE_SKIN, "skin1", status);
-        mFakeHal.mCallback.onValues(newSkin);
+        mFakeHal.mCallback.onTemperatureChanged(newSkin);
         verify(mEventListener1, timeout(CALLBACK_TIMEOUT_MILLI_SEC)
                 .times(1)).notifyThrottling(newSkin);
         verify(mStatusListener1, timeout(CALLBACK_TIMEOUT_MILLI_SEC)
@@ -325,7 +325,7 @@
         // Back to None, should only notify event not status
         status = Temperature.THROTTLING_NONE;
         newBattery = new Temperature(50, Temperature.TYPE_BATTERY, "batt", status);
-        mFakeHal.mCallback.onValues(newBattery);
+        mFakeHal.mCallback.onTemperatureChanged(newBattery);
         verify(mEventListener1, timeout(CALLBACK_TIMEOUT_MILLI_SEC)
                 .times(1)).notifyThrottling(newBattery);
         verify(mStatusListener1, timeout(CALLBACK_TIMEOUT_MILLI_SEC)
@@ -337,7 +337,7 @@
         resetListenerMock();
         // Should also notify status
         newSkin = new Temperature(50, Temperature.TYPE_SKIN, "skin1", status);
-        mFakeHal.mCallback.onValues(newSkin);
+        mFakeHal.mCallback.onTemperatureChanged(newSkin);
         verify(mEventListener1, timeout(CALLBACK_TIMEOUT_MILLI_SEC)
                 .times(1)).notifyThrottling(newSkin);
         verify(mStatusListener1, timeout(CALLBACK_TIMEOUT_MILLI_SEC)
@@ -362,7 +362,7 @@
     public void testGetCurrentStatus() throws RemoteException {
         int status = Temperature.THROTTLING_SEVERE;
         Temperature newSkin = new Temperature(100, Temperature.TYPE_SKIN, "skin1", status);
-        mFakeHal.mCallback.onValues(newSkin);
+        mFakeHal.mCallback.onTemperatureChanged(newSkin);
         assertEquals(status, mService.mService.getCurrentThermalStatus());
         int battStatus = Temperature.THROTTLING_EMERGENCY;
         Temperature newBattery = new Temperature(60, Temperature.TYPE_BATTERY, "batt", battStatus);
@@ -373,11 +373,11 @@
     public void testThermalShutdown() throws RemoteException {
         int status = Temperature.THROTTLING_SHUTDOWN;
         Temperature newSkin = new Temperature(100, Temperature.TYPE_SKIN, "skin1", status);
-        mFakeHal.mCallback.onValues(newSkin);
+        mFakeHal.mCallback.onTemperatureChanged(newSkin);
         verify(mIPowerManagerMock, timeout(CALLBACK_TIMEOUT_MILLI_SEC)
                 .times(1)).shutdown(false, PowerManager.SHUTDOWN_THERMAL_STATE, false);
         Temperature newBattery = new Temperature(60, Temperature.TYPE_BATTERY, "batt", status);
-        mFakeHal.mCallback.onValues(newBattery);
+        mFakeHal.mCallback.onTemperatureChanged(newBattery);
         verify(mIPowerManagerMock, timeout(CALLBACK_TIMEOUT_MILLI_SEC)
                 .times(1)).shutdown(false, PowerManager.SHUTDOWN_BATTERY_THERMAL_STATE, false);
     }
@@ -419,15 +419,35 @@
     }
 
     @Test
-    public void testTemperatureWatcherUpdateSevereThresholds() throws RemoteException {
+    public void testTemperatureWatcherUpdateSevereThresholds() {
         TemperatureWatcher watcher = mService.mTemperatureWatcher;
-        watcher.mSevereThresholds.erase();
-        watcher.updateThresholds();
-        assertEquals(1, watcher.mSevereThresholds.size());
-        assertEquals("skin1", watcher.mSevereThresholds.keyAt(0));
-        Float threshold = watcher.mSevereThresholds.get("skin1");
-        assertNotNull(threshold);
-        assertEquals(40.0f, threshold, 0.0f);
+        synchronized (watcher.mSamples) {
+            watcher.mSevereThresholds.erase();
+            watcher.getAndUpdateThresholds();
+            assertEquals(1, watcher.mSevereThresholds.size());
+            assertEquals("skin1", watcher.mSevereThresholds.keyAt(0));
+            Float threshold = watcher.mSevereThresholds.get("skin1");
+            assertNotNull(threshold);
+            assertEquals(40.0f, threshold, 0.0f);
+            assertArrayEquals("Got" + Arrays.toString(watcher.mHeadroomThresholds),
+                    new float[]{Float.NaN, 0.6667f, 0.8333f, 1.0f, 1.166f, 1.3333f,
+                            1.5f},
+                    watcher.mHeadroomThresholds, 0.01f);
+
+            TemperatureThreshold newThreshold = new TemperatureThreshold();
+            newThreshold.name = "skin1";
+            newThreshold.hotThrottlingThresholds = new float[] {
+                    Float.NaN, 44.0f, 47.0f, 50.0f, Float.NaN, Float.NaN, Float.NaN
+            };
+            mFakeHal.mCallback.onThresholdChanged(newThreshold);
+            threshold = watcher.mSevereThresholds.get("skin1");
+            assertNotNull(threshold);
+            assertEquals(50.0f, threshold, 0.0f);
+            assertArrayEquals("Got" + Arrays.toString(watcher.mHeadroomThresholds),
+                    new float[]{Float.NaN, 0.8f, 0.9f, 1.0f, Float.NaN, Float.NaN,
+                            Float.NaN},
+                    watcher.mHeadroomThresholds, 0.01f);
+        }
     }
 
     @Test
@@ -436,25 +456,19 @@
         synchronized (watcher.mSamples) {
             Arrays.fill(watcher.mHeadroomThresholds, Float.NaN);
         }
-        watcher.updateHeadroomThreshold(ThrottlingSeverity.LIGHT, 40, 49);
-        watcher.updateHeadroomThreshold(ThrottlingSeverity.MODERATE, 46, 49);
-        watcher.updateHeadroomThreshold(ThrottlingSeverity.SEVERE, 49, 49);
-        watcher.updateHeadroomThreshold(ThrottlingSeverity.CRITICAL, 64, 49);
-        watcher.updateHeadroomThreshold(ThrottlingSeverity.EMERGENCY, 70, 49);
-        watcher.updateHeadroomThreshold(ThrottlingSeverity.SHUTDOWN, 79, 49);
+        TemperatureThreshold threshold = new TemperatureThreshold();
+        threshold.hotThrottlingThresholds = new float[]{Float.NaN, 40, 46, 49, 64, 70, 79};
         synchronized (watcher.mSamples) {
+            watcher.updateTemperatureThresholdLocked(threshold, false /*override*/);
             assertArrayEquals(new float[]{Float.NaN, 0.7f, 0.9f, 1.0f, 1.5f, 1.7f, 2.0f},
                     watcher.mHeadroomThresholds, 0.01f);
         }
 
         // when another sensor reports different threshold, we expect to see smaller one to be used
-        watcher.updateHeadroomThreshold(ThrottlingSeverity.LIGHT, 37, 52);
-        watcher.updateHeadroomThreshold(ThrottlingSeverity.MODERATE, 46, 52);
-        watcher.updateHeadroomThreshold(ThrottlingSeverity.SEVERE, 52, 52);
-        watcher.updateHeadroomThreshold(ThrottlingSeverity.CRITICAL, 64, 52);
-        watcher.updateHeadroomThreshold(ThrottlingSeverity.EMERGENCY, 100, 52);
-        watcher.updateHeadroomThreshold(ThrottlingSeverity.SHUTDOWN, 200, 52);
+        threshold = new TemperatureThreshold();
+        threshold.hotThrottlingThresholds = new float[]{Float.NaN, 37, 46, 52, 64, 100, 200};
         synchronized (watcher.mSamples) {
+            watcher.updateTemperatureThresholdLocked(threshold, false /*override*/);
             assertArrayEquals(new float[]{Float.NaN, 0.5f, 0.8f, 1.0f, 1.4f, 1.7f, 2.0f},
                     watcher.mHeadroomThresholds, 0.01f);
         }
@@ -486,7 +500,7 @@
         TemperatureWatcher watcher = mService.mTemperatureWatcher;
         ArrayList<TemperatureThreshold> thresholds = new ArrayList<>();
         mFakeHal.mTemperatureThresholdList = thresholds;
-        watcher.updateThresholds();
+        watcher.getAndUpdateThresholds();
         synchronized (watcher.mSamples) {
             assertArrayEquals(
                     new float[]{Float.NaN, Float.NaN, Float.NaN, Float.NaN, Float.NaN, Float.NaN,
@@ -501,7 +515,7 @@
         Arrays.fill(nanThresholds.hotThrottlingThresholds, Float.NaN);
         Arrays.fill(nanThresholds.coldThrottlingThresholds, Float.NaN);
         thresholds.add(nanThresholds);
-        watcher.updateThresholds();
+        watcher.getAndUpdateThresholds();
         synchronized (watcher.mSamples) {
             assertArrayEquals(
                     new float[]{Float.NaN, Float.NaN, Float.NaN, Float.NaN, Float.NaN, Float.NaN,