Android 10无电池设备,优化系统及其UI方案

Android 10无电池设备,优化系统及其UI方案

Android 10设备直接接入电源,而无需电池,但是Android系统一直显示有电池图标、以及老是提示电池电量为0,影响某些相对耗电的功能,涉及的定制主要把握如下:

1、是否支持电池用一个系统ro属性控制,不影响同一平台的其他有电池机型

2、无电池的情况下,默认100%,而不是0%,要彻底,无论是通过应用获取,还是adb命令拿到都是100%

3、Android 10 SystemUI状态栏不显示电池图标及其百分比选项

4、Android 10 Settings主页面默认不显示电池选项

一、设置控制全局的ro属性

在该项目的init.[项目名].rc中加入如下属性:
setprop ro.sys.support.nobattery true
该属性表征了不支持电池,则为true。其他机型不添加此属性即可

二、底层获取电池为100%

如何才是最彻底的方法,根据安卓设计框架:
Android—>framework—>Hal---->driver
该设备是没有电池,没有驱动,于是在driver的上一层默认改为100%,才比较彻底,于是在healthd做调整,是比较合理的。
通过 adb shell dumpsys battery,返回结果后有电池的各种信息,其中就包括level(百分比)
需要达到的效果是:
PS C:\Users\dengtl\Desktop> adb shell dumpsys battery
Current Battery Service state:
AC powered: true
USB powered: false
Wireless powered: false
Max charging current: 0
Max charging voltage: 0
Charge counter: 0
status: 3
health: 2
present: true
level: 100
scale: 100
voltage: 0
temperature: 424
technology:

电池的调用结构如下:
Android 10无电池设备,优化系统及其UI方案_第1张图片
修改如下:

