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

Commit a26ac5f7 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Follow up to ambient brightness stats change"

parents 8fbd4705 8c2006d5
Loading
Loading
Loading
Loading
+30 −1
Original line number Diff line number Diff line
@@ -47,6 +47,11 @@ public final class AmbientBrightnessDayStats implements Parcelable {
    private final float[] mStats;

    /**
     * Initialize day stats from the given state. The time spent in each of the bucket is
     * initialized to 0.
     *
     * @param localDate        The date for which stats are being tracked
     * @param bucketBoundaries Bucket boundaries used from creating the buckets from
     * @hide
     */
    public AmbientBrightnessDayStats(@NonNull LocalDate localDate,
@@ -55,6 +60,11 @@ public final class AmbientBrightnessDayStats implements Parcelable {
    }

    /**
     * Initialize day stats from the given state
     *
     * @param localDate        The date for which stats are being tracked
     * @param bucketBoundaries Bucket boundaries used from creating the buckets from
     * @param stats            Time spent in each of the buckets (in seconds)
     * @hide
     */
    public AmbientBrightnessDayStats(@NonNull LocalDate localDate,
@@ -81,14 +91,26 @@ public final class AmbientBrightnessDayStats implements Parcelable {
        mStats = stats;
    }

    /**
     * @return The {@link LocalDate} for which brightness stats are being tracked.
     */
    public LocalDate getLocalDate() {
        return mLocalDate;
    }

    /**
     * @return Aggregated stats of time spent (in seconds) in various buckets.
     */
    public float[] getStats() {
        return mStats;
    }

    /**
     * Returns the bucket boundaries (in lux) used for creating buckets. For eg., if the bucket
     * boundaries array is {b1, b2, b3}, the buckets will be [b1, b2), [b2, b3), [b3, inf).
     *
     * @return The list of bucket boundaries.
     */
    public float[] getBucketBoundaries() {
        return mBucketBoundaries;
    }
@@ -169,7 +191,14 @@ public final class AmbientBrightnessDayStats implements Parcelable {
        dest.writeFloatArray(mStats);
    }

    /** @hide */
    /**
     * Updates the stats by incrementing the time spent for the appropriate bucket based on ambient
     * brightness reading.
     *
     * @param ambientBrightness Ambient brightness reading (in lux)
     * @param durationSec       Time spent with the given reading (in seconds)
     * @hide
     */
    public void log(float ambientBrightness, float durationSec) {
        int bucketIndex = getBucketIndex(ambientBrightness);
        if (bucketIndex >= 0) {
+11 −20
Original line number Diff line number Diff line
@@ -115,12 +115,10 @@ public class BrightnessTracker {
            = new RingBuffer<>(BrightnessChangeEvent.class, MAX_EVENTS);
    @GuardedBy("mEventsLock")
    private boolean mEventsDirty;
    private final Runnable mEventsWriter = () -> writeEvents();
    private volatile boolean mWriteEventsScheduled;

    private volatile boolean mWriteBrightnessTrackerStateScheduled;

    private AmbientBrightnessStatsTracker mAmbientBrightnessStatsTracker;
    private final Runnable mAmbientBrightnessStatsWriter = () -> writeAmbientBrightnessStats();
    private volatile boolean mWriteBrightnessStatsScheduled;

    private UserManager mUserManager;
    private final Context mContext;
@@ -167,13 +165,7 @@ public class BrightnessTracker {
        }
        mBgHandler = new TrackerHandler(mInjector.getBackgroundHandler().getLooper());
        mUserManager = mContext.getSystemService(UserManager.class);
        try {
            final ActivityManager.StackInfo focusedStack = mInjector.getFocusedStack();
            mCurrentUserId = focusedStack.userId;
        } catch (RemoteException e) {
            // Really shouldn't be possible.
            return;
        }
        mCurrentUserId = ActivityManager.getCurrentUser();
        mBgHandler.obtainMessage(MSG_BACKGROUND_START, (Float) initialBrightness).sendToTarget();
    }

@@ -355,18 +347,17 @@ public class BrightnessTracker {
    }

    private void scheduleWriteBrightnessTrackerState() {
        if (!mWriteEventsScheduled) {
            mBgHandler.post(mEventsWriter);
            mWriteEventsScheduled = true;
        }
        if (!mWriteBrightnessStatsScheduled) {
            mBgHandler.post(mAmbientBrightnessStatsWriter);
            mWriteBrightnessStatsScheduled = true;
        if (!mWriteBrightnessTrackerStateScheduled) {
            mBgHandler.post(() -> {
                mWriteBrightnessTrackerStateScheduled = false;
                writeEvents();
                writeAmbientBrightnessStats();
            });
            mWriteBrightnessTrackerStateScheduled = true;
        }
    }

    private void writeEvents() {
        mWriteEventsScheduled = false;
        synchronized (mEventsLock) {
            if (!mEventsDirty) {
                // Nothing to write
@@ -398,7 +389,6 @@ public class BrightnessTracker {
    }

    private void writeAmbientBrightnessStats() {
        mWriteBrightnessStatsScheduled = false;
        final AtomicFile writeTo = mInjector.getFile(AMBIENT_BRIGHTNESS_STATS_FILE);
        if (writeTo == null) {
            return;
@@ -642,6 +632,7 @@ public class BrightnessTracker {
            }
        }
        if (mAmbientBrightnessStatsTracker != null) {
            pw.println();
            mAmbientBrightnessStatsTracker.dump(pw);
        }
    }
+0 −1
Original line number Diff line number Diff line
@@ -51,7 +51,6 @@ import android.os.UserHandle;
import android.provider.Settings;
import android.util.MathUtils;
import android.util.Slog;
import android.util.Spline;
import android.util.TimeUtils;
import android.view.Display;