1/*
2 * Copyright (C) 2009 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
17package android.app.backup;
18
19import android.content.Context;
20import android.os.ParcelFileDescriptor;
21import android.util.Log;
22
23import java.io.File;
24
25/**
26 * A helper class that can be used in conjunction with
27 * {@link android.app.backup.BackupAgentHelper} to manage the backup of a set of
28 * files. Whenever backup is performed, all files changed since the last backup
29 * will be saved in their entirety. When backup first occurs,
30 * every file in the list provided to {@link #FileBackupHelper} will be backed up.
31 * <p>
32 * During restore, if the helper encounters data for a file that was not
33 * specified when the FileBackupHelper object was constructed, that data
34 * will be ignored.
35 * <p class="note"><strong>Note:</strong> This should be
36 * used only with small configuration files, not large binary files.
37 */
38public class FileBackupHelper extends FileBackupHelperBase implements BackupHelper {
39    private static final String TAG = "FileBackupHelper";
40    private static final boolean DEBUG = false;
41
42    Context mContext;
43    File mFilesDir;
44    String[] mFiles;
45
46    /**
47     * Construct a helper to manage backup/restore of entire files within the
48     * application's data directory hierarchy.
49     *
50     * @param context The backup agent's Context object
51     * @param files A list of the files to be backed up or restored.
52     */
53    public FileBackupHelper(Context context, String... files) {
54        super(context);
55
56        mContext = context;
57        mFilesDir = context.getFilesDir();
58        mFiles = files;
59    }
60
61    /**
62     * Based on <code>oldState</code>, determine which of the files from the
63     * application's data directory need to be backed up, write them to the data
64     * stream, and fill in <code>newState</code> with the state as it exists
65     * now. When <code>oldState</code> is <code>null</code>, all the files will
66     * be backed up.
67     * <p>
68     * This should only be called directly from within the {@link BackupAgentHelper}
69     * implementation. See
70     * {@link android.app.backup.BackupAgent#onBackup(ParcelFileDescriptor, BackupDataOutput, ParcelFileDescriptor)}
71     * for a description of parameter meanings.
72     */
73    public void performBackup(ParcelFileDescriptor oldState, BackupDataOutput data,
74            ParcelFileDescriptor newState) {
75        // file names
76        String[] files = mFiles;
77        File base = mContext.getFilesDir();
78        final int N = files.length;
79        String[] fullPaths = new String[N];
80        for (int i=0; i<N; i++) {
81            fullPaths[i] = (new File(base, files[i])).getAbsolutePath();
82        }
83
84        // go
85        performBackup_checked(oldState, data, newState, fullPaths, files);
86    }
87
88    /**
89     * Restore one record [representing a single file] from the restore dataset.
90     * <p>
91     * This should only be called directly from within the {@link BackupAgentHelper}
92     * implementation.
93     */
94    public void restoreEntity(BackupDataInputStream data) {
95        if (DEBUG) Log.d(TAG, "got entity '" + data.getKey() + "' size=" + data.size());
96        String key = data.getKey();
97        if (isKeyInList(key, mFiles)) {
98            File f = new File(mFilesDir, key);
99            writeFile(f, data);
100        }
101    }
102}
103
104