blob: 567b35a43220f306c135e4238cd1232c1778675c [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;
23import android.content.res.Resources;
Michael Kolbfe251992010-07-08 15:41:55 -070024import android.graphics.Bitmap;
Leon Scroggins571b3762010-05-26 10:25:01 -040025import android.graphics.drawable.Drawable;
Michael Kolb1ce78132010-09-23 15:50:53 -070026import android.text.TextUtils;
Leon Scroggins571b3762010-05-26 10:25:01 -040027import android.view.View;
Michael Kolba2b2ba82010-08-04 17:54:03 -070028import android.view.View.OnClickListener;
Michael Kolb31d469b2010-12-09 20:49:54 -080029import android.view.View.OnFocusChangeListener;
Michael Kolbb6bc32c2010-12-10 11:51:54 -080030import android.webkit.WebView;
Michael Kolb7cdc4902011-02-03 17:54:40 -080031import android.widget.FrameLayout;
Michael Kolb5a72f182011-01-13 20:35:06 -080032import android.widget.ImageButton;
Leon Scroggins571b3762010-05-26 10:25:01 -040033import android.widget.ImageView;
Leon Scroggins571b3762010-05-26 10:25:01 -040034
Michael Kolbcfa3af52010-12-14 10:36:11 -080035import java.util.List;
36
Leon Scroggins571b3762010-05-26 10:25:01 -040037/**
Michael Kolbfe251992010-07-08 15:41:55 -070038 * tabbed title bar for xlarge screen browser
Leon Scroggins571b3762010-05-26 10:25:01 -040039 */
Michael Kolbfe251992010-07-08 15:41:55 -070040public class TitleBarXLarge extends TitleBarBase
Patrick Scott92066772011-03-10 08:46:27 -050041 implements OnClickListener, OnFocusChangeListener, TextChangeWatcher,
42 DeviceAccountLogin.AutoLoginCallback {
Leon Scroggins571b3762010-05-26 10:25:01 -040043
Michael Kolb66706532010-12-12 19:50:22 -080044 private XLargeUi mUi;
Michael Kolb8233fac2010-10-26 16:08:53 -070045
Michael Kolba2b2ba82010-08-04 17:54:03 -070046 private Drawable mStopDrawable;
47 private Drawable mReloadDrawable;
Michael Kolbc7485ae2010-09-03 10:10:58 -070048
Michael Kolb11d19782011-03-20 10:17:40 -070049 private View mUrlContainer;
Michael Kolb5a72f182011-01-13 20:35:06 -080050 private ImageButton mBackButton;
51 private ImageButton mForwardButton;
Michael Kolb31d469b2010-12-09 20:49:54 -080052 private ImageView mStar;
Michael Kolbe3524d82011-03-02 14:53:15 -080053 private ImageView mUrlIcon;
54 private ImageView mSearchButton;
Michael Kolb513286f2010-09-09 12:55:12 -070055 private View mGoButton;
Michael Kolba2b2ba82010-08-04 17:54:03 -070056 private ImageView mStopButton;
Michael Kolba2b2ba82010-08-04 17:54:03 -070057 private View mAllButton;
Michael Kolbb7b115e2010-09-25 16:59:37 -070058 private View mClearButton;
Michael Kolbe3524d82011-03-02 14:53:15 -080059 private ImageView mVoiceSearch;
Michael Kolbcfa3af52010-12-14 10:36:11 -080060 private Drawable mFocusDrawable;
61 private Drawable mUnfocusDrawable;
Michael Kolb81b6f832010-12-12 12:44:27 -080062
Michael Kolb81b6f832010-12-12 12:44:27 -080063 public TitleBarXLarge(Activity activity, UiController controller,
Michael Kolb46f987e2011-04-05 10:39:10 -070064 XLargeUi ui, FrameLayout parent) {
65 super(activity, controller, ui, parent);
Michael Kolb81b6f832010-12-12 12:44:27 -080066 mUi = ui;
Michael Kolb8233fac2010-10-26 16:08:53 -070067 Resources resources = activity.getResources();
Michael Kolb5a72f182011-01-13 20:35:06 -080068 mStopDrawable = resources.getDrawable(R.drawable.ic_stop_holo_dark);
69 mReloadDrawable = resources.getDrawable(R.drawable.ic_refresh_holo_dark);
Michael Kolbcfa3af52010-12-14 10:36:11 -080070 mFocusDrawable = resources.getDrawable(
71 R.drawable.textfield_active_holo_dark);
72 mUnfocusDrawable = resources.getDrawable(
73 R.drawable.textfield_default_holo_dark);
Michael Kolbcfa3af52010-12-14 10:36:11 -080074 mInVoiceMode = false;
Michael Kolb11d19782011-03-20 10:17:40 -070075 initLayout(activity, R.layout.url_bar);
Michael Kolbfe251992010-07-08 15:41:55 -070076 }
Leon Scroggins571b3762010-05-26 10:25:01 -040077
Michael Kolb7cdc4902011-02-03 17:54:40 -080078 @Override
Michael Kolb11d19782011-03-20 10:17:40 -070079 protected void initLayout(Context context, int layoutId) {
80 super.initLayout(context, layoutId);
Michael Kolbfe251992010-07-08 15:41:55 -070081 mAllButton = findViewById(R.id.all_btn);
82 // TODO: Change enabled states based on whether you can go
Leon Scroggins571b3762010-05-26 10:25:01 -040083 // back/forward. Probably should be done inside onPageStarted.
Michael Kolb5a72f182011-01-13 20:35:06 -080084 mBackButton = (ImageButton) findViewById(R.id.back);
85 mForwardButton = (ImageButton) findViewById(R.id.forward);
Michael Kolbe3524d82011-03-02 14:53:15 -080086 mUrlIcon = (ImageView) findViewById(R.id.url_icon);
Michael Kolb31d469b2010-12-09 20:49:54 -080087 mStar = (ImageView) findViewById(R.id.star);
Michael Kolba2b2ba82010-08-04 17:54:03 -070088 mStopButton = (ImageView) findViewById(R.id.stop);
Michael Kolbe3524d82011-03-02 14:53:15 -080089 mSearchButton = (ImageView) findViewById(R.id.search);
Michael Kolba2b2ba82010-08-04 17:54:03 -070090 mLockIcon = (ImageView) findViewById(R.id.lock);
Michael Kolb513286f2010-09-09 12:55:12 -070091 mGoButton = findViewById(R.id.go);
Michael Kolbb7b115e2010-09-25 16:59:37 -070092 mClearButton = findViewById(R.id.clear);
Michael Kolbe3524d82011-03-02 14:53:15 -080093 mVoiceSearch = (ImageView) findViewById(R.id.voicesearch);
Michael Kolb31d469b2010-12-09 20:49:54 -080094 mUrlContainer = findViewById(R.id.urlbar_focused);
Michael Kolba2b2ba82010-08-04 17:54:03 -070095 mBackButton.setOnClickListener(this);
96 mForwardButton.setOnClickListener(this);
97 mStar.setOnClickListener(this);
98 mAllButton.setOnClickListener(this);
99 mStopButton.setOnClickListener(this);
100 mSearchButton.setOnClickListener(this);
Michael Kolb513286f2010-09-09 12:55:12 -0700101 mGoButton.setOnClickListener(this);
Michael Kolbb7b115e2010-09-25 16:59:37 -0700102 mClearButton.setOnClickListener(this);
Michael Kolbcfa3af52010-12-14 10:36:11 -0800103 mVoiceSearch.setOnClickListener(this);
Michael Kolb31d469b2010-12-09 20:49:54 -0800104 mUrlInput.setContainer(mUrlContainer);
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800105 setFocusState(false);
Michael Kolb31d469b2010-12-09 20:49:54 -0800106 }
107
Michael Kolb5a72f182011-01-13 20:35:06 -0800108 void updateNavigationState(Tab tab) {
109 WebView web = tab.getWebView();
110 if (web != null) {
111 mBackButton.setImageResource(web.canGoBack()
112 ? R.drawable.ic_back_holo_dark
113 : R.drawable.ic_back_disabled_holo_dark);
114 mForwardButton.setImageResource(web.canGoForward()
115 ? R.drawable.ic_forward_holo_dark
116 : R.drawable.ic_forward_disabled_holo_dark);
117 }
118 }
119
Michael Kolb31d469b2010-12-09 20:49:54 -0800120 @Override
121 public void onFocusChange(View view, boolean hasFocus) {
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800122 // if losing focus and not in touch mode, leave as is
123 if (hasFocus || view.isInTouchMode() || mUrlInput.needsUpdate()) {
124 setFocusState(hasFocus);
125 mUrlContainer.setBackgroundDrawable(hasFocus
126 ? mFocusDrawable : mUnfocusDrawable);
127 }
128 if (hasFocus) {
129 mUrlInput.forceIme();
130 if (mInVoiceMode) {
131 mUrlInput.forceFilter();
132 }
133 } else if (!mUrlInput.needsUpdate()) {
134 mUrlInput.dismissDropDown();
135 mUrlInput.hideIME();
Michael Kolb2a56eca2011-02-25 09:45:06 -0800136 if (mUseQuickControls) {
137 mUi.hideTitleBar();
138 }
Narayan Kamathc93a2a92011-03-15 11:33:52 +0000139
140 if (mUrlInput.getText().length() == 0) {
141 Tab currentTab = mUiController.getTabControl().getCurrentTab();
142 if (currentTab != null) {
143 mUrlInput.setText(currentTab.getUrl(), false);
144 }
145 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800146 }
147 mUrlInput.clearNeedsUpdate();
Michael Kolbc7485ae2010-09-03 10:10:58 -0700148 }
Michael Kolbb7b115e2010-09-25 16:59:37 -0700149
John Reck94b7e042011-02-15 15:02:33 -0800150 @Override
Leon Scroggins4cd97792010-12-03 15:31:56 -0500151 public void setCurrentUrlIsBookmark(boolean isBookmark) {
Michael Kolb31d469b2010-12-09 20:49:54 -0800152 mStar.setActivated(isBookmark);
Leon Scroggins4cd97792010-12-03 15:31:56 -0500153 }
154
Patrick Scott92066772011-03-10 08:46:27 -0500155
Michael Kolba2b2ba82010-08-04 17:54:03 -0700156 @Override
157 public void onClick(View v) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800158 if (mBackButton == v) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700159 mUiController.getCurrentTopWebView().goBack();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700160 } else if (mForwardButton == v) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700161 mUiController.getCurrentTopWebView().goForward();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700162 } else if (mStar == v) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700163 mUiController.bookmarkCurrentPage(
Leon Scrogginsbdff8a72011-02-11 15:49:04 -0500164 AddBookmarkPage.DEFAULT_FOLDER_ID, true);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700165 } else if (mAllButton == v) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700166 mUiController.bookmarksOrHistoryPicker(false);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700167 } else if (mSearchButton == v) {
Michael Kolb81b6f832010-12-12 12:44:27 -0800168 mUi.editUrl(true);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700169 } else if (mStopButton == v) {
170 stopOrRefresh();
Michael Kolb513286f2010-09-09 12:55:12 -0700171 } else if (mGoButton == v) {
Michael Kolb31d469b2010-12-09 20:49:54 -0800172 if (!TextUtils.isEmpty(mUrlInput.getText())) {
173 onAction(mUrlInput.getText().toString(), null,
Michael Kolb257cc2c2010-12-09 09:45:52 -0800174 UrlInputView.TYPED);
Michael Kolb1ce78132010-09-23 15:50:53 -0700175 }
Michael Kolbb7b115e2010-09-25 16:59:37 -0700176 } else if (mClearButton == v) {
Michael Kolb31d469b2010-12-09 20:49:54 -0800177 clearOrClose();
Michael Kolbcfa3af52010-12-14 10:36:11 -0800178 } else if (mVoiceSearch == v) {
179 mUiController.startVoiceSearch();
Michael Kolb11d19782011-03-20 10:17:40 -0700180 } else {
181 super.onClick(v);
Michael Kolbfe251992010-07-08 15:41:55 -0700182 }
183 }
184
Michael Kolba2b2ba82010-08-04 17:54:03 -0700185 @Override
186 void setFavicon(Bitmap icon) { }
Michael Kolbfe251992010-07-08 15:41:55 -0700187
Michael Kolb31d469b2010-12-09 20:49:54 -0800188 private void clearOrClose() {
Narayan Kamath5119edd2011-02-23 15:49:17 +0000189 if (TextUtils.isEmpty(mUrlInput.getUserText())) {
Michael Kolb31d469b2010-12-09 20:49:54 -0800190 // close
Michael Kolb7cdc4902011-02-03 17:54:40 -0800191 mUrlInput.clearFocus();
Michael Kolb31d469b2010-12-09 20:49:54 -0800192 } else {
193 // clear
194 mUrlInput.setText("");
195 }
196 }
197
Michael Kolb11d19782011-03-20 10:17:40 -0700198 @Override
199 protected void setFocusState(boolean focus) {
200 super.setFocusState(focus);
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800201 if (focus) {
Michael Kolbb7b115e2010-09-25 16:59:37 -0700202 mSearchButton.setVisibility(View.GONE);
Michael Kolb31d469b2010-12-09 20:49:54 -0800203 mStar.setVisibility(View.GONE);
204 mClearButton.setVisibility(View.VISIBLE);
Michael Kolbe3524d82011-03-02 14:53:15 -0800205 mUrlIcon.setImageResource(R.drawable.ic_search_holo_dark);
Michael Kolb60a68f52011-02-24 11:02:52 -0800206 updateSearchMode(false);
Michael Kolbb7b115e2010-09-25 16:59:37 -0700207 } else {
Michael Kolbb7b115e2010-09-25 16:59:37 -0700208 mGoButton.setVisibility(View.GONE);
Michael Kolb31d469b2010-12-09 20:49:54 -0800209 mVoiceSearch.setVisibility(View.GONE);
210 mStar.setVisibility(View.VISIBLE);
211 mClearButton.setVisibility(View.GONE);
Michael Kolb376b5412010-12-15 11:52:57 -0800212 if (mUseQuickControls) {
213 mSearchButton.setVisibility(View.GONE);
214 } else {
215 mSearchButton.setVisibility(View.VISIBLE);
216 }
Michael Kolbe3524d82011-03-02 14:53:15 -0800217 mUrlIcon.setImageResource(mInVoiceMode ?
218 R.drawable.ic_search_holo_dark
219 : R.drawable.ic_web_holo_dark);
Michael Kolbb7b115e2010-09-25 16:59:37 -0700220 }
221 }
222
Michael Kolba2b2ba82010-08-04 17:54:03 -0700223 private void stopOrRefresh() {
224 if (mInLoad) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700225 mUiController.stopLoading();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700226 } else {
Michael Kolb8233fac2010-10-26 16:08:53 -0700227 mUiController.getCurrentTopWebView().reload();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700228 }
Leon Scroggins571b3762010-05-26 10:25:01 -0400229 }
230
Michael Kolbfe251992010-07-08 15:41:55 -0700231 @Override
Michael Kolb46f987e2011-04-05 10:39:10 -0700232 protected void onProgressStarted() {
233 mStopButton.setImageDrawable(mStopDrawable);
234 }
235
236 @Override
237 protected void onProgressStopped() {
238 mStopButton.setImageDrawable(mReloadDrawable);
Michael Kolbfe251992010-07-08 15:41:55 -0700239 }
240
Michael Kolb11d19782011-03-20 10:17:40 -0700241 @Override
242 protected void updateSearchMode(boolean userEdited) {
Narayan Kamath5119edd2011-02-23 15:49:17 +0000243 setSearchMode(!userEdited || TextUtils.isEmpty(mUrlInput.getUserText()));
Michael Kolb31d469b2010-12-09 20:49:54 -0800244 }
245
Michael Kolb11d19782011-03-20 10:17:40 -0700246 @Override
247 protected void setSearchMode(boolean voiceSearchEnabled) {
Michael Kolb793e05e2011-01-11 15:17:31 -0800248 boolean showvoicebutton = voiceSearchEnabled &&
Michael Kolb736990c2011-03-20 10:01:20 -0700249 mUiController.supportsVoiceSearch();
Michael Kolb793e05e2011-01-11 15:17:31 -0800250 mVoiceSearch.setVisibility(showvoicebutton ? View.VISIBLE :
Michael Kolb31d469b2010-12-09 20:49:54 -0800251 View.GONE);
252 mGoButton.setVisibility(voiceSearchEnabled ? View.GONE :
253 View.VISIBLE);
254 }
255
Michael Kolbfe251992010-07-08 15:41:55 -0700256 @Override
Michael Kolbcfa3af52010-12-14 10:36:11 -0800257 public void setInVoiceMode(boolean voicemode, List<String> voiceResults) {
Michael Kolb11d19782011-03-20 10:17:40 -0700258 super.setInVoiceMode(voicemode, voiceResults);
Michael Kolb3aaef252011-03-09 18:13:56 -0800259 if (voicemode) {
260 mUrlIcon.setImageDrawable(mSearchButton.getDrawable());
261 }
Michael Kolbcfa3af52010-12-14 10:36:11 -0800262 }
263
John Reck117f07d2011-01-24 09:39:03 -0800264 @Override
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800265 public View focusSearch(View focused, int dir) {
266 if (FOCUS_DOWN == dir && hasFocus()) {
267 return getCurrentWebView();
268 }
269 return super.focusSearch(focused, dir);
270 }
271
Leon Scroggins571b3762010-05-26 10:25:01 -0400272}