OSDN Git Service

API: make "what's the quota?" an operation on the backup data sinks
authorChristopher Tate <ctate@google.com>
Thu, 27 Apr 2017 00:07:27 +0000 (17:07 -0700)
committerChristopher Tate <ctate@google.com>
Thu, 27 Apr 2017 19:29:22 +0000 (12:29 -0700)
...instead of a method of the BackupAgent lifecycle component, callable
only while a backup is underway.

Bug 37565111
Test: unit

Change-Id: I4a56f93ee164ec499d5621f1641d932f091adf57

api/current.txt
api/system-current.txt
api/test-current.txt
core/java/android/app/backup/BackupAgent.java
core/java/android/app/backup/BackupDataOutput.java
core/java/android/app/backup/FullBackupDataOutput.java

index 862d541..820b48f 100644 (file)
@@ -6719,7 +6719,6 @@ package android.app.backup {
   public abstract class BackupAgent extends android.content.ContextWrapper {
     ctor public BackupAgent();
     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 void onCreate();
     method public void onDestroy();
@@ -6754,6 +6753,7 @@ package android.app.backup {
   }
 
   public class BackupDataOutput {
+    method public long getQuota();
     method public int writeEntityData(byte[], int) throws java.io.IOException;
     method public int writeEntityHeader(java.lang.String, int) throws java.io.IOException;
   }
@@ -6782,6 +6782,7 @@ package android.app.backup {
   }
 
   public class FullBackupDataOutput {
+    method public long getQuota();
   }
 
   public abstract class RestoreObserver {
index 89a2525..3418eb6 100644 (file)
@@ -6967,7 +6967,6 @@ package android.app.backup {
   public abstract class BackupAgent extends android.content.ContextWrapper {
     ctor public BackupAgent();
     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 void onCreate();
     method public void onDestroy();
@@ -7004,6 +7003,8 @@ package android.app.backup {
 
   public class BackupDataOutput {
     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 writeEntityHeader(java.lang.String, int) throws java.io.IOException;
   }
@@ -7180,6 +7181,7 @@ package android.app.backup {
   }
 
   public class FullBackupDataOutput {
+    method public long getQuota();
   }
 
   public class RestoreDescription implements android.os.Parcelable {
index 83128c8..15f91ba 100644 (file)
@@ -6749,7 +6749,6 @@ package android.app.backup {
   public abstract class BackupAgent extends android.content.ContextWrapper {
     ctor public BackupAgent();
     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 void onCreate();
     method public void onDestroy();
@@ -6784,6 +6783,7 @@ package android.app.backup {
   }
 
   public class BackupDataOutput {
+    method public long getQuota();
     method public int writeEntityData(byte[], int) throws java.io.IOException;
     method public int writeEntityHeader(java.lang.String, int) throws java.io.IOException;
   }
@@ -6812,6 +6812,7 @@ package android.app.backup {
   }
 
   public class FullBackupDataOutput {
+    method public long getQuota();
   }
 
   public abstract class RestoreObserver {
index 11636a5..5f92af9 100644 (file)
@@ -133,8 +133,6 @@ public abstract class BackupAgent extends ContextWrapper {
 
     Handler mHandler = null;
 
-    private long mBackupQuotaBytes = -1;
-
     Handler getHandler() {
         if (mHandler == null) {
             mHandler = new Handler(Looper.getMainLooper());
@@ -186,21 +184,6 @@ public abstract class BackupAgent extends ContextWrapper {
     }
 
     /**
-     * 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
      * last backup pass is provided in the <code>oldState</code> file
@@ -918,10 +901,8 @@ public abstract class BackupAgent extends ContextWrapper {
             // 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());
+            BackupDataOutput output = new BackupDataOutput(data.getFileDescriptor(), quotaBytes);
 
             try {
                 BackupAgent.this.onBackup(oldState, output, newState);
@@ -937,9 +918,6 @@ 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);
@@ -997,8 +975,6 @@ public abstract class BackupAgent extends ContextWrapper {
             // 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*
@@ -1006,7 +982,7 @@ public abstract class BackupAgent extends ContextWrapper {
             waitForSharedPrefs();
 
             try {
-                BackupAgent.this.onFullBackup(new FullBackupDataOutput(data));
+                BackupAgent.this.onFullBackup(new FullBackupDataOutput(data, quotaBytes));
             } catch (IOException ex) {
                 Log.d(TAG, "onFullBackup (" + BackupAgent.this.getClass().getName() + ") threw", ex);
                 throw new RuntimeException(ex);
@@ -1017,9 +993,6 @@ 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 {
@@ -1046,9 +1019,7 @@ public abstract class BackupAgent extends ContextWrapper {
         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;
+            FullBackupDataOutput measureOutput = new FullBackupDataOutput(quotaBytes);
 
             waitForSharedPrefs();
             try {
@@ -1060,8 +1031,6 @@ 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());
index 1fe63e7..c7586a2 100644 (file)
@@ -62,12 +62,23 @@ import java.io.IOException;
  * @see BackupAgent
  */
 public class BackupDataOutput {
+    final long mQuota;
     long mBackupWriter;
 
-    /** @hide */
+    /**
+     * 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 */
+    @SystemApi
+    public BackupDataOutput(FileDescriptor fd, long quota) {
         if (fd == null) throw new NullPointerException();
+        mQuota = quota;
         mBackupWriter = ctor(fd);
         if (mBackupWriter == 0) {
             throw new RuntimeException("Native initialization failed with fd=" + fd);
@@ -75,6 +86,16 @@ public class BackupDataOutput {
     }
 
     /**
+     * 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
      * {@link #writeEntityData}.
      * @param key A string key that uniquely identifies the data record within the application.
index 94704b9..5deedd0 100644 (file)
@@ -10,17 +10,35 @@ import android.os.ParcelFileDescriptor;
 public class FullBackupDataOutput {
     // Currently a name-scoping shim around BackupDataOutput
     private final BackupDataOutput mData;
+    private final long mQuota;
     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 */
-    public FullBackupDataOutput() {
+    public FullBackupDataOutput(long quota) {
         mData = null;
+        mQuota = quota;
         mSize = 0;
     }
 
     /** @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) {
-        mData = new BackupDataOutput(fd.getFileDescriptor());
+        this(fd, -1);
     }
 
     /** @hide */