Backup / Restore locale preference.

Also backup development settings MOCK_LOCATION and USB_DEBUGGING.
Backup and restore more of the Audio settings. Won't work yet without a reboot.
Disable Wifi supplicant restore temporarily. It seems to be disabling Wifi due to
permissions problems.
Don't restore Ringtones.
diff --git a/core/java/android/provider/Settings.java b/core/java/android/provider/Settings.java
index 0d27e1e..aa583ac 100644
--- a/core/java/android/provider/Settings.java
+++ b/core/java/android/provider/Settings.java
@@ -1359,8 +1359,21 @@
             SCREEN_BRIGHTNESS,
             VIBRATE_ON,
             NOTIFICATIONS_USE_RING_VOLUME,
-            RINGTONE,
-            NOTIFICATION_SOUND,
+            MODE_RINGER,
+            MODE_RINGER_STREAMS_AFFECTED,
+            MUTE_STREAMS_AFFECTED,
+            VOLUME_VOICE,
+            VOLUME_SYSTEM,
+            VOLUME_RING,
+            VOLUME_MUSIC,
+            VOLUME_ALARM,
+            VOLUME_NOTIFICATION,
+            VOLUME_VOICE + APPEND_FOR_LAST_AUDIBLE,
+            VOLUME_SYSTEM + APPEND_FOR_LAST_AUDIBLE,
+            VOLUME_RING + APPEND_FOR_LAST_AUDIBLE,
+            VOLUME_MUSIC + APPEND_FOR_LAST_AUDIBLE,
+            VOLUME_ALARM + APPEND_FOR_LAST_AUDIBLE,
+            VOLUME_NOTIFICATION + APPEND_FOR_LAST_AUDIBLE,
             TEXT_AUTO_REPLACE,
             TEXT_AUTO_CAPS,
             TEXT_AUTO_PUNCTUATE,
@@ -2299,6 +2312,8 @@
          * @hide
          */
         public static final String[] SETTINGS_TO_BACKUP = {
+            ADB_ENABLED,
+            ALLOW_MOCK_LOCATION,
             INSTALL_NON_MARKET_APPS,
             PARENTAL_CONTROL_ENABLED,
             PARENTAL_CONTROL_REDIRECT_URL,
diff --git a/packages/SettingsProvider/src/com/android/providers/settings/SettingsBackupAgent.java b/packages/SettingsProvider/src/com/android/providers/settings/SettingsBackupAgent.java
index f9e98ef..b6bc8a5 100644
--- a/packages/SettingsProvider/src/com/android/providers/settings/SettingsBackupAgent.java
+++ b/packages/SettingsProvider/src/com/android/providers/settings/SettingsBackupAgent.java
@@ -50,6 +50,7 @@
     private static final String KEY_SYSTEM = "system";
     private static final String KEY_SECURE = "secure";
     private static final String KEY_SYNC = "sync_providers";
+    private static final String KEY_LOCALE = "locale";
 
     private static String[] sortedSystemKeys = null;
     private static String[] sortedSecureKeys = null;
@@ -85,6 +86,7 @@
         byte[] systemSettingsData = getSystemSettings();
         byte[] secureSettingsData = getSecureSettings();
         byte[] syncProviders = mSettingsHelper.getSyncProviders();
+        byte[] locale = mSettingsHelper.getLocaleData();
         
         data.writeEntityHeader(KEY_SYSTEM, systemSettingsData.length);
         data.writeEntityData(systemSettingsData, systemSettingsData.length);
@@ -94,7 +96,10 @@
 
         data.writeEntityHeader(KEY_SYNC, syncProviders.length);
         data.writeEntityData(syncProviders, syncProviders.length);
-        
+
+        data.writeEntityHeader(KEY_LOCALE, locale.length);
+        data.writeEntityData(locale, locale.length);
+
         backupFile(FILE_WIFI_SUPPLICANT, data);
     }
 
