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