Android 8.0 慢充和快充提示语的实现原理

 更新时间:2018年05月17日 10:09:43   作者:法迪  
这篇文章主要介绍了Android 8.0 慢充和快充提示语的实现原理,感兴趣的朋友跟随脚本之家小编一起看看吧

1. 慢充和快充提示语

\frameworks\base\packages\SystemUI\res-keyguard\values-zh-rCN

中文提示语

<string name="keyguard_plugged_in" msgid="89308975354638682">"正在充电"</string>
  <string name="keyguard_plugged_in_charging_fast" msgid="8869226755413795173">"正在快速充电"</string>
  <string name="keyguard_plugged_in_charging_slowly" msgid="6637043106038550407">"正在慢速充电"</string>

英文提示语

\frameworks\base\packages\SystemUI\res-keyguard\values

1.快充充电器充电-显示快速充电字符串

<!-- When the lock screen is showing and the phone plugged in, and the battery is not fully charged, and it's plugged into a fast charger, say that it's charging fast. --> 
<string name="keyguard_plugged_in_charging_fast">Charging rapidly</string>

2.普通充电电器-显示充电,该同7.0及其以前特性

<!-- When the lock screen is showing and the phone plugged in, and the battery is not fully charged, say that it's charging. --> 
<string name="keyguard_plugged_in">Charging</string>

3.电脑端或者笔记本端显示-缓慢充电

<!-- When the lock screen is showing and the phone plugged in, and the battery is not fully charged, and it's plugged into a slow charger, say that it's charging slowly. --> 
<string name="keyguard_plugged_in_charging_slowly">Charging slowly</string>

2. 原理

根据当前的最大电压和电流计算出电流速度,并进行分类为慢速充电,充电,快速充电

2.1 源代码中的原始数据

•public static final String EXTRA_MAX_CHARGING_CURRENT = “max_charging_current”;
•public static final String EXTRA_MAX_CHARGING_VOLTAGE = “max_charging_voltage”;

发送“电池广播”位置将最大电流和电压上发应用层,这里主要一些8.1以上新增的数据,7.0以前有这个数据但是framework层没有使用

frameworks/base/services/core/java/com/android/server/BatteryService.java
// 发送电池广播事件
private void sendIntentLocked() {
  // Pack up the values and broadcast them to everyone
  final Intent intent = new Intent(Intent.ACTION_BATTERY_CHANGED);
  intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY
      | Intent.FLAG_RECEIVER_REPLACE_PENDING);
.......
intent.putExtra(BatteryManager.EXTRA_MAX_CHARGING_CURRENT, mBatteryProps.maxChargingCurrent);
intent.putExtra(BatteryManager.EXTRA_MAX_CHARGING_VOLTAGE, mBatteryProps.maxChargingVoltage);

2.2 adb shell 查看linux的文件节点

•获取当前的电流

adb shell cat /sys/class/power_supply/battery/current_max
adb shell cat /sys/class/power_supply/battery/current_max
30000001

•获取当前的电压

adb shell cat /sys/class/power_supply/battery/voltage_max
adb shell cat /sys/class/power_supply/battery/voltage_max
50000001

•具体源码

system/core/healthd/BatteryMonitor.cpp
#define POWER_SUPPLY_SYSFS_PATH "/sys/class/" POWER_SUPPLY_SUBSYSTEM
path.appendFormat("%s/%s/voltage_max", POWER_SUPPLY_SYSFS_PATH,mChargerNames[i].string());

2.3 上层接收广播

frameworks/base/packages/SystemUI/src/com/android/keyguard/KeyguardUpdateMonitor.java

重点看 maxChargingMicroAmp 和 maxChargingMicroVolt 的算法规则

