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