Updates bookmark backup to use BP2

 Bug: 3094458
 This updates bookmark backup/restore to use BrowserProvider2, and
 also changes it so that it does *not* backup bookmarks anymore.
 It is now restore-only to essentially import bookmarks from older
 devices. Chrome sync is intended to take over backup duties.

Change-Id: Ie1e9eac22be1fc5db982436928fe37f64606c605
diff --git a/src/com/android/browser/BrowserBackupAgent.java b/src/com/android/browser/BrowserBackupAgent.java
index 9c5d65b..2950474 100644
--- a/src/com/android/browser/BrowserBackupAgent.java
+++ b/src/com/android/browser/BrowserBackupAgent.java
@@ -16,24 +16,23 @@
 
 package com.android.browser;
 
-import java.io.IOException;
-
 import android.app.backup.BackupAgent;
 import android.app.backup.BackupDataInput;
 import android.app.backup.BackupDataOutput;
+import android.content.ContentValues;
 import android.database.Cursor;
 import android.os.ParcelFileDescriptor;
-import android.provider.Browser;
-import android.provider.Browser.BookmarkColumns;
+import android.provider.BrowserContract;
+import android.provider.BrowserContract.Bookmarks;
 import android.util.Log;
 
-import java.io.ByteArrayOutputStream;
 import java.io.DataInputStream;
 import java.io.DataOutputStream;
 import java.io.EOFException;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
+import java.io.IOException;
 import java.util.ArrayList;
 import java.util.zip.CRC32;
 
@@ -54,19 +53,8 @@
     static final int BACKUP_AGENT_VERSION = 0;
 
     /**
-     * In order to determine whether the bookmark set has changed since the
-     * last time we did a backup, we store the following bits of info in the
-     * state file after a backup:
-     *
-     * 1. the size of the flattened bookmark file
-     * 2. the CRC32 of that file
-     * 3. the agent version number [relevant following an OTA]
-     *
-     * After we flatten the bookmarks file here in onBackup, we compare its
-     * metrics with the values from the saved state.  If they match, it means
-     * the bookmarks didn't really change and we don't need to send the data.
-     * (If they don't match, of course, then they've changed and we do indeed
-     * send the new flattened file to be backed up.)
+     * This simply preserves the existing state as we now prefer Chrome Sync
+     * to handle bookmark backup.
      */
     @Override
     public void onBackup(ParcelFileDescriptor oldState, BackupDataOutput data,
@@ -84,33 +72,14 @@
             savedVersion = in.readInt();
         } catch (EOFException e) {
             // It means we had no previous state; that's fine
+            return;
         } finally {
             if (in != null) {
                 in.close();
             }
         }
-
-        // Build a flattened representation of the bookmarks table
-        File tmpfile = File.createTempFile("bkp", null, getCacheDir());
-        try {
-            FileOutputStream outfstream = new FileOutputStream(tmpfile);
-            long newCrc = buildBookmarkFile(outfstream);
-            outfstream.close();
-
-            // Any changes since the last backup?
-            if ((savedVersion != BACKUP_AGENT_VERSION)
-                    || (newCrc != savedCrc)
-                    || (tmpfile.length() != savedFileSize)) {
-                // Different checksum or different size, so we need to back it up
-                copyFileToBackup(BOOKMARK_KEY, tmpfile, data);
-            }
-
-            // Record our backup state and we're done
-            writeBackupState(tmpfile.length(), newCrc, newState);
-        } finally {
-            // Make sure to tidy up when we're done
-            tmpfile.delete();
-        }
+        // Write the existing state
+        writeBackupState(savedFileSize, savedCrc, newState);
     }
 
     /**
@@ -153,24 +122,19 @@
                         int N = bookmarks.size();
                         int nUnique = 0;
                         if (DEBUG) Log.v(TAG, "Restoring " + N + " bookmarks");
-                        String[] urlCol = new String[] { BookmarkColumns.URL };
+                        String[] urlCol = new String[] { Bookmarks.URL };
                         for (int i = 0; i < N; i++) {
                             Bookmark mark = bookmarks.get(i);
 
                             // Does this URL exist in the bookmark table?
-                            Cursor cursor = getContentResolver().query(Browser.BOOKMARKS_URI,
-                                    urlCol,  BookmarkColumns.URL + " == '" + mark.url + "' AND " +
-                                    BookmarkColumns.BOOKMARK + " == 1 ", null, null);
+                            Cursor cursor = getContentResolver().query(
+                                    Bookmarks.CONTENT_URI, urlCol,
+                                    Bookmarks.URL + " == ?",
+                                    new String[] { mark.url }, null);
                             // if not, insert it
                             if (cursor.getCount() <= 0) {
                                 if (DEBUG) Log.v(TAG, "Did not see url: " + mark.url);
-                                // Right now we do not reconstruct the db entry in its
-                                // entirety; we just add a new bookmark with the same data
-                                // FIXME: This file needs to be reworked
-                                // anyway For now, add the bookmark at
-                                // the root level.
-                                Bookmarks.addBookmark(this, false,
-                                        mark.url, mark.title, null, false, 0);
+                                addBookmark(mark);
                                 nUnique++;
                             } else {
                                 if (DEBUG) Log.v(TAG, "Skipping extant url: " + mark.url);
@@ -198,6 +162,16 @@
         }
     }
 
+    void addBookmark(Bookmark mark) {
+        ContentValues values = new ContentValues();
+        values.put(Bookmarks.TITLE, mark.title);
+        values.put(Bookmarks.URL, mark.url);
+        values.put(Bookmarks.IS_FOLDER, 0);
+        values.put(Bookmarks.DATE_CREATED, mark.created);
+        values.put(Bookmarks.DATE_MODIFIED, mark.date);
+        getContentResolver().insert(Bookmarks.CONTENT_URI, values);
+    }
+
     static class Bookmark {
         public String url;
         public int visits;
@@ -209,80 +183,6 @@
      * Utility functions
      */
 
