blob: 844d5a1ecd283f47185fbdbb4e1dedfa0c082a55 [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;
Leon Scroggins76e16862010-02-08 14:39:34 -050037import android.text.TextUtils;
Leon Scrogginsf48d44e2010-02-05 16:41:49 -050038import android.text.style.ImageSpan;
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -040039import android.util.TypedValue;
Leon Scrogginsc6fa1102009-09-21 10:40:01 -040040import android.view.ContextMenu;
Leon Scrogginse4b3bda2009-06-09 15:46:41 -040041import android.view.LayoutInflater;
Leon Scrogginsc6fa1102009-09-21 10:40:01 -040042import android.view.MenuInflater;
Leon Scroggins68579392009-09-15 15:31:54 -040043import android.view.MotionEvent;
Leon Scrogginse4b3bda2009-06-09 15:46:41 -040044import android.view.View;
Leon Scroggins68579392009-09-15 15:31:54 -040045import android.view.ViewConfiguration;
Leon Scroggins81db3662009-06-04 17:45:11 -040046import android.widget.ImageView;
47import android.widget.LinearLayout;
48import android.widget.ProgressBar;
49import android.widget.TextView;
Leon Scroggins81db3662009-06-04 17:45:11 -040050
Leon Scroggins1fe13a52010-02-09 15:31:26 -050051import com.android.common.speech.LoggingEvents;
52
Leon Scroggins1f005d32009-08-10 17:36:42 -040053/**
54 * This class represents a title bar for a particular "tab" or "window" in the
55 * browser.
56 */
Leon Scrogginse4b3bda2009-06-09 15:46:41 -040057public class TitleBar extends LinearLayout {
Leon Scroggins81db3662009-06-04 17:45:11 -040058 private TextView mTitle;
Leon Scroggins1f005d32009-08-10 17:36:42 -040059 private Drawable mCloseDrawable;
60 private ImageView mRtButton;
Leon Scroggins62e8f942009-09-03 15:08:54 -040061 private Drawable mCircularProgress;
Leon Scroggins81db3662009-06-04 17:45:11 -040062 private ProgressBar mHorizontalProgress;
Leon Scrogginsf4bb18a2009-09-11 18:37:53 -040063 private ImageView mFavicon;
Leon Scroggins62e8f942009-09-03 15:08:54 -040064 private ImageView mLockIcon;
Leon Scrogginsa81a7642009-08-31 17:05:41 -040065 private Drawable mStopDrawable;
66 private Drawable mBookmarkDrawable;
Leon Scroggins11e71b12010-01-25 10:40:38 -050067 private Drawable mVoiceDrawable;
Leon Scroggins81db3662009-06-04 17:45:11 -040068 private boolean mInLoad;
Leon Scrogginsa81a7642009-08-31 17:05:41 -040069 private BrowserActivity mBrowserActivity;
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -040070 private Drawable mGenericFavicon;
71 private int mIconDimension;
Leon Scroggins68579392009-09-15 15:31:54 -040072 private View mTitleBg;
Leon Scrogginsc6fa1102009-09-21 10:40:01 -040073 private MyHandler mHandler;
Leon Scroggins11e71b12010-01-25 10:40:38 -050074 private Intent mVoiceSearchIntent;
75 private boolean mInVoiceMode;
Leon Scroggins58d56c62010-01-28 15:12:40 -050076 private Drawable mVoiceModeBackground;
77 private Drawable mNormalBackground;
Leon Scrogginsf48d44e2010-02-05 16:41:49 -050078 private ImageSpan mArcsSpan;
Leon Scrogginsc6fa1102009-09-21 10:40:01 -040079
80 private static int LONG_PRESS = 1;
Leon Scroggins81db3662009-06-04 17:45:11 -040081
Leon Scroggins68579392009-09-15 15:31:54 -040082 public TitleBar(BrowserActivity context) {
Leon Scroggins1f005d32009-08-10 17:36:42 -040083 super(context, null);
Leon Scrogginsc6fa1102009-09-21 10:40:01 -040084 mHandler = new MyHandler();
Leon Scroggins81db3662009-06-04 17:45:11 -040085 LayoutInflater factory = LayoutInflater.from(context);
86 factory.inflate(R.layout.title_bar, this);
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -040087 mBrowserActivity = context;
Leon Scroggins81db3662009-06-04 17:45:11 -040088
89 mTitle = (TextView) findViewById(R.id.title);
Leon Scrogginsa81a7642009-08-31 17:05:41 -040090 mTitle.setCompoundDrawablePadding(5);
Leon Scroggins81db3662009-06-04 17:45:11 -040091
Leon Scroggins68579392009-09-15 15:31:54 -040092 mTitleBg = findViewById(R.id.title_bg);
Leon Scroggins62e8f942009-09-03 15:08:54 -040093 mLockIcon = (ImageView) findViewById(R.id.lock);
Leon Scrogginsf4bb18a2009-09-11 18:37:53 -040094 mFavicon = (ImageView) findViewById(R.id.favicon);
Leon Scroggins62e8f942009-09-03 15:08:54 -040095
Leon Scrogginsa81a7642009-08-31 17:05:41 -040096 mRtButton = (ImageView) findViewById(R.id.rt_btn);
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -040097 Resources resources = context.getResources();
98 mCircularProgress = (Drawable) resources.getDrawable(
Leon Scroggins62e8f942009-09-03 15:08:54 -040099 com.android.internal.R.drawable.search_spinner);
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400100 mIconDimension = (int) TypedValue.applyDimension(
101 TypedValue.COMPLEX_UNIT_DIP, 20f,
102 resources.getDisplayMetrics());
103 mCircularProgress.setBounds(0, 0, mIconDimension, mIconDimension);
Leon Scroggins81db3662009-06-04 17:45:11 -0400104 mHorizontalProgress = (ProgressBar) findViewById(
105 R.id.progress_horizontal);
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400106 mGenericFavicon = context.getResources().getDrawable(
107 R.drawable.app_web_browser_sm);
Leon Scroggins11e71b12010-01-25 10:40:38 -0500108 mVoiceSearchIntent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
109 mVoiceSearchIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
110 RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);
111 PackageManager pm = context.getPackageManager();
112 ResolveInfo ri = pm.resolveActivity(mVoiceSearchIntent,
113 PackageManager.MATCH_DEFAULT_ONLY);
114 if (ri == null) {
115 mVoiceSearchIntent = null;
116 } else {
117 mVoiceDrawable = resources.getDrawable(
118 android.R.drawable.ic_btn_speak_now);
119 }
120 mStopDrawable = resources.getDrawable(R.drawable.ic_btn_stop_v2);
121 mBookmarkDrawable = mRtButton.getDrawable();
Leon Scroggins58d56c62010-01-28 15:12:40 -0500122 mVoiceModeBackground = resources.getDrawable(
Leon Scrogginsf48d44e2010-02-05 16:41:49 -0500123 R.drawable.title_voice);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500124 mNormalBackground = mTitleBg.getBackground();
Leon Scrogginsf48d44e2010-02-05 16:41:49 -0500125 mArcsSpan = new ImageSpan(context, R.drawable.arcs,
126 ImageSpan.ALIGN_BASELINE);
Leon Scrogginse4b3bda2009-06-09 15:46:41 -0400127 }
128
Leon Scrogginsc6fa1102009-09-21 10:40:01 -0400129 private class MyHandler extends Handler {
130 public void handleMessage(Message msg) {
131 if (msg.what == LONG_PRESS) {
132 // Prevent the normal action from happening by setting the title
133 // bar's state to false.
134 mTitleBg.setPressed(false);
135 // Need to call a special method on BrowserActivity for when the
136 // fake title bar is up, because its ViewGroup does not show a
137 // context menu.
138 mBrowserActivity.showTitleBarContextMenu();
139 }
140 }
141 };
142
143 @Override
144 protected void onCreateContextMenu(ContextMenu menu) {
145 MenuInflater inflater = mBrowserActivity.getMenuInflater();
146 inflater.inflate(R.menu.title_context, menu);
147 }
148
Leon Scroggins68579392009-09-15 15:31:54 -0400149 @Override
150 public boolean onTouchEvent(MotionEvent event) {
151 switch (event.getAction()) {
152 case MotionEvent.ACTION_DOWN:
153 // Make all touches hit either the textfield or the button,
154 // depending on which side of the right edge of the textfield
155 // they hit.
156 if ((int) event.getX() > mTitleBg.getRight()) {
157 mRtButton.setPressed(true);
158 } else {
159 mTitleBg.setPressed(true);
Leon Scrogginsc6fa1102009-09-21 10:40:01 -0400160 mHandler.sendMessageDelayed(mHandler.obtainMessage(
161 LONG_PRESS),
162 ViewConfiguration.getLongPressTimeout());
Leon Scroggins68579392009-09-15 15:31:54 -0400163 }
164 break;
165 case MotionEvent.ACTION_MOVE:
166 int slop = ViewConfiguration.get(mBrowserActivity)
167 .getScaledTouchSlop();
168 if ((int) event.getY() > getHeight() + slop) {
169 // We only trigger the actions in ACTION_UP if one or the
170 // other is pressed. Since the user moved off the title
171 // bar, mark both as not pressed.
172 mTitleBg.setPressed(false);
173 mRtButton.setPressed(false);
Leon Scrogginsc6fa1102009-09-21 10:40:01 -0400174 mHandler.removeMessages(LONG_PRESS);
Leon Scroggins68579392009-09-15 15:31:54 -0400175 break;
176 }
177 int x = (int) event.getX();
178 int titleRight = mTitleBg.getRight();
179 if (mTitleBg.isPressed() && x > titleRight + slop) {
180 mTitleBg.setPressed(false);
Leon Scrogginsc6fa1102009-09-21 10:40:01 -0400181 mHandler.removeMessages(LONG_PRESS);
Leon Scroggins68579392009-09-15 15:31:54 -0400182 } else if (mRtButton.isPressed() && x < titleRight - slop) {
183 mRtButton.setPressed(false);
184 }
185 break;
186 case MotionEvent.ACTION_CANCEL:
187 mRtButton.setPressed(false);
188 mTitleBg.setPressed(false);
Leon Scrogginsc6fa1102009-09-21 10:40:01 -0400189 mHandler.removeMessages(LONG_PRESS);
Leon Scroggins68579392009-09-15 15:31:54 -0400190 break;
191 case MotionEvent.ACTION_UP:
192 if (mRtButton.isPressed()) {
Leon Scroggins11e71b12010-01-25 10:40:38 -0500193 if (mInVoiceMode) {
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500194 if (mBrowserActivity.getTabControl().getCurrentTab()
195 .voiceSearchSourceIsGoogle()) {
196 Intent i = new Intent(
197 LoggingEvents.ACTION_LOG_EVENT);
198 i.putExtra(LoggingEvents.EXTRA_EVENT,
199 LoggingEvents.VoiceSearch.RETRY);
200 mBrowserActivity.sendBroadcast(i);
201 }
Leon Scroggins11e71b12010-01-25 10:40:38 -0500202 mBrowserActivity.startActivity(mVoiceSearchIntent);
203 } else if (mInLoad) {
Leon Scroggins68579392009-09-15 15:31:54 -0400204 mBrowserActivity.stopLoading();
205 } else {
206 mBrowserActivity.bookmarksOrHistoryPicker(false);
207 }
208 mRtButton.setPressed(false);
209 } else if (mTitleBg.isPressed()) {
Leon Scrogginsc6fa1102009-09-21 10:40:01 -0400210 mHandler.removeMessages(LONG_PRESS);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500211 if (mInVoiceMode) {
Leon Scroggins1fe13a52010-02-09 15:31:26 -0500212 if (mBrowserActivity.getTabControl().getCurrentTab()
213 .voiceSearchSourceIsGoogle()) {
214 Intent i = new Intent(
215 LoggingEvents.ACTION_LOG_EVENT);
216 i.putExtra(LoggingEvents.EXTRA_EVENT,
217 LoggingEvents.VoiceSearch.N_BEST_REVEAL);
218 mBrowserActivity.sendBroadcast(i);
219 }
Leon Scroggins58d56c62010-01-28 15:12:40 -0500220 mBrowserActivity.showVoiceSearchResults(
Leon Scrogginsea754072010-02-08 14:08:30 -0500221 mTitle.getText().toString().trim());
Leon Scroggins58d56c62010-01-28 15:12:40 -0500222 } else {
223 mBrowserActivity.onSearchRequested();
224 }
Leon Scroggins68579392009-09-15 15:31:54 -0400225 mTitleBg.setPressed(false);
226 }
227 break;
228 default:
229 break;
230 }
231 return true;
Leon Scroggins81db3662009-06-04 17:45:11 -0400232 }
233
Leon Scroggins1f005d32009-08-10 17:36:42 -0400234 /**
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400235 * Set a new Bitmap for the Favicon.
Leon Scroggins1f005d32009-08-10 17:36:42 -0400236 */
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400237 /* package */ void setFavicon(Bitmap icon) {
238 Drawable[] array = new Drawable[3];
239 array[0] = new PaintDrawable(Color.BLACK);
240 PaintDrawable p = new PaintDrawable(Color.WHITE);
241 array[1] = p;
242 if (icon == null) {
243 array[2] = mGenericFavicon;
244 } else {
245 array[2] = new BitmapDrawable(icon);
Leon Scrogginsa81a7642009-08-31 17:05:41 -0400246 }
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400247 LayerDrawable d = new LayerDrawable(array);
248 d.setLayerInset(1, 1, 1, 1, 1);
249 d.setLayerInset(2, 2, 2, 2, 2);
Leon Scrogginsf4bb18a2009-09-11 18:37:53 -0400250 mFavicon.setImageDrawable(d);
Leon Scroggins81db3662009-06-04 17:45:11 -0400251 }
252
Leon Scroggins1f005d32009-08-10 17:36:42 -0400253 /**
Leon Scroggins11e71b12010-01-25 10:40:38 -0500254 * Change the TitleBar to or from voice mode. If there is no package to
255 * handle voice search, the TitleBar cannot be set to voice mode.
256 */
257 /* package */ void setInVoiceMode(boolean inVoiceMode) {
258 if (mInVoiceMode == inVoiceMode) return;
259 mInVoiceMode = inVoiceMode && mVoiceSearchIntent != null;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500260 Drawable rightButtonDrawable, titleDrawable;
Leon Scroggins11e71b12010-01-25 10:40:38 -0500261 if (mInVoiceMode) {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500262 rightButtonDrawable = mVoiceDrawable;
263 titleDrawable = mVoiceModeBackground;
Leon Scroggins76e16862010-02-08 14:39:34 -0500264 mTitle.setEllipsize(null);
Leon Scroggins11e71b12010-01-25 10:40:38 -0500265 } else {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500266 titleDrawable = mNormalBackground;
267 if (mInLoad) {
268 rightButtonDrawable = mStopDrawable;
269 } else {
270 rightButtonDrawable = mBookmarkDrawable;
271 }
Leon Scroggins76e16862010-02-08 14:39:34 -0500272 mTitle.setEllipsize(TextUtils.TruncateAt.END);
Leon Scroggins11e71b12010-01-25 10:40:38 -0500273 }
Leon Scrogginsf48d44e2010-02-05 16:41:49 -0500274 mTitle.setSingleLine(!mInVoiceMode);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500275 mTitleBg.setBackgroundDrawable(titleDrawable);
276 mRtButton.setImageDrawable(rightButtonDrawable);
Leon Scroggins11e71b12010-01-25 10:40:38 -0500277 }
278
279 /**
Leon Scroggins1f005d32009-08-10 17:36:42 -0400280 * Set the Drawable for the lock icon, or null to hide it.
281 */
Leon Scroggins81db3662009-06-04 17:45:11 -0400282 /* package */ void setLock(Drawable d) {
Leon Scroggins62e8f942009-09-03 15:08:54 -0400283 if (null == d) {
284 mLockIcon.setVisibility(View.GONE);
285 } else {
286 mLockIcon.setImageDrawable(d);
287 mLockIcon.setVisibility(View.VISIBLE);
Leon Scroggins81db3662009-06-04 17:45:11 -0400288 }
289 }
290
Leon Scroggins1f005d32009-08-10 17:36:42 -0400291 /**
292 * Update the progress, from 0 to 100.
293 */
Leon Scroggins81db3662009-06-04 17:45:11 -0400294 /* package */ void setProgress(int newProgress) {
Nicolas Roard1382b492009-09-16 21:50:06 +0100295 if (newProgress >= mHorizontalProgress.getMax()) {
Leon Scrogginsf4bb18a2009-09-11 18:37:53 -0400296 mTitle.setCompoundDrawables(null, null, null, null);
Leon Scroggins62e8f942009-09-03 15:08:54 -0400297 ((Animatable) mCircularProgress).stop();
298 mHorizontalProgress.setVisibility(View.INVISIBLE);
Leon Scroggins11e71b12010-01-25 10:40:38 -0500299 if (!mInVoiceMode) {
Leon Scrogginsa81a7642009-08-31 17:05:41 -0400300 mRtButton.setImageDrawable(mBookmarkDrawable);
Leon Scroggins1f005d32009-08-10 17:36:42 -0400301 }
Leon Scroggins81db3662009-06-04 17:45:11 -0400302 mInLoad = false;
303 } else {
Leon Scroggins81db3662009-06-04 17:45:11 -0400304 mHorizontalProgress.setProgress(newProgress);
Nicolas Roard1382b492009-09-16 21:50:06 +0100305 if (!mInLoad && getWindowToken() != null) {
306 // checking the window token lets us be sure that we
307 // are attached to a window before starting the animation,
308 // preventing a potential race condition
309 // (fix for bug http://b/2115736)
Leon Scrogginsf4bb18a2009-09-11 18:37:53 -0400310 mTitle.setCompoundDrawables(null, null, mCircularProgress,
Leon Scroggins62e8f942009-09-03 15:08:54 -0400311 null);
312 ((Animatable) mCircularProgress).start();
313 mHorizontalProgress.setVisibility(View.VISIBLE);
Leon Scroggins11e71b12010-01-25 10:40:38 -0500314 if (!mInVoiceMode) {
Leon Scroggins62e8f942009-09-03 15:08:54 -0400315 mRtButton.setImageDrawable(mStopDrawable);
316 }
317 mInLoad = true;
Leon Scrogginse4b3bda2009-06-09 15:46:41 -0400318 }
Leon Scroggins81db3662009-06-04 17:45:11 -0400319 }
320 }
321
Leon Scroggins1f005d32009-08-10 17:36:42 -0400322 /**
Leon Scroggins58d56c62010-01-28 15:12:40 -0500323 * Update the text displayed in the title bar.
324 * @param title String to display. If null, the loading string will be
325 * shown.
Leon Scroggins1f005d32009-08-10 17:36:42 -0400326 */
Leon Scroggins58d56c62010-01-28 15:12:40 -0500327 /* package */ void setDisplayTitle(String title) {
328 if (title == null) {
Leon Scrogginsa81a7642009-08-31 17:05:41 -0400329 mTitle.setText(R.string.title_bar_loading);
Leon Scrogginsd7c3dd52009-07-20 13:38:47 -0400330 } else {
Leon Scrogginsf48d44e2010-02-05 16:41:49 -0500331 if (mInVoiceMode) {
332 // Add two spaces. The second one will be replaced with an
333 // image, and the first one will put space between it and the
334 // text
335 SpannableString spannable = new SpannableString(title + " ");
336 int end = spannable.length();
337 spannable.setSpan(mArcsSpan, end - 1, end,
338 Spanned.SPAN_MARK_POINT);
339 mTitle.setText(spannable);
340 } else {
341 mTitle.setText(title);
342 }
Leon Scrogginsd7c3dd52009-07-20 13:38:47 -0400343 }
Leon Scroggins81db3662009-06-04 17:45:11 -0400344 }
345
346 /* package */ void setToTabPicker() {
347 mTitle.setText(R.string.tab_picker_title);
348 setFavicon(null);
349 setLock(null);
Leon Scroggins81db3662009-06-04 17:45:11 -0400350 mHorizontalProgress.setVisibility(View.GONE);
Leon Scroggins81db3662009-06-04 17:45:11 -0400351 }
352}