blob: eccc438ddb7ad078324efe2586925d9e8f201056 [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;
The Android Open Source Project0c908882009-03-03 19:32:16 -080024import android.view.LayoutInflater;
John Reck83c01512011-09-09 11:14:16 -070025import android.view.MotionEvent;
The Android Open Source Project0c908882009-03-03 19:32:16 -080026import android.view.View;
John Reck83c01512011-09-09 11:14:16 -070027import android.view.ViewGroup;
28import android.widget.HorizontalScrollView;
The Android Open Source Project0c908882009-03-03 19:32:16 -080029import android.widget.ImageView;
The Android Open Source Project0c908882009-03-03 19:32:16 -080030import android.widget.TextView;
31
32/**
33 * Custom layout for an item representing a bookmark in the browser.
34 */
John Reck83c01512011-09-09 11:14:16 -070035class BookmarkItem extends HorizontalScrollView {
The Android Open Source Project0c908882009-03-03 19:32:16 -080036
John Reck35e9dd62011-04-25 09:01:54 -070037 final static int MAX_TEXTVIEW_LEN = 80;
38
The Android Open Source Project0c908882009-03-03 19:32:16 -080039 protected TextView mTextView;
40 protected TextView mUrlText;
41 protected ImageView mImageView;
42 protected String mUrl;
Ben Murdoch9907efc2009-10-28 13:22:46 +000043 protected String mTitle;
John Reck83c01512011-09-09 11:14:16 -070044 protected boolean mEnableScrolling = false;
The Android Open Source Project0c908882009-03-03 19:32:16 -080045
46 /**
47 * Instantiate a bookmark item, including a default favicon.
48 *
49 * @param context The application context for the item.
50 */
51 BookmarkItem(Context context) {
52 super(context);
53
John Reck83c01512011-09-09 11:14:16 -070054 setClickable(false);
55 setEnableScrolling(false);
The Android Open Source Project0c908882009-03-03 19:32:16 -080056 LayoutInflater factory = LayoutInflater.from(context);
57 factory.inflate(R.layout.history_item, this);
58 mTextView = (TextView) findViewById(R.id.title);
59 mUrlText = (TextView) findViewById(R.id.url);
60 mImageView = (ImageView) findViewById(R.id.favicon);
61 View star = findViewById(R.id.star);
62 star.setVisibility(View.GONE);
63 }
64
65 /**
66 * Copy this BookmarkItem to item.
67 * @param item BookmarkItem to receive the info from this BookmarkItem.
68 */
69 /* package */ void copyTo(BookmarkItem item) {
70 item.mTextView.setText(mTextView.getText());
71 item.mUrlText.setText(mUrlText.getText());
72 item.mImageView.setImageDrawable(mImageView.getDrawable());
73 }
74
75 /**
76 * Return the name assigned to this bookmark item.
77 */
78 /* package */ String getName() {
Ben Murdoch9907efc2009-10-28 13:22:46 +000079 return mTitle;
The Android Open Source Project0c908882009-03-03 19:32:16 -080080 }
81
The Android Open Source Project0c908882009-03-03 19:32:16 -080082 /* package */ String getUrl() {
83 return mUrl;
84 }
85
86 /**
87 * Set the favicon for this item.
88 *
89 * @param b The new bitmap for this item.
90 * If it is null, will use the default.
91 */
92 /* package */ void setFavicon(Bitmap b) {
93 if (b != null) {
94 mImageView.setImageBitmap(b);
95 } else {
Enrico Rosd6efa972014-12-02 19:49:59 -080096 mImageView.setImageResource(R.drawable.ic_deco_favicon_normal);
The Android Open Source Project0c908882009-03-03 19:32:16 -080097 }
98 }
99
John Reck0ce8a942011-01-14 19:57:09 -0800100 void setFaviconBackground(Drawable d) {
101 mImageView.setBackgroundDrawable(d);
102 }
103
The Android Open Source Project0c908882009-03-03 19:32:16 -0800104 /**
105 * Set the new name for the bookmark item.
106 *
107 * @param name The new name for the bookmark item.
108 */
109 /* package */ void setName(String name) {
Ben Murdoch9907efc2009-10-28 13:22:46 +0000110 if (name == null) {
111 return;
112 }
113
114 mTitle = name;
115
John Reck35e9dd62011-04-25 09:01:54 -0700116 if (name.length() > MAX_TEXTVIEW_LEN) {
117 name = name.substring(0, MAX_TEXTVIEW_LEN);
Ben Murdoch9907efc2009-10-28 13:22:46 +0000118 }
119
The Android Open Source Project0c908882009-03-03 19:32:16 -0800120 mTextView.setText(name);
121 }
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800122
The Android Open Source Project0c908882009-03-03 19:32:16 -0800123 /**
124 * Set the new url for the bookmark item.
125 * @param url The new url for the bookmark item.
126 */
127 /* package */ void setUrl(String url) {
Ben Murdoch9907efc2009-10-28 13:22:46 +0000128 if (url == null) {
129 return;
130 }
131
The Android Open Source Project0c908882009-03-03 19:32:16 -0800132 mUrl = url;
Ben Murdoch9907efc2009-10-28 13:22:46 +0000133
John Reck83c01512011-09-09 11:14:16 -0700134 url = UrlUtils.stripUrl(url);
John Reck35e9dd62011-04-25 09:01:54 -0700135 if (url.length() > MAX_TEXTVIEW_LEN) {
136 url = url.substring(0, MAX_TEXTVIEW_LEN);
Ben Murdoch9907efc2009-10-28 13:22:46 +0000137 }
138
139 mUrlText.setText(url);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800140 }
John Reck83c01512011-09-09 11:14:16 -0700141
142 void setEnableScrolling(boolean enable) {
143 mEnableScrolling = enable;
144 setFocusable(mEnableScrolling);
145 setFocusableInTouchMode(mEnableScrolling);
146 requestDisallowInterceptTouchEvent(!mEnableScrolling);
147 requestLayout();
148 }
149
150 @Override
151 public boolean onTouchEvent(MotionEvent ev) {
152 if (mEnableScrolling) {
153 return super.onTouchEvent(ev);
154 }
155 return false;
156 }
157
158 @Override
159 protected void measureChild(View child, int parentWidthMeasureSpec,
160 int parentHeightMeasureSpec) {
161 if (mEnableScrolling) {
162 super.measureChild(child, parentWidthMeasureSpec, parentHeightMeasureSpec);
163 return;
164 }
165
166 final ViewGroup.LayoutParams lp = child.getLayoutParams();
167
168 final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800169 getPaddingLeft() + getPaddingRight(), lp.width);
John Reck83c01512011-09-09 11:14:16 -0700170 final int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800171 getPaddingTop() + getPaddingBottom(), lp.height);
John Reck83c01512011-09-09 11:14:16 -0700172
173 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
174 }
175
176 @Override
177 protected void measureChildWithMargins(View child,
178 int parentWidthMeasureSpec, int widthUsed,
179 int parentHeightMeasureSpec, int heightUsed) {
180 if (mEnableScrolling) {
181 super.measureChildWithMargins(child, parentWidthMeasureSpec,
182 widthUsed, parentHeightMeasureSpec, heightUsed);
183 return;
184 }
185
186 final MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
187
188 final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800189 getPaddingLeft() + getPaddingRight() + lp.leftMargin + lp.rightMargin
John Reck83c01512011-09-09 11:14:16 -0700190 + widthUsed, lp.width);
191 final int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec,
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800192 getPaddingTop() + getPaddingBottom() + lp.topMargin + lp.bottomMargin
John Reck83c01512011-09-09 11:14:16 -0700193 + heightUsed, lp.height);
194
195 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
196 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800197}