-    // Flatten the bookmarks table into the given file, calculating its CRC in the process
-    private long buildBookmarkFile(FileOutputStream outfstream) throws IOException {
-        CRC32 crc = new CRC32();
-        ByteArrayOutputStream bufstream = new ByteArrayOutputStream(512);
-        DataOutputStream bout = new DataOutputStream(bufstream);
-
-        Cursor cursor = getContentResolver().query(Browser.BOOKMARKS_URI,
-                new String[] { BookmarkColumns.URL, BookmarkColumns.VISITS,
-                BookmarkColumns.DATE, BookmarkColumns.CREATED,
-                BookmarkColumns.TITLE },
-                BookmarkColumns.BOOKMARK + " == 1 ", null, null);
-
-        // The first thing in the file is the row count...
-        int count = cursor.getCount();
-        if (DEBUG) Log.v(TAG, "Backing up " + count + " bookmarks");
-        bout.writeInt(count);
-        byte[] record = bufstream.toByteArray();
-        crc.update(record);
-        outfstream.write(record);
-
-        // ... followed by the data for each row
-        for (int i = 0; i < count; i++) {
-            cursor.moveToNext();
-
-            String url = cursor.getString(0);
-            int visits = cursor.getInt(1);
-            long date = cursor.getLong(2);
-            long created = cursor.getLong(3);
-            String title = cursor.getString(4);
-
-            // construct the flattened record in a byte array
-            bufstream.reset();
-            bout.writeUTF(url);
-            bout.writeInt(visits);
-            bout.writeLong(date);
-            bout.writeLong(created);
-            bout.writeUTF(title);
-
-            // Update the CRC and write the record to the temp file
-            record = bufstream.toByteArray();
-            crc.update(record);
-            outfstream.write(record);
-
-            if (DEBUG) Log.v(TAG, "   wrote url " + url);
-        }
-
-        cursor.close();
-        return crc.getValue();
-    }
-
-    // Write the file to backup as a single record under the given key
-    private void copyFileToBackup(String key, File file, BackupDataOutput data)
-            throws IOException {
-        final int CHUNK = 8192;
-        byte[] buf = new byte[CHUNK];
-
-        int toCopy = (int) file.length();
-        data.writeEntityHeader(key, toCopy);
-
-        FileInputStream in = new FileInputStream(file);
-        try {
-            int nRead;
-            while (toCopy > 0) {
-                nRead = in.read(buf, 0, CHUNK);
-                data.writeEntityData(buf, nRead);
-                toCopy -= nRead;
-            }
-        } finally {
-            if (in != null) {
-                in.close();
-            }
-        }
-    }
-
     // Read the given file from backup to a file, calculating a CRC32 along the way
     private long copyBackupToFile(BackupDataInput data, File file, int toRead)
             throws IOException {