blob: 91709ccf0ab382fde67c92a9d3e458b38fe7857c [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;
Michael Kolb81b6f832010-12-12 12:44:27 -080065
Michael Kolb81b6f832010-12-12 12:44:27 -080066 public TitleBarXLarge(Activity activity, UiController controller,
Michael Kolb46f987e2011-04-05 10:39:10 -070067 XLargeUi ui, FrameLayout parent) {
68 super(activity, controller, ui, parent);
Michael Kolb81b6f832010-12-12 12:44:27 -080069 mUi = ui;
Michael Kolb8233fac2010-10-26 16:08:53 -070070 Resources resources = activity.getResources();
Michael Kolb5a72f182011-01-13 20:35:06 -080071 mStopDrawable = resources.getDrawable(R.drawable.ic_stop_holo_dark);
72 mReloadDrawable = resources.getDrawable(R.drawable.ic_refresh_holo_dark);
Michael Kolbcfa3af52010-12-14 10:36:11 -080073 mFocusDrawable = resources.getDrawable(
74 R.drawable.textfield_active_holo_dark);
75 mUnfocusDrawable = resources.getDrawable(
76 R.drawable.textfield_default_holo_dark);
Michael Kolbcfa3af52010-12-14 10:36:11 -080077 mInVoiceMode = false;
Michael Kolb11d19782011-03-20 10:17:40 -070078 initLayout(activity, R.layout.url_bar);
Michael Kolbfe251992010-07-08 15:41:55 -070079 }
Leon Scroggins571b3762010-05-26 10:25:01 -040080
Michael Kolb7cdc4902011-02-03 17:54:40 -080081 @Override
Michael Kolb11d19782011-03-20 10:17:40 -070082 protected void initLayout(Context context, int layoutId) {
83 super.initLayout(context, layoutId);
Michael Kolbfe251992010-07-08 15:41:55 -070084 mAllButton = findViewById(R.id.all_btn);
85 // TODO: Change enabled states based on whether you can go
Leon Scroggins571b3762010-05-26 10:25:01 -040086 // back/forward. Probably should be done inside onPageStarted.
Michael Kolb5a72f182011-01-13 20:35:06 -080087 mBackButton = (ImageButton) findViewById(R.id.back);
88 mForwardButton = (ImageButton) findViewById(R.id.forward);
Michael Kolbe3524d82011-03-02 14:53:15 -080089 mUrlIcon = (ImageView) findViewById(R.id.url_icon);
Michael Kolb31d469b2010-12-09 20:49:54 -080090 mStar = (ImageView) findViewById(R.id.star);
Michael Kolba2b2ba82010-08-04 17:54:03 -070091 mStopButton = (ImageView) findViewById(R.id.stop);
Michael Kolbe3524d82011-03-02 14:53:15 -080092 mSearchButton = (ImageView) findViewById(R.id.search);
Michael Kolba2b2ba82010-08-04 17:54:03 -070093 mLockIcon = (ImageView) findViewById(R.id.lock);
Michael Kolb513286f2010-09-09 12:55:12 -070094 mGoButton = findViewById(R.id.go);
Michael Kolbb7b115e2010-09-25 16:59:37 -070095 mClearButton = findViewById(R.id.clear);
Michael Kolbe3524d82011-03-02 14:53:15 -080096 mVoiceSearch = (ImageView) findViewById(R.id.voicesearch);
Michael Kolb31d469b2010-12-09 20:49:54 -080097 mUrlContainer = findViewById(R.id.urlbar_focused);
Michael Kolba2b2ba82010-08-04 17:54:03 -070098 mBackButton.setOnClickListener(this);
99 mForwardButton.setOnClickListener(this);
100 mStar.setOnClickListener(this);
101 mAllButton.setOnClickListener(this);
102 mStopButton.setOnClickListener(this);
103 mSearchButton.setOnClickListener(this);
Michael Kolb513286f2010-09-09 12:55:12 -0700104 mGoButton.setOnClickListener(this);
Michael Kolbb7b115e2010-09-25 16:59:37 -0700105 mClearButton.setOnClickListener(this);
Michael Kolbcfa3af52010-12-14 10:36:11 -0800106 mVoiceSearch.setOnClickListener(this);
John Reck46500332011-06-07 14:36:10 -0700107 setUaSwitcher(mUrlIcon);
Michael Kolb31d469b2010-12-09 20:49:54 -0800108 mUrlInput.setContainer(mUrlContainer);
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800109 setFocusState(false);
Michael Kolb31d469b2010-12-09 20:49:54 -0800110 }
111
Michael Kolb5a72f182011-01-13 20:35:06 -0800112 void updateNavigationState(Tab tab) {
113 WebView web = tab.getWebView();
114 if (web != null) {
115 mBackButton.setImageResource(web.canGoBack()
116 ? R.drawable.ic_back_holo_dark
117 : R.drawable.ic_back_disabled_holo_dark);
118 mForwardButton.setImageResource(web.canGoForward()
119 ? R.drawable.ic_forward_holo_dark
120 : R.drawable.ic_forward_disabled_holo_dark);
121 }
John Reckb8b2af82011-05-20 15:58:33 -0700122 updateUrlIcon();
Michael Kolb5a72f182011-01-13 20:35:06 -0800123 }
124
Michael Kolb31d469b2010-12-09 20:49:54 -0800125 @Override
126 public void onFocusChange(View view, boolean hasFocus) {
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800127 // if losing focus and not in touch mode, leave as is
128 if (hasFocus || view.isInTouchMode() || mUrlInput.needsUpdate()) {
129 setFocusState(hasFocus);
130 mUrlContainer.setBackgroundDrawable(hasFocus
131 ? mFocusDrawable : mUnfocusDrawable);
132 }
133 if (hasFocus) {
134 mUrlInput.forceIme();
135 if (mInVoiceMode) {
136 mUrlInput.forceFilter();
137 }
138 } else if (!mUrlInput.needsUpdate()) {
139 mUrlInput.dismissDropDown();
140 mUrlInput.hideIME();
Michael Kolb2a56eca2011-02-25 09:45:06 -0800141 if (mUseQuickControls) {
142 mUi.hideTitleBar();
143 }
Narayan Kamathc93a2a92011-03-15 11:33:52 +0000144
145 if (mUrlInput.getText().length() == 0) {
146 Tab currentTab = mUiController.getTabControl().getCurrentTab();
147 if (currentTab != null) {
148 mUrlInput.setText(currentTab.getUrl(), false);
149 }
150 }
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800151 }
152 mUrlInput.clearNeedsUpdate();
Michael Kolbc7485ae2010-09-03 10:10:58 -0700153 }
Michael Kolbb7b115e2010-09-25 16:59:37 -0700154
John Reck94b7e042011-02-15 15:02:33 -0800155 @Override
Leon Scroggins4cd97792010-12-03 15:31:56 -0500156 public void setCurrentUrlIsBookmark(boolean isBookmark) {
Michael Kolb31d469b2010-12-09 20:49:54 -0800157 mStar.setActivated(isBookmark);
Leon Scroggins4cd97792010-12-03 15:31:56 -0500158 }
159
Patrick Scott92066772011-03-10 08:46:27 -0500160
Michael Kolba2b2ba82010-08-04 17:54:03 -0700161 @Override
162 public void onClick(View v) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800163 if (mBackButton == v) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700164 mUiController.getCurrentTopWebView().goBack();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700165 } else if (mForwardButton == v) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700166 mUiController.getCurrentTopWebView().goForward();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700167 } else if (mStar == v) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700168 mUiController.bookmarkCurrentPage(
Leon Scrogginsbdff8a72011-02-11 15:49:04 -0500169 AddBookmarkPage.DEFAULT_FOLDER_ID, true);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700170 } else if (mAllButton == v) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700171 mUiController.bookmarksOrHistoryPicker(false);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700172 } else if (mSearchButton == v) {
Michael Kolb81b6f832010-12-12 12:44:27 -0800173 mUi.editUrl(true);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700174 } else if (mStopButton == v) {
175 stopOrRefresh();
Michael Kolb513286f2010-09-09 12:55:12 -0700176 } else if (mGoButton == v) {
Michael Kolb31d469b2010-12-09 20:49:54 -0800177 if (!TextUtils.isEmpty(mUrlInput.getText())) {
178 onAction(mUrlInput.getText().toString(), null,
Michael Kolb257cc2c2010-12-09 09:45:52 -0800179 UrlInputView.TYPED);
Michael Kolb1ce78132010-09-23 15:50:53 -0700180 }
Michael Kolbb7b115e2010-09-25 16:59:37 -0700181 } else if (mClearButton == v) {
Michael Kolb31d469b2010-12-09 20:49:54 -0800182 clearOrClose();
Michael Kolbcfa3af52010-12-14 10:36:11 -0800183 } else if (mVoiceSearch == v) {
184 mUiController.startVoiceSearch();
Michael Kolb11d19782011-03-20 10:17:40 -0700185 } else {
186 super.onClick(v);
Michael Kolbfe251992010-07-08 15:41:55 -0700187 }
188 }
189
Michael Kolba2b2ba82010-08-04 17:54:03 -0700190 @Override
191 void setFavicon(Bitmap icon) { }
Michael Kolbfe251992010-07-08 15:41:55 -0700192
Michael Kolb31d469b2010-12-09 20:49:54 -0800193 private void clearOrClose() {
Narayan Kamath5119edd2011-02-23 15:49:17 +0000194 if (TextUtils.isEmpty(mUrlInput.getUserText())) {
Michael Kolb31d469b2010-12-09 20:49:54 -0800195 // close
Michael Kolb7cdc4902011-02-03 17:54:40 -0800196 mUrlInput.clearFocus();
Michael Kolb31d469b2010-12-09 20:49:54 -0800197 } else {
198 // clear
199 mUrlInput.setText("");
200 }
201 }
202
John Reckb8b2af82011-05-20 15:58:33 -0700203 void updateUrlIcon() {
John Reck46500332011-06-07 14:36:10 -0700204 mUrlIcon.setImageResource(mInVoiceMode ?
205 R.drawable.ic_search_holo_dark
206 : R.drawable.ic_web_holo_dark);
John Reckb8b2af82011-05-20 15:58:33 -0700207 }
208
Michael Kolb11d19782011-03-20 10:17:40 -0700209 @Override
210 protected void setFocusState(boolean focus) {
211 super.setFocusState(focus);
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800212 if (focus) {
Michael Kolbb7b115e2010-09-25 16:59:37 -0700213 mSearchButton.setVisibility(View.GONE);
Michael Kolb31d469b2010-12-09 20:49:54 -0800214 mStar.setVisibility(View.GONE);
215 mClearButton.setVisibility(View.VISIBLE);
Michael Kolbe3524d82011-03-02 14:53:15 -0800216 mUrlIcon.setImageResource(R.drawable.ic_search_holo_dark);
Michael Kolb60a68f52011-02-24 11:02:52 -0800217 updateSearchMode(false);
Michael Kolbb7b115e2010-09-25 16:59:37 -0700218 } else {
Michael Kolbb7b115e2010-09-25 16:59:37 -0700219 mGoButton.setVisibility(View.GONE);
Michael Kolb31d469b2010-12-09 20:49:54 -0800220 mVoiceSearch.setVisibility(View.GONE);
221 mStar.setVisibility(View.VISIBLE);
222 mClearButton.setVisibility(View.GONE);
Michael Kolb376b5412010-12-15 11:52:57 -0800223 if (mUseQuickControls) {
224 mSearchButton.setVisibility(View.GONE);
225 } else {
226 mSearchButton.setVisibility(View.VISIBLE);
227 }
John Reckb8b2af82011-05-20 15:58:33 -0700228 updateUrlIcon();
Michael Kolbb7b115e2010-09-25 16:59:37 -0700229 }
230 }
231
Michael Kolba2b2ba82010-08-04 17:54:03 -0700232 private void stopOrRefresh() {
233 if (mInLoad) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700234 mUiController.stopLoading();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700235 } else {
Michael Kolb8233fac2010-10-26 16:08:53 -0700236 mUiController.getCurrentTopWebView().reload();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700237 }
Leon Scroggins571b3762010-05-26 10:25:01 -0400238 }
239
Michael Kolbfe251992010-07-08 15:41:55 -0700240 @Override
Michael Kolb46f987e2011-04-05 10:39:10 -0700241 protected void onProgressStarted() {
242 mStopButton.setImageDrawable(mStopDrawable);
243 }
244
245 @Override
246 protected void onProgressStopped() {
247 mStopButton.setImageDrawable(mReloadDrawable);
Michael Kolbfe251992010-07-08 15:41:55 -0700248 }
249
Michael Kolb11d19782011-03-20 10:17:40 -0700250 @Override
251 protected void updateSearchMode(boolean userEdited) {
Narayan Kamath5119edd2011-02-23 15:49:17 +0000252 setSearchMode(!userEdited || TextUtils.isEmpty(mUrlInput.getUserText()));
Michael Kolb31d469b2010-12-09 20:49:54 -0800253 }
254
Michael Kolb11d19782011-03-20 10:17:40 -0700255 @Override
256 protected void setSearchMode(boolean voiceSearchEnabled) {
Michael Kolb793e05e2011-01-11 15:17:31 -0800257 boolean showvoicebutton = voiceSearchEnabled &&
Michael Kolb736990c2011-03-20 10:01:20 -0700258 mUiController.supportsVoiceSearch();
Michael Kolb793e05e2011-01-11 15:17:31 -0800259 mVoiceSearch.setVisibility(showvoicebutton ? View.VISIBLE :
Michael Kolb31d469b2010-12-09 20:49:54 -0800260 View.GONE);
261 mGoButton.setVisibility(voiceSearchEnabled ? View.GONE :
262 View.VISIBLE);
263 }
264
Michael Kolbfe251992010-07-08 15:41:55 -0700265 @Override
Michael Kolbcfa3af52010-12-14 10:36:11 -0800266 public void setInVoiceMode(boolean voicemode, List<String> voiceResults) {
Michael Kolb11d19782011-03-20 10:17:40 -0700267 super.setInVoiceMode(voicemode, voiceResults);
Michael Kolb3aaef252011-03-09 18:13:56 -0800268 if (voicemode) {
269 mUrlIcon.setImageDrawable(mSearchButton.getDrawable());
270 }
Michael Kolbcfa3af52010-12-14 10:36:11 -0800271 }
272
John Reck117f07d2011-01-24 09:39:03 -0800273 @Override
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800274 public View focusSearch(View focused, int dir) {
275 if (FOCUS_DOWN == dir && hasFocus()) {
276 return getCurrentWebView();
277 }
278 return super.focusSearch(focused, dir);
279 }
280
Leon Scroggins571b3762010-05-26 10:25:01 -0400281}