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