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, or wireless).
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     }
100 
101     /**
102      * Determine whether the device is plugged in (USB, power).
103      *
104      * @return true if the device is plugged in wired (as opposed to wireless)
105      */
isPluggedInWired()106     public boolean isPluggedInWired() {
107         return plugged == BatteryManager.BATTERY_PLUGGED_AC
108                 || plugged == BatteryManager.BATTERY_PLUGGED_USB;
109     }
110 
111     /**
112      * Determine whether the device is plugged in wireless.
113      *
114      * @return true if the device is plugged in wireless
115      */
isPluggedInWireless()116     public boolean isPluggedInWireless() {
117         return plugged == BatteryManager.BATTERY_PLUGGED_WIRELESS;
118     }
119 
120     /**
121      * Whether or not the device is charged. Note that some devices never return 100% for
122      * battery level, so this allows either battery level or status to determine if the
123      * battery is charged.
124      *
125      * @return true if the device is charged
126      */
isCharged()127     public boolean isCharged() {
128         return status == BATTERY_STATUS_FULL || level >= 100;
129     }
130 
131     /**
132      * Whether battery is low and needs to be charged.
133      *
134      * @return true if battery is low
135      */
isBatteryLow()136     public boolean isBatteryLow() {
137         return level < LOW_BATTERY_THRESHOLD;
138     }
139 
140     /**
141      * Whether battery is overheated.
142      *
143      * @return true if battery is overheated
144      */
isOverheated()145     public boolean isOverheated() {
146         return health == BATTERY_HEALTH_OVERHEAT;
147     }
148 
149     /**
150      * Return current chargin speed is fast, slow or normal.
151      *
152      * @return the charing speed
153      */
getChargingSpeed(Context context)154     public final int getChargingSpeed(Context context) {
155         final int slowThreshold = context.getResources().getInteger(
156                 R.integer.config_chargingSlowlyThreshold);
157         final int fastThreshold = context.getResources().getInteger(
158                 R.integer.config_chargingFastThreshold);
159         return maxChargingWattage <= 0 ? CHARGING_UNKNOWN :
160                 maxChargingWattage < slowThreshold ? CHARGING_SLOWLY :
161                         maxChargingWattage > fastThreshold ? CHARGING_FAST :
162                                 CHARGING_REGULAR;
163     }
164 
165     @Override
toString()166     public String toString() {
167         return "BatteryStatus{status=" + status + ",level=" + level + ",plugged=" + plugged
168                 + ",health=" + health + ",maxChargingWattage=" + maxChargingWattage + "}";
169     }
170 }
171