Back up / restore default app and intent filter verification state

For apps not present on device, the state inherited from the ancestral
device is applied when the app is ultimately installed.

Bug 20144515

Change-Id: Ie05b4f1751357fc62f14e259da174b8cf465e913
diff --git a/core/java/android/content/pm/IPackageManager.aidl b/core/java/android/content/pm/IPackageManager.aidl
index 0c07bc3..2dbcde9 100644
--- a/core/java/android/content/pm/IPackageManager.aidl
+++ b/core/java/android/content/pm/IPackageManager.aidl
@@ -282,6 +282,10 @@
      */
     byte[] getPreferredActivityBackup(int userId);
     void restorePreferredActivities(in byte[] backup, int userId);
+    byte[] getDefaultAppsBackup(int userId);
+    void restoreDefaultApps(in byte[] backup, int userId);
+    byte[] getIntentFilterVerificationBackup(int userId);
+    void restoreIntentFilterVerification(in byte[] backup, int userId);
 
     /**
      * Report the set of 'Home' activity candidates, plus (if any) which of them
diff --git a/core/java/com/android/server/backup/PreferredActivityBackupHelper.java b/core/java/com/android/server/backup/PreferredActivityBackupHelper.java
index 26f5bf4..458a2ca 100644
--- a/core/java/com/android/server/backup/PreferredActivityBackupHelper.java
+++ b/core/java/com/android/server/backup/PreferredActivityBackupHelper.java
@@ -27,48 +27,69 @@
     private static final boolean DEBUG = false;
 
     // current schema of the backup state blob
-    private static final int STATE_VERSION = 2;
+    private static final int STATE_VERSION = 3;
 
     // key under which the preferred-activity state blob is committed to backup
     private static final String KEY_PREFERRED = "preferred-activity";
 
+    // key for default-browser [etc] state
+    private static final String KEY_DEFAULT_APPS = "default-apps";
+
+    // intent-filter verification state
+    private static final String KEY_INTENT_VERIFICATION = "intent-verification";
+
     public PreferredActivityBackupHelper() {
-        super(STATE_VERSION, KEY_PREFERRED);
+        super(STATE_VERSION,
+                KEY_PREFERRED,
+                KEY_DEFAULT_APPS,
+                KEY_INTENT_VERIFICATION);
     }
 
     @Override
     protected byte[] getBackupPayload(String key) {
-        if (KEY_PREFERRED.equals(key)) {
-            if (DEBUG) {
-                Slog.v(TAG, "Checking whether to back up");
+        IPackageManager pm = AppGlobals.getPackageManager();
+        if (DEBUG) {
+            Slog.d(TAG, "Handling backup of " + key);
+        }
+        try {
+            switch (key) {
+                case KEY_PREFERRED:
+                    return pm.getPreferredActivityBackup(UserHandle.USER_OWNER);
+                case KEY_DEFAULT_APPS:
+                    return pm.getDefaultAppsBackup(UserHandle.USER_OWNER);
+                case KEY_INTENT_VERIFICATION:
+                    return pm.getIntentFilterVerificationBackup(UserHandle.USER_OWNER);
+                default:
+                    Slog.w(TAG, "Unexpected backup key " + key);
             }
-            IPackageManager pm = AppGlobals.getPackageManager();
-            try {
-                return pm.getPreferredActivityBackup(UserHandle.USER_OWNER);
-            } catch (Exception e) {
-                Slog.e(TAG, "Unable to store backup payload", e);
-                // fall through to report null state
-            }
-        } else {
-            Slog.w(TAG, "Unexpected backup key " + key);
+        } catch (Exception e) {
+            Slog.e(TAG, "Unable to store payload " + key);
         }
         return null;
     }
 
     @Override
     protected void applyRestoredPayload(String key, byte[] payload) {
-        if (KEY_PREFERRED.equals(key)) {
-            if (DEBUG) {
-                Slog.v(TAG, "Restoring");
+        IPackageManager pm = AppGlobals.getPackageManager();
+        if (DEBUG) {
+            Slog.d(TAG, "Handling restore of " + key);
+        }
+        try {
+            switch (key) {
+                case KEY_PREFERRED:
+                    pm.restorePreferredActivities(payload, UserHandle.USER_OWNER);
+                    break;
+                case KEY_DEFAULT_APPS:
+                    pm.restoreDefaultApps(payload, UserHandle.USER_OWNER);
+                    break;
+                case KEY_INTENT_VERIFICATION:
+                    pm.restoreIntentFilterVerification(payload, UserHandle.USER_OWNER);
+                    break;
+                default:
+                    Slog.w(TAG, "Unexpected restore key " + key);
             }
-            IPackageManager pm = AppGlobals.getPackageManager();
-            try {
-                pm.restorePreferredActivities(payload, UserHandle.USER_OWNER);
-            } catch (Exception e) {
-                Slog.e(TAG, "Unable to restore", e);
-            }
-        } else {
-            Slog.w(TAG, "Unexpected restore key " + key);
+        } catch (Exception e) {
+            Slog.w(TAG, "Unable to restore key " + key);
         }
     }
 }