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