blob: 14ac2aeb96e0c332c62593e52a12a359b56d404d [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 Scroggins1f005d32009-08-10 17:36:42 -040051/**
52 * This class represents a title bar for a particular "tab" or "window" in the
53 * browser.
54 */
Leon Scrogginse4b3bda2009-06-09 15:46:41 -040055public class TitleBar extends LinearLayout {
Leon Scroggins81db3662009-06-04 17:45:11 -040056 private TextView mTitle;
Leon Scroggins1f005d32009-08-10 17:36:42 -040057 private Drawable mCloseDrawable;
58 private ImageView mRtButton;
Leon Scroggins62e8f942009-09-03 15:08:54 -040059 private Drawable mCircularProgress;
Leon Scroggins81db3662009-06-04 17:45:11 -040060 private ProgressBar mHorizontalProgress;
Leon Scrogginsf4bb18a2009-09-11 18:37:53 -040061 private ImageView mFavicon;
Leon Scroggins62e8f942009-09-03 15:08:54 -040062 private ImageView mLockIcon;
Leon Scrogginsa81a7642009-08-31 17:05:41 -040063 private Drawable mStopDrawable;
64 private Drawable mBookmarkDrawable;
Leon Scroggins11e71b12010-01-25 10:40:38 -050065 private Drawable mVoiceDrawable;
Leon Scroggins81db3662009-06-04 17:45:11 -040066 private boolean mInLoad;
Leon Scrogginsa81a7642009-08-31 17:05:41 -040067 private BrowserActivity mBrowserActivity;
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -040068 private Drawable mGenericFavicon;
69 private int mIconDimension;
Leon Scroggins68579392009-09-15 15:31:54 -040070 private View mTitleBg;
Leon Scrogginsc6fa1102009-09-21 10:40:01 -040071 private MyHandler mHandler;
Leon Scroggins11e71b12010-01-25 10:40:38 -050072 private Intent mVoiceSearchIntent;
73 private boolean mInVoiceMode;
Leon Scroggins58d56c62010-01-28 15:12:40 -050074 private Drawable mVoiceModeBackground;
75 private Drawable mNormalBackground;
Leon Scrogginsf48d44e2010-02-05 16:41:49 -050076 private ImageSpan mArcsSpan;
Leon Scrogginsc6fa1102009-09-21 10:40:01 -040077
78 private static int LONG_PRESS = 1;
Leon Scroggins81db3662009-06-04 17:45:11 -040079
Leon Scroggins68579392009-09-15 15:31:54 -040080 public TitleBar(BrowserActivity context) {
Leon Scroggins1f005d32009-08-10 17:36:42 -040081 super(context, null);
Leon Scrogginsc6fa1102009-09-21 10:40:01 -040082 mHandler = new MyHandler();
Leon Scroggins81db3662009-06-04 17:45:11 -040083 LayoutInflater factory = LayoutInflater.from(context);
84 factory.inflate(R.layout.title_bar, this);
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -040085 mBrowserActivity = context;
Leon Scroggins81db3662009-06-04 17:45:11 -040086
87 mTitle = (TextView) findViewById(R.id.title);
Leon Scrogginsa81a7642009-08-31 17:05:41 -040088 mTitle.setCompoundDrawablePadding(5);
Leon Scroggins81db3662009-06-04 17:45:11 -040089
Leon Scroggins68579392009-09-15 15:31:54 -040090 mTitleBg = findViewById(R.id.title_bg);
Leon Scroggins62e8f942009-09-03 15:08:54 -040091 mLockIcon = (ImageView) findViewById(R.id.lock);
Leon Scrogginsf4bb18a2009-09-11 18:37:53 -040092 mFavicon = (ImageView) findViewById(R.id.favicon);
Leon Scroggins62e8f942009-09-03 15:08:54 -040093
Leon Scrogginsa81a7642009-08-31 17:05:41 -040094 mRtButton = (ImageView) findViewById(R.id.rt_btn);
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -040095 Resources resources = context.getResources();
96 mCircularProgress = (Drawable) resources.getDrawable(
Leon Scroggins62e8f942009-09-03 15:08:54 -040097 com.android.internal.R.drawable.search_spinner);
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -040098 mIconDimension = (int) TypedValue.applyDimension(
99 TypedValue.COMPLEX_UNIT_DIP, 20f,
100 resources.getDisplayMetrics());
101 mCircularProgress.setBounds(0, 0, mIconDimension, mIconDimension);
Leon Scroggins81db3662009-06-04 17:45:11 -0400102 mHorizontalProgress = (ProgressBar) findViewById(
103 R.id.progress_horizontal);
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400104 mGenericFavicon = context.getResources().getDrawable(
105 R.drawable.app_web_browser_sm);
Leon Scroggins11e71b12010-01-25 10:40:38 -0500106 mVoiceSearchIntent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
107 mVoiceSearchIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
108 RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);
109 PackageManager pm = context.getPackageManager();
110 ResolveInfo ri = pm.resolveActivity(mVoiceSearchIntent,
111 PackageManager.MATCH_DEFAULT_ONLY);
112 if (ri == null) {
113 mVoiceSearchIntent = null;
114 } else {
115 mVoiceDrawable = resources.getDrawable(
116 android.R.drawable.ic_btn_speak_now);
117 }
118 mStopDrawable = resources.getDrawable(R.drawable.ic_btn_stop_v2);
119 mBookmarkDrawable = mRtButton.getDrawable();
Leon Scroggins58d56c62010-01-28 15:12:40 -0500120 mVoiceModeBackground = resources.getDrawable(
Leon Scrogginsf48d44e2010-02-05 16:41:49 -0500121 R.drawable.title_voice);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500122 mNormalBackground = mTitleBg.getBackground();
Leon Scrogginsf48d44e2010-02-05 16:41:49 -0500123 mArcsSpan = new ImageSpan(context, R.drawable.arcs,
124 ImageSpan.ALIGN_BASELINE);
Leon Scrogginse4b3bda2009-06-09 15:46:41 -0400125 }
126
Leon Scrogginsc6fa1102009-09-21 10:40:01 -0400127 private class MyHandler extends Handler {
128 public void handleMessage(Message msg) {
129 if (msg.what == LONG_PRESS) {
130 // Prevent the normal action from happening by setting the title
131 // bar's state to false.
132 mTitleBg.setPressed(false);
133 // Need to call a special method on BrowserActivity for when the
134 // fake title bar is up, because its ViewGroup does not show a
135 // context menu.
136 mBrowserActivity.showTitleBarContextMenu();
137 }
138 }
139 };
140
141 @Override
142 protected void onCreateContextMenu(ContextMenu menu) {
143 MenuInflater inflater = mBrowserActivity.getMenuInflater();
144 inflater.inflate(R.menu.title_context, menu);
145 }
146
Leon Scroggins68579392009-09-15 15:31:54 -0400147 @Override
148 public boolean onTouchEvent(MotionEvent event) {
149 switch (event.getAction()) {
150 case MotionEvent.ACTION_DOWN:
151 // Make all touches hit either the textfield or the button,
152 // depending on which side of the right edge of the textfield
153 // they hit.
154 if ((int) event.getX() > mTitleBg.getRight()) {
155 mRtButton.setPressed(true);
156 } else {
157 mTitleBg.setPressed(true);
Leon Scrogginsc6fa1102009-09-21 10:40:01 -0400158 mHandler.sendMessageDelayed(mHandler.obtainMessage(
159 LONG_PRESS),
160 ViewConfiguration.getLongPressTimeout());
Leon Scroggins68579392009-09-15 15:31:54 -0400161 }
162 break;
163 case MotionEvent.ACTION_MOVE:
164 int slop = ViewConfiguration.get(mBrowserActivity)
165 .getScaledTouchSlop();
166 if ((int) event.getY() > getHeight() + slop) {
167 // We only trigger the actions in ACTION_UP if one or the
168 // other is pressed. Since the user moved off the title
169 // bar, mark both as not pressed.
170 mTitleBg.setPressed(false);
171 mRtButton.setPressed(false);
Leon Scrogginsc6fa1102009-09-21 10:40:01 -0400172 mHandler.removeMessages(LONG_PRESS);
Leon Scroggins68579392009-09-15 15:31:54 -0400173 break;
174 }
175 int x = (int) event.getX();
176 int titleRight = mTitleBg.getRight();
177 if (mTitleBg.isPressed() && x > titleRight + slop) {
178 mTitleBg.setPressed(false);
Leon Scrogginsc6fa1102009-09-21 10:40:01 -0400179 mHandler.removeMessages(LONG_PRESS);
Leon Scroggins68579392009-09-15 15:31:54 -0400180 } else if (mRtButton.isPressed() && x < titleRight - slop) {
181 mRtButton.setPressed(false);
182 }
183 break;
184 case MotionEvent.ACTION_CANCEL:
185 mRtButton.setPressed(false);
186 mTitleBg.setPressed(false);
Leon Scrogginsc6fa1102009-09-21 10:40:01 -0400187 mHandler.removeMessages(LONG_PRESS);
Leon Scroggins68579392009-09-15 15:31:54 -0400188 break;
189 case MotionEvent.ACTION_UP:
190 if (mRtButton.isPressed()) {
Leon Scroggins11e71b12010-01-25 10:40:38 -0500191 if (mInVoiceMode) {
192 mBrowserActivity.startActivity(mVoiceSearchIntent);
193 } else if (mInLoad) {
Leon Scroggins68579392009-09-15 15:31:54 -0400194 mBrowserActivity.stopLoading();
195 } else {
196 mBrowserActivity.bookmarksOrHistoryPicker(false);
197 }
198 mRtButton.setPressed(false);
199 } else if (mTitleBg.isPressed()) {
Leon Scrogginsc6fa1102009-09-21 10:40:01 -0400200 mHandler.removeMessages(LONG_PRESS);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500201 if (mInVoiceMode) {
202 mBrowserActivity.showVoiceSearchResults(
Leon Scrogginsea754072010-02-08 14:08:30 -0500203 mTitle.getText().toString().trim());
Leon Scroggins58d56c62010-01-28 15:12:40 -0500204 } else {
205 mBrowserActivity.onSearchRequested();
206 }
Leon Scroggins68579392009-09-15 15:31:54 -0400207 mTitleBg.setPressed(false);
208 }
209 break;
210 default:
211 break;
212 }
213 return true;
Leon Scroggins81db3662009-06-04 17:45:11 -0400214 }
215
Leon Scroggins1f005d32009-08-10 17:36:42 -0400216 /**
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400217 * Set a new Bitmap for the Favicon.
Leon Scroggins1f005d32009-08-10 17:36:42 -0400218 */
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400219 /* package */ void setFavicon(Bitmap icon) {
220 Drawable[] array = new Drawable[3];
221 array[0] = new PaintDrawable(Color.BLACK);
222 PaintDrawable p = new PaintDrawable(Color.WHITE);
223 array[1] = p;
224 if (icon == null) {
225 array[2] = mGenericFavicon;
226 } else {
227 array[2] = new BitmapDrawable(icon);
Leon Scrogginsa81a7642009-08-31 17:05:41 -0400228 }
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400229 LayerDrawable d = new LayerDrawable(array);
230 d.setLayerInset(1, 1, 1, 1, 1);
231 d.setLayerInset(2, 2, 2, 2, 2);
Leon Scrogginsf4bb18a2009-09-11 18:37:53 -0400232 mFavicon.setImageDrawable(d);
Leon Scroggins81db3662009-06-04 17:45:11 -0400233 }
234
Leon Scroggins1f005d32009-08-10 17:36:42 -0400235 /**
Leon Scroggins11e71b12010-01-25 10:40:38 -0500236 * Change the TitleBar to or from voice mode. If there is no package to
237 * handle voice search, the TitleBar cannot be set to voice mode.
238 */
239 /* package */ void setInVoiceMode(boolean inVoiceMode) {
240 if (mInVoiceMode == inVoiceMode) return;
241 mInVoiceMode = inVoiceMode && mVoiceSearchIntent != null;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500242 Drawable rightButtonDrawable, titleDrawable;
Leon Scroggins11e71b12010-01-25 10:40:38 -0500243 if (mInVoiceMode) {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500244 rightButtonDrawable = mVoiceDrawable;
245 titleDrawable = mVoiceModeBackground;
Leon Scroggins76e16862010-02-08 14:39:34 -0500246 mTitle.setEllipsize(null);
Leon Scroggins11e71b12010-01-25 10:40:38 -0500247 } else {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500248 titleDrawable = mNormalBackground;
249 if (mInLoad) {
250 rightButtonDrawable = mStopDrawable;
251 } else {
252 rightButtonDrawable = mBookmarkDrawable;
253 }
Leon Scroggins76e16862010-02-08 14:39:34 -0500254 mTitle.setEllipsize(TextUtils.TruncateAt.END);
Leon Scroggins11e71b12010-01-25 10:40:38 -0500255 }
Leon Scrogginsf48d44e2010-02-05 16:41:49 -0500256 mTitle.setSingleLine(!mInVoiceMode);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500257 mTitleBg.setBackgroundDrawable(titleDrawable);
258 mRtButton.setImageDrawable(rightButtonDrawable);
Leon Scroggins11e71b12010-01-25 10:40:38 -0500259 }
260
261 /**
Leon Scroggins1f005d32009-08-10 17:36:42 -0400262 * Set the Drawable for the lock icon, or null to hide it.
263 */
Leon Scroggins81db3662009-06-04 17:45:11 -0400264 /* package */ void setLock(Drawable d) {
Leon Scroggins62e8f942009-09-03 15:08:54 -0400265 if (null == d) {
266 mLockIcon.setVisibility(View.GONE);
267 } else {
268 mLockIcon.setImageDrawable(d);
269 mLockIcon.setVisibility(View.VISIBLE);
Leon Scroggins81db3662009-06-04 17:45:11 -0400270 }
271 }
272
Leon Scroggins1f005d32009-08-10 17:36:42 -0400273 /**
274 * Update the progress, from 0 to 100.
275 */
Leon Scroggins81db3662009-06-04 17:45:11 -0400276 /* package */ void setProgress(int newProgress) {
Nicolas Roard1382b492009-09-16 21:50:06 +0100277 if (newProgress >= mHorizontalProgress.getMax()) {
Leon Scrogginsf4bb18a2009-09-11 18:37:53 -0400278 mTitle.setCompoundDrawables(null, null, null, null);
Leon Scroggins62e8f942009-09-03 15:08:54 -0400279 ((Animatable) mCircularProgress).stop();
280 mHorizontalProgress.setVisibility(View.INVISIBLE);
Leon Scroggins11e71b12010-01-25 10:40:38 -0500281 if (!mInVoiceMode) {
Leon Scrogginsa81a7642009-08-31 17:05:41 -0400282 mRtButton.setImageDrawable(mBookmarkDrawable);
Leon Scroggins1f005d32009-08-10 17:36:42 -0400283 }
Leon Scroggins81db3662009-06-04 17:45:11 -0400284 mInLoad = false;
285 } else {
Leon Scroggins81db3662009-06-04 17:45:11 -0400286 mHorizontalProgress.setProgress(newProgress);
Nicolas Roard1382b492009-09-16 21:50:06 +0100287 if (!mInLoad && getWindowToken() != null) {
288 // checking the window token lets us be sure that we
289 // are attached to a window before starting the animation,
290 // preventing a potential race condition
291 // (fix for bug http://b/2115736)
Leon Scrogginsf4bb18a2009-09-11 18:37:53 -0400292 mTitle.setCompoundDrawables(null, null, mCircularProgress,
Leon Scroggins62e8f942009-09-03 15:08:54 -0400293 null);
294 ((Animatable) mCircularProgress).start();
295 mHorizontalProgress.setVisibility(View.VISIBLE);
Leon Scroggins11e71b12010-01-25 10:40:38 -0500296 if (!mInVoiceMode) {
Leon Scroggins62e8f942009-09-03 15:08:54 -0400297 mRtButton.setImageDrawable(mStopDrawable);
298 }
299 mInLoad = true;
Leon Scrogginse4b3bda2009-06-09 15:46:41 -0400300 }
Leon Scroggins81db3662009-06-04 17:45:11 -0400301 }
302 }
303
Leon Scroggins1f005d32009-08-10 17:36:42 -0400304 /**
Leon Scroggins58d56c62010-01-28 15:12:40 -0500305 * Update the text displayed in the title bar.
306 * @param title String to display. If null, the loading string will be
307 * shown.
Leon Scroggins1f005d32009-08-10 17:36:42 -0400308 */
Leon Scroggins58d56c62010-01-28 15:12:40 -0500309 /* package */ void setDisplayTitle(String title) {
310 if (title == null) {
Leon Scrogginsa81a7642009-08-31 17:05:41 -0400311 mTitle.setText(R.string.title_bar_loading);
Leon Scrogginsd7c3dd52009-07-20 13:38:47 -0400312 } else {
Leon Scrogginsf48d44e2010-02-05 16:41:49 -0500313 if (mInVoiceMode) {
314 // Add two spaces. The second one will be replaced with an
315 // image, and the first one will put space between it and the
316 // text
317 SpannableString spannable = new SpannableString(title + " ");
318 int end = spannable.length();
319 spannable.setSpan(mArcsSpan, end - 1, end,
320 Spanned.SPAN_MARK_POINT);
321 mTitle.setText(spannable);
322 } else {
323 mTitle.setText(title);
324 }
Leon Scrogginsd7c3dd52009-07-20 13:38:47 -0400325 }
Leon Scroggins81db3662009-06-04 17:45:11 -0400326 }
327
328 /* package */ void setToTabPicker() {
329 mTitle.setText(R.string.tab_picker_title);
330 setFavicon(null);
331 setLock(null);
Leon Scroggins81db3662009-06-04 17:45:11 -0400332 mHorizontalProgress.setVisibility(View.GONE);
Leon Scroggins81db3662009-06-04 17:45:11 -0400333 }
334}