blob: b86073bc4b661cab1d924617accb25f99847f585 [file] [log] [blame]
Leon Scroggins81db3662009-06-04 17:45:11 -04001/*
2 * Copyright (C) 2009 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;
Leon Scroggins11e71b12010-01-25 10:40:38 -050020import android.content.Intent;
21import android.content.pm.PackageManager;
22import android.content.pm.ResolveInfo;
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -040023import android.content.res.Resources;
24import android.graphics.Bitmap;
25import android.graphics.Color;
Leon Scroggins1f005d32009-08-10 17:36:42 -040026import android.graphics.Rect;
Leon Scroggins62e8f942009-09-03 15:08:54 -040027import android.graphics.drawable.Animatable;
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -040028import android.graphics.drawable.BitmapDrawable;
Leon Scroggins81db3662009-06-04 17:45:11 -040029import android.graphics.drawable.Drawable;
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -040030import android.graphics.drawable.LayerDrawable;
31import android.graphics.drawable.PaintDrawable;
Leon Scrogginsc6fa1102009-09-21 10:40:01 -040032import android.os.Handler;
33import android.os.Message;
Leon Scroggins11e71b12010-01-25 10:40:38 -050034import android.speech.RecognizerIntent;
Leon Scrogginsf48d44e2010-02-05 16:41:49 -050035import android.text.SpannableString;
36import android.text.Spanned;
37import android.text.style.ImageSpan;
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -040038import android.util.TypedValue;
Leon Scrogginsc6fa1102009-09-21 10:40:01 -040039import android.view.ContextMenu;
Leon Scrogginse4b3bda2009-06-09 15:46:41 -040040import android.view.LayoutInflater;
Leon Scrogginsc6fa1102009-09-21 10:40:01 -040041import android.view.MenuInflater;
Leon Scroggins68579392009-09-15 15:31:54 -040042import android.view.MotionEvent;
Leon Scrogginse4b3bda2009-06-09 15:46:41 -040043import android.view.View;
Leon Scroggins68579392009-09-15 15:31:54 -040044import android.view.ViewConfiguration;
Leon Scroggins81db3662009-06-04 17:45:11 -040045import android.widget.ImageView;
46import android.widget.LinearLayout;
47import android.widget.ProgressBar;
48import android.widget.TextView;
Leon Scroggins81db3662009-06-04 17:45:11 -040049
Leon Scroggins1f005d32009-08-10 17:36:42 -040050/**
51 * This class represents a title bar for a particular "tab" or "window" in the
52 * browser.
53 */
Leon Scrogginse4b3bda2009-06-09 15:46:41 -040054public class TitleBar extends LinearLayout {
Leon Scroggins81db3662009-06-04 17:45:11 -040055 private TextView mTitle;
Leon Scroggins1f005d32009-08-10 17:36:42 -040056 private Drawable mCloseDrawable;
57 private ImageView mRtButton;
Leon Scroggins62e8f942009-09-03 15:08:54 -040058 private Drawable mCircularProgress;
Leon Scroggins81db3662009-06-04 17:45:11 -040059 private ProgressBar mHorizontalProgress;
Leon Scrogginsf4bb18a2009-09-11 18:37:53 -040060 private ImageView mFavicon;
Leon Scroggins62e8f942009-09-03 15:08:54 -040061 private ImageView mLockIcon;
Leon Scrogginsa81a7642009-08-31 17:05:41 -040062 private Drawable mStopDrawable;
63 private Drawable mBookmarkDrawable;
Leon Scroggins11e71b12010-01-25 10:40:38 -050064 private Drawable mVoiceDrawable;
Leon Scroggins81db3662009-06-04 17:45:11 -040065 private boolean mInLoad;
Leon Scrogginsa81a7642009-08-31 17:05:41 -040066 private BrowserActivity mBrowserActivity;
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -040067 private Drawable mGenericFavicon;
68 private int mIconDimension;
Leon Scroggins68579392009-09-15 15:31:54 -040069 private View mTitleBg;
Leon Scrogginsc6fa1102009-09-21 10:40:01 -040070 private MyHandler mHandler;
Leon Scroggins11e71b12010-01-25 10:40:38 -050071 private Intent mVoiceSearchIntent;
72 private boolean mInVoiceMode;
Leon Scroggins58d56c62010-01-28 15:12:40 -050073 private Drawable mVoiceModeBackground;
74 private Drawable mNormalBackground;
Leon Scrogginsf48d44e2010-02-05 16:41:49 -050075 private ImageSpan mArcsSpan;
Leon Scrogginsc6fa1102009-09-21 10:40:01 -040076
77 private static int LONG_PRESS = 1;
Leon Scroggins81db3662009-06-04 17:45:11 -040078
Leon Scroggins68579392009-09-15 15:31:54 -040079 public TitleBar(BrowserActivity context) {
Leon Scroggins1f005d32009-08-10 17:36:42 -040080 super(context, null);
Leon Scrogginsc6fa1102009-09-21 10:40:01 -040081 mHandler = new MyHandler();
Leon Scroggins81db3662009-06-04 17:45:11 -040082 LayoutInflater factory = LayoutInflater.from(context);
83 factory.inflate(R.layout.title_bar, this);
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -040084 mBrowserActivity = context;
Leon Scroggins81db3662009-06-04 17:45:11 -040085
86 mTitle = (TextView) findViewById(R.id.title);
Leon Scrogginsa81a7642009-08-31 17:05:41 -040087 mTitle.setCompoundDrawablePadding(5);
Leon Scroggins81db3662009-06-04 17:45:11 -040088
Leon Scroggins68579392009-09-15 15:31:54 -040089 mTitleBg = findViewById(R.id.title_bg);
Leon Scroggins62e8f942009-09-03 15:08:54 -040090 mLockIcon = (ImageView) findViewById(R.id.lock);
Leon Scrogginsf4bb18a2009-09-11 18:37:53 -040091 mFavicon = (ImageView) findViewById(R.id.favicon);
Leon Scroggins62e8f942009-09-03 15:08:54 -040092
Leon Scrogginsa81a7642009-08-31 17:05:41 -040093 mRtButton = (ImageView) findViewById(R.id.rt_btn);
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -040094 Resources resources = context.getResources();
95 mCircularProgress = (Drawable) resources.getDrawable(
Leon Scroggins62e8f942009-09-03 15:08:54 -040096 com.android.internal.R.drawable.search_spinner);
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -040097 mIconDimension = (int) TypedValue.applyDimension(
98 TypedValue.COMPLEX_UNIT_DIP, 20f,
99 resources.getDisplayMetrics());
100 mCircularProgress.setBounds(0, 0, mIconDimension, mIconDimension);
Leon Scroggins81db3662009-06-04 17:45:11 -0400101 mHorizontalProgress = (ProgressBar) findViewById(
102 R.id.progress_horizontal);
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400103 mGenericFavicon = context.getResources().getDrawable(
104 R.drawable.app_web_browser_sm);
Leon Scroggins11e71b12010-01-25 10:40:38 -0500105 mVoiceSearchIntent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
106 mVoiceSearchIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
107 RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);
108 PackageManager pm = context.getPackageManager();
109 ResolveInfo ri = pm.resolveActivity(mVoiceSearchIntent,
110 PackageManager.MATCH_DEFAULT_ONLY);
111 if (ri == null) {
112 mVoiceSearchIntent = null;
113 } else {
114 mVoiceDrawable = resources.getDrawable(
115 android.R.drawable.ic_btn_speak_now);
116 }
117 mStopDrawable = resources.getDrawable(R.drawable.ic_btn_stop_v2);
118 mBookmarkDrawable = mRtButton.getDrawable();
Leon Scroggins58d56c62010-01-28 15:12:40 -0500119 mVoiceModeBackground = resources.getDrawable(
Leon Scrogginsf48d44e2010-02-05 16:41:49 -0500120 R.drawable.title_voice);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500121 mNormalBackground = mTitleBg.getBackground();
Leon Scrogginsf48d44e2010-02-05 16:41:49 -0500122 mArcsSpan = new ImageSpan(context, R.drawable.arcs,
123 ImageSpan.ALIGN_BASELINE);
Leon Scrogginse4b3bda2009-06-09 15:46:41 -0400124 }
125
Leon Scrogginsc6fa1102009-09-21 10:40:01 -0400126 private class MyHandler extends Handler {
127 public void handleMessage(Message msg) {
128 if (msg.what == LONG_PRESS) {
129 // Prevent the normal action from happening by setting the title
130 // bar's state to false.
131 mTitleBg.setPressed(false);
132 // Need to call a special method on BrowserActivity for when the
133 // fake title bar is up, because its ViewGroup does not show a
134 // context menu.
135 mBrowserActivity.showTitleBarContextMenu();
136 }
137 }
138 };
139
140 @Override
141 protected void onCreateContextMenu(ContextMenu menu) {
142 MenuInflater inflater = mBrowserActivity.getMenuInflater();
143 inflater.inflate(R.menu.title_context, menu);
144 }
145
Leon Scroggins68579392009-09-15 15:31:54 -0400146 @Override
147 public boolean onTouchEvent(MotionEvent event) {
148 switch (event.getAction()) {
149 case MotionEvent.ACTION_DOWN:
150 // Make all touches hit either the textfield or the button,
151 // depending on which side of the right edge of the textfield
152 // they hit.
153 if ((int) event.getX() > mTitleBg.getRight()) {
154 mRtButton.setPressed(true);
155 } else {
156 mTitleBg.setPressed(true);
Leon Scrogginsc6fa1102009-09-21 10:40:01 -0400157 mHandler.sendMessageDelayed(mHandler.obtainMessage(
158 LONG_PRESS),
159 ViewConfiguration.getLongPressTimeout());
Leon Scroggins68579392009-09-15 15:31:54 -0400160 }
161 break;
162 case MotionEvent.ACTION_MOVE:
163 int slop = ViewConfiguration.get(mBrowserActivity)
164 .getScaledTouchSlop();
165 if ((int) event.getY() > getHeight() + slop) {
166 // We only trigger the actions in ACTION_UP if one or the
167 // other is pressed. Since the user moved off the title
168 // bar, mark both as not pressed.
169 mTitleBg.setPressed(false);
170 mRtButton.setPressed(false);
Leon Scrogginsc6fa1102009-09-21 10:40:01 -0400171 mHandler.removeMessages(LONG_PRESS);
Leon Scroggins68579392009-09-15 15:31:54 -0400172 break;
173 }
174 int x = (int) event.getX();
175 int titleRight = mTitleBg.getRight();
176 if (mTitleBg.isPressed() && x > titleRight + slop) {
177 mTitleBg.setPressed(false);
Leon Scrogginsc6fa1102009-09-21 10:40:01 -0400178 mHandler.removeMessages(LONG_PRESS);
Leon Scroggins68579392009-09-15 15:31:54 -0400179 } else if (mRtButton.isPressed() && x < titleRight - slop) {
180 mRtButton.setPressed(false);
181 }
182 break;
183 case MotionEvent.ACTION_CANCEL:
184 mRtButton.setPressed(false);
185 mTitleBg.setPressed(false);
Leon Scrogginsc6fa1102009-09-21 10:40:01 -0400186 mHandler.removeMessages(LONG_PRESS);
Leon Scroggins68579392009-09-15 15:31:54 -0400187 break;
188 case MotionEvent.ACTION_UP:
189 if (mRtButton.isPressed()) {
Leon Scroggins11e71b12010-01-25 10:40:38 -0500190 if (mInVoiceMode) {
191 mBrowserActivity.startActivity(mVoiceSearchIntent);
192 } else if (mInLoad) {
Leon Scroggins68579392009-09-15 15:31:54 -0400193 mBrowserActivity.stopLoading();
194 } else {
195 mBrowserActivity.bookmarksOrHistoryPicker(false);
196 }
197 mRtButton.setPressed(false);
198 } else if (mTitleBg.isPressed()) {
Leon Scrogginsc6fa1102009-09-21 10:40:01 -0400199 mHandler.removeMessages(LONG_PRESS);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500200 if (mInVoiceMode) {
201 mBrowserActivity.showVoiceSearchResults(
202 mTitle.getText().toString());
203 } else {
204 mBrowserActivity.onSearchRequested();
205 }
Leon Scroggins68579392009-09-15 15:31:54 -0400206 mTitleBg.setPressed(false);
207 }
208 break;
209 default:
210 break;
211 }
212 return true;
Leon Scroggins81db3662009-06-04 17:45:11 -0400213 }
214
Leon Scroggins1f005d32009-08-10 17:36:42 -0400215 /**
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400216 * Set a new Bitmap for the Favicon.
Leon Scroggins1f005d32009-08-10 17:36:42 -0400217 */
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400218 /* package */ void setFavicon(Bitmap icon) {
219 Drawable[] array = new Drawable[3];
220 array[0] = new PaintDrawable(Color.BLACK);
221 PaintDrawable p = new PaintDrawable(Color.WHITE);
222 array[1] = p;
223 if (icon == null) {
224 array[2] = mGenericFavicon;
225 } else {
226 array[2] = new BitmapDrawable(icon);
Leon Scrogginsa81a7642009-08-31 17:05:41 -0400227 }
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400228 LayerDrawable d = new LayerDrawable(array);
229 d.setLayerInset(1, 1, 1, 1, 1);
230 d.setLayerInset(2, 2, 2, 2, 2);
Leon Scrogginsf4bb18a2009-09-11 18:37:53 -0400231 mFavicon.setImageDrawable(d);
Leon Scroggins81db3662009-06-04 17:45:11 -0400232 }
233
Leon Scroggins1f005d32009-08-10 17:36:42 -0400234 /**
Leon Scroggins11e71b12010-01-25 10:40:38 -0500235 * Change the TitleBar to or from voice mode. If there is no package to
236 * handle voice search, the TitleBar cannot be set to voice mode.
237 */
238 /* package */ void setInVoiceMode(boolean inVoiceMode) {
239 if (mInVoiceMode == inVoiceMode) return;
240 mInVoiceMode = inVoiceMode && mVoiceSearchIntent != null;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500241 Drawable rightButtonDrawable, titleDrawable;
Leon Scroggins11e71b12010-01-25 10:40:38 -0500242 if (mInVoiceMode) {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500243 rightButtonDrawable = mVoiceDrawable;
244 titleDrawable = mVoiceModeBackground;
Leon Scroggins11e71b12010-01-25 10:40:38 -0500245 } else {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500246 titleDrawable = mNormalBackground;
247 if (mInLoad) {
248 rightButtonDrawable = mStopDrawable;
249 } else {
250 rightButtonDrawable = mBookmarkDrawable;
251 }
Leon Scroggins11e71b12010-01-25 10:40:38 -0500252 }
Leon Scrogginsf48d44e2010-02-05 16:41:49 -0500253 mTitle.setSingleLine(!mInVoiceMode);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500254 mTitleBg.setBackgroundDrawable(titleDrawable);
255 mRtButton.setImageDrawable(rightButtonDrawable);
Leon Scroggins11e71b12010-01-25 10:40:38 -0500256 }
257
258 /**
Leon Scroggins1f005d32009-08-10 17:36:42 -0400259 * Set the Drawable for the lock icon, or null to hide it.
260 */
Leon Scroggins81db3662009-06-04 17:45:11 -0400261 /* package */ void setLock(Drawable d) {
Leon Scroggins62e8f942009-09-03 15:08:54 -0400262 if (null == d) {
263 mLockIcon.setVisibility(View.GONE);
264 } else {
265 mLockIcon.setImageDrawable(d);
266 mLockIcon.setVisibility(View.VISIBLE);
Leon Scroggins81db3662009-06-04 17:45:11 -0400267 }
268 }
269
Leon Scroggins1f005d32009-08-10 17:36:42 -0400270 /**
271 * Update the progress, from 0 to 100.
272 */
Leon Scroggins81db3662009-06-04 17:45:11 -0400273 /* package */ void setProgress(int newProgress) {
Nicolas Roard1382b492009-09-16 21:50:06 +0100274 if (newProgress >= mHorizontalProgress.getMax()) {
Leon Scrogginsf4bb18a2009-09-11 18:37:53 -0400275 mTitle.setCompoundDrawables(null, null, null, null);
Leon Scroggins62e8f942009-09-03 15:08:54 -0400276 ((Animatable) mCircularProgress).stop();
277 mHorizontalProgress.setVisibility(View.INVISIBLE);
Leon Scroggins11e71b12010-01-25 10:40:38 -0500278 if (!mInVoiceMode) {
Leon Scrogginsa81a7642009-08-31 17:05:41 -0400279 mRtButton.setImageDrawable(mBookmarkDrawable);
Leon Scroggins1f005d32009-08-10 17:36:42 -0400280 }
Leon Scroggins81db3662009-06-04 17:45:11 -0400281 mInLoad = false;
282 } else {
Leon Scroggins81db3662009-06-04 17:45:11 -0400283 mHorizontalProgress.setProgress(newProgress);
Nicolas Roard1382b492009-09-16 21:50:06 +0100284 if (!mInLoad && getWindowToken() != null) {
285 // checking the window token lets us be sure that we
286 // are attached to a window before starting the animation,
287 // preventing a potential race condition
288 // (fix for bug http://b/2115736)
Leon Scrogginsf4bb18a2009-09-11 18:37:53 -0400289 mTitle.setCompoundDrawables(null, null, mCircularProgress,
Leon Scroggins62e8f942009-09-03 15:08:54 -0400290 null);
291 ((Animatable) mCircularProgress).start();
292 mHorizontalProgress.setVisibility(View.VISIBLE);
Leon Scroggins11e71b12010-01-25 10:40:38 -0500293 if (!mInVoiceMode) {
Leon Scroggins62e8f942009-09-03 15:08:54 -0400294 mRtButton.setImageDrawable(mStopDrawable);
295 }
296 mInLoad = true;
Leon Scrogginse4b3bda2009-06-09 15:46:41 -0400297 }
Leon Scroggins81db3662009-06-04 17:45:11 -0400298 }
299 }
300
Leon Scroggins1f005d32009-08-10 17:36:42 -0400301 /**
Leon Scroggins58d56c62010-01-28 15:12:40 -0500302 * Update the text displayed in the title bar.
303 * @param title String to display. If null, the loading string will be
304 * shown.
Leon Scroggins1f005d32009-08-10 17:36:42 -0400305 */
Leon Scroggins58d56c62010-01-28 15:12:40 -0500306 /* package */ void setDisplayTitle(String title) {
307 if (title == null) {
Leon Scrogginsa81a7642009-08-31 17:05:41 -0400308 mTitle.setText(R.string.title_bar_loading);
Leon Scrogginsd7c3dd52009-07-20 13:38:47 -0400309 } else {
Leon Scrogginsf48d44e2010-02-05 16:41:49 -0500310 if (mInVoiceMode) {
311 // Add two spaces. The second one will be replaced with an
312 // image, and the first one will put space between it and the
313 // text
314 SpannableString spannable = new SpannableString(title + " ");
315 int end = spannable.length();
316 spannable.setSpan(mArcsSpan, end - 1, end,
317 Spanned.SPAN_MARK_POINT);
318 mTitle.setText(spannable);
319 } else {
320 mTitle.setText(title);
321 }
Leon Scrogginsd7c3dd52009-07-20 13:38:47 -0400322 }
Leon Scroggins81db3662009-06-04 17:45:11 -0400323 }
324
325 /* package */ void setToTabPicker() {
326 mTitle.setText(R.string.tab_picker_title);
327 setFavicon(null);
328 setLock(null);
Leon Scroggins81db3662009-06-04 17:45:11 -0400329 mHorizontalProgress.setVisibility(View.GONE);
Leon Scroggins81db3662009-06-04 17:45:11 -0400330 }
331}