android默认设置项的修改

修改默认值

在安卓源码中,要对安卓的一些默认属性配置进行修改的话(比如:修改背光默认,修改默认锁屏开关,休眠时间等等),我们可以非常容易地找到一个xml档·

其xml档在/frameworks/base/packages/SettingsProvider/res/values/defaults.xml




    true
    -1
    -1
    false
    
    cell,bluetooth,wifi,nfc,wimax
    bluetooth,wifi,nfc
    true
    true
    true
    
    255
    false
    100%
    100%
    true

    false
    false
    false
    true
    
    gps
    true
    
    true
    true
    false
    
    2
    true

    false
    android/com.android.internal.backup.LocalTransport

    
    true

    true
    false
    true
    true

    
    1
    /system/media/audio/ui/LowBattery.ogg
    0
    /system/media/audio/ui/Dock.ogg
    /system/media/audio/ui/Undock.ogg
    /system/media/audio/ui/Dock.ogg
    /system/media/audio/ui/Undock.ogg
    1
    /system/media/audio/ui/Lock.ogg
    /system/media/audio/ui/Unlock.ogg
    /system/media/audio/ui/Trusted.ogg
    /system/media/audio/ui/WirelessChargingStarted.ogg

    true
    false
    1

    
    true

    
    true

    
    false

    
    false

    
    
            
            0x13=0x01000100;
            
            0x14=0x01010100;
            
            0x15=0x02000001;
            
            0x16=0x02010001;
            
            0x200000013=0x02000601;
            
            0x200000014=0x02010601;
            
            
            0x200000015=0x03020101;
            
            
            0x200000016=0x03010201;
            
            0x200000023=0x02000301;
            
            0x200000024=0x02010301;
            
            
            0x200000037=0x03070201;
            
            
            0x200000038=0x03000701:0x03010701:0x03020701;
    

    
    
        https://ssl.gstatic.com/accessibility/javascript/android/AndroidVox_v1.js
    

    
    false

    
    200%

    
    false

    
    true

    
    0

    
    -1
    
    -1

    
    500

    
    0

    
    true
    
    true

    
    false

    
    9

    
    false

    
    0

    
    false

    
    

    
    0

    
    1

    
    true

    
    1

    
    %1$s %2$s

    
    %1$s

    
    true

    
    true


在此,找出几个做事例:

  • -1表示休眠时间,默认如果没有修改的是6000,即6000ms的意思,6s进行休眠,在这里将其改为-1就是让其永远不休眠
  • true表示是否将锁屏屏蔽,默认是false,在这里修改了为true,表示锁屏永不启动·
  • 255表示的是背光的亮度,其值是0-255之间的值,在这里改成了255表示最亮的时候,它的值来自于LCD所给出的pwm的等级阶数·

当我们部分编译完成之后,将生成的apk文件push到我们的手机上,发现竟然没有任何效果~~,这是什么原因导致的呢?

说一下博主的解决方案,原因在于获取xml的值后,是对数据库settings.db进行操作,因此即使我们push进去apk,但这个数据库没有发生变动,因此会没有修改,然后博主对用户空间和cashe进行恢复出厂设置就可以顺利了

adb reboot bootloader
fastboot -w
fastboot reboot

那么是谁在进行了数据库的操作呢?

setting.db跟踪

当我对相关的xml的值进行追踪的时候,发现了

/frameworks/base/packages/SettingsProvider/src/com/android/providers/settings/DatabaseHelper.java

这个java文件就是对数据库setting.db,进行的操作,那么我们以def_screen_brightness_automatic_mode为追踪对象.

/**
 * Database helper class for {@link SettingsProvider}.
 * Mostly just has a bit {@link #onCreate} to initialize the database.
 */
public class DatabaseHelper extends SQLiteOpenHelper {
    private static final String TAG = "SettingsProvider";
    private static final String DATABASE_NAME = "settings.db";
        private static final int TYPE_NONE = -1;

