Android Q BatterySaver Owner/Guest在点击”Based on percentage“的区别
程序员文章站
2022-07-12 19:35:58
...
经过加Code debug发现Owner/Guest 下 Secure.LOW_POWER_WARNING_ACKNOWLEDGED 这个值会有区别.
Owner 和 Guest模式下第一次(刷完OS后,reboot不算)都会弹出“About Battery Saver“的警告对话框[Secure.LOW_POWER_WARNING_ACKNOWLEDGED 这个初始值应该是0],后面Owner不会再弹出,后来Guest/Owner为什么会有区别还要分析,应该是Secure.LOW_POWER_WARNING_ACKNOWLEDGED 这个值在Guest下发生了变化,下一步看Code里哪边有对这个值有修改.
点击 ”Based on percentage“这个 Button无论Owner或Guest都会对下面的Seekbar值进行初始化赋值(设置成5%),Guest 状态下Secure.getInt(context.getContentResolver(),
Secure.LOW_POWER_WARNING_ACKNOWLEDGED, 0) 的值会是0 ,会将triggerLevel 设置为0,继而threshold为0,Button下面的Seekbar就会隐藏(mSeekBarPreference.setVisible(false);)并发送广播弹出“About Battery Saver“警告对话框.
下面是相关的Code.
#################点击RadioButton触发部分####################### package/app/Settings/src/com/android/settings/fuelgauge/batterysaver/BatterySaverScheduleRadioButtonsController.java Log.i("TD","key 值是: "+key); if (!TextUtils.equals(key, KEY_NO_SCHEDULE) && BatterySaverUtils.maybeShowBatterySaverConfirmation( mContext, confirmationExtras)) { // reset this if we need to show the confirmation message Log.i("TD","------------------------->怀疑就是给triggerLevel赋值"); mode = PowerManager.POWER_SAVE_MODE_TRIGGER_PERCENTAGE; triggerLevel = 0; } // Trigger level is intentionally left alone when going between dynamic and percentage modes // so that a users percentage based schedule is preserved when they toggle between the two. Settings.Global.putInt(resolver, Global.AUTOMATIC_POWER_SAVE_MODE, mode); if (mode != PowerManager.POWER_SAVE_MODE_TRIGGER_DYNAMIC) { Log.i("TD","------------------------->怀疑就是这修改了triggerLevel值"); Settings.Global.putInt(resolver, Global.LOW_POWER_MODE_TRIGGER_LEVEL, triggerLevel); }else{ Log.i("TD","mode就是PowerManager.POWER_SAVE_MODE_TRIGGER_DYNAMIC"); } mSeekBarController.updateSeekBar();
#################发送广播部分####################### frameworks/base/packages/SettingsLib/src/com/android/settingslib/fuelgauge/BatterySaverUtils.java public static boolean maybeShowBatterySaverConfirmation(Context context, Bundle extras) { if (Secure.getInt(context.getContentResolver(), Secure.LOW_POWER_WARNING_ACKNOWLEDGED, 0) != 0) { Log.i("TD","------->结束没有发广播"); return false; // Already shown. } Log.i("TD","------->发射广播"); context.sendBroadcast( getSystemUiBroadcast(ACTION_SHOW_START_SAVER_CONFIRMATION, extras)); return true; }
#################更新SeekBar框部分####################### pacakage/app/Settings/src/com/android/settings/fuelgauge/batterysaver/BatterySaverScheduleSeekBarController.java public void updateSeekBar() { ....... if (mode == PowerManager.POWER_SAVE_MODE_TRIGGER_PERCENTAGE) { final int threshold = Settings.Global.getInt(resolver, Global.LOW_POWER_MODE_TRIGGER_LEVEL, 0); if (threshold <= 0) { mSeekBarPreference.setVisible(false); } else { Log.i("TD","#2--------------->mSeekBarPreference setVisible false-=-=-=->threshold: "+threshold); final int currentSeekbarValue = Math.max(threshold / 5, MIN_SEEKBAR_VALUE); ....... }
#################接受广播-弹出对话框部分####################### /frameworks/base/packages/SystemUI/src/com/android/systemui/power/PowerNotificationWarnings.java @Override public void onReceive(Context context, Intent intent) { .... else if (action.equals(ACTION_SHOW_START_SAVER_CONFIRMATION)) { Log.i("TD","------------------->ACTION_SHOW_START_SAVER_CONFIRMATION"); dismissLowBatteryNotification(); showStartSaverConfirmation(intent.getExtras()); private void showStartSaverConfirmation(Bundle extras) { if (mSaverConfirmation != null) { Log.i("TD","----------------------->mSaverConfirmation isn't null"); }else{ Log.i("TD","----------------------->mSaverConfirmation is null"); } if (mSaverConfirmation != null) return; final SystemUIDialog d = new SystemUIDialog(mContext); final boolean confirmOnly = extras.getBoolean(BatterySaverUtils.EXTRA_CONFIRM_TEXT_ONLY); final int batterySaverTriggerMode = extras.getInt(BatterySaverUtils.EXTRA_POWER_SAVE_MODE_TRIGGER, PowerManager.POWER_SAVE_MODE_TRIGGER_PERCENTAGE); final int batterySaverTriggerLevel = extras.getInt(BatterySaverUtils.EXTRA_POWER_SAVE_MODE_TRIGGER_LEVEL, 0); Log.i("TD","---->batterySaverTriggerMode: "+batterySaverTriggerMode); Log.i("TD","---->batterySaverTriggerLevel: "+batterySaverTriggerLevel); d.setMessage(getBatterySaverDescription()); .......
写道
通过adb shell 指令修改low_power_warning_acknowledged值(即LOW_POWER_WARNING_ACKNOWLEDGED)结合BatterySaverUtils.java&&PowerNotificationWarnings.java里的所加Log分析如下:
[Step0]MC93C:/ $ settings get secure low_power_warning_acknowledged null [Step1]MC93C:/ $ settings get secure low_power_warning_acknowledged 1 [Step2]MC93C:/ $ settings put secure low_power_warning_acknowledged 0 [Step3]MC93C:/ $ settings get secure low_power_warning_acknowledged 0 [Step4]MC93C:/ $ settings get secure low_power_warning_acknowledged null [Step5]MC93C:/ $ settings get secure low_power_warning_acknowledged null [Step6]MC93C:/ $ settings get secure low_power_warning_acknowledged null [Step7]MC93C:/ $ settings get secure low_power_warning_acknowledged 1 [Step8]MC93C:/ $ settings put secure low_power_warning_acknowledged 0 [Step9]MC93C:/ $ settings get secure low_power_warning_acknowledged 0
写道
Step0是刷机或做factoryreset后进入"Battery Saver"->"Set a schedule"得到的结果
Step1是在Owner下点击一次"Based on percentage"后得到的结果
LOG1:--->Secure.LOW_POWER_WARNING_ACKNOWLEDGED value is: 0
LOG2------->LOW_POWER_WARNING_ACKNOWLEDGED value is: 0
LOG3------->LOW_POWER_WARNING_ACKNOWLEDGED value is: 1
Step2手动将low_power_warning_acknowledged值修改成0
Step3查看low_power_warning_acknowledged是否修改成功
Step4将状态切到Guest,进入"Battery Saver"->"Set a schedule"得到的结果
Step5是在Guest下点击一次"Based on percentage"后得到的结果
LOG1:--->Secure.LOW_POWER_WARNING_ACKNOWLEDGED value is: 0[此后这边的值会一直为0,具体原因下面会解释]
LOG2------->LOW_POWER_WARNING_ACKNOWLEDGED value is: 0 [因为Step2有做过手动修改,如果没有的话,这边的值会是1]
LOG3------->LOW_POWER_WARNING_ACKNOWLEDGED value is: 1
Step6是在Guest下再一次点击"Based on percentage"后得到的结果
LOG1:--->Secure.LOW_POWER_WARNING_ACKNOWLEDGED value is: 0
LOG2------->LOW_POWER_WARNING_ACKNOWLEDGED value is: 1
LOG3------->LOW_POWER_WARNING_ACKNOWLEDGED value is: 1
Step7是切到Owner状态下没有做任何操作得到的值
Step8,Step9是重复过程(Step2,Step3)
如果切到Guest状态不做如何操作就切回Owner状态,那么用指令获取low_power_warning_acknowledged值,仍然为0.[Step0~Step4&&Step7]
说明PartI是读取本状态下(Owner/Guest)的数据库里的内容,PartII读写的是公共区域数据库里的内容,在Guest状态下没有权限访问公共区域的值(LOW_POWER_WARNING_ACKNOWLEDGED),
导致PartI部分的判断条件(LOW_POWER_WARNING_ACKNOWLEDGED==0)所以就会一直发送广播去弹出对话框及隐藏下面的Seekbar.
Step1是在Owner下点击一次"Based on percentage"后得到的结果
LOG1:--->Secure.LOW_POWER_WARNING_ACKNOWLEDGED value is: 0
LOG2------->LOW_POWER_WARNING_ACKNOWLEDGED value is: 0
LOG3------->LOW_POWER_WARNING_ACKNOWLEDGED value is: 1
Step2手动将low_power_warning_acknowledged值修改成0
Step3查看low_power_warning_acknowledged是否修改成功
Step4将状态切到Guest,进入"Battery Saver"->"Set a schedule"得到的结果
Step5是在Guest下点击一次"Based on percentage"后得到的结果
LOG1:--->Secure.LOW_POWER_WARNING_ACKNOWLEDGED value is: 0[此后这边的值会一直为0,具体原因下面会解释]
LOG2------->LOW_POWER_WARNING_ACKNOWLEDGED value is: 0 [因为Step2有做过手动修改,如果没有的话,这边的值会是1]
LOG3------->LOW_POWER_WARNING_ACKNOWLEDGED value is: 1
Step6是在Guest下再一次点击"Based on percentage"后得到的结果
LOG1:--->Secure.LOW_POWER_WARNING_ACKNOWLEDGED value is: 0
LOG2------->LOW_POWER_WARNING_ACKNOWLEDGED value is: 1
LOG3------->LOW_POWER_WARNING_ACKNOWLEDGED value is: 1
Step7是切到Owner状态下没有做任何操作得到的值
Step8,Step9是重复过程(Step2,Step3)
如果切到Guest状态不做如何操作就切回Owner状态,那么用指令获取low_power_warning_acknowledged值,仍然为0.[Step0~Step4&&Step7]
说明PartI是读取本状态下(Owner/Guest)的数据库里的内容,PartII读写的是公共区域数据库里的内容,在Guest状态下没有权限访问公共区域的值(LOW_POWER_WARNING_ACKNOWLEDGED),
导致PartI部分的判断条件(LOW_POWER_WARNING_ACKNOWLEDGED==0)所以就会一直发送广播去弹出对话框及隐藏下面的Seekbar.
PartI.#################发送广播部分####################### frameworks/base/packages/SettingsLib/src/com/android/settingslib/fuelgauge/BatterySaverUtils.java public static boolean maybeShowBatterySaverConfirmation(Context context, Bundle extras) { Log.i("TD","LOG1:--->Secure.LOW_POWER_WARNING_ACKNOWLEDGED value is: "+Secure.getInt(context.getContentResolver(), Secure.LOW_POWER_WARNING_ACKNOWLEDGED, 0)); if (Secure.getInt(context.getContentResolver(), Secure.LOW_POWER_WARNING_ACKNOWLEDGED, 0) != 0) { Log.i("TD","------->结束没有发广播"); return false; // Already shown. } Log.i("TD","------->发射广播"); context.sendBroadcast( getSystemUiBroadcast(ACTION_SHOW_START_SAVER_CONFIRMATION, extras)); return true; } PartII./frameworks/base/packages/SystemUI/src/com/android/systemui/power/PowerNotificationWarnings.java if (confirmOnly) { ...... Log.i("TD","LOG2------->LOW_POWER_WARNING_ACKNOWLEDGED value is: "+Secure.getInt(mContext.getContentResolver(), Secure.LOW_POWER_WARNING_ACKNOWLEDGED, 0)); Settings.Global.putInt( resolver, Global.AUTOMATIC_POWER_SAVE_MODE, batterySaverTriggerMode); Settings.Global.putInt( resolver, Global.LOW_POWER_MODE_TRIGGER_LEVEL, batterySaverTriggerLevel); boolean jg = Secure.putInt( resolver, Secure.LOW_POWER_WARNING_ACKNOWLEDGED, 1); Log.i("TD","------>putInt 结果为 : "+jg); //确实每次都能写LOW_POWER_WARNING_ACKNOWLEDGED成功 Log.i("TD","LOG3------->LOW_POWER_WARNING_ACKNOWLEDGED value is: "+Secure.getInt(mContext.getContentResolver(), Secure.LOW_POWER_WARNING_ACKNOWLEDGED, 0)); }); }
写道
在网络上有找到一个与此相关的文章,请参考https://www.guidingtech.com/guest-mode-android/
其中关于数据访问部分说明:"PROTECT PERSONAL DATA
Similar to user profiles, the primary users' installed apps, personal data (such as photos, audio, video, contacts), app data (call history, web history, chats, and messages), settings and customizations will be unavailable in the guest mode. Even notifications from the admin’s account will be blocked on the guest profile. However, you can access all those things when you switch to an admin profile.
The guest mode gives a feeling of a fresh device where you are offered only the pre-installed apps."
其中关于数据访问部分说明:"PROTECT PERSONAL DATA
Similar to user profiles, the primary users' installed apps, personal data (such as photos, audio, video, contacts), app data (call history, web history, chats, and messages), settings and customizations will be unavailable in the guest mode. Even notifications from the admin’s account will be blocked on the guest profile. However, you can access all those things when you switch to an admin profile.
The guest mode gives a feeling of a fresh device where you are offered only the pre-installed apps."