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