From c4c578f7eeb2f39c61afedf68ff0aca508724523 Mon Sep 17 00:00:00 2001 From: Sean Owen Date: Wed, 22 Jun 2016 16:52:04 +0100 Subject: [PATCH] Preload bookmarks instead of holding open cursor to avoid concurrent modification; no longer that slow that it must be avoided on the UI thread --- .../client/android/share/BookmarkAdapter.java | 26 +++++------- .../android/share/BookmarkPickerActivity.java | 41 +++++++++---------- 2 files changed, 30 insertions(+), 37 deletions(-) diff --git a/android/src/com/google/zxing/client/android/share/BookmarkAdapter.java b/android/src/com/google/zxing/client/android/share/BookmarkAdapter.java index bd8eda969d..38dbe34fbc 100644 --- a/android/src/com/google/zxing/client/android/share/BookmarkAdapter.java +++ b/android/src/com/google/zxing/client/android/share/BookmarkAdapter.java @@ -16,10 +16,11 @@ package com.google.zxing.client.android.share; +import java.util.List; + import com.google.zxing.client.android.R; import android.content.Context; -import android.database.Cursor; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -36,23 +37,23 @@ * @author dswitkin@google.com (Daniel Switkin) */ final class BookmarkAdapter extends BaseAdapter { + private final Context context; - private final Cursor cursor; + private final List titleURLs; - BookmarkAdapter(Context context, Cursor cursor) { + BookmarkAdapter(Context context, List titleURLs) { this.context = context; - this.cursor = cursor; + this.titleURLs = titleURLs; } @Override public int getCount() { - return cursor.isClosed() ? 0 : cursor.getCount(); + return titleURLs.size(); } @Override public Object getItem(int index) { - // Not used, so no point in retrieving it. - return null; + return titleURLs.get(index); } @Override @@ -69,14 +70,9 @@ public View getView(int index, View view, ViewGroup viewGroup) { LayoutInflater factory = LayoutInflater.from(context); layout = factory.inflate(R.layout.bookmark_picker_list_item, viewGroup, false); } - - if (!cursor.isClosed()) { - cursor.moveToPosition(index); - CharSequence title = cursor.getString(BookmarkPickerActivity.TITLE_COLUMN); - ((TextView) layout.findViewById(R.id.bookmark_title)).setText(title); - CharSequence url = cursor.getString(BookmarkPickerActivity.URL_COLUMN); - ((TextView) layout.findViewById(R.id.bookmark_url)).setText(url); - } // Otherwise... just don't update as the object is shutting down + String[] titleURL = titleURLs.get(index); + ((TextView) layout.findViewById(R.id.bookmark_title)).setText(titleURL[0]); + ((TextView) layout.findViewById(R.id.bookmark_url)).setText(titleURL[1]); return layout; } } diff --git a/android/src/com/google/zxing/client/android/share/BookmarkPickerActivity.java b/android/src/com/google/zxing/client/android/share/BookmarkPickerActivity.java index c4cc1f30e2..ddc98901b3 100644 --- a/android/src/com/google/zxing/client/android/share/BookmarkPickerActivity.java +++ b/android/src/com/google/zxing/client/android/share/BookmarkPickerActivity.java @@ -16,6 +16,9 @@ package com.google.zxing.client.android.share; +import java.util.ArrayList; +import java.util.List; + import android.app.ListActivity; import android.content.Intent; import android.database.Cursor; @@ -41,46 +44,40 @@ public final class BookmarkPickerActivity extends ListActivity { // Copied from android.provider.Browser.BOOKMARKS_URI: private static final Uri BOOKMARKS_URI = Uri.parse("content://browser/bookmarks"); - static final int TITLE_COLUMN = 0; - static final int URL_COLUMN = 1; - private static final String BOOKMARK_SELECTION = "bookmark = 1 AND url IS NOT NULL"; - private Cursor cursor; + private final List titleURLs = new ArrayList<>(); @Override protected void onResume() { super.onResume(); - cursor = getContentResolver().query(BOOKMARKS_URI, BOOKMARK_PROJECTION, + titleURLs.clear(); + Cursor cursor = getContentResolver().query(BOOKMARKS_URI, BOOKMARK_PROJECTION, BOOKMARK_SELECTION, null, null); if (cursor == null) { Log.w(TAG, "No cursor returned for bookmark query"); finish(); return; } - setListAdapter(new BookmarkAdapter(this, cursor)); - } - - @Override - protected void onPause() { - if (cursor != null) { + try { + while (cursor.moveToNext()) { + titleURLs.add(new String[] { cursor.getString(0), cursor.getString(1) }); + } + } finally { cursor.close(); - cursor = null; } - super.onPause(); + setListAdapter(new BookmarkAdapter(this, titleURLs)); } + @Override protected void onListItemClick(ListView l, View view, int position, long id) { - if (!cursor.isClosed() && cursor.moveToPosition(position)) { - Intent intent = new Intent(); - intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET); - intent.putExtra("title", cursor.getString(TITLE_COLUMN)); // Browser.BookmarkColumns.TITLE - intent.putExtra("url", cursor.getString(URL_COLUMN)); // Browser.BookmarkColumns.URL - setResult(RESULT_OK, intent); - } else { - setResult(RESULT_CANCELED); - } + String[] titleURL = titleURLs.get(position); + Intent intent = new Intent(); + intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET); + intent.putExtra("title", titleURL[0]); // Browser.BookmarkColumns.TITLE + intent.putExtra("url", titleURL[1]); // Browser.BookmarkColumns.URL + setResult(RESULT_OK, intent); finish(); } }