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

Commit 856dd8a6 authored by Android (Google) Code Review's avatar Android (Google) Code Review
Browse files

Merge change 4952 into donut

* changes:
  Helper API cleanup.  Allows multiple helpers to function, because they'll always go in the same order, and this lets us not have to write headers to keep them paired.
parents eb717b9c 06290a4b
Loading
Loading
Loading
Loading
+2 −2
Original line number Original line Diff line number Diff line
@@ -48,8 +48,8 @@ public class FullBackupAgent extends BackupAgent {
        }
        }


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


    @Override
    @Override
+6 −1
Original line number Original line 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 {
    protected void finalize() throws Throwable {
        try {
        try {
            dtor(mBackupWriter);
            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 writeEntityHeader_native(int mBackupWriter, String key, int dataSize);
    private native static int writeEntityData_native(int mBackupWriter, byte[] data, int size);
    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 Original line Diff line number Diff line
@@ -21,14 +21,26 @@ import android.os.ParcelFileDescriptor;
import java.io.InputStream;
import java.io.InputStream;


/** @hide */
/** @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>
     * <p class=note>
     * Do not close the <code>data</code> stream.  Do not read more than
     * Do not close the <code>data</code> stream.  Do not read more than
     * <code>dataSize</code> bytes from <code>data</code>.
     * <code>dataSize</code> bytes from <code>data</code>.
     */
     */
    public void restoreEntity(BackupDataInputStream data);
    public void restoreEntity(BackupDataInputStream data);
    public void writeSnapshot(ParcelFileDescriptor fd);

    /**
     *
     */
    public void writeRestoreSnapshot(ParcelFileDescriptor fd);
}
}
+56 −0
Original line number Original line 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");
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * you may not use this file except in compliance with the License.
@@ -16,26 +16,41 @@


package android.backup;
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 android.util.Log;


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


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


    File mFilesDir;
    BackupHelperDispatcher mDispatcher = new BackupHelperDispatcher();


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


    public void restoreEntity(BackupDataInputStream data) {
    @Override
        Log.d(TAG, "got entity '" + data.getKey() + "' size=" + data.size()); // TODO: turn this off before ship
    public void onRestore(BackupDataInput data, ParcelFileDescriptor newState)
        File f = new File(mFilesDir, data.getKey());
            throws IOException {
        writeFile(f, data);
        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 Original line Diff line number Diff line
@@ -20,20 +20,34 @@ import android.os.ParcelFileDescriptor;
import android.util.Log;
import android.util.Log;


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


/** @hide */
/** @hide */
public class RestoreHelperDispatcher {
public class BackupHelperDispatcher {
    private static final String TAG = "RestoreHelperDispatcher";
    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);
        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;
        boolean alreadyComplained = false;


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


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