10c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project/*
20c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project * Copyright (C) 2008 The Android Open Source Project
30c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project *
40c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License");
50c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project * you may not use this file except in compliance with the License.
60c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project * You may obtain a copy of the License at
70c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project *
80c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project *      http://www.apache.org/licenses/LICENSE-2.0
90c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project *
100c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project * Unless required by applicable law or agreed to in writing, software
110c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS,
120c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
130c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project * See the License for the specific language governing permissions and
140c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project * limitations under the License.
150c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project */
160c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
170c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectpackage com.android.browser;
180c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
190c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport android.content.Context;
200c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport android.graphics.Bitmap;
210ce8a942a895cfe1d432076a95f3ed3798ccb002John Reckimport android.graphics.drawable.Drawable;
220c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport android.view.LayoutInflater;
2383c0151b266f4789cb1728294eac98e3dba0ed53John Reckimport android.view.MotionEvent;
240c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport android.view.View;
2583c0151b266f4789cb1728294eac98e3dba0ed53John Reckimport android.view.ViewGroup;
2683c0151b266f4789cb1728294eac98e3dba0ed53John Reckimport android.widget.HorizontalScrollView;
270c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport android.widget.ImageView;
280c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Projectimport android.widget.TextView;
290c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
300c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project/**
310c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project *  Custom layout for an item representing a bookmark in the browser.
320c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project */
3383c0151b266f4789cb1728294eac98e3dba0ed53John Reckclass BookmarkItem extends HorizontalScrollView {
340c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
3535e9dd6283a2d65687104eb0b3a459c02dcb150bJohn Reck    final static int MAX_TEXTVIEW_LEN = 80;
3635e9dd6283a2d65687104eb0b3a459c02dcb150bJohn Reck
370c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    protected TextView    mTextView;
380c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    protected TextView    mUrlText;
390c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    protected ImageView   mImageView;
400c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    protected String      mUrl;
419907efcdb03d1e038b3923d6c58f907b1fda4c5eBen Murdoch    protected String      mTitle;
4283c0151b266f4789cb1728294eac98e3dba0ed53John Reck    protected boolean mEnableScrolling = false;
430c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
440c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    /**
450c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     *  Instantiate a bookmark item, including a default favicon.
460c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     *
470c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     *  @param context  The application context for the item.
480c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     */
490c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    BookmarkItem(Context context) {
500c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        super(context);
510c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
5283c0151b266f4789cb1728294eac98e3dba0ed53John Reck        setClickable(false);
5383c0151b266f4789cb1728294eac98e3dba0ed53John Reck        setEnableScrolling(false);
540c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        LayoutInflater factory = LayoutInflater.from(context);
550c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        factory.inflate(R.layout.history_item, this);
560c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        mTextView = (TextView) findViewById(R.id.title);
570c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        mUrlText = (TextView) findViewById(R.id.url);
580c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        mImageView = (ImageView) findViewById(R.id.favicon);
590c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        View star = findViewById(R.id.star);
600c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        star.setVisibility(View.GONE);
610c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    }
620c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
630c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    /**
640c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     *  Copy this BookmarkItem to item.
650c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     *  @param item BookmarkItem to receive the info from this BookmarkItem.
660c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     */
670c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    /* package */ void copyTo(BookmarkItem item) {
680c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        item.mTextView.setText(mTextView.getText());
690c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        item.mUrlText.setText(mUrlText.getText());
700c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        item.mImageView.setImageDrawable(mImageView.getDrawable());
710c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    }
720c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
730c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    /**
740c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     * Return the name assigned to this bookmark item.
750c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     */
760c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    /* package */ String getName() {
779907efcdb03d1e038b3923d6c58f907b1fda4c5eBen Murdoch        return mTitle;
780c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    }
790c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
800c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    /* package */ String getUrl() {
810c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        return mUrl;
820c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    }
830c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
840c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    /**
850c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     *  Set the favicon for this item.
860c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     *
870c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     *  @param b    The new bitmap for this item.
880c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     *              If it is null, will use the default.
890c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     */
900c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    /* package */ void setFavicon(Bitmap b) {
910c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        if (b != null) {
920c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            mImageView.setImageBitmap(b);
930c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        } else {
940c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project            mImageView.setImageResource(R.drawable.app_web_browser_sm);
950c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        }
960c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    }
970c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
980ce8a942a895cfe1d432076a95f3ed3798ccb002John Reck    void setFaviconBackground(Drawable d) {
990ce8a942a895cfe1d432076a95f3ed3798ccb002John Reck        mImageView.setBackgroundDrawable(d);
1000ce8a942a895cfe1d432076a95f3ed3798ccb002John Reck    }
1010ce8a942a895cfe1d432076a95f3ed3798ccb002John Reck
1020c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    /**
1030c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     *  Set the new name for the bookmark item.
1040c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     *
1050c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     *  @param name The new name for the bookmark item.
1060c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     */
1070c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    /* package */ void setName(String name) {
1089907efcdb03d1e038b3923d6c58f907b1fda4c5eBen Murdoch        if (name == null) {
1099907efcdb03d1e038b3923d6c58f907b1fda4c5eBen Murdoch            return;
1109907efcdb03d1e038b3923d6c58f907b1fda4c5eBen Murdoch        }
1119907efcdb03d1e038b3923d6c58f907b1fda4c5eBen Murdoch
1129907efcdb03d1e038b3923d6c58f907b1fda4c5eBen Murdoch        mTitle = name;
1139907efcdb03d1e038b3923d6c58f907b1fda4c5eBen Murdoch
11435e9dd6283a2d65687104eb0b3a459c02dcb150bJohn Reck        if (name.length() > MAX_TEXTVIEW_LEN) {
11535e9dd6283a2d65687104eb0b3a459c02dcb150bJohn Reck            name = name.substring(0, MAX_TEXTVIEW_LEN);
1169907efcdb03d1e038b3923d6c58f907b1fda4c5eBen Murdoch        }
1179907efcdb03d1e038b3923d6c58f907b1fda4c5eBen Murdoch
1180c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        mTextView.setText(name);
1190c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    }
1200c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project
1210c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    /**
1220c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     *  Set the new url for the bookmark item.
1230c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     *  @param url  The new url for the bookmark item.
1240c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project     */
1250c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    /* package */ void setUrl(String url) {
1269907efcdb03d1e038b3923d6c58f907b1fda4c5eBen Murdoch        if (url == null) {
1279907efcdb03d1e038b3923d6c58f907b1fda4c5eBen Murdoch            return;
1289907efcdb03d1e038b3923d6c58f907b1fda4c5eBen Murdoch        }
1299907efcdb03d1e038b3923d6c58f907b1fda4c5eBen Murdoch
1300c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project        mUrl = url;
1319907efcdb03d1e038b3923d6c58f907b1fda4c5eBen Murdoch
13283c0151b266f4789cb1728294eac98e3dba0ed53John Reck        url = UrlUtils.stripUrl(url);
13335e9dd6283a2d65687104eb0b3a459c02dcb150bJohn Reck        if (url.length() > MAX_TEXTVIEW_LEN) {
13435e9dd6283a2d65687104eb0b3a459c02dcb150bJohn Reck            url = url.substring(0, MAX_TEXTVIEW_LEN);
1359907efcdb03d1e038b3923d6c58f907b1fda4c5eBen Murdoch        }
1369907efcdb03d1e038b3923d6c58f907b1fda4c5eBen Murdoch
1379907efcdb03d1e038b3923d6c58f907b1fda4c5eBen Murdoch        mUrlText.setText(url);
1380c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project    }
13983c0151b266f4789cb1728294eac98e3dba0ed53John Reck
14083c0151b266f4789cb1728294eac98e3dba0ed53John Reck    void setEnableScrolling(boolean enable) {
14183c0151b266f4789cb1728294eac98e3dba0ed53John Reck        mEnableScrolling = enable;
14283c0151b266f4789cb1728294eac98e3dba0ed53John Reck        setFocusable(mEnableScrolling);
14383c0151b266f4789cb1728294eac98e3dba0ed53John Reck        setFocusableInTouchMode(mEnableScrolling);
14483c0151b266f4789cb1728294eac98e3dba0ed53John Reck        requestDisallowInterceptTouchEvent(!mEnableScrolling);
14583c0151b266f4789cb1728294eac98e3dba0ed53John Reck        requestLayout();
14683c0151b266f4789cb1728294eac98e3dba0ed53John Reck    }
14783c0151b266f4789cb1728294eac98e3dba0ed53John Reck
14883c0151b266f4789cb1728294eac98e3dba0ed53John Reck    @Override
14983c0151b266f4789cb1728294eac98e3dba0ed53John Reck    public boolean onTouchEvent(MotionEvent ev) {
15083c0151b266f4789cb1728294eac98e3dba0ed53John Reck        if (mEnableScrolling) {
15183c0151b266f4789cb1728294eac98e3dba0ed53John Reck            return super.onTouchEvent(ev);
15283c0151b266f4789cb1728294eac98e3dba0ed53John Reck        }
15383c0151b266f4789cb1728294eac98e3dba0ed53John Reck        return false;
15483c0151b266f4789cb1728294eac98e3dba0ed53John Reck    }
15583c0151b266f4789cb1728294eac98e3dba0ed53John Reck
15683c0151b266f4789cb1728294eac98e3dba0ed53John Reck    @Override
15783c0151b266f4789cb1728294eac98e3dba0ed53John Reck    protected void measureChild(View child, int parentWidthMeasureSpec,
15883c0151b266f4789cb1728294eac98e3dba0ed53John Reck            int parentHeightMeasureSpec) {
15983c0151b266f4789cb1728294eac98e3dba0ed53John Reck        if (mEnableScrolling) {
16083c0151b266f4789cb1728294eac98e3dba0ed53John Reck            super.measureChild(child, parentWidthMeasureSpec, parentHeightMeasureSpec);
16183c0151b266f4789cb1728294eac98e3dba0ed53John Reck            return;
16283c0151b266f4789cb1728294eac98e3dba0ed53John Reck        }
16383c0151b266f4789cb1728294eac98e3dba0ed53John Reck
16483c0151b266f4789cb1728294eac98e3dba0ed53John Reck        final ViewGroup.LayoutParams lp = child.getLayoutParams();
16583c0151b266f4789cb1728294eac98e3dba0ed53John Reck
16683c0151b266f4789cb1728294eac98e3dba0ed53John Reck        final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec,
16783c0151b266f4789cb1728294eac98e3dba0ed53John Reck                mPaddingLeft + mPaddingRight, lp.width);
16883c0151b266f4789cb1728294eac98e3dba0ed53John Reck        final int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec,
16983c0151b266f4789cb1728294eac98e3dba0ed53John Reck                mPaddingTop + mPaddingBottom, lp.height);
17083c0151b266f4789cb1728294eac98e3dba0ed53John Reck
17183c0151b266f4789cb1728294eac98e3dba0ed53John Reck        child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
17283c0151b266f4789cb1728294eac98e3dba0ed53John Reck    }
17383c0151b266f4789cb1728294eac98e3dba0ed53John Reck
17483c0151b266f4789cb1728294eac98e3dba0ed53John Reck    @Override
17583c0151b266f4789cb1728294eac98e3dba0ed53John Reck    protected void measureChildWithMargins(View child,
17683c0151b266f4789cb1728294eac98e3dba0ed53John Reck            int parentWidthMeasureSpec, int widthUsed,
17783c0151b266f4789cb1728294eac98e3dba0ed53John Reck            int parentHeightMeasureSpec, int heightUsed) {
17883c0151b266f4789cb1728294eac98e3dba0ed53John Reck        if (mEnableScrolling) {
17983c0151b266f4789cb1728294eac98e3dba0ed53John Reck            super.measureChildWithMargins(child, parentWidthMeasureSpec,
18083c0151b266f4789cb1728294eac98e3dba0ed53John Reck                    widthUsed, parentHeightMeasureSpec, heightUsed);
18183c0151b266f4789cb1728294eac98e3dba0ed53John Reck            return;
18283c0151b266f4789cb1728294eac98e3dba0ed53John Reck        }
18383c0151b266f4789cb1728294eac98e3dba0ed53John Reck
18483c0151b266f4789cb1728294eac98e3dba0ed53John Reck        final MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
18583c0151b266f4789cb1728294eac98e3dba0ed53John Reck
18683c0151b266f4789cb1728294eac98e3dba0ed53John Reck        final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec,
18783c0151b266f4789cb1728294eac98e3dba0ed53John Reck                mPaddingLeft + mPaddingRight + lp.leftMargin + lp.rightMargin
18883c0151b266f4789cb1728294eac98e3dba0ed53John Reck                        + widthUsed, lp.width);
18983c0151b266f4789cb1728294eac98e3dba0ed53John Reck        final int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec,
19083c0151b266f4789cb1728294eac98e3dba0ed53John Reck                mPaddingTop + mPaddingBottom + lp.topMargin + lp.bottomMargin
19183c0151b266f4789cb1728294eac98e3dba0ed53John Reck                        + heightUsed, lp.height);
19283c0151b266f4789cb1728294eac98e3dba0ed53John Reck
19383c0151b266f4789cb1728294eac98e3dba0ed53John Reck        child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
19483c0151b266f4789cb1728294eac98e3dba0ed53John Reck    }
1950c90888c75eed12f6e2e14a9044faf50bd4af8edThe Android Open Source Project}
196