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