Merge "Recents backup helper" into lmp-mr1-dev
This commit is contained in:
committed by
Android (Google) Code Review
commit
ad064d48ad
114
core/java/android/app/backup/RecentsBackupHelper.java
Normal file
114
core/java/android/app/backup/RecentsBackupHelper.java
Normal file
@@ -0,0 +1,114 @@
|
||||
package android.app.backup;
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.Environment;
|
||||
import android.os.ParcelFileDescriptor;
|
||||
import android.util.Slog;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
/**
|
||||
* Helper for saving/restoring 'recent tasks' infrastructure.
|
||||
* @hide
|
||||
*/
|
||||
public class RecentsBackupHelper implements BackupHelper {
|
||||
private static final String TAG = "RecentsBackup";
|
||||
private static final boolean DEBUG = false;
|
||||
|
||||
// This must match TaskPersister.TASKS_DIRNAME, but that class is not accessible from here
|
||||
private static final String RECENTS_TASK_DIR = "recent_tasks";
|
||||
|
||||
// Must match TaskPersister.IMAGES_DIRNAME, as above
|
||||
private static final String RECENTS_IMAGE_DIR = "recent_images";
|
||||
|
||||
// At restore time, tasks/thumbnails are placed in these directories alongside
|
||||
// the "live" recents dirs named above.
|
||||
private static final String RECENTS_TASK_RESTORE_DIR = "restored_" + RECENTS_TASK_DIR;
|
||||
private static final String RECENTS_IMAGE_RESTORE_DIR = "restored_" + RECENTS_IMAGE_DIR;
|
||||
|
||||
// Prefixes for tagging the two kinds of recents backup records that we might generate
|
||||
private static final String RECENTS_TASK_KEY = "task:";
|
||||
private static final String RECENTS_IMAGE_KEY = "image:";
|
||||
|
||||
FileBackupHelperBase mTaskFileHelper;
|
||||
|
||||
final File mSystemDir;
|
||||
final File mTasksDir;
|
||||
final File mRestoredTasksDir;
|
||||
final File mRestoredImagesDir;
|
||||
final String[] mRecentFiles;
|
||||
final String[] mRecentKeys;
|
||||
|
||||
/**
|
||||
* @param context The agent context in which this helper instance will run
|
||||
*/
|
||||
public RecentsBackupHelper(Context context) {
|
||||
mTaskFileHelper = new FileBackupHelperBase(context);
|
||||
|
||||
mSystemDir = new File(Environment.getDataDirectory(), "system");
|
||||
mTasksDir = new File(mSystemDir, RECENTS_TASK_DIR);
|
||||
mRestoredTasksDir = new File(mSystemDir, RECENTS_TASK_RESTORE_DIR);
|
||||
mRestoredImagesDir = new File(mSystemDir, RECENTS_IMAGE_RESTORE_DIR);
|
||||
|
||||
// Currently we back up only the recent-task descriptions, not the thumbnails
|
||||
File[] recentFiles = mTasksDir.listFiles();
|
||||
if (recentFiles != null) {
|
||||
// We explicitly proceed even if this is a zero-size array
|
||||
final int N = recentFiles.length;
|
||||
mRecentKeys = new String[N];
|
||||
mRecentFiles = new String[N];
|
||||
if (DEBUG) {
|
||||
Slog.i(TAG, "Identifying recents for backup: " + N);
|
||||
}
|
||||
for (int i = 0; i < N; i++) {
|
||||
mRecentKeys[i] = new String(RECENTS_TASK_KEY + recentFiles[i].getName());
|
||||
mRecentFiles[i] = recentFiles[i].getAbsolutePath();
|
||||
if (DEBUG) {
|
||||
Slog.i(TAG, " " + mRecentKeys[i]);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
mRecentFiles = mRecentKeys = new String[0];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Task-file key: RECENTS_TASK_KEY + leaf filename
|
||||
* Thumbnail-file key: RECENTS_IMAGE_KEY + leaf filename
|
||||
*/
|
||||
@Override
|
||||
public void performBackup(ParcelFileDescriptor oldState, BackupDataOutput data,
|
||||
ParcelFileDescriptor newState) {
|
||||
FileBackupHelperBase.performBackup_checked(oldState, data, newState,
|
||||
mRecentFiles, mRecentKeys);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void restoreEntity(BackupDataInputStream data) {
|
||||
final String key = data.getKey();
|
||||
File output = null;
|
||||
if (key.startsWith(RECENTS_TASK_KEY)) {
|
||||
String name = key.substring(RECENTS_TASK_KEY.length());
|
||||
output = new File(mRestoredTasksDir, name);
|
||||
mRestoredTasksDir.mkdirs();
|
||||
} else if (key.startsWith(RECENTS_IMAGE_KEY)) {
|
||||
String name = key.substring(RECENTS_IMAGE_KEY.length());
|
||||
output = new File(mRestoredImagesDir, name);
|
||||
mRestoredImagesDir.mkdirs();
|
||||
}
|
||||
|
||||
if (output != null) {
|
||||
if (DEBUG) {
|
||||
Slog.i(TAG, "Restoring key='"
|
||||
+ key + "' to " + output.getAbsolutePath());
|
||||
}
|
||||
mTaskFileHelper.writeFile(output, data);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeNewStateDescription(ParcelFileDescriptor newState) {
|
||||
mTaskFileHelper.writeNewStateDescription(newState);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -23,6 +23,7 @@ import android.app.backup.BackupDataOutput;
|
||||
import android.app.backup.BackupAgentHelper;
|
||||
import android.app.backup.FullBackup;
|
||||
import android.app.backup.FullBackupDataOutput;
|
||||
import android.app.backup.RecentsBackupHelper;
|
||||
import android.app.backup.WallpaperBackupHelper;
|
||||
import android.content.Context;
|
||||
import android.os.Environment;
|
||||
@@ -83,6 +84,8 @@ public class SystemBackupAgent extends BackupAgentHelper {
|
||||
}
|
||||
}
|
||||
addHelper("wallpaper", new WallpaperBackupHelper(SystemBackupAgent.this, files, keys));
|
||||
addHelper("recents", new RecentsBackupHelper(SystemBackupAgent.this));
|
||||
|
||||
super.onBackup(oldState, data, newState);
|
||||
}
|
||||
|
||||
@@ -113,6 +116,7 @@ public class SystemBackupAgent extends BackupAgentHelper {
|
||||
addHelper("system_files", new WallpaperBackupHelper(SystemBackupAgent.this,
|
||||
new String[] { WALLPAPER_IMAGE },
|
||||
new String[] { WALLPAPER_IMAGE_KEY} ));
|
||||
addHelper("recents", new RecentsBackupHelper(SystemBackupAgent.this));
|
||||
|
||||
try {
|
||||
super.onRestore(data, appVersionCode, newState);
|
||||
|
||||
@@ -63,6 +63,12 @@ public class TaskPersister {
|
||||
private static final String IMAGES_DIRNAME = "recent_images";
|
||||
static final String IMAGE_EXTENSION = ".png";
|
||||
|
||||
// Directory where restored historical task XML/PNG files are placed. This directory
|
||||
// contains subdirs named after TASKS_DIRNAME and IMAGES_DIRNAME mirroring the
|
||||
// ancestral device's dataset. This needs to match the RECENTS_TASK_RESTORE_DIR
|
||||
// value in RecentsBackupHelper.
|
||||
private static final String RESTORED_TASKS = "restored_" + TASKS_DIRNAME;
|
||||
|
||||
private static final String TAG_TASK = "task";
|
||||
|
||||
static File sImagesDir;
|
||||
|
||||
Reference in New Issue
Block a user