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

Commit 580098fe authored by Salvador Martinez's avatar Salvador Martinez
Browse files

Make Settings and SysUI estimates use same data class

This moves everything over to using a shared data class
for Estimates in SettingsLib which will facilitate a cl
that will help ensure greater consistency across
surfaces where battery estimates are shown.

Test: Tests pass
Bug: 124030091
Change-Id: I0b7f1f3a806255ff4804a00e6d90a7846c484484
parent a2620b9c
Loading
Loading
Loading
Loading
+39 −0
Original line number Diff line number Diff line
@@ -12665,6 +12665,45 @@ public final class Settings {
        @TestApi
        public static final String DYNAMIC_POWER_SAVINGS_ENABLED = "dynamic_power_savings_enabled";
        /**
         * A long value indicating how much longer the system battery is estimated to last in
         * millis. See {@link #BATTERY_ESTIMATES_LAST_UPDATE_TIME} for the last time this value
         * was updated.
         *
         * @hide
         */
        public static final String TIME_REMAINING_ESTIMATE_MILLIS =
                "time_remaining_estimate_millis";
        /**
         * A boolean indicating whether {@link #TIME_REMAINING_ESTIMATE_MILLIS} is based customized
         * to the devices usage or using global models. See
         * {@link #BATTERY_ESTIMATES_LAST_UPDATE_TIME} for the last time this value was updated.
         *
         * @hide
         */
        public static final String TIME_REMAINING_ESTIMATE_BASED_ON_USAGE =
                "time_remaining_estimate_based_on_usage";
        /**
         * A long value indicating how long the system battery takes to deplete from 100% to 0% on
         * average based on historical drain rates. See {@link #BATTERY_ESTIMATES_LAST_UPDATE_TIME}
         * for the last time this value was updated.
         *
         * @hide
         */
        public static final String AVERAGE_TIME_TO_DISCHARGE = "average_time_to_discharge";
        /**
         * A long indicating the epoch time in milliseconds when
         * {@link #TIME_REMAINING_ESTIMATE_MILLIS}, {@link #TIME_REMAINING_ESTIMATE_BASED_ON_USAGE},
         * and {@link #AVERAGE_TIME_TO_DISCHARGE} were last updated.
         *
         * @hide
         */
        public static final String BATTERY_ESTIMATES_LAST_UPDATE_TIME =
                "battery_estimates_last_update_time";
        /**
         * The max value for {@link #LOW_POWER_MODE_TRIGGER_LEVEL}. If this setting is not set
         * or the value is 0, the default max will be used.
+4 −0
Original line number Diff line number Diff line
@@ -130,8 +130,10 @@ public class SettingsBackupTest {
                    Settings.Global.AUTOFILL_MAX_PARTITIONS_SIZE,
                    Settings.Global.AUTOFILL_MAX_VISIBLE_DATASETS,
                    Settings.Global.AUTOMATIC_POWER_SAVE_MODE,
                    Settings.Global.AVERAGE_TIME_TO_DISCHARGE,
                    Settings.Global.BACKGROUND_ACTIVITY_STARTS_ENABLED,
                    Settings.Global.BATTERY_CHARGING_STATE_UPDATE_DELAY,
                    Settings.Global.BATTERY_ESTIMATES_LAST_UPDATE_TIME,
                    Settings.Global.BROADCAST_BG_CONSTANTS,
                    Settings.Global.BROADCAST_FG_CONSTANTS,
                    Settings.Global.BROADCAST_OFFLOAD_CONSTANTS,
@@ -462,6 +464,8 @@ public class SettingsBackupTest {
                    Settings.Global.TEXT_CLASSIFIER_ACTION_MODEL_PARAMS,
                    Settings.Global.THEATER_MODE_ON,
                    Settings.Global.TIME_ONLY_MODE_CONSTANTS,
                    Settings.Global.TIME_REMAINING_ESTIMATE_MILLIS,
                    Settings.Global.TIME_REMAINING_ESTIMATE_BASED_ON_USAGE,
                    Settings.Global.TRANSITION_ANIMATION_SCALE,
                    Settings.Global.TRUSTED_SOUND,
                    Settings.Global.TZINFO_UPDATE_CONTENT_URL,
+2 −1
Original line number Diff line number Diff line
@@ -37,7 +37,8 @@ public class BatterySaverUtils {
    /**
     * When set to "true" the notification will be a generic confirm message instead of asking the
     * user if they want to turn on battery saver. If set to false the dialog will specifically
     * talk about turning on battery saver and provide a button for taking the action.
     * talk about battery saver without giving the option of turning it on. The only button visible
     * will be a generic confirmation button to acknowledge the dialog.
     */
    public static final String EXTRA_CONFIRM_TEXT_ONLY = "extra_confirm_only";
    /**
+81 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License
 */

package com.android.settingslib.fuelgauge

import android.content.Context
import android.provider.Settings
import java.time.Duration
import java.time.Instant

const val AVERAGE_TIME_TO_DISCHARGE_UNKNOWN = -1
const val ESTIMATE_MILLIS_UNKNOWN = -1

class Estimate(
    val estimateMillis: Long,
    val isBasedOnUsage: Boolean,
    val averageDischargeTime: Long
) {
    companion object {
        /**
         * Returns the cached estimate if it is available and fresh. Will return null if estimate is
         * unavailable or older than 2 minutes.
         *
         * @param context A valid context
         * @return An [Estimate] object with the latest battery estimates.
         */
        @JvmStatic
        fun getCachedEstimateIfAvailable(context: Context): Estimate? {
            // if time > 2 min return null or the estimate otherwise
            val resolver = context.contentResolver
            val lastUpdateTime = Instant.ofEpochMilli(
                    Settings.Global.getLong(
                            resolver, Settings.Global.BATTERY_ESTIMATES_LAST_UPDATE_TIME, -1))
            return if (Duration.between(lastUpdateTime,
                            Instant.now()).compareTo(Duration.ofMinutes(2)) > 0) {
                null
            } else Estimate(
                    Settings.Global.getLong(resolver,
                            Settings.Global.TIME_REMAINING_ESTIMATE_MILLIS,
                            ESTIMATE_MILLIS_UNKNOWN.toLong()),
                    Settings.Global.getInt(resolver,
                            Settings.Global.TIME_REMAINING_ESTIMATE_BASED_ON_USAGE, 0) == 1,
                    Settings.Global.getLong(resolver, Settings.Global.AVERAGE_TIME_TO_DISCHARGE,
                            AVERAGE_TIME_TO_DISCHARGE_UNKNOWN.toLong()))
        }

        /**
         * Stores an estimate to the cache along with a timestamp. Can be obtained via
         * [.getCachedEstimateIfAvailable].
         *
         * @param context A valid context
         * @param estimate the [Estimate] object to store
         */
        @JvmStatic
        fun storeCachedEstimate(context: Context, estimate: Estimate) {
            // store the estimate and update the timestamp
            val resolver = context.contentResolver
            Settings.Global.putLong(resolver, Settings.Global.TIME_REMAINING_ESTIMATE_MILLIS,
                    estimate.estimateMillis)
            Settings.Global.putInt(resolver, Settings.Global.TIME_REMAINING_ESTIMATE_BASED_ON_USAGE,
                    if (estimate.isBasedOnUsage) 1 else 0)
            Settings.Global.putLong(resolver, Settings.Global.AVERAGE_TIME_TO_DISCHARGE,
                    estimate.averageDischargeTime)
            Settings.Global.putLong(resolver, Settings.Global.BATTERY_ESTIMATES_LAST_UPDATE_TIME,
                    System.currentTimeMillis())
        }
    }
}
+14 −12
Original line number Diff line number Diff line
@@ -15,6 +15,7 @@ data class BatteryStateSnapshot(
    val severeLevelThreshold: Int,
    val lowLevelThreshold: Int,
    val timeRemainingMillis: Long,
    val averageTimeToDischargeMillis: Long,
    val severeThresholdMillis: Long,
    val lowThresholdMillis: Long,
    val isBasedOnUsage: Boolean,
@@ -49,6 +50,7 @@ data class BatteryStateSnapshot(
            NO_ESTIMATE_AVAILABLE.toLong(),
            NO_ESTIMATE_AVAILABLE.toLong(),
            NO_ESTIMATE_AVAILABLE.toLong(),
            NO_ESTIMATE_AVAILABLE.toLong(),
            false,
            true
    ) {
Loading