1 /*
2  * Copyright (C) 2015 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.server.backup;
18 
19 import android.annotation.StringDef;
20 import android.annotation.UserIdInt;
21 import android.app.AppGlobals;
22 import android.app.backup.BlobBackupHelper;
23 import android.content.pm.IPackageManager;
24 import android.content.pm.verify.domain.DomainVerificationManager;
25 import android.util.Slog;
26 
27 public class PreferredActivityBackupHelper extends BlobBackupHelper {
28     private static final String TAG = "PreferredBackup";
29     private static final boolean DEBUG = false;
30 
31     // current schema of the backup state blob
32     private static final int STATE_VERSION = 4;
33 
34     // key under which the preferred-activity state blob is committed to backup
35     private static final String KEY_PREFERRED = "preferred-activity";
36 
37     // key for default-browser [etc] state
38     private static final String KEY_DEFAULT_APPS = "default-apps";
39 
40     /**
41      * Intent-filter verification state
42      * @deprecated Replaced by {@link #KEY_DOMAIN_VERIFICATION}, retained to ensure the key is
43      * never reused.
44      */
45     @Deprecated
46     private static final String KEY_INTENT_VERIFICATION = "intent-verification";
47 
48     /**
49      * State for {@link DomainVerificationManager}.
50      */
51     private static final String KEY_DOMAIN_VERIFICATION = "domain-verification";
52 
53     private static final String[] KEYS = new String[] {
54             KEY_PREFERRED,
55             KEY_DEFAULT_APPS,
56             KEY_INTENT_VERIFICATION,
57             KEY_DOMAIN_VERIFICATION
58     };
59 
60     @StringDef(value = {
61             KEY_PREFERRED,
62             KEY_DEFAULT_APPS,
63             KEY_INTENT_VERIFICATION,
64             KEY_DOMAIN_VERIFICATION
65     })
66     private @interface Key {
67     }
68 
69     @UserIdInt
70     private final int mUserId;
71 
PreferredActivityBackupHelper(@serIdInt int userId)72     public PreferredActivityBackupHelper(@UserIdInt int userId) {
73         super(STATE_VERSION, KEYS);
74         mUserId = userId;
75     }
76 
77     @Override
getBackupPayload(String key)78     protected byte[] getBackupPayload(String key) {
79         IPackageManager pm = AppGlobals.getPackageManager();
80         if (DEBUG) {
81             Slog.d(TAG, "Handling backup of " + key);
82         }
83         try {
84             switch (key) {
85                 case KEY_PREFERRED:
86                     return pm.getPreferredActivityBackup(mUserId);
87                 case KEY_DEFAULT_APPS:
88                     return pm.getDefaultAppsBackup(mUserId);
89                 case KEY_INTENT_VERIFICATION:
90                     // Deprecated
91                     return null;
92                 case KEY_DOMAIN_VERIFICATION:
93                     return pm.getDomainVerificationBackup(mUserId);
94                 default:
95                     Slog.w(TAG, "Unexpected backup key " + key);
96             }
97         } catch (Exception e) {
98             Slog.e(TAG, "Unable to store payload " + key, e);
99         }
100         return null;
101     }
102 
103     @Override
applyRestoredPayload(@ey String key, byte[] payload)104     protected void applyRestoredPayload(@Key String key, byte[] payload) {
105         IPackageManager pm = AppGlobals.getPackageManager();
106         if (DEBUG) {
107             Slog.d(TAG, "Handling restore of " + key);
108         }
109         try {
110             switch (key) {
111                 case KEY_PREFERRED:
112                     pm.restorePreferredActivities(payload, mUserId);
113                     break;
114                 case KEY_DEFAULT_APPS:
115                     pm.restoreDefaultApps(payload, mUserId);
116                     break;
117                 case KEY_INTENT_VERIFICATION:
118                     // Deprecated
119                     break;
120                 case KEY_DOMAIN_VERIFICATION:
121                     pm.restoreDomainVerification(payload, mUserId);
122                     break;
123                 default:
124                     Slog.w(TAG, "Unexpected restore key " + key);
125             }
126         } catch (Exception e) {
127             Slog.e(TAG, "Unable to restore key " + key, e);
128         }
129     }
130 }
131