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