Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 4527acb0 authored by Android (Google) Code Review's avatar Android (Google) Code Review Committed by The Android Open Source Project
Browse files

am 856dd8a6: Merge change 4952 into donut

Merge commit '856dd8a6'

* commit '856dd8a6':
  Helper API cleanup.  Allows multiple helpers to function,
parents ffe4ddfe 856dd8a6
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -48,8 +48,8 @@ public class FullBackupAgent extends BackupAgent {
        }

        // That's the file set; now back it all up
        FileBackupHelper helper = new FileBackupHelper(this);
        helper.performBackup(oldState, data, newState, (String[])allFiles.toArray());
        FileBackupHelper helper = new FileBackupHelper(this, (String[])allFiles.toArray());
        helper.performBackup(oldState, data, newState);
    }

    @Override
+6 −1
Original line number Diff line number Diff line
@@ -55,6 +55,10 @@ public class BackupDataOutput {
        }
    }

    public void setKeyPrefix(String keyPrefix) {
        setKeyPrefix_native(mBackupWriter, keyPrefix);
    }

    protected void finalize() throws Throwable {
        try {
            dtor(mBackupWriter);
@@ -68,5 +72,6 @@ public class BackupDataOutput {

    private native static int writeEntityHeader_native(int mBackupWriter, String key, int dataSize);
    private native static int writeEntityData_native(int mBackupWriter, byte[] data, int size);
    private native static void setKeyPrefix_native(int mBackupWriter, String keyPrefix);
}
+15 −3
Original line number Diff line number Diff line
@@ -21,14 +21,26 @@ import android.os.ParcelFileDescriptor;
import java.io.InputStream;

/** @hide */
public interface RestoreHelper {
public interface BackupHelper {
    /**
     * Called by RestoreHelperDispatcher to dispatch one entity of data.
     * Based on oldState, determine which of the files from the application's data directory
     * need to be backed up, write them to the data stream, and fill in newState with the
     * state as it exists now.
     */
    public void performBackup(ParcelFileDescriptor oldState, BackupDataOutput data,
            ParcelFileDescriptor newState);

    /**
     * Called by BackupHelperDispatcher to dispatch one entity of data.
     * <p class=note>
     * Do not close the <code>data</code> stream.  Do not read more than
     * <code>dataSize</code> bytes from <code>data</code>.
     */
    public void restoreEntity(BackupDataInputStream data);
    public void writeSnapshot(ParcelFileDescriptor fd);

    /**
     *
     */
    public void writeRestoreSnapshot(ParcelFileDescriptor fd);
}
+56 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2009 The Android Open Source Project
 * Copyright (C) 2007 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
@@ -16,26 +16,41 @@

package android.backup;

import android.content.Context;
import android.app.BackupAgent;
import android.backup.BackupHelper;
import android.backup.BackupHelperDispatcher;
import android.backup.BackupDataInput;
import android.backup.BackupDataOutput;
import android.os.ParcelFileDescriptor;
import android.util.Log;

import java.io.File;
import java.io.IOException;

/** @hide */
public class FileRestoreHelper extends RestoreHelperBase implements RestoreHelper {
    private static final String TAG = "FileRestoreHelper";
public class BackupHelperAgent extends BackupAgent {
    static final String TAG = "BackupHelperAgent";

    File mFilesDir;
    BackupHelperDispatcher mDispatcher = new BackupHelperDispatcher();

    public FileRestoreHelper(Context context) {
        super(context);
        mFilesDir = context.getFilesDir();
    @Override
    public void onBackup(ParcelFileDescriptor oldState, BackupDataOutput data,
             ParcelFileDescriptor newState) {
        mDispatcher.performBackup(oldState, data, newState);
    }

    public void restoreEntity(BackupDataInputStream data) {
        Log.d(TAG, "got entity '" + data.getKey() + "' size=" + data.size()); // TODO: turn this off before ship
        File f = new File(mFilesDir, data.getKey());
        writeFile(f, data);
    @Override
    public void onRestore(BackupDataInput data, ParcelFileDescriptor newState)
            throws IOException {
        mDispatcher.performRestore(data, newState);
    }

    public BackupHelperDispatcher getDispatcher() {
        return mDispatcher;
    }

    public void addHelper(String keyPrefix, BackupHelper helper) {
        mDispatcher.addHelper(keyPrefix, helper);
    }
}

+24 −16
Original line number Diff line number Diff line
@@ -20,20 +20,34 @@ import android.os.ParcelFileDescriptor;
import android.util.Log;

import java.io.IOException;
import java.util.HashMap;
import java.util.TreeMap;
import java.util.Map;

/** @hide */
public class RestoreHelperDispatcher {
    private static final String TAG = "RestoreHelperDispatcher";
public class BackupHelperDispatcher {
    private static final String TAG = "BackupHelperDispatcher";

    HashMap<String,RestoreHelper> mHelpers = new HashMap<String,RestoreHelper>();
    TreeMap<String,BackupHelper> mHelpers = new TreeMap<String,BackupHelper>();
    
    public void addHelper(String keyPrefix, RestoreHelper helper) {
    public BackupHelperDispatcher() {
    }

    public void addHelper(String keyPrefix, BackupHelper helper) {
        mHelpers.put(keyPrefix, helper);
    }

    public void dispatch(BackupDataInput input, ParcelFileDescriptor newState) throws IOException {
    /** TODO: Make this save and restore the key prefix. */
    public void performBackup(ParcelFileDescriptor oldState, BackupDataOutput data,
             ParcelFileDescriptor newState) {
        // Write out the state files -- mHelpers is a TreeMap, so the order is well defined.
        for (Map.Entry<String,BackupHelper> entry: mHelpers.entrySet()) {
            data.setKeyPrefix(entry.getKey());
            entry.getValue().performBackup(oldState, data, newState);
        }
    }

    public void performRestore(BackupDataInput input, ParcelFileDescriptor newState)
            throws IOException {
        boolean alreadyComplained = false;

        BackupDataInputStream stream = new BackupDataInputStream(input);
@@ -43,7 +57,7 @@ public class RestoreHelperDispatcher {
            int pos = rawKey.indexOf(':');
            if (pos > 0) {
                String prefix = rawKey.substring(0, pos);
                RestoreHelper helper = mHelpers.get(prefix);
                BackupHelper helper = mHelpers.get(prefix);
                if (helper != null) {
                    stream.dataSize = input.getDataSize();
                    stream.key = rawKey.substring(pos+1);
@@ -63,15 +77,9 @@ public class RestoreHelperDispatcher {
            input.skipEntityData(); // In case they didn't consume the data.
        }

        if (mHelpers.size() > 1) {
            throw new RuntimeException("RestoreHelperDispatcher won't get your your"
                    + " data in the right order yet.");
        }
        
        // Write out the state files
        for (RestoreHelper helper: mHelpers.values()) {
            // TODO: Write a header for the state
            helper.writeSnapshot(newState);
        // Write out the state files -- mHelpers is a TreeMap, so the order is well defined.
        for (BackupHelper helper: mHelpers.values()) {
            helper.writeRestoreSnapshot(newState);
        }
    }
}
Loading