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