blob: 7b5482a767fa4040089cb121e2fefa669aa7b4ac [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 Reck92026732011-02-15 10:12:30 -080037import android.view.View.OnFocusChangeListener;
38import android.widget.ImageButton;
Leon Scroggins81db3662009-06-04 17:45:11 -040039import android.widget.ImageView;
Leon Scroggins81db3662009-06-04 17:45:11 -040040import android.widget.ProgressBar;
Leon Scroggins81db3662009-06-04 17:45:11 -040041
Leon Scroggins1f005d32009-08-10 17:36:42 -040042/**
43 * This class represents a title bar for a particular "tab" or "window" in the
44 * browser.
45 */
John Reck92026732011-02-15 10:12:30 -080046public class TitleBar extends TitleBarBase implements OnFocusChangeListener {
Michael Kolb8233fac2010-10-26 16:08:53 -070047
48 private Activity mActivity;
John Reck92026732011-02-15 10:12:30 -080049 private ImageButton mRtButton;
Michael Kolb8233fac2010-10-26 16:08:53 -070050 private Drawable mCircularProgress;
51 private ProgressBar mHorizontalProgress;
52 private ImageView mStopButton;
53 private Drawable mBookmarkDrawable;
54 private Drawable mVoiceDrawable;
55 private boolean mInLoad;
56 private View mTitleBg;
Michael Kolb8233fac2010-10-26 16:08:53 -070057 private Intent mVoiceSearchIntent;
Michael Kolb8233fac2010-10-26 16:08:53 -070058 private Drawable mVoiceModeBackground;
59 private Drawable mNormalBackground;
Michael Kolb8233fac2010-10-26 16:08:53 -070060 private ImageSpan mArcsSpan;
61 private int mLeftMargin;
62 private int mRightMargin;
Leon Scrogginsc6fa1102009-09-21 10:40:01 -040063
John Reck92026732011-02-15 10:12:30 -080064 public TitleBar(Activity activity, UiController controller, PhoneUi ui) {
65 super(activity, controller, ui);
Michael Kolb8233fac2010-10-26 16:08:53 -070066 LayoutInflater factory = LayoutInflater.from(activity);
Leon Scroggins81db3662009-06-04 17:45:11 -040067 factory.inflate(R.layout.title_bar, this);
Michael Kolb8233fac2010-10-26 16:08:53 -070068 mActivity = activity;
Leon Scroggins81db3662009-06-04 17:45:11 -040069
John Reck92026732011-02-15 10:12:30 -080070 mUrlInput = (UrlInputView) findViewById(R.id.url_input);
71 mUrlInput.setCompoundDrawablePadding(5);
72 mUrlInput.setContainer(this);
73 mUrlInput.setSelectAllOnFocus(true);
74 mUrlInput.setController(mUiController);
75 mUrlInput.setUrlInputListener(this);
76 mUrlInput.setOnFocusChangeListener(this);
Leon Scroggins81db3662009-06-04 17:45:11 -040077
Leon Scroggins68579392009-09-15 15:31:54 -040078 mTitleBg = findViewById(R.id.title_bg);
Leon Scroggins62e8f942009-09-03 15:08:54 -040079 mLockIcon = (ImageView) findViewById(R.id.lock);
Leon Scrogginsf4bb18a2009-09-11 18:37:53 -040080 mFavicon = (ImageView) findViewById(R.id.favicon);
Leon Scrogginsb3b04f72010-03-03 17:17:18 -050081 mStopButton = (ImageView) findViewById(R.id.stop);
Leon Scroggins62e8f942009-09-03 15:08:54 -040082
John Reck92026732011-02-15 10:12:30 -080083 mRtButton = (ImageButton) findViewById(R.id.rt_btn);
Michael Kolb8233fac2010-10-26 16:08:53 -070084 Resources resources = activity.getResources();
85 mCircularProgress = resources.getDrawable(
Leon Scroggins62e8f942009-09-03 15:08:54 -040086 com.android.internal.R.drawable.search_spinner);
Leon Scroggins9535cee2010-03-15 20:37:16 -040087 DisplayMetrics metrics = resources.getDisplayMetrics();
Leon Scrogginscd663ea2010-03-31 14:50:47 -040088 mLeftMargin = (int) TypedValue.applyDimension(
89 TypedValue.COMPLEX_UNIT_DIP, 8f, metrics);
90 mRightMargin = (int) TypedValue.applyDimension(
91 TypedValue.COMPLEX_UNIT_DIP, 6f, metrics);
Leon Scroggins571b3762010-05-26 10:25:01 -040092 int iconDimension = (int) TypedValue.applyDimension(
Leon Scroggins9535cee2010-03-15 20:37:16 -040093 TypedValue.COMPLEX_UNIT_DIP, 20f, metrics);
Leon Scroggins571b3762010-05-26 10:25:01 -040094 mCircularProgress.setBounds(0, 0, iconDimension, iconDimension);
Leon Scroggins81db3662009-06-04 17:45:11 -040095 mHorizontalProgress = (ProgressBar) findViewById(
96 R.id.progress_horizontal);
Leon Scroggins11e71b12010-01-25 10:40:38 -050097 mVoiceSearchIntent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
98 mVoiceSearchIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
99 RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);
Mike LeBeau9f677bc2010-04-01 14:57:10 -0700100 // This extra tells voice search not to send the application id in its
101 // results intent - http://b/2546173
102 //
103 // TODO: Make a constant for this extra.
Michael Kolb8233fac2010-10-26 16:08:53 -0700104 mVoiceSearchIntent.putExtra("android.speech.extras.SEND_APPLICATION_ID_EXTRA",
105 false);
106 PackageManager pm = activity.getPackageManager();
Leon Scroggins11e71b12010-01-25 10:40:38 -0500107 ResolveInfo ri = pm.resolveActivity(mVoiceSearchIntent,
108 PackageManager.MATCH_DEFAULT_ONLY);
109 if (ri == null) {
110 mVoiceSearchIntent = null;
111 } else {
112 mVoiceDrawable = resources.getDrawable(
113 android.R.drawable.ic_btn_speak_now);
114 }
Leon Scroggins11e71b12010-01-25 10:40:38 -0500115 mBookmarkDrawable = mRtButton.getDrawable();
Leon Scroggins58d56c62010-01-28 15:12:40 -0500116 mVoiceModeBackground = resources.getDrawable(
Leon Scrogginsf48d44e2010-02-05 16:41:49 -0500117 R.drawable.title_voice);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500118 mNormalBackground = mTitleBg.getBackground();
Michael Kolb8233fac2010-10-26 16:08:53 -0700119 mArcsSpan = new ImageSpan(activity, R.drawable.arcs,
Leon Scrogginsf48d44e2010-02-05 16:41:49 -0500120 ImageSpan.ALIGN_BASELINE);
Leon Scrogginse4b3bda2009-06-09 15:46:41 -0400121 }
122
Leon Scrogginsc6fa1102009-09-21 10:40:01 -0400123 @Override
Leon Scroggins4e9f89b2010-02-22 16:54:14 -0500124 public void createContextMenu(ContextMenu menu) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700125 MenuInflater inflater = mActivity.getMenuInflater();
Leon Scrogginsc6fa1102009-09-21 10:40:01 -0400126 inflater.inflate(R.menu.title_context, menu);
Michael Kolb8233fac2010-10-26 16:08:53 -0700127 mActivity.onCreateContextMenu(menu, this, null);
Leon Scrogginsc6fa1102009-09-21 10:40:01 -0400128 }
129
Leon Scroggins1f005d32009-08-10 17:36:42 -0400130 /**
Leon Scroggins11e71b12010-01-25 10:40:38 -0500131 * Change the TitleBar to or from voice mode. If there is no package to
132 * handle voice search, the TitleBar cannot be set to voice mode.
133 */
Michael Kolb8233fac2010-10-26 16:08:53 -0700134 @Override
135 void setInVoiceMode(boolean inVoiceMode) {
Leon Scroggins11e71b12010-01-25 10:40:38 -0500136 if (mInVoiceMode == inVoiceMode) return;
137 mInVoiceMode = inVoiceMode && mVoiceSearchIntent != null;
Leon Scrogginsb3b04f72010-03-03 17:17:18 -0500138 Drawable titleDrawable;
Leon Scroggins11e71b12010-01-25 10:40:38 -0500139 if (mInVoiceMode) {
Leon Scrogginsb3b04f72010-03-03 17:17:18 -0500140 mRtButton.setImageDrawable(mVoiceDrawable);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500141 titleDrawable = mVoiceModeBackground;
John Reck92026732011-02-15 10:12:30 -0800142 mUrlInput.setEllipsize(null);
Leon Scrogginsb3b04f72010-03-03 17:17:18 -0500143 mRtButton.setVisibility(View.VISIBLE);
144 mStopButton.setVisibility(View.GONE);
Leon Scroggins III1644ff62010-04-08 10:34:14 -0400145 mTitleBg.setBackgroundDrawable(titleDrawable);
Leon Scroggins7fb1a352010-04-15 10:55:02 -0400146 mTitleBg.setPadding(mLeftMargin, mTitleBg.getPaddingTop(),
147 mRightMargin, mTitleBg.getPaddingBottom());
Leon Scroggins11e71b12010-01-25 10:40:38 -0500148 } else {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500149 if (mInLoad) {
Leon Scrogginsb3b04f72010-03-03 17:17:18 -0500150 mRtButton.setVisibility(View.GONE);
151 mStopButton.setVisibility(View.VISIBLE);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500152 } else {
Leon Scrogginsb3b04f72010-03-03 17:17:18 -0500153 mRtButton.setVisibility(View.VISIBLE);
154 mStopButton.setVisibility(View.GONE);
155 mRtButton.setImageDrawable(mBookmarkDrawable);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500156 }
John Reck92026732011-02-15 10:12:30 -0800157 mUrlInput.setEllipsize(TextUtils.TruncateAt.END);
Leon Scroggins III1644ff62010-04-08 10:34:14 -0400158 mTitleBg.setPadding(mLeftMargin, 0, mRightMargin, 0);
Leon Scroggins11e71b12010-01-25 10:40:38 -0500159 }
John Reck92026732011-02-15 10:12:30 -0800160 mUrlInput.setSingleLine(!mInVoiceMode);
Leon Scroggins11e71b12010-01-25 10:40:38 -0500161 }
162
163 /**
Leon Scroggins1f005d32009-08-10 17:36:42 -0400164 * Update the progress, from 0 to 100.
165 */
Michael Kolb8233fac2010-10-26 16:08:53 -0700166 @Override
167 void setProgress(int newProgress) {
Nicolas Roard1382b492009-09-16 21:50:06 +0100168 if (newProgress >= mHorizontalProgress.getMax()) {
John Reck92026732011-02-15 10:12:30 -0800169 mUrlInput.setCompoundDrawables(null, null, null, null);
Leon Scroggins62e8f942009-09-03 15:08:54 -0400170 ((Animatable) mCircularProgress).stop();
171 mHorizontalProgress.setVisibility(View.INVISIBLE);
Leon Scroggins11e71b12010-01-25 10:40:38 -0500172 if (!mInVoiceMode) {
Leon Scrogginsa81a7642009-08-31 17:05:41 -0400173 mRtButton.setImageDrawable(mBookmarkDrawable);
Leon Scrogginsb3b04f72010-03-03 17:17:18 -0500174 mRtButton.setVisibility(View.VISIBLE);
175 mStopButton.setVisibility(View.GONE);
176 mTitleBg.setBackgroundDrawable(mNormalBackground);
Leon Scrogginscd663ea2010-03-31 14:50:47 -0400177 mTitleBg.setPadding(mLeftMargin, 0, mRightMargin, 0);
Leon Scroggins1f005d32009-08-10 17:36:42 -0400178 }
Leon Scroggins81db3662009-06-04 17:45:11 -0400179 mInLoad = false;
180 } else {
Leon Scroggins81db3662009-06-04 17:45:11 -0400181 mHorizontalProgress.setProgress(newProgress);
Nicolas Roard1382b492009-09-16 21:50:06 +0100182 if (!mInLoad && getWindowToken() != null) {
183 // checking the window token lets us be sure that we
184 // are attached to a window before starting the animation,
185 // preventing a potential race condition
186 // (fix for bug http://b/2115736)
John Reck92026732011-02-15 10:12:30 -0800187 mUrlInput.setCompoundDrawables(null, null, mCircularProgress,
Leon Scroggins62e8f942009-09-03 15:08:54 -0400188 null);
189 ((Animatable) mCircularProgress).start();
190 mHorizontalProgress.setVisibility(View.VISIBLE);
Leon Scroggins11e71b12010-01-25 10:40:38 -0500191 if (!mInVoiceMode) {
Leon Scrogginscd663ea2010-03-31 14:50:47 -0400192 mTitleBg.setPadding(mLeftMargin, 0, mRightMargin, 0);
Leon Scrogginsb3b04f72010-03-03 17:17:18 -0500193 mRtButton.setVisibility(View.GONE);
194 mStopButton.setVisibility(View.VISIBLE);
Leon Scroggins62e8f942009-09-03 15:08:54 -0400195 }
196 mInLoad = true;
Leon Scrogginse4b3bda2009-06-09 15:46:41 -0400197 }
Leon Scroggins81db3662009-06-04 17:45:11 -0400198 }
199 }
200
Leon Scroggins1f005d32009-08-10 17:36:42 -0400201 /**
Leon Scroggins58d56c62010-01-28 15:12:40 -0500202 * Update the text displayed in the title bar.
John Reckef074262010-12-02 16:09:14 -0800203 * @param title String to display. If null, the new tab string will be
Leon Scroggins58d56c62010-01-28 15:12:40 -0500204 * shown.
Leon Scroggins1f005d32009-08-10 17:36:42 -0400205 */
Michael Kolb8233fac2010-10-26 16:08:53 -0700206 @Override
207 void setDisplayTitle(String title) {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500208 if (title == null) {
John Reck92026732011-02-15 10:12:30 -0800209 mUrlInput.setText(R.string.new_tab);
Leon Scrogginsd7c3dd52009-07-20 13:38:47 -0400210 } else {
Leon Scrogginsf48d44e2010-02-05 16:41:49 -0500211 if (mInVoiceMode) {
212 // Add two spaces. The second one will be replaced with an
213 // image, and the first one will put space between it and the
214 // text
215 SpannableString spannable = new SpannableString(title + " ");
216 int end = spannable.length();
217 spannable.setSpan(mArcsSpan, end - 1, end,
218 Spanned.SPAN_MARK_POINT);
John Reck92026732011-02-15 10:12:30 -0800219 mUrlInput.setText(spannable);
Leon Scrogginsf48d44e2010-02-05 16:41:49 -0500220 } else {
John Reck92026732011-02-15 10:12:30 -0800221 mUrlInput.setText(title);
Leon Scrogginsf48d44e2010-02-05 16:41:49 -0500222 }
Leon Scrogginsd7c3dd52009-07-20 13:38:47 -0400223 }
Leon Scroggins81db3662009-06-04 17:45:11 -0400224 }
John Reck92026732011-02-15 10:12:30 -0800225
226 @Override
227 public void onFocusChange(View v, boolean hasFocus) {
228 if (v == mUrlInput && hasFocus) {
229 mActivity.closeOptionsMenu();
230 }
231 }
Leon Scroggins81db3662009-06-04 17:45:11 -0400232}