Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.browser; |
| 18 | |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 19 | import android.app.Activity; |
Leon Scroggins | 3bbb6ca | 2009-09-09 12:51:10 -0400 | [diff] [blame] | 20 | import android.content.res.Resources; |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 21 | import android.graphics.drawable.Drawable; |
Leon Scroggins | f48d44e | 2010-02-05 16:41:49 -0500 | [diff] [blame] | 22 | import android.text.SpannableString; |
| 23 | import android.text.Spanned; |
Leon Scroggins | 76e1686 | 2010-02-08 14:39:34 -0500 | [diff] [blame] | 24 | import android.text.TextUtils; |
Leon Scroggins | f48d44e | 2010-02-05 16:41:49 -0500 | [diff] [blame] | 25 | import android.text.style.ImageSpan; |
Leon Scroggins | c6fa110 | 2009-09-21 10:40:01 -0400 | [diff] [blame] | 26 | import android.view.ContextMenu; |
Leon Scroggins | e4b3bda | 2009-06-09 15:46:41 -0400 | [diff] [blame] | 27 | import android.view.LayoutInflater; |
Leon Scroggins | c6fa110 | 2009-09-21 10:40:01 -0400 | [diff] [blame] | 28 | import android.view.MenuInflater; |
Leon Scroggins | e4b3bda | 2009-06-09 15:46:41 -0400 | [diff] [blame] | 29 | import android.view.View; |
John Reck | bb2a345 | 2011-02-15 11:31:56 -0800 | [diff] [blame] | 30 | import android.view.View.OnClickListener; |
John Reck | 9202673 | 2011-02-15 10:12:30 -0800 | [diff] [blame] | 31 | import android.view.View.OnFocusChangeListener; |
| 32 | import android.widget.ImageButton; |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 33 | import android.widget.ImageView; |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 34 | |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 35 | /** |
| 36 | * This class represents a title bar for a particular "tab" or "window" in the |
| 37 | * browser. |
| 38 | */ |
John Reck | bb2a345 | 2011-02-15 11:31:56 -0800 | [diff] [blame] | 39 | public class TitleBar extends TitleBarBase implements OnFocusChangeListener, |
| 40 | OnClickListener { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 41 | |
| 42 | private Activity mActivity; |
John Reck | bb2a345 | 2011-02-15 11:31:56 -0800 | [diff] [blame] | 43 | private ImageButton mBookmarkButton; |
John Reck | 94b7e04 | 2011-02-15 15:02:33 -0800 | [diff] [blame] | 44 | private PageProgressView mHorizontalProgress; |
John Reck | bb2a345 | 2011-02-15 11:31:56 -0800 | [diff] [blame] | 45 | private ImageButton mStopButton; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 46 | private Drawable mBookmarkDrawable; |
| 47 | private Drawable mVoiceDrawable; |
| 48 | private boolean mInLoad; |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 49 | private ImageSpan mArcsSpan; |
John Reck | b9a051b | 2011-03-18 11:55:48 -0700 | [diff] [blame] | 50 | private View mContainer; |
Leon Scroggins | c6fa110 | 2009-09-21 10:40:01 -0400 | [diff] [blame] | 51 | |
John Reck | 9202673 | 2011-02-15 10:12:30 -0800 | [diff] [blame] | 52 | public TitleBar(Activity activity, UiController controller, PhoneUi ui) { |
| 53 | super(activity, controller, ui); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 54 | LayoutInflater factory = LayoutInflater.from(activity); |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 55 | factory.inflate(R.layout.title_bar, this); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 56 | mActivity = activity; |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 57 | |
John Reck | b9a051b | 2011-03-18 11:55:48 -0700 | [diff] [blame] | 58 | mContainer = findViewById(R.id.taburlbar); |
John Reck | 9202673 | 2011-02-15 10:12:30 -0800 | [diff] [blame] | 59 | 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 Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 66 | |
Leon Scroggins | 62e8f94 | 2009-09-03 15:08:54 -0400 | [diff] [blame] | 67 | mLockIcon = (ImageView) findViewById(R.id.lock); |
Leon Scroggins | f4bb18a | 2009-09-11 18:37:53 -0400 | [diff] [blame] | 68 | mFavicon = (ImageView) findViewById(R.id.favicon); |
John Reck | bb2a345 | 2011-02-15 11:31:56 -0800 | [diff] [blame] | 69 | mStopButton = (ImageButton) findViewById(R.id.stop); |
| 70 | mBookmarkButton = (ImageButton) findViewById(R.id.bookmark); |
| 71 | mStopButton.setOnClickListener(this); |
| 72 | mBookmarkButton.setOnClickListener(this); |
Leon Scroggins | 62e8f94 | 2009-09-03 15:08:54 -0400 | [diff] [blame] | 73 | |
John Reck | 94b7e04 | 2011-02-15 15:02:33 -0800 | [diff] [blame] | 74 | mHorizontalProgress = (PageProgressView) findViewById( |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 75 | R.id.progress_horizontal); |
John Reck | 94b7e04 | 2011-02-15 15:02:33 -0800 | [diff] [blame] | 76 | Resources resources = getResources(); |
Michael Kolb | 736990c | 2011-03-20 10:01:20 -0700 | [diff] [blame^] | 77 | mVoiceDrawable = resources.getDrawable( |
| 78 | android.R.drawable.ic_btn_speak_now); |
John Reck | bb2a345 | 2011-02-15 11:31:56 -0800 | [diff] [blame] | 79 | mBookmarkDrawable = mBookmarkButton.getDrawable(); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 80 | mArcsSpan = new ImageSpan(activity, R.drawable.arcs, |
Leon Scroggins | f48d44e | 2010-02-05 16:41:49 -0500 | [diff] [blame] | 81 | ImageSpan.ALIGN_BASELINE); |
Leon Scroggins | e4b3bda | 2009-06-09 15:46:41 -0400 | [diff] [blame] | 82 | } |
| 83 | |
Leon Scroggins | c6fa110 | 2009-09-21 10:40:01 -0400 | [diff] [blame] | 84 | @Override |
John Reck | b9a051b | 2011-03-18 11:55:48 -0700 | [diff] [blame] | 85 | public int getEmbeddedHeight() { |
| 86 | int height = mContainer.getHeight(); |
| 87 | return height; |
| 88 | } |
| 89 | |
| 90 | @Override |
Leon Scroggins | 4e9f89b | 2010-02-22 16:54:14 -0500 | [diff] [blame] | 91 | public void createContextMenu(ContextMenu menu) { |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 92 | MenuInflater inflater = mActivity.getMenuInflater(); |
Leon Scroggins | c6fa110 | 2009-09-21 10:40:01 -0400 | [diff] [blame] | 93 | inflater.inflate(R.menu.title_context, menu); |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 94 | mActivity.onCreateContextMenu(menu, this, null); |
Leon Scroggins | c6fa110 | 2009-09-21 10:40:01 -0400 | [diff] [blame] | 95 | } |
| 96 | |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 97 | /** |
Leon Scroggins | 11e71b1 | 2010-01-25 10:40:38 -0500 | [diff] [blame] | 98 | * 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 Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 101 | @Override |
| 102 | void setInVoiceMode(boolean inVoiceMode) { |
Leon Scroggins | 11e71b1 | 2010-01-25 10:40:38 -0500 | [diff] [blame] | 103 | if (mInVoiceMode == inVoiceMode) return; |
Michael Kolb | 736990c | 2011-03-20 10:01:20 -0700 | [diff] [blame^] | 104 | mInVoiceMode = inVoiceMode && mUiController.supportsVoiceSearch(); |
Leon Scroggins | b3b04f7 | 2010-03-03 17:17:18 -0500 | [diff] [blame] | 105 | Drawable titleDrawable; |
Leon Scroggins | 11e71b1 | 2010-01-25 10:40:38 -0500 | [diff] [blame] | 106 | if (mInVoiceMode) { |
John Reck | bb2a345 | 2011-02-15 11:31:56 -0800 | [diff] [blame] | 107 | mBookmarkButton.setImageDrawable(mVoiceDrawable); |
John Reck | 9202673 | 2011-02-15 10:12:30 -0800 | [diff] [blame] | 108 | mUrlInput.setEllipsize(null); |
John Reck | bb2a345 | 2011-02-15 11:31:56 -0800 | [diff] [blame] | 109 | mBookmarkButton.setVisibility(View.VISIBLE); |
Leon Scroggins | b3b04f7 | 2010-03-03 17:17:18 -0500 | [diff] [blame] | 110 | mStopButton.setVisibility(View.GONE); |
Leon Scroggins | 11e71b1 | 2010-01-25 10:40:38 -0500 | [diff] [blame] | 111 | } else { |
Leon Scroggins | 58d56c6 | 2010-01-28 15:12:40 -0500 | [diff] [blame] | 112 | if (mInLoad) { |
John Reck | bb2a345 | 2011-02-15 11:31:56 -0800 | [diff] [blame] | 113 | mBookmarkButton.setVisibility(View.GONE); |
Leon Scroggins | b3b04f7 | 2010-03-03 17:17:18 -0500 | [diff] [blame] | 114 | mStopButton.setVisibility(View.VISIBLE); |
Leon Scroggins | 58d56c6 | 2010-01-28 15:12:40 -0500 | [diff] [blame] | 115 | } else { |
John Reck | bb2a345 | 2011-02-15 11:31:56 -0800 | [diff] [blame] | 116 | mBookmarkButton.setVisibility(View.VISIBLE); |
Leon Scroggins | b3b04f7 | 2010-03-03 17:17:18 -0500 | [diff] [blame] | 117 | mStopButton.setVisibility(View.GONE); |
John Reck | bb2a345 | 2011-02-15 11:31:56 -0800 | [diff] [blame] | 118 | mBookmarkButton.setImageDrawable(mBookmarkDrawable); |
Leon Scroggins | 58d56c6 | 2010-01-28 15:12:40 -0500 | [diff] [blame] | 119 | } |
John Reck | 9202673 | 2011-02-15 10:12:30 -0800 | [diff] [blame] | 120 | mUrlInput.setEllipsize(TextUtils.TruncateAt.END); |
Leon Scroggins | 11e71b1 | 2010-01-25 10:40:38 -0500 | [diff] [blame] | 121 | } |
John Reck | 9202673 | 2011-02-15 10:12:30 -0800 | [diff] [blame] | 122 | mUrlInput.setSingleLine(!mInVoiceMode); |
Leon Scroggins | 11e71b1 | 2010-01-25 10:40:38 -0500 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | /** |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 126 | * Update the progress, from 0 to 100. |
| 127 | */ |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 128 | @Override |
| 129 | void setProgress(int newProgress) { |
John Reck | 94b7e04 | 2011-02-15 15:02:33 -0800 | [diff] [blame] | 130 | if (newProgress >= PROGRESS_MAX) { |
| 131 | mHorizontalProgress.setVisibility(View.GONE); |
Leon Scroggins | 11e71b1 | 2010-01-25 10:40:38 -0500 | [diff] [blame] | 132 | if (!mInVoiceMode) { |
John Reck | bb2a345 | 2011-02-15 11:31:56 -0800 | [diff] [blame] | 133 | mBookmarkButton.setImageDrawable(mBookmarkDrawable); |
| 134 | mBookmarkButton.setVisibility(View.VISIBLE); |
Leon Scroggins | b3b04f7 | 2010-03-03 17:17:18 -0500 | [diff] [blame] | 135 | mStopButton.setVisibility(View.GONE); |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 136 | } |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 137 | mInLoad = false; |
| 138 | } else { |
John Reck | 94b7e04 | 2011-02-15 15:02:33 -0800 | [diff] [blame] | 139 | mHorizontalProgress.setProgress(newProgress * PageProgressView.MAX_PROGRESS |
| 140 | / PROGRESS_MAX); |
| 141 | if (!mInLoad) { |
Leon Scroggins | 62e8f94 | 2009-09-03 15:08:54 -0400 | [diff] [blame] | 142 | mHorizontalProgress.setVisibility(View.VISIBLE); |
Leon Scroggins | 11e71b1 | 2010-01-25 10:40:38 -0500 | [diff] [blame] | 143 | if (!mInVoiceMode) { |
John Reck | bb2a345 | 2011-02-15 11:31:56 -0800 | [diff] [blame] | 144 | mBookmarkButton.setVisibility(View.GONE); |
Leon Scroggins | b3b04f7 | 2010-03-03 17:17:18 -0500 | [diff] [blame] | 145 | mStopButton.setVisibility(View.VISIBLE); |
Leon Scroggins | 62e8f94 | 2009-09-03 15:08:54 -0400 | [diff] [blame] | 146 | } |
| 147 | mInLoad = true; |
Leon Scroggins | e4b3bda | 2009-06-09 15:46:41 -0400 | [diff] [blame] | 148 | } |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 149 | } |
| 150 | } |
| 151 | |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 152 | /** |
Leon Scroggins | 58d56c6 | 2010-01-28 15:12:40 -0500 | [diff] [blame] | 153 | * Update the text displayed in the title bar. |
John Reck | ef07426 | 2010-12-02 16:09:14 -0800 | [diff] [blame] | 154 | * @param title String to display. If null, the new tab string will be |
Leon Scroggins | 58d56c6 | 2010-01-28 15:12:40 -0500 | [diff] [blame] | 155 | * shown. |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 156 | */ |
Michael Kolb | 8233fac | 2010-10-26 16:08:53 -0700 | [diff] [blame] | 157 | @Override |
| 158 | void setDisplayTitle(String title) { |
Leon Scroggins | 58d56c6 | 2010-01-28 15:12:40 -0500 | [diff] [blame] | 159 | if (title == null) { |
John Reck | 9202673 | 2011-02-15 10:12:30 -0800 | [diff] [blame] | 160 | mUrlInput.setText(R.string.new_tab); |
Leon Scroggins | d7c3dd5 | 2009-07-20 13:38:47 -0400 | [diff] [blame] | 161 | } else { |
Leon Scroggins | f48d44e | 2010-02-05 16:41:49 -0500 | [diff] [blame] | 162 | 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 Reck | 9202673 | 2011-02-15 10:12:30 -0800 | [diff] [blame] | 170 | mUrlInput.setText(spannable); |
Leon Scroggins | f48d44e | 2010-02-05 16:41:49 -0500 | [diff] [blame] | 171 | } else { |
John Reck | 9202673 | 2011-02-15 10:12:30 -0800 | [diff] [blame] | 172 | mUrlInput.setText(title); |
Leon Scroggins | f48d44e | 2010-02-05 16:41:49 -0500 | [diff] [blame] | 173 | } |
Leon Scroggins | d7c3dd5 | 2009-07-20 13:38:47 -0400 | [diff] [blame] | 174 | } |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 175 | } |
John Reck | 9202673 | 2011-02-15 10:12:30 -0800 | [diff] [blame] | 176 | |
| 177 | @Override |
| 178 | public void onFocusChange(View v, boolean hasFocus) { |
| 179 | if (v == mUrlInput && hasFocus) { |
| 180 | mActivity.closeOptionsMenu(); |
| 181 | } |
| 182 | } |
John Reck | bb2a345 | 2011-02-15 11:31:56 -0800 | [diff] [blame] | 183 | |
| 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 Reck | 94b7e04 | 2011-02-15 15:02:33 -0800 | [diff] [blame] | 193 | |
| 194 | @Override |
| 195 | public void setCurrentUrlIsBookmark(boolean isBookmark) { |
| 196 | mBookmarkButton.setActivated(isBookmark); |
| 197 | } |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 198 | } |