blob: b230de6c39f29cb0b7bd51329fd6b3fdfa492626 [file] [log] [blame]
The Android Open Source Project0c908882009-03-03 19:32:16 -08001/*
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 Amirzada41242f22014-03-21 12:12:18 -070017package com.android.browser;
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080018
Bijan Amirzada41242f22014-03-21 12:12:18 -070019import com.android.browser.R;
The Android Open Source Project0c908882009-03-03 19:32:16 -080020
21import android.content.Context;
22import android.graphics.Bitmap;
John Reck0ce8a942011-01-14 19:57:09 -080023import android.graphics.drawable.Drawable;
Axesh R. Ajmera9b7b58c2014-12-22 16:33:31 -080024import android.text.method.ScrollingMovementMethod;
The Android Open Source Project0c908882009-03-03 19:32:16 -080025import android.view.LayoutInflater;
John Reck83c01512011-09-09 11:14:16 -070026import android.view.MotionEvent;
The Android Open Source Project0c908882009-03-03 19:32:16 -080027import android.view.View;
John Reck83c01512011-09-09 11:14:16 -070028import android.view.ViewGroup;
Axesh R. Ajmera9b7b58c2014-12-22 16:33:31 -080029import android.widget.ScrollView;
The Android Open Source Project0c908882009-03-03 19:32:16 -080030import android.widget.ImageView;
The Android Open Source Project0c908882009-03-03 19:32:16 -080031import android.widget.TextView;
32
33/**
34 * Custom layout for an item representing a bookmark in the browser.
35 */
Axesh R. Ajmera9b7b58c2014-12-22 16:33:31 -080036class BookmarkItem extends ScrollView {
The Android Open Source Project0c908882009-03-03 19:32:16 -080037
John Reck35e9dd62011-04-25 09:01:54 -070038 final static int MAX_TEXTVIEW_LEN = 80;
39
The Android Open Source Project0c908882009-03-03 19:32:16 -080040 protected TextView mTextView;
41 protected TextView mUrlText;
42 protected ImageView mImageView;
43 protected String mUrl;
Ben Murdoch9907efc2009-10-28 13:22:46 +000044 protected String mTitle;
John Reck83c01512011-09-09 11:14:16 -070045 protected boolean mEnableScrolling = false;
The Android Open Source Project0c908882009-03-03 19:32:16 -080046
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 Reck83c01512011-09-09 11:14:16 -070055 setClickable(false);
56 setEnableScrolling(false);
The Android Open Source Project0c908882009-03-03 19:32:16 -080057 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 Murdoch9907efc2009-10-28 13:22:46 +000080 return mTitle;
The Android Open Source Project0c908882009-03-03 19:32:16 -080081 }
82
The Android Open Source Project0c908882009-03-03 19:32:16 -080083 /* 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 Rosd6efa972014-12-02 19:49:59 -080097 mImageView.setImageResource(R.drawable.ic_deco_favicon_normal);
The Android Open Source Project0c908882009-03-03 19:32:16 -080098 }
99 }
100
Dave Tharp9a082b12015-06-19 08:46:57 -0700101 public int getFavIconIntrinsicWidth() {
102 return mImageView.getDrawable().getIntrinsicWidth();
103 }
104
John Reck0ce8a942011-01-14 19:57:09 -0800105 void setFaviconBackground(Drawable d) {
106 mImageView.setBackgroundDrawable(d);
107 }
108
The Android Open Source Project0c908882009-03-03 19:32:16 -0800109 /**
110 * Set the new name for the bookmark item.
111 *
112 * @param name The new name for the bookmark item.
113 */
114 /* package */ void setName(String name) {
Ben Murdoch9907efc2009-10-28 13:22:46 +0000115 if (name == null) {
116 return;
117 }
118
119 mTitle = name;
120
John Reck35e9dd62011-04-25 09:01:54 -0700121 if (name.length() > MAX_TEXTVIEW_LEN) {
122 name = name.substring(0, MAX_TEXTVIEW_LEN);
Ben Murdoch9907efc2009-10-28 13:22:46 +0000123 }
124
The Android Open Source Project0c908882009-03-03 19:32:16 -0800125 mTextView.setText(name);
126 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800127
The Android Open Source Project0c908882009-03-03 19:32:16 -0800128 /**
129 * Set the new url for the bookmark item.
130 * @param url The new url for the bookmark item.
131 */
132 /* package */ void setUrl(String url) {
Ben Murdoch9907efc2009-10-28 13:22:46 +0000133 if (url == null) {
134 return;
135 }
136
The Android Open Source Project0c908882009-03-03 19:32:16 -0800137 mUrl = url;
Ben Murdoch9907efc2009-10-28 13:22:46 +0000138
John Reck83c01512011-09-09 11:14:16 -0700139 url = UrlUtils.stripUrl(url);
Axesh R. Ajmera9b7b58c2014-12-22 16:33:31 -0800140
141 /*
142 * Since there are more than 80 characters
143 * in the URL this is formatting the url
144 * to a vertical Scroll View.
145 */
John Reck35e9dd62011-04-25 09:01:54 -0700146 if (url.length() > MAX_TEXTVIEW_LEN) {
Axesh R. Ajmera9b7b58c2014-12-22 16:33:31 -0800147
148 // url cannot exceed max length
149 if (url.length() > UrlInputView.URL_MAX_LENGTH) {
150 url = url.substring(0, UrlInputView.URL_MAX_LENGTH);
151 }
152
153 mUrlText.setHorizontallyScrolling(false);
154 mUrlText.setSingleLine(false);
155 mUrlText.setVerticalScrollBarEnabled(true);
156 /*
157 * Only the first 3 lines of the URL will be visible
158 * Rest of it will be scrollable.
159 */
160 mUrlText.setMaxLines(3);
Ben Murdoch9907efc2009-10-28 13:22:46 +0000161 }
162
163 mUrlText.setText(url);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800164 }
John Reck83c01512011-09-09 11:14:16 -0700165
166 void setEnableScrolling(boolean enable) {
167 mEnableScrolling = enable;
168 setFocusable(mEnableScrolling);
169 setFocusableInTouchMode(mEnableScrolling);
170 requestDisallowInterceptTouchEvent(!mEnableScrolling);
171 requestLayout();
172 }
173
174 @Override
175 public boolean onTouchEvent(MotionEvent ev) {
176 if (mEnableScrolling) {
177 return super.onTouchEvent(ev);
178 }
179 return false;
180 }
181
182 @Override
183 protected void measureChild(View child, int parentWidthMeasureSpec,
184 int parentHeightMeasureSpec) {
185 if (mEnableScrolling) {
186 super.measureChild(child, parentWidthMeasureSpec, parentHeightMeasureSpec);
187 return;
188 }
189
190 final ViewGroup.LayoutParams lp = child.getLayoutParams();
191
192 final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800193 getPaddingLeft() + getPaddingRight(), lp.width);
John Reck83c01512011-09-09 11:14:16 -0700194 final int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800195 getPaddingTop() + getPaddingBottom(), lp.height);
John Reck83c01512011-09-09 11:14:16 -0700196
197 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
198 }
199
200 @Override
201 protected void measureChildWithMargins(View child,
202 int parentWidthMeasureSpec, int widthUsed,
203 int parentHeightMeasureSpec, int heightUsed) {
204 if (mEnableScrolling) {
205 super.measureChildWithMargins(child, parentWidthMeasureSpec,
206 widthUsed, parentHeightMeasureSpec, heightUsed);
207 return;
208 }
209
210 final MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
211
212 final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800213 getPaddingLeft() + getPaddingRight() + lp.leftMargin + lp.rightMargin
John Reck83c01512011-09-09 11:14:16 -0700214 + widthUsed, lp.width);
215 final int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800216 getPaddingTop() + getPaddingBottom() + lp.topMargin + lp.bottomMargin
John Reck83c01512011-09-09 11:14:16 -0700217 + heightUsed, lp.height);
218
219 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
220 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800221}