In the Android operating system, SettingsProvider is a very critical component, which is responsible for storing and managing system and application settings. In this article, I’ll dig into the DatabaseHelper class, which is the core part of the SettingsProvider.
1. What is DatabaseHelper?
DatabaseHelper is a class inherited from SQLiteOpenHelper that is responsible for creating, updating and managing the system settings database. The system settings database is an SQLite database file, which is stored in /data/system/users/0/settings_system.xml, /data/system/users/0/settings_secure.xml, /data/system/users/0/ settings_global.xml
path, where user_id
is the ID of the current user. The system settings database contains multiple tables, each table corresponds to a type of settings, such as system, secure, global, etc.
2. Main method
2.1 onCreate(SQLiteDatabase db)
This method is called when the database is first created. It is responsible for initializing database tables and default system settings.
@Override public void onCreate(SQLiteDatabase db) {<!-- --> // Create system settings table db.execSQL("CREATE TABLE system (" + "_id INTEGER PRIMARY KEY AUTOINCREMENT," + "name TEXT UNIQUE ON CONFLICT REPLACE," + "value TEXT" + ");"); // ... other table creation code ... // Initialize default settings loadDefaultSystemSettings(db); }
2.2 onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
This method will be called when the database version is upgraded. It is responsible for updating the database table structure and data.
@Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {<!-- --> // Perform the corresponding database upgrade operation according to the version number if (oldVersion < 2) {<!-- --> // ... code to upgrade to version 2 ... } if (oldVersion < 3) {<!-- --> // ... code to upgrade to version 3 ... } // ... upgrade code for other versions ... }
3. Data table structure
To better understand DatabaseHelper, you need to understand the structure of the data tables it manages. Here are some main tables:
table name | description | main field |
---|---|---|
system | Storage system level settings | _id, name, value |
secure | storage Security-related settings | _id, name, value |
global | Store global settings, valid for all users | _id, name, value |
… | … | … |
4. Initialize default settings
DatabaseHelper is also responsible for loading default system settings. These settings are usually defined in XML resource files and loaded when the database is created.
private void loadDefaultSystemSettings(SQLiteDatabase db) {<!-- --> // load default settings from resource file ContentValues values = new ContentValues(); values. put("name", "example_setting_name"); values. put("value", "example_setting_value"); db.insert("system", null, values); // ... load other default settings ... }
5. Different Android system version settings storage system location, difference
The settings of Android system are stored in the system partition in the form of SQLite database. The location and structure of the settings database may be different for different Android system versions. Here are some common Android system versions and their settings database locations:
Android version | settings database location |
---|---|
Android 4.4 | /data/data/com.android.providers.settings/databases/settings.db |
Android 5.0 | /data/system/ users/0/settings.db |
Android 6.0 | /data/system/users/0/settings_system.db, /data/system/users/ 0/settings_secure.db, /data/system/users/0/settings_global.db |
Android 7.0 ~ | /data/system/users/0 /settings_system.xml, /data/system/users/0/settings_secure.xml, /data/system/users/0/settings_global.xml |
Android 11.0 | /data/system/users/0/settings_system.xml, /data/system/users/0/settings_secure.xml /data/system/users/0/settings_global.xml |
As can be seen from the above table, the Android system from version 4.4 to version 6.0 uses the SQLite database to store settings, but reads from different directories. Starting from version 7.0, the Android system uses XML files to store settings, and stores system level, security level, and global level settings separately in different directories.
6. Detailed usage of adb settings command
adb is short for Android Debug Bridge, which is a tool for connecting and controlling Android devices. adb provides a subcommand called settings that can be used to query and modify system and application settings.
The basic syntax of the adb settings command is as follows:
adb shell settings [namespace] [name] [value]
in:
- namespace refers to the namespace to which the settings to be operated belong, which can be system, secure, global or other application-defined namespaces.
- name is the name of the setting to operate on.
- value refers to the value to be set or queried.
For example:
- Query the screen brightness in system settings:
adb shell settings get system screen_brightness
- Query the status of airplane mode in global settings:
adb shell settings get global airplane_mode_on
7. How to create a settings variable in the Android system How to initialize
If you want to create a custom settings variable in the Android system, you need to do the following steps:
- Define a constant in the
SettingsContract
class as the name of the custom variable. For example:
public static final String MY_SETTING = "my_setting";
- In the
DatabaseHelper
class, add a column in the corresponding data table as the storage field of the custom variable. For example:
db.execSQL("ALTER TABLE system ADD COLUMN my_setting TEXT;");
- In the
DatabaseHelper
class, in theonCreate
method, set a default value for the custom variable. For example:
values.put("name", "my_setting"); values. put("value", "default_value"); db.insert("system", null, values);
- In the
SettingsProvider
class, in thecall
method, add a read-write interface for the custom variable. For example:
case MY_SETTING: return getOrPutSetting(uri, db, "system", "my_setting", value);
- In the
Settings
class, add a getter and setter method for the custom variable. For example:
public static String getMySetting(ContentResolver cr) {<!-- --> return Settings.System.getString(cr, MY_SETTING); } public static boolean putMySetting(ContentResolver cr, String value) {<!-- --> return Settings.System.putString(cr, MY_SETTING, value); }
You can use the custom settings variable in the Android system
The following is a table made from the contents of the provided defaults.xml
file:
field name | value range | field meaning | ||
---|---|---|---|---|
def_dim_screen | true, false | Whether to reduce screen brightness | ||
def_screen_off_timeout | Integer (such as : 60000) | Screen off timeout (milliseconds) | ||
def_sleep_timeout | integer (eg: -1) | Sleep timeout time | ||
def_airplane_mode_on | true, false | Whether to enable airplane mode | ||
def_theater_mode_on | true, false | Whether to enable theater mode | ||
def_airplane_mode_radios | String (such as: cell, bluetooth, wifi, nfc, wimax) | Wireless communication mode that is turned off in airplane mode | ||
airplane_mode_toggleable_radios | String (such as: bluetooth,wifi,nfc) | Wireless communication methods that can be switched in flight mode | ||
def_bluetooth_disabled_profiles | string (eg: 0) | Bluetooth configuration to disable by default | ||
def_auto_time | true, false | Whether to automatically set the time | ||
def_auto_time_zone | true, false | Whether to automatically set the time zone | ||
def_accelerometer_rotation | true, false | Whether to allow the accelerometer to rotate the screen | ||
def_screen_brightness | Integer (eg: 102) | Default screen brightness (0-255) | ||
def_screen_brightness_automatic_mode | true, false | Whether to automatically adjust the screen brightness | ||
def_window_animation_scale | Score (eg: 100%) | window animation scaling Scale | ||
def_window_transition_scale | Score (eg: 100%) | window transition scaling | ||
def_haptic_feedback | true, false | Whether to enable haptic feedback | ||
def_dock_sounds_enabled_when_accessibility | integer (eg: 0) | Whether the dock sound is enabled when accessibility is enabled | ||
def_desk_dock_sound | String (eg: / product/media/audio/ui/Dock.ogg) | Desktop dock sound file path | ||
def_desk_undock_sound | String (such as : /product/media/audio/ui/Undock.ogg) | Desktop undock sound file path | ||
def_car_dock_sound | characters string (such as: /product/media/audio/ui/Dock.ogg) | car docking sound file path | ||
def_car_undock_sound | String (eg: /product/media/audio/ui/Undock.ogg) | Car undock sound file path | ||
def_lockscreen_sounds_enabled | Integer (eg: 1) | Whether the lock screen sound is enabled | ||
def_lock_sound | String (eg: / product/media/audio/ui/Lock.ogg) | Lock screen sound file path | ||
def_unlock_sound | string (such as : /product/media/audio/ui/Unlock.ogg) | Unlock sound file path | ||
def_trusted_sound | string( eg: /product/media/audio/ui/Trusted.ogg) | Trusted sound file path | ||
def_wireless_charging_started_sound | String (such as: /product/media/audio/ui/WirelessChargingStarted.ogg) | Wireless charging start sound file path | ||
def_charging_started_sound | String (such as: /product/media/audio/ui/ChargingStarted.ogg) | Charging start sound file path | ||
def_max_sound_trigger_detection_service_ops_per_day | Integer (eg: 1000) | The maximum number of operations per day for the sound trigger detection service | ||
def_sound_trigger_detection_service_op_timeout | Integer (eg: 15000) | Operation timeout of sound trigger detection service | ||
def_lockscreen_disabled | true, false | Whether to disable the lock screen | ||
def_device_provisioned | true, false | Is the device provisioned | ||
def_dock_audio_media_enabled | Integer (eg: 1) | Whether to enable audio media when docked | ||
def_notifications_use_ring_volume | true, false | Whether the notification uses the ringtone volume | ||
def_vibrate_in_silent | true, false | Whether to vibrate in silent mode | ||
def_sync_parent_sounds | true, false | Synchronize parent sound effects | ||
def_accessibility_speak_password | true, false | Whether the accessibility function reads the password | ||
def_touch_exploration_enabled | true, false | Whether touch exploration is enabled | ||
def_accessibility_display_magnification_scale | Score (eg: 200%) | Accessibility display magnification ratio | ||
def_accessibility_display_magnification_enabled | true, false | Whether to enable accessibility display magnification | ||
def_accessibility_display_magnification_auto_update | true, false | Accessibility display magnification is automatically updated | ||
def_user_rotation | Integer (eg: 0) | Default setting for user rotation | ||
def_download_manager_max_bytes_over_mobile | Integer ( eg: -1) | Maximum bytes to download over mobile data | ||
def_download_manager_recommended_max_bytes_over_mobile | integer (eg: -1) | Recommended maximum number of bytes to download via mobile data | ||
def_long_press_timeout_millis | integer (eg: 400) | Long press timeout (milliseconds) | ||
def_multi_press_timeout_millis | Integer (eg: 300) | Multiple press timeout ( milliseconds) | ||
def_show_ime_with_hard_keyboard | true, false | Whether to display the input method on the hard keyboard | ||
def_pointer_speed | integer (eg: 0) | default setting for pointer speed | ||
def_dtmf_tones_enabled | true, false | Whether DTMF tones are enabled | ||
def_sound_effects_enabled | true, false | Whether to enable UI touch sound | ||
def_stay_on_while_plugged_in | true, false | Whether to stay on when plugged in | ||
def_max_dhcp_retries | Integer (eg: 9) | Maximum number of retries to connect to DHCP | ||
def_user_setup_complete | true, false | Whether user setup is complete | ||
def_tv_user_setup_complete | true, false | Whether the TV user setting is completed | ||
def_low_battery_sound_timeout | Integer (eg: 0) | Low battery sound timeout | ||
def_immersive_mode_confirmations | string | List of packages that no longer require immersive mode confirmations | ||
def_wifi_scan_always_available | integer (eg: 0) | Whether Wi-Fi scanning is always available | ||
def_lock_screen_show_notifications | Integer (eg: 1) | Whether notifications are displayed on the lock screen | ||
def_lock_screen_allow_private_notifications | true, false | Whether private notifications are allowed on the lock screen | ||
def_heads_up_enabled | Integer (eg: 1) | Whether to enable Heads Up notifications | ||
def_device_name | string (eg: %1
the s s %2 ss) |
Default device name format | ||
def_device_name_simple | String (eg: %1$s) | Simple default device name format | ||
def_wake_gesture_enabled | true, false | Whether to enable wake gesture | ||
def_double_tap_to_wake | true, false | Whether to enable double-tap to wake | ||
def_nfc_payment_component | String | Default settings of NFC payment components | ||
def_add_users_from_lockscreen | true, false | whether Allow adding users from lock screen | ||
def_end_button_behavior | Integer (eg: 0x2) | End button behavior | ||
def_restrict_background_data | true, false | Whether to restrict background data | ||
def_backup_manager_constants | String | Default settings for backup manager constants | ||
def_mobile_data_always_on | true, false | Whether mobile data is always on | ||
def_backup_local_transport_parameters | string | Backup default settings for local transport parameters | ||
def_zen_duration | Integer (eg: 0) | Default duration of Zen mode | ||
def_backup_agent_timeout_parameters | string | Default setting for backup agent timeout parameter | ||
def_vibrate_when_ringing | true, false | true, false | td> | Whether to vibrate when an incoming call |
def_apply_ramping_ringer | true, false | Whether to apply a gradually increasing ringtone | ||
def_charging_vibration_enabled | true, false | Whether to vibrate when charging | ||
def_charging_sounds_enabled | true, false | Whether to play sound when charging | ||
def_notification_bubbles | true, false | Whether to enable the notification bubble | ||
def_aware_enabled | true, false | Whether the Aware function is enabled | ||
def_skip_gesture | true, false | Whether to enable skip gesture | ||
def_silence_gesture | true, false | Whether mute gesture is enabled | ||
def_aware_lock_enabled | true, false | Aware lock function Whether to enable | ||
def_hdmiControlAutoDeviceOff | true, false | Whether the HDMI control automatic device off function is enabled | ||
def_screenshot_button_show | Integer (eg: 0) | Whether to show the default setting of the screenshot button | ||
def_dock_sounds_enabled_when_accessibility | Integer (eg: 0) | Whether dock sound is enabled when accessibility is enabled | ||
def_desk_dock_sound | String (eg: /product/media/audio/ui/Dock.ogg) | Desktop dock sound file path | ||
def_desk_undock_sound | String (eg: /product/media/audio/ui/Undock.ogg) | Desktop undock sound file path | ||
def_car_dock_sound | String (eg: /product/media/audio/ui/Dock.ogg) | Car docking sound file path | ||
def_car_undock_sound | string (eg: /product/media/audio/ui/Undock.ogg) | car undock sound file path | ||
def_lockscreen_sounds_enabled | Integer (eg: 1) | Whether the lock screen sound is enabled | ||
def_lock_sound | String (such as: /product/media/audio/ui/Lock.ogg) | Lock screen sound file path | ||
def_unlock_sound | String (such as: /product/media/audio/ui/Unlock.ogg) | Unlock sound file path | ||
def_trusted_sound | String (eg: /product/media/audio/ui/Trusted.ogg) | Trusted sound file path | ||
def_wireless_charging_started_sound | String (such as: /product/media/audio/ui/WirelessChargingStarted.ogg) | Wireless charging start sound file path | ||
def_charging_started_sound | string (such as: /product/media/audio/ui/ChargingStarted.ogg) | charging start sound file path | ||
def_max_sound_trigger_detection_service_ops_per_day | Integer (eg: 1000) | The maximum number of operations per day for the sound trigger detection service | ||
def_sound_trigger_detection_service_op_timeout | Integer (eg: 15000) | Operation timeout of sound trigger detection service | ||
def_lockscreen_disabled | true , false | Whether to disable the lock screen | ||
def_device_provisioned | true, false | Whether the device is provisioned | ||
def_dock_audio_media_enabled | Integer (eg: 1) | Whether to enable audio media when docked | ||
def_notifications_use_ring_volume | true, false | Whether the notification uses the ring volume | ||
def_vibrate_in_silent | true , false | Whether to vibrate in silent mode | ||
def_sync_parent_sounds | true, false | Whether to synchronize parent sound effects | ||
def_accessibility_speak_password | true, false | Whether the accessibility function reads the password | ||
def_touch_exploration_enabled | true, false | Whether to enable touch exploration | ||
def_accessibility_display_magnification_scale | Score (such as : 200%) | Accessibility display magnification ratio | ||
def_accessibility_display_magnification_enabled | true, false | Whether to enable Accessibility display magnification | ||
def_accessibility_display_magnification_auto_update | true, false | Whether the accessibility display magnification is automatically updated | ||
def_user_rotation | integer (eg: 0) | default setting for user rotation | ||
def_download_manager_max_bytes_over_mobile | Integer (eg: -1) | Maximum bytes to download over mobile data | ||
def_download_manager_recommended_max_bytes_over_mobile | Integer (eg: -1) | Recommended maximum number of bytes to download via mobile data | ||
def_long_press_timeout_millis | Integer (eg: 400) | Long press timeout (milliseconds) | ||
def_multi_press_timeout_millis | Integer (eg: 300) | Multiple pressing timeout (milliseconds) | ||
def_show_ime_with_hard_keyboard | true, false | Whether to display the input method on the hard keyboard | ||
def_pointer_speed | Integer (eg: 0) | Default setting for pointer speed | ||
def_dtmf_tones_enabled | true, false | Whether to enable DTMF tones | ||
def_sound_effects_enabled | true, false | Whether to enable UI touch sound | ||
def_stay_on_while_plugged_in | true, false | Whether to stay on when plugged in | ||
def_max_dhcp_retries | Integer (eg: 9) | Maximum number of retries to connect to DHCP | ||
def_user_setup_complete | true, false | Whether user setup is complete | ||
def_tv_user_setup_complete | true, false | Whether the TV user setting is complete | ||
def_low_battery_sound_timeout | integer (eg: 0) | Low battery sound timeout | ||
def_immersive_mode_confirmations | string | List of packages that no longer require immersive mode confirmation | ||
def_wifi_scan_always_available | integer (e.g. 0) | Whether Wi-Fi scan is always available | ||
def_lock_screen_show_notifications | Integer (eg: 1) | Whether to show notifications on the lock screen | ||
def_lock_screen_allow_private_notifications | true, false | Whether private notifications are allowed on the lock screen | ||
def_heads_up_enabled | Integer (eg: 1) | Whether to enable Heads Up notification | ||
def_device_name | string (eg: %1
the s s %2 ss) |
Default device name format | ||
def_device_name_simple | String (eg: %1$s) | Simple default device name format | ||
def_wake_gesture_enabled | true, false | Whether to enable wake gesture | ||
def_double_tap_to_wake | true, false | Whether to enable double-tap to wake | ||
def_nfc_payment_component | String | Default settings of NFC payment components | ||
def_add_users_from_lockscreen | true, false | whether Allow adding users from lock screen | ||
def_end_button_behavior | Integer (eg: 0x2) | End button behavior | ||
def_restrict_background_data | true, false | Whether to restrict background data | ||
def_backup_manager_constants | String | Default settings for backup manager constants | ||
def_mobile_data_always_on | true, false | Whether mobile data is always on | ||
def_backup_local_transport_parameters | string | Backup default settings for local transport parameters | ||
def_zen_duration | Integer (eg: 0) | Default duration of Zen mode | ||
def_backup_agent_timeout_parameters | string | Default setting for backup agent timeout parameter | ||
def_vibrate_when_ringing | true, false | true, false | td> | Whether to vibrate when an incoming call |
def_apply_ramping_ringer | true, false | Whether to apply a gradually increasing ringtone | ||
def_charging_vibration_enabled | true, false | Whether to vibrate when charging | ||
def_charging_sounds_enabled | true, false | Whether to play sound when charging | ||
def_notification_bubbles | true, false | Whether to enable the notification bubble | ||
def_aware_enabled | true, false | Whether the Aware function is enabled | ||
def_skip_gesture | true, false | Whether to enable skip gesture | ||
def_silence_gesture | true, false | Whether mute gesture is enabled | ||
def_aware_lock_enabled | true, false | Aware lock function Whether to enable | ||
def_hdmiControlAutoDeviceOff | true, false | Whether the HDMI control automatic device off function is enabled | ||
def_screenshot_button_show | Integer (eg: 0) | Whether to display the default setting of the screenshot button |
Summary
This article introduces the relevant knowledge of SettingsProvider DatabaseHelper above Android 11, including its function, method, data table structure, default settings, differences between different versions, the use of adb commands, the creation of custom variables, etc.
Hope this article can help you better understand Android’s SettingsProvider
and DatabaseHelper
. Please leave a message if you have any questions or suggestions.