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 | |
| 19 | import android.content.Context; |
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; |
| 24 | import android.graphics.Bitmap; |
| 25 | import android.graphics.Color; |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 26 | import android.graphics.Rect; |
Leon Scroggins | 62e8f94 | 2009-09-03 15:08:54 -0400 | [diff] [blame] | 27 | import android.graphics.drawable.Animatable; |
Leon Scroggins | 3bbb6ca | 2009-09-09 12:51:10 -0400 | [diff] [blame] | 28 | import android.graphics.drawable.BitmapDrawable; |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 29 | import android.graphics.drawable.Drawable; |
Leon Scroggins | 3bbb6ca | 2009-09-09 12:51:10 -0400 | [diff] [blame] | 30 | import android.graphics.drawable.LayerDrawable; |
| 31 | import android.graphics.drawable.PaintDrawable; |
Leon Scroggins | c6fa110 | 2009-09-21 10:40:01 -0400 | [diff] [blame] | 32 | import android.os.Handler; |
| 33 | import android.os.Message; |
Leon Scroggins | 11e71b1 | 2010-01-25 10:40:38 -0500 | [diff] [blame] | 34 | import android.speech.RecognizerIntent; |
Leon Scroggins | f48d44e | 2010-02-05 16:41:49 -0500 | [diff] [blame] | 35 | import android.text.SpannableString; |
| 36 | import android.text.Spanned; |
Leon Scroggins | 76e1686 | 2010-02-08 14:39:34 -0500 | [diff] [blame] | 37 | import android.text.TextUtils; |
Leon Scroggins | f48d44e | 2010-02-05 16:41:49 -0500 | [diff] [blame] | 38 | import android.text.style.ImageSpan; |
Leon Scroggins | 9535cee | 2010-03-15 20:37:16 -0400 | [diff] [blame] | 39 | import android.util.DisplayMetrics; |
Leon Scroggins | 3bbb6ca | 2009-09-09 12:51:10 -0400 | [diff] [blame] | 40 | import android.util.TypedValue; |
Leon Scroggins | c6fa110 | 2009-09-21 10:40:01 -0400 | [diff] [blame] | 41 | import android.view.ContextMenu; |
Leon Scroggins | e4b3bda | 2009-06-09 15:46:41 -0400 | [diff] [blame] | 42 | import android.view.LayoutInflater; |
Leon Scroggins | c6fa110 | 2009-09-21 10:40:01 -0400 | [diff] [blame] | 43 | import android.view.MenuInflater; |
Leon Scroggins | 6857939 | 2009-09-15 15:31:54 -0400 | [diff] [blame] | 44 | import android.view.MotionEvent; |
Leon Scroggins | e4b3bda | 2009-06-09 15:46:41 -0400 | [diff] [blame] | 45 | import android.view.View; |
Leon Scroggins | 6857939 | 2009-09-15 15:31:54 -0400 | [diff] [blame] | 46 | import android.view.ViewConfiguration; |
Leon Scroggins | 9535cee | 2010-03-15 20:37:16 -0400 | [diff] [blame] | 47 | import android.view.ViewGroup; |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 48 | import android.widget.ImageView; |
| 49 | import android.widget.LinearLayout; |
| 50 | import android.widget.ProgressBar; |
| 51 | import android.widget.TextView; |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 52 | |
Leon Scroggins | 1fe13a5 | 2010-02-09 15:31:26 -0500 | [diff] [blame] | 53 | import com.android.common.speech.LoggingEvents; |
| 54 | |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 55 | /** |
| 56 | * This class represents a title bar for a particular "tab" or "window" in the |
| 57 | * browser. |
| 58 | */ |
Leon Scroggins | e4b3bda | 2009-06-09 15:46:41 -0400 | [diff] [blame] | 59 | public class TitleBar extends LinearLayout { |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 60 | private TextView mTitle; |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 61 | private Drawable mCloseDrawable; |
| 62 | private ImageView mRtButton; |
Leon Scroggins | 62e8f94 | 2009-09-03 15:08:54 -0400 | [diff] [blame] | 63 | private Drawable mCircularProgress; |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 64 | private ProgressBar mHorizontalProgress; |
Leon Scroggins | f4bb18a | 2009-09-11 18:37:53 -0400 | [diff] [blame] | 65 | private ImageView mFavicon; |
Leon Scroggins | 62e8f94 | 2009-09-03 15:08:54 -0400 | [diff] [blame] | 66 | private ImageView mLockIcon; |
Leon Scroggins | b3b04f7 | 2010-03-03 17:17:18 -0500 | [diff] [blame] | 67 | private ImageView mStopButton; |
Leon Scroggins | a81a764 | 2009-08-31 17:05:41 -0400 | [diff] [blame] | 68 | private Drawable mBookmarkDrawable; |
Leon Scroggins | 11e71b1 | 2010-01-25 10:40:38 -0500 | [diff] [blame] | 69 | private Drawable mVoiceDrawable; |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 70 | private boolean mInLoad; |
Leon Scroggins | a81a764 | 2009-08-31 17:05:41 -0400 | [diff] [blame] | 71 | private BrowserActivity mBrowserActivity; |
Leon Scroggins | 3bbb6ca | 2009-09-09 12:51:10 -0400 | [diff] [blame] | 72 | private Drawable mGenericFavicon; |
| 73 | private int mIconDimension; |
Leon Scroggins | 6857939 | 2009-09-15 15:31:54 -0400 | [diff] [blame] | 74 | private View mTitleBg; |
Leon Scroggins | c6fa110 | 2009-09-21 10:40:01 -0400 | [diff] [blame] | 75 | private MyHandler mHandler; |
Leon Scroggins | 11e71b1 | 2010-01-25 10:40:38 -0500 | [diff] [blame] | 76 | private Intent mVoiceSearchIntent; |
| 77 | private boolean mInVoiceMode; |
Leon Scroggins | 58d56c6 | 2010-01-28 15:12:40 -0500 | [diff] [blame] | 78 | private Drawable mVoiceModeBackground; |
| 79 | private Drawable mNormalBackground; |
Leon Scroggins | b3b04f7 | 2010-03-03 17:17:18 -0500 | [diff] [blame] | 80 | private Drawable mLoadingBackground; |
Leon Scroggins | f48d44e | 2010-02-05 16:41:49 -0500 | [diff] [blame] | 81 | private ImageSpan mArcsSpan; |
Leon Scroggins | 9535cee | 2010-03-15 20:37:16 -0400 | [diff] [blame] | 82 | private int mExtraMargin; |
Leon Scroggins | c6fa110 | 2009-09-21 10:40:01 -0400 | [diff] [blame] | 83 | |
| 84 | private static int LONG_PRESS = 1; |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 85 | |
Leon Scroggins | 6857939 | 2009-09-15 15:31:54 -0400 | [diff] [blame] | 86 | public TitleBar(BrowserActivity context) { |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 87 | super(context, null); |
Leon Scroggins | c6fa110 | 2009-09-21 10:40:01 -0400 | [diff] [blame] | 88 | mHandler = new MyHandler(); |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 89 | LayoutInflater factory = LayoutInflater.from(context); |
| 90 | factory.inflate(R.layout.title_bar, this); |
Leon Scroggins | 3bbb6ca | 2009-09-09 12:51:10 -0400 | [diff] [blame] | 91 | mBrowserActivity = context; |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 92 | |
| 93 | mTitle = (TextView) findViewById(R.id.title); |
Leon Scroggins | a81a764 | 2009-08-31 17:05:41 -0400 | [diff] [blame] | 94 | mTitle.setCompoundDrawablePadding(5); |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 95 | |
Leon Scroggins | 6857939 | 2009-09-15 15:31:54 -0400 | [diff] [blame] | 96 | mTitleBg = findViewById(R.id.title_bg); |
Leon Scroggins | 62e8f94 | 2009-09-03 15:08:54 -0400 | [diff] [blame] | 97 | mLockIcon = (ImageView) findViewById(R.id.lock); |
Leon Scroggins | f4bb18a | 2009-09-11 18:37:53 -0400 | [diff] [blame] | 98 | mFavicon = (ImageView) findViewById(R.id.favicon); |
Leon Scroggins | b3b04f7 | 2010-03-03 17:17:18 -0500 | [diff] [blame] | 99 | mStopButton = (ImageView) findViewById(R.id.stop); |
Leon Scroggins | 62e8f94 | 2009-09-03 15:08:54 -0400 | [diff] [blame] | 100 | |
Leon Scroggins | a81a764 | 2009-08-31 17:05:41 -0400 | [diff] [blame] | 101 | mRtButton = (ImageView) findViewById(R.id.rt_btn); |
Leon Scroggins | 3bbb6ca | 2009-09-09 12:51:10 -0400 | [diff] [blame] | 102 | Resources resources = context.getResources(); |
| 103 | mCircularProgress = (Drawable) resources.getDrawable( |
Leon Scroggins | 62e8f94 | 2009-09-03 15:08:54 -0400 | [diff] [blame] | 104 | com.android.internal.R.drawable.search_spinner); |
Leon Scroggins | 9535cee | 2010-03-15 20:37:16 -0400 | [diff] [blame] | 105 | DisplayMetrics metrics = resources.getDisplayMetrics(); |
| 106 | mExtraMargin = (int) TypedValue.applyDimension( |
| 107 | TypedValue.COMPLEX_UNIT_DIP, 6.5f, metrics); |
Leon Scroggins | 3bbb6ca | 2009-09-09 12:51:10 -0400 | [diff] [blame] | 108 | mIconDimension = (int) TypedValue.applyDimension( |
Leon Scroggins | 9535cee | 2010-03-15 20:37:16 -0400 | [diff] [blame] | 109 | TypedValue.COMPLEX_UNIT_DIP, 20f, metrics); |
Leon Scroggins | 3bbb6ca | 2009-09-09 12:51:10 -0400 | [diff] [blame] | 110 | mCircularProgress.setBounds(0, 0, mIconDimension, mIconDimension); |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 111 | mHorizontalProgress = (ProgressBar) findViewById( |
| 112 | R.id.progress_horizontal); |
Leon Scroggins | 3bbb6ca | 2009-09-09 12:51:10 -0400 | [diff] [blame] | 113 | mGenericFavicon = context.getResources().getDrawable( |
| 114 | R.drawable.app_web_browser_sm); |
Leon Scroggins | 11e71b1 | 2010-01-25 10:40:38 -0500 | [diff] [blame] | 115 | mVoiceSearchIntent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH); |
| 116 | mVoiceSearchIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, |
| 117 | RecognizerIntent.LANGUAGE_MODEL_WEB_SEARCH); |
| 118 | PackageManager pm = context.getPackageManager(); |
| 119 | ResolveInfo ri = pm.resolveActivity(mVoiceSearchIntent, |
| 120 | PackageManager.MATCH_DEFAULT_ONLY); |
| 121 | if (ri == null) { |
| 122 | mVoiceSearchIntent = null; |
| 123 | } else { |
| 124 | mVoiceDrawable = resources.getDrawable( |
| 125 | android.R.drawable.ic_btn_speak_now); |
| 126 | } |
Leon Scroggins | 11e71b1 | 2010-01-25 10:40:38 -0500 | [diff] [blame] | 127 | mBookmarkDrawable = mRtButton.getDrawable(); |
Leon Scroggins | 58d56c6 | 2010-01-28 15:12:40 -0500 | [diff] [blame] | 128 | mVoiceModeBackground = resources.getDrawable( |
Leon Scroggins | f48d44e | 2010-02-05 16:41:49 -0500 | [diff] [blame] | 129 | R.drawable.title_voice); |
Leon Scroggins | 58d56c6 | 2010-01-28 15:12:40 -0500 | [diff] [blame] | 130 | mNormalBackground = mTitleBg.getBackground(); |
Leon Scroggins | b3b04f7 | 2010-03-03 17:17:18 -0500 | [diff] [blame] | 131 | mLoadingBackground = resources.getDrawable(R.drawable.title_loading); |
Leon Scroggins | f48d44e | 2010-02-05 16:41:49 -0500 | [diff] [blame] | 132 | mArcsSpan = new ImageSpan(context, R.drawable.arcs, |
| 133 | ImageSpan.ALIGN_BASELINE); |
Leon Scroggins | e4b3bda | 2009-06-09 15:46:41 -0400 | [diff] [blame] | 134 | } |
| 135 | |
Leon Scroggins | c6fa110 | 2009-09-21 10:40:01 -0400 | [diff] [blame] | 136 | private class MyHandler extends Handler { |
| 137 | public void handleMessage(Message msg) { |
| 138 | if (msg.what == LONG_PRESS) { |
| 139 | // Prevent the normal action from happening by setting the title |
| 140 | // bar's state to false. |
| 141 | mTitleBg.setPressed(false); |
| 142 | // Need to call a special method on BrowserActivity for when the |
| 143 | // fake title bar is up, because its ViewGroup does not show a |
| 144 | // context menu. |
| 145 | mBrowserActivity.showTitleBarContextMenu(); |
| 146 | } |
| 147 | } |
| 148 | }; |
| 149 | |
| 150 | @Override |
Leon Scroggins | 4e9f89b | 2010-02-22 16:54:14 -0500 | [diff] [blame] | 151 | public void createContextMenu(ContextMenu menu) { |
Leon Scroggins | c6fa110 | 2009-09-21 10:40:01 -0400 | [diff] [blame] | 152 | MenuInflater inflater = mBrowserActivity.getMenuInflater(); |
| 153 | inflater.inflate(R.menu.title_context, menu); |
Leon Scroggins | 4e9f89b | 2010-02-22 16:54:14 -0500 | [diff] [blame] | 154 | mBrowserActivity.onCreateContextMenu(menu, this, null); |
Leon Scroggins | c6fa110 | 2009-09-21 10:40:01 -0400 | [diff] [blame] | 155 | } |
| 156 | |
Leon Scroggins | 6857939 | 2009-09-15 15:31:54 -0400 | [diff] [blame] | 157 | @Override |
| 158 | public boolean onTouchEvent(MotionEvent event) { |
Leon Scroggins | bf083d2 | 2010-03-22 10:54:04 -0400 | [diff] [blame^] | 159 | ImageView button = mInLoad ? mStopButton : mRtButton; |
Leon Scroggins | 6857939 | 2009-09-15 15:31:54 -0400 | [diff] [blame] | 160 | switch (event.getAction()) { |
| 161 | case MotionEvent.ACTION_DOWN: |
| 162 | // Make all touches hit either the textfield or the button, |
| 163 | // depending on which side of the right edge of the textfield |
| 164 | // they hit. |
| 165 | if ((int) event.getX() > mTitleBg.getRight()) { |
Leon Scroggins | bf083d2 | 2010-03-22 10:54:04 -0400 | [diff] [blame^] | 166 | button.setPressed(true); |
Leon Scroggins | 6857939 | 2009-09-15 15:31:54 -0400 | [diff] [blame] | 167 | } else { |
| 168 | mTitleBg.setPressed(true); |
Leon Scroggins | c6fa110 | 2009-09-21 10:40:01 -0400 | [diff] [blame] | 169 | mHandler.sendMessageDelayed(mHandler.obtainMessage( |
| 170 | LONG_PRESS), |
| 171 | ViewConfiguration.getLongPressTimeout()); |
Leon Scroggins | 6857939 | 2009-09-15 15:31:54 -0400 | [diff] [blame] | 172 | } |
| 173 | break; |
| 174 | case MotionEvent.ACTION_MOVE: |
| 175 | int slop = ViewConfiguration.get(mBrowserActivity) |
| 176 | .getScaledTouchSlop(); |
| 177 | if ((int) event.getY() > getHeight() + slop) { |
| 178 | // We only trigger the actions in ACTION_UP if one or the |
| 179 | // other is pressed. Since the user moved off the title |
| 180 | // bar, mark both as not pressed. |
| 181 | mTitleBg.setPressed(false); |
Leon Scroggins | bf083d2 | 2010-03-22 10:54:04 -0400 | [diff] [blame^] | 182 | button.setPressed(false); |
Leon Scroggins | c6fa110 | 2009-09-21 10:40:01 -0400 | [diff] [blame] | 183 | mHandler.removeMessages(LONG_PRESS); |
Leon Scroggins | 6857939 | 2009-09-15 15:31:54 -0400 | [diff] [blame] | 184 | break; |
| 185 | } |
| 186 | int x = (int) event.getX(); |
| 187 | int titleRight = mTitleBg.getRight(); |
| 188 | if (mTitleBg.isPressed() && x > titleRight + slop) { |
| 189 | mTitleBg.setPressed(false); |
Leon Scroggins | c6fa110 | 2009-09-21 10:40:01 -0400 | [diff] [blame] | 190 | mHandler.removeMessages(LONG_PRESS); |
Leon Scroggins | bf083d2 | 2010-03-22 10:54:04 -0400 | [diff] [blame^] | 191 | } else if (button.isPressed() && x < titleRight - slop) { |
| 192 | button.setPressed(false); |
Leon Scroggins | 6857939 | 2009-09-15 15:31:54 -0400 | [diff] [blame] | 193 | } |
| 194 | break; |
| 195 | case MotionEvent.ACTION_CANCEL: |
Leon Scroggins | bf083d2 | 2010-03-22 10:54:04 -0400 | [diff] [blame^] | 196 | button.setPressed(false); |
Leon Scroggins | 6857939 | 2009-09-15 15:31:54 -0400 | [diff] [blame] | 197 | mTitleBg.setPressed(false); |
Leon Scroggins | c6fa110 | 2009-09-21 10:40:01 -0400 | [diff] [blame] | 198 | mHandler.removeMessages(LONG_PRESS); |
Leon Scroggins | 6857939 | 2009-09-15 15:31:54 -0400 | [diff] [blame] | 199 | break; |
| 200 | case MotionEvent.ACTION_UP: |
Leon Scroggins | bf083d2 | 2010-03-22 10:54:04 -0400 | [diff] [blame^] | 201 | if (button.isPressed()) { |
Leon Scroggins | 11e71b1 | 2010-01-25 10:40:38 -0500 | [diff] [blame] | 202 | if (mInVoiceMode) { |
Leon Scroggins | 1fe13a5 | 2010-02-09 15:31:26 -0500 | [diff] [blame] | 203 | if (mBrowserActivity.getTabControl().getCurrentTab() |
| 204 | .voiceSearchSourceIsGoogle()) { |
| 205 | Intent i = new Intent( |
| 206 | LoggingEvents.ACTION_LOG_EVENT); |
| 207 | i.putExtra(LoggingEvents.EXTRA_EVENT, |
| 208 | LoggingEvents.VoiceSearch.RETRY); |
| 209 | mBrowserActivity.sendBroadcast(i); |
| 210 | } |
Leon Scroggins | 11e71b1 | 2010-01-25 10:40:38 -0500 | [diff] [blame] | 211 | mBrowserActivity.startActivity(mVoiceSearchIntent); |
| 212 | } else if (mInLoad) { |
Leon Scroggins | 6857939 | 2009-09-15 15:31:54 -0400 | [diff] [blame] | 213 | mBrowserActivity.stopLoading(); |
| 214 | } else { |
| 215 | mBrowserActivity.bookmarksOrHistoryPicker(false); |
| 216 | } |
Leon Scroggins | bf083d2 | 2010-03-22 10:54:04 -0400 | [diff] [blame^] | 217 | button.setPressed(false); |
Leon Scroggins | 6857939 | 2009-09-15 15:31:54 -0400 | [diff] [blame] | 218 | } else if (mTitleBg.isPressed()) { |
Leon Scroggins | c6fa110 | 2009-09-21 10:40:01 -0400 | [diff] [blame] | 219 | mHandler.removeMessages(LONG_PRESS); |
Leon Scroggins | 58d56c6 | 2010-01-28 15:12:40 -0500 | [diff] [blame] | 220 | if (mInVoiceMode) { |
Leon Scroggins | 1fe13a5 | 2010-02-09 15:31:26 -0500 | [diff] [blame] | 221 | if (mBrowserActivity.getTabControl().getCurrentTab() |
| 222 | .voiceSearchSourceIsGoogle()) { |
| 223 | Intent i = new Intent( |
| 224 | LoggingEvents.ACTION_LOG_EVENT); |
| 225 | i.putExtra(LoggingEvents.EXTRA_EVENT, |
| 226 | LoggingEvents.VoiceSearch.N_BEST_REVEAL); |
| 227 | mBrowserActivity.sendBroadcast(i); |
| 228 | } |
Leon Scroggins | 58d56c6 | 2010-01-28 15:12:40 -0500 | [diff] [blame] | 229 | mBrowserActivity.showVoiceSearchResults( |
Leon Scroggins | ea75407 | 2010-02-08 14:08:30 -0500 | [diff] [blame] | 230 | mTitle.getText().toString().trim()); |
Leon Scroggins | 58d56c6 | 2010-01-28 15:12:40 -0500 | [diff] [blame] | 231 | } else { |
Leon Scroggins | 8ad2992 | 2010-02-16 12:33:55 -0500 | [diff] [blame] | 232 | mBrowserActivity.editUrl(); |
Leon Scroggins | 58d56c6 | 2010-01-28 15:12:40 -0500 | [diff] [blame] | 233 | } |
Leon Scroggins | 6857939 | 2009-09-15 15:31:54 -0400 | [diff] [blame] | 234 | mTitleBg.setPressed(false); |
| 235 | } |
| 236 | break; |
| 237 | default: |
| 238 | break; |
| 239 | } |
| 240 | return true; |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 241 | } |
| 242 | |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 243 | /** |
Leon Scroggins | 3bbb6ca | 2009-09-09 12:51:10 -0400 | [diff] [blame] | 244 | * Set a new Bitmap for the Favicon. |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 245 | */ |
Leon Scroggins | 3bbb6ca | 2009-09-09 12:51:10 -0400 | [diff] [blame] | 246 | /* package */ void setFavicon(Bitmap icon) { |
| 247 | Drawable[] array = new Drawable[3]; |
| 248 | array[0] = new PaintDrawable(Color.BLACK); |
| 249 | PaintDrawable p = new PaintDrawable(Color.WHITE); |
| 250 | array[1] = p; |
| 251 | if (icon == null) { |
| 252 | array[2] = mGenericFavicon; |
| 253 | } else { |
| 254 | array[2] = new BitmapDrawable(icon); |
Leon Scroggins | a81a764 | 2009-08-31 17:05:41 -0400 | [diff] [blame] | 255 | } |
Leon Scroggins | 3bbb6ca | 2009-09-09 12:51:10 -0400 | [diff] [blame] | 256 | LayerDrawable d = new LayerDrawable(array); |
| 257 | d.setLayerInset(1, 1, 1, 1, 1); |
| 258 | d.setLayerInset(2, 2, 2, 2, 2); |
Leon Scroggins | f4bb18a | 2009-09-11 18:37:53 -0400 | [diff] [blame] | 259 | mFavicon.setImageDrawable(d); |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 260 | } |
| 261 | |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 262 | /** |
Leon Scroggins | 11e71b1 | 2010-01-25 10:40:38 -0500 | [diff] [blame] | 263 | * Change the TitleBar to or from voice mode. If there is no package to |
| 264 | * handle voice search, the TitleBar cannot be set to voice mode. |
| 265 | */ |
| 266 | /* package */ void setInVoiceMode(boolean inVoiceMode) { |
| 267 | if (mInVoiceMode == inVoiceMode) return; |
| 268 | mInVoiceMode = inVoiceMode && mVoiceSearchIntent != null; |
Leon Scroggins | b3b04f7 | 2010-03-03 17:17:18 -0500 | [diff] [blame] | 269 | Drawable titleDrawable; |
Leon Scroggins | 9535cee | 2010-03-15 20:37:16 -0400 | [diff] [blame] | 270 | ViewGroup.MarginLayoutParams params |
| 271 | = (ViewGroup.MarginLayoutParams) mTitleBg.getLayoutParams(); |
Leon Scroggins | 11e71b1 | 2010-01-25 10:40:38 -0500 | [diff] [blame] | 272 | if (mInVoiceMode) { |
Leon Scroggins | b3b04f7 | 2010-03-03 17:17:18 -0500 | [diff] [blame] | 273 | mRtButton.setImageDrawable(mVoiceDrawable); |
Leon Scroggins | 58d56c6 | 2010-01-28 15:12:40 -0500 | [diff] [blame] | 274 | titleDrawable = mVoiceModeBackground; |
Leon Scroggins | 76e1686 | 2010-02-08 14:39:34 -0500 | [diff] [blame] | 275 | mTitle.setEllipsize(null); |
Leon Scroggins | b3b04f7 | 2010-03-03 17:17:18 -0500 | [diff] [blame] | 276 | mRtButton.setVisibility(View.VISIBLE); |
| 277 | mStopButton.setVisibility(View.GONE); |
Leon Scroggins | 9535cee | 2010-03-15 20:37:16 -0400 | [diff] [blame] | 278 | if (params != null) { |
| 279 | params.setMargins(0, 0, 0, 0); |
| 280 | } |
Leon Scroggins | 11e71b1 | 2010-01-25 10:40:38 -0500 | [diff] [blame] | 281 | } else { |
Leon Scroggins | 58d56c6 | 2010-01-28 15:12:40 -0500 | [diff] [blame] | 282 | if (mInLoad) { |
Leon Scroggins | b3b04f7 | 2010-03-03 17:17:18 -0500 | [diff] [blame] | 283 | titleDrawable = mLoadingBackground; |
| 284 | mRtButton.setVisibility(View.GONE); |
| 285 | mStopButton.setVisibility(View.VISIBLE); |
Leon Scroggins | 9535cee | 2010-03-15 20:37:16 -0400 | [diff] [blame] | 286 | ViewGroup.MarginLayoutParams stopParams |
| 287 | = (ViewGroup.MarginLayoutParams) |
| 288 | mStopButton.getLayoutParams(); |
| 289 | if (stopParams != null) { |
| 290 | stopParams.setMargins(0,0,0, mExtraMargin); |
| 291 | } |
| 292 | if (params != null) { |
| 293 | params.setMargins(0, 0, 0, mExtraMargin); |
| 294 | } |
Leon Scroggins | 58d56c6 | 2010-01-28 15:12:40 -0500 | [diff] [blame] | 295 | } else { |
Leon Scroggins | b3b04f7 | 2010-03-03 17:17:18 -0500 | [diff] [blame] | 296 | titleDrawable = mNormalBackground; |
| 297 | mRtButton.setVisibility(View.VISIBLE); |
| 298 | mStopButton.setVisibility(View.GONE); |
| 299 | mRtButton.setImageDrawable(mBookmarkDrawable); |
Leon Scroggins | 9535cee | 2010-03-15 20:37:16 -0400 | [diff] [blame] | 300 | if (params != null) { |
| 301 | params.setMargins(0, 0, 0, 0); |
| 302 | } |
Leon Scroggins | 58d56c6 | 2010-01-28 15:12:40 -0500 | [diff] [blame] | 303 | } |
Leon Scroggins | 76e1686 | 2010-02-08 14:39:34 -0500 | [diff] [blame] | 304 | mTitle.setEllipsize(TextUtils.TruncateAt.END); |
Leon Scroggins | 11e71b1 | 2010-01-25 10:40:38 -0500 | [diff] [blame] | 305 | } |
Leon Scroggins | f48d44e | 2010-02-05 16:41:49 -0500 | [diff] [blame] | 306 | mTitle.setSingleLine(!mInVoiceMode); |
Leon Scroggins | 58d56c6 | 2010-01-28 15:12:40 -0500 | [diff] [blame] | 307 | mTitleBg.setBackgroundDrawable(titleDrawable); |
Leon Scroggins | 11e71b1 | 2010-01-25 10:40:38 -0500 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | /** |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 311 | * Set the Drawable for the lock icon, or null to hide it. |
| 312 | */ |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 313 | /* package */ void setLock(Drawable d) { |
Leon Scroggins | 62e8f94 | 2009-09-03 15:08:54 -0400 | [diff] [blame] | 314 | if (null == d) { |
| 315 | mLockIcon.setVisibility(View.GONE); |
| 316 | } else { |
| 317 | mLockIcon.setImageDrawable(d); |
| 318 | mLockIcon.setVisibility(View.VISIBLE); |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 319 | } |
| 320 | } |
| 321 | |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 322 | /** |
| 323 | * Update the progress, from 0 to 100. |
| 324 | */ |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 325 | /* package */ void setProgress(int newProgress) { |
Leon Scroggins | 9535cee | 2010-03-15 20:37:16 -0400 | [diff] [blame] | 326 | ViewGroup.MarginLayoutParams params |
| 327 | = (ViewGroup.MarginLayoutParams) mTitleBg.getLayoutParams(); |
Nicolas Roard | 1382b49 | 2009-09-16 21:50:06 +0100 | [diff] [blame] | 328 | if (newProgress >= mHorizontalProgress.getMax()) { |
Leon Scroggins | f4bb18a | 2009-09-11 18:37:53 -0400 | [diff] [blame] | 329 | mTitle.setCompoundDrawables(null, null, null, null); |
Leon Scroggins | 62e8f94 | 2009-09-03 15:08:54 -0400 | [diff] [blame] | 330 | ((Animatable) mCircularProgress).stop(); |
| 331 | mHorizontalProgress.setVisibility(View.INVISIBLE); |
Leon Scroggins | 11e71b1 | 2010-01-25 10:40:38 -0500 | [diff] [blame] | 332 | if (!mInVoiceMode) { |
Leon Scroggins | a81a764 | 2009-08-31 17:05:41 -0400 | [diff] [blame] | 333 | mRtButton.setImageDrawable(mBookmarkDrawable); |
Leon Scroggins | b3b04f7 | 2010-03-03 17:17:18 -0500 | [diff] [blame] | 334 | mRtButton.setVisibility(View.VISIBLE); |
| 335 | mStopButton.setVisibility(View.GONE); |
| 336 | mTitleBg.setBackgroundDrawable(mNormalBackground); |
Leon Scroggins | 9535cee | 2010-03-15 20:37:16 -0400 | [diff] [blame] | 337 | // Set the margin for the textfield to 0, which is appropriate |
| 338 | // for the normal background |
| 339 | if (params != null) { |
| 340 | params.setMargins(0, 0, 0, 0); |
| 341 | } |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 342 | } |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 343 | mInLoad = false; |
| 344 | } else { |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 345 | mHorizontalProgress.setProgress(newProgress); |
Nicolas Roard | 1382b49 | 2009-09-16 21:50:06 +0100 | [diff] [blame] | 346 | if (!mInLoad && getWindowToken() != null) { |
| 347 | // checking the window token lets us be sure that we |
| 348 | // are attached to a window before starting the animation, |
| 349 | // preventing a potential race condition |
| 350 | // (fix for bug http://b/2115736) |
Leon Scroggins | f4bb18a | 2009-09-11 18:37:53 -0400 | [diff] [blame] | 351 | mTitle.setCompoundDrawables(null, null, mCircularProgress, |
Leon Scroggins | 62e8f94 | 2009-09-03 15:08:54 -0400 | [diff] [blame] | 352 | null); |
| 353 | ((Animatable) mCircularProgress).start(); |
| 354 | mHorizontalProgress.setVisibility(View.VISIBLE); |
Leon Scroggins | 11e71b1 | 2010-01-25 10:40:38 -0500 | [diff] [blame] | 355 | if (!mInVoiceMode) { |
Leon Scroggins | b3b04f7 | 2010-03-03 17:17:18 -0500 | [diff] [blame] | 356 | mTitleBg.setBackgroundDrawable(mLoadingBackground); |
| 357 | mRtButton.setVisibility(View.GONE); |
| 358 | mStopButton.setVisibility(View.VISIBLE); |
Leon Scroggins | 9535cee | 2010-03-15 20:37:16 -0400 | [diff] [blame] | 359 | // Set a margin for the bottom of the textfield and the stop |
| 360 | // button so that the total height matches that of the |
| 361 | // title bar when the normal background is showing. |
| 362 | if (params != null) { |
| 363 | params.setMargins(0,0,0, mExtraMargin); |
| 364 | } |
| 365 | ViewGroup.MarginLayoutParams stopParams |
| 366 | = (ViewGroup.MarginLayoutParams) |
| 367 | mStopButton.getLayoutParams(); |
| 368 | if (stopParams != null) { |
| 369 | stopParams.setMargins(0,0,0, mExtraMargin); |
| 370 | } |
Leon Scroggins | 62e8f94 | 2009-09-03 15:08:54 -0400 | [diff] [blame] | 371 | } |
| 372 | mInLoad = true; |
Leon Scroggins | e4b3bda | 2009-06-09 15:46:41 -0400 | [diff] [blame] | 373 | } |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 374 | } |
| 375 | } |
| 376 | |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 377 | /** |
Leon Scroggins | 58d56c6 | 2010-01-28 15:12:40 -0500 | [diff] [blame] | 378 | * Update the text displayed in the title bar. |
| 379 | * @param title String to display. If null, the loading string will be |
| 380 | * shown. |
Leon Scroggins | 1f005d3 | 2009-08-10 17:36:42 -0400 | [diff] [blame] | 381 | */ |
Leon Scroggins | 58d56c6 | 2010-01-28 15:12:40 -0500 | [diff] [blame] | 382 | /* package */ void setDisplayTitle(String title) { |
| 383 | if (title == null) { |
Leon Scroggins | a81a764 | 2009-08-31 17:05:41 -0400 | [diff] [blame] | 384 | mTitle.setText(R.string.title_bar_loading); |
Leon Scroggins | d7c3dd5 | 2009-07-20 13:38:47 -0400 | [diff] [blame] | 385 | } else { |
Leon Scroggins | f48d44e | 2010-02-05 16:41:49 -0500 | [diff] [blame] | 386 | if (mInVoiceMode) { |
| 387 | // Add two spaces. The second one will be replaced with an |
| 388 | // image, and the first one will put space between it and the |
| 389 | // text |
| 390 | SpannableString spannable = new SpannableString(title + " "); |
| 391 | int end = spannable.length(); |
| 392 | spannable.setSpan(mArcsSpan, end - 1, end, |
| 393 | Spanned.SPAN_MARK_POINT); |
| 394 | mTitle.setText(spannable); |
| 395 | } else { |
| 396 | mTitle.setText(title); |
| 397 | } |
Leon Scroggins | d7c3dd5 | 2009-07-20 13:38:47 -0400 | [diff] [blame] | 398 | } |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | /* package */ void setToTabPicker() { |
| 402 | mTitle.setText(R.string.tab_picker_title); |
| 403 | setFavicon(null); |
| 404 | setLock(null); |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 405 | mHorizontalProgress.setVisibility(View.GONE); |
Leon Scroggins | 81db366 | 2009-06-04 17:45:11 -0400 | [diff] [blame] | 406 | } |
| 407 | } |