private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
    ....
    } else if (Intent.ACTION_BATTERY_CHANGED.equals(action)) {
        final int status = intent.getIntExtra(EXTRA_STATUS, BATTERY_STATUS_UNKNOWN);
        final int plugged = intent.getIntExtra(EXTRA_PLUGGED, 0);
        final int level = intent.getIntExtra(EXTRA_LEVEL, 0);
        final int health = intent.getIntExtra(EXTRA_HEALTH, BATTERY_HEALTH_UNKNOWN);
        final int maxChargingMicroAmp = intent.getIntExtra(EXTRA_MAX_CHARGING_CURRENT, -1);
        int maxChargingMicroVolt = intent.getIntExtra(EXTRA_MAX_CHARGING_VOLTAGE, -1);
        final int maxChargingMicroWatt;
        if (maxChargingMicroVolt <= 0) {
          maxChargingMicroVolt = DEFAULT_CHARGING_VOLTAGE_MICRO_VOLT;
        }
        if (maxChargingMicroAmp > 0) {
          // Calculating muW = muA * muV / (10^6 mu^2 / mu); splitting up the divisor
          // to maintain precision equally on both factors.
          maxChargingMicroWatt = (maxChargingMicroAmp / 1000)
              * (maxChargingMicroVolt / 1000);
        } else {
          maxChargingMicroWatt = -1;
        }
        final Message msg = mHandler.obtainMessage(
            MSG_BATTERY_UPDATE, new BatteryStatus(status, level, plugged, health,
                maxChargingMicroWatt));
        mHandler.sendMessage(msg);

2.4 显示字符串

frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/KeyguardIndicationController.java

事件接收

protected class BaseKeyguardCallback extends KeyguardUpdateMonitorCallback {
    public static final int HIDE_DELAY_MS = 5000;
    private int mLastSuccessiveErrorMessage = -1;
    @Override
    public void onRefreshBatteryInfo(KeyguardUpdateMonitor.BatteryStatus status) {
      boolean isChargingOrFull = status.status == BatteryManager.BATTERY_STATUS_CHARGING
          || status.status == BatteryManager.BATTERY_STATUS_FULL;
      boolean wasPluggedIn = mPowerPluggedIn;
      mPowerPluggedIn = status.isPluggedIn() && isChargingOrFull;
      mPowerCharged = status.isCharged();
      mChargingWattage = status.maxChargingWattage;
      mChargingSpeed = status.getChargingSpeed(mSlowThreshold, mFastThreshold);
      updateIndication();
      if (mDozing) {
        if (!wasPluggedIn && mPowerPluggedIn) {
          showTransientIndication(computePowerIndication());
          hideTransientIndicationDelayed(HIDE_DELAY_MS);
        } else if (wasPluggedIn && !mPowerPluggedIn) {
          hideTransientIndication();
        }
      }
    }
=====================================================================================================
  public static class BatteryStatus {
    public static final int CHARGING_UNKNOWN = -1;
    public static final int CHARGING_SLOWLY = 0;
    public static final int CHARGING_REGULAR = 1;
    public static final int CHARGING_FAST = 2;
    public final int status;
    public final int level;
    public final int plugged;
    public final int health;
    public final int maxChargingWattage;
    public BatteryStatus(int status, int level, int plugged, int health,
        int maxChargingWattage) {
      this.status = status;
      this.level = level;
      this.plugged = plugged;
      this.health = health;
      this.maxChargingWattage = maxChargingWattage;
    }
  public final int getChargingSpeed(int slowThreshold, int fastThreshold) {
    return maxChargingWattage <= 0 ? CHARGING_UNKNOWN :
        maxChargingWattage < slowThreshold ? CHARGING_SLOWLY :
        maxChargingWattage > fastThreshold ? CHARGING_FAST :
        CHARGING_REGULAR;
  }

显示字符串

frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/KeyguardIndicationController.java
    int chargingId;
    switch (mChargingSpeed) {
      case KeyguardUpdateMonitor.BatteryStatus.CHARGING_FAST:
        chargingId = hasChargingTime
            ? R.string.keyguard_indication_charging_time_fast
            : R.string.keyguard_plugged_in_charging_fast;
        break;
      case KeyguardUpdateMonitor.BatteryStatus.CHARGING_SLOWLY:
        chargingId = hasChargingTime
            ? R.string.keyguard_indication_charging_time_slowly
            : R.string.keyguard_plugged_in_charging_slowly;
        break;
      default:
        chargingId = hasChargingTime
            ? R.string.keyguard_indication_charging_time
            : R.string.keyguard_plugged_in;
        break;
    }

总结

以上所述是小编给大家介绍的Android 8.0 慢充和快充提示语的实现原理,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

相关文章

  • android图片文件的路径地址与Uri的相互转换方法

    android图片文件的路径地址与Uri的相互转换方法

    下面小编就为大家带来一篇android图片文件的路径地址与Uri的相互转换方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-04-04
  • Android Gradle开发指南详解

    Android Gradle开发指南详解

    这篇文章主要为大家详细介绍了Android Gradle开发指南的相关资料,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-10-10
  • Android ListView UI组件使用说明

    Android ListView UI组件使用说明

    这篇文章主要介绍了Android ListView UI组件使用说明,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-04-04
  • Flutter学习之实现自定义themes详解

    Flutter学习之实现自定义themes详解

    一般情况下我们在flutter中搭建的app基本上都是用的是MaterialApp这种设计模式,MaterialApp中为我们接下来使用的按钮,菜单等提供了统一的样式,那么这种样式能不能进行修改或者自定义呢?答案是肯定的,一起来看看吧
    2023-03-03
  • Android编程之界面跳动提示动画效果实现方法

    Android编程之界面跳动提示动画效果实现方法

    这篇文章主要介绍了Android编程之界面跳动提示动画效果实现方法,实例分析了Android动画效果的布局及功能相关实现技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-11-11
  • Android更改EditText下划线颜色样式的方法

    Android更改EditText下划线颜色样式的方法

    Android 里 EditText下划线有默认的颜色样式,如何修改其颜色,而且想单独定义某一个界面的颜色样式呢。下面这篇文章主要介绍了Android更改EditText下划线颜色样式的方法,需要的朋友可以参考下
    2017-01-01
  • viewpager实现自动循环轮播图

    viewpager实现自动循环轮播图

    这篇文章主要为大家详细介绍了viewpager实现自动循环轮播图,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-01-01
  • Android简单记录和恢复ListView滚动位置的方法

    Android简单记录和恢复ListView滚动位置的方法

    这篇文章主要介绍了Android简单记录和恢复ListView滚动位置的方法,涉及Android针对ListView位置属性的相关操作技巧,需要的朋友可以参考下
    2016-08-08
  • react native中的聊天气泡及timer封装成的发送验证码倒计时

    react native中的聊天气泡及timer封装成的发送验证码倒计时

    这篇文章主要介绍了react native中的聊天气泡及timer封装成的发送验证码倒计时的相关资料,需要的朋友可以参考下
    2017-08-08
  • Android实现编程修改手机静态IP的方法

    Android实现编程修改手机静态IP的方法

    这篇文章主要介绍了Android实现编程修改手机静态IP的方法,涉及Android编程实现对系统底层信息修改的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-10-10

最新评论