@@ -107,14 +112,20 @@
 
         while (data.readNextHeader()) {
             final String key = data.getKey();
+            final int size = data.getDataSize();
             if (KEY_SYSTEM.equals(key)) {
                 restoreSettings(data, Settings.System.CONTENT_URI);
             } else if (KEY_SECURE.equals(key)) {
                 restoreSettings(data, Settings.Secure.CONTENT_URI);
-            } else if (FILE_WIFI_SUPPLICANT.equals(key)) {
-                restoreFile(FILE_WIFI_SUPPLICANT, data);
+// TODO: Re-enable WIFI restore when we figure out a solution for the permissions
+//            } else if (FILE_WIFI_SUPPLICANT.equals(key)) {
+//                restoreFile(FILE_WIFI_SUPPLICANT, data);
             } else if (KEY_SYNC.equals(key)) {
                 mSettingsHelper.setSyncProviders(data);
+            } else if (KEY_LOCALE.equals(key)) {
+                byte[] localeData = new byte[size];
+                data.readEntityData(localeData, 0, size);
+                mSettingsHelper.setLocaleData(localeData);
             } else {
                 data.skipEntityData();
             }
diff --git a/packages/SettingsProvider/src/com/android/providers/settings/SettingsHelper.java b/packages/SettingsProvider/src/com/android/providers/settings/SettingsHelper.java
index 5f3fba8..2c5775a 100644
--- a/packages/SettingsProvider/src/com/android/providers/settings/SettingsHelper.java
+++ b/packages/SettingsProvider/src/com/android/providers/settings/SettingsHelper.java
@@ -16,16 +16,22 @@
 
 package com.android.providers.settings;
 
+import java.util.Locale;
+
+import android.app.ActivityManagerNative;
+import android.app.IActivityManager;
 import android.backup.BackupDataInput;
 import android.content.ContentResolver;
 import android.content.Context;
 import android.content.IContentService;
+import android.content.res.Configuration;
 import android.location.LocationManager;
 import android.media.AudioManager;
 import android.os.IHardwareService;
 import android.os.RemoteException;
 import android.os.ServiceManager;
 import android.provider.Settings;
+import android.text.TextUtils;
 import android.util.Log;
 
 public class SettingsHelper {
@@ -110,7 +116,6 @@
         }
     }
 
-    /* TODO: Get a list of all sync providers and save/restore the settings */
     byte[] getSyncProviders() {
         byte[] sync = new byte[1 + PROVIDERS.length];
         try {
@@ -141,4 +146,53 @@
             Log.w(TAG, "Unable to read sync settings");
         }
     }
+
+    byte[] getLocaleData() {
+        Configuration conf = mContext.getResources().getConfiguration();
+        final Locale loc = conf.locale;
+        String localeString = loc.getLanguage();
+        String country = loc.getCountry();
+        if (!TextUtils.isEmpty(country)) {
+            localeString += "_" + country;
+        }
+        return localeString.getBytes();
+    }
+
+    /**
+     * Sets the locale specified. Input data is the equivalent of "ll_cc".getBytes(), where
+     * "ll" is the language code and "cc" is the country code.
+     * @param data the locale string in bytes.
+     */
+    void setLocaleData(byte[] data) {
+        // Check if locale was set by the user:
+        Configuration conf = mContext.getResources().getConfiguration();
+        Locale loc = conf.locale;
+        if (conf.userSetLocale) return; // Don't change if user set it in the SetupWizard
+
+        final String[] availableLocales = mContext.getAssets().getLocales();
+        String localeCode = new String(data);
+        String language = new String(data, 0, 2);
+        String country = data.length > 4 ? new String(data, 3, 2) : "";
+        loc = null;
+        for (int i = 0; i < availableLocales.length; i++) {
+            if (availableLocales[i].equals(localeCode)) {
+                loc = new Locale(language, country);
+                break;
+            }
+        }
+        if (loc == null) return; // Couldn't find the saved locale in this version of the software
+
+        try {
+            IActivityManager am = ActivityManagerNative.getDefault();
+            Configuration config = am.getConfiguration();
+            config.locale = loc;
+            // indicate this isn't some passing default - the user wants this remembered
+            config.userSetLocale = true;
+
+            am.updateConfiguration(config);
+        } catch (RemoteException e) {
+            // Intentionally left blank
+        }
+
+    }
 }
diff --git a/packages/SettingsProvider/src/com/android/providers/settings/SettingsProvider.java b/packages/SettingsProvider/src/com/android/providers/settings/SettingsProvider.java
index a21bf32..2abf8b3 100644
--- a/packages/SettingsProvider/src/com/android/providers/settings/SettingsProvider.java
+++ b/packages/SettingsProvider/src/com/android/providers/settings/SettingsProvider.java
@@ -18,7 +18,7 @@
 
 import java.io.FileNotFoundException;
 
-import android.backup.IBackupManager;
+import android.backup.BackupManager;
 import android.content.ContentProvider;
 import android.content.ContentUris;
 import android.content.ContentValues;
@@ -46,6 +46,8 @@
     private static final String TABLE_OLD_FAVORITES = "old_favorites";
 
     private DatabaseHelper mOpenHelper;
+    
+    private BackupManager mBackupManager;
 
     /**
      * Decode a content URL into the table, projection, and arguments
@@ -140,16 +142,7 @@
         }
 
         // Inform the backup manager about a data change
-        IBackupManager ibm = IBackupManager.Stub.asInterface(
-                ServiceManager.getService(Context.BACKUP_SERVICE));
-        if (ibm != null) {
-            try {
-                ibm.dataChanged(getContext().getPackageName());
-            } catch (Exception e) {
-                // Try again later
-            }
-        }
-
+        mBackupManager.dataChanged();
         // Now send the notification through the content framework.
 
         String notify = uri.getQueryParameter("notify");
@@ -189,6 +182,7 @@
     @Override
     public boolean onCreate() {
         mOpenHelper = new DatabaseHelper(getContext());
+        mBackupManager = new BackupManager(getContext());
         return true;
     }