Preload bookmarks instead of holding open cursor to avoid concurrent modification; no longer that slow that it must be avoided on the UI thread

This commit is contained in:
Sean Owen 2016-06-22 16:52:04 +01:00
parent 53837950aa
commit c4c578f7ee
2 changed files with 31 additions and 38 deletions

View file

@ -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 @@ import android.widget.TextView;
* @author dswitkin@google.com (Daniel Switkin)
*/
final class BookmarkAdapter extends BaseAdapter {
private final Context context;
private final Cursor cursor;
BookmarkAdapter(Context context, Cursor cursor) {
private final Context context;
private final List<String[]> titleURLs;
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
@ -69,14 +70,9 @@ final class BookmarkAdapter extends BaseAdapter {
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;
}
}

View file

@ -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<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));
try {
while (cursor.moveToNext()) {
titleURLs.add(new String[] { cursor.getString(0), cursor.getString(1) });
}
} finally {
cursor.close();
}
setListAdapter(new BookmarkAdapter(this, titleURLs));
}
@Override
protected void onPause() {
if (cursor != null) {
cursor.close();
cursor = null;
}
super.onPause();
}
@Override
protected void onListItemClick(ListView l, View view, int position, long id) {
if (!cursor.isClosed() && cursor.moveToPosition(position)) {
String[] titleURL = titleURLs.get(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
intent.putExtra("title", titleURL[0]); // Browser.BookmarkColumns.TITLE
intent.putExtra("url", titleURL[1]); // Browser.BookmarkColumns.URL
setResult(RESULT_OK, intent);
} else {
setResult(RESULT_CANCELED);
}
finish();
}
}