blob: f4ba9db3689a4d19997b185ac98ed2b73eda8aa9 [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
Narayan Kamath80aad8d2011-02-23 12:01:13 +000019import com.android.browser.autocomplete.SuggestedTextController.TextChangeWatcher;
Narayan Kamath5119edd2011-02-23 15:49:17 +000020import com.android.browser.UI.DropdownChangeListener;
Michael Kolb793e05e2011-01-11 15:17:31 -080021import com.android.browser.search.SearchEngine;
Michael Kolbc7485ae2010-09-03 10:10:58 -070022
Michael Kolb8233fac2010-10-26 16:08:53 -070023import android.app.Activity;
Leon Scroggins571b3762010-05-26 10:25:01 -040024import android.content.Context;
25import android.content.res.Resources;
Narayan Kamath5119edd2011-02-23 15:49:17 +000026import android.database.DataSetObserver;
Michael Kolbfe251992010-07-08 15:41:55 -070027import android.graphics.Bitmap;
Narayan Kamath5119edd2011-02-23 15:49:17 +000028import android.graphics.Rect;
Leon Scroggins571b3762010-05-26 10:25:01 -040029import android.graphics.drawable.Drawable;
Michael Kolb1ce78132010-09-23 15:50:53 -070030import android.text.TextUtils;
Michael Kolbdc2ee1b2011-02-14 14:34:40 -080031import android.view.KeyEvent;
Leon Scroggins571b3762010-05-26 10:25:01 -040032import android.view.LayoutInflater;
Leon Scroggins571b3762010-05-26 10:25:01 -040033import android.view.View;
Michael Kolba2b2ba82010-08-04 17:54:03 -070034import android.view.View.OnClickListener;
Michael Kolb31d469b2010-12-09 20:49:54 -080035import android.view.View.OnFocusChangeListener;
Michael Kolb7cdc4902011-02-03 17:54:40 -080036import android.view.ViewGroup;
Michael Kolbb6bc32c2010-12-10 11:51:54 -080037import android.webkit.WebView;
Michael Kolb7cdc4902011-02-03 17:54:40 -080038import android.widget.AbsoluteLayout;
39import android.widget.FrameLayout;
Michael Kolb5a72f182011-01-13 20:35:06 -080040import android.widget.ImageButton;
Leon Scroggins571b3762010-05-26 10:25:01 -040041import android.widget.ImageView;
Leon Scroggins571b3762010-05-26 10:25:01 -040042
Narayan Kamath5119edd2011-02-23 15:49:17 +000043import java.util.ArrayList;
Michael Kolbcfa3af52010-12-14 10:36:11 -080044import java.util.List;
45
Leon Scroggins571b3762010-05-26 10:25:01 -040046/**
Michael Kolbfe251992010-07-08 15:41:55 -070047 * tabbed title bar for xlarge screen browser
Leon Scroggins571b3762010-05-26 10:25:01 -040048 */
Michael Kolbfe251992010-07-08 15:41:55 -070049public class TitleBarXLarge extends TitleBarBase
Narayan Kamath80aad8d2011-02-23 12:01:13 +000050 implements OnClickListener, OnFocusChangeListener, TextChangeWatcher {
Leon Scroggins571b3762010-05-26 10:25:01 -040051
Michael Kolb66706532010-12-12 19:50:22 -080052 private XLargeUi mUi;
Michael Kolb8233fac2010-10-26 16:08:53 -070053
Michael Kolba2b2ba82010-08-04 17:54:03 -070054 private Drawable mStopDrawable;
55 private Drawable mReloadDrawable;
Michael Kolbc7485ae2010-09-03 10:10:58 -070056
Michael Kolb3f65c382010-08-20 15:31:16 -070057 private View mContainer;
Michael Kolb5a72f182011-01-13 20:35:06 -080058 private ImageButton mBackButton;
59 private ImageButton mForwardButton;
Michael Kolb31d469b2010-12-09 20:49:54 -080060 private ImageView mStar;
Michael Kolb5b2299c2011-01-25 16:54:34 -080061 private ImageView mWebIcon;
Michael Kolba2b2ba82010-08-04 17:54:03 -070062 private View mSearchButton;
Michael Kolb31d469b2010-12-09 20:49:54 -080063 private View mUrlContainer;
Michael Kolb513286f2010-09-09 12:55:12 -070064 private View mGoButton;
Michael Kolba2b2ba82010-08-04 17:54:03 -070065 private ImageView mStopButton;
Michael Kolba2b2ba82010-08-04 17:54:03 -070066 private View mAllButton;
Michael Kolbb7b115e2010-09-25 16:59:37 -070067 private View mClearButton;
Michael Kolb31d469b2010-12-09 20:49:54 -080068 private View mVoiceSearch;
Michael Kolbcfa3af52010-12-14 10:36:11 -080069 private View mVoiceSearchIndicator;
Michael Kolbc7485ae2010-09-03 10:10:58 -070070 private PageProgressView mProgressView;
Michael Kolbcfa3af52010-12-14 10:36:11 -080071 private Drawable mFocusDrawable;
72 private Drawable mUnfocusDrawable;
Michael Kolb81b6f832010-12-12 12:44:27 -080073
Michael Kolba2b2ba82010-08-04 17:54:03 -070074 private boolean mInLoad;
Michael Kolb376b5412010-12-15 11:52:57 -080075 private boolean mUseQuickControls;
Michael Kolbfe251992010-07-08 15:41:55 -070076
Michael Kolb81b6f832010-12-12 12:44:27 -080077 public TitleBarXLarge(Activity activity, UiController controller,
Michael Kolb66706532010-12-12 19:50:22 -080078 XLargeUi ui) {
John Reck92026732011-02-15 10:12:30 -080079 super(activity, controller, ui);
Michael Kolb81b6f832010-12-12 12:44:27 -080080 mUi = ui;
Michael Kolb8233fac2010-10-26 16:08:53 -070081 Resources resources = activity.getResources();
Michael Kolb5a72f182011-01-13 20:35:06 -080082 mStopDrawable = resources.getDrawable(R.drawable.ic_stop_holo_dark);
83 mReloadDrawable = resources.getDrawable(R.drawable.ic_refresh_holo_dark);
Michael Kolbcfa3af52010-12-14 10:36:11 -080084 mFocusDrawable = resources.getDrawable(
85 R.drawable.textfield_active_holo_dark);
86 mUnfocusDrawable = resources.getDrawable(
87 R.drawable.textfield_default_holo_dark);
John Reck1605bef2011-01-10 18:11:18 -080088 initLayout(activity);
Michael Kolbcfa3af52010-12-14 10:36:11 -080089 mInVoiceMode = false;
Michael Kolbfe251992010-07-08 15:41:55 -070090 }
Leon Scroggins571b3762010-05-26 10:25:01 -040091
Michael Kolb7cdc4902011-02-03 17:54:40 -080092 @Override
93 void setTitleGravity(int gravity) {
94 if (mUseQuickControls) {
95 FrameLayout.LayoutParams lp =
96 (FrameLayout.LayoutParams) getLayoutParams();
97 lp.gravity = gravity;
98 setLayoutParams(lp);
99 } else {
100 super.setTitleGravity(gravity);
101 }
102 }
103
John Reck1605bef2011-01-10 18:11:18 -0800104 private void initLayout(Context context) {
Michael Kolbfe251992010-07-08 15:41:55 -0700105 LayoutInflater factory = LayoutInflater.from(context);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700106 factory.inflate(R.layout.url_bar, this);
Michael Kolbfe251992010-07-08 15:41:55 -0700107
Michael Kolb3f65c382010-08-20 15:31:16 -0700108 mContainer = findViewById(R.id.taburlbar);
Michael Kolb31d469b2010-12-09 20:49:54 -0800109 mUrlInput = (UrlInputView) findViewById(R.id.url_focused);
Michael Kolbfe251992010-07-08 15:41:55 -0700110 mAllButton = findViewById(R.id.all_btn);
111 // TODO: Change enabled states based on whether you can go
Leon Scroggins571b3762010-05-26 10:25:01 -0400112 // back/forward. Probably should be done inside onPageStarted.
Michael Kolb5a72f182011-01-13 20:35:06 -0800113 mBackButton = (ImageButton) findViewById(R.id.back);
114 mForwardButton = (ImageButton) findViewById(R.id.forward);
Michael Kolb5b2299c2011-01-25 16:54:34 -0800115 mWebIcon = (ImageView) findViewById(R.id.web_icon);
Michael Kolb31d469b2010-12-09 20:49:54 -0800116 mStar = (ImageView) findViewById(R.id.star);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700117 mStopButton = (ImageView) findViewById(R.id.stop);
118 mSearchButton = findViewById(R.id.search);
119 mLockIcon = (ImageView) findViewById(R.id.lock);
Michael Kolb513286f2010-09-09 12:55:12 -0700120 mGoButton = findViewById(R.id.go);
Michael Kolbb7b115e2010-09-25 16:59:37 -0700121 mClearButton = findViewById(R.id.clear);
Michael Kolb31d469b2010-12-09 20:49:54 -0800122 mVoiceSearch = findViewById(R.id.voicesearch);
Michael Kolbc7485ae2010-09-03 10:10:58 -0700123 mProgressView = (PageProgressView) findViewById(R.id.progress);
Michael Kolb31d469b2010-12-09 20:49:54 -0800124 mUrlContainer = findViewById(R.id.urlbar_focused);
Michael Kolbcfa3af52010-12-14 10:36:11 -0800125 mVoiceSearchIndicator = findViewById(R.id.voice_icon);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700126 mBackButton.setOnClickListener(this);
127 mForwardButton.setOnClickListener(this);
128 mStar.setOnClickListener(this);
129 mAllButton.setOnClickListener(this);
130 mStopButton.setOnClickListener(this);
131 mSearchButton.setOnClickListener(this);
Michael Kolb513286f2010-09-09 12:55:12 -0700132 mGoButton.setOnClickListener(this);
Michael Kolbb7b115e2010-09-25 16:59:37 -0700133 mClearButton.setOnClickListener(this);
Michael Kolbcfa3af52010-12-14 10:36:11 -0800134 mVoiceSearch.setOnClickListener(this);
Michael Kolb31d469b2010-12-09 20:49:54 -0800135 mUrlInput.setUrlInputListener(this);
136 mUrlInput.setContainer(mUrlContainer);
137 mUrlInput.setController(mUiController);
138 mUrlInput.setOnFocusChangeListener(this);
139 mUrlInput.setSelectAllOnFocus(true);
Narayan Kamath80aad8d2011-02-23 12:01:13 +0000140 mUrlInput.addQueryTextWatcher(this);
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800141 setFocusState(false);
Michael Kolb31d469b2010-12-09 20:49:54 -0800142 }
143
Michael Kolb5a72f182011-01-13 20:35:06 -0800144 void updateNavigationState(Tab tab) {
145 WebView web = tab.getWebView();
146 if (web != null) {
147 mBackButton.setImageResource(web.canGoBack()
148 ? R.drawable.ic_back_holo_dark
149 : R.drawable.ic_back_disabled_holo_dark);
150 mForwardButton.setImageResource(web.canGoForward()
151 ? R.drawable.ic_forward_holo_dark
152 : R.drawable.ic_forward_disabled_holo_dark);
153 }
154 }
155
Michael Kolb7cdc4902011-02-03 17:54:40 -0800156 private ViewGroup.LayoutParams makeLayoutParams() {
157 if (mUseQuickControls) {
158 return new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT,
159 LayoutParams.WRAP_CONTENT);
Michael Kolbcfa3af52010-12-14 10:36:11 -0800160 } else {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800161 return new AbsoluteLayout.LayoutParams(
162 LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT,
163 0, 0);
Michael Kolbcfa3af52010-12-14 10:36:11 -0800164 }
165 }
166
Michael Kolb29ccf8a2011-02-23 16:13:24 -0800167 @Override
168 public int getEmbeddedHeight() {
169 return mContainer.getHeight();
170 }
171
Michael Kolb376b5412010-12-15 11:52:57 -0800172 void setUseQuickControls(boolean useQuickControls) {
173 mUseQuickControls = useQuickControls;
Michael Kolb91902d52011-01-26 17:43:48 -0800174 mUrlInput.setUseQuickControls(mUseQuickControls);
Michael Kolb7cdc4902011-02-03 17:54:40 -0800175 setLayoutParams(makeLayoutParams());
Michael Kolb376b5412010-12-15 11:52:57 -0800176 if (mUseQuickControls) {
177 mBackButton.setVisibility(View.GONE);
178 mForwardButton.setVisibility(View.GONE);
179 mStopButton.setVisibility(View.GONE);
180 mAllButton.setVisibility(View.GONE);
181 } else {
182 mBackButton.setVisibility(View.VISIBLE);
183 mForwardButton.setVisibility(View.VISIBLE);
184 mStopButton.setVisibility(View.VISIBLE);
185 mAllButton.setVisibility(View.VISIBLE);
186 }
187 }
188
189 void setShowProgressOnly(boolean progress) {
190 if (progress) {
191 mContainer.setVisibility(View.GONE);
192 } else {
193 mContainer.setVisibility(View.VISIBLE);
194 }
195 }
196
Michael Kolb31d469b2010-12-09 20:49:54 -0800197 @Override
198 public void onFocusChange(View view, boolean hasFocus) {
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800199 // if losing focus and not in touch mode, leave as is
200 if (hasFocus || view.isInTouchMode() || mUrlInput.needsUpdate()) {
201 setFocusState(hasFocus);
202 mUrlContainer.setBackgroundDrawable(hasFocus
203 ? mFocusDrawable : mUnfocusDrawable);
204 }
205 if (hasFocus) {
206 mUrlInput.forceIme();
207 if (mInVoiceMode) {
208 mUrlInput.forceFilter();
209 }
210 } else if (!mUrlInput.needsUpdate()) {
211 mUrlInput.dismissDropDown();
212 mUrlInput.hideIME();
213 }
214 mUrlInput.clearNeedsUpdate();
Michael Kolbc7485ae2010-09-03 10:10:58 -0700215 }
Michael Kolbb7b115e2010-09-25 16:59:37 -0700216
John Reck94b7e042011-02-15 15:02:33 -0800217 @Override
Leon Scroggins4cd97792010-12-03 15:31:56 -0500218 public void setCurrentUrlIsBookmark(boolean isBookmark) {
Michael Kolb31d469b2010-12-09 20:49:54 -0800219 mStar.setActivated(isBookmark);
Leon Scroggins4cd97792010-12-03 15:31:56 -0500220 }
221
Michael Kolb81b6f832010-12-12 12:44:27 -0800222 /**
223 * called from the Ui when the user wants to edit
Michael Kolb81b6f832010-12-12 12:44:27 -0800224 * @param clearInput clear the input field
225 */
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800226 void startEditingUrl(boolean clearInput) {
Michael Kolbbd018d42010-12-15 15:43:39 -0800227 // editing takes preference of progress
228 mContainer.setVisibility(View.VISIBLE);
229 if (mUseQuickControls) {
230 mProgressView.setVisibility(View.GONE);
231 }
Michael Kolbcfa3af52010-12-14 10:36:11 -0800232 if (!mUrlInput.hasFocus()) {
233 mUrlInput.requestFocus();
234 }
Michael Kolb81b6f832010-12-12 12:44:27 -0800235 if (clearInput) {
236 mUrlInput.setText("");
Michael Kolbcfa3af52010-12-14 10:36:11 -0800237 } else if (mInVoiceMode) {
238 mUrlInput.showDropDown();
Michael Kolb81b6f832010-12-12 12:44:27 -0800239 }
240 }
241
242 boolean isEditingUrl() {
243 return mUrlInput.hasFocus();
244 }
245
Michael Kolb7cdc4902011-02-03 17:54:40 -0800246 void stopEditingUrl() {
247 mUrlInput.clearFocus();
248 }
249
Michael Kolba2b2ba82010-08-04 17:54:03 -0700250 @Override
251 public void onClick(View v) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800252 if (mBackButton == v) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700253 mUiController.getCurrentTopWebView().goBack();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700254 } else if (mForwardButton == v) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700255 mUiController.getCurrentTopWebView().goForward();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700256 } else if (mStar == v) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700257 mUiController.bookmarkCurrentPage(
Leon Scrogginsbdff8a72011-02-11 15:49:04 -0500258 AddBookmarkPage.DEFAULT_FOLDER_ID, true);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700259 } else if (mAllButton == v) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700260 mUiController.bookmarksOrHistoryPicker(false);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700261 } else if (mSearchButton == v) {
Michael Kolb81b6f832010-12-12 12:44:27 -0800262 mUi.editUrl(true);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700263 } else if (mStopButton == v) {
264 stopOrRefresh();
Michael Kolb513286f2010-09-09 12:55:12 -0700265 } else if (mGoButton == v) {
Michael Kolb31d469b2010-12-09 20:49:54 -0800266 if (!TextUtils.isEmpty(mUrlInput.getText())) {
267 onAction(mUrlInput.getText().toString(), null,
Michael Kolb257cc2c2010-12-09 09:45:52 -0800268 UrlInputView.TYPED);
Michael Kolb1ce78132010-09-23 15:50:53 -0700269 }
Michael Kolbb7b115e2010-09-25 16:59:37 -0700270 } else if (mClearButton == v) {
Michael Kolb31d469b2010-12-09 20:49:54 -0800271 clearOrClose();
Michael Kolbcfa3af52010-12-14 10:36:11 -0800272 } else if (mVoiceSearch == v) {
273 mUiController.startVoiceSearch();
Michael Kolbfe251992010-07-08 15:41:55 -0700274 }
275 }
276
Michael Kolba2b2ba82010-08-04 17:54:03 -0700277 @Override
278 void setFavicon(Bitmap icon) { }
Michael Kolbfe251992010-07-08 15:41:55 -0700279
Michael Kolb31d469b2010-12-09 20:49:54 -0800280 private void clearOrClose() {
Narayan Kamath5119edd2011-02-23 15:49:17 +0000281 if (TextUtils.isEmpty(mUrlInput.getUserText())) {
Michael Kolb31d469b2010-12-09 20:49:54 -0800282 // close
Michael Kolb7cdc4902011-02-03 17:54:40 -0800283 mUrlInput.clearFocus();
Michael Kolb31d469b2010-12-09 20:49:54 -0800284 } else {
285 // clear
286 mUrlInput.setText("");
287 }
288 }
289
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800290 private void setFocusState(boolean focus) {
291 if (focus) {
Michael Kolb31d469b2010-12-09 20:49:54 -0800292 mUrlInput.setDropDownWidth(mUrlContainer.getWidth());
293 mUrlInput.setDropDownHorizontalOffset(-mUrlInput.getLeft());
Michael Kolbb7b115e2010-09-25 16:59:37 -0700294 mSearchButton.setVisibility(View.GONE);
Michael Kolb31d469b2010-12-09 20:49:54 -0800295 mStar.setVisibility(View.GONE);
296 mClearButton.setVisibility(View.VISIBLE);
Michael Kolbcfa3af52010-12-14 10:36:11 -0800297 if (mInVoiceMode) {
298 mVoiceSearchIndicator.setVisibility(View.VISIBLE);
299 }
Michael Kolb5b2299c2011-01-25 16:54:34 -0800300 mWebIcon.setImageResource(R.drawable.ic_search_holo_dark);
Michael Kolb60a68f52011-02-24 11:02:52 -0800301 updateSearchMode(false);
Michael Kolbb7b115e2010-09-25 16:59:37 -0700302 } else {
Michael Kolbb7b115e2010-09-25 16:59:37 -0700303 mGoButton.setVisibility(View.GONE);
Michael Kolb31d469b2010-12-09 20:49:54 -0800304 mVoiceSearch.setVisibility(View.GONE);
305 mStar.setVisibility(View.VISIBLE);
306 mClearButton.setVisibility(View.GONE);
Michael Kolbcfa3af52010-12-14 10:36:11 -0800307 mVoiceSearchIndicator.setVisibility(View.GONE);
Michael Kolb376b5412010-12-15 11:52:57 -0800308 if (mUseQuickControls) {
309 mSearchButton.setVisibility(View.GONE);
310 } else {
311 mSearchButton.setVisibility(View.VISIBLE);
312 }
Michael Kolb5b2299c2011-01-25 16:54:34 -0800313 mWebIcon.setImageResource(R.drawable.ic_web_holo_dark);
Michael Kolbb7b115e2010-09-25 16:59:37 -0700314 }
315 }
316
Michael Kolba2b2ba82010-08-04 17:54:03 -0700317 private void stopOrRefresh() {
318 if (mInLoad) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700319 mUiController.stopLoading();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700320 } else {
Michael Kolb8233fac2010-10-26 16:08:53 -0700321 mUiController.getCurrentTopWebView().reload();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700322 }
Leon Scroggins571b3762010-05-26 10:25:01 -0400323 }
324
325 /**
Michael Kolbfe251992010-07-08 15:41:55 -0700326 * Update the progress, from 0 to 100.
Leon Scroggins571b3762010-05-26 10:25:01 -0400327 */
Michael Kolbfe251992010-07-08 15:41:55 -0700328 @Override
Michael Kolba2b2ba82010-08-04 17:54:03 -0700329 void setProgress(int newProgress) {
Michael Kolbbd018d42010-12-15 15:43:39 -0800330 boolean blockvisuals = mUseQuickControls && isEditingUrl();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700331 if (newProgress >= PROGRESS_MAX) {
Michael Kolbbd018d42010-12-15 15:43:39 -0800332 if (!blockvisuals) {
333 mProgressView.setProgress(PageProgressView.MAX_PROGRESS);
334 mProgressView.setVisibility(View.GONE);
335 mStopButton.setImageDrawable(mReloadDrawable);
336 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700337 mInLoad = false;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700338 } else {
339 if (!mInLoad) {
Michael Kolbbd018d42010-12-15 15:43:39 -0800340 if (!blockvisuals) {
341 mProgressView.setVisibility(View.VISIBLE);
342 mStopButton.setImageDrawable(mStopDrawable);
343 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700344 mInLoad = true;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700345 }
Michael Kolbb7b115e2010-09-25 16:59:37 -0700346 mProgressView.setProgress(newProgress * PageProgressView.MAX_PROGRESS
347 / PROGRESS_MAX);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700348 }
Michael Kolbfe251992010-07-08 15:41:55 -0700349 }
350
Michael Kolb60a68f52011-02-24 11:02:52 -0800351 private void updateSearchMode(boolean userEdited) {
Narayan Kamath5119edd2011-02-23 15:49:17 +0000352 setSearchMode(!userEdited || TextUtils.isEmpty(mUrlInput.getUserText()));
Michael Kolb31d469b2010-12-09 20:49:54 -0800353 }
354
355 private void setSearchMode(boolean voiceSearchEnabled) {
Michael Kolb793e05e2011-01-11 15:17:31 -0800356 SearchEngine searchEngine = BrowserSettings.getInstance()
357 .getSearchEngine();
358 boolean showvoicebutton = voiceSearchEnabled &&
359 (searchEngine != null && searchEngine.supportsVoiceSearch());
360 mVoiceSearch.setVisibility(showvoicebutton ? View.VISIBLE :
Michael Kolb31d469b2010-12-09 20:49:54 -0800361 View.GONE);
362 mGoButton.setVisibility(voiceSearchEnabled ? View.GONE :
363 View.VISIBLE);
364 }
365
Michael Kolbfe251992010-07-08 15:41:55 -0700366 @Override
Leon Scroggins571b3762010-05-26 10:25:01 -0400367 /* package */ void setDisplayTitle(String title) {
John Reck4851f9e2010-12-22 16:40:36 -0800368 if (!isEditingUrl()) {
369 mUrlInput.setText(title, false);
370 }
Michael Kolb7b20ddd2010-10-14 15:03:28 -0700371 }
372
Michael Kolb31d469b2010-12-09 20:49:54 -0800373 // UrlInput text watcher
Leon Scroggins4cd97792010-12-03 15:31:56 -0500374
Michael Kolb31d469b2010-12-09 20:49:54 -0800375 @Override
Narayan Kamath80aad8d2011-02-23 12:01:13 +0000376 public void onTextChanged(String newText) {
Michael Kolb31d469b2010-12-09 20:49:54 -0800377 if (mUrlInput.hasFocus()) {
Michael Kolb81b6f832010-12-12 12:44:27 -0800378 // check if input field is empty and adjust voice search state
Michael Kolb60a68f52011-02-24 11:02:52 -0800379 updateSearchMode(true);
Michael Kolbcfa3af52010-12-14 10:36:11 -0800380 // clear voice mode when user types
381 setInVoiceMode(false, null);
Leon Scroggins4cd97792010-12-03 15:31:56 -0500382 }
383 }
Michael Kolb31d469b2010-12-09 20:49:54 -0800384
Michael Kolbcfa3af52010-12-14 10:36:11 -0800385 // voicesearch
386
387 @Override
388 public void setInVoiceMode(boolean voicemode) {
389 setInVoiceMode(voicemode, null);
390 }
391
392 public void setInVoiceMode(boolean voicemode, List<String> voiceResults) {
393 mInVoiceMode = voicemode;
394 mUrlInput.setVoiceResults(voiceResults);
395 mVoiceSearchIndicator.setVisibility(mInVoiceMode
396 ? View.VISIBLE : View.GONE);
Michael Kolb3a2a1642011-02-15 10:52:07 -0800397 mWebIcon.setVisibility(mInVoiceMode ? View.GONE : View.VISIBLE);
Michael Kolbcfa3af52010-12-14 10:36:11 -0800398 }
399
John Reck117f07d2011-01-24 09:39:03 -0800400 @Override
401 void setIncognitoMode(boolean incognito) {
402 mUrlInput.setIncognitoMode(incognito);
403 }
Michael Kolb47171d82011-01-28 10:34:15 -0800404
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800405 @Override
406 public View focusSearch(View focused, int dir) {
407 if (FOCUS_DOWN == dir && hasFocus()) {
408 return getCurrentWebView();
409 }
410 return super.focusSearch(focused, dir);
411 }
412
413 @Override
414 public boolean dispatchKeyEventPreIme(KeyEvent evt) {
415 if (evt.getKeyCode() == KeyEvent.KEYCODE_BACK) {
416 // catch back key in order to do slightly more cleanup than usual
417 mUrlInput.clearFocus();
418 return true;
419 }
420 return super.dispatchKeyEventPreIme(evt);
421 }
422
423 private WebView getCurrentWebView() {
424 Tab t = mUi.getActiveTab();
425 if (t != null) {
426 return t.getWebView();
427 } else {
428 return null;
429 }
430 }
431
Narayan Kamath5119edd2011-02-23 15:49:17 +0000432 void registerDropdownChangeListener(DropdownChangeListener d) {
433 mUrlInput.registerDropdownChangeListener(d);
434 }
Leon Scroggins571b3762010-05-26 10:25:01 -0400435}