Use the latest temperature read as base for headroom calculation

It was unexpectedly using the oldest temperature which can be 30s old
and not accurately reflect the current device's temperature (i.e.
getThermalheadroom(0) could return device status from 30s ago)

The unit test used to add new temperatures to the front of ring
buffer instead, which was inconsistent with the implementation which add
to the back and thus didn't catch the issue, it's now all fixed.

Bug: 343809405
Test: atest ThermalManagerServiceTest
Change-Id: I913572d04f9ca7d9106babe2017e686cf30f29ec
diff --git a/services/core/java/com/android/server/power/ThermalManagerService.java b/services/core/java/com/android/server/power/ThermalManagerService.java
index 5360788..f5882a3 100644
--- a/services/core/java/com/android/server/power/ThermalManagerService.java
+++ b/services/core/java/com/android/server/power/ThermalManagerService.java
@@ -74,6 +74,7 @@
 import java.util.Arrays;
 import java.util.Collection;
 import java.util.Iterator;
+import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
 import java.util.NoSuchElementException;
@@ -1609,7 +1610,7 @@
         /** Map of skin temperature sensor name to a corresponding list of samples */
         @GuardedBy("mSamples")
         @VisibleForTesting
-        final ArrayMap<String, ArrayList<Sample>> mSamples = new ArrayMap<>();
+        final ArrayMap<String, LinkedList<Sample>> mSamples = new ArrayMap<>();
 
         /** Map of skin temperature sensor name to the corresponding SEVERE temperature threshold */
         @GuardedBy("mSamples")
@@ -1706,10 +1707,10 @@
                         continue;
                     }
 
-                    ArrayList<Sample> samples = mSamples.computeIfAbsent(temperature.getName(),
-                            k -> new ArrayList<>(RING_BUFFER_SIZE));
+                    LinkedList<Sample> samples = mSamples.computeIfAbsent(temperature.getName(),
+                            k -> new LinkedList<>());
                     if (samples.size() == RING_BUFFER_SIZE) {
-                        samples.remove(0);
+                        samples.removeFirst();
                     }
                     samples.add(new Sample(now, temperature.getValue()));
                 }
@@ -1724,8 +1725,7 @@
         float getSlopeOf(List<Sample> samples) {
             long sumTimes = 0L;
             float sumTemperatures = 0.0f;
-            for (int s = 0; s < samples.size(); ++s) {
-                Sample sample = samples.get(s);
+            for (final Sample sample : samples) {
                 sumTimes += sample.time;
                 sumTemperatures += sample.temperature;
             }
@@ -1734,8 +1734,7 @@
 
             long sampleVariance = 0L;
             float sampleCovariance = 0.0f;
-            for (int s = 0; s < samples.size(); ++s) {
-                Sample sample = samples.get(s);
+            for (final Sample sample : samples) {
                 long timeDelta = sample.time - meanTime;
                 float temperatureDelta = sample.temperature - meanTemperature;
                 sampleVariance += timeDelta * timeDelta;
@@ -1795,9 +1794,9 @@
 
                 float maxNormalized = Float.NaN;
                 int noThresholdSampleCount = 0;
-                for (Map.Entry<String, ArrayList<Sample>> entry : mSamples.entrySet()) {
+                for (Map.Entry<String, LinkedList<Sample>> entry : mSamples.entrySet()) {
                     String name = entry.getKey();
-                    ArrayList<Sample> samples = entry.getValue();
+                    LinkedList<Sample> samples = entry.getValue();
 
                     Float threshold = mSevereThresholds.get(name);
                     if (threshold == null) {
@@ -1806,7 +1805,7 @@
                         continue;
                     }
 
-                    float currentTemperature = samples.get(0).temperature;
+                    float currentTemperature = samples.getLast().temperature;
 
                     if (samples.size() < MINIMUM_SAMPLE_COUNT) {
                         // Don't try to forecast, just use the latest one we have
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 32bbc7a..3685afd 100644
--- a/services/tests/servicestests/src/com/android/server/power/ThermalManagerServiceTest.java
+++ b/services/tests/servicestests/src/com/android/server/power/ThermalManagerServiceTest.java
@@ -68,6 +68,7 @@
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.HashSet;
+import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
 
@@ -513,7 +514,7 @@
     @Test
     public void testTemperatureWatcherGetSlopeOf() throws RemoteException {
         TemperatureWatcher watcher = mService.mTemperatureWatcher;
-        List<TemperatureWatcher.Sample> samples = new ArrayList<>();
+        List<TemperatureWatcher.Sample> samples = new LinkedList<>();
         for (int i = 0; i < 30; ++i) {
             samples.add(watcher.createSampleForTesting(i, (float) (i / 2 * 2)));
         }
@@ -538,7 +539,7 @@
     public void testTemperatureWatcherGetForecast() throws RemoteException {
         TemperatureWatcher watcher = mService.mTemperatureWatcher;
 
-        ArrayList<TemperatureWatcher.Sample> samples = new ArrayList<>();
+        LinkedList<TemperatureWatcher.Sample> samples = new LinkedList<>();
 
         // Add a single sample
         samples.add(watcher.createSampleForTesting(0, 25.0f));
@@ -551,7 +552,7 @@
 
         // Add some time-series data
         for (int i = 1; i < 20; ++i) {
-            samples.add(0, watcher.createSampleForTesting(1000 * i, 25.0f + 0.5f * i));
+            samples.add(watcher.createSampleForTesting(1000 * i, 25.0f + 0.5f * i));
         }
 
         // Now the forecast should vary depending on how far ahead we are trying to predict