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.NonNull;
20 import android.annotation.UserIdInt;
21 import android.app.backup.BlobBackupHelper;
22 import android.permission.PermissionManagerInternal;
23 import android.util.Slog;
24 
25 import com.android.server.LocalServices;
26 
27 public class PermissionBackupHelper extends BlobBackupHelper {
28     private static final String TAG = "PermissionBackup";
29     private static final boolean DEBUG = false;
30 
31     // current schema of the backup state blob
32     private static final int STATE_VERSION = 1;
33 
34     // key under which the permission-grant state blob is committed to backup
35     private static final String KEY_PERMISSIONS = "permissions";
36 
37     private final @UserIdInt int mUserId;
38 
39     private final @NonNull PermissionManagerInternal mPermissionManager;
40 
PermissionBackupHelper(int userId)41     public PermissionBackupHelper(int userId) {
42         super(STATE_VERSION, KEY_PERMISSIONS);
43 
44         mUserId = userId;
45         mPermissionManager = LocalServices.getService(PermissionManagerInternal.class);
46     }
47 
48     @Override
getBackupPayload(String key)49     protected byte[] getBackupPayload(String key) {
50         if (DEBUG) {
51             Slog.d(TAG, "Handling backup of " + key);
52         }
53         try {
54             switch (key) {
55                 case KEY_PERMISSIONS:
56                     return mPermissionManager.backupRuntimePermissions(mUserId);
57 
58                 default:
59                     Slog.w(TAG, "Unexpected backup key " + key);
60             }
61         } catch (Exception e) {
62             Slog.e(TAG, "Unable to store payload " + key, e);
63         }
64         return null;
65     }
66 
67     @Override
applyRestoredPayload(String key, byte[] payload)68     protected void applyRestoredPayload(String key, byte[] payload) {
69         if (DEBUG) {
70             Slog.d(TAG, "Handling restore of " + key);
71         }
72         try {
73             switch (key) {
74                 case KEY_PERMISSIONS:
75                     mPermissionManager.restoreRuntimePermissions(payload, mUserId);
76                     break;
77 
78                 default:
79                     Slog.w(TAG, "Unexpected restore key " + key);
80             }
81         } catch (Exception e) {
82             Slog.e(TAG, "Unable to restore key " + key, e);
83         }
84     }
85 }
86