blob: 3d4d2ec4a785564b66d87277fd137a9a62879f14 [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
Leon Scroggins4cd97792010-12-03 15:31:56 -0500126 public void setCurrentUrlIsBookmark(boolean isBookmark) {
Michael Kolb31d469b2010-12-09 20:49:54 -0800127 mStar.setActivated(isBookmark);
Leon Scroggins4cd97792010-12-03 15:31:56 -0500128 }
129
Patrick Scott92066772011-03-10 08:46:27 -0500130
Michael Kolba2b2ba82010-08-04 17:54:03 -0700131 @Override
132 public void onClick(View v) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800133 if (mBackButton == v) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700134 mUiController.getCurrentTopWebView().goBack();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700135 } else if (mForwardButton == v) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700136 mUiController.getCurrentTopWebView().goForward();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700137 } else if (mStar == v) {
John Reck3ffc5ca2011-06-10 15:56:06 -0700138 mUiController.bookmarkCurrentPage(true);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700139 } else if (mAllButton == v) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700140 mUiController.bookmarksOrHistoryPicker(false);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700141 } else if (mSearchButton == v) {
Michael Kolb81b6f832010-12-12 12:44:27 -0800142 mUi.editUrl(true);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700143 } else if (mStopButton == v) {
144 stopOrRefresh();
Michael Kolb513286f2010-09-09 12:55:12 -0700145 } else if (mGoButton == v) {
Michael Kolb31d469b2010-12-09 20:49:54 -0800146 if (!TextUtils.isEmpty(mUrlInput.getText())) {
147 onAction(mUrlInput.getText().toString(), null,
Michael Kolb257cc2c2010-12-09 09:45:52 -0800148 UrlInputView.TYPED);
Michael Kolb1ce78132010-09-23 15:50:53 -0700149 }
Michael Kolbb7b115e2010-09-25 16:59:37 -0700150 } else if (mClearButton == v) {
Michael Kolb31d469b2010-12-09 20:49:54 -0800151 clearOrClose();
Michael Kolbcfa3af52010-12-14 10:36:11 -0800152 } else if (mVoiceSearch == v) {
153 mUiController.startVoiceSearch();
Michael Kolb11d19782011-03-20 10:17:40 -0700154 } else {
155 super.onClick(v);
Michael Kolbfe251992010-07-08 15:41:55 -0700156 }
157 }
158
Michael Kolba2b2ba82010-08-04 17:54:03 -0700159 @Override
160 void setFavicon(Bitmap icon) { }
Michael Kolbfe251992010-07-08 15:41:55 -0700161
Michael Kolb31d469b2010-12-09 20:49:54 -0800162 private void clearOrClose() {
Narayan Kamath5119edd2011-02-23 15:49:17 +0000163 if (TextUtils.isEmpty(mUrlInput.getUserText())) {
Michael Kolb31d469b2010-12-09 20:49:54 -0800164 // close
Michael Kolb7cdc4902011-02-03 17:54:40 -0800165 mUrlInput.clearFocus();
Michael Kolb31d469b2010-12-09 20:49:54 -0800166 } else {
167 // clear
168 mUrlInput.setText("");
169 }
170 }
171
John Reckb8b2af82011-05-20 15:58:33 -0700172 void updateUrlIcon() {
John Reck46500332011-06-07 14:36:10 -0700173 mUrlIcon.setImageResource(mInVoiceMode ?
174 R.drawable.ic_search_holo_dark
175 : R.drawable.ic_web_holo_dark);
John Reckb8b2af82011-05-20 15:58:33 -0700176 }
177
Michael Kolb11d19782011-03-20 10:17:40 -0700178 @Override
179 protected void setFocusState(boolean focus) {
180 super.setFocusState(focus);
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800181 if (focus) {
Michael Kolbb7b115e2010-09-25 16:59:37 -0700182 mSearchButton.setVisibility(View.GONE);
Michael Kolb31d469b2010-12-09 20:49:54 -0800183 mStar.setVisibility(View.GONE);
184 mClearButton.setVisibility(View.VISIBLE);
Michael Kolbe3524d82011-03-02 14:53:15 -0800185 mUrlIcon.setImageResource(R.drawable.ic_search_holo_dark);
Michael Kolb60a68f52011-02-24 11:02:52 -0800186 updateSearchMode(false);
Michael Kolbb7b115e2010-09-25 16:59:37 -0700187 } else {
Michael Kolbb7b115e2010-09-25 16:59:37 -0700188 mGoButton.setVisibility(View.GONE);
Michael Kolb31d469b2010-12-09 20:49:54 -0800189 mVoiceSearch.setVisibility(View.GONE);
190 mStar.setVisibility(View.VISIBLE);
191 mClearButton.setVisibility(View.GONE);
Michael Kolb376b5412010-12-15 11:52:57 -0800192 if (mUseQuickControls) {
193 mSearchButton.setVisibility(View.GONE);
194 } else {
195 mSearchButton.setVisibility(View.VISIBLE);
196 }
John Reckb8b2af82011-05-20 15:58:33 -0700197 updateUrlIcon();
Michael Kolbb7b115e2010-09-25 16:59:37 -0700198 }
John Reck7e5b8b52011-06-22 13:46:25 -0700199 mUrlContainer.setBackgroundDrawable(focus
200 ? mFocusDrawable : mUnfocusDrawable);
Michael Kolbb7b115e2010-09-25 16:59:37 -0700201 }
202
Michael Kolba2b2ba82010-08-04 17:54:03 -0700203 private void stopOrRefresh() {
204 if (mInLoad) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700205 mUiController.stopLoading();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700206 } else {
Michael Kolb8233fac2010-10-26 16:08:53 -0700207 mUiController.getCurrentTopWebView().reload();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700208 }
Leon Scroggins571b3762010-05-26 10:25:01 -0400209 }
210
Michael Kolbfe251992010-07-08 15:41:55 -0700211 @Override
Michael Kolb46f987e2011-04-05 10:39:10 -0700212 protected void onProgressStarted() {
213 mStopButton.setImageDrawable(mStopDrawable);
214 }
215
216 @Override
217 protected void onProgressStopped() {
218 mStopButton.setImageDrawable(mReloadDrawable);
Michael Kolbfe251992010-07-08 15:41:55 -0700219 }
220
Michael Kolb11d19782011-03-20 10:17:40 -0700221 @Override
222 protected void updateSearchMode(boolean userEdited) {
Narayan Kamath5119edd2011-02-23 15:49:17 +0000223 setSearchMode(!userEdited || TextUtils.isEmpty(mUrlInput.getUserText()));
Michael Kolb31d469b2010-12-09 20:49:54 -0800224 }
225
Michael Kolb11d19782011-03-20 10:17:40 -0700226 @Override
227 protected void setSearchMode(boolean voiceSearchEnabled) {
Michael Kolb793e05e2011-01-11 15:17:31 -0800228 boolean showvoicebutton = voiceSearchEnabled &&
Michael Kolb736990c2011-03-20 10:01:20 -0700229 mUiController.supportsVoiceSearch();
Michael Kolb793e05e2011-01-11 15:17:31 -0800230 mVoiceSearch.setVisibility(showvoicebutton ? View.VISIBLE :
Michael Kolb31d469b2010-12-09 20:49:54 -0800231 View.GONE);
232 mGoButton.setVisibility(voiceSearchEnabled ? View.GONE :
233 View.VISIBLE);
234 }
235
Michael Kolbfe251992010-07-08 15:41:55 -0700236 @Override
Michael Kolbcfa3af52010-12-14 10:36:11 -0800237 public void setInVoiceMode(boolean voicemode, List<String> voiceResults) {
Michael Kolb11d19782011-03-20 10:17:40 -0700238 super.setInVoiceMode(voicemode, voiceResults);
Michael Kolb3aaef252011-03-09 18:13:56 -0800239 if (voicemode) {
240 mUrlIcon.setImageDrawable(mSearchButton.getDrawable());
241 }
Michael Kolbcfa3af52010-12-14 10:36:11 -0800242 }
243
John Reck117f07d2011-01-24 09:39:03 -0800244 @Override
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800245 public View focusSearch(View focused, int dir) {
246 if (FOCUS_DOWN == dir && hasFocus()) {
247 return getCurrentWebView();
248 }
249 return super.focusSearch(focused, dir);
250 }
251
Leon Scroggins571b3762010-05-26 10:25:01 -0400252}