Get the backup calling through to the file backup helper.

This includes some cleanup to make the parameters match
between BackupService.onBackup and FileBackupHelper.performBackup.
diff --git a/api/current.xml b/api/current.xml
index 623b986..56e2309 100644
--- a/api/current.xml
+++ b/api/current.xml
@@ -21839,219 +21839,6 @@
 </field>
 </class>
 </package>
-<package name="android.backup"
->
-<class name="BackupDataOutput"
- extends="java.lang.Object"
- abstract="false"
- static="false"
- final="false"
- deprecated="not deprecated"
- visibility="public"
->
-<constructor name="BackupDataOutput"
- type="android.backup.BackupDataOutput"
- static="false"
- final="false"
- deprecated="not deprecated"
- visibility="public"
->
-<parameter name="context" type="android.content.Context">
-</parameter>
-<parameter name="fd" type="java.io.FileDescriptor">
-</parameter>
-</constructor>
-<method name="close"
- return="void"
- abstract="false"
- native="false"
- synchronized="false"
- static="false"
- final="false"
- deprecated="not deprecated"
- visibility="public"
->
-</method>
-<method name="flush"
- return="void"
- abstract="false"
- native="true"
- synchronized="false"
- static="false"
- final="false"
- deprecated="not deprecated"
- visibility="public"
->
-</method>
-<method name="write"
- return="void"
- abstract="false"
- native="true"
- synchronized="false"
- static="false"
- final="false"
- deprecated="not deprecated"
- visibility="public"
->
-<parameter name="buffer" type="byte[]">
-</parameter>
-</method>
-<method name="write"
- return="void"
- abstract="false"
- native="true"
- synchronized="false"
- static="false"
- final="false"
- deprecated="not deprecated"
- visibility="public"
->
-<parameter name="oneByte" type="int">
-</parameter>
-</method>
-<method name="write"
- return="void"
- abstract="false"
- native="true"
- synchronized="false"
- static="false"
- final="false"
- deprecated="not deprecated"
- visibility="public"
->
-<parameter name="buffer" type="byte[]">
-</parameter>
-<parameter name="offset" type="int">
-</parameter>
-<parameter name="count" type="int">
-</parameter>
-</method>
-<method name="writeKey"
- return="void"
- abstract="false"
- native="true"
- synchronized="false"
- static="false"
- final="false"
- deprecated="not deprecated"
- visibility="public"
->
-<parameter name="key" type="java.lang.String">
-</parameter>
-</method>
-<method name="writeOperation"
- return="void"
- abstract="false"
- native="true"
- synchronized="false"
- static="false"
- final="false"
- deprecated="not deprecated"
- visibility="public"
->
-<parameter name="op" type="int">
-</parameter>
-</method>
-<field name="OP_DELETE"
- type="int"
- transient="false"
- volatile="false"
- value="2"
- static="true"
- final="true"
- deprecated="not deprecated"
- visibility="public"
->
-</field>
-<field name="OP_UPDATE"
- type="int"
- transient="false"
- volatile="false"
- value="1"
- static="true"
- final="true"
- deprecated="not deprecated"
- visibility="public"
->
-</field>
-</class>
-<class name="FileBackupHelper"
- extends="java.lang.Object"
- abstract="false"
- static="false"
- final="false"
- deprecated="not deprecated"
- visibility="public"
->
-<constructor name="FileBackupHelper"
- type="android.backup.FileBackupHelper"
- static="false"
- final="false"
- deprecated="not deprecated"
- visibility="public"
->
-</constructor>
-<method name="performBackup"
- return="void"
- abstract="false"
- native="false"
- synchronized="false"
- static="true"
- final="false"
- deprecated="not deprecated"
- visibility="public"
->
-<parameter name="context" type="android.content.Context">
-</parameter>
-<parameter name="oldSnapshot" type="android.os.ParcelFileDescriptor">
-</parameter>
-<parameter name="newSnapshot" type="android.os.ParcelFileDescriptor">
-</parameter>
-<parameter name="data" type="android.backup.BackupDataOutput">
-</parameter>
-<parameter name="files" type="java.lang.String[]">
-</parameter>
-</method>
-</class>
-<class name="SharedPreferencesBackupHelper"
- extends="java.lang.Object"
- abstract="false"
- static="false"
- final="false"
- deprecated="not deprecated"
- visibility="public"
->
-<constructor name="SharedPreferencesBackupHelper"
- type="android.backup.SharedPreferencesBackupHelper"
- static="false"
- final="false"
- deprecated="not deprecated"
- visibility="public"
->
-</constructor>
-<method name="performBackup"
- return="void"
- abstract="false"
- native="false"
- synchronized="false"
- static="true"
- final="false"
- deprecated="not deprecated"
- visibility="public"
->
-<parameter name="context" type="android.content.Context">
-</parameter>
-<parameter name="oldSnapshot" type="android.os.ParcelFileDescriptor">
-</parameter>
-<parameter name="newSnapshot" type="android.os.ParcelFileDescriptor">
-</parameter>
-<parameter name="data" type="android.backup.BackupDataOutput">
-</parameter>
-<parameter name="prefGroups" type="java.lang.String[]">
-</parameter>
-</method>
-</class>
-</package>
 <package name="android.content"
 >
 <class name="ActivityNotFoundException"
