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