Bug 1233048 - fix crash in AndroidImport.run(AndroidImport.java). r=liuche
authorSwaroop Rao <swaroop.rao@gmail.com>
Mon, 21 Dec 2015 18:17:00 +0100
changeset 317402 d43c0078a51d3d9159df1dede2fe683f6ae7ed02
parent 317401 50650c9b9c482fe2b365c18fcbc42649bae1c8e9
child 317403 860d77fbf406524dd7ad67066d92e051e9555fba
push id8691
push userbmo:vivekb.balakrishnan@gmail.com
push dateWed, 23 Dec 2015 21:07:27 +0000
reviewersliuche
bugs1233048
milestone46.0a1
Bug 1233048 - fix crash in AndroidImport.run(AndroidImport.java). r=liuche
mobile/android/base/java/org/mozilla/gecko/preferences/AndroidImport.java
--- a/mobile/android/base/java/org/mozilla/gecko/preferences/AndroidImport.java
+++ b/mobile/android/base/java/org/mozilla/gecko/preferences/AndroidImport.java
@@ -66,25 +66,19 @@ public class AndroidImport implements Ru
         mDB = new LocalBrowserDB(GeckoProfile.get(context).getName());
         mImportBookmarks = doBookmarks;
         mImportHistory = doHistory;
     }
 
     public void mergeBookmarks() {
         Cursor cursor = null;
         try {
-            cursor = mCr.query(LegacyBrowserProvider.BOOKMARKS_URI,
-                               null,
-                               LegacyBrowserProvider.BookmarkColumns.BOOKMARK + " = 1",
-                               null,
-                               null);
-
-            if (Build.MANUFACTURER.equals(SAMSUNG_MANUFACTURER) && cursor != null && cursor.getCount() == 0) {
-                cursor = mCr.query(SAMSUNG_BOOKMARKS_URI, null, null, null, null);
-            }
+            cursor = query(LegacyBrowserProvider.BOOKMARKS_URI,
+                               SAMSUNG_BOOKMARKS_URI,
+                               LegacyBrowserProvider.BookmarkColumns.BOOKMARK + " = 1");
 
             if (cursor != null) {
                 final int faviconCol = cursor.getColumnIndexOrThrow(LegacyBrowserProvider.BookmarkColumns.FAVICON);
                 final int titleCol = cursor.getColumnIndexOrThrow(LegacyBrowserProvider.BookmarkColumns.TITLE);
                 final int urlCol = cursor.getColumnIndexOrThrow(LegacyBrowserProvider.BookmarkColumns.URL);
                 // http://code.google.com/p/android/issues/detail?id=17969
                 final int createCol = cursor.getColumnIndex(LegacyBrowserProvider.BookmarkColumns.CREATED);
 
@@ -118,26 +112,20 @@ public class AndroidImport implements Ru
         }
 
         flushBatchOperations();
     }
 
     public void mergeHistory() {
         Cursor cursor = null;
         try {
-            cursor = mCr.query(LegacyBrowserProvider.BOOKMARKS_URI,
-                               null,
-                               LegacyBrowserProvider.BookmarkColumns.BOOKMARK + " = 0 AND " +
-                               LegacyBrowserProvider.BookmarkColumns.VISITS + " > 0",
-                               null,
-                               null);
-
-            if (Build.MANUFACTURER.equals(SAMSUNG_MANUFACTURER) && cursor.getCount() == 0) {
-                cursor = mCr.query(SAMSUNG_HISTORY_URI, null, null, null, null);
-            }
+            cursor = query (LegacyBrowserProvider.BOOKMARKS_URI,
+                                SAMSUNG_HISTORY_URI,
+                                LegacyBrowserProvider.BookmarkColumns.BOOKMARK + " = 0 AND " +
+                                LegacyBrowserProvider.BookmarkColumns.VISITS + " > 0");
 
             if (cursor != null) {
                 final int dateCol = cursor.getColumnIndexOrThrow(LegacyBrowserProvider.BookmarkColumns.DATE);
                 final int faviconCol = cursor.getColumnIndexOrThrow(LegacyBrowserProvider.BookmarkColumns.FAVICON);
                 final int titleCol = cursor.getColumnIndexOrThrow(LegacyBrowserProvider.BookmarkColumns.TITLE);
                 final int urlCol = cursor.getColumnIndexOrThrow(LegacyBrowserProvider.BookmarkColumns.URL);
                 final int visitsCol = cursor.getColumnIndexOrThrow(LegacyBrowserProvider.BookmarkColumns.VISITS);
 
@@ -158,16 +146,26 @@ public class AndroidImport implements Ru
         } finally {
             if (cursor != null)
                 cursor.close();
         }
 
         flushBatchOperations();
     }
 
+    protected Cursor query (Uri mainUri, Uri fallbackUri, String condition) {
+        Cursor cursor = mCr.query(mainUri, null, condition, null, null);
+
+        if (Build.MANUFACTURER.equals(SAMSUNG_MANUFACTURER) && (cursor == null || cursor.getCount() == 0)) {
+            cursor = mCr.query(fallbackUri, null, null, null, null);
+        }
+
+        return cursor;
+    }
+
     protected void flushBatchOperations() {
         Log.d(LOGTAG, "Flushing " + mOperations.size() + " DB operations");
         try {
             // We don't really care for the results, this is best-effort.
             mCr.applyBatch(BrowserContract.AUTHORITY, mOperations);
         } catch (RemoteException e) {
             Log.e(LOGTAG, "Remote exception while updating db: ", e);
         } catch (OperationApplicationException e) {