The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 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 | |
Bijan Amirzada | 41242f2 | 2014-03-21 12:12:18 -0700 | [diff] [blame] | 17 | package com.android.browser; |
Bijan Amirzada | 9b1e988 | 2014-02-26 17:15:46 -0800 | [diff] [blame] | 18 | |
Bijan Amirzada | 41242f2 | 2014-03-21 12:12:18 -0700 | [diff] [blame] | 19 | import com.android.browser.R; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 20 | |
| 21 | import android.content.Context; |
| 22 | import android.graphics.Bitmap; |
John Reck | 0ce8a94 | 2011-01-14 19:57:09 -0800 | [diff] [blame] | 23 | import android.graphics.drawable.Drawable; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 24 | import android.view.LayoutInflater; |
John Reck | 83c0151 | 2011-09-09 11:14:16 -0700 | [diff] [blame] | 25 | import android.view.MotionEvent; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 26 | import android.view.View; |
John Reck | 83c0151 | 2011-09-09 11:14:16 -0700 | [diff] [blame] | 27 | import android.view.ViewGroup; |
| 28 | import android.widget.HorizontalScrollView; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 29 | import android.widget.ImageView; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 30 | import android.widget.TextView; |
| 31 | |
| 32 | /** |
| 33 | * Custom layout for an item representing a bookmark in the browser. |
| 34 | */ |
John Reck | 83c0151 | 2011-09-09 11:14:16 -0700 | [diff] [blame] | 35 | class BookmarkItem extends HorizontalScrollView { |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 36 | |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 37 | final static int MAX_TEXTVIEW_LEN = 80; |
| 38 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 39 | protected TextView mTextView; |
| 40 | protected TextView mUrlText; |
| 41 | protected ImageView mImageView; |
| 42 | protected String mUrl; |
Ben Murdoch | 9907efc | 2009-10-28 13:22:46 +0000 | [diff] [blame] | 43 | protected String mTitle; |
John Reck | 83c0151 | 2011-09-09 11:14:16 -0700 | [diff] [blame] | 44 | protected boolean mEnableScrolling = false; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 45 | |
| 46 | /** |
| 47 | * Instantiate a bookmark item, including a default favicon. |
| 48 | * |
| 49 | * @param context The application context for the item. |
| 50 | */ |
| 51 | BookmarkItem(Context context) { |
| 52 | super(context); |
| 53 | |
John Reck | 83c0151 | 2011-09-09 11:14:16 -0700 | [diff] [blame] | 54 | setClickable(false); |
| 55 | setEnableScrolling(false); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 56 | LayoutInflater factory = LayoutInflater.from(context); |
| 57 | factory.inflate(R.layout.history_item, this); |
| 58 | mTextView = (TextView) findViewById(R.id.title); |
| 59 | mUrlText = (TextView) findViewById(R.id.url); |
| 60 | mImageView = (ImageView) findViewById(R.id.favicon); |
| 61 | View star = findViewById(R.id.star); |
| 62 | star.setVisibility(View.GONE); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Copy this BookmarkItem to item. |
| 67 | * @param item BookmarkItem to receive the info from this BookmarkItem. |
| 68 | */ |
| 69 | /* package */ void copyTo(BookmarkItem item) { |
| 70 | item.mTextView.setText(mTextView.getText()); |
| 71 | item.mUrlText.setText(mUrlText.getText()); |
| 72 | item.mImageView.setImageDrawable(mImageView.getDrawable()); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Return the name assigned to this bookmark item. |
| 77 | */ |
| 78 | /* package */ String getName() { |
Ben Murdoch | 9907efc | 2009-10-28 13:22:46 +0000 | [diff] [blame] | 79 | return mTitle; |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 80 | } |
| 81 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 82 | /* package */ String getUrl() { |
| 83 | return mUrl; |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Set the favicon for this item. |
| 88 | * |
| 89 | * @param b The new bitmap for this item. |
| 90 | * If it is null, will use the default. |
| 91 | */ |
| 92 | /* package */ void setFavicon(Bitmap b) { |
| 93 | if (b != null) { |
| 94 | mImageView.setImageBitmap(b); |
| 95 | } else { |
Enrico Ros | d6efa97 | 2014-12-02 19:49:59 -0800 | [diff] [blame] | 96 | mImageView.setImageResource(R.drawable.ic_deco_favicon_normal); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 97 | } |
| 98 | } |
| 99 | |
John Reck | 0ce8a94 | 2011-01-14 19:57:09 -0800 | [diff] [blame] | 100 | void setFaviconBackground(Drawable d) { |
| 101 | mImageView.setBackgroundDrawable(d); |
| 102 | } |
| 103 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 104 | /** |
| 105 | * Set the new name for the bookmark item. |
| 106 | * |
| 107 | * @param name The new name for the bookmark item. |
| 108 | */ |
| 109 | /* package */ void setName(String name) { |
Ben Murdoch | 9907efc | 2009-10-28 13:22:46 +0000 | [diff] [blame] | 110 | if (name == null) { |
| 111 | return; |
| 112 | } |
| 113 | |
| 114 | mTitle = name; |
| 115 | |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 116 | if (name.length() > MAX_TEXTVIEW_LEN) { |
| 117 | name = name.substring(0, MAX_TEXTVIEW_LEN); |
Ben Murdoch | 9907efc | 2009-10-28 13:22:46 +0000 | [diff] [blame] | 118 | } |
| 119 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 120 | mTextView.setText(name); |
| 121 | } |
Bijan Amirzada | 9b1e988 | 2014-02-26 17:15:46 -0800 | [diff] [blame] | 122 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 123 | /** |
| 124 | * Set the new url for the bookmark item. |
| 125 | * @param url The new url for the bookmark item. |
| 126 | */ |
| 127 | /* package */ void setUrl(String url) { |
Ben Murdoch | 9907efc | 2009-10-28 13:22:46 +0000 | [diff] [blame] | 128 | if (url == null) { |
| 129 | return; |
| 130 | } |
| 131 | |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 132 | mUrl = url; |
Ben Murdoch | 9907efc | 2009-10-28 13:22:46 +0000 | [diff] [blame] | 133 | |
John Reck | 83c0151 | 2011-09-09 11:14:16 -0700 | [diff] [blame] | 134 | url = UrlUtils.stripUrl(url); |
John Reck | 35e9dd6 | 2011-04-25 09:01:54 -0700 | [diff] [blame] | 135 | if (url.length() > MAX_TEXTVIEW_LEN) { |
| 136 | url = url.substring(0, MAX_TEXTVIEW_LEN); |
Ben Murdoch | 9907efc | 2009-10-28 13:22:46 +0000 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | mUrlText.setText(url); |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 140 | } |
John Reck | 83c0151 | 2011-09-09 11:14:16 -0700 | [diff] [blame] | 141 | |
| 142 | void setEnableScrolling(boolean enable) { |
| 143 | mEnableScrolling = enable; |
| 144 | setFocusable(mEnableScrolling); |
| 145 | setFocusableInTouchMode(mEnableScrolling); |
| 146 | requestDisallowInterceptTouchEvent(!mEnableScrolling); |
| 147 | requestLayout(); |
| 148 | } |
| 149 | |
| 150 | @Override |
| 151 | public boolean onTouchEvent(MotionEvent ev) { |
| 152 | if (mEnableScrolling) { |
| 153 | return super.onTouchEvent(ev); |
| 154 | } |
| 155 | return false; |
| 156 | } |
| 157 | |
| 158 | @Override |
| 159 | protected void measureChild(View child, int parentWidthMeasureSpec, |
| 160 | int parentHeightMeasureSpec) { |
| 161 | if (mEnableScrolling) { |
| 162 | super.measureChild(child, parentWidthMeasureSpec, parentHeightMeasureSpec); |
| 163 | return; |
| 164 | } |
| 165 | |
| 166 | final ViewGroup.LayoutParams lp = child.getLayoutParams(); |
| 167 | |
| 168 | final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec, |
Bijan Amirzada | 9b1e988 | 2014-02-26 17:15:46 -0800 | [diff] [blame] | 169 | getPaddingLeft() + getPaddingRight(), lp.width); |
John Reck | 83c0151 | 2011-09-09 11:14:16 -0700 | [diff] [blame] | 170 | final int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec, |
Bijan Amirzada | 9b1e988 | 2014-02-26 17:15:46 -0800 | [diff] [blame] | 171 | getPaddingTop() + getPaddingBottom(), lp.height); |
John Reck | 83c0151 | 2011-09-09 11:14:16 -0700 | [diff] [blame] | 172 | |
| 173 | child.measure(childWidthMeasureSpec, childHeightMeasureSpec); |
| 174 | } |
| 175 | |
| 176 | @Override |
| 177 | protected void measureChildWithMargins(View child, |
| 178 | int parentWidthMeasureSpec, int widthUsed, |
| 179 | int parentHeightMeasureSpec, int heightUsed) { |
| 180 | if (mEnableScrolling) { |
| 181 | super.measureChildWithMargins(child, parentWidthMeasureSpec, |
| 182 | widthUsed, parentHeightMeasureSpec, heightUsed); |
| 183 | return; |
| 184 | } |
| 185 | |
| 186 | final MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams(); |
| 187 | |
| 188 | final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec, |
Bijan Amirzada | 9b1e988 | 2014-02-26 17:15:46 -0800 | [diff] [blame] | 189 | getPaddingLeft() + getPaddingRight() + lp.leftMargin + lp.rightMargin |
John Reck | 83c0151 | 2011-09-09 11:14:16 -0700 | [diff] [blame] | 190 | + widthUsed, lp.width); |
| 191 | final int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec, |
Bijan Amirzada | 9b1e988 | 2014-02-26 17:15:46 -0800 | [diff] [blame] | 192 | getPaddingTop() + getPaddingBottom() + lp.topMargin + lp.bottomMargin |
John Reck | 83c0151 | 2011-09-09 11:14:16 -0700 | [diff] [blame] | 193 | + heightUsed, lp.height); |
| 194 | |
| 195 | child.measure(childWidthMeasureSpec, childHeightMeasureSpec); |
| 196 | } |
The Android Open Source Project | 0c90888 | 2009-03-03 19:32:16 -0800 | [diff] [blame] | 197 | } |