blob: 686416cbf9c6422829f78b7006af757a45cecbbd [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;
Leon Scrogginsc6fa1102009-09-21 10:40:01 -040055
John Reck92026732011-02-15 10:12:30 -080056 public TitleBar(Activity activity, UiController controller, PhoneUi ui) {
57 super(activity, controller, ui);
Michael Kolb8233fac2010-10-26 16:08:53 -070058 LayoutInflater factory = LayoutInflater.from(activity);
Leon Scroggins81db3662009-06-04 17:45:11 -040059 factory.inflate(R.layout.title_bar, this);
Michael Kolb8233fac2010-10-26 16:08:53 -070060 mActivity = activity;
Leon Scroggins81db3662009-06-04 17:45:11 -040061
John Reck92026732011-02-15 10:12:30 -080062 mUrlInput = (UrlInputView) findViewById(R.id.url_input);
63 mUrlInput.setCompoundDrawablePadding(5);
64 mUrlInput.setContainer(this);
65 mUrlInput.setSelectAllOnFocus(true);
66 mUrlInput.setController(mUiController);
67 mUrlInput.setUrlInputListener(this);
68 mUrlInput.setOnFocusChangeListener(this);
Leon Scroggins81db3662009-06-04 17:45:11 -040069
Leon Scroggins62e8f942009-09-03 15:08:54 -040070 mLockIcon = (ImageView) findViewById(R.id.lock);
Leon Scrogginsf4bb18a2009-09-11 18:37:53 -040071 mFavicon = (ImageView) findViewById(R.id.favicon);
John Reckbb2a3452011-02-15 11:31:56 -080072 mStopButton = (ImageButton) findViewById(R.id.stop);
73 mBookmarkButton = (ImageButton) findViewById(R.id.bookmark);
74 mStopButton.setOnClickListener(this);
75 mBookmarkButton.setOnClickListener(this);
Leon Scroggins62e8f942009-09-03 15:08:54 -040076
John Reck94b7e042011-02-15 15:02:33 -080077 mHorizontalProgress = (PageProgressView) findViewById(
Leon Scroggins81db3662009-06-04 17:45:11 -040078 R.id.progress_horizontal);
Leon Scroggins11e71b12010-01-25 10:40:38 -050079 mVoiceSearchIntent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
80 mVoiceSearchIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
81 RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH);
Mike LeBeau9f677bc2010-04-01 14:57:10 -070082 // This extra tells voice search not to send the application id in its
83 // results intent - http://b/2546173
84 //
85 // TODO: Make a constant for this extra.
Michael Kolb8233fac2010-10-26 16:08:53 -070086 mVoiceSearchIntent.putExtra("android.speech.extras.SEND_APPLICATION_ID_EXTRA",
87 false);
88 PackageManager pm = activity.getPackageManager();
Leon Scroggins11e71b12010-01-25 10:40:38 -050089 ResolveInfo ri = pm.resolveActivity(mVoiceSearchIntent,
90 PackageManager.MATCH_DEFAULT_ONLY);
John Reck94b7e042011-02-15 15:02:33 -080091 Resources resources = getResources();
Leon Scroggins11e71b12010-01-25 10:40:38 -050092 if (ri == null) {
93 mVoiceSearchIntent = null;
94 } else {
95 mVoiceDrawable = resources.getDrawable(
96 android.R.drawable.ic_btn_speak_now);
97 }
John Reckbb2a3452011-02-15 11:31:56 -080098 mBookmarkDrawable = mBookmarkButton.getDrawable();
Michael Kolb8233fac2010-10-26 16:08:53 -070099 mArcsSpan = new ImageSpan(activity, R.drawable.arcs,
Leon Scrogginsf48d44e2010-02-05 16:41:49 -0500100 ImageSpan.ALIGN_BASELINE);
Leon Scrogginse4b3bda2009-06-09 15:46:41 -0400101 }
102
Leon Scrogginsc6fa1102009-09-21 10:40:01 -0400103 @Override
Leon Scroggins4e9f89b2010-02-22 16:54:14 -0500104 public void createContextMenu(ContextMenu menu) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700105 MenuInflater inflater = mActivity.getMenuInflater();
Leon Scrogginsc6fa1102009-09-21 10:40:01 -0400106 inflater.inflate(R.menu.title_context, menu);
Michael Kolb8233fac2010-10-26 16:08:53 -0700107 mActivity.onCreateContextMenu(menu, this, null);
Leon Scrogginsc6fa1102009-09-21 10:40:01 -0400108 }
109
Leon Scroggins1f005d32009-08-10 17:36:42 -0400110 /**
Leon Scroggins11e71b12010-01-25 10:40:38 -0500111 * Change the TitleBar to or from voice mode. If there is no package to
112 * handle voice search, the TitleBar cannot be set to voice mode.
113 */
Michael Kolb8233fac2010-10-26 16:08:53 -0700114 @Override
115 void setInVoiceMode(boolean inVoiceMode) {
Leon Scroggins11e71b12010-01-25 10:40:38 -0500116 if (mInVoiceMode == inVoiceMode) return;
117 mInVoiceMode = inVoiceMode && mVoiceSearchIntent != null;
Leon Scrogginsb3b04f72010-03-03 17:17:18 -0500118 Drawable titleDrawable;
Leon Scroggins11e71b12010-01-25 10:40:38 -0500119 if (mInVoiceMode) {
John Reckbb2a3452011-02-15 11:31:56 -0800120 mBookmarkButton.setImageDrawable(mVoiceDrawable);
John Reck92026732011-02-15 10:12:30 -0800121 mUrlInput.setEllipsize(null);
John Reckbb2a3452011-02-15 11:31:56 -0800122 mBookmarkButton.setVisibility(View.VISIBLE);
Leon Scrogginsb3b04f72010-03-03 17:17:18 -0500123 mStopButton.setVisibility(View.GONE);
Leon Scroggins11e71b12010-01-25 10:40:38 -0500124 } else {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500125 if (mInLoad) {
John Reckbb2a3452011-02-15 11:31:56 -0800126 mBookmarkButton.setVisibility(View.GONE);
Leon Scrogginsb3b04f72010-03-03 17:17:18 -0500127 mStopButton.setVisibility(View.VISIBLE);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500128 } else {
John Reckbb2a3452011-02-15 11:31:56 -0800129 mBookmarkButton.setVisibility(View.VISIBLE);
Leon Scrogginsb3b04f72010-03-03 17:17:18 -0500130 mStopButton.setVisibility(View.GONE);
John Reckbb2a3452011-02-15 11:31:56 -0800131 mBookmarkButton.setImageDrawable(mBookmarkDrawable);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500132 }
John Reck92026732011-02-15 10:12:30 -0800133 mUrlInput.setEllipsize(TextUtils.TruncateAt.END);
Leon Scroggins11e71b12010-01-25 10:40:38 -0500134 }
John Reck92026732011-02-15 10:12:30 -0800135 mUrlInput.setSingleLine(!mInVoiceMode);
Leon Scroggins11e71b12010-01-25 10:40:38 -0500136 }
137
138 /**
Leon Scroggins1f005d32009-08-10 17:36:42 -0400139 * Update the progress, from 0 to 100.
140 */
Michael Kolb8233fac2010-10-26 16:08:53 -0700141 @Override
142 void setProgress(int newProgress) {
John Reck94b7e042011-02-15 15:02:33 -0800143 if (newProgress >= PROGRESS_MAX) {
144 mHorizontalProgress.setVisibility(View.GONE);
Leon Scroggins11e71b12010-01-25 10:40:38 -0500145 if (!mInVoiceMode) {
John Reckbb2a3452011-02-15 11:31:56 -0800146 mBookmarkButton.setImageDrawable(mBookmarkDrawable);
147 mBookmarkButton.setVisibility(View.VISIBLE);
Leon Scrogginsb3b04f72010-03-03 17:17:18 -0500148 mStopButton.setVisibility(View.GONE);
Leon Scroggins1f005d32009-08-10 17:36:42 -0400149 }
Leon Scroggins81db3662009-06-04 17:45:11 -0400150 mInLoad = false;
151 } else {
John Reck94b7e042011-02-15 15:02:33 -0800152 mHorizontalProgress.setProgress(newProgress * PageProgressView.MAX_PROGRESS
153 / PROGRESS_MAX);
154 if (!mInLoad) {
Leon Scroggins62e8f942009-09-03 15:08:54 -0400155 mHorizontalProgress.setVisibility(View.VISIBLE);
Leon Scroggins11e71b12010-01-25 10:40:38 -0500156 if (!mInVoiceMode) {
John Reckbb2a3452011-02-15 11:31:56 -0800157 mBookmarkButton.setVisibility(View.GONE);
Leon Scrogginsb3b04f72010-03-03 17:17:18 -0500158 mStopButton.setVisibility(View.VISIBLE);
Leon Scroggins62e8f942009-09-03 15:08:54 -0400159 }
160 mInLoad = true;
Leon Scrogginse4b3bda2009-06-09 15:46:41 -0400161 }
Leon Scroggins81db3662009-06-04 17:45:11 -0400162 }
163 }
164
Leon Scroggins1f005d32009-08-10 17:36:42 -0400165 /**
Leon Scroggins58d56c62010-01-28 15:12:40 -0500166 * Update the text displayed in the title bar.
John Reckef074262010-12-02 16:09:14 -0800167 * @param title String to display. If null, the new tab string will be
Leon Scroggins58d56c62010-01-28 15:12:40 -0500168 * shown.
Leon Scroggins1f005d32009-08-10 17:36:42 -0400169 */
Michael Kolb8233fac2010-10-26 16:08:53 -0700170 @Override
171 void setDisplayTitle(String title) {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500172 if (title == null) {
John Reck92026732011-02-15 10:12:30 -0800173 mUrlInput.setText(R.string.new_tab);
Leon Scrogginsd7c3dd52009-07-20 13:38:47 -0400174 } else {
Leon Scrogginsf48d44e2010-02-05 16:41:49 -0500175 if (mInVoiceMode) {
176 // Add two spaces. The second one will be replaced with an
177 // image, and the first one will put space between it and the
178 // text
179 SpannableString spannable = new SpannableString(title + " ");
180 int end = spannable.length();
181 spannable.setSpan(mArcsSpan, end - 1, end,
182 Spanned.SPAN_MARK_POINT);
John Reck92026732011-02-15 10:12:30 -0800183 mUrlInput.setText(spannable);
Leon Scrogginsf48d44e2010-02-05 16:41:49 -0500184 } else {
John Reck92026732011-02-15 10:12:30 -0800185 mUrlInput.setText(title);
Leon Scrogginsf48d44e2010-02-05 16:41:49 -0500186 }
Leon Scrogginsd7c3dd52009-07-20 13:38:47 -0400187 }
Leon Scroggins81db3662009-06-04 17:45:11 -0400188 }
John Reck92026732011-02-15 10:12:30 -0800189
190 @Override
191 public void onFocusChange(View v, boolean hasFocus) {
192 if (v == mUrlInput && hasFocus) {
193 mActivity.closeOptionsMenu();
194 }
195 }
John Reckbb2a3452011-02-15 11:31:56 -0800196
197 @Override
198 public void onClick(View v) {
199 if (v == mStopButton) {
200 mUiController.stopLoading();
201 } else if (v == mBookmarkButton) {
202 mUiController.bookmarkCurrentPage(AddBookmarkPage.DEFAULT_FOLDER_ID,
203 true);
204 }
205 }
John Reck94b7e042011-02-15 15:02:33 -0800206
207 @Override
208 public void setCurrentUrlIsBookmark(boolean isBookmark) {
209 mBookmarkButton.setActivated(isBookmark);
210 }
Leon Scroggins81db3662009-06-04 17:45:11 -0400211}