Merge "API: make "what's the quota?" an operation on the backup data sinks" into oc-dev

This commit is contained in:
TreeHugger Robot
2017-04-29 03:05:25 +00:00
committed by Android (Google) Code Review
6 changed files with 52 additions and 40 deletions

View File

@@ -6703,7 +6703,6 @@ package android.app.backup {
public abstract class BackupAgent extends android.content.ContextWrapper { public abstract class BackupAgent extends android.content.ContextWrapper {
ctor public BackupAgent(); ctor public BackupAgent();
method public final void fullBackupFile(java.io.File, android.app.backup.FullBackupDataOutput); method public final void fullBackupFile(java.io.File, android.app.backup.FullBackupDataOutput);
method public long getBackupQuota();
method public abstract void onBackup(android.os.ParcelFileDescriptor, android.app.backup.BackupDataOutput, android.os.ParcelFileDescriptor) throws java.io.IOException; method public abstract void onBackup(android.os.ParcelFileDescriptor, android.app.backup.BackupDataOutput, android.os.ParcelFileDescriptor) throws java.io.IOException;
method public void onCreate(); method public void onCreate();
method public void onDestroy(); method public void onDestroy();
@@ -6738,6 +6737,7 @@ package android.app.backup {
} }
public class BackupDataOutput { public class BackupDataOutput {
method public long getQuota();
method public int writeEntityData(byte[], int) throws java.io.IOException; method public int writeEntityData(byte[], int) throws java.io.IOException;
method public int writeEntityHeader(java.lang.String, int) throws java.io.IOException; method public int writeEntityHeader(java.lang.String, int) throws java.io.IOException;
} }
@@ -6766,6 +6766,7 @@ package android.app.backup {
} }
public class FullBackupDataOutput { public class FullBackupDataOutput {
method public long getQuota();
} }
public abstract class RestoreObserver { public abstract class RestoreObserver {

View File

@@ -6952,7 +6952,6 @@ package android.app.backup {
public abstract class BackupAgent extends android.content.ContextWrapper { public abstract class BackupAgent extends android.content.ContextWrapper {
ctor public BackupAgent(); ctor public BackupAgent();
method public final void fullBackupFile(java.io.File, android.app.backup.FullBackupDataOutput); method public final void fullBackupFile(java.io.File, android.app.backup.FullBackupDataOutput);
method public long getBackupQuota();
method public abstract void onBackup(android.os.ParcelFileDescriptor, android.app.backup.BackupDataOutput, android.os.ParcelFileDescriptor) throws java.io.IOException; method public abstract void onBackup(android.os.ParcelFileDescriptor, android.app.backup.BackupDataOutput, android.os.ParcelFileDescriptor) throws java.io.IOException;
method public void onCreate(); method public void onCreate();
method public void onDestroy(); method public void onDestroy();
@@ -6989,6 +6988,8 @@ package android.app.backup {
public class BackupDataOutput { public class BackupDataOutput {
ctor public BackupDataOutput(java.io.FileDescriptor); ctor public BackupDataOutput(java.io.FileDescriptor);
ctor public BackupDataOutput(java.io.FileDescriptor, long);
method public long getQuota();
method public int writeEntityData(byte[], int) throws java.io.IOException; method public int writeEntityData(byte[], int) throws java.io.IOException;
method public int writeEntityHeader(java.lang.String, int) throws java.io.IOException; method public int writeEntityHeader(java.lang.String, int) throws java.io.IOException;
} }
@@ -7165,6 +7166,7 @@ package android.app.backup {
} }
public class FullBackupDataOutput { public class FullBackupDataOutput {
method public long getQuota();
} }
public class RestoreDescription implements android.os.Parcelable { public class RestoreDescription implements android.os.Parcelable {

View File

@@ -6733,7 +6733,6 @@ package android.app.backup {
public abstract class BackupAgent extends android.content.ContextWrapper { public abstract class BackupAgent extends android.content.ContextWrapper {
ctor public BackupAgent(); ctor public BackupAgent();
method public final void fullBackupFile(java.io.File, android.app.backup.FullBackupDataOutput); method public final void fullBackupFile(java.io.File, android.app.backup.FullBackupDataOutput);
method public long getBackupQuota();
method public abstract void onBackup(android.os.ParcelFileDescriptor, android.app.backup.BackupDataOutput, android.os.ParcelFileDescriptor) throws java.io.IOException; method public abstract void onBackup(android.os.ParcelFileDescriptor, android.app.backup.BackupDataOutput, android.os.ParcelFileDescriptor) throws java.io.IOException;
method public void onCreate(); method public void onCreate();
method public void onDestroy(); method public void onDestroy();
@@ -6768,6 +6767,7 @@ package android.app.backup {
} }
public class BackupDataOutput { public class BackupDataOutput {
method public long getQuota();
method public int writeEntityData(byte[], int) throws java.io.IOException; method public int writeEntityData(byte[], int) throws java.io.IOException;
method public int writeEntityHeader(java.lang.String, int) throws java.io.IOException; method public int writeEntityHeader(java.lang.String, int) throws java.io.IOException;
} }
@@ -6796,6 +6796,7 @@ package android.app.backup {
} }
public class FullBackupDataOutput { public class FullBackupDataOutput {
method public long getQuota();
} }
public abstract class RestoreObserver { public abstract class RestoreObserver {

View File

@@ -133,8 +133,6 @@ public abstract class BackupAgent extends ContextWrapper {
Handler mHandler = null; Handler mHandler = null;
private long mBackupQuotaBytes = -1;
Handler getHandler() { Handler getHandler() {
if (mHandler == null) { if (mHandler == null) {
mHandler = new Handler(Looper.getMainLooper()); mHandler = new Handler(Looper.getMainLooper());
@@ -185,21 +183,6 @@ public abstract class BackupAgent extends ContextWrapper {
public void onDestroy() { 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 * 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 * time it performed a backup operation. The state data recorded during the
@@ -918,10 +901,8 @@ public abstract class BackupAgent extends ContextWrapper {
// Ensure that we're running with the app's normal permission level // Ensure that we're running with the app's normal permission level
long ident = Binder.clearCallingIdentity(); long ident = Binder.clearCallingIdentity();
mBackupQuotaBytes = quotaBytes;
if (DEBUG) Log.v(TAG, "doBackup() invoked"); if (DEBUG) Log.v(TAG, "doBackup() invoked");
BackupDataOutput output = new BackupDataOutput(data.getFileDescriptor()); BackupDataOutput output = new BackupDataOutput(data.getFileDescriptor(), quotaBytes);
try { try {
BackupAgent.this.onBackup(oldState, output, newState); BackupAgent.this.onBackup(oldState, output, newState);
@@ -937,9 +918,6 @@ public abstract class BackupAgent extends ContextWrapper {
// guarantee themselves). // guarantee themselves).
waitForSharedPrefs(); waitForSharedPrefs();
// Unset quota after onBackup is done.
mBackupQuotaBytes = -1;
Binder.restoreCallingIdentity(ident); Binder.restoreCallingIdentity(ident);
try { try {
callbackBinder.opComplete(token, 0); callbackBinder.opComplete(token, 0);
@@ -997,8 +975,6 @@ public abstract class BackupAgent extends ContextWrapper {
// Ensure that we're running with the app's normal permission level // Ensure that we're running with the app's normal permission level
long ident = Binder.clearCallingIdentity(); long ident = Binder.clearCallingIdentity();
mBackupQuotaBytes = quotaBytes;
if (DEBUG) Log.v(TAG, "doFullBackup() invoked"); if (DEBUG) Log.v(TAG, "doFullBackup() invoked");
// Ensure that any SharedPreferences writes have landed *before* // Ensure that any SharedPreferences writes have landed *before*
@@ -1006,7 +982,7 @@ public abstract class BackupAgent extends ContextWrapper {
waitForSharedPrefs(); waitForSharedPrefs();
try { try {
BackupAgent.this.onFullBackup(new FullBackupDataOutput(data)); BackupAgent.this.onFullBackup(new FullBackupDataOutput(data, quotaBytes));
} catch (IOException ex) { } catch (IOException ex) {
Log.d(TAG, "onFullBackup (" + BackupAgent.this.getClass().getName() + ") threw", ex); Log.d(TAG, "onFullBackup (" + BackupAgent.this.getClass().getName() + ") threw", ex);
throw new RuntimeException(ex); throw new RuntimeException(ex);
@@ -1017,9 +993,6 @@ public abstract class BackupAgent extends ContextWrapper {
// ... and then again after, as in the doBackup() case // ... and then again after, as in the doBackup() case
waitForSharedPrefs(); waitForSharedPrefs();
// Unset quota after onFullBackup is done.
mBackupQuotaBytes = -1;
// Send the EOD marker indicating that there is no more data // Send the EOD marker indicating that there is no more data
// forthcoming from this agent. // forthcoming from this agent.
try { try {
@@ -1046,9 +1019,7 @@ public abstract class BackupAgent extends ContextWrapper {
public void doMeasureFullBackup(long quotaBytes, 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 // Ensure that we're running with the app's normal permission level
final long ident = Binder.clearCallingIdentity(); final long ident = Binder.clearCallingIdentity();
FullBackupDataOutput measureOutput = new FullBackupDataOutput(); FullBackupDataOutput measureOutput = new FullBackupDataOutput(quotaBytes);
mBackupQuotaBytes = quotaBytes;
waitForSharedPrefs(); waitForSharedPrefs();
try { try {
@@ -1060,8 +1031,6 @@ public abstract class BackupAgent extends ContextWrapper {
Log.d(TAG, "onFullBackup[M] (" + BackupAgent.this.getClass().getName() + ") threw", ex); Log.d(TAG, "onFullBackup[M] (" + BackupAgent.this.getClass().getName() + ") threw", ex);
throw ex; throw ex;
} finally { } finally {
// Unset quota after onFullBackup is done.
mBackupQuotaBytes = -1;
Binder.restoreCallingIdentity(ident); Binder.restoreCallingIdentity(ident);
try { try {
callbackBinder.opComplete(token, measureOutput.getSize()); callbackBinder.opComplete(token, measureOutput.getSize());

View File

@@ -62,18 +62,39 @@ import java.io.IOException;
* @see BackupAgent * @see BackupAgent
*/ */
public class BackupDataOutput { public class BackupDataOutput {
final long mQuota;
long mBackupWriter; long mBackupWriter;
/**
* Construct a BackupDataOutput purely for data-stream manipulation. This instance will
* not report usable quota information.
* @hide */
@SystemApi
public BackupDataOutput(FileDescriptor fd) {
this(fd, -1);
}
/** @hide */ /** @hide */
@SystemApi @SystemApi
public BackupDataOutput(FileDescriptor fd) { public BackupDataOutput(FileDescriptor fd, long quota) {
if (fd == null) throw new NullPointerException(); if (fd == null) throw new NullPointerException();
mQuota = quota;
mBackupWriter = ctor(fd); mBackupWriter = ctor(fd);
if (mBackupWriter == 0) { if (mBackupWriter == 0) {
throw new RuntimeException("Native initialization failed with fd=" + fd); throw new RuntimeException("Native initialization failed with fd=" + fd);
} }
} }
/**
* Returns the quota in bytes for the application's current backup operation. The
* value can vary for each operation.
*
* @see FullBackupDataOutput#getQuota()
*/
public long getQuota() {
return mQuota;
}
/** /**
* Mark the beginning of one record in the backup data stream. This must be called before * Mark the beginning of one record in the backup data stream. This must be called before
* {@link #writeEntityData}. * {@link #writeEntityData}.

View File

@@ -10,17 +10,35 @@ import android.os.ParcelFileDescriptor;
public class FullBackupDataOutput { public class FullBackupDataOutput {
// Currently a name-scoping shim around BackupDataOutput // Currently a name-scoping shim around BackupDataOutput
private final BackupDataOutput mData; private final BackupDataOutput mData;
private final long mQuota;
private long mSize; private long mSize;
/**
* Returns the quota in bytes for the application's current backup operation. The
* value can vary for each operation.
*
* @see BackupDataOutput#getQuota()
*/
public long getQuota() {
return mQuota;
}
/** @hide - used only in measure operation */ /** @hide - used only in measure operation */
public FullBackupDataOutput() { public FullBackupDataOutput(long quota) {
mData = null; mData = null;
mQuota = quota;
mSize = 0; mSize = 0;
} }
/** @hide */ /** @hide */
public FullBackupDataOutput(ParcelFileDescriptor fd, long quota) {
mData = new BackupDataOutput(fd.getFileDescriptor(), quota);
mQuota = quota;
}
/** @hide - used only internally to the backup manager service's stream construction */
public FullBackupDataOutput(ParcelFileDescriptor fd) { public FullBackupDataOutput(ParcelFileDescriptor fd) {
mData = new BackupDataOutput(fd.getFileDescriptor()); this(fd, -1);
} }
/** @hide */ /** @hide */