Home | History | Annotate | Download | only in settings
      1 /*
      2  * Copyright (C) 2007 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.providers.settings;
     18 
     19 import android.content.ComponentName;
     20 import android.content.ContentValues;
     21 import android.content.Context;
     22 import android.content.Intent;
     23 import android.content.pm.ActivityInfo;
     24 import android.content.pm.IPackageManager;
     25 import android.content.pm.PackageManager;
     26 import android.content.res.XmlResourceParser;
     27 import android.database.Cursor;
     28 import android.database.sqlite.SQLiteDatabase;
     29 import android.database.sqlite.SQLiteOpenHelper;
     30 import android.database.sqlite.SQLiteStatement;
     31 import android.media.AudioManager;
     32 import android.media.AudioService;
     33 import android.net.ConnectivityManager;
     34 import android.os.Environment;
     35 import android.os.RemoteException;
     36 import android.os.ServiceManager;
     37 import android.os.SystemProperties;
     38 import android.os.UserHandle;
     39 import android.provider.Settings;
     40 import android.provider.Settings.Global;
     41 import android.provider.Settings.Secure;
     42 import android.telephony.TelephonyManager;
     43 import android.text.TextUtils;
     44 import android.util.Log;
     45 
     46 import com.android.internal.content.PackageHelper;
     47 import com.android.internal.telephony.Phone;
     48 import com.android.internal.telephony.PhoneConstants;
     49 import com.android.internal.telephony.RILConstants;
     50 import com.android.internal.util.XmlUtils;
     51 import com.android.internal.widget.LockPatternUtils;
     52 import com.android.internal.widget.LockPatternView;
     53 
     54 import org.xmlpull.v1.XmlPullParser;
     55 import org.xmlpull.v1.XmlPullParserException;
     56 
     57 import java.io.File;
     58 import java.io.IOException;
     59 import java.util.HashSet;
     60 import java.util.List;
     61 
     62 /**
     63  * Database helper class for {@link SettingsProvider}.
     64  * Mostly just has a bit {@link #onCreate} to initialize the database.
     65  */
     66 public class DatabaseHelper extends SQLiteOpenHelper {
     67     private static final String TAG = "SettingsProvider";
     68     private static final String DATABASE_NAME = "settings.db";
     69 
     70     // Please, please please. If you update the database version, check to make sure the
     71     // database gets upgraded properly. At a minimum, please confirm that 'upgradeVersion'
     72     // is properly propagated through your change.  Not doing so will result in a loss of user
     73     // settings.
     74     private static final int DATABASE_VERSION = 97;
     75 
     76     private Context mContext;
     77     private int mUserHandle;
     78 
     79     private static final HashSet<String> mValidTables = new HashSet<String>();
     80 
     81     private static final String TABLE_SYSTEM = "system";
     82     private static final String TABLE_SECURE = "secure";
     83     private static final String TABLE_GLOBAL = "global";
     84 
     85     static {
     86         mValidTables.add(TABLE_SYSTEM);
     87         mValidTables.add(TABLE_SECURE);
     88         mValidTables.add(TABLE_GLOBAL);
     89         mValidTables.add("bluetooth_devices");
     90         mValidTables.add("bookmarks");
     91 
     92         // These are old.
     93         mValidTables.add("favorites");
     94         mValidTables.add("gservices");
     95         mValidTables.add("old_favorites");
     96     }
     97 
     98     static String dbNameForUser(final int userHandle) {
     99         // The owner gets the unadorned db name;
    100         if (userHandle == UserHandle.USER_OWNER) {
    101             return DATABASE_NAME;
    102         } else {
    103             // Place the database in the user-specific data tree so that it's
    104             // cleaned up automatically when the user is deleted.
    105             File databaseFile = new File(
    106                     Environment.getUserSystemDirectory(userHandle), DATABASE_NAME);
    107             return databaseFile.getPath();
    108         }
    109     }
    110 
    111     public DatabaseHelper(Context context, int userHandle) {
    112         super(context, dbNameForUser(userHandle), null, DATABASE_VERSION);
    113         mContext = context;
    114         mUserHandle = userHandle;
    115     }
    116 
    117     public static boolean isValidTable(String name) {
    118         return mValidTables.contains(name);
    119     }
    120 
    121     private void createSecureTable(SQLiteDatabase db) {
    122         db.execSQL("CREATE TABLE secure (" +
    123                 "_id INTEGER PRIMARY KEY AUTOINCREMENT," +
    124                 "name TEXT UNIQUE ON CONFLICT REPLACE," +
    125                 "value TEXT" +
    126                 ");");
    127         db.execSQL("CREATE INDEX secureIndex1 ON secure (name);");
    128     }
    129 
    130     private void createGlobalTable(SQLiteDatabase db) {
    131         db.execSQL("CREATE TABLE global (" +
    132                 "_id INTEGER PRIMARY KEY AUTOINCREMENT," +
    133                 "name TEXT UNIQUE ON CONFLICT REPLACE," +
    134                 "value TEXT" +
    135                 ");");
    136         db.execSQL("CREATE INDEX globalIndex1 ON global (name);");
    137     }
    138 
    139     @Override
    140     public void onCreate(SQLiteDatabase db) {
    141         db.execSQL("CREATE TABLE system (" +
    142                     "_id INTEGER PRIMARY KEY AUTOINCREMENT," +
    143                     "name TEXT UNIQUE ON CONFLICT REPLACE," +
    144                     "value TEXT" +
    145                     ");");
    146         db.execSQL("CREATE INDEX systemIndex1 ON system (name);");
    147 
    148         createSecureTable(db);
    149 
    150         // Only create the global table for the singleton 'owner' user
    151         if (mUserHandle == UserHandle.USER_OWNER) {
    152             createGlobalTable(db);
    153         }
    154 
    155         db.execSQL("CREATE TABLE bluetooth_devices (" +
    156                     "_id INTEGER PRIMARY KEY," +
    157                     "name TEXT," +
    158                     "addr TEXT," +
    159                     "channel INTEGER," +
    160                     "type INTEGER" +
    161                     ");");
    162 
    163         db.execSQL("CREATE TABLE bookmarks (" +
    164                     "_id INTEGER PRIMARY KEY," +
    165                     "title TEXT," +
    166                     "folder TEXT," +
    167                     "intent TEXT," +
    168                     "shortcut INTEGER," +
    169                     "ordering INTEGER" +
    170                     ");");
    171 
    172         db.execSQL("CREATE INDEX bookmarksIndex1 ON bookmarks (folder);");
    173         db.execSQL("CREATE INDEX bookmarksIndex2 ON bookmarks (shortcut);");
    174 
    175         // Populate bookmarks table with initial bookmarks
    176         boolean onlyCore = false;
    177         try {
    178             onlyCore = IPackageManager.Stub.asInterface(ServiceManager.getService(
    179                     "package")).isOnlyCoreApps();
    180         } catch (RemoteException e) {
    181         }
    182         if (!onlyCore) {
    183             loadBookmarks(db);
    184         }
    185 
    186         // Load initial volume levels into DB
    187         loadVolumeLevels(db);
    188 
    189         // Load inital settings values
    190         loadSettings(db);
    191     }
    192 
    193     @Override
    194     public void onUpgrade(SQLiteDatabase db, int oldVersion, int currentVersion) {
    195         Log.w(TAG, "Upgrading settings database from version " + oldVersion + " to "
    196                 + currentVersion);
    197 
    198         int upgradeVersion = oldVersion;
    199 
    200         // Pattern for upgrade blocks:
    201         //
    202         //    if (upgradeVersion == [the DATABASE_VERSION you set] - 1) {
    203         //        .. your upgrade logic..
    204         //        upgradeVersion = [the DATABASE_VERSION you set]
    205         //    }
    206 
    207         if (upgradeVersion == 20) {
    208             /*
    209              * Version 21 is part of the volume control refresh. There is no
    210              * longer a UI-visible for setting notification vibrate on/off (in
    211              * our design), but the functionality still exists. Force the
    212              * notification vibrate to on.
    213              */
    214             loadVibrateSetting(db, true);
    215 
    216             upgradeVersion = 21;
    217         }
    218 
    219         if (upgradeVersion < 22) {
    220             upgradeVersion = 22;
    221             // Upgrade the lock gesture storage location and format
    222             upgradeLockPatternLocation(db);
    223         }
    224 
    225         if (upgradeVersion < 23) {
    226             db.execSQL("UPDATE favorites SET iconResource=0 WHERE iconType=0");
    227             upgradeVersion = 23;
    228         }
    229 
    230         if (upgradeVersion == 23) {
    231             db.beginTransaction();
    232             try {
    233                 db.execSQL("ALTER TABLE favorites ADD spanX INTEGER");
    234                 db.execSQL("ALTER TABLE favorites ADD spanY INTEGER");
    235                 // Shortcuts, applications, folders
    236                 db.execSQL("UPDATE favorites SET spanX=1, spanY=1 WHERE itemType<=0");
    237                 // Photo frames, clocks
    238                 db.execSQL(
    239                     "UPDATE favorites SET spanX=2, spanY=2 WHERE itemType=1000 or itemType=1002");
    240                 // Search boxes
    241                 db.execSQL("UPDATE favorites SET spanX=4, spanY=1 WHERE itemType=1001");
    242                 db.setTransactionSuccessful();
    243             } finally {
    244                 db.endTransaction();
    245             }
    246             upgradeVersion = 24;
    247         }
    248 
    249         if (upgradeVersion == 24) {
    250             db.beginTransaction();
    251             try {
    252                 // The value of the constants for preferring wifi or preferring mobile have been
    253                 // swapped, so reload the default.
    254                 db.execSQL("DELETE FROM system WHERE name='network_preference'");
    255                 db.execSQL("INSERT INTO system ('name', 'value') values ('network_preference', '" +
    256                         ConnectivityManager.DEFAULT_NETWORK_PREFERENCE + "')");
    257                 db.setTransactionSuccessful();
    258             } finally {
    259                 db.endTransaction();
    260             }
    261             upgradeVersion = 25;
    262         }
    263 
    264         if (upgradeVersion == 25) {
    265             db.beginTransaction();
    266             try {
    267                 db.execSQL("ALTER TABLE favorites ADD uri TEXT");
    268                 db.execSQL("ALTER TABLE favorites ADD displayMode INTEGER");
    269                 db.setTransactionSuccessful();
    270             } finally {
    271                 db.endTransaction();
    272             }
    273             upgradeVersion = 26;
    274         }
    275 
    276         if (upgradeVersion == 26) {
    277             // This introduces the new secure settings table.
    278             db.beginTransaction();
    279             try {
    280                 createSecureTable(db);
    281                 db.setTransactionSuccessful();
    282             } finally {
    283                 db.endTransaction();
    284             }
    285             upgradeVersion = 27;
    286         }
    287 
    288         if (upgradeVersion == 27) {
    289             String[] settingsToMove = {
    290                     Settings.Secure.ADB_ENABLED,
    291                     Settings.Secure.ANDROID_ID,
    292                     Settings.Secure.BLUETOOTH_ON,
    293                     Settings.Secure.DATA_ROAMING,
    294                     Settings.Secure.DEVICE_PROVISIONED,
    295                     Settings.Secure.HTTP_PROXY,
    296                     Settings.Secure.INSTALL_NON_MARKET_APPS,
    297                     Settings.Secure.LOCATION_PROVIDERS_ALLOWED,
    298                     Settings.Secure.LOGGING_ID,
    299                     Settings.Secure.NETWORK_PREFERENCE,
    300                     Settings.Secure.PARENTAL_CONTROL_ENABLED,
    301                     Settings.Secure.PARENTAL_CONTROL_LAST_UPDATE,
    302                     Settings.Secure.PARENTAL_CONTROL_REDIRECT_URL,
    303                     Settings.Secure.SETTINGS_CLASSNAME,
    304                     Settings.Secure.USB_MASS_STORAGE_ENABLED,
    305                     Settings.Secure.USE_GOOGLE_MAIL,
    306                     Settings.Secure.WIFI_NETWORKS_AVAILABLE_NOTIFICATION_ON,
    307                     Settings.Secure.WIFI_NETWORKS_AVAILABLE_REPEAT_DELAY,
    308                     Settings.Secure.WIFI_NUM_OPEN_NETWORKS_KEPT,
    309                     Settings.Secure.WIFI_ON,
    310                     Settings.Secure.WIFI_WATCHDOG_ACCEPTABLE_PACKET_LOSS_PERCENTAGE,
    311                     Settings.Secure.WIFI_WATCHDOG_AP_COUNT,
    312                     Settings.Secure.WIFI_WATCHDOG_BACKGROUND_CHECK_DELAY_MS,
    313                     Settings.Secure.WIFI_WATCHDOG_BACKGROUND_CHECK_ENABLED,
    314                     Settings.Secure.WIFI_WATCHDOG_BACKGROUND_CHECK_TIMEOUT_MS,
    315                     Settings.Secure.WIFI_WATCHDOG_INITIAL_IGNORED_PING_COUNT,
    316                     Settings.Secure.WIFI_WATCHDOG_MAX_AP_CHECKS,
    317                     Settings.Secure.WIFI_WATCHDOG_ON,
    318                     Settings.Secure.WIFI_WATCHDOG_PING_COUNT,
    319                     Settings.Secure.WIFI_WATCHDOG_PING_DELAY_MS,
    320                     Settings.Secure.WIFI_WATCHDOG_PING_TIMEOUT_MS,
    321                 };
    322             moveSettingsToNewTable(db, TABLE_SYSTEM, TABLE_SECURE, settingsToMove, false);
    323             upgradeVersion = 28;
    324         }
    325 
    326         if (upgradeVersion == 28 || upgradeVersion == 29) {
    327             // Note: The upgrade to 28 was flawed since it didn't delete the old
    328             // setting first before inserting. Combining 28 and 29 with the
    329             // fixed version.
    330 
    331             // This upgrade adds the STREAM_NOTIFICATION type to the list of
    332             // types affected by ringer modes (silent, vibrate, etc.)
    333             db.beginTransaction();
    334             try {
    335                 db.execSQL("DELETE FROM system WHERE name='"
    336                         + Settings.System.MODE_RINGER_STREAMS_AFFECTED + "'");
    337                 int newValue = (1 << AudioManager.STREAM_RING)
    338                         | (1 << AudioManager.STREAM_NOTIFICATION)
    339                         | (1 << AudioManager.STREAM_SYSTEM);
    340                 db.execSQL("INSERT INTO system ('name', 'value') values ('"
    341                         + Settings.System.MODE_RINGER_STREAMS_AFFECTED + "', '"
    342                         + String.valueOf(newValue) + "')");
    343                 db.setTransactionSuccessful();
    344             } finally {
    345                 db.endTransaction();
    346             }
    347 
    348             upgradeVersion = 30;
    349         }
    350 
    351         if (upgradeVersion == 30) {
    352             /*
    353              * Upgrade 31 clears the title for all quick launch shortcuts so the
    354              * activities' titles will be resolved at display time. Also, the
    355              * folder is changed to '@quicklaunch'.
    356              */
    357             db.beginTransaction();
    358             try {
    359                 db.execSQL("UPDATE bookmarks SET folder = '@quicklaunch'");
    360                 db.execSQL("UPDATE bookmarks SET title = ''");
    361                 db.setTransactionSuccessful();
    362             } finally {
    363                 db.endTransaction();
    364             }
    365             upgradeVersion = 31;
    366         }
    367 
    368         if (upgradeVersion == 31) {
    369             /*
    370              * Animations are now managed in preferences, and may be
    371              * enabled or disabled based on product resources.
    372              */
    373             db.beginTransaction();
    374             SQLiteStatement stmt = null;
    375             try {
    376                 db.execSQL("DELETE FROM system WHERE name='"
    377                         + Settings.System.WINDOW_ANIMATION_SCALE + "'");
    378                 db.execSQL("DELETE FROM system WHERE name='"
    379                         + Settings.System.TRANSITION_ANIMATION_SCALE + "'");
    380                 stmt = db.compileStatement("INSERT INTO system(name,value)"
    381                         + " VALUES(?,?);");
    382                 loadDefaultAnimationSettings(stmt);
    383                 db.setTransactionSuccessful();
    384             } finally {
    385                 db.endTransaction();
    386                 if (stmt != null) stmt.close();
    387             }
    388             upgradeVersion = 32;
    389         }
    390 
    391         if (upgradeVersion == 32) {
    392             // The Wi-Fi watchdog SSID list is now seeded with the value of
    393             // the property ro.com.android.wifi-watchlist
    394             String wifiWatchList = SystemProperties.get("ro.com.android.wifi-watchlist");
    395             if (!TextUtils.isEmpty(wifiWatchList)) {
    396                 db.beginTransaction();
    397                 try {
    398                     db.execSQL("INSERT OR IGNORE INTO secure(name,value) values('" +
    399                             Settings.Secure.WIFI_WATCHDOG_WATCH_LIST + "','" +
    400                             wifiWatchList + "');");
    401                     db.setTransactionSuccessful();
    402                 } finally {
    403                     db.endTransaction();
    404                 }
    405             }
    406             upgradeVersion = 33;
    407         }
    408 
    409         if (upgradeVersion == 33) {
    410             // Set the default zoom controls to: tap-twice to bring up +/-
    411             db.beginTransaction();
    412             try {
    413                 db.execSQL("INSERT INTO system(name,value) values('zoom','2');");
    414                 db.setTransactionSuccessful();
    415             } finally {
    416                 db.endTransaction();
    417             }
    418             upgradeVersion = 34;
    419         }
    420 
    421         if (upgradeVersion == 34) {
    422             db.beginTransaction();
    423             SQLiteStatement stmt = null;
    424             try {
    425                 stmt = db.compileStatement("INSERT OR IGNORE INTO secure(name,value)"
    426                         + " VALUES(?,?);");
    427                 loadSecure35Settings(stmt);
    428                 db.setTransactionSuccessful();
    429             } finally {
    430                 db.endTransaction();
    431                 if (stmt != null) stmt.close();
    432             }
    433             upgradeVersion = 35;
    434         }
    435             // due to a botched merge from donut to eclair, the initialization of ASSISTED_GPS_ENABLED
    436             // was accidentally done out of order here.
    437             // to fix this, ASSISTED_GPS_ENABLED is now initialized while upgrading from 38 to 39,
    438             // and we intentionally do nothing from 35 to 36 now.
    439         if (upgradeVersion == 35) {
    440             upgradeVersion = 36;
    441         }
    442 
    443         if (upgradeVersion == 36) {
    444            // This upgrade adds the STREAM_SYSTEM_ENFORCED type to the list of
    445             // types affected by ringer modes (silent, vibrate, etc.)
    446             db.beginTransaction();
    447             try {
    448                 db.execSQL("DELETE FROM system WHERE name='"
    449                         + Settings.System.MODE_RINGER_STREAMS_AFFECTED + "'");
    450                 int newValue = (1 << AudioManager.STREAM_RING)
    451                         | (1 << AudioManager.STREAM_NOTIFICATION)
    452                         | (1 << AudioManager.STREAM_SYSTEM)
    453                         | (1 << AudioManager.STREAM_SYSTEM_ENFORCED);
    454                 db.execSQL("INSERT INTO system ('name', 'value') values ('"
    455                         + Settings.System.MODE_RINGER_STREAMS_AFFECTED + "', '"
    456                         + String.valueOf(newValue) + "')");
    457                 db.setTransactionSuccessful();
    458             } finally {
    459                 db.endTransaction();
    460             }
    461             upgradeVersion = 37;
    462         }
    463 
    464         if (upgradeVersion == 37) {
    465             db.beginTransaction();
    466             SQLiteStatement stmt = null;
    467             try {
    468                 stmt = db.compileStatement("INSERT OR IGNORE INTO system(name,value)"
    469                         + " VALUES(?,?);");
    470                 loadStringSetting(stmt, Settings.System.AIRPLANE_MODE_TOGGLEABLE_RADIOS,
    471                         R.string.airplane_mode_toggleable_radios);
    472                 db.setTransactionSuccessful();
    473             } finally {
    474                 db.endTransaction();
    475                 if (stmt != null) stmt.close();
    476             }
    477             upgradeVersion = 38;
    478         }
    479 
    480         if (upgradeVersion == 38) {
    481             db.beginTransaction();
    482             try {
    483                 String value =
    484                         mContext.getResources().getBoolean(R.bool.assisted_gps_enabled) ? "1" : "0";
    485                 db.execSQL("INSERT OR IGNORE INTO secure(name,value) values('" +
    486                         Settings.Global.ASSISTED_GPS_ENABLED + "','" + value + "');");
    487                 db.setTransactionSuccessful();
    488             } finally {
    489                 db.endTransaction();
    490             }
    491 
    492             upgradeVersion = 39;
    493         }
    494 
    495         if (upgradeVersion == 39) {
    496             upgradeAutoBrightness(db);
    497             upgradeVersion = 40;
    498         }
    499 
    500         if (upgradeVersion == 40) {
    501             /*
    502              * All animations are now turned on by default!
    503              */
    504             db.beginTransaction();
    505             SQLiteStatement stmt = null;
    506             try {
    507                 db.execSQL("DELETE FROM system WHERE name='"
    508                         + Settings.System.WINDOW_ANIMATION_SCALE + "'");
    509                 db.execSQL("DELETE FROM system WHERE name='"
    510                         + Settings.System.TRANSITION_ANIMATION_SCALE + "'");
    511                 stmt = db.compileStatement("INSERT INTO system(name,value)"
    512                         + " VALUES(?,?);");
    513                 loadDefaultAnimationSettings(stmt);
    514                 db.setTransactionSuccessful();
    515             } finally {
    516                 db.endTransaction();
    517                 if (stmt != null) stmt.close();
    518             }
    519             upgradeVersion = 41;
    520         }
    521 
    522         if (upgradeVersion == 41) {
    523             /*
    524              * Initialize newly public haptic feedback setting
    525              */
    526             db.beginTransaction();
    527             SQLiteStatement stmt = null;
    528             try {
    529                 db.execSQL("DELETE FROM system WHERE name='"
    530                         + Settings.System.HAPTIC_FEEDBACK_ENABLED + "'");
    531                 stmt = db.compileStatement("INSERT INTO system(name,value)"
    532                         + " VALUES(?,?);");
    533                 loadDefaultHapticSettings(stmt);
    534                 db.setTransactionSuccessful();
    535             } finally {
    536                 db.endTransaction();
    537                 if (stmt != null) stmt.close();
    538             }
    539             upgradeVersion = 42;
    540         }
    541 
    542         if (upgradeVersion == 42) {
    543             /*
    544              * Initialize new notification pulse setting
    545              */
    546             db.beginTransaction();
    547             SQLiteStatement stmt = null;
    548             try {
    549                 stmt = db.compileStatement("INSERT INTO system(name,value)"
    550                         + " VALUES(?,?);");
    551                 loadBooleanSetting(stmt, Settings.System.NOTIFICATION_LIGHT_PULSE,
    552                         R.bool.def_notification_pulse);
    553                 db.setTransactionSuccessful();
    554             } finally {
    555                 db.endTransaction();
    556                 if (stmt != null) stmt.close();
    557             }
    558             upgradeVersion = 43;
    559         }
    560 
    561         if (upgradeVersion == 43) {
    562             /*
    563              * This upgrade stores bluetooth volume separately from voice volume
    564              */
    565             db.beginTransaction();
    566             SQLiteStatement stmt = null;
    567             try {
    568                 stmt = db.compileStatement("INSERT OR IGNORE INTO system(name,value)"
    569                         + " VALUES(?,?);");
    570                 loadSetting(stmt, Settings.System.VOLUME_BLUETOOTH_SCO,
    571                         AudioManager.DEFAULT_STREAM_VOLUME[AudioManager.STREAM_BLUETOOTH_SCO]);
    572                 db.setTransactionSuccessful();
    573             } finally {
    574                 db.endTransaction();
    575                 if (stmt != null) stmt.close();
    576             }
    577             upgradeVersion = 44;
    578         }
    579 
    580         if (upgradeVersion == 44) {
    581             /*
    582              * Gservices was moved into vendor/google.
    583              */
    584             db.execSQL("DROP TABLE IF EXISTS gservices");
    585             db.execSQL("DROP INDEX IF EXISTS gservicesIndex1");
    586             upgradeVersion = 45;
    587         }
    588 
    589         if (upgradeVersion == 45) {
    590              /*
    591               * New settings for MountService
    592               */
    593             db.beginTransaction();
    594             try {
    595                 db.execSQL("INSERT INTO secure(name,value) values('" +
    596                         Settings.Secure.MOUNT_PLAY_NOTIFICATION_SND + "','1');");
    597                 db.execSQL("INSERT INTO secure(name,value) values('" +
    598                         Settings.Secure.MOUNT_UMS_AUTOSTART + "','0');");
    599                 db.execSQL("INSERT INTO secure(name,value) values('" +
    600                         Settings.Secure.MOUNT_UMS_PROMPT + "','1');");
    601                 db.execSQL("INSERT INTO secure(name,value) values('" +
    602                         Settings.Secure.MOUNT_UMS_NOTIFY_ENABLED + "','1');");
    603                 db.setTransactionSuccessful();
    604             } finally {
    605                 db.endTransaction();
    606             }
    607             upgradeVersion = 46;
    608         }
    609 
    610         if (upgradeVersion == 46) {
    611             /*
    612              * The password mode constants have changed; reset back to no
    613              * password.
    614              */
    615             db.beginTransaction();
    616             try {
    617                 db.execSQL("DELETE FROM system WHERE name='lockscreen.password_type';");
    618                 db.setTransactionSuccessful();
    619             } finally {
    620                 db.endTransaction();
    621             }
    622            upgradeVersion = 47;
    623        }
    624 
    625 
    626         if (upgradeVersion == 47) {
    627             /*
    628              * The password mode constants have changed again; reset back to no
    629              * password.
    630              */
    631             db.beginTransaction();
    632             try {
    633                 db.execSQL("DELETE FROM system WHERE name='lockscreen.password_type';");
    634                 db.setTransactionSuccessful();
    635             } finally {
    636                 db.endTransaction();
    637             }
    638            upgradeVersion = 48;
    639        }
    640 
    641        if (upgradeVersion == 48) {
    642            /*
    643             * Default recognition service no longer initialized here,
    644             * moved to RecognitionManagerService.
    645             */
    646            upgradeVersion = 49;
    647        }
    648 
    649        if (upgradeVersion == 49) {
    650            /*
    651             * New settings for new user interface noises.
    652             */
    653            db.beginTransaction();
    654            SQLiteStatement stmt = null;
    655            try {
    656                 stmt = db.compileStatement("INSERT INTO system(name,value)"
    657                         + " VALUES(?,?);");
    658                 loadUISoundEffectsSettings(stmt);
    659                 db.setTransactionSuccessful();
    660             } finally {
    661                 db.endTransaction();
    662                 if (stmt != null) stmt.close();
    663             }
    664 
    665            upgradeVersion = 50;
    666        }
    667 
    668        if (upgradeVersion == 50) {
    669            /*
    670             * Install location no longer initiated here.
    671             */
    672            upgradeVersion = 51;
    673        }
    674 
    675        if (upgradeVersion == 51) {
    676            /* Move the lockscreen related settings to Secure, including some private ones. */
    677            String[] settingsToMove = {
    678                    Secure.LOCK_PATTERN_ENABLED,
    679                    Secure.LOCK_PATTERN_VISIBLE,
    680                    Secure.LOCK_PATTERN_TACTILE_FEEDBACK_ENABLED,
    681                    "lockscreen.password_type",
    682                    "lockscreen.lockoutattemptdeadline",
    683                    "lockscreen.patterneverchosen",
    684                    "lock_pattern_autolock",
    685                    "lockscreen.lockedoutpermanently",
    686                    "lockscreen.password_salt"
    687            };
    688            moveSettingsToNewTable(db, TABLE_SYSTEM, TABLE_SECURE, settingsToMove, false);
    689            upgradeVersion = 52;
    690        }
    691 
    692         if (upgradeVersion == 52) {
    693             // new vibration/silent mode settings
    694             db.beginTransaction();
    695             SQLiteStatement stmt = null;
    696             try {
    697                 stmt = db.compileStatement("INSERT INTO system(name,value)"
    698                         + " VALUES(?,?);");
    699                 loadBooleanSetting(stmt, Settings.System.VIBRATE_IN_SILENT,
    700                         R.bool.def_vibrate_in_silent);
    701                 db.setTransactionSuccessful();
    702             } finally {
    703                 db.endTransaction();
    704                 if (stmt != null) stmt.close();
    705             }
    706 
    707             upgradeVersion = 53;
    708         }
    709 
    710         if (upgradeVersion == 53) {
    711             /*
    712              * New settings for set install location UI no longer initiated here.
    713              */
    714             upgradeVersion = 54;
    715         }
    716 
    717         if (upgradeVersion == 54) {
    718             /*
    719              * Update the screen timeout value if set to never
    720              */
    721             db.beginTransaction();
    722             try {
    723                 upgradeScreenTimeoutFromNever(db);
    724                 db.setTransactionSuccessful();
    725             } finally {
    726                 db.endTransaction();
    727             }
    728 
    729             upgradeVersion = 55;
    730         }
    731 
    732         if (upgradeVersion == 55) {
    733             /* Move the install location settings. */
    734             String[] settingsToMove = {
    735                     Global.SET_INSTALL_LOCATION,
    736                     Global.DEFAULT_INSTALL_LOCATION
    737             };
    738             moveSettingsToNewTable(db, TABLE_SYSTEM, TABLE_SECURE, settingsToMove, false);
    739             db.beginTransaction();
    740             SQLiteStatement stmt = null;
    741             try {
    742                 stmt = db.compileStatement("INSERT INTO system(name,value)"
    743                         + " VALUES(?,?);");
    744                 loadSetting(stmt, Global.SET_INSTALL_LOCATION, 0);
    745                 loadSetting(stmt, Global.DEFAULT_INSTALL_LOCATION,
    746                         PackageHelper.APP_INSTALL_AUTO);
    747                 db.setTransactionSuccessful();
    748              } finally {
    749                  db.endTransaction();
    750                  if (stmt != null) stmt.close();
    751              }
    752             upgradeVersion = 56;
    753         }
    754 
    755         if (upgradeVersion == 56) {
    756             /*
    757              * Add Bluetooth to list of toggleable radios in airplane mode
    758              */
    759             db.beginTransaction();
    760             SQLiteStatement stmt = null;
    761             try {
    762                 db.execSQL("DELETE FROM system WHERE name='"
    763                         + Settings.System.AIRPLANE_MODE_TOGGLEABLE_RADIOS + "'");
    764                 stmt = db.compileStatement("INSERT OR IGNORE INTO system(name,value)"
    765                         + " VALUES(?,?);");
    766                 loadStringSetting(stmt, Settings.System.AIRPLANE_MODE_TOGGLEABLE_RADIOS,
    767                         R.string.airplane_mode_toggleable_radios);
    768                 db.setTransactionSuccessful();
    769             } finally {
    770                 db.endTransaction();
    771                 if (stmt != null) stmt.close();
    772             }
    773             upgradeVersion = 57;
    774         }
    775 
    776         /************* The following are Honeycomb changes ************/
    777 
    778         if (upgradeVersion == 57) {
    779             /*
    780              * New settings to:
    781              *  1. Enable injection of accessibility scripts in WebViews.
    782              *  2. Define the key bindings for traversing web content in WebViews.
    783              */
    784             db.beginTransaction();
    785             SQLiteStatement stmt = null;
    786             try {
    787                 stmt = db.compileStatement("INSERT INTO secure(name,value)"
    788                         + " VALUES(?,?);");
    789                 loadBooleanSetting(stmt, Settings.Secure.ACCESSIBILITY_SCRIPT_INJECTION,
    790                         R.bool.def_accessibility_script_injection);
    791                 stmt.close();
    792                 stmt = db.compileStatement("INSERT INTO secure(name,value)"
    793                         + " VALUES(?,?);");
    794                 loadStringSetting(stmt, Settings.Secure.ACCESSIBILITY_WEB_CONTENT_KEY_BINDINGS,
    795                         R.string.def_accessibility_web_content_key_bindings);
    796                 db.setTransactionSuccessful();
    797             } finally {
    798                 db.endTransaction();
    799                 if (stmt != null) stmt.close();
    800             }
    801             upgradeVersion = 58;
    802         }
    803 
    804         if (upgradeVersion == 58) {
    805             /* Add default for new Auto Time Zone */
    806             int autoTimeValue = getIntValueFromSystem(db, Settings.System.AUTO_TIME, 0);
    807             db.beginTransaction();
    808             SQLiteStatement stmt = null;
    809             try {
    810                 stmt = db.compileStatement("INSERT INTO system(name,value)" + " VALUES(?,?);");
    811                 loadSetting(stmt, Settings.System.AUTO_TIME_ZONE,
    812                         autoTimeValue); // Sync timezone to NITZ if auto_time was enabled
    813                 db.setTransactionSuccessful();
    814             } finally {
    815                 db.endTransaction();
    816                 if (stmt != null) stmt.close();
    817             }
    818             upgradeVersion = 59;
    819         }
    820 
    821         if (upgradeVersion == 59) {
    822             // Persistence for the rotation lock feature.
    823             db.beginTransaction();
    824             SQLiteStatement stmt = null;
    825             try {
    826                 stmt = db.compileStatement("INSERT INTO system(name,value)"
    827                         + " VALUES(?,?);");
    828                 loadBooleanSetting(stmt, Settings.System.USER_ROTATION,
    829                         R.integer.def_user_rotation); // should be zero degrees
    830                 db.setTransactionSuccessful();
    831             } finally {
    832                 db.endTransaction();
    833                 if (stmt != null) stmt.close();
    834             }
    835             upgradeVersion = 60;
    836         }
    837 
    838         if (upgradeVersion == 60) {
    839             // Don't do this for upgrades from Gingerbread
    840             // Were only required for intra-Honeycomb upgrades for testing
    841             // upgradeScreenTimeout(db);
    842             upgradeVersion = 61;
    843         }
    844 
    845         if (upgradeVersion == 61) {
    846             // Don't do this for upgrades from Gingerbread
    847             // Were only required for intra-Honeycomb upgrades for testing
    848             // upgradeScreenTimeout(db);
    849             upgradeVersion = 62;
    850         }
    851 
    852         // Change the default for screen auto-brightness mode
    853         if (upgradeVersion == 62) {
    854             // Don't do this for upgrades from Gingerbread
    855             // Were only required for intra-Honeycomb upgrades for testing
    856             // upgradeAutoBrightness(db);
    857             upgradeVersion = 63;
    858         }
    859 
    860         if (upgradeVersion == 63) {
    861             // This upgrade adds the STREAM_MUSIC type to the list of
    862              // types affected by ringer modes (silent, vibrate, etc.)
    863              db.beginTransaction();
    864              try {
    865                  db.execSQL("DELETE FROM system WHERE name='"
    866                          + Settings.System.MODE_RINGER_STREAMS_AFFECTED + "'");
    867                  int newValue = (1 << AudioManager.STREAM_RING)
    868                          | (1 << AudioManager.STREAM_NOTIFICATION)
    869                          | (1 << AudioManager.STREAM_SYSTEM)
    870                          | (1 << AudioManager.STREAM_SYSTEM_ENFORCED)
    871                          | (1 << AudioManager.STREAM_MUSIC);
    872                  db.execSQL("INSERT INTO system ('name', 'value') values ('"
    873                          + Settings.System.MODE_RINGER_STREAMS_AFFECTED + "', '"
    874                          + String.valueOf(newValue) + "')");
    875                  db.setTransactionSuccessful();
    876              } finally {
    877                  db.endTransaction();
    878              }
    879              upgradeVersion = 64;
    880          }
    881 
    882         if (upgradeVersion == 64) {
    883             // New setting to configure the long press timeout.
    884             db.beginTransaction();
    885             SQLiteStatement stmt = null;
    886             try {
    887                 stmt = db.compileStatement("INSERT INTO secure(name,value)"
    888                         + " VALUES(?,?);");
    889                 loadIntegerSetting(stmt, Settings.Secure.LONG_PRESS_TIMEOUT,
    890                         R.integer.def_long_press_timeout_millis);
    891                 stmt.close();
    892                 db.setTransactionSuccessful();
    893             } finally {
    894                 db.endTransaction();
    895                 if (stmt != null) stmt.close();
    896             }
    897             upgradeVersion = 65;
    898         }
    899 
    900         /************* The following are Ice Cream Sandwich changes ************/
    901 
    902         if (upgradeVersion == 65) {
    903             /*
    904              * Animations are removed from Settings. Turned on by default
    905              */
    906             db.beginTransaction();
    907             SQLiteStatement stmt = null;
    908             try {
    909                 db.execSQL("DELETE FROM system WHERE name='"
    910                         + Settings.System.WINDOW_ANIMATION_SCALE + "'");
    911                 db.execSQL("DELETE FROM system WHERE name='"
    912                         + Settings.System.TRANSITION_ANIMATION_SCALE + "'");
    913                 stmt = db.compileStatement("INSERT INTO system(name,value)"
    914                         + " VALUES(?,?);");
    915                 loadDefaultAnimationSettings(stmt);
    916                 db.setTransactionSuccessful();
    917             } finally {
    918                 db.endTransaction();
    919                 if (stmt != null) stmt.close();
    920             }
    921             upgradeVersion = 66;
    922         }
    923 
    924         if (upgradeVersion == 66) {
    925             // This upgrade makes sure that MODE_RINGER_STREAMS_AFFECTED is set
    926             // according to device voice capability
    927             db.beginTransaction();
    928             try {
    929                 int ringerModeAffectedStreams = (1 << AudioManager.STREAM_RING) |
    930                                                 (1 << AudioManager.STREAM_NOTIFICATION) |
    931                                                 (1 << AudioManager.STREAM_SYSTEM) |
    932                                                 (1 << AudioManager.STREAM_SYSTEM_ENFORCED);
    933                 if (!mContext.getResources().getBoolean(
    934                         com.android.internal.R.bool.config_voice_capable)) {
    935                     ringerModeAffectedStreams |= (1 << AudioManager.STREAM_MUSIC);
    936                 }
    937                 db.execSQL("DELETE FROM system WHERE name='"
    938                         + Settings.System.MODE_RINGER_STREAMS_AFFECTED + "'");
    939                 db.execSQL("INSERT INTO system ('name', 'value') values ('"
    940                         + Settings.System.MODE_RINGER_STREAMS_AFFECTED + "', '"
    941                         + String.valueOf(ringerModeAffectedStreams) + "')");
    942                 db.setTransactionSuccessful();
    943             } finally {
    944                 db.endTransaction();
    945             }
    946             upgradeVersion = 67;
    947         }
    948 
    949         if (upgradeVersion == 67) {
    950             // New setting to enable touch exploration.
    951             db.beginTransaction();
    952             SQLiteStatement stmt = null;
    953             try {
    954                 stmt = db.compileStatement("INSERT INTO secure(name,value)"
    955                         + " VALUES(?,?);");
    956                 loadBooleanSetting(stmt, Settings.Secure.TOUCH_EXPLORATION_ENABLED,
    957                         R.bool.def_touch_exploration_enabled);
    958                 stmt.close();
    959                 db.setTransactionSuccessful();
    960             } finally {
    961                 db.endTransaction();
    962                 if (stmt != null) stmt.close();
    963             }
    964             upgradeVersion = 68;
    965         }
    966 
    967         if (upgradeVersion == 68) {
    968             // Enable all system sounds by default
    969             db.beginTransaction();
    970             try {
    971                 db.execSQL("DELETE FROM system WHERE name='"
    972                         + Settings.System.NOTIFICATIONS_USE_RING_VOLUME + "'");
    973                 db.setTransactionSuccessful();
    974             } finally {
    975                 db.endTransaction();
    976             }
    977             upgradeVersion = 69;
    978         }
    979 
    980         if (upgradeVersion == 69) {
    981             // Add RADIO_NFC to AIRPLANE_MODE_RADIO and AIRPLANE_MODE_TOGGLEABLE_RADIOS
    982             String airplaneRadios = mContext.getResources().getString(
    983                     R.string.def_airplane_mode_radios);
    984             String toggleableRadios = mContext.getResources().getString(
    985                     R.string.airplane_mode_toggleable_radios);
    986             db.beginTransaction();
    987             try {
    988                 db.execSQL("UPDATE system SET value='" + airplaneRadios + "' " +
    989                         "WHERE name='" + Settings.System.AIRPLANE_MODE_RADIOS + "'");
    990                 db.execSQL("UPDATE system SET value='" + toggleableRadios + "' " +
    991                         "WHERE name='" + Settings.System.AIRPLANE_MODE_TOGGLEABLE_RADIOS + "'");
    992                 db.setTransactionSuccessful();
    993             } finally {
    994                 db.endTransaction();
    995             }
    996             upgradeVersion = 70;
    997         }
    998 
    999         if (upgradeVersion == 70) {
   1000             // Update all built-in bookmarks.  Some of the package names have changed.
   1001             loadBookmarks(db);
   1002             upgradeVersion = 71;
   1003         }
   1004 
   1005         if (upgradeVersion == 71) {
   1006              // New setting to specify whether to speak passwords in accessibility mode.
   1007             db.beginTransaction();
   1008             SQLiteStatement stmt = null;
   1009             try {
   1010                 stmt = db.compileStatement("INSERT INTO secure(name,value)"
   1011                         + " VALUES(?,?);");
   1012                 loadBooleanSetting(stmt, Settings.Secure.ACCESSIBILITY_SPEAK_PASSWORD,
   1013                         R.bool.def_accessibility_speak_password);
   1014                 db.setTransactionSuccessful();
   1015             } finally {
   1016                 db.endTransaction();
   1017                 if (stmt != null) stmt.close();
   1018             }
   1019             upgradeVersion = 72;
   1020         }
   1021 
   1022         if (upgradeVersion == 72) {
   1023             // update vibration settings
   1024             db.beginTransaction();
   1025             SQLiteStatement stmt = null;
   1026             try {
   1027                 stmt = db.compileStatement("INSERT OR REPLACE INTO system(name,value)"
   1028                         + " VALUES(?,?);");
   1029                 loadBooleanSetting(stmt, Settings.System.VIBRATE_IN_SILENT,
   1030                         R.bool.def_vibrate_in_silent);
   1031                 db.setTransactionSuccessful();
   1032             } finally {
   1033                 db.endTransaction();
   1034                 if (stmt != null) stmt.close();
   1035             }
   1036             upgradeVersion = 73;
   1037         }
   1038 
   1039         if (upgradeVersion == 73) {
   1040             upgradeVibrateSettingFromNone(db);
   1041             upgradeVersion = 74;
   1042         }
   1043 
   1044         if (upgradeVersion == 74) {
   1045             // URL from which WebView loads a JavaScript based screen-reader.
   1046             db.beginTransaction();
   1047             SQLiteStatement stmt = null;
   1048             try {
   1049                 stmt = db.compileStatement("INSERT INTO secure(name,value) VALUES(?,?);");
   1050                 loadStringSetting(stmt, Settings.Secure.ACCESSIBILITY_SCREEN_READER_URL,
   1051                         R.string.def_accessibility_screen_reader_url);
   1052                 db.setTransactionSuccessful();
   1053             } finally {
   1054                 db.endTransaction();
   1055                 if (stmt != null) stmt.close();
   1056             }
   1057             upgradeVersion = 75;
   1058         }
   1059         if (upgradeVersion == 75) {
   1060             db.beginTransaction();
   1061             SQLiteStatement stmt = null;
   1062             Cursor c = null;
   1063             try {
   1064                 c = db.query(TABLE_SECURE, new String[] {"_id", "value"},
   1065                         "name='lockscreen.disabled'",
   1066                         null, null, null, null);
   1067                 // only set default if it has not yet been set
   1068                 if (c == null || c.getCount() == 0) {
   1069                     stmt = db.compileStatement("INSERT INTO system(name,value)"
   1070                             + " VALUES(?,?);");
   1071                     loadBooleanSetting(stmt, Settings.System.LOCKSCREEN_DISABLED,
   1072                             R.bool.def_lockscreen_disabled);
   1073                 }
   1074                 db.setTransactionSuccessful();
   1075             } finally {
   1076                 db.endTransaction();
   1077                 if (c != null) c.close();
   1078                 if (stmt != null) stmt.close();
   1079             }
   1080             upgradeVersion = 76;
   1081         }
   1082 
   1083         /************* The following are Jelly Bean changes ************/
   1084 
   1085         if (upgradeVersion == 76) {
   1086             // Removed VIBRATE_IN_SILENT setting
   1087             db.beginTransaction();
   1088             try {
   1089                 db.execSQL("DELETE FROM system WHERE name='"
   1090                                 + Settings.System.VIBRATE_IN_SILENT + "'");
   1091                 db.setTransactionSuccessful();
   1092             } finally {
   1093                 db.endTransaction();
   1094             }
   1095 
   1096             upgradeVersion = 77;
   1097         }
   1098 
   1099         if (upgradeVersion == 77) {
   1100             // Introduce "vibrate when ringing" setting
   1101             loadVibrateWhenRingingSetting(db);
   1102 
   1103             upgradeVersion = 78;
   1104         }
   1105 
   1106         if (upgradeVersion == 78) {
   1107             // The JavaScript based screen-reader URL changes in JellyBean.
   1108             db.beginTransaction();
   1109             SQLiteStatement stmt = null;
   1110             try {
   1111                 stmt = db.compileStatement("INSERT OR REPLACE INTO secure(name,value)"
   1112                         + " VALUES(?,?);");
   1113                 loadStringSetting(stmt, Settings.Secure.ACCESSIBILITY_SCREEN_READER_URL,
   1114                         R.string.def_accessibility_screen_reader_url);
   1115                 db.setTransactionSuccessful();
   1116             } finally {
   1117                 db.endTransaction();
   1118                 if (stmt != null) stmt.close();
   1119             }
   1120             upgradeVersion = 79;
   1121         }
   1122 
   1123         if (upgradeVersion == 79) {
   1124             // Before touch exploration was a global setting controlled by the user
   1125             // via the UI. However, if the enabled accessibility services do not
   1126             // handle touch exploration mode, enabling it makes no sense. Therefore,
   1127             // now the services request touch exploration mode and the user is
   1128             // presented with a dialog to allow that and if she does we store that
   1129             // in the database. As a result of this change a user that has enabled
   1130             // accessibility, touch exploration, and some accessibility services
   1131             // may lose touch exploration state, thus rendering the device useless
   1132             // unless sighted help is provided, since the enabled service(s) are
   1133             // not in the list of services to which the user granted a permission
   1134             // to put the device in touch explore mode. Here we are allowing all
   1135             // enabled accessibility services to toggle touch exploration provided
   1136             // accessibility and touch exploration are enabled and no services can
   1137             // toggle touch exploration. Note that the user has already manually
   1138             // enabled the services and touch exploration which means the she has
   1139             // given consent to have these services work in touch exploration mode.
   1140             final boolean accessibilityEnabled = getIntValueFromTable(db, TABLE_SECURE,
   1141                     Settings.Secure.ACCESSIBILITY_ENABLED, 0) == 1;
   1142             final boolean touchExplorationEnabled = getIntValueFromTable(db, TABLE_SECURE,
   1143                     Settings.Secure.TOUCH_EXPLORATION_ENABLED, 0) == 1;
   1144             if (accessibilityEnabled && touchExplorationEnabled) {
   1145                 String enabledServices = getStringValueFromTable(db, TABLE_SECURE,
   1146                         Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES, "");
   1147                 String touchExplorationGrantedServices = getStringValueFromTable(db, TABLE_SECURE,
   1148                         Settings.Secure.TOUCH_EXPLORATION_GRANTED_ACCESSIBILITY_SERVICES, "");
   1149                 if (TextUtils.isEmpty(touchExplorationGrantedServices)
   1150                         && !TextUtils.isEmpty(enabledServices)) {
   1151                     SQLiteStatement stmt = null;
   1152                     try {
   1153                         db.beginTransaction();
   1154                         stmt = db.compileStatement("INSERT OR REPLACE INTO secure(name,value)"
   1155                                 + " VALUES(?,?);");
   1156                         loadSetting(stmt,
   1157                                 Settings.Secure.TOUCH_EXPLORATION_GRANTED_ACCESSIBILITY_SERVICES,
   1158                                 enabledServices);
   1159                         db.setTransactionSuccessful();
   1160                     } finally {
   1161                         db.endTransaction();
   1162                         if (stmt != null) stmt.close();
   1163                     }
   1164                 }
   1165             }
   1166             upgradeVersion = 80;
   1167         }
   1168 
   1169         // vvv Jelly Bean MR1 changes begin here vvv
   1170 
   1171         if (upgradeVersion == 80) {
   1172             // update screensaver settings
   1173             db.beginTransaction();
   1174             SQLiteStatement stmt = null;
   1175             try {
   1176                 stmt = db.compileStatement("INSERT OR REPLACE INTO secure(name,value)"
   1177                         + " VALUES(?,?);");
   1178                 loadBooleanSetting(stmt, Settings.Secure.SCREENSAVER_ENABLED,
   1179                         com.android.internal.R.bool.config_dreamsEnabledByDefault);
   1180                 loadBooleanSetting(stmt, Settings.Secure.SCREENSAVER_ACTIVATE_ON_DOCK,
   1181                         com.android.internal.R.bool.config_dreamsActivatedOnDockByDefault);
   1182                 loadBooleanSetting(stmt, Settings.Secure.SCREENSAVER_ACTIVATE_ON_SLEEP,
   1183                         com.android.internal.R.bool.config_dreamsActivatedOnSleepByDefault);
   1184                 loadStringSetting(stmt, Settings.Secure.SCREENSAVER_COMPONENTS,
   1185                         com.android.internal.R.string.config_dreamsDefaultComponent);
   1186                 loadStringSetting(stmt, Settings.Secure.SCREENSAVER_DEFAULT_COMPONENT,
   1187                         com.android.internal.R.string.config_dreamsDefaultComponent);
   1188 
   1189                 db.setTransactionSuccessful();
   1190             } finally {
   1191                 db.endTransaction();
   1192                 if (stmt != null) stmt.close();
   1193             }
   1194             upgradeVersion = 81;
   1195         }
   1196 
   1197         if (upgradeVersion == 81) {
   1198             // Add package verification setting
   1199             db.beginTransaction();
   1200             SQLiteStatement stmt = null;
   1201             try {
   1202                 stmt = db.compileStatement("INSERT OR REPLACE INTO secure(name,value)"
   1203                         + " VALUES(?,?);");
   1204                 loadBooleanSetting(stmt, Settings.Global.PACKAGE_VERIFIER_ENABLE,
   1205                         R.bool.def_package_verifier_enable);
   1206                 db.setTransactionSuccessful();
   1207             } finally {
   1208                 db.endTransaction();
   1209                 if (stmt != null) stmt.close();
   1210             }
   1211             upgradeVersion = 82;
   1212         }
   1213 
   1214         if (upgradeVersion == 82) {
   1215             // Move to per-user settings dbs
   1216             if (mUserHandle == UserHandle.USER_OWNER) {
   1217 
   1218                 db.beginTransaction();
   1219                 SQLiteStatement stmt = null;
   1220                 try {
   1221                     // Migrate now-global settings. Note that this happens before
   1222                     // new users can be created.
   1223                     createGlobalTable(db);
   1224                     String[] settingsToMove = hashsetToStringArray(SettingsProvider.sSystemGlobalKeys);
   1225                     moveSettingsToNewTable(db, TABLE_SYSTEM, TABLE_GLOBAL, settingsToMove, false);
   1226                     settingsToMove = hashsetToStringArray(SettingsProvider.sSecureGlobalKeys);
   1227                     moveSettingsToNewTable(db, TABLE_SECURE, TABLE_GLOBAL, settingsToMove, false);
   1228 
   1229                     db.setTransactionSuccessful();
   1230                 } finally {
   1231                     db.endTransaction();
   1232                     if (stmt != null) stmt.close();
   1233                 }
   1234             }
   1235             upgradeVersion = 83;
   1236         }
   1237 
   1238         if (upgradeVersion == 83) {
   1239             // 1. Setting whether screen magnification is enabled.
   1240             // 2. Setting for screen magnification scale.
   1241             // 3. Setting for screen magnification auto update.
   1242             db.beginTransaction();
   1243             SQLiteStatement stmt = null;
   1244             try {
   1245                 stmt = db.compileStatement("INSERT INTO secure(name,value) VALUES(?,?);");
   1246                 loadBooleanSetting(stmt,
   1247                         Settings.Secure.ACCESSIBILITY_DISPLAY_MAGNIFICATION_ENABLED,
   1248                         R.bool.def_accessibility_display_magnification_enabled);
   1249                 stmt.close();
   1250                 stmt = db.compileStatement("INSERT INTO secure(name,value) VALUES(?,?);");
   1251                 loadFractionSetting(stmt, Settings.Secure.ACCESSIBILITY_DISPLAY_MAGNIFICATION_SCALE,
   1252                         R.fraction.def_accessibility_display_magnification_scale, 1);
   1253                 stmt.close();
   1254                 stmt = db.compileStatement("INSERT INTO secure(name,value) VALUES(?,?);");
   1255                 loadBooleanSetting(stmt,
   1256                         Settings.Secure.ACCESSIBILITY_DISPLAY_MAGNIFICATION_AUTO_UPDATE,
   1257                         R.bool.def_accessibility_display_magnification_auto_update);
   1258 
   1259                 db.setTransactionSuccessful();
   1260             } finally {
   1261                 db.endTransaction();
   1262                 if (stmt != null) stmt.close();
   1263             }
   1264             upgradeVersion = 84;
   1265         }
   1266 
   1267         if (upgradeVersion == 84) {
   1268             if (mUserHandle == UserHandle.USER_OWNER) {
   1269                 db.beginTransaction();
   1270                 SQLiteStatement stmt = null;
   1271                 try {
   1272                     // Patch up the slightly-wrong key migration from 82 -> 83 for those
   1273                     // devices that missed it, ignoring if the move is redundant
   1274                     String[] settingsToMove = {
   1275                             Settings.Secure.ADB_ENABLED,
   1276                             Settings.Secure.BLUETOOTH_ON,
   1277                             Settings.Secure.DATA_ROAMING,
   1278                             Settings.Secure.DEVICE_PROVISIONED,
   1279                             Settings.Secure.INSTALL_NON_MARKET_APPS,
   1280                             Settings.Secure.USB_MASS_STORAGE_ENABLED
   1281                     };
   1282                     moveSettingsToNewTable(db, TABLE_SECURE, TABLE_GLOBAL, settingsToMove, true);
   1283                     db.setTransactionSuccessful();
   1284                 } finally {
   1285                     db.endTransaction();
   1286                     if (stmt != null) stmt.close();
   1287                 }
   1288             }
   1289             upgradeVersion = 85;
   1290         }
   1291 
   1292         if (upgradeVersion == 85) {
   1293             if (mUserHandle == UserHandle.USER_OWNER) {
   1294                 db.beginTransaction();
   1295                 try {
   1296                     // Fix up the migration, ignoring already-migrated elements, to snap up to
   1297                     // date with new changes to the set of global versus system/secure settings
   1298                     String[] settingsToMove = { Settings.System.STAY_ON_WHILE_PLUGGED_IN };
   1299                     moveSettingsToNewTable(db, TABLE_SYSTEM, TABLE_GLOBAL, settingsToMove, true);
   1300 
   1301                     db.setTransactionSuccessful();
   1302                 } finally {
   1303                     db.endTransaction();
   1304                 }
   1305             }
   1306             upgradeVersion = 86;
   1307         }
   1308 
   1309         if (upgradeVersion == 86) {
   1310             if (mUserHandle == UserHandle.USER_OWNER) {
   1311                 db.beginTransaction();
   1312                 try {
   1313                     String[] settingsToMove = {
   1314                             Settings.Global.PACKAGE_VERIFIER_ENABLE,
   1315                             Settings.Global.PACKAGE_VERIFIER_TIMEOUT,
   1316                             Settings.Global.PACKAGE_VERIFIER_DEFAULT_RESPONSE
   1317                     };
   1318                     moveSettingsToNewTable(db, TABLE_SECURE, TABLE_GLOBAL, settingsToMove, true);
   1319 
   1320                     db.setTransactionSuccessful();
   1321                 } finally {
   1322                     db.endTransaction();
   1323                 }
   1324             }
   1325             upgradeVersion = 87;
   1326         }
   1327 
   1328         if (upgradeVersion == 87) {
   1329             if (mUserHandle == UserHandle.USER_OWNER) {
   1330                 db.beginTransaction();
   1331                 try {
   1332                     String[] settingsToMove = {
   1333                             Settings.Global.DATA_STALL_ALARM_NON_AGGRESSIVE_DELAY_IN_MS,
   1334                             Settings.Global.DATA_STALL_ALARM_AGGRESSIVE_DELAY_IN_MS,
   1335                             Settings.Global.GPRS_REGISTER_CHECK_PERIOD_MS
   1336                     };
   1337                     moveSettingsToNewTable(db, TABLE_SECURE, TABLE_GLOBAL, settingsToMove, true);
   1338 
   1339                     db.setTransactionSuccessful();
   1340                 } finally {
   1341                     db.endTransaction();
   1342                 }
   1343             }
   1344             upgradeVersion = 88;
   1345         }
   1346 
   1347         if (upgradeVersion == 88) {
   1348             if (mUserHandle == UserHandle.USER_OWNER) {
   1349                 db.beginTransaction();
   1350                 try {
   1351                     String[] settingsToMove = {
   1352                             Settings.Global.BATTERY_DISCHARGE_DURATION_THRESHOLD,
   1353                             Settings.Global.BATTERY_DISCHARGE_THRESHOLD,
   1354                             Settings.Global.SEND_ACTION_APP_ERROR,
   1355                             Settings.Global.DROPBOX_AGE_SECONDS,
   1356                             Settings.Global.DROPBOX_MAX_FILES,
   1357                             Settings.Global.DROPBOX_QUOTA_KB,
   1358                             Settings.Global.DROPBOX_QUOTA_PERCENT,
   1359                             Settings.Global.DROPBOX_RESERVE_PERCENT,
   1360                             Settings.Global.DROPBOX_TAG_PREFIX,
   1361                             Settings.Global.ERROR_LOGCAT_PREFIX,
   1362                             Settings.Global.SYS_FREE_STORAGE_LOG_INTERVAL,
   1363                             Settings.Global.DISK_FREE_CHANGE_REPORTING_THRESHOLD,
   1364                             Settings.Global.SYS_STORAGE_THRESHOLD_PERCENTAGE,
   1365                             Settings.Global.SYS_STORAGE_THRESHOLD_MAX_BYTES,
   1366                             Settings.Global.SYS_STORAGE_FULL_THRESHOLD_BYTES,
   1367                             Settings.Global.SYNC_MAX_RETRY_DELAY_IN_SECONDS,
   1368                             Settings.Global.CONNECTIVITY_CHANGE_DELAY,
   1369                             Settings.Global.CAPTIVE_PORTAL_DETECTION_ENABLED,
   1370                             Settings.Global.CAPTIVE_PORTAL_SERVER,
   1371                             Settings.Global.NSD_ON,
   1372                             Settings.Global.SET_INSTALL_LOCATION,
   1373                             Settings.Global.DEFAULT_INSTALL_LOCATION,
   1374                             Settings.Global.INET_CONDITION_DEBOUNCE_UP_DELAY,
   1375                             Settings.Global.INET_CONDITION_DEBOUNCE_DOWN_DELAY,
   1376                             Settings.Global.READ_EXTERNAL_STORAGE_ENFORCED_DEFAULT,
   1377                             Settings.Global.HTTP_PROXY,
   1378                             Settings.Global.GLOBAL_HTTP_PROXY_HOST,
   1379                             Settings.Global.GLOBAL_HTTP_PROXY_PORT,
   1380                             Settings.Global.GLOBAL_HTTP_PROXY_EXCLUSION_LIST,
   1381                             Settings.Global.SET_GLOBAL_HTTP_PROXY,
   1382                             Settings.Global.DEFAULT_DNS_SERVER,
   1383                     };
   1384                     moveSettingsToNewTable(db, TABLE_SECURE, TABLE_GLOBAL, settingsToMove, true);
   1385                     db.setTransactionSuccessful();
   1386                 } finally {
   1387                     db.endTransaction();
   1388                 }
   1389             }
   1390             upgradeVersion = 89;
   1391         }
   1392 
   1393         if (upgradeVersion == 89) {
   1394             if (mUserHandle == UserHandle.USER_OWNER) {
   1395                 db.beginTransaction();
   1396                 try {
   1397                     String[] prefixesToMove = {
   1398                             Settings.Global.BLUETOOTH_HEADSET_PRIORITY_PREFIX,
   1399                             Settings.Global.BLUETOOTH_A2DP_SINK_PRIORITY_PREFIX,
   1400                             Settings.Global.BLUETOOTH_INPUT_DEVICE_PRIORITY_PREFIX,
   1401                     };
   1402 
   1403                     movePrefixedSettingsToNewTable(db, TABLE_SECURE, TABLE_GLOBAL, prefixesToMove);
   1404 
   1405                     db.setTransactionSuccessful();
   1406                 } finally {
   1407                     db.endTransaction();
   1408                 }
   1409             }
   1410             upgradeVersion = 90;
   1411         }
   1412 
   1413         if (upgradeVersion == 90) {
   1414             if (mUserHandle == UserHandle.USER_OWNER) {
   1415                 db.beginTransaction();
   1416                 try {
   1417                     String[] systemToGlobal = {
   1418                             Settings.Global.WINDOW_ANIMATION_SCALE,
   1419                             Settings.Global.TRANSITION_ANIMATION_SCALE,
   1420                             Settings.Global.ANIMATOR_DURATION_SCALE,
   1421                             Settings.Global.FANCY_IME_ANIMATIONS,
   1422                             Settings.Global.COMPATIBILITY_MODE,
   1423                             Settings.Global.EMERGENCY_TONE,
   1424                             Settings.Global.CALL_AUTO_RETRY,
   1425                             Settings.Global.DEBUG_APP,
   1426                             Settings.Global.WAIT_FOR_DEBUGGER,
   1427                             Settings.Global.SHOW_PROCESSES,
   1428                             Settings.Global.ALWAYS_FINISH_ACTIVITIES,
   1429                     };
   1430                     String[] secureToGlobal = {
   1431                             Settings.Global.PREFERRED_NETWORK_MODE,
   1432                             Settings.Global.PREFERRED_CDMA_SUBSCRIPTION,
   1433                     };
   1434 
   1435                     moveSettingsToNewTable(db, TABLE_SYSTEM, TABLE_GLOBAL, systemToGlobal, true);
   1436                     moveSettingsToNewTable(db, TABLE_SECURE, TABLE_GLOBAL, secureToGlobal, true);
   1437 
   1438                     db.setTransactionSuccessful();
   1439                 } finally {
   1440                     db.endTransaction();
   1441                 }
   1442             }
   1443             upgradeVersion = 91;
   1444         }
   1445 
   1446         if (upgradeVersion == 91) {
   1447             if (mUserHandle == UserHandle.USER_OWNER) {
   1448                 db.beginTransaction();
   1449                 try {
   1450                     // Move ringer mode from system to global settings
   1451                     String[] settingsToMove = { Settings.Global.MODE_RINGER };
   1452                     moveSettingsToNewTable(db, TABLE_SYSTEM, TABLE_GLOBAL, settingsToMove, true);
   1453 
   1454                     db.setTransactionSuccessful();
   1455                 } finally {
   1456                     db.endTransaction();
   1457                 }
   1458             }
   1459             upgradeVersion = 92;
   1460         }
   1461 
   1462         if (upgradeVersion == 92) {
   1463             SQLiteStatement stmt = null;
   1464             try {
   1465                 stmt = db.compileStatement("INSERT OR IGNORE INTO secure(name,value)"
   1466                         + " VALUES(?,?);");
   1467                 if (mUserHandle == UserHandle.USER_OWNER) {
   1468                     // consider existing primary users to have made it through user setup
   1469                     // if the globally-scoped device-provisioned bit is set
   1470                     // (indicating they already made it through setup as primary)
   1471                     int deviceProvisioned = getIntValueFromTable(db, TABLE_GLOBAL,
   1472                             Settings.Global.DEVICE_PROVISIONED, 0);
   1473                     loadSetting(stmt, Settings.Secure.USER_SETUP_COMPLETE,
   1474                             deviceProvisioned);
   1475                 } else {
   1476                     // otherwise use the default
   1477                     loadBooleanSetting(stmt, Settings.Secure.USER_SETUP_COMPLETE,
   1478                             R.bool.def_user_setup_complete);
   1479                 }
   1480             } finally {
   1481                 if (stmt != null) stmt.close();
   1482             }
   1483             upgradeVersion = 93;
   1484         }
   1485 
   1486         if (upgradeVersion == 93) {
   1487             // Redo this step, since somehow it didn't work the first time for some users
   1488             if (mUserHandle == UserHandle.USER_OWNER) {
   1489                 db.beginTransaction();
   1490                 try {
   1491                     // Migrate now-global settings
   1492                     String[] settingsToMove = hashsetToStringArray(SettingsProvider.sSystemGlobalKeys);
   1493                     moveSettingsToNewTable(db, TABLE_SYSTEM, TABLE_GLOBAL, settingsToMove, true);
   1494                     settingsToMove = hashsetToStringArray(SettingsProvider.sSecureGlobalKeys);
   1495                     moveSettingsToNewTable(db, TABLE_SECURE, TABLE_GLOBAL, settingsToMove, true);
   1496 
   1497                     db.setTransactionSuccessful();
   1498                 } finally {
   1499                     db.endTransaction();
   1500                 }
   1501             }
   1502             upgradeVersion = 94;
   1503         }
   1504 
   1505         if (upgradeVersion == 94) {
   1506             // Add wireless charging started sound setting
   1507             if (mUserHandle == UserHandle.USER_OWNER) {
   1508                 db.beginTransaction();
   1509                 SQLiteStatement stmt = null;
   1510                 try {
   1511                     stmt = db.compileStatement("INSERT OR REPLACE INTO global(name,value)"
   1512                             + " VALUES(?,?);");
   1513                     loadStringSetting(stmt, Settings.Global.WIRELESS_CHARGING_STARTED_SOUND,
   1514                             R.string.def_wireless_charging_started_sound);
   1515                     db.setTransactionSuccessful();
   1516                 } finally {
   1517                     db.endTransaction();
   1518                     if (stmt != null) stmt.close();
   1519                 }
   1520             }
   1521             upgradeVersion = 95;
   1522         }
   1523 
   1524         if (upgradeVersion == 95) {
   1525             if (mUserHandle == UserHandle.USER_OWNER) {
   1526                 db.beginTransaction();
   1527                 try {
   1528                     String[] settingsToMove = { Settings.Global.BUGREPORT_IN_POWER_MENU };
   1529                     moveSettingsToNewTable(db, TABLE_SECURE, TABLE_GLOBAL, settingsToMove, true);
   1530                     db.setTransactionSuccessful();
   1531                 } finally {
   1532                     db.endTransaction();
   1533                 }
   1534             }
   1535             upgradeVersion = 96;
   1536         }
   1537 
   1538         if (upgradeVersion == 96) {
   1539             // NOP bump due to a reverted change that some people got on upgrade.
   1540             upgradeVersion = 97;
   1541         }
   1542 
   1543         // *** Remember to update DATABASE_VERSION above!
   1544 
   1545         if (upgradeVersion != currentVersion) {
   1546             Log.w(TAG, "Got stuck trying to upgrade from version " + upgradeVersion
   1547                     + ", must wipe the settings provider");
   1548             db.execSQL("DROP TABLE IF EXISTS global");
   1549             db.execSQL("DROP TABLE IF EXISTS globalIndex1");
   1550             db.execSQL("DROP TABLE IF EXISTS system");
   1551             db.execSQL("DROP INDEX IF EXISTS systemIndex1");
   1552             db.execSQL("DROP TABLE IF EXISTS secure");
   1553             db.execSQL("DROP INDEX IF EXISTS secureIndex1");
   1554             db.execSQL("DROP TABLE IF EXISTS gservices");
   1555             db.execSQL("DROP INDEX IF EXISTS gservicesIndex1");
   1556             db.execSQL("DROP TABLE IF EXISTS bluetooth_devices");
   1557             db.execSQL("DROP TABLE IF EXISTS bookmarks");
   1558             db.execSQL("DROP INDEX IF EXISTS bookmarksIndex1");
   1559             db.execSQL("DROP INDEX IF EXISTS bookmarksIndex2");
   1560             db.execSQL("DROP TABLE IF EXISTS favorites");
   1561             onCreate(db);
   1562 
   1563             // Added for diagnosing settings.db wipes after the fact
   1564             String wipeReason = oldVersion + "/" + upgradeVersion + "/" + currentVersion;
   1565             db.execSQL("INSERT INTO secure(name,value) values('" +
   1566                     "wiped_db_reason" + "','" + wipeReason + "');");
   1567         }
   1568     }
   1569 
   1570     private String[] hashsetToStringArray(HashSet<String> set) {
   1571         String[] array = new String[set.size()];
   1572         return set.toArray(array);
   1573     }
   1574 
   1575     private void moveSettingsToNewTable(SQLiteDatabase db,
   1576             String sourceTable, String destTable,
   1577             String[] settingsToMove, boolean doIgnore) {
   1578         // Copy settings values from the source table to the dest, and remove from the source
   1579         SQLiteStatement insertStmt = null;
   1580         SQLiteStatement deleteStmt = null;
   1581 
   1582         db.beginTransaction();
   1583         try {
   1584             insertStmt = db.compileStatement("INSERT "
   1585                     + (doIgnore ? " OR IGNORE " : "")
   1586                     + " INTO " + destTable + " (name,value) SELECT name,value FROM "
   1587                     + sourceTable + " WHERE name=?");
   1588             deleteStmt = db.compileStatement("DELETE FROM " + sourceTable + " WHERE name=?");
   1589 
   1590             for (String setting : settingsToMove) {
   1591                 insertStmt.bindString(1, setting);
   1592                 insertStmt.execute();
   1593 
   1594                 deleteStmt.bindString(1, setting);
   1595                 deleteStmt.execute();
   1596             }
   1597             db.setTransactionSuccessful();
   1598         } finally {
   1599             db.endTransaction();
   1600             if (insertStmt != null) {
   1601                 insertStmt.close();
   1602             }
   1603             if (deleteStmt != null) {
   1604                 deleteStmt.close();
   1605             }
   1606         }
   1607     }
   1608 
   1609     /**
   1610      * Move any settings with the given prefixes from the source table to the
   1611      * destination table.
   1612      */
   1613     private void movePrefixedSettingsToNewTable(
   1614             SQLiteDatabase db, String sourceTable, String destTable, String[] prefixesToMove) {
   1615         SQLiteStatement insertStmt = null;
   1616         SQLiteStatement deleteStmt = null;
   1617 
   1618         db.beginTransaction();
   1619         try {
   1620             insertStmt = db.compileStatement("INSERT INTO " + destTable
   1621                     + " (name,value) SELECT name,value FROM " + sourceTable
   1622                     + " WHERE substr(name,0,?)=?");
   1623             deleteStmt = db.compileStatement(
   1624                     "DELETE FROM " + sourceTable + " WHERE substr(name,0,?)=?");
   1625 
   1626             for (String prefix : prefixesToMove) {
   1627                 insertStmt.bindLong(1, prefix.length() + 1);
   1628                 insertStmt.bindString(2, prefix);
   1629                 insertStmt.execute();
   1630 
   1631                 deleteStmt.bindLong(1, prefix.length() + 1);
   1632                 deleteStmt.bindString(2, prefix);
   1633                 deleteStmt.execute();
   1634             }
   1635             db.setTransactionSuccessful();
   1636         } finally {
   1637             db.endTransaction();
   1638             if (insertStmt != null) {
   1639                 insertStmt.close();
   1640             }
   1641             if (deleteStmt != null) {
   1642                 deleteStmt.close();
   1643             }
   1644         }
   1645     }
   1646 
   1647     private void upgradeLockPatternLocation(SQLiteDatabase db) {
   1648         Cursor c = db.query(TABLE_SYSTEM, new String[] {"_id", "value"}, "name='lock_pattern'",
   1649                 null, null, null, null);
   1650         if (c.getCount() > 0) {
   1651             c.moveToFirst();
   1652             String lockPattern = c.getString(1);
   1653             if (!TextUtils.isEmpty(lockPattern)) {
   1654                 // Convert lock pattern
   1655                 try {
   1656                     LockPatternUtils lpu = new LockPatternUtils(mContext);
   1657                     List<LockPatternView.Cell> cellPattern =
   1658                             LockPatternUtils.stringToPattern(lockPattern);
   1659                     lpu.saveLockPattern(cellPattern);
   1660                 } catch (IllegalArgumentException e) {
   1661                     // Don't want corrupted lock pattern to hang the reboot process
   1662                 }
   1663             }
   1664             c.close();
   1665             db.delete(TABLE_SYSTEM, "name='lock_pattern'", null);
   1666         } else {
   1667             c.close();
   1668         }
   1669     }
   1670 
   1671     private void upgradeScreenTimeoutFromNever(SQLiteDatabase db) {
   1672         // See if the timeout is -1 (for "Never").
   1673         Cursor c = db.query(TABLE_SYSTEM, new String[] { "_id", "value" }, "name=? AND value=?",
   1674                 new String[] { Settings.System.SCREEN_OFF_TIMEOUT, "-1" },
   1675                 null, null, null);
   1676 
   1677         SQLiteStatement stmt = null;
   1678         if (c.getCount() > 0) {
   1679             c.close();
   1680             try {
   1681                 stmt = db.compileStatement("INSERT OR REPLACE INTO system(name,value)"
   1682                         + " VALUES(?,?);");
   1683 
   1684                 // Set the timeout to 30 minutes in milliseconds
   1685                 loadSetting(stmt, Settings.System.SCREEN_OFF_TIMEOUT,
   1686                         Integer.toString(30 * 60 * 1000));
   1687             } finally {
   1688                 if (stmt != null) stmt.close();
   1689             }
   1690         } else {
   1691             c.close();
   1692         }
   1693     }
   1694 
   1695     private void upgradeVibrateSettingFromNone(SQLiteDatabase db) {
   1696         int vibrateSetting = getIntValueFromSystem(db, Settings.System.VIBRATE_ON, 0);
   1697         // If the ringer vibrate value is invalid, set it to the default
   1698         if ((vibrateSetting & 3) == AudioManager.VIBRATE_SETTING_OFF) {
   1699             vibrateSetting = AudioService.getValueForVibrateSetting(0,
   1700                     AudioManager.VIBRATE_TYPE_RINGER, AudioManager.VIBRATE_SETTING_ONLY_SILENT);
   1701         }
   1702         // Apply the same setting to the notification vibrate value
   1703         vibrateSetting = AudioService.getValueForVibrateSetting(vibrateSetting,
   1704                 AudioManager.VIBRATE_TYPE_NOTIFICATION, vibrateSetting);
   1705 
   1706         SQLiteStatement stmt = null;
   1707         try {
   1708             stmt = db.compileStatement("INSERT OR REPLACE INTO system(name,value)"
   1709                     + " VALUES(?,?);");
   1710             loadSetting(stmt, Settings.System.VIBRATE_ON, vibrateSetting);
   1711         } finally {
   1712             if (stmt != null)
   1713                 stmt.close();
   1714         }
   1715     }
   1716 
   1717     private void upgradeScreenTimeout(SQLiteDatabase db) {
   1718         // Change screen timeout to current default
   1719         db.beginTransaction();
   1720         SQLiteStatement stmt = null;
   1721         try {
   1722             stmt = db.compileStatement("INSERT OR REPLACE INTO system(name,value)"
   1723                     + " VALUES(?,?);");
   1724             loadIntegerSetting(stmt, Settings.System.SCREEN_OFF_TIMEOUT,
   1725                     R.integer.def_screen_off_timeout);
   1726             db.setTransactionSuccessful();
   1727         } finally {
   1728             db.endTransaction();
   1729             if (stmt != null)
   1730                 stmt.close();
   1731         }
   1732     }
   1733 
   1734     private void upgradeAutoBrightness(SQLiteDatabase db) {
   1735         db.beginTransaction();
   1736         try {
   1737             String value =
   1738                     mContext.getResources().getBoolean(
   1739                     R.bool.def_screen_brightness_automatic_mode) ? "1" : "0";
   1740             db.execSQL("INSERT OR REPLACE INTO system(name,value) values('" +
   1741                     Settings.System.SCREEN_BRIGHTNESS_MODE + "','" + value + "');");
   1742             db.setTransactionSuccessful();
   1743         } finally {
   1744             db.endTransaction();
   1745         }
   1746     }
   1747 
   1748     /**
   1749      * Loads the default set of bookmarked shortcuts from an xml file.
   1750      *
   1751      * @param db The database to write the values into
   1752      */
   1753     private void loadBookmarks(SQLiteDatabase db) {
   1754         ContentValues values = new ContentValues();
   1755 
   1756         PackageManager packageManager = mContext.getPackageManager();
   1757         try {
   1758             XmlResourceParser parser = mContext.getResources().getXml(R.xml.bookmarks);
   1759             XmlUtils.beginDocument(parser, "bookmarks");
   1760 
   1761             final int depth = parser.getDepth();
   1762             int type;
   1763 
   1764             while (((type = parser.next()) != XmlPullParser.END_TAG ||
   1765                     parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
   1766 
   1767                 if (type != XmlPullParser.START_TAG) {
   1768                     continue;
   1769                 }
   1770 
   1771                 String name = parser.getName();
   1772                 if (!"bookmark".equals(name)) {
   1773                     break;
   1774                 }
   1775 
   1776                 String pkg = parser.getAttributeValue(null, "package");
   1777                 String cls = parser.getAttributeValue(null, "class");
   1778                 String shortcutStr = parser.getAttributeValue(null, "shortcut");
   1779                 String category = parser.getAttributeValue(null, "category");
   1780 
   1781                 int shortcutValue = shortcutStr.charAt(0);
   1782                 if (TextUtils.isEmpty(shortcutStr)) {
   1783                     Log.w(TAG, "Unable to get shortcut for: " + pkg + "/" + cls);
   1784                     continue;
   1785                 }
   1786 
   1787                 final Intent intent;
   1788                 final String title;
   1789                 if (pkg != null && cls != null) {
   1790                     ActivityInfo info = null;
   1791                     ComponentName cn = new ComponentName(pkg, cls);
   1792                     try {
   1793                         info = packageManager.getActivityInfo(cn, 0);
   1794                     } catch (PackageManager.NameNotFoundException e) {
   1795                         String[] packages = packageManager.canonicalToCurrentPackageNames(
   1796                                 new String[] { pkg });
   1797                         cn = new ComponentName(packages[0], cls);
   1798                         try {
   1799                             info = packageManager.getActivityInfo(cn, 0);
   1800                         } catch (PackageManager.NameNotFoundException e1) {
   1801                             Log.w(TAG, "Unable to add bookmark: " + pkg + "/" + cls, e);
   1802                             continue;
   1803                         }
   1804                     }
   1805 
   1806                     intent = new Intent(Intent.ACTION_MAIN, null);
   1807                     intent.addCategory(Intent.CATEGORY_LAUNCHER);
   1808                     intent.setComponent(cn);
   1809                     title = info.loadLabel(packageManager).toString();
   1810                 } else if (category != null) {
   1811                     intent = Intent.makeMainSelectorActivity(Intent.ACTION_MAIN, category);
   1812                     title = "";
   1813                 } else {
   1814                     Log.w(TAG, "Unable to add bookmark for shortcut " + shortcutStr
   1815                             + ": missing package/class or category attributes");
   1816                     continue;
   1817                 }
   1818 
   1819                 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
   1820                 values.put(Settings.Bookmarks.INTENT, intent.toUri(0));
   1821                 values.put(Settings.Bookmarks.TITLE, title);
   1822                 values.put(Settings.Bookmarks.SHORTCUT, shortcutValue);
   1823                 db.delete("bookmarks", "shortcut = ?",
   1824                         new String[] { Integer.toString(shortcutValue) });
   1825                 db.insert("bookmarks", null, values);
   1826             }
   1827         } catch (XmlPullParserException e) {
   1828             Log.w(TAG, "Got execption parsing bookmarks.", e);
   1829         } catch (IOException e) {
   1830             Log.w(TAG, "Got execption parsing bookmarks.", e);
   1831         }
   1832     }
   1833 
   1834     /**
   1835      * Loads the default volume levels. It is actually inserting the index of
   1836      * the volume array for each of the volume controls.
   1837      *
   1838      * @param db the database to insert the volume levels into
   1839      */
   1840     private void loadVolumeLevels(SQLiteDatabase db) {
   1841         SQLiteStatement stmt = null;
   1842         try {
   1843             stmt = db.compileStatement("INSERT OR IGNORE INTO system(name,value)"
   1844                     + " VALUES(?,?);");
   1845 
   1846             loadSetting(stmt, Settings.System.VOLUME_MUSIC,
   1847                     AudioManager.DEFAULT_STREAM_VOLUME[AudioManager.STREAM_MUSIC]);
   1848             loadSetting(stmt, Settings.System.VOLUME_RING,
   1849                     AudioManager.DEFAULT_STREAM_VOLUME[AudioManager.STREAM_RING]);
   1850             loadSetting(stmt, Settings.System.VOLUME_SYSTEM,
   1851                     AudioManager.DEFAULT_STREAM_VOLUME[AudioManager.STREAM_SYSTEM]);
   1852             loadSetting(
   1853                     stmt,
   1854                     Settings.System.VOLUME_VOICE,
   1855                     AudioManager.DEFAULT_STREAM_VOLUME[AudioManager.STREAM_VOICE_CALL]);
   1856             loadSetting(stmt, Settings.System.VOLUME_ALARM,
   1857                     AudioManager.DEFAULT_STREAM_VOLUME[AudioManager.STREAM_ALARM]);
   1858             loadSetting(
   1859                     stmt,
   1860                     Settings.System.VOLUME_NOTIFICATION,
   1861                     AudioManager.DEFAULT_STREAM_VOLUME[AudioManager.STREAM_NOTIFICATION]);
   1862             loadSetting(
   1863                     stmt,
   1864                     Settings.System.VOLUME_BLUETOOTH_SCO,
   1865                     AudioManager.DEFAULT_STREAM_VOLUME[AudioManager.STREAM_BLUETOOTH_SCO]);
   1866 
   1867             // By default:
   1868             // - ringtones, notification, system and music streams are affected by ringer mode
   1869             // on non voice capable devices (tablets)
   1870             // - ringtones, notification and system streams are affected by ringer mode
   1871             // on voice capable devices (phones)
   1872             int ringerModeAffectedStreams = (1 << AudioManager.STREAM_RING) |
   1873                                             (1 << AudioManager.STREAM_NOTIFICATION) |
   1874                                             (1 << AudioManager.STREAM_SYSTEM) |
   1875                                             (1 << AudioManager.STREAM_SYSTEM_ENFORCED);
   1876             if (!mContext.getResources().getBoolean(
   1877                     com.android.internal.R.bool.config_voice_capable)) {
   1878                 ringerModeAffectedStreams |= (1 << AudioManager.STREAM_MUSIC);
   1879             }
   1880             loadSetting(stmt, Settings.System.MODE_RINGER_STREAMS_AFFECTED,
   1881                     ringerModeAffectedStreams);
   1882 
   1883             loadSetting(stmt, Settings.System.MUTE_STREAMS_AFFECTED,
   1884                     ((1 << AudioManager.STREAM_MUSIC) |
   1885                      (1 << AudioManager.STREAM_RING) |
   1886                      (1 << AudioManager.STREAM_NOTIFICATION) |
   1887                      (1 << AudioManager.STREAM_SYSTEM)));
   1888         } finally {
   1889             if (stmt != null) stmt.close();
   1890         }
   1891 
   1892         loadVibrateWhenRingingSetting(db);
   1893     }
   1894 
   1895     private void loadVibrateSetting(SQLiteDatabase db, boolean deleteOld) {
   1896         if (deleteOld) {
   1897             db.execSQL("DELETE FROM system WHERE name='" + Settings.System.VIBRATE_ON + "'");
   1898         }
   1899 
   1900         SQLiteStatement stmt = null;
   1901         try {
   1902             stmt = db.compileStatement("INSERT OR IGNORE INTO system(name,value)"
   1903                     + " VALUES(?,?);");
   1904 
   1905             // Vibrate on by default for ringer, on for notification
   1906             int vibrate = 0;
   1907             vibrate = AudioService.getValueForVibrateSetting(vibrate,
   1908                     AudioManager.VIBRATE_TYPE_NOTIFICATION,
   1909                     AudioManager.VIBRATE_SETTING_ONLY_SILENT);
   1910             vibrate |= AudioService.getValueForVibrateSetting(vibrate,
   1911                     AudioManager.VIBRATE_TYPE_RINGER, AudioManager.VIBRATE_SETTING_ONLY_SILENT);
   1912             loadSetting(stmt, Settings.System.VIBRATE_ON, vibrate);
   1913         } finally {
   1914             if (stmt != null) stmt.close();
   1915         }
   1916     }
   1917 
   1918     private void loadVibrateWhenRingingSetting(SQLiteDatabase db) {
   1919         // The default should be off. VIBRATE_SETTING_ONLY_SILENT should also be ignored here.
   1920         // Phone app should separately check whether AudioManager#getRingerMode() returns
   1921         // RINGER_MODE_VIBRATE, with which the device should vibrate anyway.
   1922         int vibrateSetting = getIntValueFromSystem(db, Settings.System.VIBRATE_ON,
   1923                 AudioManager.VIBRATE_SETTING_OFF);
   1924         boolean vibrateWhenRinging = ((vibrateSetting & 3) == AudioManager.VIBRATE_SETTING_ON);
   1925 
   1926         SQLiteStatement stmt = null;
   1927         try {
   1928             stmt = db.compileStatement("INSERT OR IGNORE INTO system(name,value)"
   1929                     + " VALUES(?,?);");
   1930             loadSetting(stmt, Settings.System.VIBRATE_WHEN_RINGING, vibrateWhenRinging ? 1 : 0);
   1931         } finally {
   1932             if (stmt != null) stmt.close();
   1933         }
   1934     }
   1935 
   1936     private void loadSettings(SQLiteDatabase db) {
   1937         loadSystemSettings(db);
   1938         loadSecureSettings(db);
   1939         // The global table only exists for the 'owner' user
   1940         if (mUserHandle == UserHandle.USER_OWNER) {
   1941             loadGlobalSettings(db);
   1942         }
   1943     }
   1944 
   1945     private void loadSystemSettings(SQLiteDatabase db) {
   1946         SQLiteStatement stmt = null;
   1947         try {
   1948             stmt = db.compileStatement("INSERT OR IGNORE INTO system(name,value)"
   1949                     + " VALUES(?,?);");
   1950 
   1951             loadBooleanSetting(stmt, Settings.System.DIM_SCREEN,
   1952                     R.bool.def_dim_screen);
   1953             loadIntegerSetting(stmt, Settings.System.SCREEN_OFF_TIMEOUT,
   1954                     R.integer.def_screen_off_timeout);
   1955 
   1956             // Set default cdma DTMF type
   1957             loadSetting(stmt, Settings.System.DTMF_TONE_TYPE_WHEN_DIALING, 0);
   1958 
   1959             // Set default hearing aid
   1960             loadSetting(stmt, Settings.System.HEARING_AID, 0);
   1961 
   1962             // Set default tty mode
   1963             loadSetting(stmt, Settings.System.TTY_MODE, 0);
   1964 
   1965             loadIntegerSetting(stmt, Settings.System.SCREEN_BRIGHTNESS,
   1966                     R.integer.def_screen_brightness);
   1967 
   1968             loadBooleanSetting(stmt, Settings.System.SCREEN_BRIGHTNESS_MODE,
   1969                     R.bool.def_screen_brightness_automatic_mode);
   1970 
   1971             loadDefaultAnimationSettings(stmt);
   1972 
   1973             loadBooleanSetting(stmt, Settings.System.ACCELEROMETER_ROTATION,
   1974                     R.bool.def_accelerometer_rotation);
   1975 
   1976             loadDefaultHapticSettings(stmt);
   1977 
   1978             loadBooleanSetting(stmt, Settings.System.NOTIFICATION_LIGHT_PULSE,
   1979                     R.bool.def_notification_pulse);
   1980 
   1981             loadUISoundEffectsSettings(stmt);
   1982 
   1983             loadIntegerSetting(stmt, Settings.System.POINTER_SPEED,
   1984                     R.integer.def_pointer_speed);
   1985         } finally {
   1986             if (stmt != null) stmt.close();
   1987         }
   1988     }
   1989 
   1990     private void loadUISoundEffectsSettings(SQLiteStatement stmt) {
   1991         loadBooleanSetting(stmt, Settings.System.DTMF_TONE_WHEN_DIALING,
   1992                 R.bool.def_dtmf_tones_enabled);
   1993         loadBooleanSetting(stmt, Settings.System.SOUND_EFFECTS_ENABLED,
   1994                 R.bool.def_sound_effects_enabled);
   1995         loadBooleanSetting(stmt, Settings.System.HAPTIC_FEEDBACK_ENABLED,
   1996                 R.bool.def_haptic_feedback);
   1997 
   1998         loadIntegerSetting(stmt, Settings.System.LOCKSCREEN_SOUNDS_ENABLED,
   1999             R.integer.def_lockscreen_sounds_enabled);
   2000     }
   2001 
   2002     private void loadDefaultAnimationSettings(SQLiteStatement stmt) {
   2003         loadFractionSetting(stmt, Settings.System.WINDOW_ANIMATION_SCALE,
   2004                 R.fraction.def_window_animation_scale, 1);
   2005         loadFractionSetting(stmt, Settings.System.TRANSITION_ANIMATION_SCALE,
   2006                 R.fraction.def_window_transition_scale, 1);
   2007     }
   2008 
   2009     private void loadDefaultHapticSettings(SQLiteStatement stmt) {
   2010         loadBooleanSetting(stmt, Settings.System.HAPTIC_FEEDBACK_ENABLED,
   2011                 R.bool.def_haptic_feedback);
   2012     }
   2013 
   2014     private void loadSecureSettings(SQLiteDatabase db) {
   2015         SQLiteStatement stmt = null;
   2016         try {
   2017             stmt = db.compileStatement("INSERT OR IGNORE INTO secure(name,value)"
   2018                     + " VALUES(?,?);");
   2019 
   2020             loadStringSetting(stmt, Settings.Secure.LOCATION_PROVIDERS_ALLOWED,
   2021                     R.string.def_location_providers_allowed);
   2022 
   2023             String wifiWatchList = SystemProperties.get("ro.com.android.wifi-watchlist");
   2024             if (!TextUtils.isEmpty(wifiWatchList)) {
   2025                 loadSetting(stmt, Settings.Secure.WIFI_WATCHDOG_WATCH_LIST, wifiWatchList);
   2026             }
   2027 
   2028             // Don't do this.  The SystemServer will initialize ADB_ENABLED from a
   2029             // persistent system property instead.
   2030             //loadSetting(stmt, Settings.Secure.ADB_ENABLED, 0);
   2031 
   2032             // Allow mock locations default, based on build
   2033             loadSetting(stmt, Settings.Secure.ALLOW_MOCK_LOCATION,
   2034                     "1".equals(SystemProperties.get("ro.allow.mock.location")) ? 1 : 0);
   2035 
   2036             loadSecure35Settings(stmt);
   2037 
   2038             loadBooleanSetting(stmt, Settings.Secure.MOUNT_PLAY_NOTIFICATION_SND,
   2039                     R.bool.def_mount_play_notification_snd);
   2040 
   2041             loadBooleanSetting(stmt, Settings.Secure.MOUNT_UMS_AUTOSTART,
   2042                     R.bool.def_mount_ums_autostart);
   2043 
   2044             loadBooleanSetting(stmt, Settings.Secure.MOUNT_UMS_PROMPT,
   2045                     R.bool.def_mount_ums_prompt);
   2046 
   2047             loadBooleanSetting(stmt, Settings.Secure.MOUNT_UMS_NOTIFY_ENABLED,
   2048                     R.bool.def_mount_ums_notify_enabled);
   2049 
   2050             loadBooleanSetting(stmt, Settings.Secure.ACCESSIBILITY_SCRIPT_INJECTION,
   2051                     R.bool.def_accessibility_script_injection);
   2052 
   2053             loadStringSetting(stmt, Settings.Secure.ACCESSIBILITY_WEB_CONTENT_KEY_BINDINGS,
   2054                     R.string.def_accessibility_web_content_key_bindings);
   2055 
   2056             loadIntegerSetting(stmt, Settings.Secure.LONG_PRESS_TIMEOUT,
   2057                     R.integer.def_long_press_timeout_millis);
   2058 
   2059             loadBooleanSetting(stmt, Settings.Secure.TOUCH_EXPLORATION_ENABLED,
   2060                     R.bool.def_touch_exploration_enabled);
   2061 
   2062             loadBooleanSetting(stmt, Settings.Secure.ACCESSIBILITY_SPEAK_PASSWORD,
   2063                     R.bool.def_accessibility_speak_password);
   2064 
   2065             loadStringSetting(stmt, Settings.Secure.ACCESSIBILITY_SCREEN_READER_URL,
   2066                     R.string.def_accessibility_screen_reader_url);
   2067 
   2068             if (SystemProperties.getBoolean("ro.lockscreen.disable.default", false) == true) {
   2069                 loadSetting(stmt, Settings.System.LOCKSCREEN_DISABLED, "1");
   2070             } else {
   2071                 loadBooleanSetting(stmt, Settings.System.LOCKSCREEN_DISABLED,
   2072                         R.bool.def_lockscreen_disabled);
   2073             }
   2074 
   2075             loadBooleanSetting(stmt, Settings.Secure.SCREENSAVER_ENABLED,
   2076                     com.android.internal.R.bool.config_dreamsEnabledByDefault);
   2077             loadBooleanSetting(stmt, Settings.Secure.SCREENSAVER_ACTIVATE_ON_DOCK,
   2078                     com.android.internal.R.bool.config_dreamsActivatedOnDockByDefault);
   2079             loadBooleanSetting(stmt, Settings.Secure.SCREENSAVER_ACTIVATE_ON_SLEEP,
   2080                     com.android.internal.R.bool.config_dreamsActivatedOnSleepByDefault);
   2081             loadStringSetting(stmt, Settings.Secure.SCREENSAVER_COMPONENTS,
   2082                     com.android.internal.R.string.config_dreamsDefaultComponent);
   2083             loadStringSetting(stmt, Settings.Secure.SCREENSAVER_DEFAULT_COMPONENT,
   2084                     com.android.internal.R.string.config_dreamsDefaultComponent);
   2085 
   2086             loadBooleanSetting(stmt, Settings.Secure.ACCESSIBILITY_DISPLAY_MAGNIFICATION_ENABLED,
   2087                     R.bool.def_accessibility_display_magnification_enabled);
   2088 
   2089             loadFractionSetting(stmt, Settings.Secure.ACCESSIBILITY_DISPLAY_MAGNIFICATION_SCALE,
   2090                     R.fraction.def_accessibility_display_magnification_scale, 1);
   2091 
   2092             loadBooleanSetting(stmt,
   2093                     Settings.Secure.ACCESSIBILITY_DISPLAY_MAGNIFICATION_AUTO_UPDATE,
   2094                     R.bool.def_accessibility_display_magnification_auto_update);
   2095 
   2096             loadBooleanSetting(stmt, Settings.Secure.USER_SETUP_COMPLETE,
   2097                     R.bool.def_user_setup_complete);
   2098         } finally {
   2099             if (stmt != null) stmt.close();
   2100         }
   2101     }
   2102 
   2103     private void loadSecure35Settings(SQLiteStatement stmt) {
   2104         loadBooleanSetting(stmt, Settings.Secure.BACKUP_ENABLED,
   2105                 R.bool.def_backup_enabled);
   2106 
   2107         loadStringSetting(stmt, Settings.Secure.BACKUP_TRANSPORT,
   2108                 R.string.def_backup_transport);
   2109     }
   2110 
   2111     private void loadGlobalSettings(SQLiteDatabase db) {
   2112         SQLiteStatement stmt = null;
   2113         try {
   2114             stmt = db.compileStatement("INSERT OR IGNORE INTO global(name,value)"
   2115                     + " VALUES(?,?);");
   2116 
   2117             // --- Previously in 'system'
   2118             loadBooleanSetting(stmt, Settings.Global.AIRPLANE_MODE_ON,
   2119                     R.bool.def_airplane_mode_on);
   2120 
   2121             loadStringSetting(stmt, Settings.Global.AIRPLANE_MODE_RADIOS,
   2122                     R.string.def_airplane_mode_radios);
   2123 
   2124             loadStringSetting(stmt, Settings.Global.AIRPLANE_MODE_TOGGLEABLE_RADIOS,
   2125                     R.string.airplane_mode_toggleable_radios);
   2126 
   2127             loadBooleanSetting(stmt, Settings.Global.ASSISTED_GPS_ENABLED,
   2128                     R.bool.assisted_gps_enabled);
   2129 
   2130             loadBooleanSetting(stmt, Settings.Global.AUTO_TIME,
   2131                     R.bool.def_auto_time); // Sync time to NITZ
   2132 
   2133             loadBooleanSetting(stmt, Settings.Global.AUTO_TIME_ZONE,
   2134                     R.bool.def_auto_time_zone); // Sync timezone to NITZ
   2135 
   2136             loadSetting(stmt, Settings.Global.STAY_ON_WHILE_PLUGGED_IN,
   2137                     ("1".equals(SystemProperties.get("ro.kernel.qemu")) ||
   2138                         mContext.getResources().getBoolean(R.bool.def_stay_on_while_plugged_in))
   2139                      ? 1 : 0);
   2140 
   2141             loadIntegerSetting(stmt, Settings.Global.WIFI_SLEEP_POLICY,
   2142                     R.integer.def_wifi_sleep_policy);
   2143 
   2144             loadSetting(stmt, Settings.Global.MODE_RINGER,
   2145                     AudioManager.RINGER_MODE_NORMAL);
   2146 
   2147             // --- Previously in 'secure'
   2148             loadBooleanSetting(stmt, Settings.Global.PACKAGE_VERIFIER_ENABLE,
   2149                     R.bool.def_package_verifier_enable);
   2150 
   2151             loadBooleanSetting(stmt, Settings.Global.WIFI_ON,
   2152                     R.bool.def_wifi_on);
   2153 
   2154             loadBooleanSetting(stmt, Settings.Global.WIFI_NETWORKS_AVAILABLE_NOTIFICATION_ON,
   2155                     R.bool.def_networks_available_notification_on);
   2156 
   2157             loadBooleanSetting(stmt, Settings.Global.BLUETOOTH_ON,
   2158                     R.bool.def_bluetooth_on);
   2159 
   2160             // Enable or disable Cell Broadcast SMS
   2161             loadSetting(stmt, Settings.Global.CDMA_CELL_BROADCAST_SMS,
   2162                     RILConstants.CDMA_CELL_BROADCAST_SMS_DISABLED);
   2163 
   2164             // Data roaming default, based on build
   2165             loadSetting(stmt, Settings.Global.DATA_ROAMING,
   2166                     "true".equalsIgnoreCase(
   2167                             SystemProperties.get("ro.com.android.dataroaming",
   2168                                     "false")) ? 1 : 0);
   2169 
   2170             loadBooleanSetting(stmt, Settings.Global.DEVICE_PROVISIONED,
   2171                     R.bool.def_device_provisioned);
   2172 
   2173             final int maxBytes = mContext.getResources().getInteger(
   2174                     R.integer.def_download_manager_max_bytes_over_mobile);
   2175             if (maxBytes > 0) {
   2176                 loadSetting(stmt, Settings.Global.DOWNLOAD_MAX_BYTES_OVER_MOBILE,
   2177                         Integer.toString(maxBytes));
   2178             }
   2179 
   2180             final int recommendedMaxBytes = mContext.getResources().getInteger(
   2181                     R.integer.def_download_manager_recommended_max_bytes_over_mobile);
   2182             if (recommendedMaxBytes > 0) {
   2183                 loadSetting(stmt, Settings.Global.DOWNLOAD_RECOMMENDED_MAX_BYTES_OVER_MOBILE,
   2184                         Integer.toString(recommendedMaxBytes));
   2185             }
   2186 
   2187             // Mobile Data default, based on build
   2188             loadSetting(stmt, Settings.Global.MOBILE_DATA,
   2189                     "true".equalsIgnoreCase(
   2190                             SystemProperties.get("ro.com.android.mobiledata",
   2191                                     "true")) ? 1 : 0);
   2192 
   2193             loadBooleanSetting(stmt, Settings.Global.NETSTATS_ENABLED,
   2194                     R.bool.def_netstats_enabled);
   2195 
   2196             loadBooleanSetting(stmt, Settings.Global.INSTALL_NON_MARKET_APPS,
   2197                     R.bool.def_install_non_market_apps);
   2198 
   2199             loadBooleanSetting(stmt, Settings.Global.USB_MASS_STORAGE_ENABLED,
   2200                     R.bool.def_usb_mass_storage_enabled);
   2201 
   2202             loadIntegerSetting(stmt, Settings.Global.WIFI_MAX_DHCP_RETRY_COUNT,
   2203                     R.integer.def_max_dhcp_retries);
   2204 
   2205             loadBooleanSetting(stmt, Settings.Global.WIFI_DISPLAY_ON,
   2206                     R.bool.def_wifi_display_on);
   2207 
   2208             loadStringSetting(stmt, Settings.Global.LOCK_SOUND,
   2209                     R.string.def_lock_sound);
   2210             loadStringSetting(stmt, Settings.Global.UNLOCK_SOUND,
   2211                     R.string.def_unlock_sound);
   2212             loadIntegerSetting(stmt, Settings.Global.POWER_SOUNDS_ENABLED,
   2213                     R.integer.def_power_sounds_enabled);
   2214             loadStringSetting(stmt, Settings.Global.LOW_BATTERY_SOUND,
   2215                     R.string.def_low_battery_sound);
   2216             loadIntegerSetting(stmt, Settings.Global.DOCK_SOUNDS_ENABLED,
   2217                     R.integer.def_dock_sounds_enabled);
   2218             loadStringSetting(stmt, Settings.Global.DESK_DOCK_SOUND,
   2219                     R.string.def_desk_dock_sound);
   2220             loadStringSetting(stmt, Settings.Global.DESK_UNDOCK_SOUND,
   2221                     R.string.def_desk_undock_sound);
   2222             loadStringSetting(stmt, Settings.Global.CAR_DOCK_SOUND,
   2223                     R.string.def_car_dock_sound);
   2224             loadStringSetting(stmt, Settings.Global.CAR_UNDOCK_SOUND,
   2225                     R.string.def_car_undock_sound);
   2226             loadStringSetting(stmt, Settings.Global.WIRELESS_CHARGING_STARTED_SOUND,
   2227                     R.string.def_wireless_charging_started_sound);
   2228 
   2229             loadIntegerSetting(stmt, Settings.Global.DOCK_AUDIO_MEDIA_ENABLED,
   2230                     R.integer.def_dock_audio_media_enabled);
   2231 
   2232             loadSetting(stmt, Settings.Global.SET_INSTALL_LOCATION, 0);
   2233             loadSetting(stmt, Settings.Global.DEFAULT_INSTALL_LOCATION,
   2234                     PackageHelper.APP_INSTALL_AUTO);
   2235 
   2236             // Set default cdma emergency tone
   2237             loadSetting(stmt, Settings.Global.EMERGENCY_TONE, 0);
   2238 
   2239             // Set default cdma call auto retry
   2240             loadSetting(stmt, Settings.Global.CALL_AUTO_RETRY, 0);
   2241 
   2242             // Set the preferred network mode to 0 = Global, CDMA default
   2243             int type;
   2244             if (TelephonyManager.getLteOnCdmaModeStatic() == PhoneConstants.LTE_ON_CDMA_TRUE) {
   2245                 type = Phone.NT_MODE_GLOBAL;
   2246             } else {
   2247                 type = SystemProperties.getInt("ro.telephony.default_network",
   2248                         RILConstants.PREFERRED_NETWORK_MODE);
   2249             }
   2250             loadSetting(stmt, Settings.Global.PREFERRED_NETWORK_MODE, type);
   2251 
   2252             // --- New global settings start here
   2253         } finally {
   2254             if (stmt != null) stmt.close();
   2255         }
   2256     }
   2257 
   2258     private void loadSetting(SQLiteStatement stmt, String key, Object value) {
   2259         stmt.bindString(1, key);
   2260         stmt.bindString(2, value.toString());
   2261         stmt.execute();
   2262     }
   2263 
   2264     private void loadStringSetting(SQLiteStatement stmt, String key, int resid) {
   2265         loadSetting(stmt, key, mContext.getResources().getString(resid));
   2266     }
   2267 
   2268     private void loadBooleanSetting(SQLiteStatement stmt, String key, int resid) {
   2269         loadSetting(stmt, key,
   2270                 mContext.getResources().getBoolean(resid) ? "1" : "0");
   2271     }
   2272 
   2273     private void loadIntegerSetting(SQLiteStatement stmt, String key, int resid) {
   2274         loadSetting(stmt, key,
   2275                 Integer.toString(mContext.getResources().getInteger(resid)));
   2276     }
   2277 
   2278     private void loadFractionSetting(SQLiteStatement stmt, String key, int resid, int base) {
   2279         loadSetting(stmt, key,
   2280                 Float.toString(mContext.getResources().getFraction(resid, base, base)));
   2281     }
   2282 
   2283     private int getIntValueFromSystem(SQLiteDatabase db, String name, int defaultValue) {
   2284         return getIntValueFromTable(db, TABLE_SYSTEM, name, defaultValue);
   2285     }
   2286 
   2287     private int getIntValueFromTable(SQLiteDatabase db, String table, String name,
   2288             int defaultValue) {
   2289         String value = getStringValueFromTable(db, table, name, null);
   2290         return (value != null) ? Integer.parseInt(value) : defaultValue;
   2291     }
   2292 
   2293     private String getStringValueFromTable(SQLiteDatabase db, String table, String name,
   2294             String defaultValue) {
   2295         Cursor c = null;
   2296         try {
   2297             c = db.query(table, new String[] { Settings.System.VALUE }, "name='" + name + "'",
   2298                     null, null, null, null);
   2299             if (c != null && c.moveToFirst()) {
   2300                 String val = c.getString(0);
   2301                 return val == null ? defaultValue : val;
   2302             }
   2303         } finally {
   2304             if (c != null) c.close();
   2305         }
   2306         return defaultValue;
   2307     }
   2308 }
   2309