The default Settings
“This is the fourth day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”
SettingsProvider stores data
-
SettingsProvider creates a database. When a user changes a SettngsProvider’s Settings, the user changes the value of the SettingsProvider. When the user changes the value of the SettingsProvider, the SettingsProvider service detects the changes. To modify system Settings and properties.
-
In Android version 6.0, the SettingsProvider was reconfigured to save the SettingsProvider data in XML files for performance and security purposes.
-
During the SetttngsProvider startup process, the database is created, the default data values are written to the database, and all the data in the database is migrated to an XML file.
SetttngsProvider starts the process
frameworks\base\packages\settingsprovider\src\com\android\providers\settings\SettingsProvider.java
Copy the code
-
Call the onCreate () method
The instantiated HandlerThread object mHandlerThread has a priority of THREAD_PRIORITY_BACKGROUND
1.2. Instantiate mSettingsRegistry, an instance of SettingsRegistry.
1.3. Next, a broadcast receiver is registered. The broadcasts of interest include device user changes and APP uninstallation, etc.
-
Call migrateAllLegacySettingsIfNeeded method. The makeKey method is called to obtain the key, which are three types: Global, System and Secure. After obtaining the key, the getSettingsFile method is used to create three types of XML files to store the data.
Global: All preference Settings are open to all users of the system, and third-party apps have read or write permissions; System: contains a variety of user preference System Settings; Secure: Indicates the user preference for security. Third-party apps have read and write permissions.Copy the code
-
Instantiate a DatabaseHelper, which is a subclass of SQLiteOpenHelper, and call its getWritableDatabase method to get an instance database of SQLiteDatabase pointing to the database file
-
On instantiation, it calls its onCreate method, creates the system, Secure, and Global database tables, calls the loadSettings method, loads a number of default values and writes them to the database. Most of these defaults defined in defaults. XML file (/ frameworks/base/packages/SettingsProvider/res/values/defaults. The XML)
-
Call migrateLegacySettingsForUserLocked method
1.1. Call ensureSettingsStateLocked method, instantiated settingsState object points to the sysytem of data files (/ data/system/users / 0 / settings_system. The XML).
1.2 call migrateLegacySettingsLocked method, query the database, query out all of the information System first, and then in a loop as SettingsState. InsertSettingLocked parameters ().
1.3 insertSettingLocked() encapsulates each Setting entry into the Setting object, and then puts the Setting object method into the ArrayMap<String, Setting> object mSettings.
SettingsState persistSyncLocked persistSyncLocked persistingsState persistSyncLocked persistingsState persistSyncLocked persistingsState persistSyncLocked persistingsState persistSyncLocked persistingsState persistSyncLocked
-
Delete the database file.
SettingsProvider encapsulation
-
To ensure that anywhere in the Java layer of Android can easily and quickly use SettingsProvider for data query, data insertion, data update, So there’s a settings. Java class in the Framework provider that encapsulates using SettingsProvider.
-
The settings. Java code creates three static internal classes, System, Secure, and Global, for each of the three data types in SettingsProvider.
-
The Global, Secure, and System static inner classes each hold their own instance variables for NameValueCache, Each NameValueCache holds an AIDL remote call to IContentProvider that points to settingSProvider.java in SettingsProvider, so, Data is queried through the getStringForUser() method of NameValueCache and inserted through the putStringForUser() method. Eventually, it switches to settingsProvider.java for data access.