1/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17
18package com.android.browser;
19
20import android.content.Context;
21import android.provider.Browser;
22import android.view.View;
23import android.widget.CompoundButton;
24import android.widget.CompoundButton.OnCheckedChangeListener;
25
26/**
27 *  Layout representing a history item in the classic history viewer.
28 */
29/* package */ class HistoryItem extends BookmarkItem
30        implements OnCheckedChangeListener {
31
32    private CompoundButton  mStar;      // Star for bookmarking
33    /**
34     *  Create a new HistoryItem.
35     *  @param context  Context for this HistoryItem.
36     */
37    /* package */ HistoryItem(Context context) {
38        this(context, true);
39    }
40
41    /* package */ HistoryItem(Context context, boolean showStar) {
42        super(context);
43
44        mStar = (CompoundButton) findViewById(R.id.star);
45        mStar.setOnCheckedChangeListener(this);
46        if (showStar) {
47            mStar.setVisibility(View.VISIBLE);
48        } else {
49            mStar.setVisibility(View.GONE);
50        }
51    }
52
53    /* package */ void copyTo(HistoryItem item) {
54        item.mTextView.setText(mTextView.getText());
55        item.mUrlText.setText(mUrlText.getText());
56        item.setIsBookmark(mStar.isChecked());
57        item.mImageView.setImageDrawable(mImageView.getDrawable());
58    }
59
60    /**
61     * Whether or not this item represents a bookmarked site
62     */
63    /* package */ boolean isBookmark() {
64        return mStar.isChecked();
65    }
66
67    /**
68     *  Set whether or not this represents a bookmark, and make sure the star
69     *  behaves appropriately.
70     */
71    /* package */ void setIsBookmark(boolean isBookmark) {
72        mStar.setOnCheckedChangeListener(null);
73        mStar.setChecked(isBookmark);
74        mStar.setOnCheckedChangeListener(this);
75    }
76
77    @Override
78    public void onCheckedChanged(CompoundButton buttonView,
79            boolean isChecked) {
80        if (isChecked) {
81            // Uncheck ourseves. When the bookmark is actually added,
82            // we will be notified
83            setIsBookmark(false);
84            Browser.saveBookmark(getContext(), getName(), mUrl);
85        } else {
86            Bookmarks.removeFromBookmarks(getContext(),
87                    getContext().getContentResolver(), mUrl, getName());
88        }
89    }
90}
91