Following the last analysis of settings's article Android system settings profile, now you want to add some of the system does not have the settings, because the last time just guess Databasehelper.java In the creation of the database in the Defaults.xml in the configuration in the database, so now to analyze the Settingsprivider source code (source or official ANDROID-4.4_R1 version).
First, the main analysis Databasehelper.java file:
1. database file for settings.db defined table someone, inside system, secure, global is three than the main one.
private static final String TAG = "Settingsprovider"; private static final String database_name = "SETTINGS.DB"; Please. IF you update the database version, check to make sure the//database gets upgraded properly. At a minimum, please confirm this ' upgradeversion '//is properly propagated through your. Not doing so would result in a loss of user//settings. private static final int database_version = 98; Private Context Mcontext; private int muserhandle; private static final hashset<string> mvalidtables = new hashset<string> (); private static final String Table_system = "SYSTEM"; private static final String table_secure = "SECURE"; private static final String Table_global = "GLOBAL"; static {Mvalidtables.add (Table_system); Mvalidtables.add (table_secure); Mvalidtables.add (Table_global); Mvalidtables.add ("bluetooth_devices"); Mvalidtables.add ("bookmarks"); TheSE is old. Mvalidtables.add ("Favorites"); Mvalidtables.add ("gservices"); Mvalidtables.add ("Old_favorites"); }
2. Look at the OnCreate () method, create indexes on tables and tables, and then call Loadbookmarks (DB); Loadvolumelevels (DB); LoadSettings (DB); Three methods, Loadbookmarks (db), is the intent of several common applications (e.g., Address Book App,emailapp, SMS App,), Loadvolumelevels (db); The initialization is a sound-related formulation, loadsettings (db), by reading the default values from the Defaults.xml file into the corresponding table, validating the previous guess.
@Override public void OnCreate (Sqlitedatabase db) {db.execsql ("CREATE TABLE system (" + "_id INTEGER PRIMARY KEY autoincrement," + "name TEXT UNIQUE on CONFLICT REPLACE," + "Value TEXT" + ");"); Db.execsql ("CREATE INDEX systemIndex1 on system (name);"); Createsecuretable (DB); Only create the global table for the singleton ' owner ' user if (Muserhandle = = Userhandle.user_owner) { Createglobaltable (DB); } db.execsql ("CREATE TABLE bluetooth_devices (" + "_id INTEGER PRIMARY KEY," + "Name text," + "addr TEXT," + "channel INTEGER," + "type Intege R "+"); "); Db.execsql ("CREATE TABLE bookmarks (" + "_id INTEGER PRIMARY KEY," + "title TEXT," + "Folder TEXT," + "Intent TEXT," + "shortcut Integer," + "ordering integer" + " );"); Db.execsql ("CREATE INDEX bookmarksIndex1 on bookmarks (folder);"); Db.execsql ("CREATE INDEX bookmarksIndex2 on bookmarks (shortcut);"); Populate bookmarks table with initial bookmarks Boolean onlycore = false; try {onlycore = IPackageManager.Stub.asInterface (Servicemanager.getservice ("package")). IsO Nlycoreapps (); } catch (RemoteException e) {} if (!onlycore) {loadbookmarks (db); }//Load initial volume levels into DB loadvolumelevels (db); Load inital settings Values loadsettings (db); }
3. LoadSettings (DB); The three methods are read from the Defaults.xml file to the corresponding table, loadsystemsettings (db) is stored in the system table, loadsecuresettings (db) is stored in the secure table.
private void LoadSettings (Sqlitedatabase db) { loadsystemsettings (db); Loadsecuresettings (db); The global table is exists for the ' owner ' user if (muserhandle = = Userhandle.user_owner) { loadglobalsetting s (db); } }
In the Loadsetting () method, sqlitestatement values are stored in the database:
private void Loadsystemsettings (Sqlitedatabase db) { sqlitestatement stmt = null; try { stmt = db.compilestatement ("INSERT OR IGNORE into system (Name,value)" + "VALUES (?,?);"); Loadbooleansetting (stmt, Settings.System.DIM_SCREEN, r.bool.def_dim_screen); //.............. to omit ........... Loadintegersetting (stmt, Settings.System.POINTER_SPEED, r.integer.def_pointer_speed); } finally { if (stmt! = null) stmt.close (); } }
Second, how to increase the setting (for example, to add an account information to the settings):
1. Add the interface-related code in Settings:
1.a. The Settings/res/xml/settings_headers.xml file adds:
Android:id= "@+id/account_info"
Android:fragment= "Com.android.settings.accounts.AccountInfoSettings"
android:title= "@string/account_info_label"
android:icon= "@drawable/ic_menu_add_dark"/>
1.b. Add a string icon, etc., such as the Settings/res/values/strings.xml file:
<string name= "Account_info_label" > "Account Information" </string>
1.C related classes and code writing: such as creating fragment:accountinfosettings and writing code, the code inside will settings.system.put** the code will operate Settingsprovider
2. Modify Settingsprivider
2. A defalult.xml
<bool name= "Def_acctount_islogin" >false</bool>
<string name= "Def_acctount_name" > Please set nickname </string>
2. b Databasehelper.java increased in the loadsecuresettings (Sqlitedatabase db) method
Loadbooleansetting (stmt, Settings.Secure.ACCOUNT_ISLOGIN,
R.bool.def_account_islogin);
Loadstringsetting (stmt, Settings.Secure.ACCOUNT_NAME,
R.string.def_account_name);
3. Find the internal static class in the/android-4.4_r1/frameworks/base/core/java/android/provider/settings.java file: public static final class Secure extends Namevaluetable {...} is added in the Secure class:
public static final String Account_islogin = "Def_account_islogin";
public static final String account_name = "Def_account_name";
This modification is basically done and can be compiled, run, tested. The actual development of the steps should be 3.2,1 in reverse order to do ^-^.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Settingsprivider analysis and modification method of Android system