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.os.UserHandle;
22 import android.util.Slog;
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 
AccountManagerBackupHelper()38     public AccountManagerBackupHelper() {
39         super(STATE_VERSION, KEY_ACCOUNT_ACCESS_GRANTS);
40     }
41 
42     @Override
getBackupPayload(String key)43     protected byte[] getBackupPayload(String key) {
44         AccountManagerInternal am = LocalServices.getService(AccountManagerInternal.class);
45         if (DEBUG) {
46             Slog.d(TAG, "Handling backup of " + key);
47         }
48         try {
49             switch (key) {
50                 case KEY_ACCOUNT_ACCESS_GRANTS: {
51                     return am.backupAccountAccessPermissions(UserHandle.USER_SYSTEM);
52                 }
53 
54                 default: {
55                     Slog.w(TAG, "Unexpected backup key " + key);
56                 }
57             }
58         } catch (Exception e) {
59             Slog.e(TAG, "Unable to store payload " + key, e);
60         }
61 
62         return new byte[0];
63     }
64 
65     @Override
applyRestoredPayload(String key, byte[] payload)66     protected void applyRestoredPayload(String key, byte[] payload) {
67         AccountManagerInternal am = LocalServices.getService(AccountManagerInternal.class);
68         if (DEBUG) {
69             Slog.d(TAG, "Handling restore of " + key);
70         }
71         try {
72             switch (key) {
73                 case KEY_ACCOUNT_ACCESS_GRANTS: {
74                     am.restoreAccountAccessPermissions(payload, UserHandle.USER_SYSTEM);
75                 } break;
76 
77                 default: {
78                     Slog.w(TAG, "Unexpected restore key " + key);
79                 }
80             }
81         } catch (Exception e) {
82             Slog.e(TAG, "Unable to restore key " + key, e);
83         }
84     }
85 }
86