涉及修改三处:
添加开宏条件:
MTK_TEMPERATURE_WARNING = yes
添加宏:
ifeq ($(strip $(MTK_TEMPERATURE_WARNING)), yes)
PRODUCT_PROPERTY_OVERRIDES += odm.agnfun.temperature_warning=true
endif
添加属性:
odm.mtk.temperature_warning u:object_r:vendor_mtk_default_prop:s0
并放在该目录下:vendor\mediatek\proprietary\packages\apps\SystemUI\src\com\mediatek\systemui\
BatteryTemperatureWarning.java代码里主要编写了在充电电池处于不同温度情况下各自执行什么操作,操作具体包括提示、停止充电、关机等,对应的这些操作也要分别定义,写出方法,以下是在温度处于60度时的关键代码:
private SystemUIDialog mCharging60Dialog;
private Runnable mCharging60DialogShow = new Runnable() {
@Override public void run() {
if (!mCharging60Dialog.isShowing()) {
mCharging60Dialog.show();
}
mHandler.postDelayed(mCharging60DialogShow, 30000);
}
};
public void updateCharging60Dialog(int level, boolean plugged) {
if (mCharging60Dialog == null) {
final SystemUIDialog d = new SystemUIDialog(mContext);
d.setIconAttribute(android.R.attr.alertDialogIcon);
d.setTitle(R.string.battery_warning_title);
d.setMessage(plugged ? R.string.battery_warning_level_5 : R.string.battery_warning_level_5_noplugged);
d.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Log.d(TAG, "updateCharging60Dialog click ok");
}
});
d.setShowForAllUsers(true);
d.setOnDismissListener(new OnDismissListener() {
@Override public void onDismiss(DialogInterface dialog) {
Log.d(TAG, "updateCharging60Dialog onDismiss");
}
});
mCharging60Dialog = d;
mCharging60Dialog.show();
}
if (plugged) {
mHandler.postDelayed(mCharging60DialogShow, 0);
}
else {
RingtoneManager rm = new RingtoneManager(mContext);
Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Ringtone mRingtone = RingtoneManager.getRingtone(mContext, uri);
mRingtone.play();
if (!mCharging60Dialog.isShowing()) {
mCharging60Dialog.show();
}
mHandler.postDelayed(mShutdownRunnable, 300000);
mHandler.removeCallbacks(mCharging60DialogShow);
}
if (level != 5) {
mHandler.removeCallbacks(mCharging60DialogShow);
}
}
其他温度下的代码编写参考上面关键代码
60度所处温度级别(level)为5,关于温度级别的判断逻辑以及执行提示的方法在PowerUI.java上编辑
PowerUI.java文件路径:
vendor\mediatek\proprietary\packages\apps\SystemUI\src\com\android\systemui\power\PowerUI.java
在SystemUI.java中主要是增加信息提示模块,
用于监测电池的温度并根据不同的温度等级执行不同的操作,包括弹出不同的温度警告对话框、停止或恢复充电等。
大体增添的内容如下:
import com.mediatek.systemui.BatteryTemperatureWarning;
该类定义了各种电量温度警告的提醒对话框,并根据不同的温度等级执行不同的操作。import android.os.SystemProperties;
该类用于读取系统属性值,比如用来判断是否需要打开电池温度检测。@VisibleForTesting int mBatteryTemperature = 35;
表示电池温度,初始值为35。private boolean isStopCharge = false;
用于标记是否停止充电,初始值为false。private BatteryTemperatureWarning mBatteryTemperatureWarning;
用于与电量温度警告对话框交互。mBatteryTemperatureWarning = new BatteryTemperatureWarning(mContext, mHandler);
public int batteryTemperatureJudgment(int temperature){...}
public void executeTemperatureWarning(int level, boolean plugged) {...}
final int oldBatteryTemperature = mBatteryTemperature;...
读取当前电池温度值,计算电池温度变化量, public void executeTemperatureWarning(int level, boolean plugged) {
if (0 == level) {//Normal temperature or temperature has not changed,then return;
return;
}
if (DEBUG){
Slog.d(TAG, "executeTemperatureWarning : level = " + level + ", plugged = " + plugged + ",isStopCharge=" + isStopCharge);
}
if (5 == level && !isStopCharge) {//Battery over temperature, pelese remove charger!
if (DEBUG){
Slog.d(TAG,"Battery temperature level = 5");
}
//isStopCharge = true;
//mPowerManager.stopCharge(isStopCharge);
mBatteryTemperatureWarning.updateCharging60Dialog(level,plugged);
} else if (6 == level) {//Battery over temperature, phone will shutdown !
...
} else if (4 == level) {//Resume charging
...
} else if (level <= 3 && plugged && !isStopCharge) {//Battery's temperature is too low, Stop charging!
...
} else if (level == 3 && !plugged && !isStopCharge) {//Battery's temperature is too low, Stop charging!
...
} else if (2 == level && !plugged) {//The pop-up indicates the temperature is too low
...
} else if (1 == level && !plugged) {//Battery over temperature, phone will shutdown in 15s!
...
}
编译、打包、刷机测试的完整步骤与昨日已经总结完成:第二天分析-二、编译、打包、刷机测试-过程
测试结果:模拟电池温度达60度时手机弹出温度过热提示框,达65度时手机进入关机,但并未出现温度过热提示框
将60度updateCharging60Dialog方法的代码同理移植到65度updateCharging65Dialog中,同时注意调用关机方法不要漏掉,
关键代码如下:
public void updateCharging65Dialog(int level, boolean plugged) {
if (mCharging65Dialog == null) {
final SystemUIDialog d = new SystemUIDialog(mContext);
d.setIconAttribute(android.R.attr.alertDialogIcon);
d.setTitle(R.string.battery_warning_title);
d.setMessage(plugged ? R.string.battery_warning_level_5:R.string.battery_warning_level_5_noplugged);
d.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Log.d(TAG,"updateCharging65Dialog click ok");
}
});
d.setShowForAllUsers(true);
d.setOnDismissListener(new OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
Log.d(TAG,"updateCharging65Dialog onDismiss");
}
});
mCharging65Dialog = d;
mCharging65Dialog.show();
}
mHandler.postDelayed(mShutdownRunnable, 0);
}
代码保存、编译、打包、刷机测试
测试结果:模拟电池温度达60度时手机弹出温度过热提示框,达65度时出现温度过热提示框手机进入关机。
目标需求已经完成。
再打包软件发送测试申请,进行最后的验证。。。