blob: 361e94cd0b24b879ad0459b15ee75536b692d2ba [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 Scroggins3bbb6ca2009-09-09 12:51:10 -040020import android.content.res.Resources;
Leon Scroggins81db3662009-06-04 17:45:11 -040021import android.graphics.drawable.Drawable;
Leon Scrogginsf48d44e2010-02-05 16:41:49 -050022import android.text.SpannableString;
23import android.text.Spanned;
Leon Scroggins76e16862010-02-08 14:39:34 -050024import android.text.TextUtils;
Leon Scrogginsf48d44e2010-02-05 16:41:49 -050025import android.text.style.ImageSpan;
Leon Scrogginsc6fa1102009-09-21 10:40:01 -040026import android.view.ContextMenu;
Leon Scrogginse4b3bda2009-06-09 15:46:41 -040027import android.view.LayoutInflater;
Leon Scrogginsc6fa1102009-09-21 10:40:01 -040028import android.view.MenuInflater;
Leon Scrogginse4b3bda2009-06-09 15:46:41 -040029import android.view.View;
John Reckbb2a3452011-02-15 11:31:56 -080030import android.view.View.OnClickListener;
John Reck92026732011-02-15 10:12:30 -080031import android.view.View.OnFocusChangeListener;
32import android.widget.ImageButton;
Leon Scroggins81db3662009-06-04 17:45:11 -040033import android.widget.ImageView;
Leon Scroggins81db3662009-06-04 17:45:11 -040034
Leon Scroggins1f005d32009-08-10 17:36:42 -040035/**
36 * This class represents a title bar for a particular "tab" or "window" in the
37 * browser.
38 */
John Reckbb2a3452011-02-15 11:31:56 -080039public class TitleBar extends TitleBarBase implements OnFocusChangeListener,
40 OnClickListener {
Michael Kolb8233fac2010-10-26 16:08:53 -070041
42 private Activity mActivity;
John Reckbb2a3452011-02-15 11:31:56 -080043 private ImageButton mBookmarkButton;
John Reck94b7e042011-02-15 15:02:33 -080044 private PageProgressView mHorizontalProgress;
John Reckbb2a3452011-02-15 11:31:56 -080045 private ImageButton mStopButton;
Michael Kolb8233fac2010-10-26 16:08:53 -070046 private Drawable mBookmarkDrawable;
47 private Drawable mVoiceDrawable;
48 private boolean mInLoad;
Michael Kolb8233fac2010-10-26 16:08:53 -070049 private ImageSpan mArcsSpan;
John Reckb9a051b2011-03-18 11:55:48 -070050 private View mContainer;
Leon Scrogginsc6fa1102009-09-21 10:40:01 -040051
John Reck92026732011-02-15 10:12:30 -080052 public TitleBar(Activity activity, UiController controller, PhoneUi ui) {
53 super(activity, controller, ui);
Michael Kolb8233fac2010-10-26 16:08:53 -070054 LayoutInflater factory = LayoutInflater.from(activity);
Leon Scroggins81db3662009-06-04 17:45:11 -040055 factory.inflate(R.layout.title_bar, this);
Michael Kolb8233fac2010-10-26 16:08:53 -070056 mActivity = activity;
Leon Scroggins81db3662009-06-04 17:45:11 -040057
John Reckb9a051b2011-03-18 11:55:48 -070058 mContainer = findViewById(R.id.taburlbar);
John Reck92026732011-02-15 10:12:30 -080059 mUrlInput = (UrlInputView) findViewById(R.id.url_input);
60 mUrlInput.setCompoundDrawablePadding(5);
61 mUrlInput.setContainer(this);
62 mUrlInput.setSelectAllOnFocus(true);
63 mUrlInput.setController(mUiController);
64 mUrlInput.setUrlInputListener(this);
65 mUrlInput.setOnFocusChangeListener(this);
Leon Scroggins81db3662009-06-04 17:45:11 -040066
Leon Scroggins62e8f942009-09-03 15:08:54 -040067 mLockIcon = (ImageView) findViewById(R.id.lock);
Leon Scrogginsf4bb18a2009-09-11 18:37:53 -040068 mFavicon = (ImageView) findViewById(R.id.favicon);
John Reckbb2a3452011-02-15 11:31:56 -080069 mStopButton = (ImageButton) findViewById(R.id.stop);
70 mBookmarkButton = (ImageButton) findViewById(R.id.bookmark);
71 mStopButton.setOnClickListener(this);
72 mBookmarkButton.setOnClickListener(this);
Leon Scroggins62e8f942009-09-03 15:08:54 -040073
John Reck94b7e042011-02-15 15:02:33 -080074 mHorizontalProgress = (PageProgressView) findViewById(
Leon Scroggins81db3662009-06-04 17:45:11 -040075 R.id.progress_horizontal);
John Reck94b7e042011-02-15 15:02:33 -080076 Resources resources = getResources();
Michael Kolb736990c2011-03-20 10:01:20 -070077 mVoiceDrawable = resources.getDrawable(
78 android.R.drawable.ic_btn_speak_now);
John Reckbb2a3452011-02-15 11:31:56 -080079 mBookmarkDrawable = mBookmarkButton.getDrawable();
Michael Kolb8233fac2010-10-26 16:08:53 -070080 mArcsSpan = new ImageSpan(activity, R.drawable.arcs,
Leon Scrogginsf48d44e2010-02-05 16:41:49 -050081 ImageSpan.ALIGN_BASELINE);
Leon Scrogginse4b3bda2009-06-09 15:46:41 -040082 }
83
Leon Scrogginsc6fa1102009-09-21 10:40:01 -040084 @Override
John Reckb9a051b2011-03-18 11:55:48 -070085 public int getEmbeddedHeight() {
86 int height = mContainer.getHeight();
87 return height;
88 }
89
90 @Override
Leon Scroggins4e9f89b2010-02-22 16:54:14 -050091 public void createContextMenu(ContextMenu menu) {
Michael Kolb8233fac2010-10-26 16:08:53 -070092 MenuInflater inflater = mActivity.getMenuInflater();
Leon Scrogginsc6fa1102009-09-21 10:40:01 -040093 inflater.inflate(R.menu.title_context, menu);
Michael Kolb8233fac2010-10-26 16:08:53 -070094 mActivity.onCreateContextMenu(menu, this, null);
Leon Scrogginsc6fa1102009-09-21 10:40:01 -040095 }
96
Leon Scroggins1f005d32009-08-10 17:36:42 -040097 /**
Leon Scroggins11e71b12010-01-25 10:40:38 -050098 * Change the TitleBar to or from voice mode. If there is no package to
99 * handle voice search, the TitleBar cannot be set to voice mode.
100 */
Michael Kolb8233fac2010-10-26 16:08:53 -0700101 @Override
102 void setInVoiceMode(boolean inVoiceMode) {
Leon Scroggins11e71b12010-01-25 10:40:38 -0500103 if (mInVoiceMode == inVoiceMode) return;
Michael Kolb736990c2011-03-20 10:01:20 -0700104 mInVoiceMode = inVoiceMode && mUiController.supportsVoiceSearch();
Leon Scrogginsb3b04f72010-03-03 17:17:18 -0500105 Drawable titleDrawable;
Leon Scroggins11e71b12010-01-25 10:40:38 -0500106 if (mInVoiceMode) {
John Reckbb2a3452011-02-15 11:31:56 -0800107 mBookmarkButton.setImageDrawable(mVoiceDrawable);
John Reck92026732011-02-15 10:12:30 -0800108 mUrlInput.setEllipsize(null);
John Reckbb2a3452011-02-15 11:31:56 -0800109 mBookmarkButton.setVisibility(View.VISIBLE);
Leon Scrogginsb3b04f72010-03-03 17:17:18 -0500110 mStopButton.setVisibility(View.GONE);
Leon Scroggins11e71b12010-01-25 10:40:38 -0500111 } else {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500112 if (mInLoad) {
John Reckbb2a3452011-02-15 11:31:56 -0800113 mBookmarkButton.setVisibility(View.GONE);
Leon Scrogginsb3b04f72010-03-03 17:17:18 -0500114 mStopButton.setVisibility(View.VISIBLE);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500115 } else {
John Reckbb2a3452011-02-15 11:31:56 -0800116 mBookmarkButton.setVisibility(View.VISIBLE);
Leon Scrogginsb3b04f72010-03-03 17:17:18 -0500117 mStopButton.setVisibility(View.GONE);
John Reckbb2a3452011-02-15 11:31:56 -0800118 mBookmarkButton.setImageDrawable(mBookmarkDrawable);
Leon Scroggins58d56c62010-01-28 15:12:40 -0500119 }
John Reck92026732011-02-15 10:12:30 -0800120 mUrlInput.setEllipsize(TextUtils.TruncateAt.END);
Leon Scroggins11e71b12010-01-25 10:40:38 -0500121 }
John Reck92026732011-02-15 10:12:30 -0800122 mUrlInput.setSingleLine(!mInVoiceMode);
Leon Scroggins11e71b12010-01-25 10:40:38 -0500123 }
124
125 /**
Leon Scroggins1f005d32009-08-10 17:36:42 -0400126 * Update the progress, from 0 to 100.
127 */
Michael Kolb8233fac2010-10-26 16:08:53 -0700128 @Override
129 void setProgress(int newProgress) {
John Reck94b7e042011-02-15 15:02:33 -0800130 if (newProgress >= PROGRESS_MAX) {
131 mHorizontalProgress.setVisibility(View.GONE);
Leon Scroggins11e71b12010-01-25 10:40:38 -0500132 if (!mInVoiceMode) {
John Reckbb2a3452011-02-15 11:31:56 -0800133 mBookmarkButton.setImageDrawable(mBookmarkDrawable);
134 mBookmarkButton.setVisibility(View.VISIBLE);
Leon Scrogginsb3b04f72010-03-03 17:17:18 -0500135 mStopButton.setVisibility(View.GONE);
Leon Scroggins1f005d32009-08-10 17:36:42 -0400136 }
Leon Scroggins81db3662009-06-04 17:45:11 -0400137 mInLoad = false;
138 } else {
John Reck94b7e042011-02-15 15:02:33 -0800139 mHorizontalProgress.setProgress(newProgress * PageProgressView.MAX_PROGRESS
140 / PROGRESS_MAX);
141 if (!mInLoad) {
Leon Scroggins62e8f942009-09-03 15:08:54 -0400142 mHorizontalProgress.setVisibility(View.VISIBLE);
Leon Scroggins11e71b12010-01-25 10:40:38 -0500143 if (!mInVoiceMode) {
John Reckbb2a3452011-02-15 11:31:56 -0800144 mBookmarkButton.setVisibility(View.GONE);
Leon Scrogginsb3b04f72010-03-03 17:17:18 -0500145 mStopButton.setVisibility(View.VISIBLE);
Leon Scroggins62e8f942009-09-03 15:08:54 -0400146 }
147 mInLoad = true;
Leon Scrogginse4b3bda2009-06-09 15:46:41 -0400148 }
Leon Scroggins81db3662009-06-04 17:45:11 -0400149 }
150 }
151
Leon Scroggins1f005d32009-08-10 17:36:42 -0400152 /**
Leon Scroggins58d56c62010-01-28 15:12:40 -0500153 * Update the text displayed in the title bar.
John Reckef074262010-12-02 16:09:14 -0800154 * @param title String to display. If null, the new tab string will be
Leon Scroggins58d56c62010-01-28 15:12:40 -0500155 * shown.
Leon Scroggins1f005d32009-08-10 17:36:42 -0400156 */
Michael Kolb8233fac2010-10-26 16:08:53 -0700157 @Override
158 void setDisplayTitle(String title) {
Leon Scroggins58d56c62010-01-28 15:12:40 -0500159 if (title == null) {
John Reck92026732011-02-15 10:12:30 -0800160 mUrlInput.setText(R.string.new_tab);
Leon Scrogginsd7c3dd52009-07-20 13:38:47 -0400161 } else {
Leon Scrogginsf48d44e2010-02-05 16:41:49 -0500162 if (mInVoiceMode) {
163 // Add two spaces. The second one will be replaced with an
164 // image, and the first one will put space between it and the
165 // text
166 SpannableString spannable = new SpannableString(title + " ");
167 int end = spannable.length();
168 spannable.setSpan(mArcsSpan, end - 1, end,
169 Spanned.SPAN_MARK_POINT);
John Reck92026732011-02-15 10:12:30 -0800170 mUrlInput.setText(spannable);
Leon Scrogginsf48d44e2010-02-05 16:41:49 -0500171 } else {
John Reck92026732011-02-15 10:12:30 -0800172 mUrlInput.setText(title);
Leon Scrogginsf48d44e2010-02-05 16:41:49 -0500173 }
Leon Scrogginsd7c3dd52009-07-20 13:38:47 -0400174 }
Leon Scroggins81db3662009-06-04 17:45:11 -0400175 }
John Reck92026732011-02-15 10:12:30 -0800176
177 @Override
178 public void onFocusChange(View v, boolean hasFocus) {
179 if (v == mUrlInput && hasFocus) {
180 mActivity.closeOptionsMenu();
181 }
182 }
John Reckbb2a3452011-02-15 11:31:56 -0800183
184 @Override
185 public void onClick(View v) {
186 if (v == mStopButton) {
187 mUiController.stopLoading();
188 } else if (v == mBookmarkButton) {
189 mUiController.bookmarkCurrentPage(AddBookmarkPage.DEFAULT_FOLDER_ID,
190 true);
191 }
192 }
John Reck94b7e042011-02-15 15:02:33 -0800193
194 @Override
195 public void setCurrentUrlIsBookmark(boolean isBookmark) {
196 mBookmarkButton.setActivated(isBookmark);
197 }
Leon Scroggins81db3662009-06-04 17:45:11 -0400198}