    // Please, please please. If you update the database version, check to make sure the
    // database gets upgraded properly. At a minimum, please confirm that 'upgradeVersion'
    // is properly propagated through your change.  Not doing so will result in a loss of user
    // settings.
    private static final int DATABASE_VERSION = 113;
    ....

这里可以看到DatabaseHelper其实是继承了SQLiteOpenHelper这个类·其数据库的名字为settings.db·其版本是113

更新数据库

看到 public void onUpgrade(SQLiteDatabase db, int oldVersion, int currentVersion)这个函数,就是对数据库的更新

 if (upgradeVersion < 108) {
            // Reset the auto-brightness setting to default since the behavior
            // of the feature is now quite different and is being presented to
            // the user in a new way as "adaptive brightness".
            db.beginTransaction();
            SQLiteStatement stmt = null;
            try {
                stmt = db.compileStatement("INSERT OR REPLACE INTO system(name,value)"
                        + " VALUES(?,?);");
                loadBooleanSetting(stmt, Settings.System.SCREEN_BRIGHTNESS_MODE,
                        R.bool.def_screen_brightness_automatic_mode);
                db.setTransactionSuccessful();
            } finally {
                db.endTransaction();
                if (stmt != null) stmt.close();
            }
            upgradeVersion = 108;
        }

再追loadBooleanSetting;

    private void loadBooleanSetting(SQLiteStatement stmt, String key, int resid) {
        loadSetting(stmt, key,
                mContext.getResources().getBoolean(resid) ? "1" : "0");
    }

再追loadSetting;

 private void loadSetting(SQLiteStatement stmt, String key, Object value) {
        stmt.bindString(1, key);
        stmt.bindString(2, value.toString());
        stmt.execute();
    }

大致上我们可以看到是将相关的键值对存到数据库中,而且呢,这个键值对所谓的true或者false其实是0或者1·且是字符串类型的·

源码追到这里已经差不多了·

加载xml文件

    private void loadSettings(SQLiteDatabase db) {
        loadSystemSettings(db);
        loadSecureSettings(db);
        // The global table only exists for the 'owner' user
        if (mUserHandle == UserHandle.USER_OWNER) {
            loadGlobalSettings(db);
        }
    }

    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);
            loadIntegerSetting(stmt, Settings.System.SCREEN_OFF_TIMEOUT,
                    R.integer.def_screen_off_timeout);

            // Set default cdma DTMF type
            loadSetting(stmt, Settings.System.DTMF_TONE_TYPE_WHEN_DIALING, 0);

            // Set default hearing aid
            loadSetting(stmt, Settings.System.HEARING_AID, 0);

            // Set default tty mode
            loadSetting(stmt, Settings.System.TTY_MODE, 0);

            loadIntegerSetting(stmt, Settings.System.SCREEN_BRIGHTNESS,
                    R.integer.def_screen_brightness);

            loadBooleanSetting(stmt, Settings.System.SCREEN_BRIGHTNESS_MODE,
                    R.bool.def_screen_brightness_automatic_mode);

            loadDefaultAnimationSettings(stmt);

            loadBooleanSetting(stmt, Settings.System.ACCELEROMETER_ROTATION,
                    R.bool.def_accelerometer_rotation);

            loadDefaultHapticSettings(stmt);

            loadBooleanSetting(stmt, Settings.System.NOTIFICATION_LIGHT_PULSE,
                    R.bool.def_notification_pulse);

            loadUISoundEffectsSettings(stmt);

            loadIntegerSetting(stmt, Settings.System.POINTER_SPEED,
                    R.integer.def_pointer_speed);

            loadStringSetting(stmt, Settings.System.TIME_12_24,
                    R.string.def_time_format);

            loadStringSetting(stmt, Settings.System.DATE_FORMAT,
                    R.string.def_date_format);
        } finally {
            if (stmt != null) stmt.close();
        }
    }

