blob: 12b2b705f53b15dd663ba60870e8c21821e3fd4f [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
The Android Open Source Project0c908882009-03-03 19:32:16 -080019import android.content.Context;
20import android.graphics.Bitmap;
John Reck0ce8a942011-01-14 19:57:09 -080021import android.graphics.drawable.Drawable;
The Android Open Source Project0c908882009-03-03 19:32:16 -080022import android.view.LayoutInflater;
John Reck83c01512011-09-09 11:14:16 -070023import android.view.MotionEvent;
The Android Open Source Project0c908882009-03-03 19:32:16 -080024import android.view.View;
John Reck83c01512011-09-09 11:14:16 -070025import android.view.ViewGroup;
Axesh R. Ajmera9b7b58c2014-12-22 16:33:31 -080026import android.widget.ScrollView;
The Android Open Source Project0c908882009-03-03 19:32:16 -080027import android.widget.TextView;
28
29/**
30 * Custom layout for an item representing a bookmark in the browser.
31 */
Axesh R. Ajmera9b7b58c2014-12-22 16:33:31 -080032class BookmarkItem extends ScrollView {
The Android Open Source Project0c908882009-03-03 19:32:16 -080033
John Reck35e9dd62011-04-25 09:01:54 -070034 final static int MAX_TEXTVIEW_LEN = 80;
35
Pankaj Garg21dad562015-07-02 17:17:24 -070036 protected TextView mTextView;
37 protected TextView mUrlText;
38 protected SiteTileView mTileView;
39 protected String mUrl;
40 protected String mTitle;
John Reck83c01512011-09-09 11:14:16 -070041 protected boolean mEnableScrolling = false;
The Android Open Source Project0c908882009-03-03 19:32:16 -080042
Pankaj Garg21dad562015-07-02 17:17:24 -070043 protected Bitmap mBitmap;
The Android Open Source Project0c908882009-03-03 19:32:16 -080044 /**
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 Reck83c01512011-09-09 11:14:16 -070052 setClickable(false);
53 setEnableScrolling(false);
The Android Open Source Project0c908882009-03-03 19:32:16 -080054 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 Garg21dad562015-07-02 17:17:24 -070058 mTileView = (SiteTileView) findViewById(R.id.favicon);
The Android Open Source Project0c908882009-03-03 19:32:16 -080059 View star = findViewById(R.id.star);
60 star.setVisibility(View.GONE);
61 }
62
63 /**
The Android Open Source Project0c908882009-03-03 19:32:16 -080064 * Return the name assigned to this bookmark item.
65 */
66 /* package */ String getName() {
Ben Murdoch9907efc2009-10-28 13:22:46 +000067 return mTitle;
The Android Open Source Project0c908882009-03-03 19:32:16 -080068 }
69
The Android Open Source Project0c908882009-03-03 19:32:16 -080070 /* 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 Garg21dad562015-07-02 17:17:24 -070082 mTileView.replaceFavicon(b);
83 mBitmap = b;
The Android Open Source Project0c908882009-03-03 19:32:16 -080084 }
85 }
86
Dave Tharp9a082b12015-06-19 08:46:57 -070087 public int getFavIconIntrinsicWidth() {
Pankaj Garg21dad562015-07-02 17:17:24 -070088 return mTileView.getMeasuredWidth();
Dave Tharp9a082b12015-06-19 08:46:57 -070089 }
90
John Reck0ce8a942011-01-14 19:57:09 -080091 void setFaviconBackground(Drawable d) {
Pankaj Garg21dad562015-07-02 17:17:24 -070092 mTileView.setBackgroundDrawable(d);
John Reck0ce8a942011-01-14 19:57:09 -080093 }
94
The Android Open Source Project0c908882009-03-03 19:32:16 -080095 /**
96 * Set the new name for the bookmark item.
97 *
98 * @param name The new name for the bookmark item.
99 */
100 /* package */ void setName(String name) {
Ben Murdoch9907efc2009-10-28 13:22:46 +0000101 if (name == null) {
102 return;
103 }
104
105 mTitle = name;
106
John Reck35e9dd62011-04-25 09:01:54 -0700107 if (name.length() > MAX_TEXTVIEW_LEN) {
108 name = name.substring(0, MAX_TEXTVIEW_LEN);
Ben Murdoch9907efc2009-10-28 13:22:46 +0000109 }
110
The Android Open Source Project0c908882009-03-03 19:32:16 -0800111 mTextView.setText(name);
112 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800113
The Android Open Source Project0c908882009-03-03 19:32:16 -0800114 /**
115 * Set the new url for the bookmark item.
116 * @param url The new url for the bookmark item.
117 */
118 /* package */ void setUrl(String url) {
Ben Murdoch9907efc2009-10-28 13:22:46 +0000119 if (url == null) {
120 return;
121 }
122
The Android Open Source Project0c908882009-03-03 19:32:16 -0800123 mUrl = url;
Ben Murdoch9907efc2009-10-28 13:22:46 +0000124
John Reck83c01512011-09-09 11:14:16 -0700125 url = UrlUtils.stripUrl(url);
Axesh R. Ajmera9b7b58c2014-12-22 16:33:31 -0800126
127 /*
128 * Since there are more than 80 characters
129 * in the URL this is formatting the url
130 * to a vertical Scroll View.
131 */
John Reck35e9dd62011-04-25 09:01:54 -0700132 if (url.length() > MAX_TEXTVIEW_LEN) {
Axesh R. Ajmera9b7b58c2014-12-22 16:33:31 -0800133
134 // url cannot exceed max length
135 if (url.length() > UrlInputView.URL_MAX_LENGTH) {
136 url = url.substring(0, UrlInputView.URL_MAX_LENGTH);
137 }
138
139 mUrlText.setHorizontallyScrolling(false);
140 mUrlText.setSingleLine(false);
141 mUrlText.setVerticalScrollBarEnabled(true);
142 /*
143 * Only the first 3 lines of the URL will be visible
144 * Rest of it will be scrollable.
145 */
146 mUrlText.setMaxLines(3);
Ben Murdoch9907efc2009-10-28 13:22:46 +0000147 }
148
149 mUrlText.setText(url);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800150 }
John Reck83c01512011-09-09 11:14:16 -0700151
152 void setEnableScrolling(boolean enable) {
153 mEnableScrolling = enable;
154 setFocusable(mEnableScrolling);
155 setFocusableInTouchMode(mEnableScrolling);
156 requestDisallowInterceptTouchEvent(!mEnableScrolling);
157 requestLayout();
158 }
159
160 @Override
161 public boolean onTouchEvent(MotionEvent ev) {
162 if (mEnableScrolling) {
163 return super.onTouchEvent(ev);
164 }
165 return false;
166 }
167
168 @Override
169 protected void measureChild(View child, int parentWidthMeasureSpec,
170 int parentHeightMeasureSpec) {
171 if (mEnableScrolling) {
172 super.measureChild(child, parentWidthMeasureSpec, parentHeightMeasureSpec);
173 return;
174 }
175
176 final ViewGroup.LayoutParams lp = child.getLayoutParams();
177
178 final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800179 getPaddingLeft() + getPaddingRight(), lp.width);
John Reck83c01512011-09-09 11:14:16 -0700180 final int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800181 getPaddingTop() + getPaddingBottom(), lp.height);
John Reck83c01512011-09-09 11:14:16 -0700182
183 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
184 }
185
186 @Override
187 protected void measureChildWithMargins(View child,
188 int parentWidthMeasureSpec, int widthUsed,
189 int parentHeightMeasureSpec, int heightUsed) {
190 if (mEnableScrolling) {
191 super.measureChildWithMargins(child, parentWidthMeasureSpec,
192 widthUsed, parentHeightMeasureSpec, heightUsed);
193 return;
194 }
195
196 final MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
197
198 final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800199 getPaddingLeft() + getPaddingRight() + lp.leftMargin + lp.rightMargin
John Reck83c01512011-09-09 11:14:16 -0700200 + widthUsed, lp.width);
201 final int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800202 getPaddingTop() + getPaddingBottom() + lp.topMargin + lp.bottomMargin
John Reck83c01512011-09-09 11:14:16 -0700203 + heightUsed, lp.height);
204
205 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
206 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800207}