The Settings Provider system settings data table saves the preference settings in the system, and the application can access the corresponding preference settings through the Settings API, mainly three types of settings: Settings.Global, Settings.Secure, and Settings.System.
Settings.Global : Global system settings containing preferences that always apply the same way to all defined users. Applications can read these, but not write to them; like the "Security" settings, these apply to preferences that the user must explicitly modify through the system UI or a dedicated API for these values. Commonly used setting items include Bluetooth switch status, adb switch status, etc.
Settings.Secure : Secure system settings, containing system preferences that apps can read but not write to. These preferences are for preferences that the user must explicitly modify through the system UI or a dedicated API for these values, rather than directly by the application. Commonly used setting items include accessibility, VPN status, etc.
Settings.System : System settings, including miscellaneous system preferences. This table contains simple name/value pairs. There are some convenience methods for accessing individual setting entries. Commonly used setting items include ringtone, brightness, time format, etc.
The following describes the process of customizing a Settings.Global key. The method of customizing Settings.Secure and Settings.System is similar.
Modifications in Settings
Source code path:
frameworks/base/core/java/android/provider/Settings.java
Add the constant value of the custom Key in the internal class, which can be of common types such as String, int, and boolean Settings
.Global
public static final class Global extends NameValueTable {
/**
* @hide
*/
public static final String CUSTOM_KEY = "your_custom_key";
}
MOVED_TO_GLOBAL
Add the corresponding custom Key to the list :
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
private static final HashSet<String> MOVED_TO_GLOBAL;
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
private static final HashSet<String> MOVED_TO_SECURE_THEN_GLOBAL;
static {
MOVED_TO_GLOBAL = new HashSet<>();
MOVED_TO_SECURE_THEN_GLOBAL = new HashSet<>();
// these were originally in system but migrated to secure in the past,
// so are duplicated in the Secure.* namespace
MOVED_TO_SECURE_THEN_GLOBAL.add(Global.ADB_ENABLED);
MOVED_TO_SECURE_THEN_GLOBAL.add(Global.BLUETOOTH_ON);
...省略
MOVED_TO_GLOBAL.add(Settings.Global.RADIO_NFC);
MOVED_TO_GLOBAL.add(Settings.Global.RADIO_CELL);
MOVED_TO_GLOBAL.add(Settings.Global.RADIO_WIFI);
MOVED_TO_GLOBAL.add(Settings.Global.RADIO_BLUETOOTH);
MOVED_TO_GLOBAL.add(Settings.Global.RADIO_WIMAX);
MOVED_TO_GLOBAL.add(Settings.Global.SHOW_PROCESSES);
/** 添加你的自定义Keys */
MOVED_TO_GLOBAL.add(Settings.Global.CUSTOM_KEY);
}
Then add the custom key PUBLIC_SETTINGS
to the public system key list:
/**
* @hide
*/
public static final String CUSTOM_KEY = Global.CUSTOM_KEY;
/**
* These are all public system settings
*
* @hide
*/
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
public static final Set<String> PUBLIC_SETTINGS = new ArraySet<>();
static {
PUBLIC_SETTINGS.add(END_BUTTON_BEHAVIOR);
PUBLIC_SETTINGS.add(WIFI_USE_STATIC_IP);
PUBLIC_SETTINGS.add(WIFI_STATIC_IP);
PUBLIC_SETTINGS.add(WIFI_STATIC_GATEWAY);
PUBLIC_SETTINGS.add(WIFI_STATIC_NETMASK);
...省略
PUBLIC_SETTINGS.add(HAPTIC_FEEDBACK_ENABLED);
PUBLIC_SETTINGS.add(SHOW_WEB_SUGGESTIONS);
PUBLIC_SETTINGS.add(VIBRATE_WHEN_RINGING);
PUBLIC_SETTINGS.add(APPLY_RAMPING_RINGER);
/** 添加你的自定义Keys*/
PUBLIC_SETTINGS.add(CUSTOM_KEY);
}
Modifications in SettingsProvider
1. SettingsProvider
Add it in the system application module 默认值
.
Source path:
frameworks/base/packages/SettingsProvider/res/values/defaults.xml
code show as below:
<resources>
<bool name="def_dim_screen">true</bool>
<integer name="def_screen_off_timeout">60000</integer>
<integer name="def_sleep_timeout">-1</integer>
<bool name="def_airplane_mode_on">false</bool>
<bool name="def_theater_mode_on">false</bool>
<!-- Comma-separated list of bluetooth, wifi, and cell. -->
<string name="def_airplane_mode_radios" translatable="false">cell,bluetooth,wifi,nfc,wimax</string>
<string name="airplane_mode_toggleable_radios" translatable="false">bluetooth,wifi,nfc</string>
<string name="def_bluetooth_disabled_profiles" translatable="false">0</string>
<bool name="def_auto_time">true</bool>
<bool name="def_auto_time_zone">true</bool>
<bool name="def_accelerometer_rotation">false</bool>
<!-- Default screen brightness, from 0 to 255. 102 is 40%. -->
<integer name="def_screen_brightness">102</integer>
<bool name="def_screen_brightness_automatic_mode">false</bool>
<fraction name="def_window_animation_scale">100%</fraction>
<fraction name="def_window_transition_scale">100%</fraction>
<bool name="def_haptic_feedback">true</bool>
...省略
<!-- 添加你的自定义Keys -->
<string name="def_custom_key">key1</bool>
2. By loadGlobalSettings
loading custom Keys into the SettingsProvider system application module 数据库
.
Source path:
frameworks/base/packages/SettingsProvider/src/com/android/providers/settings/DatabaseHelper.java
code show as below:
private void loadGlobalSettings(SQLiteDatabase db) {
SQLiteStatement stmt = null;
final Resources res = mContext.getResources();
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);
...省略
loadSetting(stmt, Settings.Global.LID_BEHAVIOR, defaultLidBehavior);
//添加你的自定义Keys
loadStringSetting(stmt, Settings.Global.CUSTOM_KEY, R.string.def_custom_key);
} finally {
if (stmt != null) stmt.close();
}
}
private void loadStringSetting(SQLiteStatement stmt, String key, int resid) {
loadSetting(stmt, key, mContext.getResources().getString(resid));
}
epilogue
After completing the above modifications, you can use the custom Key in any system application:
String value = Settings.Global.getString(getContentResolver(), Settings.Global.CUSTOM_KEY);
Settings.Global.putString(getContentResolver(), Settings.Global.CUSTOM_KEY, " ");
For more information, please refer to the related introduction to SettingsProvider in another blog: Talking about data sharing in android system applications