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