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