1e9fd1fa31ad6f62d1eb6f32cdcdab50349f246ebChristopher Tate/*
2e9fd1fa31ad6f62d1eb6f32cdcdab50349f246ebChristopher Tate * Copyright (C) 2015 The Android Open Source Project
3e9fd1fa31ad6f62d1eb6f32cdcdab50349f246ebChristopher Tate *
4e9fd1fa31ad6f62d1eb6f32cdcdab50349f246ebChristopher Tate * Licensed under the Apache License, Version 2.0 (the "License");
5e9fd1fa31ad6f62d1eb6f32cdcdab50349f246ebChristopher Tate * you may not use this file except in compliance with the License.
6e9fd1fa31ad6f62d1eb6f32cdcdab50349f246ebChristopher Tate * You may obtain a copy of the License at
7e9fd1fa31ad6f62d1eb6f32cdcdab50349f246ebChristopher Tate *
8e9fd1fa31ad6f62d1eb6f32cdcdab50349f246ebChristopher Tate *      http://www.apache.org/licenses/LICENSE-2.0
9e9fd1fa31ad6f62d1eb6f32cdcdab50349f246ebChristopher Tate *
10e9fd1fa31ad6f62d1eb6f32cdcdab50349f246ebChristopher Tate * Unless required by applicable law or agreed to in writing, software
11e9fd1fa31ad6f62d1eb6f32cdcdab50349f246ebChristopher Tate * distributed under the License is distributed on an "AS IS" BASIS,
12e9fd1fa31ad6f62d1eb6f32cdcdab50349f246ebChristopher Tate * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13e9fd1fa31ad6f62d1eb6f32cdcdab50349f246ebChristopher Tate * See the License for the specific language governing permissions and
14e9fd1fa31ad6f62d1eb6f32cdcdab50349f246ebChristopher Tate * limitations under the License.
15e9fd1fa31ad6f62d1eb6f32cdcdab50349f246ebChristopher Tate */
16e9fd1fa31ad6f62d1eb6f32cdcdab50349f246ebChristopher Tate
17e9fd1fa31ad6f62d1eb6f32cdcdab50349f246ebChristopher Tatepackage com.android.server.backup;
18e9fd1fa31ad6f62d1eb6f32cdcdab50349f246ebChristopher Tate
19e9fd1fa31ad6f62d1eb6f32cdcdab50349f246ebChristopher Tateimport android.app.AppGlobals;
20e9fd1fa31ad6f62d1eb6f32cdcdab50349f246ebChristopher Tateimport android.app.backup.BlobBackupHelper;
21e9fd1fa31ad6f62d1eb6f32cdcdab50349f246ebChristopher Tateimport android.content.pm.IPackageManager;
22e9fd1fa31ad6f62d1eb6f32cdcdab50349f246ebChristopher Tateimport android.os.UserHandle;
23e9fd1fa31ad6f62d1eb6f32cdcdab50349f246ebChristopher Tateimport android.util.Slog;
24e9fd1fa31ad6f62d1eb6f32cdcdab50349f246ebChristopher Tate
25e9fd1fa31ad6f62d1eb6f32cdcdab50349f246ebChristopher Tatepublic class PermissionBackupHelper extends BlobBackupHelper {
26e9fd1fa31ad6f62d1eb6f32cdcdab50349f246ebChristopher Tate    private static final String TAG = "PermissionBackup";
27e9fd1fa31ad6f62d1eb6f32cdcdab50349f246ebChristopher Tate    private static final boolean DEBUG = false;
28e9fd1fa31ad6f62d1eb6f32cdcdab50349f246ebChristopher Tate
29e9fd1fa31ad6f62d1eb6f32cdcdab50349f246ebChristopher Tate    // current schema of the backup state blob
30e9fd1fa31ad6f62d1eb6f32cdcdab50349f246ebChristopher Tate    private static final int STATE_VERSION = 1;
31e9fd1fa31ad6f62d1eb6f32cdcdab50349f246ebChristopher Tate
32e9fd1fa31ad6f62d1eb6f32cdcdab50349f246ebChristopher Tate    // key under which the permission-grant state blob is committed to backup
33e9fd1fa31ad6f62d1eb6f32cdcdab50349f246ebChristopher Tate    private static final String KEY_PERMISSIONS = "permissions";
34e9fd1fa31ad6f62d1eb6f32cdcdab50349f246ebChristopher Tate
35e9fd1fa31ad6f62d1eb6f32cdcdab50349f246ebChristopher Tate    public PermissionBackupHelper() {
36e9fd1fa31ad6f62d1eb6f32cdcdab50349f246ebChristopher Tate        super(STATE_VERSION, KEY_PERMISSIONS);
37e9fd1fa31ad6f62d1eb6f32cdcdab50349f246ebChristopher Tate    }
38e9fd1fa31ad6f62d1eb6f32cdcdab50349f246ebChristopher Tate
39e9fd1fa31ad6f62d1eb6f32cdcdab50349f246ebChristopher Tate    @Override
40e9fd1fa31ad6f62d1eb6f32cdcdab50349f246ebChristopher Tate    protected byte[] getBackupPayload(String key) {
41e9fd1fa31ad6f62d1eb6f32cdcdab50349f246ebChristopher Tate        IPackageManager pm = AppGlobals.getPackageManager();
42e9fd1fa31ad6f62d1eb6f32cdcdab50349f246ebChristopher Tate        if (DEBUG) {
43e9fd1fa31ad6f62d1eb6f32cdcdab50349f246ebChristopher Tate            Slog.d(TAG, "Handling backup of " + key);
44e9fd1fa31ad6f62d1eb6f32cdcdab50349f246ebChristopher Tate        }
45e9fd1fa31ad6f62d1eb6f32cdcdab50349f246ebChristopher Tate        try {
46e9fd1fa31ad6f62d1eb6f32cdcdab50349f246ebChristopher Tate            switch (key) {
47e9fd1fa31ad6f62d1eb6f32cdcdab50349f246ebChristopher Tate                case KEY_PERMISSIONS:
48e9fd1fa31ad6f62d1eb6f32cdcdab50349f246ebChristopher Tate                    return pm.getPermissionGrantBackup(UserHandle.USER_SYSTEM);
49e9fd1fa31ad6f62d1eb6f32cdcdab50349f246ebChristopher Tate
50e9fd1fa31ad6f62d1eb6f32cdcdab50349f246ebChristopher Tate                default:
51e9fd1fa31ad6f62d1eb6f32cdcdab50349f246ebChristopher Tate                    Slog.w(TAG, "Unexpected backup key " + key);
52e9fd1fa31ad6f62d1eb6f32cdcdab50349f246ebChristopher Tate            }
53e9fd1fa31ad6f62d1eb6f32cdcdab50349f246ebChristopher Tate        } catch (Exception e) {
54e9fd1fa31ad6f62d1eb6f32cdcdab50349f246ebChristopher Tate            Slog.e(TAG, "Unable to store payload " + key);
55e9fd1fa31ad6f62d1eb6f32cdcdab50349f246ebChristopher Tate        }
56e9fd1fa31ad6f62d1eb6f32cdcdab50349f246ebChristopher Tate        return null;
57e9fd1fa31ad6f62d1eb6f32cdcdab50349f246ebChristopher Tate    }
58e9fd1fa31ad6f62d1eb6f32cdcdab50349f246ebChristopher Tate
59e9fd1fa31ad6f62d1eb6f32cdcdab50349f246ebChristopher Tate    @Override
60e9fd1fa31ad6f62d1eb6f32cdcdab50349f246ebChristopher Tate    protected void applyRestoredPayload(String key, byte[] payload) {
61e9fd1fa31ad6f62d1eb6f32cdcdab50349f246ebChristopher Tate        IPackageManager pm = AppGlobals.getPackageManager();
62e9fd1fa31ad6f62d1eb6f32cdcdab50349f246ebChristopher Tate        if (DEBUG) {
63e9fd1fa31ad6f62d1eb6f32cdcdab50349f246ebChristopher Tate            Slog.d(TAG, "Handling restore of " + key);
64e9fd1fa31ad6f62d1eb6f32cdcdab50349f246ebChristopher Tate        }
65e9fd1fa31ad6f62d1eb6f32cdcdab50349f246ebChristopher Tate        try {
66e9fd1fa31ad6f62d1eb6f32cdcdab50349f246ebChristopher Tate            switch (key) {
67e9fd1fa31ad6f62d1eb6f32cdcdab50349f246ebChristopher Tate                case KEY_PERMISSIONS:
68e9fd1fa31ad6f62d1eb6f32cdcdab50349f246ebChristopher Tate                    pm.restorePermissionGrants(payload, UserHandle.USER_SYSTEM);
69e9fd1fa31ad6f62d1eb6f32cdcdab50349f246ebChristopher Tate                    break;
70e9fd1fa31ad6f62d1eb6f32cdcdab50349f246ebChristopher Tate
71e9fd1fa31ad6f62d1eb6f32cdcdab50349f246ebChristopher Tate                default:
72e9fd1fa31ad6f62d1eb6f32cdcdab50349f246ebChristopher Tate                    Slog.w(TAG, "Unexpected restore key " + key);
73e9fd1fa31ad6f62d1eb6f32cdcdab50349f246ebChristopher Tate            }
74e9fd1fa31ad6f62d1eb6f32cdcdab50349f246ebChristopher Tate        } catch (Exception e) {
75e9fd1fa31ad6f62d1eb6f32cdcdab50349f246ebChristopher Tate            Slog.w(TAG, "Unable to restore key " + key);
76e9fd1fa31ad6f62d1eb6f32cdcdab50349f246ebChristopher Tate        }
77e9fd1fa31ad6f62d1eb6f32cdcdab50349f246ebChristopher Tate    }
78e9fd1fa31ad6f62d1eb6f32cdcdab50349f246ebChristopher Tate}
79