android database with default values

Recently encountered in the project to set the default value. Take it as an example.

Suppose we want to change the default value of the Settings->Display->Daydream->menu->When to daydream-> popup menu from While docked to While charging.

OK, let's begin.

First find the createWhenToDreamDialog method of the DreamSettings class. It is found that the default selected value comes from the variable initialSelection, which is used as a subscript, and its value is obtained through

int initialSelection = mBackend.isActivatedOnDock() && mBackend.isActivatedOnSleep() ? 2
        : mBackend.isActivatedOnDock() ? 0
        : mBackend.isActivatedOnSleep() ? 1
        : -1;

 so judged.

Continue to track down to the DreamBackend class

isActivatedOnDock

method, note the key value

SCREENSAVER_ACTIVATE_ON_DOCK

Then go to the getBoolean method, by

return Settings.Secure.getInt(mContext.getContentResolver(), key, def ? 1 : 0) == 1;

Discovery is fetching the value from the database. So we searched for the key value we wrote down before, and quickly found the DatabaseHelper class, from

loadBooleanSetting(stmt, Settings.Secure.SCREENSAVER_ACTIVATE_ON_DOCK,
                        com.android.internal.R.bool.config_dreamsActivatedOnDockByDefault);

It is found that the default value is obtained from a boolean value, so we search for config_dreamsActivatedOnDockByDefault to find a config.xml file under frameworks/base/core/res... .

There are two of these

<!-- If supported and enabled, are dreams activated when docked? (by default) -->
    <bool name="config_dreamsActivatedOnDockByDefault">false</bool>
    <!-- If supported and enabled, are dreams activated when asleep and charging? (by default) -->
    <bool name="config_dreamsActivatedOnSleepByDefault">true</bool>

OK! This is exactly where we are looking. I swapped the false and true above. mmm compile frameworks/base/core/res/ to generate frameworks-res.apk. Push it into the phone and restart. Verification found no effect.

So here comes our problem!

So I looked up the database and found two items in the secure table in data/data/com.android.providers.settings/databases/settings.db with the sqlite3 tool ( this method of looking up the database is very important!!! )

17|screensaver_activate_on_dock|1
18|screensaver_activate_on_sleep|0

Their values ​​have not changed.

Then, I package it after the single-edit frameworks/base/core/res/, as follows

mmm frameworks/base/core/res/ snod

Generate the system.img image, wipe the phone completely and flash it in. After startup, it was found that the value in the database successfully became

17|screensaver_activate_on_dock|0
18|screensaver_activate_on_sleep|1

I went to the Setting directory to check, and indeed, while charging was checked by default. The problem was successfully solved! !

Then we can summarize the following reasons:

When the phone is turned on for the first time after the version is programmed, it will write our default values ​​such as config.xml/defaults.xml to the database at one time. Then applications like Settings fetch values ​​from these databases. US

Therefore, if these default values ​​are modified in the future, the verification needs to be done by means of single-editing packaging. Simply editing this module and pushing does not affect the database, so it is useless.

Today, when I was helping a colleague read the code, I found that it is useless to simply delete an item from the layout. It needs to be chased deeply to find the default value of the function corresponding to the item. Intrigued me, and then successfully solved the problem with my method and successfully verified it. It also let me figure out the android database and default values. A lot of harvest, very happy!

 

 

 

 

 

 

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327089604&siteId=291194637