SettingsProvider defaults.xml 修改默认值数据及更新数据

SettingsProvider 修改默认值数据及更新数值

    • SettingsProvider数据存储
    • 强制更新数据

SettingsProvider数据存储

frameworks\base\packages\SettingsProvider中android6.0之后用不在用settings.db这个数据了,会存储在/data/system/users/0这个目录三个xml文件下:

  1. settings_global.xml:所有的偏好设置对系统的所有用户公开,第三方有读没有写的权限
  2. settings_system.xml:包含各种各样的用户偏好系统设置
  3. settings_secure.xml:安全性的用户偏好系统设置,第三方APP有读没有写的权限
    在SettingsProvider\src\com\android\providers\settings\SettingsProvider.java中创建
 final class SettingsRegistry {
        private static final String DROPBOX_TAG_USERLOG = "restricted_profile_ssaid";

        private static final String SETTINGS_FILE_GLOBAL = "settings_global.xml";
        private static final String SETTINGS_FILE_SYSTEM = "settings_system.xml";
        private static final String SETTINGS_FILE_SECURE = "settings_secure.xml";

        private final SparseArray<SettingsState> mSettingsStates = new SparseArray<>();

        private GenerationRegistry mGenerationRegistry;

private File getSettingsFile(int key) {                                   
    if (isGlobalSettingsKey(key)) {                                       
        final int userId = getUserIdFromKey(key);                         
        return new File(Environment.getUserSystemDirectory(userId),       
                SETTINGS_FILE_GLOBAL);                                    
    } else if (isSystemSettingsKey(key)) {                                
        final int userId = getUserIdFromKey(key);                         
        return new File(Environment.getUserSystemDirectory(userId),       
                SETTINGS_FILE_SYSTEM);                                    
    } else if (isSecureSettingsKey(key)) {                                
        final int userId = getUserIdFromKey(key);                         
        return new File(Environment.getUserSystemDirectory(userId),       
                SETTINGS_FILE_SECURE);                                    
    } else {                                                              
        throw new IllegalArgumentException("Invalid settings key:" + key);
    }                                                                     
}                                                                         
                                                                          ```

## 调试的时候获取或者设置相关的默认值
通过三种类型system, secure, global 可以分别获取和这种值
设置:
 - adb shell settings put system [key] [value]
 - adb shell settings put global [key] [value]
 - adb shell settings put secure default_input_method com.sohu.inputmethod.sogouoem/.SogouIME
 
获取:
 - adb shell settings get secure default_input_method
 - adb shell settings get global device_provisioned
 - adb shell settings get system [key]
 
也可以进入/data/system/users/0输入如下命令获取
 - cat settings_global.xml | grep device_provisioned
## 修改系统默认的一些设置配置值
当我们要修改一些默认值时一般修改SettingsProvider/res/values/defaults.xml可以修改通用属性
```java
<resources>
    <bool name="def_dim_screen">true</bool>自动关屏        开
    <integer name="def_screen_off_timeout">30000</integer> Android睡眠时间为1分钟
    //假设默认打开wifi可以将下面自动设置为true
    <bool name="def_wifi_on">false</bool> //wifi 关
    ...

在SettingsProvider\src\com\android\providers\settings\DatabaseHelper.java中

private void loadGlobalSettings(SQLiteDatabase db) {
        SQLiteStatement stmt = null;
        try {
            stmt = db.compileStatement("INSERT OR IGNORE INTO global(name,value)"
                    + " VALUES(?,?);");

            // --- Previously in 'system'
            loadBooleanSetting(stmt, Settings.Global.AIRPLANE_MODE_ON,
                    R.bool.def_airplane_mode_on);
            loadBooleanSetting(stmt, Settings.Global.THEATER_MODE_ON,
                    R.bool.def_theater_mode_on);
            //获取wifi默认值并设置
            loadBooleanSetting(stmt, Settings.Global.WIFI_ON,
                    R.bool.def_wifi_on);

强制更新数据

由于公司是定制产品需要ota强制默认打开wifi开关

  • 设置default.xml字段:"def_wifi_on"为true
  • 更改SettingsProvider\src\com\android\providers\settings\SettingsProvider.java中的字段SETTINGS_VERSION 加1
private final class UpgradeController {
			//ota升级强制更改默认值需要该字段自增一如
        private static final int SETTINGS_VERSION = 132;
		private final int mUserId;
  • 最后一定要更新currentVersion如果有字段可以添加相应的字段
if (currentVersion == 131) {
    // Initialize new multi-press timeout to default value
//	final SettingsState systemSecureSettings = getSecureSettingsLocked(userId);
//    final String oldValue = systemSecureSettings.getSettingLocked(
//    				Settings.Secure.MULTI_PRESS_TIMEOUT).getValue();
//    if (TextUtils.equals(null, oldValue)) {
//    	systemSecureSettings.insertSettingLocked(
//        	Settings.Secure.MULTI_PRESS_TIMEOUT,
//            String.valueOf(getContext().getResources().getInteger(
//            	R.integer.def_multi_press_timeout_millis)),
//           SettingsState.SYSTEM_PACKAGE_NAME);
    }
	currentVersion = 132;
}

不执行的会获取SettingsProvider\src\com\android\providers\settings\DatabaseHelper.java中的版本号DATABASE_VERSION导致相应的bug
SettingsProvider defaults.xml 修改默认值数据及更新数据_第1张图片
参考文献:
https://www.jianshu.com/p/3922f68b3560
https://www.jianshu.com/p/7815d573b12f

你可能感兴趣的:(android系统)