1 /* 2 * Copyright (C) 2019 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.settingslib.fuelgauge; 18 19 import static android.os.BatteryManager.BATTERY_HEALTH_UNKNOWN; 20 import static android.os.BatteryManager.BATTERY_STATUS_FULL; 21 import static android.os.BatteryManager.BATTERY_STATUS_UNKNOWN; 22 import static android.os.BatteryManager.EXTRA_HEALTH; 23 import static android.os.BatteryManager.EXTRA_LEVEL; 24 import static android.os.BatteryManager.EXTRA_MAX_CHARGING_CURRENT; 25 import static android.os.BatteryManager.EXTRA_MAX_CHARGING_VOLTAGE; 26 import static android.os.BatteryManager.EXTRA_PLUGGED; 27 import static android.os.BatteryManager.EXTRA_STATUS; 28 29 import android.content.Context; 30 import android.content.Intent; 31 import android.os.BatteryManager; 32 33 import com.android.settingslib.R; 34 35 /** 36 * Stores and computes some battery information. 37 */ 38 public class BatteryStatus { 39 private static final int LOW_BATTERY_THRESHOLD = 20; 40 private static final int DEFAULT_CHARGING_VOLTAGE_MICRO_VOLT = 5000000; 41 42 public static final int CHARGING_UNKNOWN = -1; 43 public static final int CHARGING_SLOWLY = 0; 44 public static final int CHARGING_REGULAR = 1; 45 public static final int CHARGING_FAST = 2; 46 47 public final int status; 48 public final int level; 49 public final int plugged; 50 public final int health; 51 public final int maxChargingWattage; 52 BatteryStatus(int status, int level, int plugged, int health, int maxChargingWattage)53 public BatteryStatus(int status, int level, int plugged, int health, 54 int maxChargingWattage) { 55 this.status = status; 56 this.level = level; 57 this.plugged = plugged; 58 this.health = health; 59 this.maxChargingWattage = maxChargingWattage; 60 } 61 BatteryStatus(Intent batteryChangedIntent)62 public BatteryStatus(Intent batteryChangedIntent) { 63 status = batteryChangedIntent.getIntExtra(EXTRA_STATUS, BATTERY_STATUS_UNKNOWN); 64 plugged = batteryChangedIntent.getIntExtra(EXTRA_PLUGGED, 0); 65 level = batteryChangedIntent.getIntExtra(EXTRA_LEVEL, 0); 66 health = batteryChangedIntent.getIntExtra(EXTRA_HEALTH, BATTERY_HEALTH_UNKNOWN); 67 68 final int maxChargingMicroAmp = batteryChangedIntent.getIntExtra(EXTRA_MAX_CHARGING_CURRENT, 69 -1); 70 int maxChargingMicroVolt = batteryChangedIntent.getIntExtra(EXTRA_MAX_CHARGING_VOLTAGE, -1); 71 72 if (maxChargingMicroVolt <= 0) { 73 maxChargingMicroVolt = DEFAULT_CHARGING_VOLTAGE_MICRO_VOLT; 74 } 75 if (maxChargingMicroAmp > 0) { 76 // Calculating muW = muA * muV / (10^6 mu^2 / mu); splitting up the divisor 77 // to maintain precision equally on both factors. 78 maxChargingWattage = (maxChargingMicroAmp / 1000) 79 * (maxChargingMicroVolt / 1000); 80 } else { 81 maxChargingWattage = -1; 82 } 83 } 84 85 /** 86 * Determine whether the device is plugged in (USB, power, or wireless). 87 * 88 * @return true if the device is plugged in. 89 */ isPluggedIn()90 public boolean isPluggedIn() { 91 return plugged == BatteryManager.BATTERY_PLUGGED_AC 92 || plugged == BatteryManager.BATTERY_PLUGGED_USB 93 || plugged == BatteryManager.BATTERY_PLUGGED_WIRELESS; 94 } 95 96 /** 97 * Determine whether the device is plugged in (USB, power). 98 * 99 * @return true if the device is plugged in wired (as opposed to wireless) 100 */ isPluggedInWired()101 public boolean isPluggedInWired() { 102 return plugged == BatteryManager.BATTERY_PLUGGED_AC 103 || plugged == BatteryManager.BATTERY_PLUGGED_USB; 104 } 105 106 /** 107 * Whether or not the device is charged. Note that some devices never return 100% for 108 * battery level, so this allows either battery level or status to determine if the 109 * battery is charged. 110 * 111 * @return true if the device is charged 112 */ isCharged()113 public boolean isCharged() { 114 return status == BATTERY_STATUS_FULL || level >= 100; 115 } 116 117 /** 118 * Whether battery is low and needs to be charged. 119 * 120 * @return true if battery is low 121 */ isBatteryLow()122 public boolean isBatteryLow() { 123 return level < LOW_BATTERY_THRESHOLD; 124 } 125 126 /** 127 * Return current chargin speed is fast, slow or normal. 128 * 129 * @return the charing speed 130 */ getChargingSpeed(Context context)131 public final int getChargingSpeed(Context context) { 132 final int slowThreshold = context.getResources().getInteger( 133 R.integer.config_chargingSlowlyThreshold); 134 final int fastThreshold = context.getResources().getInteger( 135 R.integer.config_chargingFastThreshold); 136 return maxChargingWattage <= 0 ? CHARGING_UNKNOWN : 137 maxChargingWattage < slowThreshold ? CHARGING_SLOWLY : 138 maxChargingWattage > fastThreshold ? CHARGING_FAST : 139 CHARGING_REGULAR; 140 } 141 142 @Override toString()143 public String toString() { 144 return "BatteryStatus{status=" + status + ",level=" + level + ",plugged=" + plugged 145 + ",health=" + health + ",maxChargingWattage=" + maxChargingWattage + "}"; 146 } 147 } 148