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 PermissionBackupHelper extends BlobBackupHelper {
26    private static final String TAG = "PermissionBackup";
27    private static final boolean DEBUG = false;
28
29    // current schema of the backup state blob
30    private static final int STATE_VERSION = 1;
31
32    // key under which the permission-grant state blob is committed to backup
33    private static final String KEY_PERMISSIONS = "permissions";
34
35    public PermissionBackupHelper() {
36        super(STATE_VERSION, KEY_PERMISSIONS);
37    }
38
39    @Override
40    protected byte[] getBackupPayload(String key) {
41        IPackageManager pm = AppGlobals.getPackageManager();
42        if (DEBUG) {
43            Slog.d(TAG, "Handling backup of " + key);
44        }
45        try {
46            switch (key) {
47                case KEY_PERMISSIONS:
48                    return pm.getPermissionGrantBackup(UserHandle.USER_SYSTEM);
49
50                default:
51                    Slog.w(TAG, "Unexpected backup key " + key);
52            }
53        } catch (Exception e) {
54            Slog.e(TAG, "Unable to store payload " + key);
55        }
56        return null;
57    }
58
59    @Override
60    protected void applyRestoredPayload(String key, byte[] payload) {
61        IPackageManager pm = AppGlobals.getPackageManager();
62        if (DEBUG) {
63            Slog.d(TAG, "Handling restore of " + key);
64        }
65        try {
66            switch (key) {
67                case KEY_PERMISSIONS:
68                    pm.restorePermissionGrants(payload, UserHandle.USER_SYSTEM);
69                    break;
70
71                default:
72                    Slog.w(TAG, "Unexpected restore key " + key);
73            }
74        } catch (Exception e) {
75            Slog.w(TAG, "Unable to restore key " + key);
76        }
77    }
78}
79