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