diff --git a/idh.code/system/core/healthd/BatteryMonitor.cpp b/idh.code/system/core/healthd/BatteryMonitor.cpp
old mode 100644
new mode 100755
index 06c8176..ea21e8f
--- a/idh.code/system/core/healthd/BatteryMonitor.cpp
+++ b/idh.code/system/core/healthd/BatteryMonitor.cpp
@@ -73,6 +73,9 @@ static void initBatteryProperties(BatteryProperties* props) {
     props->batteryHealth = BATTERY_HEALTH_UNKNOWN;
     props->batteryPresent = false;
     props->batteryLevel = 0;
+    if (property_get_bool("ro.sys.support.nobattery", false)) {
+        props->batteryLevel = 100;
+    }
     props->batteryVoltage = 0;
     props->batteryTemperature = 0;
     props->batteryCurrent = 0;
@@ -213,6 +216,12 @@ bool BatteryMonitor::update(void) {
     props.batteryLevel = mBatteryFixedCapacity ?
         mBatteryFixedCapacity :
         getIntField(mHealthdConfig->batteryCapacityPath);
+
+    //add by dengtl , default 100% battery level
+    if (property_get_bool("ro.sys.support.nobattery", false)) {
+        props.batteryLevel = 100;
+    }
+
     props.batteryVoltage = getIntField(mHealthdConfig->batteryVoltagePath) / 1000;
 
     if (!mHealthdConfig->batteryCurrentNowPath.isEmpty())

此外,注意此处有selinux权限问题,需要在对应te文件下加入。

三、SystemUI状态栏不显示电池图标及其百分比选项

SystemUI只需要在添加此View的时候,返回即可:

diff --git a/idh.code/frameworks/base/packages/SystemUI/src/com/android/systemui/BatteryMeterView.java b/idh.code/frameworks/base/packages/SystemUI/src/com/android/systemui/BatteryMeterView.java
old mode 100644
new mode 100755
index 6bd0765..3b11c64
--- a/idh.code/frameworks/base/packages/SystemUI/src/com/android/systemui/BatteryMeterView.java
+++ b/idh.code/frameworks/base/packages/SystemUI/src/com/android/systemui/BatteryMeterView.java
@@ -67,6 +67,7 @@ import java.io.PrintWriter;
 import java.lang.annotation.Retention;
 import java.text.NumberFormat;
 import com.sprd.systemui.battery.BatteryMeterDrawable;
+import android.os.SystemProperties;
 
 public class BatteryMeterView extends LinearLayout implements
         BatteryStateChangeCallback, Tunable, DarkReceiver, ConfigurationListener {
@@ -173,7 +174,10 @@ public class BatteryMeterView extends LinearLayout implements
                 getResources().getDimensionPixelSize(R.dimen.status_bar_battery_icon_height));
         mlp.setMargins(0, 0, 0,
                 getResources().getDimensionPixelOffset(R.dimen.battery_margin_bottom));
-        addView(mBatteryIconView, mlp);
+
+        if(!SystemProperties.getBoolean("ro.sys.support.nobattery",false)){
+            addView(mBatteryIconView, mlp);
+        }
 
         updateShowPercent();
         mDualToneHandler = new DualToneHandler(context);

四、Settings主页面默认不显示电池选项

Settings主页面去除选项,找了很多方法都没有,尝试有如下:

  • 方法1:
    比如:去掉Settings一级菜单中的“Storage”选项
    直接在AndroidManifest.xml屏蔽掉即可
    vendor\mediatek\proprietary\packages\apps\MtkSettings\AndroidManifest.xml

这种方法比较暴力,但仍然不行;

  • 方法2:
    在Java文件中隐藏,无须修改AndroidManifest.xml

packages\apps\Settings\src\com\android\settings\SettingsActivity.java

final boolean showBattery = !SystemProperties.getBoolean("ro.sys.support.nobattery",false);
        somethingChanged = setTileEnabled(changedList, new ComponentName(packageName,
                        Settings.PowerUsageSummaryActivity.class.getName()),
                mBatteryPresent && showBattery , isAdmin) || somethingChanged;

该方法还是无效,无法隐藏;

  • 方法3:
vendor/mediatek/proprietary/packages/apps/SettingsLib/src/com/android/settingslib/drawer/TileUtils.java
  HashMap categoryMap = new HashMap<>();
        for (Tile tile : tiles) {
            DashboardCategory category = categoryMap.get(tile.category);
            if (category == null) {
                category = createCategory(context, tile.category, categoryDefinedInManifest);
                if (category == null) {
                    Log.w(LOG_TAG, "Couldn't find category " + tile.category);
                    continue;
                }
                categoryMap.put(category.key, category);
            }
            //add code
               if(tile.title.equals("应用和通知") || tile.title.equals("存储") || tile.title.equals("无障碍") ||
                   tile.title.equals("安全性和位置信息") || tile.title.equals("帐号")){
                    continue;
                }
                //end code
            
            category.addTile(tile);
        }

这个地方是控制是否为菜单列表获取的一些策略校验,控制一部分什么样的菜单才能显示
仍然不奏效,无果。

  • 方法4
    终极方法:
diff --git a/idh.code/packages/apps/Settings/src/com/android/settings/Utils.java b/idh.code/packages/apps/Settings/src/com/android/settings/Utils.java
index e0484b3..345b01f 100755
--- a/idh.code/packages/apps/Settings/src/com/android/settings/Utils.java
+++ b/idh.code/packages/apps/Settings/src/com/android/settings/Utils.java
@@ -285,7 +285,7 @@ public final class Utils extends com.android.settingslib.Utils {
     }
 
     public static boolean isBatteryPresent(Intent batteryChangedIntent) {
-        return batteryChangedIntent.getBooleanExtra(BatteryManager.EXTRA_PRESENT, true);
+        return isBatteryAvailable() && batteryChangedIntent.getBooleanExtra(BatteryManager.EXTRA_PRESENT, true);
     }
 
     public static String getBatteryPercentage(Intent batteryChangedIntent) {
@@ -871,10 +871,15 @@ public final class Utils extends com.android.settingslib.Utils {
     }
    
+    public static boolean isBatteryAvailable() {
+        return !SystemProperties.getBoolean("ro.sys.support.nobattery",false);
+    }
+

diff --git a/idh.code/packages/apps/Settings/src/com/android/settings/fuelgauge/TopLevelBatteryPreferenceController.java b/idh.code/packages/apps/Settings/src/com/android/settings/fuelgauge/TopLevelBatteryPreferenceController.java
old mode 100644
new mode 100755
index c4da670..1eaa4e7
--- a/idh.code/packages/apps/Settings/src/com/android/settings/fuelgauge/TopLevelBatteryPreferenceController.java
+++ b/idh.code/packages/apps/Settings/src/com/android/settings/fuelgauge/TopLevelBatteryPreferenceController.java
@@ -22,7 +22,9 @@ import android.text.BidiFormatter;
 import androidx.preference.Preference;
 import androidx.preference.PreferenceScreen;
 
+
 import com.android.settings.R;
+import com.android.settings.Utils;
 import com.android.settings.core.BasePreferenceController;
 import com.android.settingslib.core.lifecycle.LifecycleObserver;
 import com.android.settingslib.core.lifecycle.events.OnStart;
@@ -48,7 +50,7 @@ public class TopLevelBatteryPreferenceController extends BasePreferenceControlle
 
     @Override
     public int getAvailabilityStatus() {
      return mContext.getResources().getBoolean(R.bool.config_show_top_level_battery)
      return (mContext.getResources().getBoolean(R.bool.config_show_top_level_battery) && Utils.isBatteryAvailable())
         ? AVAILABLE_UNSEARCHABLE
         : UNSUPPORTED_ON_DEVICE;

此方法奏效了,TopSettings 的单元选项,都是通过getAvailabilityStatus这个方法来控制的。

你可能感兴趣的:(Android,System,android,java)