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

Commit 3a1fc35b authored by Pajace Chen's avatar Pajace Chen Committed by Android (Google) Code Review
Browse files

Merge "[BatteryEventFramework] Update util functions for BatteryEventModule" into main

parents bf875df5 bcacb5f7
Loading
Loading
Loading
Loading
+1 −2
Original line number Original line Diff line number Diff line
@@ -67,8 +67,7 @@ public class Utils {
    static final String STORAGE_MANAGER_ENABLED_PROPERTY =
    static final String STORAGE_MANAGER_ENABLED_PROPERTY =
            "ro.storage_manager.enabled";
            "ro.storage_manager.enabled";


    @VisibleForTesting
    public static final String INCOMPATIBLE_CHARGER_WARNING_DISABLED =
    static final String INCOMPATIBLE_CHARGER_WARNING_DISABLED =
            "incompatible_charger_warning_disabled";
            "incompatible_charger_warning_disabled";


    private static Signature[] sSystemSignature;
    private static Signature[] sSystemSignature;
+84 −11
Original line number Original line Diff line number Diff line
@@ -110,7 +110,8 @@ public class BatteryStatus {
    }
    }


    /**
    /**
     * Determine whether the device is plugged in wireless. */
     * Determine whether the device is plugged in wireless.
     */
    public boolean isPluggedInWireless() {
    public boolean isPluggedInWireless() {
        return plugged == BatteryManager.BATTERY_PLUGGED_WIRELESS;
        return plugged == BatteryManager.BATTERY_PLUGGED_WIRELESS;
    }
    }
