blob: f1fcdc334ebe4fcfc4eff6d66466382348f3dbbe [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 Scroggins81db3662009-06-04 17:45:11 -040024import android.graphics.drawable.Drawable;
Leon Scroggins11e71b12010-01-25 10:40:38 -050025import android.speech.RecognizerIntent;
Leon Scrogginsf48d44e2010-02-05 16:41:49 -050026import android.text.SpannableString;
27import android.text.Spanned;
Leon Scroggins76e16862010-02-08 14:39:34 -050028import android.text.TextUtils;
Leon Scrogginsf48d44e2010-02-05 16:41:49 -050029import android.text.style.ImageSpan;
Leon Scrogginsc6fa1102009-09-21 10:40:01 -040030import android.view.ContextMenu;
Leon Scrogginse4b3bda2009-06-09 15:46:41 -040031import android.view.LayoutInflater;
Leon Scrogginsc6fa1102009-09-21 10:40:01 -040032import android.view.MenuInflater;
Leon Scrogginse4b3bda2009-06-09 15:46:41 -040033import android.view.View;
John Reckbb2a3452011-02-15 11:31:56 -080034import android.view.View.OnClickListener;
John Reck92026732011-02-15 10:12:30 -080035import android.view.View.OnFocusChangeListener;
36import android.widget.ImageButton;
Leon Scroggins81db3662009-06-04 17:45:11 -040037import android.widget.ImageView;
Leon Scroggins81db3662009-06-04 17:45:11 -040038
Leon Scroggins1f005d32009-08-10 17:36:42 -040039/**
40 * This class represents a title bar for a particular "tab" or "window" in the
41 * browser.
42 */
John Reckbb2a3452011-02-15 11:31:56 -080043public class TitleBar extends TitleBarBase implements OnFocusChangeListener,
44 OnClickListener {
Michael Kolb8233fac2010-10-26 16:08:53 -070045
46 private Activity mActivity;
John Reckbb2a3452011-02-15 11:31:56 -080047 private ImageButton mBookmarkButton;
John Reck94b7e042011-02-15 15:02:33 -080048 private PageProgressView mHorizontalProgress;
John Reckbb2a3452011-02-15 11:31:56 -080049 private ImageButton mStopButton;
Michael Kolb8233fac2010-10-26 16:08:53 -070050 private Drawable mBookmarkDrawable;
51 private Drawable mVoiceDrawable;
52 private boolean mInLoad;
Michael Kolb8233fac2010-10-26 16:08:53 -070053 private Intent mVoiceSearchIntent;
Michael Kolb8233fac2010-10-26 16:08:53 -070054 private ImageSpan mArcsSpan;
John Reckb9a051b2011-03-18 11:55:48 -070055 private View mContainer;
Leon Scrogginsc6fa1102009-09-21 10:40:01 -040056
John Reck92026732011-02-15 10:12:30 -080057 public TitleBar(Activity activity, UiController controller, PhoneUi ui) {
58 super(activity, controller, ui);
Michael Kolb8233fac2010-10-26 16:08:53 -070059 LayoutInflater factory = LayoutInflater.from(activity);
Leon Scroggins81db3662009-06-04 17:45:11 -040060 factory.inflate(R.layout.title_bar, this);
Michael Kolb8233fac2010-10-26 16:08:53 -070061 mActivity = activity;
Leon Scroggins81db3662009-06-04 17:45:11 -040062
John Reckb9a051b2011-03-18 11:55:48 -070063 mContainer = findViewById(R.id.taburlbar);
John Reck92026732011-02-15 10:12:30 -080064 mUrlInput = (UrlInputView) findViewById(R.id.url_input);
65 mUrlInput.setCompoundDrawablePadding(5);
66 mUrlInput.setContainer(this);
67 mUrlInput.setSelectAllOnFocus(true);
68 mUrlInput.setController(mUiController);
69 mUrlInput.setUrlInputListener(this);
70 mUrlInput.setOnFocusChangeListener(this);
Leon Scroggins81db3662009-06-04 17:45:11 -040071
Leon Scroggins62e8f942009-09-03 15:08:54 -040072 mLockIcon = (ImageView) findViewById(R.id.lock);
Leon Scrogginsf4bb18a2009-09-11 18:37:53 -040073 mFavicon = (ImageView) findViewById(R.id.favicon);
John Reckbb2a3452011-02-15 11:31:56 -080074 mStopButton = (ImageButton) findViewById(R.id.stop);
75 mBookmarkButton = (ImageButton) findViewById(R.id.bookmark);
76 mStopButton.setOnClickListener(this);
77 mBookmarkButton.setOnClickListener(this);
Leon Scroggins62e8f942009-09-03 15:08:54 -040078
John Reck94b7e042011-02-15 15:02:33 -080079 mHorizontalProgress = (PageProgressView) findViewById(
Leon Scroggins81db3662009-06-04 17:45:11 -040080 R.id.progress_horizontal);
Leon Scroggins11e71b12010-01-25 10:40:38 -050081 mVoiceSearchIntent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
82 mVoiceSearchIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
83 RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);
Mike LeBeau9f677bc2010-04-01 14:57:10 -070084 // This extra tells voice search not to send the application id in its
85 // results intent - http://b/2546173
86 //
87 // TODO: Make a constant for this extra.
Michael Kolb8233fac2010-10-26 16:08:53 -070088 mVoiceSearchIntent.putExtra("android.speech.extras.SEND_APPLICATION_ID_EXTRA",
89 false);
90 PackageManager pm = activity.getPackageManager();
Leon Scroggins11e71b12010-01-25 10:40:38 -050091 ResolveInfo ri = pm.resolveActivity(mVoiceSearchIntent,
92 PackageManager.MATCH_DEFAULT_ONLY);
John Reck94b7e042011-02-15 15:02:33 -080093 Resources resources = getResources();
Leon Scroggins11e71b12010-01-25 10:40:38 -050094 if (ri == null) {
95 mVoiceSearchIntent = null;
96 } else {
97 mVoiceDrawable = resources.getDrawable(
98 android.R.drawable.ic_btn_speak_now);
99 }
John Reckbb2a3452011-02-15 11:31:56 -0800100 mBookmarkDrawable = mBookmarkButton.getDrawable();
Michael Kolb8233fac2010-10-26 16:08:53 -0700101 mArcsSpan = new ImageSpan(activity, R.drawable.arcs,
Leon Scrogginsf48d44e2010-02-05 16:41:49 -0500102 ImageSpan.ALIGN_BASELINE);
Leon Scrogginse4b3bda2009-06-09 15:46:41 -0400103 }
104
Leon Scrogginsc6fa1102009-09-21 10:40:01 -0400105 @Override
John Reckb9a051b2011-03-18 11:55:48 -0700106 public int getEmbeddedHeight() {
107 int height = mContainer.getHeight();
108 return height;
109 }
110
111 @Override
Leon Scroggins4e9f89b2010-02-22 16:54:14 -0500112 public void createContextMenu(ContextMenu menu) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700113 MenuInflater inflater = mActivity.getMenuInflater();
Leon Scrogginsc6fa1102009-09-21 10:40:01 -0400114 inflater.inflate(R.menu.title_context, menu);
Michael Kolb8233fac2010-10-26 16:08:53 -0700115 mActivity.onCreateContextMenu(menu, this, null);
Leon Scrogginsc6fa1102009-09-21 10:40:01 -0400116 }
117
Leon Scroggins1f005d32009-08-10 17:36:42 -0400118 /**
Leon Scroggins11e71b12010-01-25 10:40:38 -0500119 * Change the TitleBar to or from voice mode. If there is no package to
120 * handle voice search, the TitleBar cannot be set to voice mode.
121 */
Michael Kolb8233fac2010-10-26 16:08:53 -0700122 @Override
123 void setInVoiceMode(boolean inVoiceMode) {
Leon Scroggins11e71b12010-01-25 10:40:38 -0500124 if (mInVoiceMode == inVoiceMode) return;
125 mInVoiceMode = inVoiceMode && mVoiceSearchIntent != null;
Leon Scrogginsb3b04f72010-03-03 17:17:18 -0500126 Drawable titleDrawable;
Leon Scroggins11e71b12010-01-25 10:40:38 -0500127 if (mInVoiceMode) {
John Reckbb2a3452011-02-15 11:31:56 -0800128 mBookmarkButton.setImageDrawable(mVoiceDrawable);
John Reck92026732011-02-15 10:12:30 -0800129 mUrlInput.setEllipsize(null);
John Reckbb2a3452011-02-15 11:31:56 -0800130 mBookmarkButton.setVisibility(View.VISIBLE);
Leon Scrogginsb3b04f72010-03-03 17:17:18 -0500131 mStopButton.setVisibility(View.GONE);
Leon Scroggins11e71b12010-01-25 10:40:38 -0500132 } else {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500133 if (mInLoad) {
John Reckbb2a3452011-02-15 11:31:56 -0800134 mBookmarkButton.setVisibility(View.GONE);
Leon Scrogginsb3b04f72010-03-03 17:17:18 -0500135 mStopButton.setVisibility(View.VISIBLE);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500136 } else {
John Reckbb2a3452011-02-15 11:31:56 -0800137 mBookmarkButton.setVisibility(View.VISIBLE);
Leon Scrogginsb3b04f72010-03-03 17:17:18 -0500138 mStopButton.setVisibility(View.GONE);
John Reckbb2a3452011-02-15 11:31:56 -0800139 mBookmarkButton.setImageDrawable(mBookmarkDrawable);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500140 }
John Reck92026732011-02-15 10:12:30 -0800141 mUrlInput.setEllipsize(TextUtils.TruncateAt.END);
Leon Scroggins11e71b12010-01-25 10:40:38 -0500142 }
John Reck92026732011-02-15 10:12:30 -0800143 mUrlInput.setSingleLine(!mInVoiceMode);
Leon Scroggins11e71b12010-01-25 10:40:38 -0500144 }
145
146 /**
Leon Scroggins1f005d32009-08-10 17:36:42 -0400147 * Update the progress, from 0 to 100.
148 */
Michael Kolb8233fac2010-10-26 16:08:53 -0700149 @Override
150 void setProgress(int newProgress) {
John Reck94b7e042011-02-15 15:02:33 -0800151 if (newProgress >= PROGRESS_MAX) {
152 mHorizontalProgress.setVisibility(View.GONE);
Leon Scroggins11e71b12010-01-25 10:40:38 -0500153 if (!mInVoiceMode) {
John Reckbb2a3452011-02-15 11:31:56 -0800154 mBookmarkButton.setImageDrawable(mBookmarkDrawable);
155 mBookmarkButton.setVisibility(View.VISIBLE);
Leon Scrogginsb3b04f72010-03-03 17:17:18 -0500156 mStopButton.setVisibility(View.GONE);
Leon Scroggins1f005d32009-08-10 17:36:42 -0400157 }
Leon Scroggins81db3662009-06-04 17:45:11 -0400158 mInLoad = false;
159 } else {
John Reck94b7e042011-02-15 15:02:33 -0800160 mHorizontalProgress.setProgress(newProgress * PageProgressView.MAX_PROGRESS
161 / PROGRESS_MAX);
162 if (!mInLoad) {
Leon Scroggins62e8f942009-09-03 15:08:54 -0400163 mHorizontalProgress.setVisibility(View.VISIBLE);
Leon Scroggins11e71b12010-01-25 10:40:38 -0500164 if (!mInVoiceMode) {
John Reckbb2a3452011-02-15 11:31:56 -0800165 mBookmarkButton.setVisibility(View.GONE);
Leon Scrogginsb3b04f72010-03-03 17:17:18 -0500166 mStopButton.setVisibility(View.VISIBLE);
Leon Scroggins62e8f942009-09-03 15:08:54 -0400167 }
168 mInLoad = true;
Leon Scrogginse4b3bda2009-06-09 15:46:41 -0400169 }
Leon Scroggins81db3662009-06-04 17:45:11 -0400170 }
171 }
172
Leon Scroggins1f005d32009-08-10 17:36:42 -0400173 /**
Leon Scroggins58d56c62010-01-28 15:12:40 -0500174 * Update the text displayed in the title bar.
John Reckef074262010-12-02 16:09:14 -0800175 * @param title String to display. If null, the new tab string will be
Leon Scroggins58d56c62010-01-28 15:12:40 -0500176 * shown.
Leon Scroggins1f005d32009-08-10 17:36:42 -0400177 */
Michael Kolb8233fac2010-10-26 16:08:53 -0700178 @Override
179 void setDisplayTitle(String title) {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500180 if (title == null) {
John Reck92026732011-02-15 10:12:30 -0800181 mUrlInput.setText(R.string.new_tab);
Leon Scrogginsd7c3dd52009-07-20 13:38:47 -0400182 } else {
Leon Scrogginsf48d44e2010-02-05 16:41:49 -0500183 if (mInVoiceMode) {
184 // Add two spaces. The second one will be replaced with an
185 // image, and the first one will put space between it and the
186 // text
187 SpannableString spannable = new SpannableString(title + " ");
188 int end = spannable.length();
189 spannable.setSpan(mArcsSpan, end - 1, end,
190 Spanned.SPAN_MARK_POINT);
John Reck92026732011-02-15 10:12:30 -0800191 mUrlInput.setText(spannable);
Leon Scrogginsf48d44e2010-02-05 16:41:49 -0500192 } else {
John Reck92026732011-02-15 10:12:30 -0800193 mUrlInput.setText(title);
Leon Scrogginsf48d44e2010-02-05 16:41:49 -0500194 }
Leon Scrogginsd7c3dd52009-07-20 13:38:47 -0400195 }
Leon Scroggins81db3662009-06-04 17:45:11 -0400196 }
John Reck92026732011-02-15 10:12:30 -0800197
198 @Override
199 public void onFocusChange(View v, boolean hasFocus) {
200 if (v == mUrlInput && hasFocus) {
201 mActivity.closeOptionsMenu();
202 }
203 }
John Reckbb2a3452011-02-15 11:31:56 -0800204
205 @Override
206 public void onClick(View v) {
207 if (v == mStopButton) {
208 mUiController.stopLoading();
209 } else if (v == mBookmarkButton) {
210 mUiController.bookmarkCurrentPage(AddBookmarkPage.DEFAULT_FOLDER_ID,
211 true);
212 }
213 }
John Reck94b7e042011-02-15 15:02:33 -0800214
215 @Override
216 public void setCurrentUrlIsBookmark(boolean isBookmark) {
217 mBookmarkButton.setActivated(isBookmark);
218 }
Leon Scroggins81db3662009-06-04 17:45:11 -0400219}