Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 637f8be6 authored by Kenny Guy's avatar Kenny Guy
Browse files

Avoid null pointer when in getAmbientBrightnessStats

Check whether the tracker has been started before
trying to return stats.
Add unit test to check empty lists are returned
rather than null.

Bug: 76008231
Test: atest BrightnessTrackerTest
Change-Id: Icc7ef0ee43a95eb182cb1b0b9452f122363e67ba
parent 07a75e0f
Loading
Loading
Loading
Loading
+9 −4
Original line number Diff line number Diff line
@@ -686,10 +686,15 @@ public class BrightnessTracker {
    }

    public ParceledListSlice<AmbientBrightnessDayStats> getAmbientBrightnessStats(int userId) {
        ArrayList<AmbientBrightnessDayStats> stats = mAmbientBrightnessStatsTracker.getUserStats(
        if (mAmbientBrightnessStatsTracker != null) {
            ArrayList<AmbientBrightnessDayStats> stats =
                    mAmbientBrightnessStatsTracker.getUserStats(
                            userId);
        return (stats != null) ? new ParceledListSlice<>(stats) : new ParceledListSlice<>(
                Collections.EMPTY_LIST);
            if (stats != null) {
                return new ParceledListSlice<>(stats);
            }
        }
        return ParceledListSlice.emptyList();
    }

    // Not allowed to keep the SensorEvent so used to copy the data we care about.
+15 −0
Original line number Diff line number Diff line
@@ -30,9 +30,11 @@ import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ParceledListSlice;
import android.database.ContentObserver;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.display.AmbientBrightnessDayStats;
import android.hardware.display.BrightnessChangeEvent;
import android.os.BatteryManager;
import android.os.Handler;
@@ -571,6 +573,19 @@ public class BrightnessTrackerTest {
        assertEquals(event.batteryLevel, event2.batteryLevel, FLOAT_DELTA);
    }

    @Test
    public void testNonNullAmbientStats() {
        // getAmbientBrightnessStats should return an empty list rather than null when
        // tracker isn't started or hasn't collected any data.
        ParceledListSlice<AmbientBrightnessDayStats> slice = mTracker.getAmbientBrightnessStats(0);
        assertNotNull(slice);
        assertTrue(slice.getList().isEmpty());
        startTracker(mTracker);
        slice = mTracker.getAmbientBrightnessStats(0);
        assertNotNull(slice);
        assertTrue(slice.getList().isEmpty());
    }

    private InputStream getInputStream(String data) {
        return new ByteArrayInputStream(data.getBytes(StandardCharsets.UTF_8));
    }