篇头
如今去过厂家的SOC,尤其是国产芯片,在设计其Android AOSP系统的时候,基本都会遵循谷歌的架构和规范。本文基于全志H713 Android 11平台,介绍如何给谷歌标准版的TvSettings添加default.xml默认值。
一、问题及需求
1.1 需求
给投影的HDMI CEC功能,配置几个默认值;
1.2 问题
- TvSettings的默认值在哪配置,如何配置?
1.3 CEC设置界面
- TvSettings->Device Preferences->Inputs
二、源码分析
2.1 查找思路
根据UI上的字符串,如"TV auto set language",顺藤摸瓜,在代码中找到set value 的地方,之后进一步找到配置文件位置。
2.2 TvSettings 数据设置
- 如下,找到了点击CEC界面开关后,写入值的地方
- 从 writeCecOption(Settings.Global.HDMI_CONTROL_AUTO_SET_LANGUAGE, mTvAutoSetLanguage.isChecked());等语句,可以得知值存储在Settings Global数据库中。
- A:\works\aw\android\packages\apps\TvSettings\Settings\src\com\android\tv\settings\system\InputsFragment.java
Plain
@Override
public boolean onPreferenceTreeClick(Preference preference) {
final String key = preference.getKey();
if (key == null) {
return super.onPreferenceTreeClick(preference);
}
switch (key) {
case KEY_HDMI_CONTROL:
writeCecOption(Settings.Global.HDMI_CONTROL_ENABLED, mHdmiControlPref.isChecked());
/// AW CODE: [feat] support hdmi cec with more switches
setPreferncesEnabled(mHdmiControlPref.isChecked());
/// AW:add end
return true;
case KEY_DEVICE_AUTO_OFF:
writeCecOption(Settings.Global.HDMI_CONTROL_AUTO_DEVICE_OFF_ENABLED,
mDeviceAutoOffPref.isChecked());
return true;
case KEY_TV_AUTO_ON:
writeCecOption(Settings.Global.HDMI_CONTROL_AUTO_WAKEUP_ENABLED,
mTvAutoOnPref.isChecked());
return true;
/// AW CODE: [feat] support hdmi cec with more switches
case KEY_TV_AUTO_SET_LANGUAGE:
writeCecOption(Settings.Global.HDMI_CONTROL_AUTO_SET_LANGUAGE,
mTvAutoSetLanguage.isChecked());
return true;
case KEY_TV_AUTO_INPUT_PASSTHROUGH:
writeCecOption(Settings.Global.HDMI_CONTROL_AUTO_INPUT_PASSTHROUGH,
mTvAutoInputPassThrough.isChecked());
return true;
/// AW:add end
/// AW CODE: [feat] add item for AwLiveTv recovery
case KEY_INPUT_RECOVERY:
writeCecOption(Settings.Global.INPUT_RECOVERY,
mInputRecovery.isChecked());
return true;
/// AW:add end
}
return super.onPreferenceTreeClick(preference);
}
2.3 配置文件
- 位置:A:\works\aw\android\frameworks\base\packages\SettingsProvider\res\values\defaults.xml
- 新的问题:从配置文件中,只找到1个HDMI CEC的开关的默认值,其余5个则找不到
Plain
<!-- Default for setting for Settings.Global.HDMI_CONTROL_AUTO_DEVICE_OFF_ENABLED -->
<bool name="def_hdmiControlAutoDeviceOff">false</bool>
2.4 Global 枚举定义的地方
- 位置 A:\works\aw\android\frameworks\base\core\java\android\provider\Settings.java
- 在这个文件里,定义了Global.xxxx枚举,例如Settings.Global.HDMI_CONTROL_AUTO_DEVICE_OFF_ENABLED
Plain
public static final String HDMI_CONTROL_AUTO_WAKEUP_ENABLED =
"hdmi_control_auto_wakeup_enabled";
/**
* Whether TV will also turn off other CEC devices when it goes to standby mode.
* (0 = false, 1 = true)
*
* @hide
*/
public static final String HDMI_CONTROL_AUTO_DEVICE_OFF_ENABLED =
"hdmi_control_auto_device_off_enabled";
/// AW CODE: [feat] HDMI-CEC: supprot device auto power on
/**
* Whether TV will also turn on other CEC devices when it goes to wakeup mode.
* (0 = false, 1 = true)
*
* @hide
*/
public static final String HDMI_CONTROL_AUTO_DEVICE_ON_ENABLED =
"hdmi_control_auto_device_on_enabled";
/// AW: add end.
2.5 加载默认值的地方
- 位置:android\frameworks\base\packages\SettingsProvider\src\com\android\providers\settings\SettingsProvider.java
- 通过关键字符串的查找,可以定位到默认值加载的地方,如下即使用字符串:def_hdmiControlAutoDeviceOff找到的
- 大致阅读此处代码,可知此处的作用就是在Settings.Gloabl数据表中插入新的字段,并赋予默认值,而默认值就是来自default.xml
Plain
if (currentVersion == 190) {
// Version 190: get HDMI auto device off from overlay
final SettingsState globalSettings = getGlobalSettingsLocked();
final Setting currentSetting = globalSettings.getSettingLocked(
Global.HDMI_CONTROL_AUTO_DEVICE_OFF_ENABLED);
if (currentSetting.isNull()) {
globalSettings.insertSettingLocked(
Global.HDMI_CONTROL_AUTO_DEVICE_OFF_ENABLED,
getContext().getResources().getBoolean(
R.bool.def_hdmiControlAutoDeviceOff) ? "1" : "0",
null, true, SettingsState.SYSTEM_PACKAGE_NAME);
}
currentVersion = 191;
}
// vXXX: Add new settings above this point.
2.6 查看实时数据
2.6.1 查看默认数据
- 烧录镜像后,不要操作UI开关,可查询到如下数据;
- 可得知,此时,数据库中只有一个HDMI的开关默认值,和楼上default.xml与SettingsProvider.java的逻辑一致
Plain
h713-tuna_p3:/ # settings list global | grep hdmi
hdmi_control_auto_device_off_enabled=1
h713-tuna_p3:/ # pwd
2.6.2 写入值
- 将UI里的HDMI CEC开关,全部关闭,再打开
- 查询DB的结果如下,可见已为我们添加了相关字段
Plain
h713-tuna_p3:/ # settings list global | grep hdmi
hdmi_control_auto_device_off_enabled=1
hdmi_control_auto_input_passthrough=1
hdmi_control_auto_set_language=1
hdmi_control_auto_wakeup_enabled=1
hdmi_control_enabled=1
h713-tuna_p3:/ # pwd
三、举例:添加两个HDMI CEC开关的默认值
3.1 添加默认值:defaults.xml
- A:\works\aw\android\frameworks\base\packages\SettingsProvider\res\values\defaults.xml
- 以下def_xxx都是自定义的
Plain
<!-- Default for setting for Settings.Global.HDMI_CONTROL_ENABLED -->
<bool name="def_hdmi_control_enabled">false</bool>
<!-- Default for setting for Settings.Global.HDMI_CONTROL_AUTO_SET_LANGUAGE -->
<bool name="def_hdmi_control_auto_set_language">false</bool>
3.2 加载默认值:SettingsProvider.java
- A:\works\aw\android\frameworks\base\packages\SettingsProvider\src\com\android\providers\settings\SettingsProvider.java
- 如下,找到if (currentVersion == 190),按格式,追加我们需要设定的两个def_xxxx 默认值
- R.bool.def_hdmi_control_enabled =>Global.HDMI_CONTROL_ENABLED
- R.bool.def_hdmi_control_auto_set_language=>Global.HDMI_CONTROL_AUTO_SET_LANGUAGE
Plain
if (currentVersion == 191) {
//
final SettingsState globalSettings = getGlobalSettingsLocked();
final Setting currentSetting = globalSettings.getSettingLocked(
Global.HDMI_CONTROL_ENABLED);
if (currentSetting.isNull()) {
globalSettings.insertSettingLocked(
Global.HDMI_CONTROL_ENABLED,
getContext().getResources().getBoolean(
R.bool.def_hdmi_control_enabled) ? "1" : "0",
null, true, SettingsState.SYSTEM_PACKAGE_NAME);
}
currentVersion = 192;
}
if (currentVersion == 192) {
//
final SettingsState globalSettings = getGlobalSettingsLocked();
final Setting currentSetting = globalSettings.getSettingLocked(
Global.HDMI_CONTROL_AUTO_SET_LANGUAGE);
if (currentSetting.isNull()) {
globalSettings.insertSettingLocked(
Global.HDMI_CONTROL_AUTO_SET_LANGUAGE,
getContext().getResources().getBoolean(
R.bool.def_hdmi_control_auto_set_language) ? "1" : "0",
null, true, SettingsState.SYSTEM_PACKAGE_NAME);
}
currentVersion = 193;
}
// vXXX: Add new settings above this point.
3.3 测试结果
- 烧录后,第一次开机,测试结果如下
Plain
C:\Users\szhou\Desktop>adb shell
h713-tuna_p3:/ # settings list global | grep hdmi
hdmi_control_auto_device_off_enabled=0
hdmi_control_auto_set_language=0
hdmi_control_enabled=0
h713-tuna_p3:/ #
3.4 运行效果图
- 效果如下图,CEC总开关【HDMI control】和子开关【TV auto set language】均被设置成了FALSE