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
17package com.android.server.backup;
18
19import android.app.AppGlobals;
20import android.app.backup.BlobBackupHelper;
21import android.content.pm.IPackageManager;
22import android.os.UserHandle;
23import android.util.Slog;
24
25public class PreferredActivityBackupHelper extends BlobBackupHelper {
26    private static final String TAG = "PreferredBackup";
27    private static final boolean DEBUG = false;
28
29    // current schema of the backup state blob
30    private static final int STATE_VERSION = 3;
31
32    // key under which the preferred-activity state blob is committed to backup
33    private static final String KEY_PREFERRED = "preferred-activity";
34
35    // key for default-browser [etc] state
36    private static final String KEY_DEFAULT_APPS = "default-apps";
37
38    // intent-filter verification state
39    private static final String KEY_INTENT_VERIFICATION = "intent-verification";
40
41    public PreferredActivityBackupHelper() {
42        super(STATE_VERSION,
43                KEY_PREFERRED,
44                KEY_DEFAULT_APPS,
45                KEY_INTENT_VERIFICATION);
46    }
47
48    @Override
49    protected byte[] getBackupPayload(String key) {
50        IPackageManager pm = AppGlobals.getPackageManager();
51        if (DEBUG) {
52            Slog.d(TAG, "Handling backup of " + key);
53        }
54        try {
55            // TODO: http://b/22388012
56            switch (key) {
57                case KEY_PREFERRED:
58                    return pm.getPreferredActivityBackup(UserHandle.USER_SYSTEM);
59                case KEY_DEFAULT_APPS:
60                    return pm.getDefaultAppsBackup(UserHandle.USER_SYSTEM);
61                case KEY_INTENT_VERIFICATION:
62                    return pm.getIntentFilterVerificationBackup(UserHandle.USER_SYSTEM);
63                default:
64                    Slog.w(TAG, "Unexpected backup key " + key);
65            }
66        } catch (Exception e) {
67            Slog.e(TAG, "Unable to store payload " + key);
68        }
69        return null;
70    }
71
72    @Override
73    protected void applyRestoredPayload(String key, byte[] payload) {
74        IPackageManager pm = AppGlobals.getPackageManager();
75        if (DEBUG) {
76            Slog.d(TAG, "Handling restore of " + key);
77        }
78        try {
79            // TODO: http://b/22388012
80            switch (key) {
81                case KEY_PREFERRED:
82                    pm.restorePreferredActivities(payload, UserHandle.USER_SYSTEM);
83                    break;
84                case KEY_DEFAULT_APPS:
85                    pm.restoreDefaultApps(payload, UserHandle.USER_SYSTEM);
86                    break;
87                case KEY_INTENT_VERIFICATION:
88                    pm.restoreIntentFilterVerification(payload, UserHandle.USER_SYSTEM);
89                    break;
90                default:
91                    Slog.w(TAG, "Unexpected restore key " + key);
92            }
93        } catch (Exception e) {
94            Slog.w(TAG, "Unable to restore key " + key);
95        }
96    }
97}
98