blob: 1f7b7d2278292309305bb92d3e75ffe05af51195 [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
John Reck0ce8a942011-01-14 19:57:09 -080087 void setFaviconBackground(Drawable d) {
Pankaj Garg21dad562015-07-02 17:17:24 -070088 mTileView.setBackgroundDrawable(d);
John Reck0ce8a942011-01-14 19:57:09 -080089 }
90
The Android Open Source Project0c908882009-03-03 19:32:16 -080091 /**
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 Murdoch9907efc2009-10-28 13:22:46 +000097 if (name == null) {
98 return;
99 }
100
101 mTitle = name;
102
John Reck35e9dd62011-04-25 09:01:54 -0700103 if (name.length() > MAX_TEXTVIEW_LEN) {
104 name = name.substring(0, MAX_TEXTVIEW_LEN);
Ben Murdoch9907efc2009-10-28 13:22:46 +0000105 }
106
The Android Open Source Project0c908882009-03-03 19:32:16 -0800107 mTextView.setText(name);
108 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800109
The Android Open Source Project0c908882009-03-03 19:32:16 -0800110 /**
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 Murdoch9907efc2009-10-28 13:22:46 +0000115 if (url == null) {
116 return;
117 }
118
The Android Open Source Project0c908882009-03-03 19:32:16 -0800119 mUrl = url;
Ben Murdoch9907efc2009-10-28 13:22:46 +0000120
John Reck83c01512011-09-09 11:14:16 -0700121 url = UrlUtils.stripUrl(url);
Axesh R. Ajmera9b7b58c2014-12-22 16:33:31 -0800122
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 Reck35e9dd62011-04-25 09:01:54 -0700128 if (url.length() > MAX_TEXTVIEW_LEN) {
Axesh R. Ajmera9b7b58c2014-12-22 16:33:31 -0800129
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 Murdoch9907efc2009-10-28 13:22:46 +0000143 }
144
145 mUrlText.setText(url);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800146 }
John Reck83c01512011-09-09 11:14:16 -0700147
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 Amirzada9b1e9882014-02-26 17:15:46 -0800175 getPaddingLeft() + getPaddingRight(), lp.width);
John Reck83c01512011-09-09 11:14:16 -0700176 final int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800177 getPaddingTop() + getPaddingBottom(), lp.height);
John Reck83c01512011-09-09 11:14:16 -0700178
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 Amirzada9b1e9882014-02-26 17:15:46 -0800195 getPaddingLeft() + getPaddingRight() + lp.leftMargin + lp.rightMargin
John Reck83c01512011-09-09 11:14:16 -0700196 + widthUsed, lp.width);
197 final int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800198 getPaddingTop() + getPaddingBottom() + lp.topMargin + lp.bottomMargin
John Reck83c01512011-09-09 11:14:16 -0700199 + heightUsed, lp.height);
200
201 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
202 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800203}