blob: 857329038bc72d4aa197bcd19dc11c1c658c5382 [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
John Reck0ce8a942011-01-14 19:57:09 -0800101 void setFaviconBackground(Drawable d) {
102 mImageView.setBackgroundDrawable(d);
103 }
104
The Android Open Source Project0c908882009-03-03 19:32:16 -0800105 /**
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 Murdoch9907efc2009-10-28 13:22:46 +0000111 if (name == null) {
112 return;
113 }
114
115 mTitle = name;
116
John Reck35e9dd62011-04-25 09:01:54 -0700117 if (name.length() > MAX_TEXTVIEW_LEN) {
118 name = name.substring(0, MAX_TEXTVIEW_LEN);
Ben Murdoch9907efc2009-10-28 13:22:46 +0000119 }
120
The Android Open Source Project0c908882009-03-03 19:32:16 -0800121 mTextView.setText(name);
122 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800123
The Android Open Source Project0c908882009-03-03 19:32:16 -0800124 /**
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 Murdoch9907efc2009-10-28 13:22:46 +0000129 if (url == null) {
130 return;
131 }
132
The Android Open Source Project0c908882009-03-03 19:32:16 -0800133 mUrl = url;
Ben Murdoch9907efc2009-10-28 13:22:46 +0000134
John Reck83c01512011-09-09 11:14:16 -0700135 url = UrlUtils.stripUrl(url);
Axesh R. Ajmera9b7b58c2014-12-22 16:33:31 -0800136
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 Reck35e9dd62011-04-25 09:01:54 -0700142 if (url.length() > MAX_TEXTVIEW_LEN) {
Axesh R. Ajmera9b7b58c2014-12-22 16:33:31 -0800143
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 Murdoch9907efc2009-10-28 13:22:46 +0000158 }
159
160 mUrlText.setText(url);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800161 }
John Reck83c01512011-09-09 11:14:16 -0700162
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 Amirzada9b1e9882014-02-26 17:15:46 -0800190 getPaddingLeft() + getPaddingRight(), lp.width);
John Reck83c01512011-09-09 11:14:16 -0700191 final int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800192 getPaddingTop() + getPaddingBottom(), lp.height);
John Reck83c01512011-09-09 11:14:16 -0700193
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 Amirzada9b1e9882014-02-26 17:15:46 -0800210 getPaddingLeft() + getPaddingRight() + lp.leftMargin + lp.rightMargin
John Reck83c01512011-09-09 11:14:16 -0700211 + widthUsed, lp.width);
212 final int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800213 getPaddingTop() + getPaddingBottom() + lp.topMargin + lp.bottomMargin
John Reck83c01512011-09-09 11:14:16 -0700214 + heightUsed, lp.height);
215
216 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
217 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800218}