blob: 56db187c06cb40adcc5dc7f1aca2f555f6256070 [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
Michael Kolb8233fac2010-10-26 16:08:53 -070019import android.app.Activity;
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;
Leon Scroggins62e8f942009-09-03 15:08:54 -040024import android.graphics.drawable.Animatable;
Leon Scroggins81db3662009-06-04 17:45:11 -040025import android.graphics.drawable.Drawable;
Leon Scroggins11e71b12010-01-25 10:40:38 -050026import android.speech.RecognizerIntent;
Leon Scrogginsf48d44e2010-02-05 16:41:49 -050027import android.text.SpannableString;
28import android.text.Spanned;
Leon Scroggins76e16862010-02-08 14:39:34 -050029import android.text.TextUtils;
Leon Scrogginsf48d44e2010-02-05 16:41:49 -050030import android.text.style.ImageSpan;
Leon Scroggins9535cee2010-03-15 20:37:16 -040031import android.util.DisplayMetrics;
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -040032import android.util.TypedValue;
Leon Scrogginsc6fa1102009-09-21 10:40:01 -040033import android.view.ContextMenu;
Leon Scrogginse4b3bda2009-06-09 15:46:41 -040034import android.view.LayoutInflater;
Leon Scrogginsc6fa1102009-09-21 10:40:01 -040035import android.view.MenuInflater;
Leon Scrogginse4b3bda2009-06-09 15:46:41 -040036import android.view.View;
John Reckbb2a3452011-02-15 11:31:56 -080037import android.view.View.OnClickListener;
John Reck92026732011-02-15 10:12:30 -080038import android.view.View.OnFocusChangeListener;
39import android.widget.ImageButton;
Leon Scroggins81db3662009-06-04 17:45:11 -040040import android.widget.ImageView;
Leon Scroggins81db3662009-06-04 17:45:11 -040041import android.widget.ProgressBar;
Leon Scroggins81db3662009-06-04 17:45:11 -040042
Leon Scroggins1f005d32009-08-10 17:36:42 -040043/**
44 * This class represents a title bar for a particular "tab" or "window" in the
45 * browser.
46 */
John Reckbb2a3452011-02-15 11:31:56 -080047public class TitleBar extends TitleBarBase implements OnFocusChangeListener,
48 OnClickListener {
Michael Kolb8233fac2010-10-26 16:08:53 -070049
50 private Activity mActivity;
John Reckbb2a3452011-02-15 11:31:56 -080051 private ImageButton mBookmarkButton;
Michael Kolb8233fac2010-10-26 16:08:53 -070052 private Drawable mCircularProgress;
53 private ProgressBar mHorizontalProgress;
John Reckbb2a3452011-02-15 11:31:56 -080054 private ImageButton mStopButton;
Michael Kolb8233fac2010-10-26 16:08:53 -070055 private Drawable mBookmarkDrawable;
56 private Drawable mVoiceDrawable;
57 private boolean mInLoad;
58 private View mTitleBg;
Michael Kolb8233fac2010-10-26 16:08:53 -070059 private Intent mVoiceSearchIntent;
Michael Kolb8233fac2010-10-26 16:08:53 -070060 private Drawable mVoiceModeBackground;
61 private Drawable mNormalBackground;
Michael Kolb8233fac2010-10-26 16:08:53 -070062 private ImageSpan mArcsSpan;
63 private int mLeftMargin;
64 private int mRightMargin;
Leon Scrogginsc6fa1102009-09-21 10:40:01 -040065
John Reck92026732011-02-15 10:12:30 -080066 public TitleBar(Activity activity, UiController controller, PhoneUi ui) {
67 super(activity, controller, ui);
Michael Kolb8233fac2010-10-26 16:08:53 -070068 LayoutInflater factory = LayoutInflater.from(activity);
Leon Scroggins81db3662009-06-04 17:45:11 -040069 factory.inflate(R.layout.title_bar, this);
Michael Kolb8233fac2010-10-26 16:08:53 -070070 mActivity = activity;
Leon Scroggins81db3662009-06-04 17:45:11 -040071
John Reck92026732011-02-15 10:12:30 -080072 mUrlInput = (UrlInputView) findViewById(R.id.url_input);
73 mUrlInput.setCompoundDrawablePadding(5);
74 mUrlInput.setContainer(this);
75 mUrlInput.setSelectAllOnFocus(true);
76 mUrlInput.setController(mUiController);
77 mUrlInput.setUrlInputListener(this);
78 mUrlInput.setOnFocusChangeListener(this);
Leon Scroggins81db3662009-06-04 17:45:11 -040079
Leon Scroggins68579392009-09-15 15:31:54 -040080 mTitleBg = findViewById(R.id.title_bg);
Leon Scroggins62e8f942009-09-03 15:08:54 -040081 mLockIcon = (ImageView) findViewById(R.id.lock);
Leon Scrogginsf4bb18a2009-09-11 18:37:53 -040082 mFavicon = (ImageView) findViewById(R.id.favicon);
John Reckbb2a3452011-02-15 11:31:56 -080083 mStopButton = (ImageButton) findViewById(R.id.stop);
84 mBookmarkButton = (ImageButton) findViewById(R.id.bookmark);
85 mStopButton.setOnClickListener(this);
86 mBookmarkButton.setOnClickListener(this);
Leon Scroggins62e8f942009-09-03 15:08:54 -040087
Michael Kolb8233fac2010-10-26 16:08:53 -070088 Resources resources = activity.getResources();
89 mCircularProgress = resources.getDrawable(
Leon Scroggins62e8f942009-09-03 15:08:54 -040090 com.android.internal.R.drawable.search_spinner);
Leon Scroggins9535cee2010-03-15 20:37:16 -040091 DisplayMetrics metrics = resources.getDisplayMetrics();
Leon Scrogginscd663ea2010-03-31 14:50:47 -040092 mLeftMargin = (int) TypedValue.applyDimension(
93 TypedValue.COMPLEX_UNIT_DIP, 8f, metrics);
94 mRightMargin = (int) TypedValue.applyDimension(
95 TypedValue.COMPLEX_UNIT_DIP, 6f, metrics);
Leon Scroggins571b3762010-05-26 10:25:01 -040096 int iconDimension = (int) TypedValue.applyDimension(
Leon Scroggins9535cee2010-03-15 20:37:16 -040097 TypedValue.COMPLEX_UNIT_DIP, 20f, metrics);
Leon Scroggins571b3762010-05-26 10:25:01 -040098 mCircularProgress.setBounds(0, 0, iconDimension, iconDimension);
Leon Scroggins81db3662009-06-04 17:45:11 -040099 mHorizontalProgress = (ProgressBar) findViewById(
100 R.id.progress_horizontal);
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);
Mike LeBeau9f677bc2010-04-01 14:57:10 -0700104 // This extra tells voice search not to send the application id in its
105 // results intent - http://b/2546173
106 //
107 // TODO: Make a constant for this extra.
Michael Kolb8233fac2010-10-26 16:08:53 -0700108 mVoiceSearchIntent.putExtra("android.speech.extras.SEND_APPLICATION_ID_EXTRA",
109 false);
110 PackageManager pm = activity.getPackageManager();
Leon Scroggins11e71b12010-01-25 10:40:38 -0500111 ResolveInfo ri = pm.resolveActivity(mVoiceSearchIntent,
112 PackageManager.MATCH_DEFAULT_ONLY);
113 if (ri == null) {
114 mVoiceSearchIntent = null;
115 } else {
116 mVoiceDrawable = resources.getDrawable(
117 android.R.drawable.ic_btn_speak_now);
118 }
John Reckbb2a3452011-02-15 11:31:56 -0800119 mBookmarkDrawable = mBookmarkButton.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();
Michael Kolb8233fac2010-10-26 16:08:53 -0700123 mArcsSpan = new ImageSpan(activity, R.drawable.arcs,
Leon Scrogginsf48d44e2010-02-05 16:41:49 -0500124 ImageSpan.ALIGN_BASELINE);
Leon Scrogginse4b3bda2009-06-09 15:46:41 -0400125 }
126
Leon Scrogginsc6fa1102009-09-21 10:40:01 -0400127 @Override
Leon Scroggins4e9f89b2010-02-22 16:54:14 -0500128 public void createContextMenu(ContextMenu menu) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700129 MenuInflater inflater = mActivity.getMenuInflater();
Leon Scrogginsc6fa1102009-09-21 10:40:01 -0400130 inflater.inflate(R.menu.title_context, menu);
Michael Kolb8233fac2010-10-26 16:08:53 -0700131 mActivity.onCreateContextMenu(menu, this, null);
Leon Scrogginsc6fa1102009-09-21 10:40:01 -0400132 }
133
Leon Scroggins1f005d32009-08-10 17:36:42 -0400134 /**
Leon Scroggins11e71b12010-01-25 10:40:38 -0500135 * Change the TitleBar to or from voice mode. If there is no package to
136 * handle voice search, the TitleBar cannot be set to voice mode.
137 */
Michael Kolb8233fac2010-10-26 16:08:53 -0700138 @Override
139 void setInVoiceMode(boolean inVoiceMode) {
Leon Scroggins11e71b12010-01-25 10:40:38 -0500140 if (mInVoiceMode == inVoiceMode) return;
141 mInVoiceMode = inVoiceMode && mVoiceSearchIntent != null;
Leon Scrogginsb3b04f72010-03-03 17:17:18 -0500142 Drawable titleDrawable;
Leon Scroggins11e71b12010-01-25 10:40:38 -0500143 if (mInVoiceMode) {
John Reckbb2a3452011-02-15 11:31:56 -0800144 mBookmarkButton.setImageDrawable(mVoiceDrawable);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500145 titleDrawable = mVoiceModeBackground;
John Reck92026732011-02-15 10:12:30 -0800146 mUrlInput.setEllipsize(null);
John Reckbb2a3452011-02-15 11:31:56 -0800147 mBookmarkButton.setVisibility(View.VISIBLE);
Leon Scrogginsb3b04f72010-03-03 17:17:18 -0500148 mStopButton.setVisibility(View.GONE);
Leon Scroggins III1644ff62010-04-08 10:34:14 -0400149 mTitleBg.setBackgroundDrawable(titleDrawable);
Leon Scroggins7fb1a352010-04-15 10:55:02 -0400150 mTitleBg.setPadding(mLeftMargin, mTitleBg.getPaddingTop(),
151 mRightMargin, mTitleBg.getPaddingBottom());
Leon Scroggins11e71b12010-01-25 10:40:38 -0500152 } else {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500153 if (mInLoad) {
John Reckbb2a3452011-02-15 11:31:56 -0800154 mBookmarkButton.setVisibility(View.GONE);
Leon Scrogginsb3b04f72010-03-03 17:17:18 -0500155 mStopButton.setVisibility(View.VISIBLE);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500156 } else {
John Reckbb2a3452011-02-15 11:31:56 -0800157 mBookmarkButton.setVisibility(View.VISIBLE);
Leon Scrogginsb3b04f72010-03-03 17:17:18 -0500158 mStopButton.setVisibility(View.GONE);
John Reckbb2a3452011-02-15 11:31:56 -0800159 mBookmarkButton.setImageDrawable(mBookmarkDrawable);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500160 }
John Reck92026732011-02-15 10:12:30 -0800161 mUrlInput.setEllipsize(TextUtils.TruncateAt.END);
Leon Scroggins III1644ff62010-04-08 10:34:14 -0400162 mTitleBg.setPadding(mLeftMargin, 0, mRightMargin, 0);
Leon Scroggins11e71b12010-01-25 10:40:38 -0500163 }
John Reck92026732011-02-15 10:12:30 -0800164 mUrlInput.setSingleLine(!mInVoiceMode);
Leon Scroggins11e71b12010-01-25 10:40:38 -0500165 }
166
167 /**
Leon Scroggins1f005d32009-08-10 17:36:42 -0400168 * Update the progress, from 0 to 100.
169 */
Michael Kolb8233fac2010-10-26 16:08:53 -0700170 @Override
171 void setProgress(int newProgress) {
Nicolas Roard1382b492009-09-16 21:50:06 +0100172 if (newProgress >= mHorizontalProgress.getMax()) {
John Reck92026732011-02-15 10:12:30 -0800173 mUrlInput.setCompoundDrawables(null, null, null, null);
Leon Scroggins62e8f942009-09-03 15:08:54 -0400174 ((Animatable) mCircularProgress).stop();
175 mHorizontalProgress.setVisibility(View.INVISIBLE);
Leon Scroggins11e71b12010-01-25 10:40:38 -0500176 if (!mInVoiceMode) {
John Reckbb2a3452011-02-15 11:31:56 -0800177 mBookmarkButton.setImageDrawable(mBookmarkDrawable);
178 mBookmarkButton.setVisibility(View.VISIBLE);
Leon Scrogginsb3b04f72010-03-03 17:17:18 -0500179 mStopButton.setVisibility(View.GONE);
180 mTitleBg.setBackgroundDrawable(mNormalBackground);
Leon Scrogginscd663ea2010-03-31 14:50:47 -0400181 mTitleBg.setPadding(mLeftMargin, 0, mRightMargin, 0);
Leon Scroggins1f005d32009-08-10 17:36:42 -0400182 }
Leon Scroggins81db3662009-06-04 17:45:11 -0400183 mInLoad = false;
184 } else {
Leon Scroggins81db3662009-06-04 17:45:11 -0400185 mHorizontalProgress.setProgress(newProgress);
Nicolas Roard1382b492009-09-16 21:50:06 +0100186 if (!mInLoad && getWindowToken() != null) {
187 // checking the window token lets us be sure that we
188 // are attached to a window before starting the animation,
189 // preventing a potential race condition
190 // (fix for bug http://b/2115736)
John Reck92026732011-02-15 10:12:30 -0800191 mUrlInput.setCompoundDrawables(null, null, mCircularProgress,
Leon Scroggins62e8f942009-09-03 15:08:54 -0400192 null);
193 ((Animatable) mCircularProgress).start();
194 mHorizontalProgress.setVisibility(View.VISIBLE);
Leon Scroggins11e71b12010-01-25 10:40:38 -0500195 if (!mInVoiceMode) {
Leon Scrogginscd663ea2010-03-31 14:50:47 -0400196 mTitleBg.setPadding(mLeftMargin, 0, mRightMargin, 0);
John Reckbb2a3452011-02-15 11:31:56 -0800197 mBookmarkButton.setVisibility(View.GONE);
Leon Scrogginsb3b04f72010-03-03 17:17:18 -0500198 mStopButton.setVisibility(View.VISIBLE);
Leon Scroggins62e8f942009-09-03 15:08:54 -0400199 }
200 mInLoad = true;
Leon Scrogginse4b3bda2009-06-09 15:46:41 -0400201 }
Leon Scroggins81db3662009-06-04 17:45:11 -0400202 }
203 }
204
Leon Scroggins1f005d32009-08-10 17:36:42 -0400205 /**
Leon Scroggins58d56c62010-01-28 15:12:40 -0500206 * Update the text displayed in the title bar.
John Reckef074262010-12-02 16:09:14 -0800207 * @param title String to display. If null, the new tab string will be
Leon Scroggins58d56c62010-01-28 15:12:40 -0500208 * shown.
Leon Scroggins1f005d32009-08-10 17:36:42 -0400209 */
Michael Kolb8233fac2010-10-26 16:08:53 -0700210 @Override
211 void setDisplayTitle(String title) {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500212 if (title == null) {
John Reck92026732011-02-15 10:12:30 -0800213 mUrlInput.setText(R.string.new_tab);
Leon Scrogginsd7c3dd52009-07-20 13:38:47 -0400214 } else {
Leon Scrogginsf48d44e2010-02-05 16:41:49 -0500215 if (mInVoiceMode) {
216 // Add two spaces. The second one will be replaced with an
217 // image, and the first one will put space between it and the
218 // text
219 SpannableString spannable = new SpannableString(title + " ");
220 int end = spannable.length();
221 spannable.setSpan(mArcsSpan, end - 1, end,
222 Spanned.SPAN_MARK_POINT);
John Reck92026732011-02-15 10:12:30 -0800223 mUrlInput.setText(spannable);
Leon Scrogginsf48d44e2010-02-05 16:41:49 -0500224 } else {
John Reck92026732011-02-15 10:12:30 -0800225 mUrlInput.setText(title);
Leon Scrogginsf48d44e2010-02-05 16:41:49 -0500226 }
Leon Scrogginsd7c3dd52009-07-20 13:38:47 -0400227 }
Leon Scroggins81db3662009-06-04 17:45:11 -0400228 }
John Reck92026732011-02-15 10:12:30 -0800229
230 @Override
231 public void onFocusChange(View v, boolean hasFocus) {
232 if (v == mUrlInput && hasFocus) {
233 mActivity.closeOptionsMenu();
234 }
235 }
John Reckbb2a3452011-02-15 11:31:56 -0800236
237 @Override
238 public void onClick(View v) {
239 if (v == mStopButton) {
240 mUiController.stopLoading();
241 } else if (v == mBookmarkButton) {
242 mUiController.bookmarkCurrentPage(AddBookmarkPage.DEFAULT_FOLDER_ID,
243 true);
244 }
245 }
Leon Scroggins81db3662009-06-04 17:45:11 -0400246}