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/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");