diff --git a/core/java/android/backup/BackupDataOutput.java b/core/java/android/backup/BackupDataOutput.java
index 6c47f7e..555494e 100644
--- a/core/java/android/backup/BackupDataOutput.java
+++ b/core/java/android/backup/BackupDataOutput.java
@@ -20,6 +20,7 @@
 
 import java.io.FileDescriptor;
 
+/** @hide */
 public class BackupDataOutput {
     /* package */ FileDescriptor fd;
 
diff --git a/core/java/android/backup/BackupService.java b/core/java/android/backup/BackupService.java
index 6ac703a..50a5921 100644
--- a/core/java/android/backup/BackupService.java
+++ b/core/java/android/backup/BackupService.java
@@ -75,9 +75,8 @@
      *                 file.  The application should record the final backup state
      *                 here after writing the requested data to dataFd.
      */
-    public abstract void onBackup(ParcelFileDescriptor oldState,
-            ParcelFileDescriptor data,
-            ParcelFileDescriptor newState);
+    public abstract void onBackup(ParcelFileDescriptor oldState, BackupDataOutput data,
+             ParcelFileDescriptor newState);
     
     /**
      * The application is being restored from backup, and should replace any
@@ -92,7 +91,7 @@
      *                 file.  The application should record the final backup state
      *                 here after restoring its data from dataFd.
      */
-    public abstract void onRestore(ParcelFileDescriptor data, ParcelFileDescriptor newState);
+    public abstract void onRestore(ParcelFileDescriptor /* TODO: BackupDataInput */ data, ParcelFileDescriptor newState);
 
 
     // ----- Core implementation -----
@@ -117,7 +116,15 @@
                 ParcelFileDescriptor newState) throws RemoteException {
             // !!! TODO - real implementation; for now just invoke the callbacks directly
             Log.v("BackupServiceBinder", "doBackup() invoked");
-            BackupService.this.onBackup(oldState, data, newState);
+            BackupDataOutput output = new BackupDataOutput(BackupService.this,
+                    data.getFileDescriptor());
+            try {
+                BackupService.this.onBackup(oldState, output, newState);
+            } catch (RuntimeException ex) {
+                Log.d("BackupService", "onBackup ("
+                        + BackupService.this.getClass().getName() + ") threw", ex);
+                throw ex;
+            }
         }
 
         public void doRestore(ParcelFileDescriptor data,
diff --git a/core/java/android/backup/FileBackupHelper.java b/core/java/android/backup/FileBackupHelper.java
index 3b2122c..2762f22 100644
--- a/core/java/android/backup/FileBackupHelper.java
+++ b/core/java/android/backup/FileBackupHelper.java
@@ -18,20 +18,24 @@
 
 import android.content.Context;
 import android.os.ParcelFileDescriptor;
+import android.util.Log;
 
 import java.io.FileDescriptor;
 
+/** @hide */
 public class FileBackupHelper {
+    private static final String TAG = "FileBackupHelper";
+
     /**
-     * Based on oldSnapshot, 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 newSnapshot with the
+     * 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 static void performBackup(Context context,
-            ParcelFileDescriptor oldSnapshot, ParcelFileDescriptor newSnapshot,
-            BackupDataOutput data, String[] files) {
+            ParcelFileDescriptor oldState, BackupDataOutput data,
+            ParcelFileDescriptor newState, String[] files) {
         String basePath = context.getFilesDir().getAbsolutePath();
-        performBackup_checked(basePath, oldSnapshot, newSnapshot, data, files);
+        performBackup_checked(basePath, oldState, data, newState, files);
     }
 
     /**
@@ -39,30 +43,34 @@
      * since it's easier to do that from java.
      */
     static void performBackup_checked(String basePath,
-            ParcelFileDescriptor oldSnapshot, ParcelFileDescriptor newSnapshot,
-            BackupDataOutput data, String[] files) {
-        if (newSnapshot == null) {
-            throw new NullPointerException("newSnapshot==null");
+            ParcelFileDescriptor oldState, BackupDataOutput data,
+            ParcelFileDescriptor newState, String[] files) {
+        if (files.length == 0) {
+            return;
         }
-        if (data == null) {
-            throw new NullPointerException("data==null");
+        if (basePath == null) {
+            throw new NullPointerException();
         }
+        // oldStateFd can be null
+        FileDescriptor oldStateFd = oldState != null ? oldState.getFileDescriptor() : null;
         if (data.fd == null) {
-            throw new NullPointerException("data.fd==null");
+            throw new NullPointerException();
+        }
+        FileDescriptor newStateFd = newState.getFileDescriptor();
+        if (newStateFd == null) {
+            throw new NullPointerException();
         }
         if (files == null) {
-            throw new NullPointerException("files==null");
+            throw new NullPointerException();
         }
 
-        int err = performBackup_native(basePath, oldSnapshot.getFileDescriptor(),
-                newSnapshot.getFileDescriptor(), data.fd, files);
+        int err = performBackup_native(basePath, oldStateFd, data.fd, newStateFd, files);
 
         if (err != 0) {
             throw new RuntimeException("Backup failed"); // TODO: more here
         }
     }
 
-    native private static int performBackup_native(String basePath,
-            FileDescriptor oldSnapshot, FileDescriptor newSnapshot,
-            FileDescriptor data, String[] files);
+    native private static int performBackup_native(String basePath, FileDescriptor oldState,
+            FileDescriptor data, FileDescriptor newState, String[] files);
 }
diff --git a/core/java/android/backup/SharedPreferencesBackupHelper.java b/core/java/android/backup/SharedPreferencesBackupHelper.java
index e839bb4..8627f08 100644
--- a/core/java/android/backup/SharedPreferencesBackupHelper.java
+++ b/core/java/android/backup/SharedPreferencesBackupHelper.java
@@ -21,6 +21,7 @@
 
 import java.io.FileDescriptor;
 
+/** @hide */
 public class SharedPreferencesBackupHelper {
     public static void performBackup(Context context,
             ParcelFileDescriptor oldSnapshot, ParcelFileDescriptor newSnapshot,
@@ -34,7 +35,7 @@
             files[i] = prefGroups[i] + ".xml";
         }
 
-        FileBackupHelper.performBackup_checked(basePath, oldSnapshot, newSnapshot, data, files);
+        FileBackupHelper.performBackup_checked(basePath, oldSnapshot, data, newSnapshot, files);
     }
 }
 
diff --git a/core/jni/android_backup_FileBackupHelper.cpp b/core/jni/android_backup_FileBackupHelper.cpp
index e8d60a0..c6de3a5 100644
--- a/core/jni/android_backup_FileBackupHelper.cpp
+++ b/core/jni/android_backup_FileBackupHelper.cpp
@@ -14,6 +14,9 @@
  * limitations under the License.
  */
 
+#define LOG_TAG "FileBackupHelper_native"
+#include <utils/Log.h>
+
 #include "JNIHelp.h"
 #include <android_runtime/AndroidRuntime.h>
 
@@ -22,29 +25,29 @@
 namespace android
 {
 
-static jfieldID s_descriptorField;
+static jfieldID s_descriptorField = 0;
 
 static int
-performBackup_native(JNIEnv* env, jstring basePath,
-            jobject oldSnapshot, jobject newSnapshot,
-            jobject data, jobjectArray files)
+performBackup_native(JNIEnv* env, jobject clazz, jstring basePath, jobject oldState, jobject data,
+        jobject newState, jobjectArray files)
 {
     int err;
 
     // all parameters have already been checked against null
-
-    int oldSnapshotFD = env->GetIntField(oldSnapshot, s_descriptorField);
-    int newSnapshotFD = env->GetIntField(newSnapshot, s_descriptorField);
+    LOGD("oldState=%p newState=%p data=%p\n", oldState, newState, data);
+    int oldStateFD = oldState != NULL ? env->GetIntField(oldState, s_descriptorField) : -1;
+    int newStateFD = env->GetIntField(newState, s_descriptorField);
     int dataFD = env->GetIntField(data, s_descriptorField);
 
     char const* basePathUTF = env->GetStringUTFChars(basePath, NULL);
+    LOGD("basePathUTF=\"%s\"\n", basePathUTF);
     const int fileCount = env->GetArrayLength(files);
     char const** filesUTF = (char const**)malloc(sizeof(char*)*fileCount);
     for (int i=0; i<fileCount; i++) {
         filesUTF[i] = env->GetStringUTFChars((jstring)env->GetObjectArrayElement(files, i), NULL);
     }
 
-    err = back_up_files(oldSnapshotFD, newSnapshotFD, dataFD, basePathUTF, filesUTF, fileCount);
+    err = back_up_files(oldStateFD, dataFD, newStateFD, basePathUTF, filesUTF, fileCount);
 
     for (int i=0; i<fileCount; i++) {
         env->ReleaseStringUTFChars((jstring)env->GetObjectArrayElement(files, i), filesUTF[i]);
@@ -64,6 +67,8 @@
 
 int register_android_backup_FileBackupHelper(JNIEnv* env)
 {
+    LOGD("register_android_backup_FileBackupHelper");
+
     jclass clazz;
 
     clazz = env->FindClass("java/io/FileDescriptor");
diff --git a/include/utils/backup_helpers.h b/include/utils/backup_helpers.h
index 61bee340..137c5f1 100644
--- a/include/utils/backup_helpers.h
+++ b/include/utils/backup_helpers.h
@@ -1,7 +1,7 @@
 #ifndef _UTILS_BACKUP_HELPERS_H
 #define _UTILS_BACKUP_HELPERS_H
 
-int back_up_files(int oldSnapshotFD, int newSnapshotFD, int oldDataStream,
+int back_up_files(int oldSnapshotFD, int oldDataStream, int newSnapshotFD,
         char const* fileBase, char const* const* files, int fileCount);
 
 #define TEST_BACKUP_HELPERS 0
diff --git a/libs/utils/file_backup_helper.cpp b/libs/utils/file_backup_helper.cpp
index 111f88d..453084a 100644
--- a/libs/utils/file_backup_helper.cpp
+++ b/libs/utils/file_backup_helper.cpp
@@ -24,6 +24,9 @@
 #define MAGIC0 0x70616e53 // Snap
 #define MAGIC1 0x656c6946 // File
 
+#define LOGP(x...) LOGD(x)
+//#define LOGP(x...) printf(x)
+
 struct SnapshotHeader {
     int magic0;
     int fileCount;
@@ -159,14 +162,14 @@
 static int
 write_delete_file(const String8& key)
 {
-    printf("write_delete_file %s\n", key.string());
+    LOGP("write_delete_file %s\n", key.string());
     return 0;
 }
 
 static int
 write_update_file(const String8& realFilename, const String8& key)
 {
-    printf("write_update_file %s (%s)\n", realFilename.string(), key.string());
+    LOGP("write_update_file %s (%s)\n", realFilename.string(), key.string());
     return 0;
 }
 
@@ -195,7 +198,7 @@
 }
 
 int
-back_up_files(int oldSnapshotFD, int newSnapshotFD, int oldDataStream,
+back_up_files(int oldSnapshotFD, int oldDataStream, int newSnapshotFD,
         char const* fileBase, char const* const* files, int fileCount)
 {
     int err;
@@ -260,10 +263,10 @@
             const FileState& f = oldSnapshot.valueAt(n);
             const FileState& g = newSnapshot.valueAt(m);
 
-            printf("%s\n", q.string());
-            printf("  new: modTime=%d,%d size=%-3d crc32=0x%08x\n",
+            LOGP("%s\n", q.string());
+            LOGP("  new: modTime=%d,%d size=%-3d crc32=0x%08x\n",
                     f.modTime_sec, f.modTime_nsec, f.size, f.crc32);
-            printf("  old: modTime=%d,%d size=%-3d crc32=0x%08x\n",
+            LOGP("  old: modTime=%d,%d size=%-3d crc32=0x%08x\n",
                     g.modTime_sec, g.modTime_nsec, g.size, g.crc32);
             if (f.modTime_sec != g.modTime_sec || f.modTime_nsec != g.modTime_nsec
                     || f.size != g.size || f.crc32 != g.crc32) {
diff --git a/services/java/com/android/server/BackupManagerService.java b/services/java/com/android/server/BackupManagerService.java
index db1deae..983329b 100644
--- a/services/java/com/android/server/BackupManagerService.java
+++ b/services/java/com/android/server/BackupManagerService.java
@@ -220,6 +220,9 @@
 
             BackupRequest request;
             synchronized (mQueueLock) {
+                if (mBackupQueue == null) {
+                    Log.d(TAG, "mBackupQueue is null.  WHY?");
+                }
                 request = mBackupQueue.get(0);
             }
 
diff --git a/tests/backup/src/com/android/backuptest/BackupTestActivity.java b/tests/backup/src/com/android/backuptest/BackupTestActivity.java
index de68cb7..af7dfd4 100644
--- a/tests/backup/src/com/android/backuptest/BackupTestActivity.java
+++ b/tests/backup/src/com/android/backuptest/BackupTestActivity.java
@@ -83,6 +83,27 @@
                 bm.dataChanged();
             }
         },
+        new Test("Clear File") {
+            void run() {
+                PrintStream output = null;
+                try {
+                    output = new PrintStream(openFileOutput(FILE_NAME, MODE_PRIVATE));
+                    output.close();
+                } catch (IOException ex) {
+                    if (output != null) {
+                        output.close();
+                    }
+                }
+                BackupManager bm = new BackupManager(BackupTestActivity.this);
+                bm.dataChanged();
+            }
+        },
+        new Test("Poke") {
+            void run() {
+                BackupManager bm = new BackupManager(BackupTestActivity.this);
+                bm.dataChanged();
+            }
+        },
         new Test("Show Shared Pref") {
             void run() {
                 SharedPreferences prefs = getSharedPreferences(PREF_GROUP_SETTINGS, MODE_PRIVATE);
diff --git a/tests/backup/src/com/android/backuptest/BackupTestService.java b/tests/backup/src/com/android/backuptest/BackupTestService.java
index c58c98b..00eb86e 100644
--- a/tests/backup/src/com/android/backuptest/BackupTestService.java
+++ b/tests/backup/src/com/android/backuptest/BackupTestService.java
@@ -17,6 +17,8 @@
 package com.android.backuptest;
 
 import android.backup.BackupService;
+import android.backup.BackupDataOutput;
+import android.backup.FileBackupHelper;
 import android.os.ParcelFileDescriptor;
 import android.util.Log;
 
@@ -25,10 +27,12 @@
     static final String TAG = "BackupTestService";
 
     @Override
-    public void onBackup(ParcelFileDescriptor oldState,
-            ParcelFileDescriptor data,
-            ParcelFileDescriptor newState) {
+    public void onBackup(ParcelFileDescriptor oldState, BackupDataOutput data,
+             ParcelFileDescriptor newState) {
         Log.d(TAG, "onBackup");
+        FileBackupHelper.performBackup(this, oldState, data, newState, new String[] {
+                    BackupTestActivity.FILE_NAME
+                });
     }
 
     @Override