From 0c90888c75eed12f6e2e14a9044faf50bd4af8ed Mon Sep 17 00:00:00 2001 From: The Android Open Source Project Date: Tue, 3 Mar 2009 19:32:16 -0800 Subject: auto import from //depot/cupcake/@135843 --- src/com/android/browser/HistoryItem.java | 127 +++++++++++++++++++++++++++++++ 1 file changed, 127 insertions(+) create mode 100644 src/com/android/browser/HistoryItem.java (limited to 'src/com/android/browser/HistoryItem.java') diff --git a/src/com/android/browser/HistoryItem.java b/src/com/android/browser/HistoryItem.java new file mode 100644 index 000000000..55e43f03f --- /dev/null +++ b/src/com/android/browser/HistoryItem.java @@ -0,0 +1,127 @@ +/* + * Copyright (C) 2008 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +package com.android.browser; + +import android.content.ContentResolver; +import android.content.ContentUris; +import android.content.ContentValues; +import android.content.Context; +import android.database.Cursor; +import android.graphics.Bitmap; +import android.net.Uri; +import android.provider.Browser; +import android.util.Log; +import android.view.View; +import android.webkit.WebIconDatabase; +import android.widget.CompoundButton; +import android.widget.ImageView; +import android.widget.TextView; +import android.widget.Toast; + +import java.util.Date; + +/** + * Layout representing a history item in the classic history viewer. + */ +/* package */ class HistoryItem extends BookmarkItem { + + private CompoundButton mStar; // Star for bookmarking + private CompoundButton.OnCheckedChangeListener mListener; + /** + * Create a new HistoryItem. + * @param context Context for this HistoryItem. + */ + /* package */ HistoryItem(Context context) { + 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(); + } + }; + } + + void copyTo(HistoryItem item) { + item.mTextView.setText(mTextView.getText()); + item.mUrlText.setText(mUrlText.getText()); + item.setIsBookmark(mStar.isChecked()); + item.mImageView.setImageDrawable(mImageView.getDrawable()); + } + + /** + * Set whether or not this represents a bookmark, and make sure the star + * behaves appropriately. + */ + void setIsBookmark(boolean isBookmark) { + mStar.setOnCheckedChangeListener(null); + mStar.setChecked(isBookmark); + mStar.setOnCheckedChangeListener(mListener); + } +} -- cgit v1.2.3