整日播种码子,很少抽出时间来总结,今儿来了劲儿,特将自己在android framework及hal层添加battery信息节点的处理的工作做以下总结,回顾的同时,加深理解:
软件环境:android4.4.3
目的:添加上层对battery两个节点的处理,两节点分别为battery可用时长和battery是否对外充电(我们的设备可以作为master对其他USB设备充电)。
印象当中,期初battery的hal层节点的封装处理是在hardware目录下进行的,而此番试图去那里修改hal层却遇到了困难,hardware下找不到封装的文件。冥想之余,怀疑google难道把power的hal去掉了?如果去掉,那取代这部分功能的代码在哪里实现?几经周折,发现了端倪,原来在system目录下,有一个healthd目录,该目录下封装了framework层获取相关battery节点的路径。sys下的节点是驱动程序中创建的。
首先呈现healthd这部分的修改:
--- a/healthd/BatteryMonitor.cpp
+++ b/healthd/BatteryMonitor.cpp
@@ -196,6 +196,8 @@ bool BatteryMonitor::update(void) {
props.batteryChargeCounter = getIntField(mHealthdConfig->batteryChargeCounterPath);
props.batteryTemperature = getIntField(mHealthdConfig->batteryTemperaturePath);
+ props.batteryTimeToEmpty = getIntField(mHealthdConfig->batteryTimeToEmptyPath);
+ props.batteryExternalCharging = getIntField(mHealthdConfig->batteryExternalChargingPath);
const int SIZE = 128;
char buf[SIZE];
@@ -389,6 +391,22 @@ void BatteryMonitor::init(struct healthd_config *hc, bool nosvcmgr) {
mHealthdConfig->batteryTechnologyPath = path;
}
+ if (mHealthdConfig->batteryTimeToEmptyPath.isEmpty()) { //添加电池剩余电量可用时长
+ path.clear();
+ path.appendFormat("%s/%s/time_to_empty_now",
+ POWER_SUPPLY_SYSFS_PATH, name);
+ if (access(path, R_OK) == 0)
+ mHealthdConfig->batteryTimeToEmptyPath = path;
+ }
+
+ if (mHealthdConfig->batteryExternalChargingPath.isEmpty()) { //添加电池是否对外充电的状态
+ path.clear();
+ path.appendFormat("%s/%s/usb_otg",
+ POWER_SUPPLY_SYSFS_PATH, name);
+ if (access(path, R_OK) == 0)
+ mHealthdConfig->batteryExternalChargingPath = path;
+ }
+
break;
case ANDROID_POWER_SUPPLY_TYPE_UNKNOWN:
@@ -414,6 +432,10 @@ void BatteryMonitor::init(struct healthd_config *hc, bool nosvcmgr) {
KLOG_WARNING(LOG_TAG, "BatteryTemperaturePath not found\n");
if (mHealthdConfig->batteryTechnologyPath.isEmpty())
KLOG_WARNING(LOG_TAG, "BatteryTechnologyPath not found\n");
+ if (mHealthdConfig->batteryTimeToEmptyPath.isEmpty())
+ KLOG_WARNING(LOG_TAG, "BatteryTimeToEmptyPath not found\n");
+ if (mHealthdConfig->batteryExternalChargingPath.isEmpty())
+ KLOG_WARNING(LOG_TAG, "BatteryExternalChargingPath not found\n");
+ if (mHealthdConfig->batteryTimeToEmptyPath.isEmpty()) {
+ path.clear();
+ path.appendFormat("%s/%s/time_to_empty_now",
+ POWER_SUPPLY_SYSFS_PATH, name);
+ if (access(path, R_OK) == 0)
+ mHealthdConfig->batteryTimeToEmptyPath = path;
+ }
+
+ if (mHealthdConfig->batteryExternalChargingPath.isEmpty()) {
+ path.clear();
+ path.appendFormat("%s/%s/usb_otg",
+ POWER_SUPPLY_SYSFS_PATH, name);
+ if (access(path, R_OK) == 0)
+ mHealthdConfig->batteryExternalChargingPath = path;
+ }
+
break;
case ANDROID_POWER_SUPPLY_TYPE_UNKNOWN:
@@ -414,6 +432,10 @@ void BatteryMonitor::init(struct healthd_config *hc, bool nosvcmgr) {
KLOG_WARNING(LOG_TAG, "BatteryTemperaturePath not found\n");
if (mHealthdConfig->batteryTechnologyPath.isEmpty())
KLOG_WARNING(LOG_TAG, "BatteryTechnologyPath not found\n");
+ if (mHealthdConfig->batteryTimeToEmptyPath.isEmpty())
+ KLOG_WARNING(LOG_TAG, "BatteryTimeToEmptyPath not found\n");
+ if (mHealthdConfig->batteryExternalChargingPath.isEmpty())
+ KLOG_WARNING(LOG_TAG, "BatteryExternalChargingPath not found\n");
--- a/healthd/healthd.cpp
+++ b/healthd/healthd.cpp
@@ -50,6 +50,8 @@ static struct healthd_config healthd_config = {
.batteryTechnologyPath = String8(String8::kEmptyString),
.batteryCurrentNowPath = String8(String8::kEmptyString),
.batteryChargeCounterPath = String8(String8::kEmptyString),
+ .batteryTimeToEmptyPath = String8(String8::kEmptyString),
+ .batteryExternalChargingPath = String8(String8::kEmptyString),
};
#define POWER_SUPPLY_SUBSYSTEM "power_supply"
--- a/healthd/healthd.h
+++ b/healthd/healthd.h
@@ -62,6 +62,8 @@ struct healthd_config {
android::String8 batteryTechnologyPath;
android::String8 batteryCurrentNowPath;
android::String8 batteryChargeCounterPath;
+ android::String8 batteryTimeToEmptyPath;
+ android::String8 batteryExternalChargingPath;
};
Framework部分修改:
1,修改api接口
--- a/api/current.txt
+++ b/api/current.txt
@@ -17531,6 +17531,8 @@ package android.os {
field public static final java.lang.String EXTRA_STATUS = "status";
field public static final java.lang.String EXTRA_TECHNOLOGY = "technology";
field public static final java.lang.String EXTRA_TEMPERATURE = "temperature";
+ field public static final java.lang.String EXTRA_TIMETOEMPTY = "timetoempty";
+ field public static final java.lang.String EXTRA_USBOTG = "usbotg";
field public static final java.lang.String EXTRA_VOLTAGE = "voltage";
}
2,batterymanager添加接口
--- a/core/java/android/os/BatteryManager.java
+++ b/core/java/android/os/BatteryManager.java
@@ -87,6 +87,18 @@ public class BatteryManager {
/**
* Extra for {@link android.content.Intent#ACTION_BATTERY_CHANGED}:
+ * integer containing the current battery time to empty from now on.
+ */
+ public static final String EXTRA_TIMETOEMPTY = "timetoempty";
+
+ /**
+ * Extra for {@link android.content.Intent#ACTION_BATTERY_CHANGED}:
+ * integer containing the current battery external type.
+ */
+ public static final String EXTRA_USBOTG = "usbotg";
+
+ /**
+ * Extra for {@link android.content.Intent#ACTION_BATTERY_CHANGED}:
* Int value set to nonzero if an unsupported charger is attached
* to the device.
* {@hide}
3,属性文件添加parcel读写接口
--- a/core/java/android/os/BatteryProperties.java
+++ b/core/java/android/os/BatteryProperties.java
@@ -34,6 +34,8 @@ public class BatteryProperties implements Parcelable {
public int batteryChargeCounter;
public int batteryTemperature;
public String batteryTechnology;
+ public int batteryTimeToEmpty;
+ public int batteryExternalCharging;
/*
* Parcel read/write code must be kept in sync with
@@ -53,6 +55,8 @@ public class BatteryProperties implements Parcelable {
batteryChargeCounter = p.readInt();
batteryTemperature = p.readInt();
batteryTechnology = p.readString();
+ batteryTimeToEmpty = p.readInt();
+ batteryExternalCharging = p.readInt();
}
public void writeToParcel(Parcel p, int flags) {
@@ -68,6 +72,8 @@ public class BatteryProperties implements Parcelable {
p.writeInt(batteryChargeCounter);
p.writeInt(batteryTemperature);
p.writeString(batteryTechnology);
+ p.writeInt(batteryTimeToEmpty);
+ p.writeInt(batteryExternalCharging);
}
4,添加battery状态获取的接口,实现机制是将battery的几个状态值通过移位操作以一个整型的形式呈现出来,该文件内部保存有两个battery的状态值,一个是最近一次读取的状态,一个是上一次保存的状态,只要两个状态字不一致,则判断状态发生变化,则battery状态按移位操作可以从前文提到的整型中分别提取出,从而更新相应的属性字段。
--- a/core/java/android/os/BatteryStats.java
+++ b/core/java/android/os/BatteryStats.java
@@ -462,6 +462,8 @@ public abstract class BatteryStats implements Parcelable {
public char batteryTemperature;
public char batteryVoltage;
+ public char batteryTimeToEmpty;
+ public char batteryExternalCharging;
// Constants from SCREEN_BRIGHTNESS_*
public static final int STATE_BRIGHTNESS_MASK = 0x0000000f;
@@ -526,6 +528,9 @@ public abstract class BatteryStats implements Parcelable {
bat = (((int)batteryTemperature)&0xffff)
| ((((int)batteryVoltage)<<16)&0xffff0000);
dest.writeInt(bat);
+ bat = (((int)batteryTimeToEmpty)&0xffff)
+ | ((((int)batteryExternalCharging)<<16)&0xffff0000);
+ dest.writeInt(bat);
dest.writeInt(states);
}
@@ -539,6 +544,9 @@ public abstract class BatteryStats implements Parcelable {
bat = src.readInt();
batteryTemperature = (char)(bat&0xffff);
batteryVoltage = (char)((bat>>16)&0xffff);
+ bat = src.readInt();
+ batteryTimeToEmpty = (char)(bat&0xffff);
+ batteryExternalCharging = (char)((bat>>16)&0xffff);
states = src.readInt();
}
@@ -550,6 +558,8 @@ public abstract class BatteryStats implements Parcelable {
// Part of initial delta int holding the command code.
static final int DELTA_CMD_MASK = 0x3;
static final int DELTA_CMD_SHIFT = 18;
+ // Flag in delta int: a new battery time to empty and extype int follows.
+ static final int DELTA_BATTERY_TIME_FLAG = 1<<19;
// Flag in delta int: a new battery level int follows.
static final int DELTA_BATTERY_LEVEL_FLAG = 1<<20;
// Flag in delta int: a new full state and battery status int follows.
@@ -566,6 +576,7 @@ public abstract class BatteryStats implements Parcelable {
final long deltaTime = time - last.time;
final int lastBatteryLevelInt = last.buildBatteryLevelInt();
final int lastStateInt = last.buildStateInt();
+ final int lastBatteryTimeInt = last.buildBatteryTimeInt();
int deltaTimeToken;
if (deltaTime < 0 || deltaTime > Integer.MAX_VALUE) {
@@ -583,6 +594,11 @@ public abstract class BatteryStats implements Parcelable {
if (batteryLevelIntChanged) {
firstToken |= DELTA_BATTERY_LEVEL_FLAG;
}
+ final int batteryTimeInt = buildBatteryTimeInt();
+ final boolean batteryTimeIntChanged = batteryTimeInt != lastBatteryTimeInt;
+ if (batteryTimeIntChanged) {
+ firstToken |= DELTA_BATTERY_TIME_FLAG;
+ }
final int stateInt = buildStateInt();
final boolean stateIntChanged = stateInt != lastStateInt;
if (stateIntChanged) {
@@ -609,6 +625,13 @@ public abstract class BatteryStats implements Parcelable {
+ " batteryTemp=" + (int)batteryTemperature
+ " batteryVolt=" + (int)batteryVoltage);
}
+ if (batteryTimeIntChanged) {
+ dest.writeInt(batteryTimeInt);
+ if (DEBUG) Slog.i(TAG, "WRITE DELTA: batteryToken=0x"
+ + Integer.toHexString(batteryTimeInt)
+ + " batteryTime=" + (int)batteryTimeToEmpty
+ + " batteryExternalCharging=" + (int)batteryExternalCharging);
+ }
if (stateIntChanged) {
dest.writeInt(stateInt);
if (DEBUG) Slog.i(TAG, "WRITE DELTA: stateToken=0x"
@@ -626,6 +649,11 @@ public abstract class BatteryStats implements Parcelable {
| (((int)batteryVoltage)&0x00003fff);
}
+ private int buildBatteryTimeInt() {
+ return ((((int)batteryTimeToEmpty)<<2)&0xfffffffc)
+ | (((int)batteryExternalCharging)&0x00000003);
+ }
+
private int buildStateInt() {
return ((((int)batteryStatus)<<28)&0xf0000000)
| ((((int)batteryHealth)<<24)&0x0f000000)
@@ -667,6 +695,16 @@ public abstract class BatteryStats implements Parcelable {
+ " batteryTemp=" + (int)batteryTemperature
+ " batteryVolt=" + (int)batteryVoltage);
}
+
+ if ((firstToken&DELTA_BATTERY_TIME_FLAG) != 0) {
+ int batteryTimeInt = src.readInt();
+ batteryTimeToEmpty = (char)(batteryTimeInt>>2&0x3fffffff);
+ batteryExternalCharging = (char)(batteryTimeInt&0x00000003);
+ if (DEBUG) Slog.i(TAG, "READ DELTA: batteryToken=0x"
+ + Integer.toHexString(batteryTimeInt)
+ + " batteryTime=" + (int)batteryTimeToEmpty
+ + " batteryExternalCharging=" + (int)batteryExternalCharging);
+ }
if ((firstToken&DELTA_STATE_FLAG) != 0) {
int stateInt = src.readInt();
@@ -694,6 +732,8 @@ public abstract class BatteryStats implements Parcelable {
batteryPlugType = 0;
batteryTemperature = 0;
batteryVoltage = 0;
+ batteryTimeToEmpty = 0;
+ batteryExternalCharging = 0;
states = 0;
}
@@ -706,6 +746,8 @@ public abstract class BatteryStats implements Parcelable {
batteryPlugType = o.batteryPlugType;
batteryTemperature = o.batteryTemperature;
batteryVoltage = o.batteryVoltage;
+ batteryTimeToEmpty = o.batteryTimeToEmpty;
+ batteryExternalCharging = o.batteryExternalCharging;
states = o.states;
}
@@ -718,6 +760,8 @@ public abstract class BatteryStats implements Parcelable {
batteryPlugType = o.batteryPlugType;
batteryTemperature = o.batteryTemperature;
batteryVoltage = o.batteryVoltage;
+ batteryTimeToEmpty = o.batteryTimeToEmpty;
+ batteryExternalCharging = o.batteryExternalCharging;
states = o.states;
}
@@ -728,6 +772,8 @@ public abstract class BatteryStats implements Parcelable {
&& batteryPlugType == o.batteryPlugType
&& batteryTemperature == o.batteryTemperature
&& batteryVoltage == o.batteryVoltage
+ && batteryTimeToEmpty == o.batteryTimeToEmpty
+ && batteryExternalCharging == o.batteryExternalCharging
&& states == o.states;
}
}
@@ -2185,6 +2231,8 @@ public abstract class BatteryStats implements Parcelable {
int oldPlug = -1;
int oldTemp = -1;
int oldVolt = -1;
+ int oldTime = -1;
+ int oldExt = -1;
public void printNextItem(PrintWriter pw, HistoryItem rec, long now) {
pw.print(" ");
@@ -2289,6 +2337,16 @@ public abstract class BatteryStats implements Parcelable {
pw.print(" volt=");
pw.print(oldVolt);
}
+ if (oldTime != rec.batteryTimeToEmpty) {
+ oldTime = rec.batteryTimeToEmpty;
+ pw.print(" timetoempty=");
+ pw.print(oldTime);
+ }
+ if (oldExt != rec.batteryExternalCharging) {
+ oldExt = rec.batteryExternalCharging;
+ pw.print(" usbotgtype=");
+ pw.print(oldExt);
+ }
printBitDescriptions(pw, oldState, rec.states,
HISTORY_STATE_DESCRIPTIONS);
pw.println();
@@ -2317,6 +2375,10 @@ public abstract class BatteryStats implements Parcelable {
pw.print((int)rec.batteryTemperature);
pw.print(",");
pw.print((int)rec.batteryVoltage);
+ pw.print(",");
+ pw.print((int)rec.batteryTimeToEmpty);
+ pw.print(",");
+ pw.print((int)rec.batteryExternalCharging);
}
}
}
5,由于状态字段增加,则状态设置接口需要更新。
--- a/core/java/com/android/internal/app/IBatteryStats.aidl
+++ b/core/java/com/android/internal/app/IBatteryStats.aidl
@@ -74,7 +74,7 @@ interface IBatteryStats {
void noteWifiMulticastDisabledFromSource(in WorkSource ws);
void noteNetworkInterfaceType(String iface, int type);
void noteNetworkStatsEnabled();
- void setBatteryState(int status, int health, int plugType, int level, int temp, int volt);
+ void setBatteryState(int status, int health, int plugType, int level, int temp, int volt, int time, int extype);
6,实现上一步提到的接口修改,并保存相关的状态参数到历史记录。
--- a/core/java/com/android/internal/os/BatteryStatsImpl.java
+++ b/core/java/com/android/internal/os/BatteryStatsImpl.java
@@ -4811,7 +4811,7 @@ public final class BatteryStatsImpl extends BatteryStats {
private static final int BATTERY_PLUGGED_NONE = 0;
public void setBatteryState(int status, int health, int plugType, int level,
- int temp, int volt) {
+ int temp, int volt, int time, int extype) {
synchronized(this) {
boolean onBattery = plugType == BATTERY_PLUGGED_NONE;
int oldStatus = mHistoryCur.batteryStatus;
@@ -4841,6 +4841,8 @@ public final class BatteryStatsImpl extends BatteryStats {
mHistoryCur.batteryPlugType = (byte)plugType;
mHistoryCur.batteryTemperature = (char)temp;
mHistoryCur.batteryVoltage = (char)volt;
+ mHistoryCur.batteryTimeToEmpty = (char)time;
+ mHistoryCur.batteryExternalCharging = (char)extype;
setOnBatteryLocked(onBattery, oldStatus, level);
} else {
boolean changed = false;
7,同步SystemUI先关数据接口。
--- a/packages/SystemUI/src/com/android/systemui/BatteryMeterView.java
+++ b/packages/SystemUI/src/com/android/systemui/BatteryMeterView.java
@@ -82,6 +82,8 @@ public class BatteryMeterView extends View implements DemoMode {
String technology;
int voltage;
int temperature;
+ int time;
+ int extype;
boolean testmode = false;
@Override
@@ -103,6 +105,8 @@ public class BatteryMeterView extends View implements DemoMode {
technology = intent.getStringExtra(BatteryManager.EXTRA_TECHNOLOGY);
voltage = intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE, 0);
temperature = intent.getIntExtra(BatteryManager.EXTRA_TEMPERATURE, 0);
+ time = intent.getIntExtra(BatteryManager.EXTRA_TIMETOEMPTY, 0);
+ extype = intent.getIntExtra(BatteryManager.EXTRA_USBOTG, 0);
setContentDescription(
context.getString(R.string.accessibility_battery_level, level));
8,修改batteryservice中对状态为判断的部分,以及广播字段中添加这两个字段,方便应用程序监听到电池状态变化后提取相应属性字段。
--- a/services/java/com/android/server/BatteryService.java
+++ b/services/java/com/android/server/BatteryService.java
@@ -113,6 +113,8 @@ public final class BatteryService extends Binder {
private int mLastBatteryLevel;
private int mLastBatteryVoltage;
private int mLastBatteryTemperature;
+ private int mLastBatteryTimeToEmpty;
+ private int mLastBatteryExternalCharging;
private boolean mLastBatteryLevelCritical;
private int mInvalidCharger;
@@ -317,6 +319,8 @@ public final class BatteryService extends Binder {
+ ", batteryCurrentNow=" + mBatteryProps.batteryCurrentNow
+ ", batteryChargeCounter=" + mBatteryProps.batteryChargeCounter
+ ", batteryTemperature=" + mBatteryProps.batteryTemperature
+ + ", batteryTimeToEmpty=" + mBatteryProps.batteryTimeToEmpty
+ + ", batteryExternalCharging=" + mBatteryProps.batteryExternalCharging
+ ", mBatteryLevelCritical=" + mBatteryLevelCritical
+ ", mPlugType=" + mPlugType);
}
@@ -325,7 +329,8 @@ public final class BatteryService extends Binder {
try {
mBatteryStats.setBatteryState(mBatteryProps.batteryStatus, mBatteryProps.batteryHealth,
mPlugType, mBatteryProps.batteryLevel, mBatteryProps.batteryTemperature,
- mBatteryProps.batteryVoltage);
+ mBatteryProps.batteryVoltage, mBatteryProps.batteryTimeToEmpty,
+ mBatteryProps.batteryExternalCharging);
} catch (RemoteException e) {
// Should never happen.
}
@@ -339,6 +344,8 @@ public final class BatteryService extends Binder {
mBatteryProps.batteryLevel != mLastBatteryLevel ||
mPlugType != mLastPlugType ||
mBatteryProps.batteryVoltage != mLastBatteryVoltage ||
+ mBatteryProps.batteryTimeToEmpty != mLastBatteryTimeToEmpty ||
+ mBatteryProps.batteryExternalCharging != mLastBatteryExternalCharging ||
mBatteryProps.batteryTemperature != mLastBatteryTemperature ||
mInvalidCharger != mLastInvalidCharger) {
@@ -374,7 +381,9 @@ public final class BatteryService extends Binder {
// Don't do this just from voltage or temperature changes, that is
// too noisy.
EventLog.writeEvent(EventLogTags.BATTERY_LEVEL,
- mBatteryProps.batteryLevel, mBatteryProps.batteryVoltage, mBatteryProps.batteryTemperature);
+ mBatteryProps.batteryLevel, mBatteryProps.batteryVoltage,
+ mBatteryProps.batteryTemperature, mBatteryProps.batteryTimeToEmpty,
+ mBatteryProps.batteryExternalCharging);
}
if (mBatteryLevelCritical && !mLastBatteryLevelCritical &&
mPlugType == BATTERY_PLUGGED_NONE) {
@@ -461,6 +470,8 @@ public final class BatteryService extends Binder {
mLastPlugType = mPlugType;
mLastBatteryVoltage = mBatteryProps.batteryVoltage;
mLastBatteryTemperature = mBatteryProps.batteryTemperature;
+ mLastBatteryTimeToEmpty = mBatteryProps.batteryTimeToEmpty;
+ mLastBatteryExternalCharging = mBatteryProps.batteryExternalCharging;
mLastBatteryLevelCritical = mBatteryLevelCritical;
mLastInvalidCharger = mInvalidCharger;
}
@@ -483,6 +494,8 @@ public final class BatteryService extends Binder {
intent.putExtra(BatteryManager.EXTRA_PLUGGED, mPlugType);
intent.putExtra(BatteryManager.EXTRA_VOLTAGE, mBatteryProps.batteryVoltage);
intent.putExtra(BatteryManager.EXTRA_TEMPERATURE, mBatteryProps.batteryTemperature);
+ intent.putExtra(BatteryManager.EXTRA_USBOTG, mBatteryProps.batteryExternalCharging);
+ intent.putExtra(BatteryManager.EXTRA_TIMETOEMPTY, mBatteryProps.batteryTimeToEmpty);
intent.putExtra(BatteryManager.EXTRA_TECHNOLOGY, mBatteryProps.batteryTechnology);
intent.putExtra(BatteryManager.EXTRA_INVALID_CHARGER, mInvalidCharger);
@@ -492,6 +505,8 @@ public final class BatteryService extends Binder {
", health:" + mBatteryProps.batteryHealth + ", present:" + mBatteryProps.batteryPresent +
", voltage: " + mBatteryProps.batteryVoltage +
", temperature: " + mBatteryProps.batteryTemperature +
+ ", timetoempty: " + mBatteryProps.batteryTimeToEmpty +
+ ", extcharge: " + mBatteryProps.batteryExternalCharging +
", technology: " + mBatteryProps.batteryTechnology +
", AC powered:" + mBatteryProps.chargerAcOnline + ", USB powered:" + mBatteryProps.chargerUsbOnline +
", Wireless powered:" + mBatteryProps.chargerWirelessOnline +
@@ -626,6 +641,8 @@ public final class BatteryService extends Binder {
}
pw.println(" temperature: " + mBatteryProps.batteryTemperature);
+ pw.println(" time: " + mBatteryProps.batteryTimeToEmpty);
+ pw.println(" extcharge: " + mBatteryProps.batteryExternalCharging);
pw.println(" technology: " + mBatteryProps.batteryTechnology);
} else if (args.length == 3 && "set".equals(args[0])) {
String key = args[1];
9,更新接口。
--- a/services/java/com/android/server/am/BatteryStatsService.java
+++ b/services/java/com/android/server/am/BatteryStatsService.java
@@ -468,9 +468,9 @@ public final class BatteryStatsService extends IBatteryStats.Stub {
}
public void setBatteryState(int status, int health, int plugType, int level,
- int temp, int volt) {
+ int temp, int volt, int time, int extype) {
enforceCallingPermission();
- mStats.setBatteryState(status, health, plugType, level, temp, volt);
+ mStats.setBatteryState(status, health, plugType, level, temp, volt, time, extype);
}
10,BatteryProperties结构体添加两字段。
--- a/include/batteryservice/BatteryService.h
+++ b/include/batteryservice/BatteryService.h
@@ -56,6 +56,8 @@ struct BatteryProperties {
int batteryChargeCounter;
int batteryTemperature;
String8 batteryTechnology;
+ int batteryTimeToEmpty;
+ int batteryExternalCharging;
11,添加BatteryProperties这两个字段的parcel包写入和读取。
--- a/services/batteryservice/BatteryProperties.cpp
+++ b/services/batteryservice/BatteryProperties.cpp
@@ -42,6 +42,8 @@ status_t BatteryProperties::readFromParcel(Parcel* p) {
batteryChargeCounter = p->readInt32();
batteryTemperature = p->readInt32();
batteryTechnology = String8((p->readString16()).string());
+ batteryTimeToEmpty = p->readInt32();
+ batteryExternalCharging = p->readInt32();
return OK;
}
@@ -58,6 +60,8 @@ status_t BatteryProperties::writeToParcel(Parcel* p) const {
p->writeInt32(batteryChargeCounter);
p->writeInt32(batteryTemperature);
p->writeString16(String16(batteryTechnology));
+ p->writeInt32(batteryTimeToEmpty);
+ p->writeInt32(batteryExternalCharging);
return OK;
}
至此,添加完成。应用程序可以通过注册监听BATTERY_STATUS_CHANGED广播来获取电量剩余可用时长以及电池是否对外充电等状态。