在源码2400多行左右有loadSecureSettings函数,这个函数来加载xml文件中的各个选项

    private void loadSecureSettings(SQLiteDatabase db) {
        SQLiteStatement stmt = null;
        try {
            stmt = db.compileStatement("INSERT OR IGNORE INTO secure(name,value)"
                    + " VALUES(?,?);");

            loadStringSetting(stmt, Settings.Secure.LOCATION_PROVIDERS_ALLOWED,
                    R.string.def_location_providers_allowed);

            String wifiWatchList = SystemProperties.get("ro.com.android.wifi-watchlist");
            if (!TextUtils.isEmpty(wifiWatchList)) {
                loadSetting(stmt, Settings.Secure.WIFI_WATCHDOG_WATCH_LIST, wifiWatchList);
            }

            // Don't do this.  The SystemServer will initialize ADB_ENABLED from a
            // persistent system property instead.
            //loadSetting(stmt, Settings.Secure.ADB_ENABLED, 0);

            // Allow mock locations default, based on build
            loadSetting(stmt, Settings.Secure.ALLOW_MOCK_LOCATION,
                    "1".equals(SystemProperties.get("persist.env.c.allow.enable")) ? 1 : 0);

            loadSecure35Settings(stmt);

            loadBooleanSetting(stmt, Settings.Secure.MOUNT_PLAY_NOTIFICATION_SND,
                    R.bool.def_mount_play_notification_snd);

            loadBooleanSetting(stmt, Settings.Secure.MOUNT_UMS_AUTOSTART,
                    R.bool.def_mount_ums_autostart);

            loadBooleanSetting(stmt, Settings.Secure.MOUNT_UMS_PROMPT,
                    R.bool.def_mount_ums_prompt);

            loadBooleanSetting(stmt, Settings.Secure.MOUNT_UMS_NOTIFY_ENABLED,
                    R.bool.def_mount_ums_notify_enabled);

            loadBooleanSetting(stmt, Settings.Secure.ACCESSIBILITY_SCRIPT_INJECTION,
                    R.bool.def_accessibility_script_injection);

            loadStringSetting(stmt, Settings.Secure.ACCESSIBILITY_WEB_CONTENT_KEY_BINDINGS,
                    R.string.def_accessibility_web_content_key_bindings);

            loadIntegerSetting(stmt, Settings.Secure.LONG_PRESS_TIMEOUT,
                    R.integer.def_long_press_timeout_millis);

            loadBooleanSetting(stmt, Settings.Secure.TOUCH_EXPLORATION_ENABLED,
                    R.bool.def_touch_exploration_enabled);

            loadBooleanSetting(stmt, Settings.Secure.ACCESSIBILITY_SPEAK_PASSWORD,
                    R.bool.def_accessibility_speak_password);

            loadStringSetting(stmt, Settings.Secure.ACCESSIBILITY_SCREEN_READER_URL,
                    R.string.def_accessibility_screen_reader_url);

            if (SystemProperties.getBoolean("ro.lockscreen.disable.default", false) == true) {
                loadSetting(stmt, Settings.System.LOCKSCREEN_DISABLED, "1");
            } else {
                loadBooleanSetting(stmt, Settings.System.LOCKSCREEN_DISABLED,
                        R.bool.def_lockscreen_disabled);
            }

            loadBooleanSetting(stmt, Settings.Secure.SCREENSAVER_ENABLED,
                    com.android.internal.R.bool.config_dreamsEnabledByDefault);
            loadBooleanSetting(stmt, Settings.Secure.SCREENSAVER_ACTIVATE_ON_DOCK,
                    com.android.internal.R.bool.config_dreamsActivatedOnDockByDefault);
            loadBooleanSetting(stmt, Settings.Secure.SCREENSAVER_ACTIVATE_ON_SLEEP,
                    com.android.internal.R.bool.config_dreamsActivatedOnSleepByDefault);
            loadStringSetting(stmt, Settings.Secure.SCREENSAVER_COMPONENTS,
                    com.android.internal.R.string.config_dreamsDefaultComponent);
            loadStringSetting(stmt, Settings.Secure.SCREENSAVER_DEFAULT_COMPONENT,
                    com.android.internal.R.string.config_dreamsDefaultComponent);

            loadBooleanSetting(stmt, Settings.Secure.ACCESSIBILITY_DISPLAY_MAGNIFICATION_ENABLED,
                    R.bool.def_accessibility_display_magnification_enabled);

            loadFractionSetting(stmt, Settings.Secure.ACCESSIBILITY_DISPLAY_MAGNIFICATION_SCALE,
                    R.fraction.def_accessibility_display_magnification_scale, 1);

            loadBooleanSetting(stmt,
                    Settings.Secure.ACCESSIBILITY_DISPLAY_MAGNIFICATION_AUTO_UPDATE,
                    R.bool.def_accessibility_display_magnification_auto_update);

            loadBooleanSetting(stmt, Settings.Secure.USER_SETUP_COMPLETE,
                    R.bool.def_user_setup_complete);

            loadStringSetting(stmt, Settings.Secure.IMMERSIVE_MODE_CONFIRMATIONS,
                        R.string.def_immersive_mode_confirmations);

            loadBooleanSetting(stmt, Settings.Secure.INSTALL_NON_MARKET_APPS,
                    R.bool.def_install_non_market_apps);

            loadBooleanSetting(stmt, Settings.Secure.WAKE_GESTURE_ENABLED,
                    R.bool.def_wake_gesture_enabled);

            loadIntegerSetting(stmt, Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS,
                    R.integer.def_lock_screen_show_notifications);

            loadBooleanSetting(stmt, Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS,
                    R.bool.def_lock_screen_allow_private_notifications);

            loadIntegerSetting(stmt, Settings.Secure.SLEEP_TIMEOUT,
                    R.integer.def_sleep_timeout);

            if (!TextUtils.isEmpty(mContext.getResources().getString(R.string.def_input_method))) {
                loadStringSetting(stmt, Settings.Secure.DEFAULT_INPUT_METHOD,
                        R.string.def_input_method);
            }

            if (!TextUtils.isEmpty(mContext.getResources().getString(
                    R.string.def_enable_input_methods))) {
                loadStringSetting(stmt, Settings.Secure.ENABLED_INPUT_METHODS,
                        R.string.def_enable_input_methods);
            }

            // for accessibility enabled
            loadStringSetting(stmt, Settings.Secure.ACCESSIBILITY_ENABLED,
                    R.integer.def_enable_accessiblity);
            loadStringSetting(stmt, Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES,
                    R.string.def_enable_accessiblity_services);
        } finally {
            if (stmt != null) stmt.close();
        }
    

上面的源码就是来加载xml·

    static String dbNameForUser(final int userHandle) {
        // The owner gets the unadorned db name;
        if (userHandle == UserHandle.USER_OWNER) {
            return DATABASE_NAME;
        } else {
            // Place the database in the user-specific data tree so that it's
            // cleaned up automatically when the user is deleted.
            File databaseFile = new File(
                    Environment.getUserSystemDirectory(userHandle), DATABASE_NAME);
            return databaseFile.getPath();
        }
    }
public DatabaseHelper(Context context, int userHandle) {
        super(context, dbNameForUser(userHandle), null, DATABASE_VERSION);
        mContext = context;
        mUserHandle = userHandle;
    }

这2个函数一起看,可以知道在Setting中就可以使用getContentResolver()去查询对应的字段
该数据库名就是从dbNameForUser函数中得到·

关闭相关电源控制

在这个过程中一定要注意,要将设备的电源管理给关闭了
在frameworks/base/services/core/java/com/android/server/power/PowerManagerService.java

private int getScreenOffTimeoutLocked(int sleepTimeout) {
        int timeout = mScreenOffTimeoutSetting;
    //bsp add for never sleep device (debug require).
        if (timeout < 0) {
            timeout = mMaximumScreenOffTimeoutFromDeviceAdmin;
        }
        if (isMaximumScreenOffTimeoutFromDeviceAdminEnforcedLocked()) {
            timeout = Math.min(timeout, mMaximumScreenOffTimeoutFromDeviceAdmin);
        }
}

你可能感兴趣的:(android默认设置项的修改)