blob: 8c03e4c5c0ff344adb6a37dc6dcd70239bfe5e29 [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 Kolb3aaef252011-03-09 18:13:56 -080019import com.android.browser.autocomplete.SuggestedTextController.TextChangeWatcher;
Michael Kolbc7485ae2010-09-03 10:10:58 -070020
Michael Kolb8233fac2010-10-26 16:08:53 -070021import android.app.Activity;
Leon Scroggins571b3762010-05-26 10:25:01 -040022import android.content.Context;
John Reckb8b2af82011-05-20 15:58:33 -070023import android.content.SharedPreferences;
24import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
Leon Scroggins571b3762010-05-26 10:25:01 -040025import android.content.res.Resources;
Michael Kolbfe251992010-07-08 15:41:55 -070026import android.graphics.Bitmap;
Leon Scroggins571b3762010-05-26 10:25:01 -040027import android.graphics.drawable.Drawable;
John Reckb8b2af82011-05-20 15:58:33 -070028import android.preference.PreferenceManager;
Michael Kolb1ce78132010-09-23 15:50:53 -070029import android.text.TextUtils;
Leon Scroggins571b3762010-05-26 10:25:01 -040030import android.view.View;
Michael Kolba2b2ba82010-08-04 17:54:03 -070031import android.view.View.OnClickListener;
Michael Kolb31d469b2010-12-09 20:49:54 -080032import android.view.View.OnFocusChangeListener;
Michael Kolbb6bc32c2010-12-10 11:51:54 -080033import android.webkit.WebView;
Michael Kolb7cdc4902011-02-03 17:54:40 -080034import android.widget.FrameLayout;
Michael Kolb5a72f182011-01-13 20:35:06 -080035import android.widget.ImageButton;
Leon Scroggins571b3762010-05-26 10:25:01 -040036import android.widget.ImageView;
Leon Scroggins571b3762010-05-26 10:25:01 -040037
Michael Kolbcfa3af52010-12-14 10:36:11 -080038import java.util.List;
39
Leon Scroggins571b3762010-05-26 10:25:01 -040040/**
Michael Kolbfe251992010-07-08 15:41:55 -070041 * tabbed title bar for xlarge screen browser
Leon Scroggins571b3762010-05-26 10:25:01 -040042 */
Michael Kolbfe251992010-07-08 15:41:55 -070043public class TitleBarXLarge extends TitleBarBase
Patrick Scott92066772011-03-10 08:46:27 -050044 implements OnClickListener, OnFocusChangeListener, TextChangeWatcher,
45 DeviceAccountLogin.AutoLoginCallback {
Leon Scroggins571b3762010-05-26 10:25:01 -040046
Michael Kolb66706532010-12-12 19:50:22 -080047 private XLargeUi mUi;
Michael Kolb8233fac2010-10-26 16:08:53 -070048
Michael Kolba2b2ba82010-08-04 17:54:03 -070049 private Drawable mStopDrawable;
50 private Drawable mReloadDrawable;
Michael Kolbc7485ae2010-09-03 10:10:58 -070051
Michael Kolb11d19782011-03-20 10:17:40 -070052 private View mUrlContainer;
Michael Kolb5a72f182011-01-13 20:35:06 -080053 private ImageButton mBackButton;
54 private ImageButton mForwardButton;
Michael Kolb31d469b2010-12-09 20:49:54 -080055 private ImageView mStar;
Michael Kolbe3524d82011-03-02 14:53:15 -080056 private ImageView mUrlIcon;
57 private ImageView mSearchButton;
Michael Kolb513286f2010-09-09 12:55:12 -070058 private View mGoButton;
Michael Kolba2b2ba82010-08-04 17:54:03 -070059 private ImageView mStopButton;
Michael Kolba2b2ba82010-08-04 17:54:03 -070060 private View mAllButton;
Michael Kolbb7b115e2010-09-25 16:59:37 -070061 private View mClearButton;
Michael Kolbe3524d82011-03-02 14:53:15 -080062 private ImageView mVoiceSearch;
Michael Kolbcfa3af52010-12-14 10:36:11 -080063 private Drawable mFocusDrawable;
64 private Drawable mUnfocusDrawable;
John Reckb8b2af82011-05-20 15:58:33 -070065 private boolean mHasFocus = false;
66 private BrowserSettings mSettings;
Michael Kolb81b6f832010-12-12 12:44:27 -080067
Michael Kolb81b6f832010-12-12 12:44:27 -080068 public TitleBarXLarge(Activity activity, UiController controller,
Michael Kolb46f987e2011-04-05 10:39:10 -070069 XLargeUi ui, FrameLayout parent) {
70 super(activity, controller, ui, parent);
Michael Kolb81b6f832010-12-12 12:44:27 -080071 mUi = ui;
Michael Kolb8233fac2010-10-26 16:08:53 -070072 Resources resources = activity.getResources();
Michael Kolb5a72f182011-01-13 20:35:06 -080073 mStopDrawable = resources.getDrawable(R.drawable.ic_stop_holo_dark);
74 mReloadDrawable = resources.getDrawable(R.drawable.ic_refresh_holo_dark);
Michael Kolbcfa3af52010-12-14 10:36:11 -080075 mFocusDrawable = resources.getDrawable(
76 R.drawable.textfield_active_holo_dark);
77 mUnfocusDrawable = resources.getDrawable(
78 R.drawable.textfield_default_holo_dark);
Michael Kolbcfa3af52010-12-14 10:36:11 -080079 mInVoiceMode = false;
John Reckb8b2af82011-05-20 15:58:33 -070080 mSettings = BrowserSettings.getInstance();
Michael Kolb11d19782011-03-20 10:17:40 -070081 initLayout(activity, R.layout.url_bar);
John Reckb8b2af82011-05-20 15:58:33 -070082 PreferenceManager.getDefaultSharedPreferences(activity)
83 .registerOnSharedPreferenceChangeListener(mSharedPrefsListener);
Michael Kolbfe251992010-07-08 15:41:55 -070084 }
Leon Scroggins571b3762010-05-26 10:25:01 -040085
Michael Kolb7cdc4902011-02-03 17:54:40 -080086 @Override
Michael Kolb11d19782011-03-20 10:17:40 -070087 protected void initLayout(Context context, int layoutId) {
88 super.initLayout(context, layoutId);
Michael Kolbfe251992010-07-08 15:41:55 -070089 mAllButton = findViewById(R.id.all_btn);
90 // TODO: Change enabled states based on whether you can go
Leon Scroggins571b3762010-05-26 10:25:01 -040091 // back/forward. Probably should be done inside onPageStarted.
Michael Kolb5a72f182011-01-13 20:35:06 -080092 mBackButton = (ImageButton) findViewById(R.id.back);
93 mForwardButton = (ImageButton) findViewById(R.id.forward);
Michael Kolbe3524d82011-03-02 14:53:15 -080094 mUrlIcon = (ImageView) findViewById(R.id.url_icon);
Michael Kolb31d469b2010-12-09 20:49:54 -080095 mStar = (ImageView) findViewById(R.id.star);
Michael Kolba2b2ba82010-08-04 17:54:03 -070096 mStopButton = (ImageView) findViewById(R.id.stop);
Michael Kolbe3524d82011-03-02 14:53:15 -080097 mSearchButton = (ImageView) findViewById(R.id.search);
Michael Kolba2b2ba82010-08-04 17:54:03 -070098 mLockIcon = (ImageView) findViewById(R.id.lock);
Michael Kolb513286f2010-09-09 12:55:12 -070099 mGoButton = findViewById(R.id.go);
Michael Kolbb7b115e2010-09-25 16:59:37 -0700100 mClearButton = findViewById(R.id.clear);
Michael Kolbe3524d82011-03-02 14:53:15 -0800101 mVoiceSearch = (ImageView) findViewById(R.id.voicesearch);
Michael Kolb31d469b2010-12-09 20:49:54 -0800102 mUrlContainer = findViewById(R.id.urlbar_focused);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700103 mBackButton.setOnClickListener(this);
104 mForwardButton.setOnClickListener(this);
105 mStar.setOnClickListener(this);
106 mAllButton.setOnClickListener(this);
107 mStopButton.setOnClickListener(this);
108 mSearchButton.setOnClickListener(this);
Michael Kolb513286f2010-09-09 12:55:12 -0700109 mGoButton.setOnClickListener(this);
Michael Kolbb7b115e2010-09-25 16:59:37 -0700110 mClearButton.setOnClickListener(this);
Michael Kolbcfa3af52010-12-14 10:36:11 -0800111 mVoiceSearch.setOnClickListener(this);
John Reckb8b2af82011-05-20 15:58:33 -0700112 mUrlIcon.setOnClickListener(this);
Michael Kolb31d469b2010-12-09 20:49:54 -0800113 mUrlInput.setContainer(mUrlContainer);
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800114 setFocusState(false);
Michael Kolb31d469b2010-12-09 20:49:54 -0800115 }
116
Michael Kolb5a72f182011-01-13 20:35:06 -0800117 void updateNavigationState(Tab tab) {
118 WebView web = tab.getWebView();
119 if (web != null) {
120 mBackButton.setImageResource(web.canGoBack()
121 ? R.drawable.ic_back_holo_dark
122 : R.drawable.ic_back_disabled_holo_dark);
123 mForwardButton.setImageResource(web.canGoForward()
124 ? R.drawable.ic_forward_holo_dark
125 : R.drawable.ic_forward_disabled_holo_dark);
126 }
John Reckb8b2af82011-05-20 15:58:33 -0700127 updateUrlIcon();
Michael Kolb5a72f182011-01-13 20:35:06 -0800128 }
129
Michael Kolb31d469b2010-12-09 20:49:54 -0800130 @Override
131 public void onFocusChange(View view, boolean hasFocus) {
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800132 // if losing focus and not in touch mode, leave as is
133 if (hasFocus || view.isInTouchMode() || mUrlInput.needsUpdate()) {
134 setFocusState(hasFocus);
135 mUrlContainer.setBackgroundDrawable(hasFocus
136 ? mFocusDrawable : mUnfocusDrawable);
137 }
138 if (hasFocus) {
139 mUrlInput.forceIme();
140 if (mInVoiceMode) {
141 mUrlInput.forceFilter();
142 }
143 } else if (!mUrlInput.needsUpdate()) {
144 mUrlInput.dismissDropDown();
145 mUrlInput.hideIME();
Michael Kolb2a56eca2011-02-25 09:45:06 -0800146 if (mUseQuickControls) {
147 mUi.hideTitleBar();
148 }
Narayan Kamathc93a2a92011-03-15 11:33:52 +0000149
150 if (mUrlInput.getText().length() == 0) {
151 Tab currentTab = mUiController.getTabControl().getCurrentTab();
152 if (currentTab != null) {
153 mUrlInput.setText(currentTab.getUrl(), false);
154 }
155 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800156 }
157 mUrlInput.clearNeedsUpdate();
Michael Kolbc7485ae2010-09-03 10:10:58 -0700158 }
Michael Kolbb7b115e2010-09-25 16:59:37 -0700159
John Reck94b7e042011-02-15 15:02:33 -0800160 @Override
Leon Scroggins4cd97792010-12-03 15:31:56 -0500161 public void setCurrentUrlIsBookmark(boolean isBookmark) {
Michael Kolb31d469b2010-12-09 20:49:54 -0800162 mStar.setActivated(isBookmark);
Leon Scroggins4cd97792010-12-03 15:31:56 -0500163 }
164
Patrick Scott92066772011-03-10 08:46:27 -0500165
Michael Kolba2b2ba82010-08-04 17:54:03 -0700166 @Override
167 public void onClick(View v) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800168 if (mBackButton == v) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700169 mUiController.getCurrentTopWebView().goBack();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700170 } else if (mForwardButton == v) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700171 mUiController.getCurrentTopWebView().goForward();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700172 } else if (mStar == v) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700173 mUiController.bookmarkCurrentPage(
Leon Scrogginsbdff8a72011-02-11 15:49:04 -0500174 AddBookmarkPage.DEFAULT_FOLDER_ID, true);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700175 } else if (mAllButton == v) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700176 mUiController.bookmarksOrHistoryPicker(false);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700177 } else if (mSearchButton == v) {
Michael Kolb81b6f832010-12-12 12:44:27 -0800178 mUi.editUrl(true);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700179 } else if (mStopButton == v) {
180 stopOrRefresh();
Michael Kolb513286f2010-09-09 12:55:12 -0700181 } else if (mGoButton == v) {
Michael Kolb31d469b2010-12-09 20:49:54 -0800182 if (!TextUtils.isEmpty(mUrlInput.getText())) {
183 onAction(mUrlInput.getText().toString(), null,
Michael Kolb257cc2c2010-12-09 09:45:52 -0800184 UrlInputView.TYPED);
Michael Kolb1ce78132010-09-23 15:50:53 -0700185 }
Michael Kolbb7b115e2010-09-25 16:59:37 -0700186 } else if (mClearButton == v) {
Michael Kolb31d469b2010-12-09 20:49:54 -0800187 clearOrClose();
Michael Kolbcfa3af52010-12-14 10:36:11 -0800188 } else if (mVoiceSearch == v) {
189 mUiController.startVoiceSearch();
John Reckb8b2af82011-05-20 15:58:33 -0700190 } else if (mUrlIcon == v) {
191 WebView web = mUiController.getCurrentWebView();
192 if (mSettings.enableUseragentSwitcher() && web != null) {
193 mSettings.toggleDesktopUseragent(web);
194 web.loadUrl(web.getOriginalUrl());
195 updateUrlIcon();
196 }
Michael Kolb11d19782011-03-20 10:17:40 -0700197 } else {
198 super.onClick(v);
Michael Kolbfe251992010-07-08 15:41:55 -0700199 }
200 }
201
Michael Kolba2b2ba82010-08-04 17:54:03 -0700202 @Override
203 void setFavicon(Bitmap icon) { }
Michael Kolbfe251992010-07-08 15:41:55 -0700204
Michael Kolb31d469b2010-12-09 20:49:54 -0800205 private void clearOrClose() {
Narayan Kamath5119edd2011-02-23 15:49:17 +0000206 if (TextUtils.isEmpty(mUrlInput.getUserText())) {
Michael Kolb31d469b2010-12-09 20:49:54 -0800207 // close
Michael Kolb7cdc4902011-02-03 17:54:40 -0800208 mUrlInput.clearFocus();
Michael Kolb31d469b2010-12-09 20:49:54 -0800209 } else {
210 // clear
211 mUrlInput.setText("");
212 }
213 }
214
John Reckb8b2af82011-05-20 15:58:33 -0700215 void updateUrlIcon() {
216 if (mHasFocus) {
217 return;
218 }
219 if (!mInVoiceMode && mSettings.enableUseragentSwitcher()) {
220 WebView web = mUiController.getCurrentWebView();
221 if (mSettings.hasDesktopUseragent(web)) {
222 mUrlIcon.setImageResource(R.drawable.ic_ua_desktop);
223 } else {
224 mUrlIcon.setImageResource(R.drawable.ic_ua_android);
225 }
226 } else {
227 mUrlIcon.setImageResource(mInVoiceMode ?
228 R.drawable.ic_search_holo_dark
229 : R.drawable.ic_web_holo_dark);
230 }
231 }
232
233 private OnSharedPreferenceChangeListener mSharedPrefsListener =
234 new OnSharedPreferenceChangeListener() {
235
236 @Override
237 public void onSharedPreferenceChanged(
238 SharedPreferences sharedPreferences, String key) {
239 updateUrlIcon();
240 }
241
242 };
243
Michael Kolb11d19782011-03-20 10:17:40 -0700244 @Override
245 protected void setFocusState(boolean focus) {
246 super.setFocusState(focus);
John Reckb8b2af82011-05-20 15:58:33 -0700247 mHasFocus = focus;
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800248 if (focus) {
Michael Kolbb7b115e2010-09-25 16:59:37 -0700249 mSearchButton.setVisibility(View.GONE);
Michael Kolb31d469b2010-12-09 20:49:54 -0800250 mStar.setVisibility(View.GONE);
251 mClearButton.setVisibility(View.VISIBLE);
Michael Kolbe3524d82011-03-02 14:53:15 -0800252 mUrlIcon.setImageResource(R.drawable.ic_search_holo_dark);
Michael Kolb60a68f52011-02-24 11:02:52 -0800253 updateSearchMode(false);
Michael Kolbb7b115e2010-09-25 16:59:37 -0700254 } else {
Michael Kolbb7b115e2010-09-25 16:59:37 -0700255 mGoButton.setVisibility(View.GONE);
Michael Kolb31d469b2010-12-09 20:49:54 -0800256 mVoiceSearch.setVisibility(View.GONE);
257 mStar.setVisibility(View.VISIBLE);
258 mClearButton.setVisibility(View.GONE);
Michael Kolb376b5412010-12-15 11:52:57 -0800259 if (mUseQuickControls) {
260 mSearchButton.setVisibility(View.GONE);
261 } else {
262 mSearchButton.setVisibility(View.VISIBLE);
263 }
John Reckb8b2af82011-05-20 15:58:33 -0700264 updateUrlIcon();
Michael Kolbb7b115e2010-09-25 16:59:37 -0700265 }
266 }
267
Michael Kolba2b2ba82010-08-04 17:54:03 -0700268 private void stopOrRefresh() {
269 if (mInLoad) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700270 mUiController.stopLoading();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700271 } else {
Michael Kolb8233fac2010-10-26 16:08:53 -0700272 mUiController.getCurrentTopWebView().reload();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700273 }
Leon Scroggins571b3762010-05-26 10:25:01 -0400274 }
275
Michael Kolbfe251992010-07-08 15:41:55 -0700276 @Override
Michael Kolb46f987e2011-04-05 10:39:10 -0700277 protected void onProgressStarted() {
278 mStopButton.setImageDrawable(mStopDrawable);
279 }
280
281 @Override
282 protected void onProgressStopped() {
283 mStopButton.setImageDrawable(mReloadDrawable);
Michael Kolbfe251992010-07-08 15:41:55 -0700284 }
285
Michael Kolb11d19782011-03-20 10:17:40 -0700286 @Override
287 protected void updateSearchMode(boolean userEdited) {
Narayan Kamath5119edd2011-02-23 15:49:17 +0000288 setSearchMode(!userEdited || TextUtils.isEmpty(mUrlInput.getUserText()));
Michael Kolb31d469b2010-12-09 20:49:54 -0800289 }
290
Michael Kolb11d19782011-03-20 10:17:40 -0700291 @Override
292 protected void setSearchMode(boolean voiceSearchEnabled) {
Michael Kolb793e05e2011-01-11 15:17:31 -0800293 boolean showvoicebutton = voiceSearchEnabled &&
Michael Kolb736990c2011-03-20 10:01:20 -0700294 mUiController.supportsVoiceSearch();
Michael Kolb793e05e2011-01-11 15:17:31 -0800295 mVoiceSearch.setVisibility(showvoicebutton ? View.VISIBLE :
Michael Kolb31d469b2010-12-09 20:49:54 -0800296 View.GONE);
297 mGoButton.setVisibility(voiceSearchEnabled ? View.GONE :
298 View.VISIBLE);
299 }
300
Michael Kolbfe251992010-07-08 15:41:55 -0700301 @Override
Michael Kolbcfa3af52010-12-14 10:36:11 -0800302 public void setInVoiceMode(boolean voicemode, List<String> voiceResults) {
Michael Kolb11d19782011-03-20 10:17:40 -0700303 super.setInVoiceMode(voicemode, voiceResults);
Michael Kolb3aaef252011-03-09 18:13:56 -0800304 if (voicemode) {
305 mUrlIcon.setImageDrawable(mSearchButton.getDrawable());
306 }
Michael Kolbcfa3af52010-12-14 10:36:11 -0800307 }
308
John Reck117f07d2011-01-24 09:39:03 -0800309 @Override
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800310 public View focusSearch(View focused, int dir) {
311 if (FOCUS_DOWN == dir && hasFocus()) {
312 return getCurrentWebView();
313 }
314 return super.focusSearch(focused, dir);
315 }
316
Leon Scroggins571b3762010-05-26 10:25:01 -0400317}