1111bd4acdb378266221b430a284f88a3990a3958Christopher Tate/*
2111bd4acdb378266221b430a284f88a3990a3958Christopher Tate * Copyright (C) 2009 The Android Open Source Project
3111bd4acdb378266221b430a284f88a3990a3958Christopher Tate *
4111bd4acdb378266221b430a284f88a3990a3958Christopher Tate * Licensed under the Apache License, Version 2.0 (the "License");
5111bd4acdb378266221b430a284f88a3990a3958Christopher Tate * you may not use this file except in compliance with the License.
6111bd4acdb378266221b430a284f88a3990a3958Christopher Tate * You may obtain a copy of the License at
7111bd4acdb378266221b430a284f88a3990a3958Christopher Tate *
8111bd4acdb378266221b430a284f88a3990a3958Christopher Tate *      http://www.apache.org/licenses/LICENSE-2.0
9111bd4acdb378266221b430a284f88a3990a3958Christopher Tate *
10111bd4acdb378266221b430a284f88a3990a3958Christopher Tate * Unless required by applicable law or agreed to in writing, software
11111bd4acdb378266221b430a284f88a3990a3958Christopher Tate * distributed under the License is distributed on an "AS IS" BASIS,
12111bd4acdb378266221b430a284f88a3990a3958Christopher Tate * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13111bd4acdb378266221b430a284f88a3990a3958Christopher Tate * See the License for the specific language governing permissions and
14111bd4acdb378266221b430a284f88a3990a3958Christopher Tate * limitations under the License.
15111bd4acdb378266221b430a284f88a3990a3958Christopher Tate */
16111bd4acdb378266221b430a284f88a3990a3958Christopher Tate
174528186e0d65fc68ef0dd1941aa2ac8aefcd55a3Christopher Tatepackage android.app.backup;
18111bd4acdb378266221b430a284f88a3990a3958Christopher Tate
19111bd4acdb378266221b430a284f88a3990a3958Christopher Tateimport android.content.Context;
20111bd4acdb378266221b430a284f88a3990a3958Christopher Tateimport android.os.ParcelFileDescriptor;
21111bd4acdb378266221b430a284f88a3990a3958Christopher Tateimport android.util.Log;
22111bd4acdb378266221b430a284f88a3990a3958Christopher Tate
23111bd4acdb378266221b430a284f88a3990a3958Christopher Tateimport java.io.File;
24111bd4acdb378266221b430a284f88a3990a3958Christopher Tate
25111bd4acdb378266221b430a284f88a3990a3958Christopher Tate/**
26111bd4acdb378266221b430a284f88a3990a3958Christopher Tate * Like FileBackupHelper, but takes absolute paths for the files instead of
27111bd4acdb378266221b430a284f88a3990a3958Christopher Tate * subpaths of getFilesDir()
28111bd4acdb378266221b430a284f88a3990a3958Christopher Tate *
2927a63583bfb8b4668911a819f3c7827ef0cc2ec8Christopher Tate * @hide
30111bd4acdb378266221b430a284f88a3990a3958Christopher Tate */
31111bd4acdb378266221b430a284f88a3990a3958Christopher Tatepublic class AbsoluteFileBackupHelper extends FileBackupHelperBase implements BackupHelper {
32111bd4acdb378266221b430a284f88a3990a3958Christopher Tate    private static final String TAG = "AbsoluteFileBackupHelper";
33436344ae12c819f58306ceb94241a266141e1218Christopher Tate    private static final boolean DEBUG = false;
34111bd4acdb378266221b430a284f88a3990a3958Christopher Tate
35111bd4acdb378266221b430a284f88a3990a3958Christopher Tate    Context mContext;
36111bd4acdb378266221b430a284f88a3990a3958Christopher Tate    String[] mFiles;
37111bd4acdb378266221b430a284f88a3990a3958Christopher Tate
38e28290e21f908b4e917099ff2aa41e3aab9310c2Christopher Tate    /**
39e28290e21f908b4e917099ff2aa41e3aab9310c2Christopher Tate     * Construct a helper for backing up / restoring the files at the given absolute locations
40e28290e21f908b4e917099ff2aa41e3aab9310c2Christopher Tate     * within the file system.
41e28290e21f908b4e917099ff2aa41e3aab9310c2Christopher Tate     *
42e28290e21f908b4e917099ff2aa41e3aab9310c2Christopher Tate     * @param context
43e28290e21f908b4e917099ff2aa41e3aab9310c2Christopher Tate     * @param files
44e28290e21f908b4e917099ff2aa41e3aab9310c2Christopher Tate     */
45111bd4acdb378266221b430a284f88a3990a3958Christopher Tate    public AbsoluteFileBackupHelper(Context context, String... files) {
46111bd4acdb378266221b430a284f88a3990a3958Christopher Tate        super(context);
47111bd4acdb378266221b430a284f88a3990a3958Christopher Tate
48111bd4acdb378266221b430a284f88a3990a3958Christopher Tate        mContext = context;
49111bd4acdb378266221b430a284f88a3990a3958Christopher Tate        mFiles = files;
50111bd4acdb378266221b430a284f88a3990a3958Christopher Tate    }
51111bd4acdb378266221b430a284f88a3990a3958Christopher Tate
52111bd4acdb378266221b430a284f88a3990a3958Christopher Tate    /**
53111bd4acdb378266221b430a284f88a3990a3958Christopher Tate     * Based on oldState, determine which of the files from the application's data directory
54111bd4acdb378266221b430a284f88a3990a3958Christopher Tate     * need to be backed up, write them to the data stream, and fill in newState with the
55111bd4acdb378266221b430a284f88a3990a3958Christopher Tate     * state as it exists now.
56111bd4acdb378266221b430a284f88a3990a3958Christopher Tate     */
57111bd4acdb378266221b430a284f88a3990a3958Christopher Tate    public void performBackup(ParcelFileDescriptor oldState, BackupDataOutput data,
58111bd4acdb378266221b430a284f88a3990a3958Christopher Tate            ParcelFileDescriptor newState) {
59111bd4acdb378266221b430a284f88a3990a3958Christopher Tate        // use the file paths as the keys, too
60111bd4acdb378266221b430a284f88a3990a3958Christopher Tate        performBackup_checked(oldState, data, newState, mFiles, mFiles);
61111bd4acdb378266221b430a284f88a3990a3958Christopher Tate    }
62111bd4acdb378266221b430a284f88a3990a3958Christopher Tate
63e28290e21f908b4e917099ff2aa41e3aab9310c2Christopher Tate    /**
64e28290e21f908b4e917099ff2aa41e3aab9310c2Christopher Tate     * Restore one absolute file entity from the restore stream
65e28290e21f908b4e917099ff2aa41e3aab9310c2Christopher Tate     */
66111bd4acdb378266221b430a284f88a3990a3958Christopher Tate    public void restoreEntity(BackupDataInputStream data) {
67436344ae12c819f58306ceb94241a266141e1218Christopher Tate        if (DEBUG) Log.d(TAG, "got entity '" + data.getKey() + "' size=" + data.size());
68111bd4acdb378266221b430a284f88a3990a3958Christopher Tate        String key = data.getKey();
69111bd4acdb378266221b430a284f88a3990a3958Christopher Tate        if (isKeyInList(key, mFiles)) {
70111bd4acdb378266221b430a284f88a3990a3958Christopher Tate            File f = new File(key);
71111bd4acdb378266221b430a284f88a3990a3958Christopher Tate            writeFile(f, data);
72111bd4acdb378266221b430a284f88a3990a3958Christopher Tate        }
73111bd4acdb378266221b430a284f88a3990a3958Christopher Tate    }
74111bd4acdb378266221b430a284f88a3990a3958Christopher Tate}
75111bd4acdb378266221b430a284f88a3990a3958Christopher Tate
76