blob: 85c1fff21e0f4f5aad2d47a8a8f401936bc6476d [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
17package com.android.browser;
18
19import 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;
26import android.widget.HorizontalScrollView;
The Android Open Source Project0c908882009-03-03 19:32:16 -080027import android.widget.ImageView;
The Android Open Source Project0c908882009-03-03 19:32:16 -080028import android.widget.TextView;
29
30/**
31 * Custom layout for an item representing a bookmark in the browser.
32 */
John Reck83c01512011-09-09 11:14:16 -070033class BookmarkItem extends HorizontalScrollView {
The Android Open Source Project0c908882009-03-03 19:32:16 -080034
John Reck35e9dd62011-04-25 09:01:54 -070035 final static int MAX_TEXTVIEW_LEN = 80;
36
The Android Open Source Project0c908882009-03-03 19:32:16 -080037 protected TextView mTextView;
38 protected TextView mUrlText;
39 protected ImageView mImageView;
40 protected String mUrl;
Ben Murdoch9907efc2009-10-28 13:22:46 +000041 protected String mTitle;
John Reck83c01512011-09-09 11:14:16 -070042 protected boolean mEnableScrolling = false;
The Android Open Source Project0c908882009-03-03 19:32:16 -080043
44 /**
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);
58 mImageView = (ImageView) findViewById(R.id.favicon);
59 View star = findViewById(R.id.star);
60 star.setVisibility(View.GONE);
61 }
62
63 /**
64 * Copy this BookmarkItem to item.
65 * @param item BookmarkItem to receive the info from this BookmarkItem.
66 */
67 /* package */ void copyTo(BookmarkItem item) {
68 item.mTextView.setText(mTextView.getText());
69 item.mUrlText.setText(mUrlText.getText());
70 item.mImageView.setImageDrawable(mImageView.getDrawable());
71 }
72
73 /**
74 * Return the name assigned to this bookmark item.
75 */
76 /* package */ String getName() {
Ben Murdoch9907efc2009-10-28 13:22:46 +000077 return mTitle;
The Android Open Source Project0c908882009-03-03 19:32:16 -080078 }
79
The Android Open Source Project0c908882009-03-03 19:32:16 -080080 /* package */ String getUrl() {
81 return mUrl;
82 }
83
84 /**
85 * Set the favicon for this item.
86 *
87 * @param b The new bitmap for this item.
88 * If it is null, will use the default.
89 */
90 /* package */ void setFavicon(Bitmap b) {
91 if (b != null) {
92 mImageView.setImageBitmap(b);
93 } else {
94 mImageView.setImageResource(R.drawable.app_web_browser_sm);
95 }
96 }
97
John Reck0ce8a942011-01-14 19:57:09 -080098 void setFaviconBackground(Drawable d) {
99 mImageView.setBackgroundDrawable(d);
100 }
101
The Android Open Source Project0c908882009-03-03 19:32:16 -0800102 /**
103 * Set the new name for the bookmark item.
104 *
105 * @param name The new name for the bookmark item.
106 */
107 /* package */ void setName(String name) {
Ben Murdoch9907efc2009-10-28 13:22:46 +0000108 if (name == null) {
109 return;
110 }
111
112 mTitle = name;
113
John Reck35e9dd62011-04-25 09:01:54 -0700114 if (name.length() > MAX_TEXTVIEW_LEN) {
115 name = name.substring(0, MAX_TEXTVIEW_LEN);
Ben Murdoch9907efc2009-10-28 13:22:46 +0000116 }
117
The Android Open Source Project0c908882009-03-03 19:32:16 -0800118 mTextView.setText(name);
119 }
120
121 /**
122 * Set the new url for the bookmark item.
123 * @param url The new url for the bookmark item.
124 */
125 /* package */ void setUrl(String url) {
Ben Murdoch9907efc2009-10-28 13:22:46 +0000126 if (url == null) {
127 return;
128 }
129
The Android Open Source Project0c908882009-03-03 19:32:16 -0800130 mUrl = url;
Ben Murdoch9907efc2009-10-28 13:22:46 +0000131
John Reck83c01512011-09-09 11:14:16 -0700132 url = UrlUtils.stripUrl(url);
John Reck35e9dd62011-04-25 09:01:54 -0700133 if (url.length() > MAX_TEXTVIEW_LEN) {
134 url = url.substring(0, MAX_TEXTVIEW_LEN);
Ben Murdoch9907efc2009-10-28 13:22:46 +0000135 }
136
137 mUrlText.setText(url);
The Android Open Source Project0c908882009-03-03 19:32:16 -0800138 }
John Reck83c01512011-09-09 11:14:16 -0700139
140 void setEnableScrolling(boolean enable) {
141 mEnableScrolling = enable;
142 setFocusable(mEnableScrolling);
143 setFocusableInTouchMode(mEnableScrolling);
144 requestDisallowInterceptTouchEvent(!mEnableScrolling);
145 requestLayout();
146 }
147
148 @Override
149 public boolean onTouchEvent(MotionEvent ev) {
150 if (mEnableScrolling) {
151 return super.onTouchEvent(ev);
152 }
153 return false;
154 }
155
156 @Override
157 protected void measureChild(View child, int parentWidthMeasureSpec,
158 int parentHeightMeasureSpec) {
159 if (mEnableScrolling) {
160 super.measureChild(child, parentWidthMeasureSpec, parentHeightMeasureSpec);
161 return;
162 }
163
164 final ViewGroup.LayoutParams lp = child.getLayoutParams();
165
166 final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec,
167 mPaddingLeft + mPaddingRight, lp.width);
168 final int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec,
169 mPaddingTop + mPaddingBottom, lp.height);
170
171 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
172 }
173
174 @Override
175 protected void measureChildWithMargins(View child,
176 int parentWidthMeasureSpec, int widthUsed,
177 int parentHeightMeasureSpec, int heightUsed) {
178 if (mEnableScrolling) {
179 super.measureChildWithMargins(child, parentWidthMeasureSpec,
180 widthUsed, parentHeightMeasureSpec, heightUsed);
181 return;
182 }
183
184 final MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
185
186 final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec,
187 mPaddingLeft + mPaddingRight + lp.leftMargin + lp.rightMargin
188 + widthUsed, lp.width);
189 final int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec,
190 mPaddingTop + mPaddingBottom + lp.topMargin + lp.bottomMargin
191 + heightUsed, lp.height);
192
193 child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
194 }
The Android Open Source Project0c908882009-03-03 19:32:16 -0800195}