Loading packages/SettingsLib/res/values/config.xml 0 → 100644 +24 −0 Original line number Diff line number Diff line <?xml version="1.0" encoding="utf-8"?> <!-- ~ 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. --> <!-- These resources are around just to allow their values to be customized --> <resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2"> <!-- Threshold in micro watts below which a charger is rated as "slow"; 1A @ 5V --> <integer name="config_chargingSlowlyThreshold">5000000</integer> <!-- Threshold in micro watts above which a charger is rated as "fast"; 1.5A @ 5V --> <integer name="config_chargingFastThreshold">7500000</integer> </resources> No newline at end of file packages/SettingsLib/res/values/strings.xml +4 −2 Original line number Diff line number Diff line Loading @@ -1034,8 +1034,10 @@ <string name="battery_info_status_unknown">Unknown</string> <!-- [CHAR_LIMIT=20] Battery use screen. Battery status shown in chart label when charging from an unknown source. --> <string name="battery_info_status_charging">Charging</string> <!-- [CHAR_LIMIT=20] Battery use screen with lower case. Battery status shown in chart label when charging from an unknown source. --> <string name="battery_info_status_charging_lower">charging</string> <!-- [CHAR_LIMIT=20] Battery use screen. Battery status shown in chart label when charging speed is fast. --> <string name="battery_info_status_charging_fast">Charging rapidly</string> <!-- [CHAR_LIMIT=20] Battery use screen. Battery status shown in chart label when charging speed is slow. --> <string name="battery_info_status_charging_slow">Charging slowly</string> <!-- Battery Info screen. Value for a status item. Used for diagnostic info screens, precise translation isn't needed --> <string name="battery_info_status_discharging">Not charging</string> <!-- Battery Info screen. Value for a status item. Used for diagnostic info screens, precise translation isn't needed --> Loading packages/SettingsLib/src/com/android/settingslib/Utils.java +37 −13 Original line number Diff line number Diff line Loading @@ -35,6 +35,7 @@ import com.android.internal.annotations.VisibleForTesting; import com.android.internal.util.UserIcons; import com.android.launcher3.icons.IconFactory; import com.android.settingslib.drawable.UserIconDrawable; import com.android.settingslib.fuelgauge.BatteryStatus; import java.text.NumberFormat; Loading Loading @@ -164,20 +165,43 @@ public class Utils { return (level * 100) / scale; } public static String getBatteryStatus(Resources res, Intent batteryChangedIntent) { int status = batteryChangedIntent.getIntExtra(BatteryManager.EXTRA_STATUS, /** * Get battery status string * * @param context the context * @param batteryChangedIntent battery broadcast intent received from {@link * Intent.ACTION_BATTERY_CHANGED}. * @return battery status string */ public static String getBatteryStatus(Context context, Intent batteryChangedIntent) { final int status = batteryChangedIntent.getIntExtra(BatteryManager.EXTRA_STATUS, BatteryManager.BATTERY_STATUS_UNKNOWN); String statusString; final Resources res = context.getResources(); String statusString = res.getString(R.string.battery_info_status_unknown); final BatteryStatus batteryStatus = new BatteryStatus(batteryChangedIntent); if (batteryStatus.isCharged()) { statusString = res.getString(R.string.battery_info_status_full); } else { if (status == BatteryManager.BATTERY_STATUS_CHARGING) { switch (batteryStatus.getChargingSpeed(context)) { case BatteryStatus.CHARGING_FAST: statusString = res.getString(R.string.battery_info_status_charging_fast); break; case BatteryStatus.CHARGING_SLOWLY: statusString = res.getString(R.string.battery_info_status_charging_slow); break; default: statusString = res.getString(R.string.battery_info_status_charging); break; } } else if (status == BatteryManager.BATTERY_STATUS_DISCHARGING) { statusString = res.getString(R.string.battery_info_status_discharging); } else if (status == BatteryManager.BATTERY_STATUS_NOT_CHARGING) { statusString = res.getString(R.string.battery_info_status_not_charging); } else if (status == BatteryManager.BATTERY_STATUS_FULL) { statusString = res.getString(R.string.battery_info_status_full); } else { statusString = res.getString(R.string.battery_info_status_unknown); } } return statusString; Loading Loading @@ -211,7 +235,7 @@ public class Utils { /** * This method computes disabled color from normal color * * @param context * @param context the context * @param inputColor normal color. * @return disabled color. */ Loading packages/SettingsLib/src/com/android/settingslib/fuelgauge/BatteryStatus.java 0 → 100644 +147 −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 static android.os.BatteryManager.BATTERY_HEALTH_UNKNOWN; import static android.os.BatteryManager.BATTERY_STATUS_FULL; import static android.os.BatteryManager.BATTERY_STATUS_UNKNOWN; import static android.os.BatteryManager.EXTRA_HEALTH; import static android.os.BatteryManager.EXTRA_LEVEL; import static android.os.BatteryManager.EXTRA_MAX_CHARGING_CURRENT; import static android.os.BatteryManager.EXTRA_MAX_CHARGING_VOLTAGE; import static android.os.BatteryManager.EXTRA_PLUGGED; import static android.os.BatteryManager.EXTRA_STATUS; import android.content.Context; import android.content.Intent; import android.os.BatteryManager; import com.android.settingslib.R; /** * Stores and computes some battery information. */ public class BatteryStatus { private static final int LOW_BATTERY_THRESHOLD = 20; private static final int DEFAULT_CHARGING_VOLTAGE_MICRO_VOLT = 5000000; public static final int CHARGING_UNKNOWN = -1; public static final int CHARGING_SLOWLY = 0; public static final int CHARGING_REGULAR = 1; public static final int CHARGING_FAST = 2; public final int status; public final int level; public final int plugged; public final int health; public final int maxChargingWattage; public BatteryStatus(int status, int level, int plugged, int health, int maxChargingWattage) { this.status = status; this.level = level; this.plugged = plugged; this.health = health; this.maxChargingWattage = maxChargingWattage; } public BatteryStatus(Intent batteryChangedIntent) { status = batteryChangedIntent.getIntExtra(EXTRA_STATUS, BATTERY_STATUS_UNKNOWN); plugged = batteryChangedIntent.getIntExtra(EXTRA_PLUGGED, 0); level = batteryChangedIntent.getIntExtra(EXTRA_LEVEL, 0); health = batteryChangedIntent.getIntExtra(EXTRA_HEALTH, BATTERY_HEALTH_UNKNOWN); final int maxChargingMicroAmp = batteryChangedIntent.getIntExtra(EXTRA_MAX_CHARGING_CURRENT, -1); int maxChargingMicroVolt = batteryChangedIntent.getIntExtra(EXTRA_MAX_CHARGING_VOLTAGE, -1); if (maxChargingMicroVolt <= 0) { maxChargingMicroVolt = DEFAULT_CHARGING_VOLTAGE_MICRO_VOLT; } if (maxChargingMicroAmp > 0) { // Calculating muW = muA * muV / (10^6 mu^2 / mu); splitting up the divisor // to maintain precision equally on both factors. maxChargingWattage = (maxChargingMicroAmp / 1000) * (maxChargingMicroVolt / 1000); } else { maxChargingWattage = -1; } } /** * Determine whether the device is plugged in (USB, power, or wireless). * * @return true if the device is plugged in. */ public boolean isPluggedIn() { return plugged == BatteryManager.BATTERY_PLUGGED_AC || plugged == BatteryManager.BATTERY_PLUGGED_USB || plugged == BatteryManager.BATTERY_PLUGGED_WIRELESS; } /** * Determine whether the device is plugged in (USB, power). * * @return true if the device is plugged in wired (as opposed to wireless) */ public boolean isPluggedInWired() { return plugged == BatteryManager.BATTERY_PLUGGED_AC || plugged == BatteryManager.BATTERY_PLUGGED_USB; } /** * 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. * * @return true if the device is charged */ public boolean isCharged() { return status == BATTERY_STATUS_FULL || level >= 100; } /** * Whether battery is low and needs to be charged. * * @return true if battery is low */ public boolean isBatteryLow() { return level < LOW_BATTERY_THRESHOLD; } /** * Return current chargin speed is fast, slow or normal. * * @return the charing speed */ public final int getChargingSpeed(Context context) { final int slowThreshold = context.getResources().getInteger( R.integer.config_chargingSlowlyThreshold); final int fastThreshold = context.getResources().getInteger( R.integer.config_chargingFastThreshold); return maxChargingWattage <= 0 ? CHARGING_UNKNOWN : maxChargingWattage < slowThreshold ? CHARGING_SLOWLY : maxChargingWattage > fastThreshold ? CHARGING_FAST : CHARGING_REGULAR; } @Override public String toString() { return "BatteryStatus{status=" + status + ",level=" + level + ",plugged=" + plugged + ",health=" + health + ",maxChargingWattage=" + maxChargingWattage + "}"; } } packages/SettingsLib/tests/robotests/src/com/android/settingslib/UtilsTest.java +38 −7 Original line number Diff line number Diff line Loading @@ -31,6 +31,7 @@ import android.content.Intent; import android.content.res.Resources; import android.location.LocationManager; import android.media.AudioManager; import android.os.BatteryManager; import android.os.SystemProperties; import android.os.UserHandle; import android.provider.Settings; Loading Loading @@ -147,7 +148,8 @@ public class UtilsTest { private static Map<String, Integer> map = new HashMap<>(); @Implementation public static boolean putIntForUser(ContentResolver cr, String name, int value, int userHandle) { public static boolean putIntForUser(ContentResolver cr, String name, int value, int userHandle) { map.put(name, value); return true; } Loading Loading @@ -312,4 +314,33 @@ public class UtilsTest { assertThat(Utils.getCombinedServiceState(mServiceState)).isEqualTo( ServiceState.STATE_OUT_OF_SERVICE); } @Test public void getBatteryStatus_statusIsFull_returnFullString() { final Intent intent = new Intent().putExtra(BatteryManager.EXTRA_LEVEL, 100); final Resources resources = mContext.getResources(); assertThat(Utils.getBatteryStatus(mContext, intent)).isEqualTo( resources.getString(R.string.battery_info_status_full)); } @Test public void getBatteryStatus_batteryLevelIs100_returnFullString() { final Intent intent = new Intent().putExtra(BatteryManager.EXTRA_STATUS, BatteryManager.BATTERY_STATUS_FULL); final Resources resources = mContext.getResources(); assertThat(Utils.getBatteryStatus(mContext, intent)).isEqualTo( resources.getString(R.string.battery_info_status_full)); } @Test public void getBatteryStatus_batteryLevel99_returnChargingString() { final Intent intent = new Intent().putExtra(BatteryManager.EXTRA_STATUS, BatteryManager.BATTERY_STATUS_CHARGING); final Resources resources = mContext.getResources(); assertThat(Utils.getBatteryStatus(mContext, intent)).isEqualTo( resources.getString(R.string.battery_info_status_charging)); } } Loading
packages/SettingsLib/res/values/config.xml 0 → 100644 +24 −0 Original line number Diff line number Diff line <?xml version="1.0" encoding="utf-8"?> <!-- ~ 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. --> <!-- These resources are around just to allow their values to be customized --> <resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2"> <!-- Threshold in micro watts below which a charger is rated as "slow"; 1A @ 5V --> <integer name="config_chargingSlowlyThreshold">5000000</integer> <!-- Threshold in micro watts above which a charger is rated as "fast"; 1.5A @ 5V --> <integer name="config_chargingFastThreshold">7500000</integer> </resources> No newline at end of file
packages/SettingsLib/res/values/strings.xml +4 −2 Original line number Diff line number Diff line Loading @@ -1034,8 +1034,10 @@ <string name="battery_info_status_unknown">Unknown</string> <!-- [CHAR_LIMIT=20] Battery use screen. Battery status shown in chart label when charging from an unknown source. --> <string name="battery_info_status_charging">Charging</string> <!-- [CHAR_LIMIT=20] Battery use screen with lower case. Battery status shown in chart label when charging from an unknown source. --> <string name="battery_info_status_charging_lower">charging</string> <!-- [CHAR_LIMIT=20] Battery use screen. Battery status shown in chart label when charging speed is fast. --> <string name="battery_info_status_charging_fast">Charging rapidly</string> <!-- [CHAR_LIMIT=20] Battery use screen. Battery status shown in chart label when charging speed is slow. --> <string name="battery_info_status_charging_slow">Charging slowly</string> <!-- Battery Info screen. Value for a status item. Used for diagnostic info screens, precise translation isn't needed --> <string name="battery_info_status_discharging">Not charging</string> <!-- Battery Info screen. Value for a status item. Used for diagnostic info screens, precise translation isn't needed --> Loading
packages/SettingsLib/src/com/android/settingslib/Utils.java +37 −13 Original line number Diff line number Diff line Loading @@ -35,6 +35,7 @@ import com.android.internal.annotations.VisibleForTesting; import com.android.internal.util.UserIcons; import com.android.launcher3.icons.IconFactory; import com.android.settingslib.drawable.UserIconDrawable; import com.android.settingslib.fuelgauge.BatteryStatus; import java.text.NumberFormat; Loading Loading @@ -164,20 +165,43 @@ public class Utils { return (level * 100) / scale; } public static String getBatteryStatus(Resources res, Intent batteryChangedIntent) { int status = batteryChangedIntent.getIntExtra(BatteryManager.EXTRA_STATUS, /** * Get battery status string * * @param context the context * @param batteryChangedIntent battery broadcast intent received from {@link * Intent.ACTION_BATTERY_CHANGED}. * @return battery status string */ public static String getBatteryStatus(Context context, Intent batteryChangedIntent) { final int status = batteryChangedIntent.getIntExtra(BatteryManager.EXTRA_STATUS, BatteryManager.BATTERY_STATUS_UNKNOWN); String statusString; final Resources res = context.getResources(); String statusString = res.getString(R.string.battery_info_status_unknown); final BatteryStatus batteryStatus = new BatteryStatus(batteryChangedIntent); if (batteryStatus.isCharged()) { statusString = res.getString(R.string.battery_info_status_full); } else { if (status == BatteryManager.BATTERY_STATUS_CHARGING) { switch (batteryStatus.getChargingSpeed(context)) { case BatteryStatus.CHARGING_FAST: statusString = res.getString(R.string.battery_info_status_charging_fast); break; case BatteryStatus.CHARGING_SLOWLY: statusString = res.getString(R.string.battery_info_status_charging_slow); break; default: statusString = res.getString(R.string.battery_info_status_charging); break; } } else if (status == BatteryManager.BATTERY_STATUS_DISCHARGING) { statusString = res.getString(R.string.battery_info_status_discharging); } else if (status == BatteryManager.BATTERY_STATUS_NOT_CHARGING) { statusString = res.getString(R.string.battery_info_status_not_charging); } else if (status == BatteryManager.BATTERY_STATUS_FULL) { statusString = res.getString(R.string.battery_info_status_full); } else { statusString = res.getString(R.string.battery_info_status_unknown); } } return statusString; Loading Loading @@ -211,7 +235,7 @@ public class Utils { /** * This method computes disabled color from normal color * * @param context * @param context the context * @param inputColor normal color. * @return disabled color. */ Loading
packages/SettingsLib/src/com/android/settingslib/fuelgauge/BatteryStatus.java 0 → 100644 +147 −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 static android.os.BatteryManager.BATTERY_HEALTH_UNKNOWN; import static android.os.BatteryManager.BATTERY_STATUS_FULL; import static android.os.BatteryManager.BATTERY_STATUS_UNKNOWN; import static android.os.BatteryManager.EXTRA_HEALTH; import static android.os.BatteryManager.EXTRA_LEVEL; import static android.os.BatteryManager.EXTRA_MAX_CHARGING_CURRENT; import static android.os.BatteryManager.EXTRA_MAX_CHARGING_VOLTAGE; import static android.os.BatteryManager.EXTRA_PLUGGED; import static android.os.BatteryManager.EXTRA_STATUS; import android.content.Context; import android.content.Intent; import android.os.BatteryManager; import com.android.settingslib.R; /** * Stores and computes some battery information. */ public class BatteryStatus { private static final int LOW_BATTERY_THRESHOLD = 20; private static final int DEFAULT_CHARGING_VOLTAGE_MICRO_VOLT = 5000000; public static final int CHARGING_UNKNOWN = -1; public static final int CHARGING_SLOWLY = 0; public static final int CHARGING_REGULAR = 1; public static final int CHARGING_FAST = 2; public final int status; public final int level; public final int plugged; public final int health; public final int maxChargingWattage; public BatteryStatus(int status, int level, int plugged, int health, int maxChargingWattage) { this.status = status; this.level = level; this.plugged = plugged; this.health = health; this.maxChargingWattage = maxChargingWattage; } public BatteryStatus(Intent batteryChangedIntent) { status = batteryChangedIntent.getIntExtra(EXTRA_STATUS, BATTERY_STATUS_UNKNOWN); plugged = batteryChangedIntent.getIntExtra(EXTRA_PLUGGED, 0); level = batteryChangedIntent.getIntExtra(EXTRA_LEVEL, 0); health = batteryChangedIntent.getIntExtra(EXTRA_HEALTH, BATTERY_HEALTH_UNKNOWN); final int maxChargingMicroAmp = batteryChangedIntent.getIntExtra(EXTRA_MAX_CHARGING_CURRENT, -1); int maxChargingMicroVolt = batteryChangedIntent.getIntExtra(EXTRA_MAX_CHARGING_VOLTAGE, -1); if (maxChargingMicroVolt <= 0) { maxChargingMicroVolt = DEFAULT_CHARGING_VOLTAGE_MICRO_VOLT; } if (maxChargingMicroAmp > 0) { // Calculating muW = muA * muV / (10^6 mu^2 / mu); splitting up the divisor // to maintain precision equally on both factors. maxChargingWattage = (maxChargingMicroAmp / 1000) * (maxChargingMicroVolt / 1000); } else { maxChargingWattage = -1; } } /** * Determine whether the device is plugged in (USB, power, or wireless). * * @return true if the device is plugged in. */ public boolean isPluggedIn() { return plugged == BatteryManager.BATTERY_PLUGGED_AC || plugged == BatteryManager.BATTERY_PLUGGED_USB || plugged == BatteryManager.BATTERY_PLUGGED_WIRELESS; } /** * Determine whether the device is plugged in (USB, power). * * @return true if the device is plugged in wired (as opposed to wireless) */ public boolean isPluggedInWired() { return plugged == BatteryManager.BATTERY_PLUGGED_AC || plugged == BatteryManager.BATTERY_PLUGGED_USB; } /** * 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. * * @return true if the device is charged */ public boolean isCharged() { return status == BATTERY_STATUS_FULL || level >= 100; } /** * Whether battery is low and needs to be charged. * * @return true if battery is low */ public boolean isBatteryLow() { return level < LOW_BATTERY_THRESHOLD; } /** * Return current chargin speed is fast, slow or normal. * * @return the charing speed */ public final int getChargingSpeed(Context context) { final int slowThreshold = context.getResources().getInteger( R.integer.config_chargingSlowlyThreshold); final int fastThreshold = context.getResources().getInteger( R.integer.config_chargingFastThreshold); return maxChargingWattage <= 0 ? CHARGING_UNKNOWN : maxChargingWattage < slowThreshold ? CHARGING_SLOWLY : maxChargingWattage > fastThreshold ? CHARGING_FAST : CHARGING_REGULAR; } @Override public String toString() { return "BatteryStatus{status=" + status + ",level=" + level + ",plugged=" + plugged + ",health=" + health + ",maxChargingWattage=" + maxChargingWattage + "}"; } }
packages/SettingsLib/tests/robotests/src/com/android/settingslib/UtilsTest.java +38 −7 Original line number Diff line number Diff line Loading @@ -31,6 +31,7 @@ import android.content.Intent; import android.content.res.Resources; import android.location.LocationManager; import android.media.AudioManager; import android.os.BatteryManager; import android.os.SystemProperties; import android.os.UserHandle; import android.provider.Settings; Loading Loading @@ -147,7 +148,8 @@ public class UtilsTest { private static Map<String, Integer> map = new HashMap<>(); @Implementation public static boolean putIntForUser(ContentResolver cr, String name, int value, int userHandle) { public static boolean putIntForUser(ContentResolver cr, String name, int value, int userHandle) { map.put(name, value); return true; } Loading Loading @@ -312,4 +314,33 @@ public class UtilsTest { assertThat(Utils.getCombinedServiceState(mServiceState)).isEqualTo( ServiceState.STATE_OUT_OF_SERVICE); } @Test public void getBatteryStatus_statusIsFull_returnFullString() { final Intent intent = new Intent().putExtra(BatteryManager.EXTRA_LEVEL, 100); final Resources resources = mContext.getResources(); assertThat(Utils.getBatteryStatus(mContext, intent)).isEqualTo( resources.getString(R.string.battery_info_status_full)); } @Test public void getBatteryStatus_batteryLevelIs100_returnFullString() { final Intent intent = new Intent().putExtra(BatteryManager.EXTRA_STATUS, BatteryManager.BATTERY_STATUS_FULL); final Resources resources = mContext.getResources(); assertThat(Utils.getBatteryStatus(mContext, intent)).isEqualTo( resources.getString(R.string.battery_info_status_full)); } @Test public void getBatteryStatus_batteryLevel99_returnChargingString() { final Intent intent = new Intent().putExtra(BatteryManager.EXTRA_STATUS, BatteryManager.BATTERY_STATUS_CHARGING); final Resources resources = mContext.getResources(); assertThat(Utils.getBatteryStatus(mContext, intent)).isEqualTo( resources.getString(R.string.battery_info_status_charging)); } }