blob: ae11038bd7cbdd62a1c15c736200a1b45b7c64ee [file] [log] [blame]
Leon Scroggins571b3762010-05-26 10:25:01 -04001/*
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
17package com.android.browser;
18
Michael Kolb46f987e2011-04-05 10:39:10 -070019import android.animation.Animator;
20import android.animation.Animator.AnimatorListener;
21import android.animation.ObjectAnimator;
John Reck92026732011-02-15 10:12:30 -080022import android.app.SearchManager;
Leon Scroggins571b3762010-05-26 10:25:01 -040023import android.content.Context;
John Reck92026732011-02-15 10:12:30 -080024import android.content.Intent;
John Reck38def322011-06-24 16:13:24 -070025import android.content.res.Resources;
Leon Scroggins571b3762010-05-26 10:25:01 -040026import android.graphics.Bitmap;
Leon Scroggins571b3762010-05-26 10:25:01 -040027import android.graphics.drawable.Drawable;
John Reck92026732011-02-15 10:12:30 -080028import android.os.Bundle;
29import android.speech.RecognizerResultsIntent;
Michael Kolb11d19782011-03-20 10:17:40 -070030import android.text.TextUtils;
31import android.view.ContextThemeWrapper;
Michael Kolb7cdc4902011-02-03 17:54:40 -080032import android.view.Gravity;
Michael Kolb11d19782011-03-20 10:17:40 -070033import android.view.KeyEvent;
34import android.view.LayoutInflater;
John Reck46500332011-06-07 14:36:10 -070035import android.view.Menu;
36import android.view.MenuItem;
Leon Scroggins571b3762010-05-26 10:25:01 -040037import android.view.View;
Michael Kolb11d19782011-03-20 10:17:40 -070038import android.view.View.OnClickListener;
39import android.view.View.OnFocusChangeListener;
Michael Kolb46f987e2011-04-05 10:39:10 -070040import android.view.ViewGroup;
Michael Kolb11d19782011-03-20 10:17:40 -070041import android.view.animation.Animation;
42import android.view.animation.Animation.AnimationListener;
43import android.view.animation.AnimationUtils;
John Reck38def322011-06-24 16:13:24 -070044import android.view.animation.DecelerateInterpolator;
Michael Kolb11d19782011-03-20 10:17:40 -070045import android.webkit.WebView;
Michael Kolb7cdc4902011-02-03 17:54:40 -080046import android.widget.AbsoluteLayout;
Michael Kolb11d19782011-03-20 10:17:40 -070047import android.widget.ArrayAdapter;
48import android.widget.Button;
Michael Kolb46f987e2011-04-05 10:39:10 -070049import android.widget.FrameLayout;
Leon Scroggins571b3762010-05-26 10:25:01 -040050import android.widget.ImageView;
John Reck46500332011-06-07 14:36:10 -070051import android.widget.PopupMenu;
52import android.widget.PopupMenu.OnMenuItemClickListener;
Michael Kolb11d19782011-03-20 10:17:40 -070053import android.widget.ProgressBar;
54import android.widget.RelativeLayout;
55import android.widget.Spinner;
56import android.widget.TextView;
57
John Reck38def322011-06-24 16:13:24 -070058import com.android.browser.UI.DropdownChangeListener;
59import com.android.browser.UrlInputView.UrlInputListener;
60import com.android.browser.autocomplete.SuggestedTextController.TextChangeWatcher;
61
Michael Kolb11d19782011-03-20 10:17:40 -070062import java.util.List;
Leon Scroggins571b3762010-05-26 10:25:01 -040063
64/**
65 * Base class for a title bar used by the browser.
66 */
Michael Kolb11d19782011-03-20 10:17:40 -070067public class TitleBarBase extends RelativeLayout
68 implements OnClickListener, OnFocusChangeListener, UrlInputListener,
John Reck46500332011-06-07 14:36:10 -070069 TextChangeWatcher, DeviceAccountLogin.AutoLoginCallback,
70 OnMenuItemClickListener {
John Reck94b7e042011-02-15 15:02:33 -080071
72 protected static final int PROGRESS_MAX = 100;
John Reck38def322011-06-24 16:13:24 -070073 private static final float ANIM_TITLEBAR_DECELERATE = 2.5f;
John Reck94b7e042011-02-15 15:02:33 -080074
Leon Scroggins571b3762010-05-26 10:25:01 -040075 // These need to be set by the subclass.
76 protected ImageView mFavicon;
77 protected ImageView mLockIcon;
78
John Reck92026732011-02-15 10:12:30 -080079 protected UiController mUiController;
80 protected BaseUi mBaseUi;
Michael Kolb46f987e2011-04-05 10:39:10 -070081 protected FrameLayout mParent;
82 protected PageProgressView mProgress;
John Reck92026732011-02-15 10:12:30 -080083 protected UrlInputView mUrlInput;
84 protected boolean mInVoiceMode;
Michael Kolb46f987e2011-04-05 10:39:10 -070085 protected View mContainer;
John Reck46500332011-06-07 14:36:10 -070086 private View mUaSwitcher;
Leon Scroggins571b3762010-05-26 10:25:01 -040087
Michael Kolb11d19782011-03-20 10:17:40 -070088 // Auto-login UI
89 protected View mAutoLogin;
90 protected Spinner mAutoLoginAccount;
91 protected Button mAutoLoginLogin;
92 protected ProgressBar mAutoLoginProgress;
93 protected TextView mAutoLoginError;
John Reck12472f62011-04-27 15:32:10 -070094 protected View mAutoLoginCancel;
Michael Kolb11d19782011-03-20 10:17:40 -070095 protected DeviceAccountLogin mAutoLoginHandler;
96 protected ArrayAdapter<String> mAccountsAdapter;
Michael Kolbfdb70242011-03-24 09:41:11 -070097 protected boolean mUseQuickControls;
Michael Kolb11d19782011-03-20 10:17:40 -070098
Michael Kolb46f987e2011-04-05 10:39:10 -070099 //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 Scroggins571b3762010-05-26 10:25:01 -0400107 super(context, null);
John Reck92026732011-02-15 10:12:30 -0800108 mUiController = controller;
109 mBaseUi = ui;
Michael Kolb46f987e2011-04-05 10:39:10 -0700110 mParent = parent;
Leon Scroggins571b3762010-05-26 10:25:01 -0400111 }
112
Michael Kolb11d19782011-03-20 10:17:40 -0700113 protected void initLayout(Context context, int layoutId) {
114 LayoutInflater factory = LayoutInflater.from(context);
115 factory.inflate(layoutId, this);
Michael Kolb46f987e2011-04-05 10:39:10 -0700116 mContainer = findViewById(R.id.taburlbar);
117 mProgress = (PageProgressView) findViewById(R.id.progress);
Michael Kolb11d19782011-03-20 10:17:40 -0700118 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 Reck12472f62011-04-27 15:32:10 -0700131 mAutoLoginCancel = mAutoLogin.findViewById(R.id.autologin_close);
Michael Kolb11d19782011-03-20 10:17:40 -0700132 mAutoLoginCancel.setOnClickListener(this);
133 }
134
135 protected void setupUrlInput() {
136 }
137
Michael Kolbfdb70242011-03-24 09:41:11 -0700138 protected void setUseQuickControls(boolean use) {
139 mUseQuickControls = use;
Michael Kolb46f987e2011-04-05 10:39:10 -0700140 setLayoutParams(makeLayoutParams());
Michael Kolbfdb70242011-03-24 09:41:11 -0700141 }
142
Michael Kolb46f987e2011-04-05 10:39:10 -0700143 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 Reck38def322011-06-24 16:13:24 -0700155 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 Kolb46f987e2011-04-05 10:39:10 -0700163 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 Reck38def322011-06-24 16:13:24 -0700177 setupTitleBarAnimator(mTitleBarAnimator);
Michael Kolb46f987e2011-04-05 10:39:10 -0700178 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 Reck38def322011-06-24 16:13:24 -0700196 setupTitleBarAnimator(mTitleBarAnimator);
Michael Kolb46f987e2011-04-05 10:39:10 -0700197 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 Scroggins571b3762010-05-26 10:25:01 -0400289
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 Kolb5a4372f2011-04-29 13:53:10 -0700301 mFavicon.setImageDrawable(mBaseUi.getFaviconDrawable(icon));
Leon Scroggins571b3762010-05-26 10:25:01 -0400302 }
303
Michael Kolb29ccf8a2011-02-23 16:13:24 -0800304 public int getEmbeddedHeight() {
Michael Kolb46f987e2011-04-05 10:39:10 -0700305 int height = mContainer.getHeight();
306 if (mAutoLogin.getVisibility() == View.VISIBLE) {
307 height += mAutoLogin.getHeight();
308 }
309 return height;
Michael Kolb29ccf8a2011-02-23 16:13:24 -0800310 }
311
Michael Kolb11d19782011-03-20 10:17:40 -0700312 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 Reck12472f62011-04-27 15:32:10 -0700326 mAutoLoginProgress.setVisibility(View.INVISIBLE);
Michael Kolb11d19782011-03-20 10:17:40 -0700327 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 Reck12472f62011-04-27 15:32:10 -0700335 mAutoLoginProgress.setVisibility(View.INVISIBLE);
Michael Kolb11d19782011-03-20 10:17:40 -0700336 mAutoLoginError.setVisibility(View.VISIBLE);
337 break;
338 case DeviceAccountLogin.INITIAL:
339 break;
340 default:
341 throw new IllegalStateException();
342 }
343 showAutoLogin(animate);
Michael Kolb43909f22011-03-23 13:18:36 -0700344 } else {
345 hideAutoLogin(animate);
Michael Kolb11d19782011-03-20 10:17:40 -0700346 }
347 }
348
349 protected void showAutoLogin(boolean animate) {
Michael Kolb46f987e2011-04-05 10:39:10 -0700350 if (mUseQuickControls) {
351 mBaseUi.showTitleBar();
352 }
Michael Kolb11d19782011-03-20 10:17:40 -0700353 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 Kolb46f987e2011-04-05 10:39:10 -0700362 if (mUseQuickControls) {
363 mBaseUi.hideTitleBar();
Michael Kolb11d19782011-03-20 10:17:40 -0700364 mAutoLogin.setVisibility(View.GONE);
365 mBaseUi.refreshWebView();
Michael Kolb46f987e2011-04-05 10:39:10 -0700366 } 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 Kolb11d19782011-03-20 10:17:40 -0700390 }
391 }
392
393 @Override
394 public void loginFailed() {
395 mAutoLoginAccount.setEnabled(true);
396 mAutoLoginLogin.setEnabled(true);
John Reck12472f62011-04-27 15:32:10 -0700397 mAutoLoginProgress.setVisibility(View.INVISIBLE);
Michael Kolb11d19782011-03-20 10:17:40 -0700398 mAutoLoginError.setVisibility(View.VISIBLE);
399 }
400
401
402 protected boolean inAutoLogin() {
403 return mAutoLoginHandler != null;
404 }
405
John Reck46500332011-06-07 14:36:10 -0700406 public void setUaSwitcher(View v) {
407 if (mUaSwitcher != null) {
408 mUaSwitcher.setOnClickListener(null);
409 }
410 mUaSwitcher = v;
411 mUaSwitcher.setOnClickListener(this);
412 }
413
Michael Kolb11d19782011-03-20 10:17:40 -0700414 @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 Reck46500332011-06-07 14:36:10 -0700431 } 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 Kolb11d19782011-03-20 10:17:40 -0700443 }
444 }
445
446 @Override
John Reck46500332011-06-07 14:36:10 -0700447 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 Kolb11d19782011-03-20 10:17:40 -0700470 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 Reck7e5b8b52011-06-22 13:46:25 -0700476 mBaseUi.showTitleBar();
Michael Kolb11d19782011-03-20 10:17:40 -0700477 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 Reck7e5b8b52011-06-22 13:46:25 -0700490 mBaseUi.suggestHideTitleBar();
Michael Kolb11d19782011-03-20 10:17:40 -0700491 }
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 Reck92026732011-02-15 10:12:30 -0800548 // 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();
John Reck71e51422011-07-01 16:49:28 -0700557 if (UrlInputView.TYPED.equals(source)) {
558 String url = UrlUtils.smartUrlFilter(text, false);
559 Tab t = mBaseUi.getActiveTab();
560 if (url != null && t != null) {
561 mUiController.loadUrl(t, url);
562 setDisplayTitle(text);
563 return;
564 }
565 }
John Reck92026732011-02-15 10:12:30 -0800566 Intent i = new Intent();
567 String action = null;
568 if (UrlInputView.VOICE.equals(source)) {
569 action = RecognizerResultsIntent.ACTION_VOICE_SEARCH_RESULTS;
570 source = null;
571 } else {
572 action = Intent.ACTION_SEARCH;
573 }
574 i.setAction(action);
575 i.putExtra(SearchManager.QUERY, text);
576 if (extra != null) {
577 i.putExtra(SearchManager.EXTRA_DATA_KEY, extra);
578 }
579 if (source != null) {
580 Bundle appData = new Bundle();
581 appData.putString(com.android.common.Search.SOURCE, source);
582 i.putExtra(SearchManager.APP_DATA, appData);
583 }
584 mUiController.handleNewIntent(i);
585 setDisplayTitle(text);
586 }
587
588 @Override
589 public void onDismiss() {
590 final Tab currentTab = mBaseUi.getActiveTab();
591 mBaseUi.hideTitleBar();
592 post(new Runnable() {
593 public void run() {
594 clearFocus();
595 if ((currentTab != null) && !mInVoiceMode) {
596 setDisplayTitle(currentTab.getUrl());
597 }
598 }
599 });
600 }
601
602 /**
603 * callback from the suggestion dropdown
604 * copy text to input field and stay in edit mode
605 */
606 @Override
607 public void onCopySuggestion(String text) {
608 mUrlInput.setText(text, true);
609 if (text != null) {
610 mUrlInput.setSelection(text.length());
611 }
612 }
613
John Reck94b7e042011-02-15 15:02:33 -0800614 public void setCurrentUrlIsBookmark(boolean isBookmark) {
615 }
616
Michael Kolb11d19782011-03-20 10:17:40 -0700617 @Override
618 public boolean dispatchKeyEventPreIme(KeyEvent evt) {
619 if (evt.getKeyCode() == KeyEvent.KEYCODE_BACK) {
620 // catch back key in order to do slightly more cleanup than usual
621 mUrlInput.clearFocus();
622 return true;
623 }
624 return super.dispatchKeyEventPreIme(evt);
625 }
626
627 protected WebView getCurrentWebView() {
628 Tab t = mBaseUi.getActiveTab();
629 if (t != null) {
630 return t.getWebView();
631 } else {
632 return null;
633 }
634 }
635
636 void registerDropdownChangeListener(DropdownChangeListener d) {
637 mUrlInput.registerDropdownChangeListener(d);
638 }
639
Michael Kolbfdb70242011-03-24 09:41:11 -0700640 /**
641 * called from the Ui when the user wants to edit
642 * @param clearInput clear the input field
643 */
Michael Kolb46f987e2011-04-05 10:39:10 -0700644 void startEditingUrl(boolean clearInput) {
645 // editing takes preference of progress
646 mContainer.setVisibility(View.VISIBLE);
647 if (mUseQuickControls) {
648 mProgress.setVisibility(View.GONE);
649 }
650 if (!mUrlInput.hasFocus()) {
651 mUrlInput.requestFocus();
652 }
653 if (clearInput) {
654 mUrlInput.setText("");
655 } else if (mInVoiceMode) {
656 mUrlInput.showDropDown();
657 }
658 }
659
660 private ViewGroup.LayoutParams makeLayoutParams() {
661 if (mUseQuickControls) {
662 return new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT,
663 LayoutParams.WRAP_CONTENT);
664 } else {
665 return new AbsoluteLayout.LayoutParams(
666 LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT,
667 0, 0);
668 }
669 }
Michael Kolbfdb70242011-03-24 09:41:11 -0700670
Leon Scroggins571b3762010-05-26 10:25:01 -0400671}