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_OVERHEAT; 20 import static android.os.BatteryManager.BATTERY_HEALTH_UNKNOWN; 21 import static android.os.BatteryManager.BATTERY_STATUS_FULL; 22 import static android.os.BatteryManager.BATTERY_STATUS_UNKNOWN; 23 import static android.os.BatteryManager.EXTRA_HEALTH; 24 import static android.os.BatteryManager.EXTRA_LEVEL; 25 import static android.os.BatteryManager.EXTRA_MAX_CHARGING_CURRENT; 26 import static android.os.BatteryManager.EXTRA_MAX_CHARGING_VOLTAGE; 27 import static android.os.BatteryManager.EXTRA_PLUGGED; 28 import static android.os.BatteryManager.EXTRA_PRESENT; 29 import static android.os.BatteryManager.EXTRA_STATUS; 30 31 import android.content.Context; 32 import android.content.Intent; 33 import android.os.BatteryManager; 34 35 import com.android.settingslib.R; 36 37 /** 38 * Stores and computes some battery information. 39 */ 40 public class BatteryStatus { 41 private static final int LOW_BATTERY_THRESHOLD = 20; 42 private static final int DEFAULT_CHARGING_VOLTAGE_MICRO_VOLT = 5000000; 43 44 public static final int CHARGING_UNKNOWN = -1; 45 public static final int CHARGING_SLOWLY = 0; 46 public static final int CHARGING_REGULAR = 1; 47 public static final int CHARGING_FAST = 2; 48 49 public final int status; 50 public final int level; 51 public final int plugged; 52 public final int health; 53 public final int maxChargingWattage; 54 public final boolean present; 55 BatteryStatus(int status, int level, int plugged, int health, int maxChargingWattage, boolean present)56 public BatteryStatus(int status, int level, int plugged, int health, 57 int maxChargingWattage, boolean present) { 58 this.status = status; 59 this.level = level; 60 this.plugged = plugged; 61 this.health = health; 62 this.maxChargingWattage = maxChargingWattage; 63 this.present = present; 64 } 65 BatteryStatus(Intent batteryChangedIntent)66 public BatteryStatus(Intent batteryChangedIntent) { 67 status = batteryChangedIntent.getIntExtra(EXTRA_STATUS, BATTERY_STATUS_UNKNOWN); 68 plugged = batteryChangedIntent.getIntExtra(EXTRA_PLUGGED, 0); 69 level = batteryChangedIntent.getIntExtra(EXTRA_LEVEL, 0); 70 health = batteryChangedIntent.getIntExtra(EXTRA_HEALTH, BATTERY_HEALTH_UNKNOWN); 71 present = batteryChangedIntent.getBooleanExtra(EXTRA_PRESENT, true); 72 73 final int maxChargingMicroAmp = batteryChangedIntent.getIntExtra(EXTRA_MAX_CHARGING_CURRENT, 74 -1); 75 int maxChargingMicroVolt = batteryChangedIntent.getIntExtra(EXTRA_MAX_CHARGING_VOLTAGE, -1); 76 77 if (maxChargingMicroVolt <= 0) { 78 maxChargingMicroVolt = DEFAULT_CHARGING_VOLTAGE_MICRO_VOLT; 79 } 80 if (maxChargingMicroAmp > 0) { 81 // Calculating muW = muA * muV / (10^6 mu^2 / mu); splitting up the divisor 82 // to maintain precision equally on both factors. 83 maxChargingWattage = (maxChargingMicroAmp / 1000) 84 * (maxChargingMicroVolt / 1000); 85 } else { 86 maxChargingWattage = -1; 87 } 88 } 89 90 /** 91 * Determine whether the device is plugged in (USB, power, wireless or dock). 92 * 93 * @return true if the device is plugged in. 94 */ isPluggedIn()95 public boolean isPluggedIn() { 96 return plugged == BatteryManager.BATTERY_PLUGGED_AC 97 || plugged == BatteryManager.BATTERY_PLUGGED_USB 98 || plugged == BatteryManager.BATTERY_PLUGGED_WIRELESS 99 || plugged == BatteryManager.BATTERY_PLUGGED_DOCK; 100 } 101 102 /** 103 * Determine whether the device is plugged in (USB, power). 104 * 105 * @return true if the device is plugged in wired (as opposed to wireless) 106 */ isPluggedInWired()107 public boolean isPluggedInWired() { 108 return plugged == BatteryManager.BATTERY_PLUGGED_AC 109 || plugged == BatteryManager.BATTERY_PLUGGED_USB; 110 } 111 112 /** 113 * Determine whether the device is plugged in wireless. 114 * 115 * @return true if the device is plugged in wireless 116 */ isPluggedInWireless()117 public boolean isPluggedInWireless() { 118 return plugged == BatteryManager.BATTERY_PLUGGED_WIRELESS; 119 } 120 121 /** 122 * Determine whether the device is plugged in dock. 123 * 124 * @return true if the device is plugged in dock 125 */ isPluggedInDock()126 public boolean isPluggedInDock() { 127 return plugged == BatteryManager.BATTERY_PLUGGED_DOCK; 128 } 129 130 /** 131 * Whether or not the device is charged. Note that some devices never return 100% for 132 * battery level, so this allows either battery level or status to determine if the 133 * battery is charged. 134 * 135 * @return true if the device is charged 136 */ isCharged()137 public boolean isCharged() { 138 return isCharged(status, level); 139 } 140 141 /** 142 * Whether battery is low and needs to be charged. 143 * 144 * @return true if battery is low 145 */ isBatteryLow()146 public boolean isBatteryLow() { 147 return level < LOW_BATTERY_THRESHOLD; 148 } 149 150 /** 151 * Whether battery is overheated. 152 * 153 * @return true if battery is overheated 154 */ isOverheated()155 public boolean isOverheated() { 156 return health == BATTERY_HEALTH_OVERHEAT; 157 } 158 159 /** 160 * Return current chargin speed is fast, slow or normal. 161 * 162 * @return the charing speed 163 */ getChargingSpeed(Context context)164 public final int getChargingSpeed(Context context) { 165 final int slowThreshold = context.getResources().getInteger( 166 R.integer.config_chargingSlowlyThreshold); 167 final int fastThreshold = context.getResources().getInteger( 168 R.integer.config_chargingFastThreshold); 169 return maxChargingWattage <= 0 ? CHARGING_UNKNOWN : 170 maxChargingWattage < slowThreshold ? CHARGING_SLOWLY : 171 maxChargingWattage > fastThreshold ? CHARGING_FAST : 172 CHARGING_REGULAR; 173 } 174 175 @Override toString()176 public String toString() { 177 return "BatteryStatus{status=" + status + ",level=" + level + ",plugged=" + plugged 178 + ",health=" + health + ",maxChargingWattage=" + maxChargingWattage + "}"; 179 } 180 181 /** 182 * Whether or not the device is charged. Note that some devices never return 100% for 183 * battery level, so this allows either battery level or status to determine if the 184 * battery is charged. 185 * 186 * @param batteryChangedIntent ACTION_BATTERY_CHANGED intent 187 * @return true if the device is charged 188 */ isCharged(Intent batteryChangedIntent)189 public static boolean isCharged(Intent batteryChangedIntent) { 190 int status = batteryChangedIntent.getIntExtra(EXTRA_STATUS, BATTERY_STATUS_UNKNOWN); 191 int level = batteryChangedIntent.getIntExtra(EXTRA_LEVEL, 0); 192 return isCharged(status, level); 193 } 194 195 /** 196 * Whether or not the device is charged. Note that some devices never return 100% for 197 * battery level, so this allows either battery level or status to determine if the 198 * battery is charged. 199 * 200 * @param status values for "status" field in the ACTION_BATTERY_CHANGED Intent 201 * @param level values from 0 to 100 202 * @return true if the device is charged 203 */ isCharged(int status, int level)204 public static boolean isCharged(int status, int level) { 205 return status == BATTERY_STATUS_FULL || level >= 100; 206 } 207 } 208