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

Commit 0941120f authored by Ng Zhi An's avatar Ng Zhi An
Browse files

Add api to change charging state update delay

This adds a field Settings.Global.BATTERY_CHARGING_STATE_UPDATE_DELAY
that overrides the value of battery_charged_delay_ms in
Settings.GLOBAL.BATTERY_STATS_CONSTANTS.

This new field can then be set for experimentation, and easily reset to
default by deleting, or setting it to a negative value.

Expose a method in BatteryManager to set a value for this new setting.

Bug: 111360323
Test: adb shell settings put global battery_charging_state_update_delay 999
adb shell dumpsys batterystats --settings # should see battery_charged_delay_ms=999
adb shell settings put global battery_charging_state_update_delay -1
adb shell dumpsys batterystats --settings # should see battery_charged_delay_ms=90000
Change-Id: Ic308af938836a1f9c235cec341808b6c6c28d22d
parent c1b889d2
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -5017,6 +5017,7 @@ package android.nfc {
package android.os {
  public class BatteryManager {
    method @RequiresPermission(android.Manifest.permission.POWER_SAVER) public boolean setChargingStateUpdateDelayMillis(int);
    field public static final String EXTRA_EVENTS = "android.os.extra.EVENTS";
    field public static final String EXTRA_EVENT_TIMESTAMP = "android.os.extra.EVENT_TIMESTAMP";
  }
+24 −0
Original line number Diff line number Diff line
@@ -16,6 +16,8 @@

package android.os;

import android.Manifest.permission;
import android.annotation.RequiresPermission;
import android.annotation.SystemApi;
import android.annotation.SystemService;
import android.content.Context;
@@ -369,4 +371,26 @@ public class BatteryManager {
            throw e.rethrowFromSystemServer();
        }
    }

    /**
     * Sets the delay for reporting battery state as charging after device is plugged in.
     * This allows machine-learning or heuristics to delay the reporting and the corresponding
     * broadcast, based on battery level, charging rate, and/or other parameters.
     *
     * @param delayMillis the delay in milliseconds, negative value to reset.
     *
     * @return True if the delay was set successfully.
     *
     * @see ACTION_CHARGING
     * @hide
     */
    @RequiresPermission(permission.POWER_SAVER)
    @SystemApi
    public boolean setChargingStateUpdateDelayMillis(int delayMillis) {
        try {
            return mBatteryStats.setChargingStateUpdateDelayMillis(delayMillis);
        } catch (RemoteException e) {
            throw e.rethrowFromSystemServer();
        }
    }
}
+11 −0
Original line number Diff line number Diff line
@@ -14279,6 +14279,17 @@ public final class Settings {
         */
        public static final String APPOP_HISTORY_PARAMETERS =
                "appop_history_parameters";
        /**
         * Delay for sending ACTION_CHARGING after device is plugged in.
         * This is used as an override for constants defined in BatteryStatsImpl for
         * ease of experimentation.
         *
         * @see com.android.internal.os.BatteryStatsImpl.Constants.KEY_BATTERY_CHARGED_DELAY_MS
         * @hide
         */
        public static final String BATTERY_CHARGING_STATE_UPDATE_DELAY =
                "battery_charging_state_update_delay";
    }
    /**
+3 −0
Original line number Diff line number Diff line
@@ -154,4 +154,7 @@ interface IBatteryStats {
    oneway void noteBluetoothControllerActivity(in BluetoothActivityEnergyInfo info);
    oneway void noteModemControllerActivity(in ModemActivityInfo info);
    oneway void noteWifiControllerActivity(in WifiActivityEnergyInfo info);

    /** {@hide} */
    boolean setChargingStateUpdateDelayMillis(int delay);
}
+23 −3
Original line number Diff line number Diff line
@@ -13395,11 +13395,22 @@ public class BatteryStatsImpl extends BatteryStats {
            mResolver.registerContentObserver(
                    Settings.Global.getUriFor(Settings.Global.BATTERY_STATS_CONSTANTS),
                    false /* notifyForDescendants */, this);
            mResolver.registerContentObserver(
                    Settings.Global.getUriFor(Settings.Global.BATTERY_CHARGING_STATE_UPDATE_DELAY),
                    false /* notifyForDescendants */, this);
            updateConstants();
        }
        @Override
        public void onChange(boolean selfChange, Uri uri) {
            if (uri.equals(
                    Settings.Global.getUriFor(
                            Settings.Global.BATTERY_CHARGING_STATE_UPDATE_DELAY))) {
                synchronized (BatteryStatsImpl.this) {
                    updateBatteryChargedDelayMsLocked();
                }
                return;
            }
            updateConstants();
        }
@@ -13443,11 +13454,20 @@ public class BatteryStatsImpl extends BatteryStats {
                                DEFAULT_MAX_HISTORY_BUFFER_LOW_RAM_DEVICE_KB
                                : DEFAULT_MAX_HISTORY_BUFFER_KB)
                        * 1024;
                BATTERY_CHARGED_DELAY_MS = mParser.getInt(
                updateBatteryChargedDelayMsLocked();
            }
        }
        private void updateBatteryChargedDelayMsLocked() {
            // a negative value indicates that we should ignore this override
            final int delay = Settings.Global.getInt(mResolver,
                    Settings.Global.BATTERY_CHARGING_STATE_UPDATE_DELAY,
                    -1);
            BATTERY_CHARGED_DELAY_MS = delay >= 0 ? delay : mParser.getInt(
                    KEY_BATTERY_CHARGED_DELAY_MS,
                    DEFAULT_BATTERY_CHARGED_DELAY_MS);
        }
        }
        private void updateTrackCpuTimesByProcStateLocked(boolean wasEnabled, boolean isEnabled) {
            TRACK_CPU_TIMES_BY_PROC_STATE = isEnabled;
Loading