• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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      * Whether or not the device is charged. Note that some devices never return 100% for
113      * battery level, so this allows either battery level or status to determine if the
114      * battery is charged.
115      *
116      * @return true if the device is charged
117      */
isCharged()118     public boolean isCharged() {
119         return status == BATTERY_STATUS_FULL || level >= 100;
120     }
121 
122     /**
123      * Whether battery is low and needs to be charged.
124      *
125      * @return true if battery is low
126      */
isBatteryLow()127     public boolean isBatteryLow() {
128         return level < LOW_BATTERY_THRESHOLD;
129     }
130 
131     /**
132      * Whether battery is overheated.
133      *
134      * @return true if battery is overheated
135      */
isOverheated()136     public boolean isOverheated() {
137         return health == BATTERY_HEALTH_OVERHEAT;
138     }
139 
140     /**
141      * Return current chargin speed is fast, slow or normal.
142      *
143      * @return the charing speed
144      */
getChargingSpeed(Context context)145     public final int getChargingSpeed(Context context) {
146         final int slowThreshold = context.getResources().getInteger(
147                 R.integer.config_chargingSlowlyThreshold);
148         final int fastThreshold = context.getResources().getInteger(
149                 R.integer.config_chargingFastThreshold);
150         return maxChargingWattage <= 0 ? CHARGING_UNKNOWN :
151                 maxChargingWattage < slowThreshold ? CHARGING_SLOWLY :
152                         maxChargingWattage > fastThreshold ? CHARGING_FAST :
153                                 CHARGING_REGULAR;
154     }
155 
156     @Override
toString()157     public String toString() {
158         return "BatteryStatus{status=" + status + ",level=" + level + ",plugged=" + plugged
159                 + ",health=" + health + ",maxChargingWattage=" + maxChargingWattage + "}";
160     }
161 }
162