FileDocCategorySizeDatePackage
HistoryItem.javaAPI DocAndroid 1.5 API5142Wed May 06 22:42:42 BST 2009com.android.browser

HistoryItem

public class HistoryItem extends BookmarkItem
Layout representing a history item in the classic history viewer.

Fields Summary
private android.widget.CompoundButton
mStar
private CompoundButton.OnCheckedChangeListener
mListener
Constructors Summary
HistoryItem(android.content.Context context)
Create a new HistoryItem.

param
context Context for this 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();
            }
        };
    
Methods Summary
voidcopyTo(com.android.browser.HistoryItem item)

        item.mTextView.setText(mTextView.getText());
        item.mUrlText.setText(mUrlText.getText());
        item.setIsBookmark(mStar.isChecked());
        item.mImageView.setImageDrawable(mImageView.getDrawable());
    
voidsetIsBookmark(boolean isBookmark)
Set whether or not this represents a bookmark, and make sure the star behaves appropriately.

        mStar.setOnCheckedChangeListener(null);
        mStar.setChecked(isBookmark);
        mStar.setOnCheckedChangeListener(mListener);