Create a new HistoryItem.
super(context);
mStar = (CompoundButton) findViewById(R.id.star);
mStar.setVisibility(View.VISIBLE);
mListener = new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
ContentResolver cr = mContext.getContentResolver();
Cursor cursor = cr.query(
Browser.BOOKMARKS_URI,
Browser.HISTORY_PROJECTION,
"url = ?",
new String[] { mUrl },
null);
boolean first = cursor.moveToFirst();
// Should be in the database no matter what
if (!first) {
throw new AssertionError("URL is not in the database!");
}
if (isChecked) {
// Add to bookmarks
// FIXME: Share code with AddBookmarkPage.java
ContentValues map = new ContentValues();
map.put(Browser.BookmarkColumns.CREATED,
new Date().getTime());
map.put(Browser.BookmarkColumns.TITLE, getName());
map.put(Browser.BookmarkColumns.BOOKMARK, 1);
try {
cr.update(Browser.BOOKMARKS_URI, map,
"_id = " + cursor.getInt(0), null);
} catch (IllegalStateException e) {
Log.e("HistoryItem", "no database!");
}
WebIconDatabase.getInstance().retainIconForPageUrl(mUrl);
// catch IllegalStateException?
Toast.makeText(mContext, R.string.added_to_bookmarks,
Toast.LENGTH_LONG).show();
} else {
// Remove from bookmarks
// FIXME: This code should be shared with
// BrowserBookmarksAdapter.java
WebIconDatabase.getInstance().releaseIconForPageUrl(mUrl);
Uri uri = ContentUris.withAppendedId(Browser.BOOKMARKS_URI,
cursor.getInt(Browser.HISTORY_PROJECTION_ID_INDEX));
// It is no longer a bookmark, but it is still a visited
// site.
ContentValues values = new ContentValues();
values.put(Browser.BookmarkColumns.BOOKMARK, 0);
try {
cr.update(uri, values, null, null);
} catch (IllegalStateException e) {
Log.e("HistoryItem", "no database!");
}
Toast.makeText(mContext, R.string.removed_from_bookmarks,
Toast.LENGTH_LONG).show();
}
cursor.deactivate();
}
};