Skip to content

Commit

Permalink
Preload bookmarks instead of holding open cursor to avoid concurrent …
Browse files Browse the repository at this point in the history
…modification; no longer that slow that it must be avoided on the UI thread
  • Loading branch information
srowen committed Jun 22, 2016
1 parent 5383795 commit c4c578f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -36,23 +37,23 @@
* @author [email protected] (Daniel Switkin)
*/
final class BookmarkAdapter extends BaseAdapter {

private final Context context;
private final Cursor cursor;
private final List<String[]> titleURLs;

BookmarkAdapter(Context context, Cursor cursor) {
BookmarkAdapter(Context context, List<String[]> 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
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<String[]> 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();
}
}

0 comments on commit c4c578f

Please sign in to comment.