Add auto-restore setting and Backup Manager awareness thereof

This setting, like BACKUP_ENABLE, should never be set directly in the secure
settings database.  Instead, it should be manipulated through the new IBackupManager
method setAutoRestore(boolean).

Change-Id: I5c3226ca85b6148bb756d753d7f9e4ea76e878c4
diff --git a/core/java/android/backup/IBackupManager.aidl b/core/java/android/backup/IBackupManager.aidl
index cb775f7..bf6c79f 100644
--- a/core/java/android/backup/IBackupManager.aidl
+++ b/core/java/android/backup/IBackupManager.aidl
@@ -73,6 +73,21 @@
     void setBackupEnabled(boolean isEnabled);
 
     /**
+     * Enable/disable automatic restore of application data at install time.  When
+     * enabled, installation of any package will involve the Backup Manager.  If data
+     * exists for the newly-installed package, either from the device's current [enabled]
+     * backup dataset or from the restore set used in the last wholesale restore operation,
+     * that data will be supplied to the new package's restore agent before the package
+     * is made generally available for launch.
+     *
+     * <p>Callers must hold  the android.permission.BACKUP permission to use this method.
+     *
+     * @param doAutoRestore When true, enables the automatic app-data restore facility.  When
+     *   false, this facility will be disabled.
+     */
+    void setAutoRestore(boolean doAutoRestore);
+
+    /**
      * Indicate that any necessary one-time provisioning has occurred.
      *
      * <p>Callers must hold the android.permission.BACKUP permission to use this method.
diff --git a/core/java/android/provider/Settings.java b/core/java/android/provider/Settings.java
index 23a9f49..7128005 100644
--- a/core/java/android/provider/Settings.java
+++ b/core/java/android/provider/Settings.java
@@ -2425,6 +2425,14 @@
         public static final String BACKUP_ENABLED = "backup_enabled";
 
         /**
+         * Controls whether application data is automatically restored from backup
+         * at install time.
+         * Type: int ( 0 = disabled, 1 = enabled )
+         * @hide
+         */
+        public static final String BACKUP_AUTO_RESTORE = "backup_auto_restore";
+
+        /**
          * Indicates whether settings backup has been fully provisioned.
          * Type: int ( 0 = unprovisioned, 1 = fully provisioned )
          * @hide
diff --git a/services/java/com/android/server/BackupManagerService.java b/services/java/com/android/server/BackupManagerService.java
index 0562c55..0c1e0602 100644
--- a/services/java/com/android/server/BackupManagerService.java
+++ b/services/java/com/android/server/BackupManagerService.java
@@ -118,6 +118,7 @@
 
     boolean mEnabled;   // access to this is synchronized on 'this'
     boolean mProvisioned;
+    boolean mAutoRestore;
     PowerManager.WakeLock mWakelock;
     HandlerThread mHandlerThread = new HandlerThread("backup", Process.THREAD_PRIORITY_BACKGROUND);
     BackupHandler mBackupHandler;
@@ -340,6 +341,8 @@
                 Settings.Secure.BACKUP_ENABLED, 0) != 0;
         mProvisioned = Settings.Secure.getInt(context.getContentResolver(),
                 Settings.Secure.BACKUP_PROVISIONED, 0) != 0;
+        mAutoRestore = Settings.Secure.getInt(context.getContentResolver(),
+                Settings.Secure.BACKUP_AUTO_RESTORE, 0) != 0;
         // If Encrypted file systems is enabled or disabled, this call will return the
         // correct directory.
         mBaseStateDir = new File(Environment.getSecureDataDirectory(), "backup");
@@ -2019,6 +2022,20 @@
         }
     }
 
+    // Enable/disable automatic restore of app data at install time
+    public void setAutoRestore(boolean doAutoRestore) {
+        mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP,
+        "setBackupEnabled");
+
+        Log.i(TAG, "Auto restore => " + doAutoRestore);
+
+        synchronized (this) {
+            Settings.Secure.putInt(mContext.getContentResolver(),
+                    Settings.Secure.BACKUP_AUTO_RESTORE, doAutoRestore ? 1 : 0);
+            mAutoRestore = doAutoRestore;
+        }
+    }
+
     // Mark the backup service as having been provisioned
     public void setBackupProvisioned(boolean available) {
         mContext.enforceCallingOrSelfPermission(android.Manifest.permission.BACKUP,