MTK-BatteryWarming初识

1. 源码位置

\vendor\mediatek\proprietary\packages\apps\BatteryWarning\

\vendor\mediatek\proprietary\frameworks\opt\batterywarning\

BatteryWarmning

2. 应用层的 BatteryWarning 核心逻辑

主要根据 type 进行提示框显示

注意 type = (int) (Math.log(type) / Math.log(2));

    @Override
    public void onReceive(Context context, Intent intent) {
        mContext = context;
        String action = intent.getAction();
        Log.d(TAG, "action = " + action);
        if (Intent.ACTION_BOOT_COMPLETED.equals(action)) {
            Log.d(TAG, action + " clear battery_warning_settings shared preference");
            SharedPreferences.Editor editor = getSharedPreferences().edit();
            editor.clear();
            editor.apply();
        } else if (ACTION_BATTERY_WARNING.equals(action)) {
            Log.d(TAG, action + " start activity according to shared preference");
            int type = intent.getIntExtra("type", -1);
            Log.d(TAG, "type = " + type);
            type = (int) (Math.log(type) / Math.log(2));
            if (type < 0 || type >= BatteryWarningActivity.sWarningTitle.length) {
                return;
            }
            boolean showDialogFlag = getSharedPreferences().getBoolean(
                    Integer.toString(type), true);
            Log.d(TAG, "type = " + type + "showDialogFlag = " + showDialogFlag);
            if (showDialogFlag) {
                Intent activityIntent = new Intent();
                activityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                        | Intent.FLAG_ACTIVITY_CLEAR_TOP
                        | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
                activityIntent.setClass(mContext, BatteryWarningActivity.class);
                activityIntent.putExtra(BatteryWarningActivity.KEY_TYPE, type);
                mContext.startActivity(activityIntent);
            }
        } else if (ACTION_THERMAL_WARNING.equals(action)) {
            int typeValue = intent.getIntExtra(ThermalWarningActivity.KEY_TYPE, -1);
            Log.d(TAG, "typeValue = " + typeValue);
            if (typeValue == 0 || typeValue == 1) {
                Intent thermalIntent = new Intent();
                thermalIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                        | Intent.FLAG_ACTIVITY_CLEAR_TOP
                        | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
                thermalIntent.setClass(mContext, ThermalWarningActivity.class);
                thermalIntent.putExtra(ThermalWarningActivity.KEY_TYPE, typeValue);
                mContext.startActivity(thermalIntent);
            }
        }
    }

3. 模拟数据

触发上述事件

package com.example.mtkbatterywarmning;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends Activity {

    protected static final String KEY_TYPE = "type";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    // msg_charger_over_voltage
    public void msg_charger_over_voltage(View view) {
        Intent mIntent = new Intent();
        mIntent.setAction("mediatek.intent.action.BATTERY_WARNING");
        mIntent.putExtra(KEY_TYPE, 1);
        sendBroadcast(mIntent);
    }

    // msg_battery_over_temperature
    public void msg_battery_over_temperature(View view) {
        Intent mIntent = new Intent();
        mIntent.setAction("mediatek.intent.action.BATTERY_WARNING");
        mIntent.putExtra(KEY_TYPE, 2);
        sendBroadcast(mIntent);
    }

    // msg_over_current_protection
    public void msg_over_current_protection(View view) {
        Intent mIntent = new Intent();
        mIntent.setAction("mediatek.intent.action.BATTERY_WARNING");
        mIntent.putExtra(KEY_TYPE, 4);
        sendBroadcast(mIntent);
    }

    // msg_battery_over_voltage
    public void msg_battery_over_voltage(View view) {
        Intent mIntent = new Intent();
        mIntent.setAction("mediatek.intent.action.BATTERY_WARNING");
        mIntent.putExtra(KEY_TYPE, 10);
        sendBroadcast(mIntent);
    }

    // msg_safety_timer_timeout
    public void msg_safety_timer_timeout(View view) {
        Intent mIntent = new Intent();
        mIntent.setAction("mediatek.intent.action.BATTERY_WARNING");
        mIntent.putExtra(KEY_TYPE, 17);
        sendBroadcast(mIntent);
    }

    // msg_battery_low_temperature
    public void msg_battery_low_temperature(View view) {
        Intent mIntent = new Intent();
        mIntent.setAction("mediatek.intent.action.BATTERY_WARNING");
        mIntent.putExtra(KEY_TYPE, 60);
        sendBroadcast(mIntent);
    }

    // thermal_clear_temperature
    public void thermal_clear_temperature(View view) {
        Intent mIntent = new Intent();
        mIntent.setAction("mediatek.intent.action.THERMAL_DIAG");
        mIntent.putExtra(KEY_TYPE, 0);
        sendBroadcast(mIntent);
    }

    // thermal_over_temperature
    public void thermal_over_temperature(View view) {
        Intent mIntent = new Intent();
        mIntent.setAction("mediatek.intent.action.THERMAL_DIAG");
        mIntent.putExtra(KEY_TYPE, 1);
        sendBroadcast(mIntent);
    }

}

4. 源码下载

https://github.com/sufadi/MtkBatteryWarming

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

法迪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值