1 /*
2  * Copyright (C) 2017 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 package com.android.dialer.backup;
17 
18 import android.app.backup.BackupDataInput;
19 import android.app.backup.BackupDataOutput;
20 import android.os.ParcelFileDescriptor;
21 import android.support.annotation.NonNull;
22 import android.support.annotation.VisibleForTesting;
23 import android.util.ArrayMap;
24 import com.android.dialer.common.Assert;
25 import com.android.dialer.common.LogUtil;
26 import com.android.dialer.logging.DialerImpression;
27 import com.android.dialer.logging.Logger;
28 import com.google.android.libraries.backup.BackupKeyPredicate;
29 import com.google.android.libraries.backup.BackupKeyPredicates;
30 import com.google.android.libraries.backup.PersistentBackupAgentHelper;
31 import java.io.IOException;
32 import java.util.Map;
33 
34 /** Implementation of Key/Value Backup that powers Dialer's backup and restore. */
35 public class DialerPersistentBackupAgent extends PersistentBackupAgentHelper {
36 
37   private static final String[] BACKUP_NAMED_SHARED_PREFS = {
38     "com.google.android.dialer_preferences", "com.google.android.dialer", "com.android.dialer"
39   };
40 
41   @NonNull private final String[] sharedPreferencesToBackup;
42 
43   @VisibleForTesting(otherwise = VisibleForTesting.NONE)
DialerPersistentBackupAgent(@onNull String[] sharedPrefs)44   DialerPersistentBackupAgent(@NonNull String[] sharedPrefs) {
45     this.sharedPreferencesToBackup = Assert.isNotNull(sharedPrefs);
46     Logger.get(this).logImpression(DialerImpression.Type.BACKUP_KEY_VALUE_BACKUP_AGENT_CONSTRUCTOR);
47   }
48 
DialerPersistentBackupAgent()49   public DialerPersistentBackupAgent() {
50     this(BACKUP_NAMED_SHARED_PREFS);
51   }
52 
53   @Override
onRestore(BackupDataInput data, int appVersionCode, ParcelFileDescriptor stateFile)54   public void onRestore(BackupDataInput data, int appVersionCode, ParcelFileDescriptor stateFile)
55       throws IOException {
56     Logger.get(this).logImpression(DialerImpression.Type.BACKUP_KEY_VALUE_ON_RESTORE);
57     LogUtil.i("DialerPersistentBackupAgent.onRestore", "restore from version: " + appVersionCode);
58     super.onRestore(data, appVersionCode, stateFile);
59   }
60 
61   @Override
onBackup( ParcelFileDescriptor oldState, BackupDataOutput data, ParcelFileDescriptor newState)62   public void onBackup(
63       ParcelFileDescriptor oldState, BackupDataOutput data, ParcelFileDescriptor newState)
64       throws IOException {
65     Logger.get(this).logImpression(DialerImpression.Type.BACKUP_KEY_VALUE_ON_BACKUP);
66     LogUtil.i("DialerPersistentBackupAgent.onBackup", "onBackup being performed");
67     super.onBackup(oldState, data, newState);
68   }
69 
70   @Override
getBackupSpecification()71   public Map<String, BackupKeyPredicate> getBackupSpecification() {
72     Logger.get(this).logImpression(DialerImpression.Type.BACKUP_KEY_VALUE_GET_BACKUP_SPECIFICATION);
73     LogUtil.i(
74         "DialerPersistentBackupAgent.getBackupSpecification",
75         "number of files being backed up: " + sharedPreferencesToBackup.length);
76 
77     Map<String, BackupKeyPredicate> arrayMap = new ArrayMap<>();
78     for (String fileName : sharedPreferencesToBackup) {
79       LogUtil.i("DialerPersistentBackupAgent.getBackupSpecification", "arrayMap.put: " + fileName);
80       arrayMap.put(fileName, BackupKeyPredicates.alwaysTrue());
81     }
82 
83     return arrayMap;
84   }
85 
86   @Override
onRestoreFinished()87   public void onRestoreFinished() {
88     Logger.get(this).logImpression(DialerImpression.Type.BACKUP_KEY_VALUE_ON_RESTORE_FINISHED);
89     super.onRestoreFinished();
90   }
91 }
92