blob: 743af9b0c0571fc08d049d60840407ee8cc74950 [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 Scroggins3bbb6ca2009-09-09 12:51:10 -040035import android.util.TypedValue;
Leon Scrogginsc6fa1102009-09-21 10:40:01 -040036import android.view.ContextMenu;
Leon Scrogginse4b3bda2009-06-09 15:46:41 -040037import android.view.LayoutInflater;
Leon Scrogginsc6fa1102009-09-21 10:40:01 -040038import android.view.MenuInflater;
Leon Scroggins68579392009-09-15 15:31:54 -040039import android.view.MotionEvent;
Leon Scrogginse4b3bda2009-06-09 15:46:41 -040040import android.view.View;
Leon Scroggins68579392009-09-15 15:31:54 -040041import android.view.ViewConfiguration;
Leon Scroggins81db3662009-06-04 17:45:11 -040042import android.widget.ImageView;
43import android.widget.LinearLayout;
44import android.widget.ProgressBar;
45import android.widget.TextView;
Leon Scroggins81db3662009-06-04 17:45:11 -040046
Leon Scroggins1f005d32009-08-10 17:36:42 -040047/**
48 * This class represents a title bar for a particular "tab" or "window" in the
49 * browser.
50 */
Leon Scrogginse4b3bda2009-06-09 15:46:41 -040051public class TitleBar extends LinearLayout {
Leon Scroggins81db3662009-06-04 17:45:11 -040052 private TextView mTitle;
Leon Scroggins1f005d32009-08-10 17:36:42 -040053 private Drawable mCloseDrawable;
54 private ImageView mRtButton;
Leon Scroggins62e8f942009-09-03 15:08:54 -040055 private Drawable mCircularProgress;
Leon Scroggins81db3662009-06-04 17:45:11 -040056 private ProgressBar mHorizontalProgress;
Leon Scrogginsf4bb18a2009-09-11 18:37:53 -040057 private ImageView mFavicon;
Leon Scroggins62e8f942009-09-03 15:08:54 -040058 private ImageView mLockIcon;
Leon Scrogginsa81a7642009-08-31 17:05:41 -040059 private Drawable mStopDrawable;
60 private Drawable mBookmarkDrawable;
Leon Scroggins11e71b12010-01-25 10:40:38 -050061 private Drawable mVoiceDrawable;
Leon Scroggins81db3662009-06-04 17:45:11 -040062 private boolean mInLoad;
Leon Scrogginsa81a7642009-08-31 17:05:41 -040063 private BrowserActivity mBrowserActivity;
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -040064 private Drawable mGenericFavicon;
65 private int mIconDimension;
Leon Scroggins68579392009-09-15 15:31:54 -040066 private View mTitleBg;
Leon Scrogginsc6fa1102009-09-21 10:40:01 -040067 private MyHandler mHandler;
Leon Scroggins11e71b12010-01-25 10:40:38 -050068 private Intent mVoiceSearchIntent;
69 private boolean mInVoiceMode;
Leon Scroggins58d56c62010-01-28 15:12:40 -050070 private Drawable mVoiceModeBackground;
71 private Drawable mNormalBackground;
Leon Scrogginsc6fa1102009-09-21 10:40:01 -040072
73 private static int LONG_PRESS = 1;
Leon Scroggins81db3662009-06-04 17:45:11 -040074
Leon Scroggins68579392009-09-15 15:31:54 -040075 public TitleBar(BrowserActivity context) {
Leon Scroggins1f005d32009-08-10 17:36:42 -040076 super(context, null);
Leon Scrogginsc6fa1102009-09-21 10:40:01 -040077 mHandler = new MyHandler();
Leon Scroggins81db3662009-06-04 17:45:11 -040078 LayoutInflater factory = LayoutInflater.from(context);
79 factory.inflate(R.layout.title_bar, this);
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -040080 mBrowserActivity = context;
Leon Scroggins81db3662009-06-04 17:45:11 -040081
82 mTitle = (TextView) findViewById(R.id.title);
Leon Scrogginsa81a7642009-08-31 17:05:41 -040083 mTitle.setCompoundDrawablePadding(5);
Leon Scroggins81db3662009-06-04 17:45:11 -040084
Leon Scroggins68579392009-09-15 15:31:54 -040085 mTitleBg = findViewById(R.id.title_bg);
Leon Scroggins62e8f942009-09-03 15:08:54 -040086 mLockIcon = (ImageView) findViewById(R.id.lock);
Leon Scrogginsf4bb18a2009-09-11 18:37:53 -040087 mFavicon = (ImageView) findViewById(R.id.favicon);
Leon Scroggins62e8f942009-09-03 15:08:54 -040088
Leon Scrogginsa81a7642009-08-31 17:05:41 -040089 mRtButton = (ImageView) findViewById(R.id.rt_btn);
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -040090 Resources resources = context.getResources();
91 mCircularProgress = (Drawable) resources.getDrawable(
Leon Scroggins62e8f942009-09-03 15:08:54 -040092 com.android.internal.R.drawable.search_spinner);
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -040093 mIconDimension = (int) TypedValue.applyDimension(
94 TypedValue.COMPLEX_UNIT_DIP, 20f,
95 resources.getDisplayMetrics());
96 mCircularProgress.setBounds(0, 0, mIconDimension, mIconDimension);
Leon Scroggins81db3662009-06-04 17:45:11 -040097 mHorizontalProgress = (ProgressBar) findViewById(
98 R.id.progress_horizontal);
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -040099 mGenericFavicon = context.getResources().getDrawable(
100 R.drawable.app_web_browser_sm);
Leon Scroggins11e71b12010-01-25 10:40:38 -0500101 mVoiceSearchIntent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
102 mVoiceSearchIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
103 RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);
104 PackageManager pm = context.getPackageManager();
105 ResolveInfo ri = pm.resolveActivity(mVoiceSearchIntent,
106 PackageManager.MATCH_DEFAULT_ONLY);
107 if (ri == null) {
108 mVoiceSearchIntent = null;
109 } else {
110 mVoiceDrawable = resources.getDrawable(
111 android.R.drawable.ic_btn_speak_now);
112 }
113 mStopDrawable = resources.getDrawable(R.drawable.ic_btn_stop_v2);
114 mBookmarkDrawable = mRtButton.getDrawable();
Leon Scroggins58d56c62010-01-28 15:12:40 -0500115 mVoiceModeBackground = resources.getDrawable(
116 R.drawable.textfield_voice_search);
117 mNormalBackground = mTitleBg.getBackground();
Leon Scrogginse4b3bda2009-06-09 15:46:41 -0400118 }
119
Leon Scrogginsc6fa1102009-09-21 10:40:01 -0400120 private class MyHandler extends Handler {
121 public void handleMessage(Message msg) {
122 if (msg.what == LONG_PRESS) {
123 // Prevent the normal action from happening by setting the title
124 // bar's state to false.
125 mTitleBg.setPressed(false);
126 // Need to call a special method on BrowserActivity for when the
127 // fake title bar is up, because its ViewGroup does not show a
128 // context menu.
129 mBrowserActivity.showTitleBarContextMenu();
130 }
131 }
132 };
133
134 @Override
135 protected void onCreateContextMenu(ContextMenu menu) {
136 MenuInflater inflater = mBrowserActivity.getMenuInflater();
137 inflater.inflate(R.menu.title_context, menu);
138 }
139
Leon Scroggins68579392009-09-15 15:31:54 -0400140 @Override
141 public boolean onTouchEvent(MotionEvent event) {
142 switch (event.getAction()) {
143 case MotionEvent.ACTION_DOWN:
144 // Make all touches hit either the textfield or the button,
145 // depending on which side of the right edge of the textfield
146 // they hit.
147 if ((int) event.getX() > mTitleBg.getRight()) {
148 mRtButton.setPressed(true);
149 } else {
150 mTitleBg.setPressed(true);
Leon Scrogginsc6fa1102009-09-21 10:40:01 -0400151 mHandler.sendMessageDelayed(mHandler.obtainMessage(
152 LONG_PRESS),
153 ViewConfiguration.getLongPressTimeout());
Leon Scroggins68579392009-09-15 15:31:54 -0400154 }
155 break;
156 case MotionEvent.ACTION_MOVE:
157 int slop = ViewConfiguration.get(mBrowserActivity)
158 .getScaledTouchSlop();
159 if ((int) event.getY() > getHeight() + slop) {
160 // We only trigger the actions in ACTION_UP if one or the
161 // other is pressed. Since the user moved off the title
162 // bar, mark both as not pressed.
163 mTitleBg.setPressed(false);
164 mRtButton.setPressed(false);
Leon Scrogginsc6fa1102009-09-21 10:40:01 -0400165 mHandler.removeMessages(LONG_PRESS);
Leon Scroggins68579392009-09-15 15:31:54 -0400166 break;
167 }
168 int x = (int) event.getX();
169 int titleRight = mTitleBg.getRight();
170 if (mTitleBg.isPressed() && x > titleRight + slop) {
171 mTitleBg.setPressed(false);
Leon Scrogginsc6fa1102009-09-21 10:40:01 -0400172 mHandler.removeMessages(LONG_PRESS);
Leon Scroggins68579392009-09-15 15:31:54 -0400173 } else if (mRtButton.isPressed() && x < titleRight - slop) {
174 mRtButton.setPressed(false);
175 }
176 break;
177 case MotionEvent.ACTION_CANCEL:
178 mRtButton.setPressed(false);
179 mTitleBg.setPressed(false);
Leon Scrogginsc6fa1102009-09-21 10:40:01 -0400180 mHandler.removeMessages(LONG_PRESS);
Leon Scroggins68579392009-09-15 15:31:54 -0400181 break;
182 case MotionEvent.ACTION_UP:
183 if (mRtButton.isPressed()) {
Leon Scroggins11e71b12010-01-25 10:40:38 -0500184 if (mInVoiceMode) {
185 mBrowserActivity.startActivity(mVoiceSearchIntent);
186 } else if (mInLoad) {
Leon Scroggins68579392009-09-15 15:31:54 -0400187 mBrowserActivity.stopLoading();
188 } else {
189 mBrowserActivity.bookmarksOrHistoryPicker(false);
190 }
191 mRtButton.setPressed(false);
192 } else if (mTitleBg.isPressed()) {
Leon Scrogginsc6fa1102009-09-21 10:40:01 -0400193 mHandler.removeMessages(LONG_PRESS);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500194 if (mInVoiceMode) {
195 mBrowserActivity.showVoiceSearchResults(
196 mTitle.getText().toString());
197 } else {
198 mBrowserActivity.onSearchRequested();
199 }
Leon Scroggins68579392009-09-15 15:31:54 -0400200 mTitleBg.setPressed(false);
201 }
202 break;
203 default:
204 break;
205 }
206 return true;
Leon Scroggins81db3662009-06-04 17:45:11 -0400207 }
208
Leon Scroggins1f005d32009-08-10 17:36:42 -0400209 /**
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400210 * Set a new Bitmap for the Favicon.
Leon Scroggins1f005d32009-08-10 17:36:42 -0400211 */
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400212 /* package */ void setFavicon(Bitmap icon) {
213 Drawable[] array = new Drawable[3];
214 array[0] = new PaintDrawable(Color.BLACK);
215 PaintDrawable p = new PaintDrawable(Color.WHITE);
216 array[1] = p;
217 if (icon == null) {
218 array[2] = mGenericFavicon;
219 } else {
220 array[2] = new BitmapDrawable(icon);
Leon Scrogginsa81a7642009-08-31 17:05:41 -0400221 }
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400222 LayerDrawable d = new LayerDrawable(array);
223 d.setLayerInset(1, 1, 1, 1, 1);
224 d.setLayerInset(2, 2, 2, 2, 2);
Leon Scrogginsf4bb18a2009-09-11 18:37:53 -0400225 mFavicon.setImageDrawable(d);
Leon Scroggins81db3662009-06-04 17:45:11 -0400226 }
227
Leon Scroggins1f005d32009-08-10 17:36:42 -0400228 /**
Leon Scroggins11e71b12010-01-25 10:40:38 -0500229 * Change the TitleBar to or from voice mode. If there is no package to
230 * handle voice search, the TitleBar cannot be set to voice mode.
231 */
232 /* package */ void setInVoiceMode(boolean inVoiceMode) {
233 if (mInVoiceMode == inVoiceMode) return;
234 mInVoiceMode = inVoiceMode && mVoiceSearchIntent != null;
Leon Scroggins58d56c62010-01-28 15:12:40 -0500235 Drawable rightButtonDrawable, titleDrawable;
Leon Scroggins11e71b12010-01-25 10:40:38 -0500236 if (mInVoiceMode) {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500237 rightButtonDrawable = mVoiceDrawable;
238 titleDrawable = mVoiceModeBackground;
Leon Scroggins11e71b12010-01-25 10:40:38 -0500239 } else {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500240 titleDrawable = mNormalBackground;
241 if (mInLoad) {
242 rightButtonDrawable = mStopDrawable;
243 } else {
244 rightButtonDrawable = mBookmarkDrawable;
245 }
Leon Scroggins11e71b12010-01-25 10:40:38 -0500246 }
Leon Scroggins58d56c62010-01-28 15:12:40 -0500247 mTitleBg.setBackgroundDrawable(titleDrawable);
248 mRtButton.setImageDrawable(rightButtonDrawable);
Leon Scroggins11e71b12010-01-25 10:40:38 -0500249 }
250
251 /**
Leon Scroggins1f005d32009-08-10 17:36:42 -0400252 * Set the Drawable for the lock icon, or null to hide it.
253 */
Leon Scroggins81db3662009-06-04 17:45:11 -0400254 /* package */ void setLock(Drawable d) {
Leon Scroggins62e8f942009-09-03 15:08:54 -0400255 if (null == d) {
256 mLockIcon.setVisibility(View.GONE);
257 } else {
258 mLockIcon.setImageDrawable(d);
259 mLockIcon.setVisibility(View.VISIBLE);
Leon Scroggins81db3662009-06-04 17:45:11 -0400260 }
261 }
262
Leon Scroggins1f005d32009-08-10 17:36:42 -0400263 /**
264 * Update the progress, from 0 to 100.
265 */
Leon Scroggins81db3662009-06-04 17:45:11 -0400266 /* package */ void setProgress(int newProgress) {
Nicolas Roard1382b492009-09-16 21:50:06 +0100267 if (newProgress >= mHorizontalProgress.getMax()) {
Leon Scrogginsf4bb18a2009-09-11 18:37:53 -0400268 mTitle.setCompoundDrawables(null, null, null, null);
Leon Scroggins62e8f942009-09-03 15:08:54 -0400269 ((Animatable) mCircularProgress).stop();
270 mHorizontalProgress.setVisibility(View.INVISIBLE);
Leon Scroggins11e71b12010-01-25 10:40:38 -0500271 if (!mInVoiceMode) {
Leon Scrogginsa81a7642009-08-31 17:05:41 -0400272 mRtButton.setImageDrawable(mBookmarkDrawable);
Leon Scroggins1f005d32009-08-10 17:36:42 -0400273 }
Leon Scroggins81db3662009-06-04 17:45:11 -0400274 mInLoad = false;
275 } else {
Leon Scroggins81db3662009-06-04 17:45:11 -0400276 mHorizontalProgress.setProgress(newProgress);
Nicolas Roard1382b492009-09-16 21:50:06 +0100277 if (!mInLoad && getWindowToken() != null) {
278 // checking the window token lets us be sure that we
279 // are attached to a window before starting the animation,
280 // preventing a potential race condition
281 // (fix for bug http://b/2115736)
Leon Scrogginsf4bb18a2009-09-11 18:37:53 -0400282 mTitle.setCompoundDrawables(null, null, mCircularProgress,
Leon Scroggins62e8f942009-09-03 15:08:54 -0400283 null);
284 ((Animatable) mCircularProgress).start();
285 mHorizontalProgress.setVisibility(View.VISIBLE);
Leon Scroggins11e71b12010-01-25 10:40:38 -0500286 if (!mInVoiceMode) {
Leon Scroggins62e8f942009-09-03 15:08:54 -0400287 mRtButton.setImageDrawable(mStopDrawable);
288 }
289 mInLoad = true;
Leon Scrogginse4b3bda2009-06-09 15:46:41 -0400290 }
Leon Scroggins81db3662009-06-04 17:45:11 -0400291 }
292 }
293
Leon Scroggins1f005d32009-08-10 17:36:42 -0400294 /**
Leon Scroggins58d56c62010-01-28 15:12:40 -0500295 * Update the text displayed in the title bar.
296 * @param title String to display. If null, the loading string will be
297 * shown.
Leon Scroggins1f005d32009-08-10 17:36:42 -0400298 */
Leon Scroggins58d56c62010-01-28 15:12:40 -0500299 /* package */ void setDisplayTitle(String title) {
300 if (title == null) {
Leon Scrogginsa81a7642009-08-31 17:05:41 -0400301 mTitle.setText(R.string.title_bar_loading);
Leon Scrogginsd7c3dd52009-07-20 13:38:47 -0400302 } else {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500303 mTitle.setText(title);
Leon Scrogginsd7c3dd52009-07-20 13:38:47 -0400304 }
Leon Scroggins81db3662009-06-04 17:45:11 -0400305 }
306
307 /* package */ void setToTabPicker() {
308 mTitle.setText(R.string.tab_picker_title);
309 setFavicon(null);
310 setLock(null);
Leon Scroggins81db3662009-06-04 17:45:11 -0400311 mHorizontalProgress.setVisibility(View.GONE);
Leon Scroggins81db3662009-06-04 17:45:11 -0400312 }
313}