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.documentsui.prefs;
18 
19 import android.app.backup.BackupAgentHelper;
20 import android.app.backup.BackupDataInput;
21 import android.app.backup.BackupDataOutput;
22 import android.app.backup.SharedPreferencesBackupHelper;
23 import android.content.SharedPreferences;
24 import android.os.ParcelFileDescriptor;
25 import androidx.annotation.VisibleForTesting;
26 
27 import java.io.IOException;
28 
29 /**
30  * Provides glue between backup infrastructure and PrefsBackupHelper (which contains the core logic
31  * for retrieving and restoring settings).
32  *
33  * When doing backup & restore, we create and add a {@link SharedPreferencesBackupHelper} for our
34  * backup preferences file in {@link #onCreate}, and populate the backup preferences file in
35  * {@link #onBackup} and {@link #onRestore}. Then {@link BackupAgentHelper#onBackup} and
36  * {@link BackupAgentHelper#onRestore} will take care of the rest of the work. See external
37  * documentation below.
38  *
39  * https://developer.android.com/guide/topics/data/keyvaluebackup.html#BackupAgentHelper
40  */
41 public class BackupAgent extends BackupAgentHelper {
42 
43     /**
44      * Name of the shared preferences file used by BackupAgent. Should only be used in
45      * this class.
46      *
47      * @see #onBackup(ParcelFileDescriptor, BackupDataOutput, ParcelFileDescriptor)
48      * @see #onRestore(BackupDataInput, int, ParcelFileDescriptor)
49      */
50     private static final String BACKUP_PREFS = "documentsui_backup_prefs";
51 
52     /**
53      * An arbitrary string used by the BackupHelper.
54      *
55      * BackupAgentHelper works with BackupHelper. When adding a BackupHelper in #onCreate,
56      * it requires a "key". This string is that "key".
57      *
58      * https://developer.android.com/guide/topics/data/keyvaluebackup.html#BackupAgentHelper
59      * See "Backing up SharedPreference" for the purpose of this string.
60      *
61      * @see #onCreate()
62      */
63     private static final String BACKUP_HELPER_KEY = "DOCUMENTSUI_BACKUP_HELPER_KEY";
64 
65     private PrefsBackupHelper mPrefsBackupHelper;
66     private SharedPreferences mBackupPreferences;
67 
68     @Override
onCreate()69     public void onCreate() {
70         addHelper(BACKUP_HELPER_KEY, new SharedPreferencesBackupHelper(this, BACKUP_PREFS));
71         mPrefsBackupHelper = new PrefsBackupHelper(this);
72         mBackupPreferences = getSharedPreferences(BACKUP_PREFS, MODE_PRIVATE);
73     }
74 
75     @Override
onBackup(ParcelFileDescriptor oldState, BackupDataOutput data, ParcelFileDescriptor newState)76     public void onBackup(ParcelFileDescriptor oldState, BackupDataOutput data,
77             ParcelFileDescriptor newState) throws IOException {
78         mPrefsBackupHelper.getBackupPreferences(mBackupPreferences);
79         super.onBackup(oldState, data, newState);
80         mBackupPreferences.edit().clear().apply();
81     }
82 
83     @Override
onRestore(BackupDataInput data, int appVersionCode, ParcelFileDescriptor newState)84     public void onRestore(BackupDataInput data, int appVersionCode, ParcelFileDescriptor newState)
85             throws IOException {
86         // TODO: refresh the UI after restore finished. Currently the restore for system apps only
87         // happens during SUW, at which time user haven't open the app. However the restore time may
88         // change in ODR. Once it happens, we may need to refresh the UI after restore finished.
89         super.onRestore(data, appVersionCode, newState);
90         mPrefsBackupHelper.putBackupPreferences(mBackupPreferences);
91         mBackupPreferences.edit().clear().apply();
92     }
93 
94 }
95