AbsoluteFileBackupHelper.java revision 27a63583bfb8b4668911a819f3c7827ef0cc2ec8
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 * Like FileBackupHelper, but takes absolute paths for the files instead of
27 * subpaths of getFilesDir()
28 *
29 * @hide
30 */
31public class AbsoluteFileBackupHelper extends FileBackupHelperBase implements BackupHelper {
32    private static final String TAG = "AbsoluteFileBackupHelper";
33    private static final boolean DEBUG = false;
34
35    Context mContext;
36    String[] mFiles;
37
38    /**
39     * Construct a helper for backing up / restoring the files at the given absolute locations
40     * within the file system.
41     *
42     * @param context
43     * @param files
44     */
45    public AbsoluteFileBackupHelper(Context context, String... files) {
46        super(context);
47
48        mContext = context;
49        mFiles = files;
50    }
51
52    /**
53     * Based on oldState, determine which of the files from the application's data directory
54     * need to be backed up, write them to the data stream, and fill in newState with the
55     * state as it exists now.
56     */
57    public void performBackup(ParcelFileDescriptor oldState, BackupDataOutput data,
58            ParcelFileDescriptor newState) {
59        // use the file paths as the keys, too
60        performBackup_checked(oldState, data, newState, mFiles, mFiles);
61    }
62
63    /**
64     * Restore one absolute file entity from the restore stream
65     */
66    public void restoreEntity(BackupDataInputStream data) {
67        if (DEBUG) Log.d(TAG, "got entity '" + data.getKey() + "' size=" + data.size());
68        String key = data.getKey();
69        if (isKeyInList(key, mFiles)) {
70            File f = new File(key);
71            writeFile(f, data);
72        }
73    }
74}
75
76