mobile/android/base/preferences/AndroidImport.java

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

michael@0 1 /* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*-
michael@0 2 * This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 package org.mozilla.gecko.preferences;
michael@0 7
michael@0 8 import org.mozilla.gecko.GeckoProfile;
michael@0 9 import org.mozilla.gecko.db.BrowserContract;
michael@0 10 import org.mozilla.gecko.db.BrowserContract.Bookmarks;
michael@0 11 import org.mozilla.gecko.db.LocalBrowserDB;
michael@0 12
michael@0 13 import android.content.ContentProviderOperation;
michael@0 14 import android.content.ContentResolver;
michael@0 15 import android.content.Context;
michael@0 16 import android.content.OperationApplicationException;
michael@0 17 import android.database.Cursor;
michael@0 18 import android.os.RemoteException;
michael@0 19 import android.provider.Browser;
michael@0 20 import android.util.Log;
michael@0 21
michael@0 22 import java.util.ArrayList;
michael@0 23
michael@0 24 class AndroidImport implements Runnable {
michael@0 25 static final private String LOGTAG = "AndroidImport";
michael@0 26 private Context mContext;
michael@0 27 private Runnable mOnDoneRunnable;
michael@0 28 private ArrayList<ContentProviderOperation> mOperations;
michael@0 29 private ContentResolver mCr;
michael@0 30 private LocalBrowserDB mDB;
michael@0 31 private boolean mImportBookmarks;
michael@0 32 private boolean mImportHistory;
michael@0 33
michael@0 34 public AndroidImport(Context context, Runnable onDoneRunnable,
michael@0 35 boolean doBookmarks, boolean doHistory) {
michael@0 36 mContext = context;
michael@0 37 mOnDoneRunnable = onDoneRunnable;
michael@0 38 mOperations = new ArrayList<ContentProviderOperation>();
michael@0 39 mCr = mContext.getContentResolver();
michael@0 40 mDB = new LocalBrowserDB(GeckoProfile.get(context).getName());
michael@0 41 mImportBookmarks = doBookmarks;
michael@0 42 mImportHistory = doHistory;
michael@0 43 }
michael@0 44
michael@0 45 public void mergeBookmarks() {
michael@0 46 Cursor cursor = null;
michael@0 47 try {
michael@0 48 cursor = mCr.query(Browser.BOOKMARKS_URI,
michael@0 49 null,
michael@0 50 Browser.BookmarkColumns.BOOKMARK + " = 1",
michael@0 51 null,
michael@0 52 null);
michael@0 53
michael@0 54 if (cursor != null) {
michael@0 55 final int faviconCol = cursor.getColumnIndexOrThrow(Browser.BookmarkColumns.FAVICON);
michael@0 56 final int titleCol = cursor.getColumnIndexOrThrow(Browser.BookmarkColumns.TITLE);
michael@0 57 final int urlCol = cursor.getColumnIndexOrThrow(Browser.BookmarkColumns.URL);
michael@0 58 // http://code.google.com/p/android/issues/detail?id=17969
michael@0 59 final int createCol = cursor.getColumnIndex(Browser.BookmarkColumns.CREATED);
michael@0 60
michael@0 61 cursor.moveToFirst();
michael@0 62 while (!cursor.isAfterLast()) {
michael@0 63 String url = cursor.getString(urlCol);
michael@0 64 String title = cursor.getString(titleCol);
michael@0 65 long created;
michael@0 66 if (createCol >= 0) {
michael@0 67 created = cursor.getLong(createCol);
michael@0 68 } else {
michael@0 69 created = System.currentTimeMillis();
michael@0 70 }
michael@0 71 // Need to set it to the current time so Sync picks it up.
michael@0 72 long modified = System.currentTimeMillis();
michael@0 73 byte[] data = cursor.getBlob(faviconCol);
michael@0 74 mDB.updateBookmarkInBatch(mCr, mOperations,
michael@0 75 url, title, null, -1,
michael@0 76 created, modified,
michael@0 77 BrowserContract.Bookmarks.DEFAULT_POSITION,
michael@0 78 null, Bookmarks.TYPE_BOOKMARK);
michael@0 79 if (data != null) {
michael@0 80 mDB.updateFaviconInBatch(mCr, mOperations, url, null, null, data);
michael@0 81 }
michael@0 82 cursor.moveToNext();
michael@0 83 }
michael@0 84 }
michael@0 85 } finally {
michael@0 86 if (cursor != null)
michael@0 87 cursor.close();
michael@0 88 }
michael@0 89
michael@0 90 flushBatchOperations();
michael@0 91 }
michael@0 92
michael@0 93 public void mergeHistory() {
michael@0 94 Cursor cursor = null;
michael@0 95 try {
michael@0 96 cursor = mCr.query(Browser.BOOKMARKS_URI,
michael@0 97 null,
michael@0 98 Browser.BookmarkColumns.BOOKMARK + " = 0 AND " +
michael@0 99 Browser.BookmarkColumns.VISITS + " > 0",
michael@0 100 null,
michael@0 101 null);
michael@0 102
michael@0 103 if (cursor != null) {
michael@0 104 final int dateCol = cursor.getColumnIndexOrThrow(Browser.BookmarkColumns.DATE);
michael@0 105 final int faviconCol = cursor.getColumnIndexOrThrow(Browser.BookmarkColumns.FAVICON);
michael@0 106 final int titleCol = cursor.getColumnIndexOrThrow(Browser.BookmarkColumns.TITLE);
michael@0 107 final int urlCol = cursor.getColumnIndexOrThrow(Browser.BookmarkColumns.URL);
michael@0 108 final int visitsCol = cursor.getColumnIndexOrThrow(Browser.BookmarkColumns.VISITS);
michael@0 109
michael@0 110 cursor.moveToFirst();
michael@0 111 while (!cursor.isAfterLast()) {
michael@0 112 String url = cursor.getString(urlCol);
michael@0 113 String title = cursor.getString(titleCol);
michael@0 114 long date = cursor.getLong(dateCol);
michael@0 115 int visits = cursor.getInt(visitsCol);
michael@0 116 byte[] data = cursor.getBlob(faviconCol);
michael@0 117 mDB.updateHistoryInBatch(mCr, mOperations, url, title, date, visits);
michael@0 118 if (data != null) {
michael@0 119 mDB.updateFaviconInBatch(mCr, mOperations, url, null, null, data);
michael@0 120 }
michael@0 121 cursor.moveToNext();
michael@0 122 }
michael@0 123 }
michael@0 124 } finally {
michael@0 125 if (cursor != null)
michael@0 126 cursor.close();
michael@0 127 }
michael@0 128
michael@0 129 flushBatchOperations();
michael@0 130 }
michael@0 131
michael@0 132 protected void flushBatchOperations() {
michael@0 133 Log.d(LOGTAG, "Flushing " + mOperations.size() + " DB operations");
michael@0 134 try {
michael@0 135 // We don't really care for the results, this is best-effort.
michael@0 136 mCr.applyBatch(BrowserContract.AUTHORITY, mOperations);
michael@0 137 } catch (RemoteException e) {
michael@0 138 Log.e(LOGTAG, "Remote exception while updating db: ", e);
michael@0 139 } catch (OperationApplicationException e) {
michael@0 140 // Bug 716729 means this happens even in normal circumstances
michael@0 141 Log.d(LOGTAG, "Error while applying database updates: ", e);
michael@0 142 }
michael@0 143 mOperations.clear();
michael@0 144 }
michael@0 145
michael@0 146 @Override
michael@0 147 public void run() {
michael@0 148 if (mImportBookmarks) {
michael@0 149 mergeBookmarks();
michael@0 150 }
michael@0 151 if (mImportHistory) {
michael@0 152 mergeHistory();
michael@0 153 }
michael@0 154
michael@0 155 mOnDoneRunnable.run();
michael@0 156 }
michael@0 157 }

mercurial