@@ -185,6 +186,22 @@ public class BatteryStatus {
        return status == BATTERY_STATUS_FULL || level >= 100;
        return status == BATTERY_STATUS_FULL || level >= 100;
    }
    }


    /**
     * Whether or not the device is charged. Note that some devices never return 100% for battery
     * level, so this allows either battery level or status to determine if the battery is charged.
     *
     * @param status the value from extra {@link BatteryManager.EXTRA_STATUS} of
     *     {@link Intent.ACTION_BATTERY_CHANGED} intent
     * @param level the value from extra {@link BatteryManager.EXTRA_LEVEL} of
     *     {@link Intent.ACTION_BATTERY_CHANGED} intent
     * @param scale the value from extra {@link BatteryManager.EXTRA_SCALE} of
     *     {@link Intent.ACTION_BATTERY_CHANGED} intent
     */
    public static boolean isCharged(int status, int level, int scale) {
        var batteryLevel = getBatteryLevel(level, scale);
        return isCharged(status, batteryLevel);
    }

    /** Gets the battery level from the intent. */
    /** Gets the battery level from the intent. */
    public static int getBatteryLevel(Intent batteryChangedIntent) {
    public static int getBatteryLevel(Intent batteryChangedIntent) {
        if (batteryChangedIntent == null) {
        if (batteryChangedIntent == null) {
@@ -193,6 +210,14 @@ public class BatteryStatus {
        final int level =
        final int level =
                batteryChangedIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, BATTERY_LEVEL_UNKNOWN);
                batteryChangedIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, BATTERY_LEVEL_UNKNOWN);
        final int scale = batteryChangedIntent.getIntExtra(BatteryManager.EXTRA_SCALE, 0);
        final int scale = batteryChangedIntent.getIntExtra(BatteryManager.EXTRA_SCALE, 0);
        return getBatteryLevel(level, scale);
    }

    /**
     * Gets the battery level from the value of {@link Intent.BATTERY_CHANGED_INTENT}'s EXTRA_LEVEL
     * and EXTRA_SCALE.
     */
    public static int getBatteryLevel(int level, int scale) {
        return scale == 0
        return scale == 0
                ? BATTERY_LEVEL_UNKNOWN
                ? BATTERY_LEVEL_UNKNOWN
                : Math.round((level / (float) scale) * 100f);
                : Math.round((level / (float) scale) * 100f);
@@ -256,8 +281,19 @@ public class BatteryStatus {
     * SEVERE_LOW_BATTERY_THRESHOLD}
     * SEVERE_LOW_BATTERY_THRESHOLD}
     */
     */
    public static boolean isSevereLowBattery(Intent batteryChangedIntent) {
    public static boolean isSevereLowBattery(Intent batteryChangedIntent) {
        int level = getBatteryLevel(batteryChangedIntent);
        int batteryLevel = getBatteryLevel(batteryChangedIntent);
        return level <= SEVERE_LOW_BATTERY_THRESHOLD;
        return isSevereLowBattery(batteryLevel);
    }

    /**
     * Whether the battery is severe low or not.
     *
     * @param batteryLevel the value of battery level
     * @return {@code true} if the battery level is less or equal to {@link
     * SEVERE_LOW_BATTERY_THRESHOLD}
     */
    public static boolean isSevereLowBattery(int batteryLevel) {
        return batteryLevel <= SEVERE_LOW_BATTERY_THRESHOLD;
    }
    }


    /**
    /**
@@ -269,7 +305,17 @@ public class BatteryStatus {
     */
     */
    public static boolean isExtremeLowBattery(Intent batteryChangedIntent) {
    public static boolean isExtremeLowBattery(Intent batteryChangedIntent) {
        int level = getBatteryLevel(batteryChangedIntent);
        int level = getBatteryLevel(batteryChangedIntent);
        return level <= EXTREME_LOW_BATTERY_THRESHOLD;
        return isExtremeLowBattery(level);
    }

    /**
     * Whether the battery is extreme low or not.
     *
     * @return {@code true} if the {@code batteryLevel} is less or equal to
     * {@link EXTREME_LOW_BATTERY_THRESHOLD}
     */
    public static boolean isExtremeLowBattery(int batteryLevel) {
        return batteryLevel <= EXTREME_LOW_BATTERY_THRESHOLD;
    }
    }


    /**
    /**
@@ -298,9 +344,8 @@ public class BatteryStatus {
    }
    }


    /**
    /**
     * Gets the max charging current and max charging voltage form {@link
     * Calculates the charging speed based on the {@link R.integer.config_chargingSlowlyThreshold}
     * Intent.ACTION_BATTERY_CHANGED} and calculates the charging speed based on the {@link
     * and {@link R.integer.config_chargingFastThreshold}.
     * R.integer.config_chargingSlowlyThreshold} and {@link R.integer.config_chargingFastThreshold}.
     *
     *
     * @param context the application context
     * @param context the application context
     * @param batteryChangedIntent the intent from {@link Intent.ACTION_BATTERY_CHANGED}
     * @param batteryChangedIntent the intent from {@link Intent.ACTION_BATTERY_CHANGED}
@@ -308,7 +353,29 @@ public class BatteryStatus {
     *     CHARGING_SLOWLY} or {@link CHARGING_UNKNOWN}
     *     CHARGING_SLOWLY} or {@link CHARGING_UNKNOWN}
     */
     */
    public static int getChargingSpeed(Context context, Intent batteryChangedIntent) {
    public static int getChargingSpeed(Context context, Intent batteryChangedIntent) {
        final int maxChargingMicroWatt = calculateMaxChargingMicroWatt(batteryChangedIntent);
        final int maxChargingMicroCurrent =
                batteryChangedIntent.getIntExtra(EXTRA_MAX_CHARGING_CURRENT, -1);
        int maxChargingMicroVolt = batteryChangedIntent.getIntExtra(EXTRA_MAX_CHARGING_VOLTAGE, -1);

        return calculateChargingSpeed(context, maxChargingMicroCurrent, maxChargingMicroVolt);
    }

    /**
     * Calculates the charging speed based on the {@link R.integer.config_chargingSlowlyThreshold}
     * and {@link R.integer.config_chargingFastThreshold}.
     *
     * @param maxChargingMicroCurrent the max charging micro current that is retrieved form the
     *     extra of {@link Intent.Action_BATTERY_CHANGED}
     * @param maxChargingMicroVolt the max charging micro voltage that is retrieved form the extra
     *     of {@link Intent.Action_BATTERY_CHANGED}
     * @return the charging speed. {@link CHARGING_REGULAR}, {@link CHARGING_FAST}, {@link
     *     CHARGING_SLOWLY} or {@link CHARGING_UNKNOWN}
     */
    public static int calculateChargingSpeed(
            Context context, int maxChargingMicroCurrent, int maxChargingMicroVolt) {
        final int maxChargingMicroWatt =
                calculateMaxChargingMicroWatt(maxChargingMicroCurrent, maxChargingMicroVolt);

        if (maxChargingMicroWatt <= 0) {
        if (maxChargingMicroWatt <= 0) {
            return CHARGING_UNKNOWN;
            return CHARGING_UNKNOWN;
        } else if (maxChargingMicroWatt
        } else if (maxChargingMicroWatt
@@ -326,6 +393,12 @@ public class BatteryStatus {
        final int maxChargingMicroAmp =
        final int maxChargingMicroAmp =
                batteryChangedIntent.getIntExtra(EXTRA_MAX_CHARGING_CURRENT, -1);
                batteryChangedIntent.getIntExtra(EXTRA_MAX_CHARGING_CURRENT, -1);
        int maxChargingMicroVolt = batteryChangedIntent.getIntExtra(EXTRA_MAX_CHARGING_VOLTAGE, -1);
        int maxChargingMicroVolt = batteryChangedIntent.getIntExtra(EXTRA_MAX_CHARGING_VOLTAGE, -1);

        return calculateMaxChargingMicroWatt(maxChargingMicroAmp, maxChargingMicroVolt);
    }

    private static int calculateMaxChargingMicroWatt(int maxChargingMicroAmp,
            int maxChargingMicroVolt) {
        if (maxChargingMicroVolt <= 0) {
        if (maxChargingMicroVolt <= 0) {
            maxChargingMicroVolt = DEFAULT_CHARGING_VOLTAGE_MICRO_VOLT;
            maxChargingMicroVolt = DEFAULT_CHARGING_VOLTAGE_MICRO_VOLT;
        }
        }