Merge "BackupAgent#getBackupQuota() API"

This commit is contained in:
Shreyas Basarge
2017-02-13 13:45:55 +00:00
committed by Android (Google) Code Review
7 changed files with 84 additions and 28 deletions

View File

@@ -41,6 +41,8 @@ oneway interface IBackupAgent {
* @param newState Read-write file, empty when onBackup() is called,
* where the new state blob is to be recorded.
*
* @param quota Quota reported by the transport for this backup operation (in bytes).
*
* @param token Opaque token identifying this transaction. This must
* be echoed back to the backup service binder once the new
* data has been written to the data and newState files.
@@ -51,7 +53,7 @@ oneway interface IBackupAgent {
void doBackup(in ParcelFileDescriptor oldState,
in ParcelFileDescriptor data,
in ParcelFileDescriptor newState,
int token, IBackupManager callbackBinder);
long quotaBytes, int token, IBackupManager callbackBinder);
/**
* Restore an entire data snapshot to the application.
@@ -89,6 +91,8 @@ oneway interface IBackupAgent {
* The data must be formatted correctly for the resulting archive to be
* legitimate, so that will be tightly controlled by the available API.
*
* @param quota Quota reported by the transport for this backup operation (in bytes).
*
* @param token Opaque token identifying this transaction. This must
* be echoed back to the backup service binder once the agent is
* finished restoring the application based on the restore data
@@ -97,12 +101,12 @@ oneway interface IBackupAgent {
* @param callbackBinder Binder on which to indicate operation completion,
* passed here as a convenience to the agent.
*/
void doFullBackup(in ParcelFileDescriptor data, int token, IBackupManager callbackBinder);
void doFullBackup(in ParcelFileDescriptor data, long quotaBytes, int token, IBackupManager callbackBinder);
/**
* Estimate how much data a full backup will deliver
*/
void doMeasureFullBackup(int token, IBackupManager callbackBinder);
void doMeasureFullBackup(long quotaBytes, int token, IBackupManager callbackBinder);
/**
* Tells the application agent that the backup data size exceeded current transport quota.

View File

@@ -133,6 +133,8 @@ public abstract class BackupAgent extends ContextWrapper {
Handler mHandler = null;
private long mBackupQuotaBytes = -1;
Handler getHandler() {
if (mHandler == null) {
mHandler = new Handler(Looper.getMainLooper());
@@ -183,6 +185,21 @@ public abstract class BackupAgent extends ContextWrapper {
public void onDestroy() {
}
/**
* Returns the quota in bytes for the currently requested backup operation. The value can
* vary for each operation depending on the type of backup being done.
*
* <p>Can be called only from {@link BackupAgent#onFullBackup(FullBackupDataOutput)} or
* {@link BackupAgent#onBackup(ParcelFileDescriptor, BackupDataOutput, ParcelFileDescriptor)}.
*/
public long getBackupQuota() {
if (mBackupQuotaBytes < 0) {
throw new IllegalStateException(
"Backup quota is available only during backup operations.");
}
return mBackupQuotaBytes;
}
/**
* The application is being asked to write any data changed since the last
* time it performed a backup operation. The state data recorded during the
@@ -897,10 +914,12 @@ public abstract class BackupAgent extends ContextWrapper {
public void doBackup(ParcelFileDescriptor oldState,
ParcelFileDescriptor data,
ParcelFileDescriptor newState,
int token, IBackupManager callbackBinder) throws RemoteException {
long quotaBytes, int token, IBackupManager callbackBinder) throws RemoteException {
// Ensure that we're running with the app's normal permission level
long ident = Binder.clearCallingIdentity();
mBackupQuotaBytes = quotaBytes;
if (DEBUG) Log.v(TAG, "doBackup() invoked");
BackupDataOutput output = new BackupDataOutput(data.getFileDescriptor());
@@ -918,6 +937,9 @@ public abstract class BackupAgent extends ContextWrapper {
// guarantee themselves).
waitForSharedPrefs();
// Unset quota after onBackup is done.
mBackupQuotaBytes = -1;
Binder.restoreCallingIdentity(ident);
try {
callbackBinder.opComplete(token, 0);
@@ -971,10 +993,12 @@ public abstract class BackupAgent extends ContextWrapper {
@Override
public void doFullBackup(ParcelFileDescriptor data,
int token, IBackupManager callbackBinder) {
long quotaBytes, int token, IBackupManager callbackBinder) {
// Ensure that we're running with the app's normal permission level
long ident = Binder.clearCallingIdentity();
mBackupQuotaBytes = quotaBytes;
if (DEBUG) Log.v(TAG, "doFullBackup() invoked");
// Ensure that any SharedPreferences writes have landed *before*
@@ -993,6 +1017,9 @@ public abstract class BackupAgent extends ContextWrapper {
// ... and then again after, as in the doBackup() case
waitForSharedPrefs();
// Unset quota after onFullBackup is done.
mBackupQuotaBytes = -1;
// Send the EOD marker indicating that there is no more data
// forthcoming from this agent.
try {
@@ -1016,11 +1043,13 @@ public abstract class BackupAgent extends ContextWrapper {
}
}
public void doMeasureFullBackup(int token, IBackupManager callbackBinder) {
public void doMeasureFullBackup(long quotaBytes, int token, IBackupManager callbackBinder) {
// Ensure that we're running with the app's normal permission level
final long ident = Binder.clearCallingIdentity();
FullBackupDataOutput measureOutput = new FullBackupDataOutput();
mBackupQuotaBytes = quotaBytes;
waitForSharedPrefs();
try {
BackupAgent.this.onFullBackup(measureOutput);
@@ -1031,6 +1060,8 @@ public abstract class BackupAgent extends ContextWrapper {
Log.d(TAG, "onFullBackup[M] (" + BackupAgent.this.getClass().getName() + ") threw", ex);
throw ex;
} finally {
// Unset quota after onFullBackup is done.
mBackupQuotaBytes = -1;
Binder.restoreCallingIdentity(ident);
try {
callbackBinder.opComplete(token, measureOutput.getSize());

View File

@@ -73,6 +73,8 @@ public class LocalTransport extends BackupTransport {
// Full backup size quota is set to reasonable value.
private static final long FULL_BACKUP_SIZE_QUOTA = 25 * 1024 * 1024;
private static final long KEY_VALUE_BACKUP_SIZE_QUOTA = 5 * 1024 * 1024;
private Context mContext;
private File mDataDir = new File(Environment.getDownloadCacheDirectory(), "backup");
private File mCurrentSetDir = new File(mDataDir, Long.toString(CURRENT_SET_TOKEN));
@@ -712,6 +714,6 @@ public class LocalTransport extends BackupTransport {
@Override
public long getBackupQuota(String packageName, boolean isFullBackup) {
return isFullBackup ? FULL_BACKUP_SIZE_QUOTA : Long.MAX_VALUE;
return isFullBackup ? FULL_BACKUP_SIZE_QUOTA : KEY_VALUE_BACKUP_SIZE_QUOTA;
}
}