1 /*
2  * Copyright (C) 2016 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.accounts.AccountManagerInternal;
20 import android.app.backup.BlobBackupHelper;
21 import android.util.Slog;
22 
23 import com.android.server.LocalServices;
24 
25 /**
26  * Helper for handling backup of account manager specific state.
27  */
28 public class AccountManagerBackupHelper extends BlobBackupHelper {
29     private static final String TAG = "AccountsBackup";
30     private static final boolean DEBUG = false;
31 
32     // current schema of the backup state blob
33     private static final int STATE_VERSION = 1;
34 
35     // key under which the account access grant state blob is committed to backup
36     private static final String KEY_ACCOUNT_ACCESS_GRANTS = "account_access_grants";
37 
38     private final int mUserId;
39 
AccountManagerBackupHelper(int userId)40     public AccountManagerBackupHelper(int userId) {
41         super(STATE_VERSION, KEY_ACCOUNT_ACCESS_GRANTS);
42         mUserId = userId;
43     }
44 
45     @Override
getBackupPayload(String key)46     protected byte[] getBackupPayload(String key) {
47         AccountManagerInternal am = LocalServices.getService(AccountManagerInternal.class);
48         if (DEBUG) {
49             Slog.d(TAG, "Handling backup of " + key);
50         }
51         try {
52             switch (key) {
53                 case KEY_ACCOUNT_ACCESS_GRANTS: {
54                     return am.backupAccountAccessPermissions(mUserId);
55                 }
56 
57                 default: {
58                     Slog.w(TAG, "Unexpected backup key " + key);
59                 }
60             }
61         } catch (Exception e) {
62             Slog.e(TAG, "Unable to store payload " + key, e);
63         }
64 
65         return new byte[0];
66     }
67 
68     @Override
applyRestoredPayload(String key, byte[] payload)69     protected void applyRestoredPayload(String key, byte[] payload) {
70         AccountManagerInternal am = LocalServices.getService(AccountManagerInternal.class);
71         if (DEBUG) {
72             Slog.d(TAG, "Handling restore of " + key);
73         }
74         try {
75             switch (key) {
76                 case KEY_ACCOUNT_ACCESS_GRANTS: {
77                     am.restoreAccountAccessPermissions(payload, mUserId);
78                 } break;
79 
80                 default: {
81                     Slog.w(TAG, "Unexpected restore key " + key);
82                 }
83             }
84         } catch (Exception e) {
85             Slog.e(TAG, "Unable to restore key " + key, e);
86         }
87     }
88 }
89