Leon Scroggins | 571b376 | 2010-05-26 10:25:01 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 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 | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 19 | import com.android.browser.UI.DropdownChangeListener; |
John Reck | 9202673 | 2011-02-15 10:12:30 -0800 | [diff] [blame] | 20 | import com.android.browser.UrlInputView.UrlInputListener; |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 21 | import com.android.browser.autocomplete.SuggestedTextController.TextChangeWatcher; |
John Reck | 9202673 | 2011-02-15 10:12:30 -0800 | [diff] [blame] | 22 | |
Michael Kolb | 46f987e | 2011-04-05 10:39:10 -0700 | [diff] [blame] | 23 | import android.animation.Animator; |
| 24 | import android.animation.Animator.AnimatorListener; |
| 25 | import android.animation.ObjectAnimator; |
John Reck | 9202673 | 2011-02-15 10:12:30 -0800 | [diff] [blame] | 26 | import android.app.SearchManager; |
Leon Scroggins | 571b376 | 2010-05-26 10:25:01 -0400 | [diff] [blame] | 27 | import android.content.Context; |
John Reck | 9202673 | 2011-02-15 10:12:30 -0800 | [diff] [blame] | 28 | import android.content.Intent; |
Leon Scroggins | 571b376 | 2010-05-26 10:25:01 -0400 | [diff] [blame] | 29 | import android.graphics.Bitmap; |
Leon Scroggins | 571b376 | 2010-05-26 10:25:01 -0400 | [diff] [blame] | 30 | import android.graphics.drawable.Drawable; |
John Reck | 9202673 | 2011-02-15 10:12:30 -0800 | [diff] [blame] | 31 | import android.os.Bundle; |
| 32 | import android.speech.RecognizerResultsIntent; |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 33 | import android.text.TextUtils; |
| 34 | import android.view.ContextThemeWrapper; |
Michael Kolb | 7cdc490 | 2011-02-03 17:54:40 -0800 | [diff] [blame] | 35 | import android.view.Gravity; |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 36 | import android.view.KeyEvent; |
| 37 | import android.view.LayoutInflater; |
Leon Scroggins | 571b376 | 2010-05-26 10:25:01 -0400 | [diff] [blame] | 38 | import android.view.View; |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 39 | import android.view.View.OnClickListener; |
| 40 | import android.view.View.OnFocusChangeListener; |
Michael Kolb | 46f987e | 2011-04-05 10:39:10 -0700 | [diff] [blame] | 41 | import android.view.ViewGroup; |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 42 | import android.view.animation.Animation; |
| 43 | import android.view.animation.Animation.AnimationListener; |
| 44 | import android.view.animation.AnimationUtils; |
| 45 | import android.webkit.WebView; |
Michael Kolb | 7cdc490 | 2011-02-03 17:54:40 -0800 | [diff] [blame] | 46 | import android.widget.AbsoluteLayout; |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 47 | import android.widget.ArrayAdapter; |
| 48 | import android.widget.Button; |
Michael Kolb | 46f987e | 2011-04-05 10:39:10 -0700 | [diff] [blame] | 49 | import android.widget.FrameLayout; |
Leon Scroggins | 571b376 | 2010-05-26 10:25:01 -0400 | [diff] [blame] | 50 | import android.widget.ImageView; |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 51 | import android.widget.ProgressBar; |
| 52 | import android.widget.RelativeLayout; |
| 53 | import android.widget.Spinner; |
| 54 | import android.widget.TextView; |
| 55 | |
| 56 | import java.util.List; |
Leon Scroggins | 571b376 | 2010-05-26 10:25:01 -0400 | [diff] [blame] | 57 | |
| 58 | /** |
| 59 | * Base class for a title bar used by the browser. |
| 60 | */ |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 61 | public class TitleBarBase extends RelativeLayout |
| 62 | implements OnClickListener, OnFocusChangeListener, UrlInputListener, |
| 63 | TextChangeWatcher, DeviceAccountLogin.AutoLoginCallback { |
John Reck | 94b7e04 | 2011-02-15 15:02:33 -0800 | [diff] [blame] | 64 | |
| 65 | protected static final int PROGRESS_MAX = 100; |
| 66 | |
Leon Scroggins | 571b376 | 2010-05-26 10:25:01 -0400 | [diff] [blame] | 67 | // These need to be set by the subclass. |
| 68 | protected ImageView mFavicon; |
| 69 | protected ImageView mLockIcon; |
| 70 | |
John Reck | 9202673 | 2011-02-15 10:12:30 -0800 | [diff] [blame] | 71 | protected UiController mUiController; |
| 72 | protected BaseUi mBaseUi; |
Michael Kolb | 46f987e | 2011-04-05 10:39:10 -0700 | [diff] [blame] | 73 | protected FrameLayout mParent; |
| 74 | protected PageProgressView mProgress; |
John Reck | 9202673 | 2011-02-15 10:12:30 -0800 | [diff] [blame] | 75 | protected UrlInputView mUrlInput; |
| 76 | protected boolean mInVoiceMode; |
Michael Kolb | 46f987e | 2011-04-05 10:39:10 -0700 | [diff] [blame] | 77 | protected View mContainer; |
| 78 | |
Leon Scroggins | 571b376 | 2010-05-26 10:25:01 -0400 | [diff] [blame] | 79 | |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 80 | // Auto-login UI |
| 81 | protected View mAutoLogin; |
| 82 | protected Spinner mAutoLoginAccount; |
| 83 | protected Button mAutoLoginLogin; |
| 84 | protected ProgressBar mAutoLoginProgress; |
| 85 | protected TextView mAutoLoginError; |
John Reck | 12472f6 | 2011-04-27 15:32:10 -0700 | [diff] [blame] | 86 | protected View mAutoLoginCancel; |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 87 | protected DeviceAccountLogin mAutoLoginHandler; |
| 88 | protected ArrayAdapter<String> mAccountsAdapter; |
Michael Kolb | fdb7024 | 2011-03-24 09:41:11 -0700 | [diff] [blame] | 89 | protected boolean mUseQuickControls; |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 90 | |
Michael Kolb | 46f987e | 2011-04-05 10:39:10 -0700 | [diff] [blame] | 91 | //state |
| 92 | protected boolean mShowing; |
| 93 | protected boolean mInLoad; |
| 94 | protected boolean mSkipTitleBarAnimations; |
| 95 | private Animator mTitleBarAnimator; |
| 96 | |
| 97 | public TitleBarBase(Context context, UiController controller, BaseUi ui, |
| 98 | FrameLayout parent) { |
Leon Scroggins | 571b376 | 2010-05-26 10:25:01 -0400 | [diff] [blame] | 99 | super(context, null); |
John Reck | 9202673 | 2011-02-15 10:12:30 -0800 | [diff] [blame] | 100 | mUiController = controller; |
| 101 | mBaseUi = ui; |
Michael Kolb | 46f987e | 2011-04-05 10:39:10 -0700 | [diff] [blame] | 102 | mParent = parent; |
Leon Scroggins | 571b376 | 2010-05-26 10:25:01 -0400 | [diff] [blame] | 103 | } |
| 104 | |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 105 | protected void initLayout(Context context, int layoutId) { |
| 106 | LayoutInflater factory = LayoutInflater.from(context); |
| 107 | factory.inflate(layoutId, this); |
Michael Kolb | 46f987e | 2011-04-05 10:39:10 -0700 | [diff] [blame] | 108 | mContainer = findViewById(R.id.taburlbar); |
| 109 | mProgress = (PageProgressView) findViewById(R.id.progress); |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 110 | mUrlInput = (UrlInputView) findViewById(R.id.url); |
| 111 | mLockIcon = (ImageView) findViewById(R.id.lock); |
| 112 | mUrlInput.setUrlInputListener(this); |
| 113 | mUrlInput.setController(mUiController); |
| 114 | mUrlInput.setOnFocusChangeListener(this); |
| 115 | mUrlInput.setSelectAllOnFocus(true); |
| 116 | mUrlInput.addQueryTextWatcher(this); |
| 117 | mAutoLogin = findViewById(R.id.autologin); |
| 118 | mAutoLoginAccount = (Spinner) findViewById(R.id.autologin_account); |
| 119 | mAutoLoginLogin = (Button) findViewById(R.id.autologin_login); |
| 120 | mAutoLoginLogin.setOnClickListener(this); |
| 121 | mAutoLoginProgress = (ProgressBar) findViewById(R.id.autologin_progress); |
| 122 | mAutoLoginError = (TextView) findViewById(R.id.autologin_error); |
John Reck | 12472f6 | 2011-04-27 15:32:10 -0700 | [diff] [blame] | 123 | mAutoLoginCancel = mAutoLogin.findViewById(R.id.autologin_close); |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 124 | mAutoLoginCancel.setOnClickListener(this); |
| 125 | } |
| 126 | |
| 127 | protected void setupUrlInput() { |
| 128 | } |
| 129 | |
Michael Kolb | fdb7024 | 2011-03-24 09:41:11 -0700 | [diff] [blame] | 130 | protected void setUseQuickControls(boolean use) { |
| 131 | mUseQuickControls = use; |
Michael Kolb | 46f987e | 2011-04-05 10:39:10 -0700 | [diff] [blame] | 132 | setLayoutParams(makeLayoutParams()); |
Michael Kolb | fdb7024 | 2011-03-24 09:41:11 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Michael Kolb | 46f987e | 2011-04-05 10:39:10 -0700 | [diff] [blame] | 135 | void setShowProgressOnly(boolean progress) { |
| 136 | if (progress && !inAutoLogin()) { |
| 137 | mContainer.setVisibility(View.GONE); |
| 138 | } else { |
| 139 | mContainer.setVisibility(View.VISIBLE); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | void setSkipTitleBarAnimations(boolean skip) { |
| 144 | mSkipTitleBarAnimations = skip; |
| 145 | } |
| 146 | |
| 147 | void show() { |
| 148 | if (mUseQuickControls) { |
| 149 | mParent.addView(this); |
| 150 | } else { |
| 151 | if (!mSkipTitleBarAnimations) { |
| 152 | cancelTitleBarAnimation(false); |
| 153 | int visibleHeight = getVisibleTitleHeight(); |
| 154 | float startPos = (-getEmbeddedHeight() + visibleHeight); |
| 155 | if (getTranslationY() != 0) { |
| 156 | startPos = Math.max(startPos, getTranslationY()); |
| 157 | } |
| 158 | mTitleBarAnimator = ObjectAnimator.ofFloat(this, |
| 159 | "translationY", |
| 160 | startPos, 0); |
| 161 | mTitleBarAnimator.start(); |
| 162 | } |
| 163 | mBaseUi.setTitleGravity(Gravity.TOP); |
| 164 | } |
| 165 | mShowing = true; |
| 166 | } |
| 167 | |
| 168 | void hide() { |
| 169 | if (mUseQuickControls) { |
| 170 | mParent.removeView(this); |
| 171 | } else { |
| 172 | if (!mSkipTitleBarAnimations) { |
| 173 | cancelTitleBarAnimation(false); |
| 174 | int visibleHeight = getVisibleTitleHeight(); |
| 175 | mTitleBarAnimator = ObjectAnimator.ofFloat(this, |
| 176 | "translationY", getTranslationY(), |
| 177 | (-getEmbeddedHeight() + visibleHeight)); |
| 178 | mTitleBarAnimator.addListener(mHideTileBarAnimatorListener); |
| 179 | mTitleBarAnimator.start(); |
| 180 | } else { |
| 181 | mBaseUi.setTitleGravity(Gravity.NO_GRAVITY); |
| 182 | } |
| 183 | } |
| 184 | mShowing = false; |
| 185 | } |
| 186 | |
| 187 | boolean isShowing() { |
| 188 | return mShowing; |
| 189 | } |
| 190 | |
| 191 | void cancelTitleBarAnimation(boolean reset) { |
| 192 | if (mTitleBarAnimator != null) { |
| 193 | mTitleBarAnimator.cancel(); |
| 194 | mTitleBarAnimator = null; |
| 195 | } |
| 196 | if (reset) { |
| 197 | setTranslationY(0); |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | private AnimatorListener mHideTileBarAnimatorListener = new AnimatorListener() { |
| 202 | |
| 203 | boolean mWasCanceled; |
| 204 | @Override |
| 205 | public void onAnimationStart(Animator animation) { |
| 206 | mWasCanceled = false; |
| 207 | } |
| 208 | |
| 209 | @Override |
| 210 | public void onAnimationRepeat(Animator animation) { |
| 211 | } |
| 212 | |
| 213 | @Override |
| 214 | public void onAnimationEnd(Animator animation) { |
| 215 | if (!mWasCanceled) { |
| 216 | setTranslationY(0); |
| 217 | } |
| 218 | mBaseUi.setTitleGravity(Gravity.NO_GRAVITY); |
| 219 | } |
| 220 | |
| 221 | @Override |
| 222 | public void onAnimationCancel(Animator animation) { |
| 223 | mWasCanceled = true; |
| 224 | } |
| 225 | }; |
| 226 | |
| 227 | private int getVisibleTitleHeight() { |
| 228 | Tab tab = mBaseUi.getActiveTab(); |
| 229 | WebView webview = tab != null ? tab.getWebView() : null; |
| 230 | return webview != null ? webview.getVisibleTitleHeight() : 0; |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * Update the progress, from 0 to 100. |
| 235 | */ |
| 236 | void setProgress(int newProgress) { |
| 237 | if (newProgress >= PROGRESS_MAX) { |
| 238 | mProgress.setProgress(PageProgressView.MAX_PROGRESS); |
| 239 | mProgress.setVisibility(View.GONE); |
| 240 | mInLoad = false; |
| 241 | onProgressStopped(); |
| 242 | // check if needs to be hidden |
| 243 | if (!isEditingUrl() && !inAutoLogin()) { |
| 244 | hide(); |
| 245 | if (mUseQuickControls) { |
| 246 | setShowProgressOnly(false); |
| 247 | } |
| 248 | } |
| 249 | } else { |
| 250 | if (!mInLoad) { |
| 251 | mProgress.setVisibility(View.VISIBLE); |
| 252 | mInLoad = true; |
| 253 | onProgressStarted(); |
| 254 | } |
| 255 | mProgress.setProgress(newProgress * PageProgressView.MAX_PROGRESS |
| 256 | / PROGRESS_MAX); |
| 257 | if (!mShowing) { |
| 258 | if (mUseQuickControls && !isEditingUrl()) { |
| 259 | setShowProgressOnly(true); |
| 260 | } |
| 261 | show(); |
| 262 | } |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | protected void onProgressStarted() { |
| 267 | } |
| 268 | |
| 269 | protected void onProgressStopped() { |
| 270 | } |
Leon Scroggins | 571b376 | 2010-05-26 10:25:01 -0400 | [diff] [blame] | 271 | |
| 272 | /* package */ void setLock(Drawable d) { |
| 273 | assert mLockIcon != null; |
| 274 | if (null == d) { |
| 275 | mLockIcon.setVisibility(View.GONE); |
| 276 | } else { |
| 277 | mLockIcon.setImageDrawable(d); |
| 278 | mLockIcon.setVisibility(View.VISIBLE); |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | /* package */ void setFavicon(Bitmap icon) { |
Michael Kolb | 5a4372f | 2011-04-29 13:53:10 -0700 | [diff] [blame] | 283 | mFavicon.setImageDrawable(mBaseUi.getFaviconDrawable(icon)); |
Leon Scroggins | 571b376 | 2010-05-26 10:25:01 -0400 | [diff] [blame] | 284 | } |
| 285 | |
Michael Kolb | 29ccf8a | 2011-02-23 16:13:24 -0800 | [diff] [blame] | 286 | public int getEmbeddedHeight() { |
Michael Kolb | 46f987e | 2011-04-05 10:39:10 -0700 | [diff] [blame] | 287 | int height = mContainer.getHeight(); |
| 288 | if (mAutoLogin.getVisibility() == View.VISIBLE) { |
| 289 | height += mAutoLogin.getHeight(); |
| 290 | } |
| 291 | return height; |
Michael Kolb | 29ccf8a | 2011-02-23 16:13:24 -0800 | [diff] [blame] | 292 | } |
| 293 | |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 294 | protected void updateAutoLogin(Tab tab, boolean animate) { |
| 295 | DeviceAccountLogin login = tab.getDeviceAccountLogin(); |
| 296 | if (login != null) { |
| 297 | mAutoLoginHandler = login; |
| 298 | ContextThemeWrapper wrapper = new ContextThemeWrapper(mContext, |
| 299 | android.R.style.Theme_Holo_Light); |
| 300 | mAccountsAdapter = new ArrayAdapter<String>(wrapper, |
| 301 | android.R.layout.simple_spinner_item, login.getAccountNames()); |
| 302 | mAccountsAdapter.setDropDownViewResource( |
| 303 | android.R.layout.simple_spinner_dropdown_item); |
| 304 | mAutoLoginAccount.setAdapter(mAccountsAdapter); |
| 305 | mAutoLoginAccount.setSelection(0); |
| 306 | mAutoLoginAccount.setEnabled(true); |
| 307 | mAutoLoginLogin.setEnabled(true); |
John Reck | 12472f6 | 2011-04-27 15:32:10 -0700 | [diff] [blame] | 308 | mAutoLoginProgress.setVisibility(View.INVISIBLE); |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 309 | mAutoLoginError.setVisibility(View.GONE); |
| 310 | switch (login.getState()) { |
| 311 | case DeviceAccountLogin.PROCESSING: |
| 312 | mAutoLoginAccount.setEnabled(false); |
| 313 | mAutoLoginLogin.setEnabled(false); |
| 314 | mAutoLoginProgress.setVisibility(View.VISIBLE); |
| 315 | break; |
| 316 | case DeviceAccountLogin.FAILED: |
John Reck | 12472f6 | 2011-04-27 15:32:10 -0700 | [diff] [blame] | 317 | mAutoLoginProgress.setVisibility(View.INVISIBLE); |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 318 | mAutoLoginError.setVisibility(View.VISIBLE); |
| 319 | break; |
| 320 | case DeviceAccountLogin.INITIAL: |
| 321 | break; |
| 322 | default: |
| 323 | throw new IllegalStateException(); |
| 324 | } |
| 325 | showAutoLogin(animate); |
Michael Kolb | 43909f2 | 2011-03-23 13:18:36 -0700 | [diff] [blame] | 326 | } else { |
| 327 | hideAutoLogin(animate); |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 328 | } |
| 329 | } |
| 330 | |
| 331 | protected void showAutoLogin(boolean animate) { |
Michael Kolb | 46f987e | 2011-04-05 10:39:10 -0700 | [diff] [blame] | 332 | if (mUseQuickControls) { |
| 333 | mBaseUi.showTitleBar(); |
| 334 | } |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 335 | mAutoLogin.setVisibility(View.VISIBLE); |
| 336 | if (animate) { |
| 337 | mAutoLogin.startAnimation(AnimationUtils.loadAnimation( |
| 338 | getContext(), R.anim.autologin_enter)); |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | protected void hideAutoLogin(boolean animate) { |
| 343 | mAutoLoginHandler = null; |
Michael Kolb | 46f987e | 2011-04-05 10:39:10 -0700 | [diff] [blame] | 344 | if (mUseQuickControls) { |
| 345 | mBaseUi.hideTitleBar(); |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 346 | mAutoLogin.setVisibility(View.GONE); |
| 347 | mBaseUi.refreshWebView(); |
Michael Kolb | 46f987e | 2011-04-05 10:39:10 -0700 | [diff] [blame] | 348 | } else { |
| 349 | if (animate) { |
| 350 | Animation anim = AnimationUtils.loadAnimation(getContext(), |
| 351 | R.anim.autologin_exit); |
| 352 | anim.setAnimationListener(new AnimationListener() { |
| 353 | @Override |
| 354 | public void onAnimationEnd(Animation a) { |
| 355 | mAutoLogin.setVisibility(View.GONE); |
| 356 | mBaseUi.refreshWebView(); |
| 357 | } |
| 358 | |
| 359 | @Override |
| 360 | public void onAnimationStart(Animation a) { |
| 361 | } |
| 362 | |
| 363 | @Override |
| 364 | public void onAnimationRepeat(Animation a) { |
| 365 | } |
| 366 | }); |
| 367 | mAutoLogin.startAnimation(anim); |
| 368 | } else if (mAutoLogin.getAnimation() == null) { |
| 369 | mAutoLogin.setVisibility(View.GONE); |
| 370 | mBaseUi.refreshWebView(); |
| 371 | } |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 372 | } |
| 373 | } |
| 374 | |
| 375 | @Override |
| 376 | public void loginFailed() { |
| 377 | mAutoLoginAccount.setEnabled(true); |
| 378 | mAutoLoginLogin.setEnabled(true); |
John Reck | 12472f6 | 2011-04-27 15:32:10 -0700 | [diff] [blame] | 379 | mAutoLoginProgress.setVisibility(View.INVISIBLE); |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 380 | mAutoLoginError.setVisibility(View.VISIBLE); |
| 381 | } |
| 382 | |
| 383 | |
| 384 | protected boolean inAutoLogin() { |
| 385 | return mAutoLoginHandler != null; |
| 386 | } |
| 387 | |
| 388 | @Override |
| 389 | public void onClick(View v) { |
| 390 | if (mAutoLoginCancel == v) { |
| 391 | if (mAutoLoginHandler != null) { |
| 392 | mAutoLoginHandler.cancel(); |
| 393 | mAutoLoginHandler = null; |
| 394 | } |
| 395 | hideAutoLogin(true); |
| 396 | } else if (mAutoLoginLogin == v) { |
| 397 | if (mAutoLoginHandler != null) { |
| 398 | mAutoLoginAccount.setEnabled(false); |
| 399 | mAutoLoginLogin.setEnabled(false); |
| 400 | mAutoLoginProgress.setVisibility(View.VISIBLE); |
| 401 | mAutoLoginError.setVisibility(View.GONE); |
| 402 | mAutoLoginHandler.login( |
| 403 | mAutoLoginAccount.getSelectedItemPosition(), this); |
| 404 | } |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | @Override |
| 409 | public void onFocusChange(View view, boolean hasFocus) { |
| 410 | // if losing focus and not in touch mode, leave as is |
| 411 | if (hasFocus || view.isInTouchMode() || mUrlInput.needsUpdate()) { |
| 412 | setFocusState(hasFocus); |
| 413 | } |
| 414 | if (hasFocus) { |
| 415 | mUrlInput.forceIme(); |
| 416 | if (mInVoiceMode) { |
| 417 | mUrlInput.forceFilter(); |
| 418 | } |
| 419 | } else if (!mUrlInput.needsUpdate()) { |
| 420 | mUrlInput.dismissDropDown(); |
| 421 | mUrlInput.hideIME(); |
| 422 | if (mUrlInput.getText().length() == 0) { |
| 423 | Tab currentTab = mUiController.getTabControl().getCurrentTab(); |
| 424 | if (currentTab != null) { |
| 425 | mUrlInput.setText(currentTab.getUrl(), false); |
| 426 | } |
| 427 | } |
| 428 | } |
| 429 | mUrlInput.clearNeedsUpdate(); |
| 430 | } |
| 431 | |
| 432 | protected void setFocusState(boolean focus) { |
| 433 | if (focus) { |
| 434 | updateSearchMode(false); |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | protected void updateSearchMode(boolean userEdited) { |
| 439 | setSearchMode(!userEdited || TextUtils.isEmpty(mUrlInput.getUserText())); |
| 440 | } |
| 441 | |
| 442 | protected void setSearchMode(boolean voiceSearchEnabled) {} |
| 443 | |
| 444 | boolean isEditingUrl() { |
| 445 | return mUrlInput.hasFocus(); |
| 446 | } |
| 447 | |
| 448 | void stopEditingUrl() { |
| 449 | mUrlInput.clearFocus(); |
| 450 | } |
| 451 | |
| 452 | void setDisplayTitle(String title) { |
| 453 | if (!isEditingUrl()) { |
| 454 | mUrlInput.setText(title, false); |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | // UrlInput text watcher |
| 459 | |
| 460 | @Override |
| 461 | public void onTextChanged(String newText) { |
| 462 | if (mUrlInput.hasFocus()) { |
| 463 | // check if input field is empty and adjust voice search state |
| 464 | updateSearchMode(true); |
| 465 | // clear voice mode when user types |
| 466 | setInVoiceMode(false, null); |
| 467 | } |
| 468 | } |
| 469 | |
| 470 | // voicesearch |
| 471 | |
| 472 | public void setInVoiceMode(boolean voicemode, List<String> voiceResults) { |
| 473 | mInVoiceMode = voicemode; |
| 474 | mUrlInput.setVoiceResults(voiceResults); |
| 475 | } |
| 476 | |
| 477 | void setIncognitoMode(boolean incognito) { |
| 478 | mUrlInput.setIncognitoMode(incognito); |
| 479 | } |
| 480 | |
| 481 | void clearCompletions() { |
| 482 | mUrlInput.setSuggestedText(null); |
| 483 | } |
| 484 | |
John Reck | 9202673 | 2011-02-15 10:12:30 -0800 | [diff] [blame] | 485 | // UrlInputListener implementation |
| 486 | |
| 487 | /** |
| 488 | * callback from suggestion dropdown |
| 489 | * user selected a suggestion |
| 490 | */ |
| 491 | @Override |
| 492 | public void onAction(String text, String extra, String source) { |
| 493 | mUiController.getCurrentTopWebView().requestFocus(); |
| 494 | mBaseUi.hideTitleBar(); |
| 495 | Intent i = new Intent(); |
| 496 | String action = null; |
| 497 | if (UrlInputView.VOICE.equals(source)) { |
| 498 | action = RecognizerResultsIntent.ACTION_VOICE_SEARCH_RESULTS; |
| 499 | source = null; |
| 500 | } else { |
| 501 | action = Intent.ACTION_SEARCH; |
| 502 | } |
| 503 | i.setAction(action); |
| 504 | i.putExtra(SearchManager.QUERY, text); |
| 505 | if (extra != null) { |
| 506 | i.putExtra(SearchManager.EXTRA_DATA_KEY, extra); |
| 507 | } |
| 508 | if (source != null) { |
| 509 | Bundle appData = new Bundle(); |
| 510 | appData.putString(com.android.common.Search.SOURCE, source); |
| 511 | i.putExtra(SearchManager.APP_DATA, appData); |
| 512 | } |
| 513 | mUiController.handleNewIntent(i); |
| 514 | setDisplayTitle(text); |
| 515 | } |
| 516 | |
| 517 | @Override |
| 518 | public void onDismiss() { |
| 519 | final Tab currentTab = mBaseUi.getActiveTab(); |
| 520 | mBaseUi.hideTitleBar(); |
| 521 | post(new Runnable() { |
| 522 | public void run() { |
| 523 | clearFocus(); |
| 524 | if ((currentTab != null) && !mInVoiceMode) { |
| 525 | setDisplayTitle(currentTab.getUrl()); |
| 526 | } |
| 527 | } |
| 528 | }); |
| 529 | } |
| 530 | |
| 531 | /** |
| 532 | * callback from the suggestion dropdown |
| 533 | * copy text to input field and stay in edit mode |
| 534 | */ |
| 535 | @Override |
| 536 | public void onCopySuggestion(String text) { |
| 537 | mUrlInput.setText(text, true); |
| 538 | if (text != null) { |
| 539 | mUrlInput.setSelection(text.length()); |
| 540 | } |
| 541 | } |
| 542 | |
John Reck | 94b7e04 | 2011-02-15 15:02:33 -0800 | [diff] [blame] | 543 | public void setCurrentUrlIsBookmark(boolean isBookmark) { |
| 544 | } |
| 545 | |
Michael Kolb | 11d1978 | 2011-03-20 10:17:40 -0700 | [diff] [blame] | 546 | @Override |
| 547 | public boolean dispatchKeyEventPreIme(KeyEvent evt) { |
| 548 | if (evt.getKeyCode() == KeyEvent.KEYCODE_BACK) { |
| 549 | // catch back key in order to do slightly more cleanup than usual |
| 550 | mUrlInput.clearFocus(); |
| 551 | return true; |
| 552 | } |
| 553 | return super.dispatchKeyEventPreIme(evt); |
| 554 | } |
| 555 | |
| 556 | protected WebView getCurrentWebView() { |
| 557 | Tab t = mBaseUi.getActiveTab(); |
| 558 | if (t != null) { |
| 559 | return t.getWebView(); |
| 560 | } else { |
| 561 | return null; |
| 562 | } |
| 563 | } |
| 564 | |
| 565 | void registerDropdownChangeListener(DropdownChangeListener d) { |
| 566 | mUrlInput.registerDropdownChangeListener(d); |
| 567 | } |
| 568 | |
Michael Kolb | fdb7024 | 2011-03-24 09:41:11 -0700 | [diff] [blame] | 569 | /** |
| 570 | * called from the Ui when the user wants to edit |
| 571 | * @param clearInput clear the input field |
| 572 | */ |
Michael Kolb | 46f987e | 2011-04-05 10:39:10 -0700 | [diff] [blame] | 573 | void startEditingUrl(boolean clearInput) { |
| 574 | // editing takes preference of progress |
| 575 | mContainer.setVisibility(View.VISIBLE); |
| 576 | if (mUseQuickControls) { |
| 577 | mProgress.setVisibility(View.GONE); |
| 578 | } |
| 579 | if (!mUrlInput.hasFocus()) { |
| 580 | mUrlInput.requestFocus(); |
| 581 | } |
| 582 | if (clearInput) { |
| 583 | mUrlInput.setText(""); |
| 584 | } else if (mInVoiceMode) { |
| 585 | mUrlInput.showDropDown(); |
| 586 | } |
| 587 | } |
| 588 | |
| 589 | private ViewGroup.LayoutParams makeLayoutParams() { |
| 590 | if (mUseQuickControls) { |
| 591 | return new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, |
| 592 | LayoutParams.WRAP_CONTENT); |
| 593 | } else { |
| 594 | return new AbsoluteLayout.LayoutParams( |
| 595 | LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, |
| 596 | 0, 0); |
| 597 | } |
| 598 | } |
Michael Kolb | fdb7024 | 2011-03-24 09:41:11 -0700 | [diff] [blame] | 599 | |
Leon Scroggins | 571b376 | 2010-05-26 10:25:01 -0400 | [diff] [blame] | 600 | } |