• 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.PackageManager;
25 import android.content.res.XmlResourceParser;
26 import android.database.Cursor;
27 import android.database.sqlite.SQLiteDatabase;
28 import android.database.sqlite.SQLiteOpenHelper;
29 import android.database.sqlite.SQLiteStatement;
30 import android.media.AudioManager;
31 import android.media.AudioService;
32 import android.net.ConnectivityManager;
33 import android.os.SystemProperties;
34 import android.provider.Settings;
35 import android.provider.Settings.Secure;
36 import android.text.TextUtils;
37 import android.util.Log;
38 
39 import com.android.internal.content.PackageHelper;
40 import com.android.internal.telephony.BaseCommands;
41 import com.android.internal.telephony.Phone;
42 import com.android.internal.telephony.RILConstants;
43 import com.android.internal.util.XmlUtils;
44 import com.android.internal.widget.LockPatternUtils;
45 import com.android.internal.widget.LockPatternView;
46 
47 import org.xmlpull.v1.XmlPullParser;
48 import org.xmlpull.v1.XmlPullParserException;
49 
50 import java.io.IOException;
51 import java.util.HashSet;
52 import java.util.List;
53 
54 /**
55  * Database helper class for {@link SettingsProvider}.
56  * Mostly just has a bit {@link #onCreate} to initialize the database.
57  */
58 public class DatabaseHelper extends SQLiteOpenHelper {
59     private static final String TAG = "SettingsProvider";
60     private static final String DATABASE_NAME = "settings.db";
61 
62     // Please, please please. If you update the database version, check to make sure the
63     // database gets upgraded properly. At a minimum, please confirm that 'upgradeVersion'
64     // is properly propagated through your change.  Not doing so will result in a loss of user
65     // settings.
66     private static final int DATABASE_VERSION = 70;
67 
68     private Context mContext;
69 
70     private static final HashSet<String> mValidTables = new HashSet<String>();
71 
72     static {
73         mValidTables.add("system");
74         mValidTables.add("secure");
75         mValidTables.add("bluetooth_devices");
76         mValidTables.add("bookmarks");
77 
78         // These are old.
79         mValidTables.add("favorites");
80         mValidTables.add("gservices");
81         mValidTables.add("old_favorites");
82     }
83 
DatabaseHelper(Context context)84     public DatabaseHelper(Context context) {
85         super(context, DATABASE_NAME, null, DATABASE_VERSION);
86         mContext = context;
87     }
88 
isValidTable(String name)89     public static boolean isValidTable(String name) {
90         return mValidTables.contains(name);
91     }
92 
createSecureTable(SQLiteDatabase db)93     private void createSecureTable(SQLiteDatabase db) {
94         db.execSQL("CREATE TABLE secure (" +
95                 "_id INTEGER PRIMARY KEY AUTOINCREMENT," +
96                 "name TEXT UNIQUE ON CONFLICT REPLACE," +
97                 "value TEXT" +
98                 ");");
99         db.execSQL("CREATE INDEX secureIndex1 ON secure (name);");
100     }
101 
102     @Override
onCreate(SQLiteDatabase db)103     public void onCreate(SQLiteDatabase db) {
104         db.execSQL("CREATE TABLE system (" +
105                     "_id INTEGER PRIMARY KEY AUTOINCREMENT," +
106                     "name TEXT UNIQUE ON CONFLICT REPLACE," +
107                     "value TEXT" +
108                     ");");
109         db.execSQL("CREATE INDEX systemIndex1 ON system (name);");
110 
111         createSecureTable(db);
112 
113         db.execSQL("CREATE TABLE bluetooth_devices (" +
114                     "_id INTEGER PRIMARY KEY," +
115                     "name TEXT," +
116                     "addr TEXT," +
117                     "channel INTEGER," +
118                     "type INTEGER" +
119                     ");");
120 
121         db.execSQL("CREATE TABLE bookmarks (" +
122                     "_id INTEGER PRIMARY KEY," +
123                     "title TEXT," +
124                     "folder TEXT," +
125                     "intent TEXT," +
126                     "shortcut INTEGER," +
127                     "ordering INTEGER" +
128                     ");");
129 
130         db.execSQL("CREATE INDEX bookmarksIndex1 ON bookmarks (folder);");
131         db.execSQL("CREATE INDEX bookmarksIndex2 ON bookmarks (shortcut);");
132 
133         // Populate bookmarks table with initial bookmarks
134         loadBookmarks(db);
135 
136         // Load initial volume levels into DB
137         loadVolumeLevels(db);
138 
139         // Load inital settings values
140         loadSettings(db);
141     }
142 
143     @Override
onUpgrade(SQLiteDatabase db, int oldVersion, int currentVersion)144     public void onUpgrade(SQLiteDatabase db, int oldVersion, int currentVersion) {
145         Log.w(TAG, "Upgrading settings database from version " + oldVersion + " to "
146                 + currentVersion);
147 
148         int upgradeVersion = oldVersion;
149 
150         // Pattern for upgrade blocks:
151         //
152         //    if (upgradeVersion == [the DATABASE_VERSION you set] - 1) {
153         //        .. your upgrade logic..
154         //        upgradeVersion = [the DATABASE_VERSION you set]
155         //    }
156 
157         if (upgradeVersion == 20) {
158             /*
159              * Version 21 is part of the volume control refresh. There is no
160              * longer a UI-visible for setting notification vibrate on/off (in
161              * our design), but the functionality still exists. Force the
162              * notification vibrate to on.
163              */
164             loadVibrateSetting(db, true);
165 
166             upgradeVersion = 21;
167         }
168 
169         if (upgradeVersion < 22) {
170             upgradeVersion = 22;
171             // Upgrade the lock gesture storage location and format
172             upgradeLockPatternLocation(db);
173         }
174 
175         if (upgradeVersion < 23) {
176             db.execSQL("UPDATE favorites SET iconResource=0 WHERE iconType=0");
177             upgradeVersion = 23;
178         }
179 
180         if (upgradeVersion == 23) {
181             db.beginTransaction();
182             try {
183                 db.execSQL("ALTER TABLE favorites ADD spanX INTEGER");
184                 db.execSQL("ALTER TABLE favorites ADD spanY INTEGER");
185                 // Shortcuts, applications, folders
186                 db.execSQL("UPDATE favorites SET spanX=1, spanY=1 WHERE itemType<=0");
187                 // Photo frames, clocks
188                 db.execSQL(
189                     "UPDATE favorites SET spanX=2, spanY=2 WHERE itemType=1000 or itemType=1002");
190                 // Search boxes
191                 db.execSQL("UPDATE favorites SET spanX=4, spanY=1 WHERE itemType=1001");
192                 db.setTransactionSuccessful();
193             } finally {
194                 db.endTransaction();
195             }
196             upgradeVersion = 24;
197         }
198 
199         if (upgradeVersion == 24) {
200             db.beginTransaction();
201             try {
202                 // The value of the constants for preferring wifi or preferring mobile have been
203                 // swapped, so reload the default.
204                 db.execSQL("DELETE FROM system WHERE name='network_preference'");
205                 db.execSQL("INSERT INTO system ('name', 'value') values ('network_preference', '" +
206                         ConnectivityManager.DEFAULT_NETWORK_PREFERENCE + "')");
207                 db.setTransactionSuccessful();
208             } finally {
209                 db.endTransaction();
210             }
211             upgradeVersion = 25;
212         }
213 
214         if (upgradeVersion == 25) {
215             db.beginTransaction();
216             try {
217                 db.execSQL("ALTER TABLE favorites ADD uri TEXT");
218                 db.execSQL("ALTER TABLE favorites ADD displayMode INTEGER");
219                 db.setTransactionSuccessful();
220             } finally {
221                 db.endTransaction();
222             }
223             upgradeVersion = 26;
224         }
225 
226         if (upgradeVersion == 26) {
227             // This introduces the new secure settings table.
228             db.beginTransaction();
229             try {
230                 createSecureTable(db);
231                 db.setTransactionSuccessful();
232             } finally {
233                 db.endTransaction();
234             }
235             upgradeVersion = 27;
236         }
237 
238         if (upgradeVersion == 27) {
239             String[] settingsToMove = {
240                     Settings.Secure.ADB_ENABLED,
241                     Settings.Secure.ANDROID_ID,
242                     Settings.Secure.BLUETOOTH_ON,
243                     Settings.Secure.DATA_ROAMING,
244                     Settings.Secure.DEVICE_PROVISIONED,
245                     Settings.Secure.HTTP_PROXY,
246                     Settings.Secure.INSTALL_NON_MARKET_APPS,
247                     Settings.Secure.LOCATION_PROVIDERS_ALLOWED,
248                     Settings.Secure.LOGGING_ID,
249                     Settings.Secure.NETWORK_PREFERENCE,
250                     Settings.Secure.PARENTAL_CONTROL_ENABLED,
251                     Settings.Secure.PARENTAL_CONTROL_LAST_UPDATE,
252                     Settings.Secure.PARENTAL_CONTROL_REDIRECT_URL,
253                     Settings.Secure.SETTINGS_CLASSNAME,
254                     Settings.Secure.USB_MASS_STORAGE_ENABLED,
255                     Settings.Secure.USE_GOOGLE_MAIL,
256                     Settings.Secure.WIFI_NETWORKS_AVAILABLE_NOTIFICATION_ON,
257                     Settings.Secure.WIFI_NETWORKS_AVAILABLE_REPEAT_DELAY,
258                     Settings.Secure.WIFI_NUM_OPEN_NETWORKS_KEPT,
259                     Settings.Secure.WIFI_ON,
260                     Settings.Secure.WIFI_WATCHDOG_ACCEPTABLE_PACKET_LOSS_PERCENTAGE,
261                     Settings.Secure.WIFI_WATCHDOG_AP_COUNT,
262                     Settings.Secure.WIFI_WATCHDOG_BACKGROUND_CHECK_DELAY_MS,
263                     Settings.Secure.WIFI_WATCHDOG_BACKGROUND_CHECK_ENABLED,
264                     Settings.Secure.WIFI_WATCHDOG_BACKGROUND_CHECK_TIMEOUT_MS,
265                     Settings.Secure.WIFI_WATCHDOG_INITIAL_IGNORED_PING_COUNT,
266                     Settings.Secure.WIFI_WATCHDOG_MAX_AP_CHECKS,
267                     Settings.Secure.WIFI_WATCHDOG_ON,
268                     Settings.Secure.WIFI_WATCHDOG_PING_COUNT,
269                     Settings.Secure.WIFI_WATCHDOG_PING_DELAY_MS,
270                     Settings.Secure.WIFI_WATCHDOG_PING_TIMEOUT_MS,
271                 };
272             moveFromSystemToSecure(db, settingsToMove);
273             upgradeVersion = 28;
274         }
275 
276         if (upgradeVersion == 28 || upgradeVersion == 29) {
277             // Note: The upgrade to 28 was flawed since it didn't delete the old
278             // setting first before inserting. Combining 28 and 29 with the
279             // fixed version.
280 
281             // This upgrade adds the STREAM_NOTIFICATION type to the list of
282             // types affected by ringer modes (silent, vibrate, etc.)
283             db.beginTransaction();
284             try {
285                 db.execSQL("DELETE FROM system WHERE name='"
286                         + Settings.System.MODE_RINGER_STREAMS_AFFECTED + "'");
287                 int newValue = (1 << AudioManager.STREAM_RING)
288                         | (1 << AudioManager.STREAM_NOTIFICATION)
289                         | (1 << AudioManager.STREAM_SYSTEM);
290                 db.execSQL("INSERT INTO system ('name', 'value') values ('"
291                         + Settings.System.MODE_RINGER_STREAMS_AFFECTED + "', '"
292                         + String.valueOf(newValue) + "')");
293                 db.setTransactionSuccessful();
294             } finally {
295                 db.endTransaction();
296             }
297 
298             upgradeVersion = 30;
299         }
300 
301         if (upgradeVersion == 30) {
302             /*
303              * Upgrade 31 clears the title for all quick launch shortcuts so the
304              * activities' titles will be resolved at display time. Also, the
305              * folder is changed to '@quicklaunch'.
306              */
307             db.beginTransaction();
308             try {
309                 db.execSQL("UPDATE bookmarks SET folder = '@quicklaunch'");
310                 db.execSQL("UPDATE bookmarks SET title = ''");
311                 db.setTransactionSuccessful();
312             } finally {
313                 db.endTransaction();
314             }
315             upgradeVersion = 31;
316         }
317 
318         if (upgradeVersion == 31) {
319             /*
320              * Animations are now managed in preferences, and may be
321              * enabled or disabled based on product resources.
322              */
323             db.beginTransaction();
324             SQLiteStatement stmt = null;
325             try {
326                 db.execSQL("DELETE FROM system WHERE name='"
327                         + Settings.System.WINDOW_ANIMATION_SCALE + "'");
328                 db.execSQL("DELETE FROM system WHERE name='"
329                         + Settings.System.TRANSITION_ANIMATION_SCALE + "'");
330                 stmt = db.compileStatement("INSERT INTO system(name,value)"
331                         + " VALUES(?,?);");
332                 loadDefaultAnimationSettings(stmt);
333                 db.setTransactionSuccessful();
334             } finally {
335                 db.endTransaction();
336                 if (stmt != null) stmt.close();
337             }
338             upgradeVersion = 32;
339         }
340 
341         if (upgradeVersion == 32) {
342             // The Wi-Fi watchdog SSID list is now seeded with the value of
343             // the property ro.com.android.wifi-watchlist
344             String wifiWatchList = SystemProperties.get("ro.com.android.wifi-watchlist");
345             if (!TextUtils.isEmpty(wifiWatchList)) {
346                 db.beginTransaction();
347                 try {
348                     db.execSQL("INSERT OR IGNORE INTO secure(name,value) values('" +
349                             Settings.Secure.WIFI_WATCHDOG_WATCH_LIST + "','" +
350                             wifiWatchList + "');");
351                     db.setTransactionSuccessful();
352                 } finally {
353                     db.endTransaction();
354                 }
355             }
356             upgradeVersion = 33;
357         }
358 
359         if (upgradeVersion == 33) {
360             // Set the default zoom controls to: tap-twice to bring up +/-
361             db.beginTransaction();
362             try {
363                 db.execSQL("INSERT INTO system(name,value) values('zoom','2');");
364                 db.setTransactionSuccessful();
365             } finally {
366                 db.endTransaction();
367             }
368             upgradeVersion = 34;
369         }
370 
371         if (upgradeVersion == 34) {
372             db.beginTransaction();
373             SQLiteStatement stmt = null;
374             try {
375                 stmt = db.compileStatement("INSERT OR IGNORE INTO secure(name,value)"
376                         + " VALUES(?,?);");
377                 loadSecure35Settings(stmt);
378                 db.setTransactionSuccessful();
379             } finally {
380                 db.endTransaction();
381                 if (stmt != null) stmt.close();
382             }
383             upgradeVersion = 35;
384         }
385             // due to a botched merge from donut to eclair, the initialization of ASSISTED_GPS_ENABLED
386             // was accidentally done out of order here.
387             // to fix this, ASSISTED_GPS_ENABLED is now initialized while upgrading from 38 to 39,
388             // and we intentionally do nothing from 35 to 36 now.
389         if (upgradeVersion == 35) {
390             upgradeVersion = 36;
391         }
392 
393         if (upgradeVersion == 36) {
394            // This upgrade adds the STREAM_SYSTEM_ENFORCED type to the list of
395             // types affected by ringer modes (silent, vibrate, etc.)
396             db.beginTransaction();
397             try {
398                 db.execSQL("DELETE FROM system WHERE name='"
399                         + Settings.System.MODE_RINGER_STREAMS_AFFECTED + "'");
400                 int newValue = (1 << AudioManager.STREAM_RING)
401                         | (1 << AudioManager.STREAM_NOTIFICATION)
402                         | (1 << AudioManager.STREAM_SYSTEM)
403                         | (1 << AudioManager.STREAM_SYSTEM_ENFORCED);
404                 db.execSQL("INSERT INTO system ('name', 'value') values ('"
405                         + Settings.System.MODE_RINGER_STREAMS_AFFECTED + "', '"
406                         + String.valueOf(newValue) + "')");
407                 db.setTransactionSuccessful();
408             } finally {
409                 db.endTransaction();
410             }
411             upgradeVersion = 37;
412         }
413 
414         if (upgradeVersion == 37) {
415             db.beginTransaction();
416             SQLiteStatement stmt = null;
417             try {
418                 stmt = db.compileStatement("INSERT OR IGNORE INTO system(name,value)"
419                         + " VALUES(?,?);");
420                 loadStringSetting(stmt, Settings.System.AIRPLANE_MODE_TOGGLEABLE_RADIOS,
421                         R.string.airplane_mode_toggleable_radios);
422                 db.setTransactionSuccessful();
423             } finally {
424                 db.endTransaction();
425                 if (stmt != null) stmt.close();
426             }
427             upgradeVersion = 38;
428         }
429 
430         if (upgradeVersion == 38) {
431             db.beginTransaction();
432             try {
433                 String value =
434                         mContext.getResources().getBoolean(R.bool.assisted_gps_enabled) ? "1" : "0";
435                 db.execSQL("INSERT OR IGNORE INTO secure(name,value) values('" +
436                         Settings.Secure.ASSISTED_GPS_ENABLED + "','" + value + "');");
437                 db.setTransactionSuccessful();
438             } finally {
439                 db.endTransaction();
440             }
441 
442             upgradeVersion = 39;
443         }
444 
445         if (upgradeVersion == 39) {
446             upgradeAutoBrightness(db);
447             upgradeVersion = 40;
448         }
449 
450         if (upgradeVersion == 40) {
451             /*
452              * All animations are now turned on by default!
453              */
454             db.beginTransaction();
455             SQLiteStatement stmt = null;
456             try {
457                 db.execSQL("DELETE FROM system WHERE name='"
458                         + Settings.System.WINDOW_ANIMATION_SCALE + "'");
459                 db.execSQL("DELETE FROM system WHERE name='"
460                         + Settings.System.TRANSITION_ANIMATION_SCALE + "'");
461                 stmt = db.compileStatement("INSERT INTO system(name,value)"
462                         + " VALUES(?,?);");
463                 loadDefaultAnimationSettings(stmt);
464                 db.setTransactionSuccessful();
465             } finally {
466                 db.endTransaction();
467                 if (stmt != null) stmt.close();
468             }
469             upgradeVersion = 41;
470         }
471 
472         if (upgradeVersion == 41) {
473             /*
474              * Initialize newly public haptic feedback setting
475              */
476             db.beginTransaction();
477             SQLiteStatement stmt = null;
478             try {
479                 db.execSQL("DELETE FROM system WHERE name='"
480                         + Settings.System.HAPTIC_FEEDBACK_ENABLED + "'");
481                 stmt = db.compileStatement("INSERT INTO system(name,value)"
482                         + " VALUES(?,?);");
483                 loadDefaultHapticSettings(stmt);
484                 db.setTransactionSuccessful();
485             } finally {
486                 db.endTransaction();
487                 if (stmt != null) stmt.close();
488             }
489             upgradeVersion = 42;
490         }
491 
492         if (upgradeVersion == 42) {
493             /*
494              * Initialize new notification pulse setting
495              */
496             db.beginTransaction();
497             SQLiteStatement stmt = null;
498             try {
499                 stmt = db.compileStatement("INSERT INTO system(name,value)"
500                         + " VALUES(?,?);");
501                 loadBooleanSetting(stmt, Settings.System.NOTIFICATION_LIGHT_PULSE,
502                         R.bool.def_notification_pulse);
503                 db.setTransactionSuccessful();
504             } finally {
505                 db.endTransaction();
506                 if (stmt != null) stmt.close();
507             }
508             upgradeVersion = 43;
509         }
510 
511         if (upgradeVersion == 43) {
512             /*
513              * This upgrade stores bluetooth volume separately from voice volume
514              */
515             db.beginTransaction();
516             SQLiteStatement stmt = null;
517             try {
518                 stmt = db.compileStatement("INSERT OR IGNORE INTO system(name,value)"
519                         + " VALUES(?,?);");
520                 loadSetting(stmt, Settings.System.VOLUME_BLUETOOTH_SCO,
521                         AudioManager.DEFAULT_STREAM_VOLUME[AudioManager.STREAM_BLUETOOTH_SCO]);
522                 db.setTransactionSuccessful();
523             } finally {
524                 db.endTransaction();
525                 if (stmt != null) stmt.close();
526             }
527             upgradeVersion = 44;
528         }
529 
530         if (upgradeVersion == 44) {
531             /*
532              * Gservices was moved into vendor/google.
533              */
534             db.execSQL("DROP TABLE IF EXISTS gservices");
535             db.execSQL("DROP INDEX IF EXISTS gservicesIndex1");
536             upgradeVersion = 45;
537         }
538 
539         if (upgradeVersion == 45) {
540              /*
541               * New settings for MountService
542               */
543             db.beginTransaction();
544             try {
545                 db.execSQL("INSERT INTO secure(name,value) values('" +
546                         Settings.Secure.MOUNT_PLAY_NOTIFICATION_SND + "','1');");
547                 db.execSQL("INSERT INTO secure(name,value) values('" +
548                         Settings.Secure.MOUNT_UMS_AUTOSTART + "','0');");
549                 db.execSQL("INSERT INTO secure(name,value) values('" +
550                         Settings.Secure.MOUNT_UMS_PROMPT + "','1');");
551                 db.execSQL("INSERT INTO secure(name,value) values('" +
552                         Settings.Secure.MOUNT_UMS_NOTIFY_ENABLED + "','1');");
553                 db.setTransactionSuccessful();
554             } finally {
555                 db.endTransaction();
556             }
557             upgradeVersion = 46;
558         }
559 
560         if (upgradeVersion == 46) {
561             /*
562              * The password mode constants have changed; reset back to no
563              * password.
564              */
565             db.beginTransaction();
566             try {
567                 db.execSQL("DELETE FROM system WHERE name='lockscreen.password_type';");
568                 db.setTransactionSuccessful();
569             } finally {
570                 db.endTransaction();
571             }
572            upgradeVersion = 47;
573        }
574 
575 
576         if (upgradeVersion == 47) {
577             /*
578              * The password mode constants have changed again; reset back to no
579              * password.
580              */
581             db.beginTransaction();
582             try {
583                 db.execSQL("DELETE FROM system WHERE name='lockscreen.password_type';");
584                 db.setTransactionSuccessful();
585             } finally {
586                 db.endTransaction();
587             }
588            upgradeVersion = 48;
589        }
590 
591        if (upgradeVersion == 48) {
592            /*
593             * Default recognition service no longer initialized here,
594             * moved to RecognitionManagerService.
595             */
596            upgradeVersion = 49;
597        }
598 
599        if (upgradeVersion == 49) {
600            /*
601             * New settings for new user interface noises.
602             */
603            db.beginTransaction();
604            SQLiteStatement stmt = null;
605            try {
606                 stmt = db.compileStatement("INSERT INTO system(name,value)"
607                         + " VALUES(?,?);");
608                 loadUISoundEffectsSettings(stmt);
609                 db.setTransactionSuccessful();
610             } finally {
611                 db.endTransaction();
612                 if (stmt != null) stmt.close();
613             }
614 
615            upgradeVersion = 50;
616        }
617 
618        if (upgradeVersion == 50) {
619            /*
620             * Install location no longer initiated here.
621             */
622            upgradeVersion = 51;
623        }
624 
625        if (upgradeVersion == 51) {
626            /* Move the lockscreen related settings to Secure, including some private ones. */
627            String[] settingsToMove = {
628                    Secure.LOCK_PATTERN_ENABLED,
629                    Secure.LOCK_PATTERN_VISIBLE,
630                    Secure.LOCK_PATTERN_TACTILE_FEEDBACK_ENABLED,
631                    "lockscreen.password_type",
632                    "lockscreen.lockoutattemptdeadline",
633                    "lockscreen.patterneverchosen",
634                    "lock_pattern_autolock",
635                    "lockscreen.lockedoutpermanently",
636                    "lockscreen.password_salt"
637            };
638            moveFromSystemToSecure(db, settingsToMove);
639            upgradeVersion = 52;
640        }
641 
642         if (upgradeVersion == 52) {
643             // new vibration/silent mode settings
644             db.beginTransaction();
645             SQLiteStatement stmt = null;
646             try {
647                 stmt = db.compileStatement("INSERT INTO system(name,value)"
648                         + " VALUES(?,?);");
649                 loadBooleanSetting(stmt, Settings.System.VIBRATE_IN_SILENT,
650                         R.bool.def_vibrate_in_silent);
651                 db.setTransactionSuccessful();
652             } finally {
653                 db.endTransaction();
654                 if (stmt != null) stmt.close();
655             }
656 
657             upgradeVersion = 53;
658         }
659 
660         if (upgradeVersion == 53) {
661             /*
662              * New settings for set install location UI no longer initiated here.
663              */
664             upgradeVersion = 54;
665         }
666 
667         if (upgradeVersion == 54) {
668             /*
669              * Update the screen timeout value if set to never
670              */
671             db.beginTransaction();
672             try {
673                 upgradeScreenTimeoutFromNever(db);
674                 db.setTransactionSuccessful();
675             } finally {
676                 db.endTransaction();
677             }
678 
679             upgradeVersion = 55;
680         }
681 
682         if (upgradeVersion == 55) {
683             /* Move the install location settings. */
684             String[] settingsToMove = {
685                     Secure.SET_INSTALL_LOCATION,
686                     Secure.DEFAULT_INSTALL_LOCATION
687             };
688             moveFromSystemToSecure(db, settingsToMove);
689             db.beginTransaction();
690             SQLiteStatement stmt = null;
691             try {
692                 stmt = db.compileStatement("INSERT INTO system(name,value)"
693                         + " VALUES(?,?);");
694                 loadSetting(stmt, Secure.SET_INSTALL_LOCATION, 0);
695                 loadSetting(stmt, Secure.DEFAULT_INSTALL_LOCATION,
696                         PackageHelper.APP_INSTALL_AUTO);
697                 db.setTransactionSuccessful();
698              } finally {
699                  db.endTransaction();
700                  if (stmt != null) stmt.close();
701              }
702             upgradeVersion = 56;
703         }
704 
705         if (upgradeVersion == 56) {
706             /*
707              * Add Bluetooth to list of toggleable radios in airplane mode
708              */
709             db.beginTransaction();
710             SQLiteStatement stmt = null;
711             try {
712                 db.execSQL("DELETE FROM system WHERE name='"
713                         + Settings.System.AIRPLANE_MODE_TOGGLEABLE_RADIOS + "'");
714                 stmt = db.compileStatement("INSERT OR IGNORE INTO system(name,value)"
715                         + " VALUES(?,?);");
716                 loadStringSetting(stmt, Settings.System.AIRPLANE_MODE_TOGGLEABLE_RADIOS,
717                         R.string.airplane_mode_toggleable_radios);
718                 db.setTransactionSuccessful();
719             } finally {
720                 db.endTransaction();
721                 if (stmt != null) stmt.close();
722             }
723             upgradeVersion = 57;
724         }
725 
726         if (upgradeVersion == 57) {
727             /*
728              * New settings to:
729              *  1. Enable injection of accessibility scripts in WebViews.
730              *  2. Define the key bindings for traversing web content in WebViews.
731              */
732             db.beginTransaction();
733             SQLiteStatement stmt = null;
734             try {
735                 stmt = db.compileStatement("INSERT INTO secure(name,value)"
736                         + " VALUES(?,?);");
737                 loadBooleanSetting(stmt, Settings.Secure.ACCESSIBILITY_SCRIPT_INJECTION,
738                         R.bool.def_accessibility_script_injection);
739                 stmt.close();
740                 stmt = db.compileStatement("INSERT INTO secure(name,value)"
741                         + " VALUES(?,?);");
742                 loadStringSetting(stmt, Settings.Secure.ACCESSIBILITY_WEB_CONTENT_KEY_BINDINGS,
743                         R.string.def_accessibility_web_content_key_bindings);
744                 db.setTransactionSuccessful();
745             } finally {
746                 db.endTransaction();
747                 if (stmt != null) stmt.close();
748             }
749             upgradeVersion = 58;
750         }
751 
752         if (upgradeVersion == 58) {
753             /* Add default for new Auto Time Zone */
754             db.beginTransaction();
755             SQLiteStatement stmt = null;
756             try {
757                 stmt = db.compileStatement("INSERT INTO secure(name,value)"
758                         + " VALUES(?,?);");
759                 loadBooleanSetting(stmt, Settings.System.AUTO_TIME_ZONE,
760                         R.bool.def_auto_time_zone); // Sync timezone to NITZ
761                 db.setTransactionSuccessful();
762             } finally {
763                 db.endTransaction();
764                 if (stmt != null) stmt.close();
765             }
766             upgradeVersion = 59;
767         }
768 
769         if (upgradeVersion == 59) {
770             // Persistence for the rotation lock feature.
771             db.beginTransaction();
772             SQLiteStatement stmt = null;
773             try {
774                 stmt = db.compileStatement("INSERT INTO system(name,value)"
775                         + " VALUES(?,?);");
776                 loadBooleanSetting(stmt, Settings.System.USER_ROTATION,
777                         R.integer.def_user_rotation); // should be zero degrees
778                 db.setTransactionSuccessful();
779             } finally {
780                 db.endTransaction();
781                 if (stmt != null) stmt.close();
782             }
783             upgradeVersion = 60;
784         }
785 
786         if (upgradeVersion == 60) {
787             upgradeScreenTimeout(db);
788             upgradeVersion = 61;
789         }
790 
791         if (upgradeVersion == 61) {
792             upgradeScreenTimeout(db);
793             upgradeVersion = 62;
794         }
795 
796         // Change the default for screen auto-brightness mode
797         if (upgradeVersion == 62) {
798             upgradeAutoBrightness(db);
799             upgradeVersion = 63;
800         }
801 
802         if (upgradeVersion == 63) {
803             // This upgrade adds the STREAM_MUSIC type to the list of
804              // types affected by ringer modes (silent, vibrate, etc.)
805              db.beginTransaction();
806              try {
807                  db.execSQL("DELETE FROM system WHERE name='"
808                          + Settings.System.MODE_RINGER_STREAMS_AFFECTED + "'");
809                  int newValue = (1 << AudioManager.STREAM_RING)
810                          | (1 << AudioManager.STREAM_NOTIFICATION)
811                          | (1 << AudioManager.STREAM_SYSTEM)
812                          | (1 << AudioManager.STREAM_SYSTEM_ENFORCED)
813                          | (1 << AudioManager.STREAM_MUSIC);
814                  db.execSQL("INSERT INTO system ('name', 'value') values ('"
815                          + Settings.System.MODE_RINGER_STREAMS_AFFECTED + "', '"
816                          + String.valueOf(newValue) + "')");
817                  db.setTransactionSuccessful();
818              } finally {
819                  db.endTransaction();
820              }
821              upgradeVersion = 64;
822          }
823 
824         if (upgradeVersion == 64) {
825             // New setting to configure the long press timeout.
826             db.beginTransaction();
827             SQLiteStatement stmt = null;
828             try {
829                 stmt = db.compileStatement("INSERT INTO secure(name,value)"
830                         + " VALUES(?,?);");
831                 loadIntegerSetting(stmt, Settings.Secure.LONG_PRESS_TIMEOUT,
832                         R.integer.def_long_press_timeout_millis);
833                 stmt.close();
834                 db.setTransactionSuccessful();
835             } finally {
836                 db.endTransaction();
837                 if (stmt != null) stmt.close();
838             }
839             upgradeVersion = 65;
840         }
841 
842         if (upgradeVersion == 65) {
843             /*
844              * Animations are removed from Settings. Turned on by default
845              */
846             db.beginTransaction();
847             SQLiteStatement stmt = null;
848             try {
849                 db.execSQL("DELETE FROM system WHERE name='"
850                         + Settings.System.WINDOW_ANIMATION_SCALE + "'");
851                 db.execSQL("DELETE FROM system WHERE name='"
852                         + Settings.System.TRANSITION_ANIMATION_SCALE + "'");
853                 stmt = db.compileStatement("INSERT INTO system(name,value)"
854                         + " VALUES(?,?);");
855                 loadDefaultAnimationSettings(stmt);
856                 db.setTransactionSuccessful();
857             } finally {
858                 db.endTransaction();
859                 if (stmt != null) stmt.close();
860             }
861             upgradeVersion = 66;
862         }
863 
864         if (upgradeVersion == 66) {
865             // This upgrade makes sure that MODE_RINGER_STREAMS_AFFECTED is set
866             // according to device voice capability
867             db.beginTransaction();
868             try {
869                 int ringerModeAffectedStreams = (1 << AudioManager.STREAM_RING) |
870                                                 (1 << AudioManager.STREAM_NOTIFICATION) |
871                                                 (1 << AudioManager.STREAM_SYSTEM) |
872                                                 (1 << AudioManager.STREAM_SYSTEM_ENFORCED);
873                 if (!mContext.getResources().getBoolean(
874                         com.android.internal.R.bool.config_voice_capable)) {
875                     ringerModeAffectedStreams |= (1 << AudioManager.STREAM_MUSIC);
876                 }
877                 db.execSQL("DELETE FROM system WHERE name='"
878                         + Settings.System.MODE_RINGER_STREAMS_AFFECTED + "'");
879                 db.execSQL("INSERT INTO system ('name', 'value') values ('"
880                         + Settings.System.MODE_RINGER_STREAMS_AFFECTED + "', '"
881                         + String.valueOf(ringerModeAffectedStreams) + "')");
882                 db.setTransactionSuccessful();
883             } finally {
884                 db.endTransaction();
885             }
886             upgradeVersion = 67;
887         }
888 
889         if (upgradeVersion == 67) {
890             // New setting to enable touch exploration.
891             db.beginTransaction();
892             SQLiteStatement stmt = null;
893             try {
894                 stmt = db.compileStatement("INSERT INTO secure(name,value)"
895                         + " VALUES(?,?);");
896                 loadBooleanSetting(stmt, Settings.Secure.TOUCH_EXPLORATION_ENABLED,
897                         R.bool.def_touch_exploration_enabled);
898                 stmt.close();
899                 db.setTransactionSuccessful();
900             } finally {
901                 db.endTransaction();
902                 if (stmt != null) stmt.close();
903             }
904             upgradeVersion = 68;
905         }
906 
907         if (upgradeVersion == 68) {
908             // Enable all system sounds by default
909             db.beginTransaction();
910             try {
911                 db.execSQL("DELETE FROM system WHERE name='"
912                         + Settings.System.NOTIFICATIONS_USE_RING_VOLUME + "'");
913                 db.setTransactionSuccessful();
914             } finally {
915                 db.endTransaction();
916             }
917             upgradeVersion = 69;
918         }
919 
920         if (upgradeVersion == 69) {
921             // Add RADIO_NFC to AIRPLANE_MODE_RADIO and AIRPLANE_MODE_TOGGLEABLE_RADIOS
922             String airplaneRadios = mContext.getResources().getString(
923                     R.string.def_airplane_mode_radios);
924             String toggleableRadios = mContext.getResources().getString(
925                     R.string.airplane_mode_toggleable_radios);
926             db.beginTransaction();
927             try {
928                 db.execSQL("UPDATE system SET value='" + airplaneRadios + "' " +
929                         "WHERE name='" + Settings.System.AIRPLANE_MODE_RADIOS + "'");
930                 db.execSQL("UPDATE system SET value='" + toggleableRadios + "' " +
931                         "WHERE name='" + Settings.System.AIRPLANE_MODE_TOGGLEABLE_RADIOS + "'");
932                 db.setTransactionSuccessful();
933             } finally {
934                 db.endTransaction();
935             }
936             upgradeVersion = 70;
937         }
938 
939         // *** Remember to update DATABASE_VERSION above!
940 
941         if (upgradeVersion != currentVersion) {
942             Log.w(TAG, "Got stuck trying to upgrade from version " + upgradeVersion
943                     + ", must wipe the settings provider");
944             db.execSQL("DROP TABLE IF EXISTS system");
945             db.execSQL("DROP INDEX IF EXISTS systemIndex1");
946             db.execSQL("DROP TABLE IF EXISTS secure");
947             db.execSQL("DROP INDEX IF EXISTS secureIndex1");
948             db.execSQL("DROP TABLE IF EXISTS gservices");
949             db.execSQL("DROP INDEX IF EXISTS gservicesIndex1");
950             db.execSQL("DROP TABLE IF EXISTS bluetooth_devices");
951             db.execSQL("DROP TABLE IF EXISTS bookmarks");
952             db.execSQL("DROP INDEX IF EXISTS bookmarksIndex1");
953             db.execSQL("DROP INDEX IF EXISTS bookmarksIndex2");
954             db.execSQL("DROP TABLE IF EXISTS favorites");
955             onCreate(db);
956 
957             // Added for diagnosing settings.db wipes after the fact
958             String wipeReason = oldVersion + "/" + upgradeVersion + "/" + currentVersion;
959             db.execSQL("INSERT INTO secure(name,value) values('" +
960                     "wiped_db_reason" + "','" + wipeReason + "');");
961         }
962     }
963 
moveFromSystemToSecure(SQLiteDatabase db, String [] settingsToMove)964     private void moveFromSystemToSecure(SQLiteDatabase db, String [] settingsToMove) {
965         // Copy settings values from 'system' to 'secure' and delete them from 'system'
966         SQLiteStatement insertStmt = null;
967         SQLiteStatement deleteStmt = null;
968 
969         db.beginTransaction();
970         try {
971             insertStmt =
972                 db.compileStatement("INSERT INTO secure (name,value) SELECT name,value FROM "
973                     + "system WHERE name=?");
974             deleteStmt = db.compileStatement("DELETE FROM system WHERE name=?");
975 
976 
977             for (String setting : settingsToMove) {
978                 insertStmt.bindString(1, setting);
979                 insertStmt.execute();
980 
981                 deleteStmt.bindString(1, setting);
982                 deleteStmt.execute();
983             }
984             db.setTransactionSuccessful();
985         } finally {
986             db.endTransaction();
987             if (insertStmt != null) {
988                 insertStmt.close();
989             }
990             if (deleteStmt != null) {
991                 deleteStmt.close();
992             }
993         }
994     }
995 
upgradeLockPatternLocation(SQLiteDatabase db)996     private void upgradeLockPatternLocation(SQLiteDatabase db) {
997         Cursor c = db.query("system", new String[] {"_id", "value"}, "name='lock_pattern'",
998                 null, null, null, null);
999         if (c.getCount() > 0) {
1000             c.moveToFirst();
1001             String lockPattern = c.getString(1);
1002             if (!TextUtils.isEmpty(lockPattern)) {
1003                 // Convert lock pattern
1004                 try {
1005                     LockPatternUtils lpu = new LockPatternUtils(mContext);
1006                     List<LockPatternView.Cell> cellPattern =
1007                             LockPatternUtils.stringToPattern(lockPattern);
1008                     lpu.saveLockPattern(cellPattern);
1009                 } catch (IllegalArgumentException e) {
1010                     // Don't want corrupted lock pattern to hang the reboot process
1011                 }
1012             }
1013             c.close();
1014             db.delete("system", "name='lock_pattern'", null);
1015         } else {
1016             c.close();
1017         }
1018     }
1019 
upgradeScreenTimeoutFromNever(SQLiteDatabase db)1020     private void upgradeScreenTimeoutFromNever(SQLiteDatabase db) {
1021         // See if the timeout is -1 (for "Never").
1022         Cursor c = db.query("system", new String[] { "_id", "value" }, "name=? AND value=?",
1023                 new String[] { Settings.System.SCREEN_OFF_TIMEOUT, "-1" },
1024                 null, null, null);
1025 
1026         SQLiteStatement stmt = null;
1027         if (c.getCount() > 0) {
1028             c.close();
1029             try {
1030                 stmt = db.compileStatement("INSERT OR REPLACE INTO system(name,value)"
1031                         + " VALUES(?,?);");
1032 
1033                 // Set the timeout to 30 minutes in milliseconds
1034                 loadSetting(stmt, Settings.System.SCREEN_OFF_TIMEOUT,
1035                         Integer.toString(30 * 60 * 1000));
1036             } finally {
1037                 if (stmt != null) stmt.close();
1038             }
1039         } else {
1040             c.close();
1041         }
1042     }
1043 
upgradeScreenTimeout(SQLiteDatabase db)1044     private void upgradeScreenTimeout(SQLiteDatabase db) {
1045         // Change screen timeout to current default
1046         db.beginTransaction();
1047         SQLiteStatement stmt = null;
1048         try {
1049             stmt = db.compileStatement("INSERT OR REPLACE INTO system(name,value)"
1050                     + " VALUES(?,?);");
1051             loadIntegerSetting(stmt, Settings.System.SCREEN_OFF_TIMEOUT,
1052                     R.integer.def_screen_off_timeout);
1053             db.setTransactionSuccessful();
1054         } finally {
1055             db.endTransaction();
1056             if (stmt != null)
1057                 stmt.close();
1058         }
1059     }
1060 
upgradeAutoBrightness(SQLiteDatabase db)1061     private void upgradeAutoBrightness(SQLiteDatabase db) {
1062         db.beginTransaction();
1063         try {
1064             String value =
1065                     mContext.getResources().getBoolean(
1066                     R.bool.def_screen_brightness_automatic_mode) ? "1" : "0";
1067             db.execSQL("INSERT OR REPLACE INTO system(name,value) values('" +
1068                     Settings.System.SCREEN_BRIGHTNESS_MODE + "','" + value + "');");
1069             db.setTransactionSuccessful();
1070         } finally {
1071             db.endTransaction();
1072         }
1073     }
1074 
1075     /**
1076      * Loads the default set of bookmarked shortcuts from an xml file.
1077      *
1078      * @param db The database to write the values into
1079      * @param startingIndex The zero-based position at which bookmarks in this file should begin
1080      */
loadBookmarks(SQLiteDatabase db, int startingIndex)1081     private int loadBookmarks(SQLiteDatabase db, int startingIndex) {
1082         Intent intent = new Intent(Intent.ACTION_MAIN, null);
1083         intent.addCategory(Intent.CATEGORY_LAUNCHER);
1084         ContentValues values = new ContentValues();
1085 
1086         PackageManager packageManager = mContext.getPackageManager();
1087         int i = startingIndex;
1088 
1089         try {
1090             XmlResourceParser parser = mContext.getResources().getXml(R.xml.bookmarks);
1091             XmlUtils.beginDocument(parser, "bookmarks");
1092 
1093             final int depth = parser.getDepth();
1094             int type;
1095 
1096             while (((type = parser.next()) != XmlPullParser.END_TAG ||
1097                     parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
1098 
1099                 if (type != XmlPullParser.START_TAG) {
1100                     continue;
1101                 }
1102 
1103                 String name = parser.getName();
1104                 if (!"bookmark".equals(name)) {
1105                     break;
1106                 }
1107 
1108                 String pkg = parser.getAttributeValue(null, "package");
1109                 String cls = parser.getAttributeValue(null, "class");
1110                 String shortcutStr = parser.getAttributeValue(null, "shortcut");
1111 
1112                 int shortcutValue = shortcutStr.charAt(0);
1113                 if (TextUtils.isEmpty(shortcutStr)) {
1114                     Log.w(TAG, "Unable to get shortcut for: " + pkg + "/" + cls);
1115                 }
1116 
1117                 ActivityInfo info = null;
1118                 ComponentName cn = new ComponentName(pkg, cls);
1119                 try {
1120                     info = packageManager.getActivityInfo(cn, 0);
1121                 } catch (PackageManager.NameNotFoundException e) {
1122                     String[] packages = packageManager.canonicalToCurrentPackageNames(
1123                             new String[] { pkg });
1124                     cn = new ComponentName(packages[0], cls);
1125                     try {
1126                         info = packageManager.getActivityInfo(cn, 0);
1127                     } catch (PackageManager.NameNotFoundException e1) {
1128                         Log.w(TAG, "Unable to add bookmark: " + pkg + "/" + cls, e);
1129                     }
1130                 }
1131 
1132                 if (info != null) {
1133                     intent.setComponent(cn);
1134                     intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1135                     values.put(Settings.Bookmarks.INTENT, intent.toUri(0));
1136                     values.put(Settings.Bookmarks.TITLE,
1137                             info.loadLabel(packageManager).toString());
1138                     values.put(Settings.Bookmarks.SHORTCUT, shortcutValue);
1139                     db.insert("bookmarks", null, values);
1140                     i++;
1141                 }
1142             }
1143         } catch (XmlPullParserException e) {
1144             Log.w(TAG, "Got execption parsing bookmarks.", e);
1145         } catch (IOException e) {
1146             Log.w(TAG, "Got execption parsing bookmarks.", e);
1147         }
1148 
1149         return i;
1150     }
1151 
1152     /**
1153      * Loads the default set of bookmark packages.
1154      *
1155      * @param db The database to write the values into
1156      */
loadBookmarks(SQLiteDatabase db)1157     private void loadBookmarks(SQLiteDatabase db) {
1158         loadBookmarks(db, 0);
1159     }
1160 
1161     /**
1162      * Loads the default volume levels. It is actually inserting the index of
1163      * the volume array for each of the volume controls.
1164      *
1165      * @param db the database to insert the volume levels into
1166      */
loadVolumeLevels(SQLiteDatabase db)1167     private void loadVolumeLevels(SQLiteDatabase db) {
1168         SQLiteStatement stmt = null;
1169         try {
1170             stmt = db.compileStatement("INSERT OR IGNORE INTO system(name,value)"
1171                     + " VALUES(?,?);");
1172 
1173             loadSetting(stmt, Settings.System.VOLUME_MUSIC,
1174                     AudioManager.DEFAULT_STREAM_VOLUME[AudioManager.STREAM_MUSIC]);
1175             loadSetting(stmt, Settings.System.VOLUME_RING,
1176                     AudioManager.DEFAULT_STREAM_VOLUME[AudioManager.STREAM_RING]);
1177             loadSetting(stmt, Settings.System.VOLUME_SYSTEM,
1178                     AudioManager.DEFAULT_STREAM_VOLUME[AudioManager.STREAM_SYSTEM]);
1179             loadSetting(
1180                     stmt,
1181                     Settings.System.VOLUME_VOICE,
1182                     AudioManager.DEFAULT_STREAM_VOLUME[AudioManager.STREAM_VOICE_CALL]);
1183             loadSetting(stmt, Settings.System.VOLUME_ALARM,
1184                     AudioManager.DEFAULT_STREAM_VOLUME[AudioManager.STREAM_ALARM]);
1185             loadSetting(
1186                     stmt,
1187                     Settings.System.VOLUME_NOTIFICATION,
1188                     AudioManager.DEFAULT_STREAM_VOLUME[AudioManager.STREAM_NOTIFICATION]);
1189             loadSetting(
1190                     stmt,
1191                     Settings.System.VOLUME_BLUETOOTH_SCO,
1192                     AudioManager.DEFAULT_STREAM_VOLUME[AudioManager.STREAM_BLUETOOTH_SCO]);
1193 
1194             loadSetting(stmt, Settings.System.MODE_RINGER,
1195                     AudioManager.RINGER_MODE_NORMAL);
1196 
1197             loadVibrateSetting(db, false);
1198 
1199             // By default:
1200             // - ringtones, notification, system and music streams are affected by ringer mode
1201             // on non voice capable devices (tablets)
1202             // - ringtones, notification and system streams are affected by ringer mode
1203             // on voice capable devices (phones)
1204             int ringerModeAffectedStreams = (1 << AudioManager.STREAM_RING) |
1205                                             (1 << AudioManager.STREAM_NOTIFICATION) |
1206                                             (1 << AudioManager.STREAM_SYSTEM) |
1207                                             (1 << AudioManager.STREAM_SYSTEM_ENFORCED);
1208             if (!mContext.getResources().getBoolean(
1209                     com.android.internal.R.bool.config_voice_capable)) {
1210                 ringerModeAffectedStreams |= (1 << AudioManager.STREAM_MUSIC);
1211             }
1212             loadSetting(stmt, Settings.System.MODE_RINGER_STREAMS_AFFECTED,
1213                     ringerModeAffectedStreams);
1214 
1215             loadSetting(stmt, Settings.System.MUTE_STREAMS_AFFECTED,
1216                     ((1 << AudioManager.STREAM_MUSIC) |
1217                      (1 << AudioManager.STREAM_RING) |
1218                      (1 << AudioManager.STREAM_NOTIFICATION) |
1219                      (1 << AudioManager.STREAM_SYSTEM)));
1220         } finally {
1221             if (stmt != null) stmt.close();
1222         }
1223     }
1224 
loadVibrateSetting(SQLiteDatabase db, boolean deleteOld)1225     private void loadVibrateSetting(SQLiteDatabase db, boolean deleteOld) {
1226         if (deleteOld) {
1227             db.execSQL("DELETE FROM system WHERE name='" + Settings.System.VIBRATE_ON + "'");
1228         }
1229 
1230         SQLiteStatement stmt = null;
1231         try {
1232             stmt = db.compileStatement("INSERT OR IGNORE INTO system(name,value)"
1233                     + " VALUES(?,?);");
1234 
1235             // Vibrate off by default for ringer, on for notification
1236             int vibrate = 0;
1237             vibrate = AudioService.getValueForVibrateSetting(vibrate,
1238                     AudioManager.VIBRATE_TYPE_NOTIFICATION, AudioManager.VIBRATE_SETTING_ON);
1239             vibrate |= AudioService.getValueForVibrateSetting(vibrate,
1240                     AudioManager.VIBRATE_TYPE_RINGER, AudioManager.VIBRATE_SETTING_OFF);
1241             loadSetting(stmt, Settings.System.VIBRATE_ON, vibrate);
1242         } finally {
1243             if (stmt != null) stmt.close();
1244         }
1245     }
1246 
loadSettings(SQLiteDatabase db)1247     private void loadSettings(SQLiteDatabase db) {
1248         loadSystemSettings(db);
1249         loadSecureSettings(db);
1250     }
1251 
loadSystemSettings(SQLiteDatabase db)1252     private void loadSystemSettings(SQLiteDatabase db) {
1253         SQLiteStatement stmt = null;
1254         try {
1255             stmt = db.compileStatement("INSERT OR IGNORE INTO system(name,value)"
1256                     + " VALUES(?,?);");
1257 
1258             loadBooleanSetting(stmt, Settings.System.DIM_SCREEN,
1259                     R.bool.def_dim_screen);
1260             loadSetting(stmt, Settings.System.STAY_ON_WHILE_PLUGGED_IN,
1261                     "1".equals(SystemProperties.get("ro.kernel.qemu")) ? 1 : 0);
1262             loadIntegerSetting(stmt, Settings.System.SCREEN_OFF_TIMEOUT,
1263                     R.integer.def_screen_off_timeout);
1264 
1265             // Set default cdma emergency tone
1266             loadSetting(stmt, Settings.System.EMERGENCY_TONE, 0);
1267 
1268             // Set default cdma call auto retry
1269             loadSetting(stmt, Settings.System.CALL_AUTO_RETRY, 0);
1270 
1271             // Set default cdma DTMF type
1272             loadSetting(stmt, Settings.System.DTMF_TONE_TYPE_WHEN_DIALING, 0);
1273 
1274             // Set default hearing aid
1275             loadSetting(stmt, Settings.System.HEARING_AID, 0);
1276 
1277             // Set default tty mode
1278             loadSetting(stmt, Settings.System.TTY_MODE, 0);
1279 
1280             loadBooleanSetting(stmt, Settings.System.AIRPLANE_MODE_ON,
1281                     R.bool.def_airplane_mode_on);
1282 
1283             loadStringSetting(stmt, Settings.System.AIRPLANE_MODE_RADIOS,
1284                     R.string.def_airplane_mode_radios);
1285 
1286             loadStringSetting(stmt, Settings.System.AIRPLANE_MODE_TOGGLEABLE_RADIOS,
1287                     R.string.airplane_mode_toggleable_radios);
1288 
1289             loadBooleanSetting(stmt, Settings.System.AUTO_TIME,
1290                     R.bool.def_auto_time); // Sync time to NITZ
1291 
1292             loadBooleanSetting(stmt, Settings.System.AUTO_TIME_ZONE,
1293                     R.bool.def_auto_time_zone); // Sync timezone to NITZ
1294 
1295             loadIntegerSetting(stmt, Settings.System.SCREEN_BRIGHTNESS,
1296                     R.integer.def_screen_brightness);
1297 
1298             loadBooleanSetting(stmt, Settings.System.SCREEN_BRIGHTNESS_MODE,
1299                     R.bool.def_screen_brightness_automatic_mode);
1300 
1301             loadDefaultAnimationSettings(stmt);
1302 
1303             loadBooleanSetting(stmt, Settings.System.ACCELEROMETER_ROTATION,
1304                     R.bool.def_accelerometer_rotation);
1305 
1306             loadDefaultHapticSettings(stmt);
1307 
1308             loadBooleanSetting(stmt, Settings.System.NOTIFICATION_LIGHT_PULSE,
1309                     R.bool.def_notification_pulse);
1310             loadSetting(stmt, Settings.Secure.SET_INSTALL_LOCATION, 0);
1311             loadSetting(stmt, Settings.Secure.DEFAULT_INSTALL_LOCATION,
1312                     PackageHelper.APP_INSTALL_AUTO);
1313 
1314             loadUISoundEffectsSettings(stmt);
1315 
1316             loadBooleanSetting(stmt, Settings.System.VIBRATE_IN_SILENT,
1317                     R.bool.def_vibrate_in_silent);
1318 
1319             loadIntegerSetting(stmt, Settings.System.POINTER_SPEED,
1320                     R.integer.def_pointer_speed);
1321 
1322         } finally {
1323             if (stmt != null) stmt.close();
1324         }
1325     }
1326 
loadUISoundEffectsSettings(SQLiteStatement stmt)1327     private void loadUISoundEffectsSettings(SQLiteStatement stmt) {
1328         loadIntegerSetting(stmt, Settings.System.POWER_SOUNDS_ENABLED,
1329             R.integer.def_power_sounds_enabled);
1330         loadStringSetting(stmt, Settings.System.LOW_BATTERY_SOUND,
1331             R.string.def_low_battery_sound);
1332         loadBooleanSetting(stmt, Settings.System.DTMF_TONE_WHEN_DIALING,
1333                 R.bool.def_dtmf_tones_enabled);
1334         loadBooleanSetting(stmt, Settings.System.SOUND_EFFECTS_ENABLED,
1335                 R.bool.def_sound_effects_enabled);
1336         loadBooleanSetting(stmt, Settings.System.HAPTIC_FEEDBACK_ENABLED,
1337                 R.bool.def_haptic_feedback);
1338 
1339         loadIntegerSetting(stmt, Settings.System.DOCK_SOUNDS_ENABLED,
1340             R.integer.def_dock_sounds_enabled);
1341         loadStringSetting(stmt, Settings.System.DESK_DOCK_SOUND,
1342             R.string.def_desk_dock_sound);
1343         loadStringSetting(stmt, Settings.System.DESK_UNDOCK_SOUND,
1344             R.string.def_desk_undock_sound);
1345         loadStringSetting(stmt, Settings.System.CAR_DOCK_SOUND,
1346             R.string.def_car_dock_sound);
1347         loadStringSetting(stmt, Settings.System.CAR_UNDOCK_SOUND,
1348             R.string.def_car_undock_sound);
1349 
1350         loadIntegerSetting(stmt, Settings.System.LOCKSCREEN_SOUNDS_ENABLED,
1351             R.integer.def_lockscreen_sounds_enabled);
1352         loadStringSetting(stmt, Settings.System.LOCK_SOUND,
1353             R.string.def_lock_sound);
1354         loadStringSetting(stmt, Settings.System.UNLOCK_SOUND,
1355             R.string.def_unlock_sound);
1356     }
1357 
loadDefaultAnimationSettings(SQLiteStatement stmt)1358     private void loadDefaultAnimationSettings(SQLiteStatement stmt) {
1359         loadFractionSetting(stmt, Settings.System.WINDOW_ANIMATION_SCALE,
1360                 R.fraction.def_window_animation_scale, 1);
1361         loadFractionSetting(stmt, Settings.System.TRANSITION_ANIMATION_SCALE,
1362                 R.fraction.def_window_transition_scale, 1);
1363     }
1364 
loadDefaultHapticSettings(SQLiteStatement stmt)1365     private void loadDefaultHapticSettings(SQLiteStatement stmt) {
1366         loadBooleanSetting(stmt, Settings.System.HAPTIC_FEEDBACK_ENABLED,
1367                 R.bool.def_haptic_feedback);
1368     }
1369 
loadSecureSettings(SQLiteDatabase db)1370     private void loadSecureSettings(SQLiteDatabase db) {
1371         SQLiteStatement stmt = null;
1372         try {
1373             stmt = db.compileStatement("INSERT OR IGNORE INTO secure(name,value)"
1374                     + " VALUES(?,?);");
1375 
1376             loadBooleanSetting(stmt, Settings.Secure.BLUETOOTH_ON,
1377                     R.bool.def_bluetooth_on);
1378 
1379             // Data roaming default, based on build
1380             loadSetting(stmt, Settings.Secure.DATA_ROAMING,
1381                     "true".equalsIgnoreCase(
1382                             SystemProperties.get("ro.com.android.dataroaming",
1383                                     "false")) ? 1 : 0);
1384 
1385             loadBooleanSetting(stmt, Settings.Secure.INSTALL_NON_MARKET_APPS,
1386                     R.bool.def_install_non_market_apps);
1387 
1388             loadStringSetting(stmt, Settings.Secure.LOCATION_PROVIDERS_ALLOWED,
1389                     R.string.def_location_providers_allowed);
1390 
1391             loadBooleanSetting(stmt, Settings.Secure.ASSISTED_GPS_ENABLED,
1392                     R.bool.assisted_gps_enabled);
1393 
1394             loadIntegerSetting(stmt, Settings.Secure.NETWORK_PREFERENCE,
1395                     R.integer.def_network_preference);
1396 
1397             loadBooleanSetting(stmt, Settings.Secure.USB_MASS_STORAGE_ENABLED,
1398                     R.bool.def_usb_mass_storage_enabled);
1399 
1400             loadBooleanSetting(stmt, Settings.Secure.WIFI_ON,
1401                     R.bool.def_wifi_on);
1402             loadBooleanSetting(stmt, Settings.Secure.WIFI_NETWORKS_AVAILABLE_NOTIFICATION_ON,
1403                     R.bool.def_networks_available_notification_on);
1404 
1405             String wifiWatchList = SystemProperties.get("ro.com.android.wifi-watchlist");
1406             if (!TextUtils.isEmpty(wifiWatchList)) {
1407                 loadSetting(stmt, Settings.Secure.WIFI_WATCHDOG_WATCH_LIST, wifiWatchList);
1408             }
1409 
1410             // Set the preferred network mode to 0 = Global, CDMA default
1411             int type;
1412             if (BaseCommands.getLteOnCdmaModeStatic() == Phone.LTE_ON_CDMA_TRUE) {
1413                 type = Phone.NT_MODE_GLOBAL;
1414             } else {
1415                 type = SystemProperties.getInt("ro.telephony.default_network",
1416                         RILConstants.PREFERRED_NETWORK_MODE);
1417             }
1418             loadSetting(stmt, Settings.Secure.PREFERRED_NETWORK_MODE, type);
1419 
1420             // Enable or disable Cell Broadcast SMS
1421             loadSetting(stmt, Settings.Secure.CDMA_CELL_BROADCAST_SMS,
1422                     RILConstants.CDMA_CELL_BROADCAST_SMS_DISABLED);
1423 
1424             // Set the preferred cdma subscription to 0 = Subscription from RUIM, when available
1425             loadSetting(stmt, Settings.Secure.PREFERRED_CDMA_SUBSCRIPTION,
1426                     RILConstants.PREFERRED_CDMA_SUBSCRIPTION);
1427 
1428             // Don't do this.  The SystemServer will initialize ADB_ENABLED from a
1429             // persistent system property instead.
1430             //loadSetting(stmt, Settings.Secure.ADB_ENABLED, 0);
1431 
1432             // Allow mock locations default, based on build
1433             loadSetting(stmt, Settings.Secure.ALLOW_MOCK_LOCATION,
1434                     "1".equals(SystemProperties.get("ro.allow.mock.location")) ? 1 : 0);
1435 
1436             loadSecure35Settings(stmt);
1437 
1438             loadBooleanSetting(stmt, Settings.Secure.MOUNT_PLAY_NOTIFICATION_SND,
1439                     R.bool.def_mount_play_notification_snd);
1440 
1441             loadBooleanSetting(stmt, Settings.Secure.MOUNT_UMS_AUTOSTART,
1442                     R.bool.def_mount_ums_autostart);
1443 
1444             loadBooleanSetting(stmt, Settings.Secure.MOUNT_UMS_PROMPT,
1445                     R.bool.def_mount_ums_prompt);
1446 
1447             loadBooleanSetting(stmt, Settings.Secure.MOUNT_UMS_NOTIFY_ENABLED,
1448                     R.bool.def_mount_ums_notify_enabled);
1449 
1450             loadBooleanSetting(stmt, Settings.Secure.ACCESSIBILITY_SCRIPT_INJECTION,
1451                     R.bool.def_accessibility_script_injection);
1452 
1453             loadStringSetting(stmt, Settings.Secure.ACCESSIBILITY_WEB_CONTENT_KEY_BINDINGS,
1454                     R.string.def_accessibility_web_content_key_bindings);
1455 
1456             final int maxBytes = mContext.getResources().getInteger(
1457                     R.integer.def_download_manager_max_bytes_over_mobile);
1458             if (maxBytes > 0) {
1459                 loadSetting(stmt, Settings.Secure.DOWNLOAD_MAX_BYTES_OVER_MOBILE,
1460                         Integer.toString(maxBytes));
1461             }
1462 
1463             final int recommendedMaxBytes = mContext.getResources().getInteger(
1464                     R.integer.def_download_manager_recommended_max_bytes_over_mobile);
1465             if (recommendedMaxBytes > 0) {
1466                 loadSetting(stmt, Settings.Secure.DOWNLOAD_RECOMMENDED_MAX_BYTES_OVER_MOBILE,
1467                         Integer.toString(recommendedMaxBytes));
1468             }
1469 
1470             loadIntegerSetting(stmt, Settings.Secure.LONG_PRESS_TIMEOUT,
1471                     R.integer.def_long_press_timeout_millis);
1472 
1473             loadBooleanSetting(stmt, Settings.Secure.TOUCH_EXPLORATION_ENABLED,
1474                     R.bool.def_touch_exploration_enabled);
1475         } finally {
1476             if (stmt != null) stmt.close();
1477         }
1478     }
1479 
loadSecure35Settings(SQLiteStatement stmt)1480     private void loadSecure35Settings(SQLiteStatement stmt) {
1481         loadBooleanSetting(stmt, Settings.Secure.BACKUP_ENABLED,
1482                 R.bool.def_backup_enabled);
1483 
1484         loadStringSetting(stmt, Settings.Secure.BACKUP_TRANSPORT,
1485                 R.string.def_backup_transport);
1486     }
1487 
loadSetting(SQLiteStatement stmt, String key, Object value)1488     private void loadSetting(SQLiteStatement stmt, String key, Object value) {
1489         stmt.bindString(1, key);
1490         stmt.bindString(2, value.toString());
1491         stmt.execute();
1492     }
1493 
loadStringSetting(SQLiteStatement stmt, String key, int resid)1494     private void loadStringSetting(SQLiteStatement stmt, String key, int resid) {
1495         loadSetting(stmt, key, mContext.getResources().getString(resid));
1496     }
1497 
loadBooleanSetting(SQLiteStatement stmt, String key, int resid)1498     private void loadBooleanSetting(SQLiteStatement stmt, String key, int resid) {
1499         loadSetting(stmt, key,
1500                 mContext.getResources().getBoolean(resid) ? "1" : "0");
1501     }
1502 
loadIntegerSetting(SQLiteStatement stmt, String key, int resid)1503     private void loadIntegerSetting(SQLiteStatement stmt, String key, int resid) {
1504         loadSetting(stmt, key,
1505                 Integer.toString(mContext.getResources().getInteger(resid)));
1506     }
1507 
loadFractionSetting(SQLiteStatement stmt, String key, int resid, int base)1508     private void loadFractionSetting(SQLiteStatement stmt, String key, int resid, int base) {
1509         loadSetting(stmt, key,
1510                 Float.toString(mContext.getResources().getFraction(resid, base, base)));
1511     }
1512 }
1513