android底层开发进阶(1)-默认设置项的修改

来源:互联网 发布:淘宝兔妈美国代购真假 编辑:程序博客网 时间:2024/05/29 17:01

android系统默认设置

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

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

<?xml version="1.0" encoding="utf-8"?><!--/** * Copyright (c) 2009, The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * *     http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */--><resources>    <bool name="def_dim_screen">true</bool>    <integer name="def_screen_off_timeout">-1</integer>    <integer name="def_sleep_timeout">-1</integer>    <bool name="def_airplane_mode_on">false</bool>    <!-- Comma-separated list of bluetooth, wifi, and cell. -->    <string name="def_airplane_mode_radios" translatable="false">cell,bluetooth,wifi,nfc,wimax</string>    <string name="airplane_mode_toggleable_radios" translatable="false">bluetooth,wifi,nfc</string>    <bool name="def_auto_time">true</bool>    <bool name="def_auto_time_zone">true</bool>    <bool name="def_accelerometer_rotation">true</bool>    <!-- Default screen brightness, from 0 to 255.  102 is 40%. -->    <integer name="def_screen_brightness">255</integer>    <bool name="def_screen_brightness_automatic_mode">false</bool>    <fraction name="def_window_animation_scale">100%</fraction>    <fraction name="def_window_transition_scale">100%</fraction>    <bool name="def_haptic_feedback">true</bool>    <bool name="def_bluetooth_on">false</bool>    <bool name="def_wifi_display_on">false</bool>    <bool name="def_install_non_market_apps">false</bool>    <bool name="def_package_verifier_enable">true</bool>    <!-- Comma-separated list of location providers.         Network location is off by default because it requires         user opt-in via Setup Wizard or Settings.    -->    <string name="def_location_providers_allowed" translatable="false">gps</string>    <bool name="assisted_gps_enabled">true</bool>    <string name="assisted_gps_configurable_list" translatable="false"></string>    <bool name="def_netstats_enabled">true</bool>    <bool name="def_usb_mass_storage_enabled">true</bool>    <bool name="def_wifi_on">false</bool>    <!-- 0 == never, 1 == only when plugged in, 2 == always -->    <integer name="def_wifi_sleep_policy">2</integer>    <bool name="def_networks_available_notification_on">true</bool>    <bool name="def_backup_enabled">false</bool>    <string name="def_backup_transport" translatable="false">android/com.android.internal.backup.LocalTransport</string>    <!-- Default value for whether or not to pulse the notification LED when there is a         pending notification -->    <bool name="def_notification_pulse">true</bool>    <bool name="def_mount_play_notification_snd">true</bool>    <bool name="def_mount_ums_autostart">false</bool>    <bool name="def_mount_ums_prompt">true</bool>    <bool name="def_mount_ums_notify_enabled">true</bool>    <!-- user interface sound effects -->    <integer name="def_power_sounds_enabled">1</integer>    <string name="def_low_battery_sound" translatable="false">/system/media/audio/ui/LowBattery.ogg</string>    <integer name="def_dock_sounds_enabled">0</integer>    <string name="def_desk_dock_sound" translatable="false">/system/media/audio/ui/Dock.ogg</string>    <string name="def_desk_undock_sound" translatable="false">/system/media/audio/ui/Undock.ogg</string>    <string name="def_car_dock_sound" translatable="false">/system/media/audio/ui/Dock.ogg</string>    <string name="def_car_undock_sound" translatable="false">/system/media/audio/ui/Undock.ogg</string>    <integer name="def_lockscreen_sounds_enabled">1</integer>    <string name="def_lock_sound" translatable="false">/system/media/audio/ui/Lock.ogg</string>    <string name="def_unlock_sound" translatable="false">/system/media/audio/ui/Unlock.ogg</string>    <string name="def_trusted_sound" translatable="false">/system/media/audio/ui/Trusted.ogg</string>    <string name="def_wireless_charging_started_sound" translatable="false">/system/media/audio/ui/WirelessChargingStarted.ogg</string>    <bool name="def_lockscreen_disabled">true</bool>    <bool name="def_device_provisioned">false</bool>    <integer name="def_dock_audio_media_enabled">1</integer>    <!-- Notifications use ringer volume -->    <bool name="def_notifications_use_ring_volume">true</bool>    <!-- Default for Settings.System.VIBRATE_IN_SILENT -->    <bool name="def_vibrate_in_silent">true</bool>    <!-- Default for Settings.Secure.ACCESSIBILITY_SCRIPT_INJECTION -->    <bool name="def_accessibility_script_injection">false</bool>    <!-- Default for Settings.Secure.ACCESSIBILITY_SPEAK_PASSWORD -->    <bool name="def_accessibility_speak_password">false</bool>    <!-- Default for Settings.Secure.ACCESSIBILITY_WEB_CONTENT_KEY_BINDINGS -->    <string name="def_accessibility_web_content_key_bindings" translatable="false">            <!-- DPAD/Trackball UP - traverse previous on current axis and send an event. -->            0x13=0x01000100;            <!-- DPAD/Trackball DOWN - traverse next on current axis and send an event. -->            0x14=0x01010100;            <!-- DPAD/Trackball LEFT - traverse previous on the character navigation axis and send event. -->            0x15=0x02000001;            <!-- DPAD/Trackball RIGHT - traverse next on the character navigation axis end send event. -->            0x16=0x02010001;            <!-- Alt+DPAD/Trackball UP - go to the top of the document. -->            0x200000013=0x02000601;            <!-- Alt+DPAD/Trackball DOWN - go to the bottom of the document. -->            0x200000014=0x02010601;            <!-- Alt+DPAD/Trackball LEFT - transition from an axis to another and sends an event.-->            <!-- Axis transitions: 2 -> 1; -->            0x200000015=0x03020101;            <!-- Alt+DPAD/Trackball RIGHT - transition from an axis to another and sends an event. -->            <!-- Axis transitions:  1 -> 2; -->            0x200000016=0x03010201;            <!-- Alt+g - go to the previous heading and send an event. -->            0x200000023=0x02000301;            <!-- Alt+h - go to the next heading and send an event. -->            0x200000024=0x02010301;            <!-- Alt+COMMA - transition to sentence navigation axis and send an event. -->            <!-- Axis transitions:  7 -> 2; -->            0x200000037=0x03070201;            <!-- Alt+PERIOD - transition to default web view behavior axis and send an event. -->            <!-- Axis transitions:  0 -> 7; 1 - > 7; 2 -> 7; -->            0x200000038=0x03000701:0x03010701:0x03020701;    </string>    <!-- Default for Settings.Secure.ACCESSIBILITY_SCRIPT_INJECTION_URL -->    <string name="def_accessibility_screen_reader_url" translatable="false">        https://ssl.gstatic.com/accessibility/javascript/android/AndroidVox_v1.js    </string>    <!-- Default for Settings.Secure.TOUCH_EXPLORATION_ENABLED -->    <bool name="def_touch_exploration_enabled">false</bool>    <!-- Default value for Settings.Secure.ACCESSIBILITY_DISPLAY_MAGNIFICATION_SCALE -->    <fraction name="def_accessibility_display_magnification_scale">200%</fraction>    <!-- Default value for Settings.Secure.ACCESSIBILITY_DISPLAY_MAGNIFICATION_ENABLED -->    <bool name="def_accessibility_display_magnification_enabled">false</bool>    <!-- Default value for Settings.Secure.ACCESSIBILITY_DISPLAY_MAGNIFICATION_AUTO_UPDATE -->    <bool name="def_accessibility_display_magnification_auto_update">true</bool>    <!-- Default for Settings.System.USER_ROTATION -->    <integer name="def_user_rotation">0</integer>    <!-- Default for Settings.Secure.DOWNLOAD_MAX_BYTES_OVER_MOBILE. <=0 if no limit -->    <integer name="def_download_manager_max_bytes_over_mobile">-1</integer>    <!-- Default for Settings.Secure.DOWNLOAD_RECOMMENDED_MAX_BYTES_OVER_MOBILE. <=0 if no limit -->    <integer name="def_download_manager_recommended_max_bytes_over_mobile">-1</integer>    <!-- Default for Settings.Secure.LONG_PRESS_TIMEOUT_MILLIS -->    <integer name="def_long_press_timeout_millis">500</integer>    <!-- Default for Settings.System.POINTER_SPEED -->    <integer name="def_pointer_speed">0</integer>    <!-- Default for DTMF tones enabled -->    <bool name="def_dtmf_tones_enabled">true</bool>    <!-- Default for UI touch sounds enabled -->    <bool name="def_sound_effects_enabled">true</bool>    <!-- Development settings -->    <bool name="def_stay_on_while_plugged_in">false</bool>    <!-- Number of retries for connecting to DHCP.         Value here is the same as WifiStateMachine.DEFAULT_MAX_DHCP_RETRIES -->    <integer name="def_max_dhcp_retries">9</integer>    <!-- Default for Settings.Secure.USER_SETUP_COMPLETE -->    <bool name="def_user_setup_complete">false</bool>    <!-- Default for Settings.Global.LOW_BATTERY_SOUND_TIMEOUT.         0 means no timeout; battery sounds will always play         >0 is milliseconds of screen-off time after which battery sounds will not play -->    <integer name="def_low_battery_sound_timeout">0</integer>    <!-- Default for Settings.System.VIBRATE_WHEN_RINGING enabled -->    <bool name="def_vibrate_when_ringing_enabled">false</bool>    <!-- Initial value for the Settings.Secure.IMMERSIVE_MODE_CONFIRMATIONS setting,         which is a comma separated list of packages that no longer need confirmation         for immersive mode.         Override to disable immersive mode confirmation for certain packages. -->    <string name="def_immersive_mode_confirmations" translatable="false"></string>    <!-- Default for Settings.Global.WIFI_SCAN_ALWAYS_AVAILABLE -->    <integer name="def_wifi_scan_always_available">0</integer>    <!-- Default for Settings.Secure.LOCK_SCREEN_SHOW_NOTIFICATIONS, 1==on -->    <integer name="def_lock_screen_show_notifications">1</integer>    <!-- Default for Settings.Secure.LOCK_SCREEN_ALLOW_PRIVATE_NOTIFICATIONS -->    <bool name="def_lock_screen_allow_private_notifications">true</bool>    <!-- Default for Settings.Global.HEADS_UP_NOTIFICATIONS_ENABLED, 1==on -->    <integer name="def_heads_up_enabled">1</integer>    <!-- Default for Settings.Global.DEVICE_NAME $1=MANUFACTURER $2=MODEL-->    <string name="def_device_name">%1$s %2$s</string>    <!-- Default for Settings.Global.DEVICE_NAME $1=MODEL-->    <string name="def_device_name_simple">%1$s</string>    <!-- Default for Settings.Secure.WAKE_GESTURE_ENABLED -->    <bool name="def_wake_gesture_enabled">true</bool>    <!-- Default for Settings.Global.GUEST_USER_ENABLED -->    <bool name="def_guest_user_enabled">true</bool></resources>

在此,找出几个做事例:

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

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

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

adb reboot bootloaderfastboot -wfastboot 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函数中得到·

0 0
原创粉丝点击