blob: 5890810830fe57691c706e2cb887ff74be6e5c2d [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 Kolbc7485ae2010-09-03 10:10:58 -070019import com.android.browser.UrlInputView.UrlInputListener;
20
Michael Kolb8233fac2010-10-26 16:08:53 -070021import android.app.Activity;
Michael Kolbfe251992010-07-08 15:41:55 -070022import android.app.SearchManager;
Leon Scroggins571b3762010-05-26 10:25:01 -040023import android.content.Context;
Michael Kolbfe251992010-07-08 15:41:55 -070024import android.content.Intent;
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;
Michael Kolb257cc2c2010-12-09 09:45:52 -080028import android.os.Bundle;
Michael Kolb31d469b2010-12-09 20:49:54 -080029import android.text.Editable;
Michael Kolb1ce78132010-09-23 15:50:53 -070030import android.text.TextUtils;
Michael Kolb31d469b2010-12-09 20:49:54 -080031import android.text.TextWatcher;
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 Kolbb6bc32c2010-12-10 11:51:54 -080036import android.webkit.WebView;
Leon Scroggins571b3762010-05-26 10:25:01 -040037import android.widget.ImageView;
Leon Scroggins571b3762010-05-26 10:25:01 -040038
Leon Scroggins571b3762010-05-26 10:25:01 -040039/**
Michael Kolbfe251992010-07-08 15:41:55 -070040 * tabbed title bar for xlarge screen browser
Leon Scroggins571b3762010-05-26 10:25:01 -040041 */
Michael Kolbfe251992010-07-08 15:41:55 -070042public class TitleBarXLarge extends TitleBarBase
Michael Kolb31d469b2010-12-09 20:49:54 -080043 implements UrlInputListener, OnClickListener, OnFocusChangeListener,
44 TextWatcher {
Leon Scroggins571b3762010-05-26 10:25:01 -040045
Michael Kolbfe251992010-07-08 15:41:55 -070046 private static final int PROGRESS_MAX = 100;
Leon Scroggins571b3762010-05-26 10:25:01 -040047
Michael Kolb8233fac2010-10-26 16:08:53 -070048 private UiController mUiController;
Michael Kolb66706532010-12-12 19:50:22 -080049 private XLargeUi mUi;
Michael Kolb8233fac2010-10-26 16:08:53 -070050
Michael Kolba2b2ba82010-08-04 17:54:03 -070051 private Drawable mStopDrawable;
52 private Drawable mReloadDrawable;
Michael Kolbc7485ae2010-09-03 10:10:58 -070053
Michael Kolb3f65c382010-08-20 15:31:16 -070054 private View mContainer;
Michael Kolba2b2ba82010-08-04 17:54:03 -070055 private View mBackButton;
56 private View mForwardButton;
Michael Kolb31d469b2010-12-09 20:49:54 -080057 private ImageView mStar;
Michael Kolba2b2ba82010-08-04 17:54:03 -070058 private View mSearchButton;
Michael Kolb31d469b2010-12-09 20:49:54 -080059 private View mUrlContainer;
Michael Kolb513286f2010-09-09 12:55:12 -070060 private View mGoButton;
Michael Kolba2b2ba82010-08-04 17:54:03 -070061 private ImageView mStopButton;
Michael Kolba2b2ba82010-08-04 17:54:03 -070062 private View mAllButton;
Michael Kolbb7b115e2010-09-25 16:59:37 -070063 private View mClearButton;
Michael Kolb31d469b2010-12-09 20:49:54 -080064 private View mVoiceSearch;
Michael Kolbc7485ae2010-09-03 10:10:58 -070065 private PageProgressView mProgressView;
Michael Kolb31d469b2010-12-09 20:49:54 -080066 private UrlInputView mUrlInput;
Michael Kolb81b6f832010-12-12 12:44:27 -080067
Michael Kolba2b2ba82010-08-04 17:54:03 -070068 private boolean mInLoad;
Michael Kolbfe251992010-07-08 15:41:55 -070069
Michael Kolb81b6f832010-12-12 12:44:27 -080070 public TitleBarXLarge(Activity activity, UiController controller,
Michael Kolb66706532010-12-12 19:50:22 -080071 XLargeUi ui) {
Michael Kolb8233fac2010-10-26 16:08:53 -070072 super(activity);
Michael Kolb8233fac2010-10-26 16:08:53 -070073 mUiController = controller;
Michael Kolb81b6f832010-12-12 12:44:27 -080074 mUi = ui;
Michael Kolb8233fac2010-10-26 16:08:53 -070075 Resources resources = activity.getResources();
Michael Kolbc7485ae2010-09-03 10:10:58 -070076 mStopDrawable = resources.getDrawable(R.drawable.ic_stop_normal);
77 mReloadDrawable = resources.getDrawable(R.drawable.ic_refresh_normal);
Michael Kolb8233fac2010-10-26 16:08:53 -070078 rebuildLayout(activity, true);
Michael Kolbfe251992010-07-08 15:41:55 -070079 }
Leon Scroggins571b3762010-05-26 10:25:01 -040080
Michael Kolbfe251992010-07-08 15:41:55 -070081 private void rebuildLayout(Context context, boolean rebuildData) {
Michael Kolbfe251992010-07-08 15:41:55 -070082 LayoutInflater factory = LayoutInflater.from(context);
Michael Kolba2b2ba82010-08-04 17:54:03 -070083 factory.inflate(R.layout.url_bar, this);
Michael Kolbfe251992010-07-08 15:41:55 -070084
Michael Kolb3f65c382010-08-20 15:31:16 -070085 mContainer = findViewById(R.id.taburlbar);
Michael Kolb31d469b2010-12-09 20:49:54 -080086 mUrlInput = (UrlInputView) findViewById(R.id.url_focused);
Michael Kolbfe251992010-07-08 15:41:55 -070087 mAllButton = findViewById(R.id.all_btn);
88 // TODO: Change enabled states based on whether you can go
Leon Scroggins571b3762010-05-26 10:25:01 -040089 // back/forward. Probably should be done inside onPageStarted.
90 mBackButton = findViewById(R.id.back);
91 mForwardButton = findViewById(R.id.forward);
Michael Kolb31d469b2010-12-09 20:49:54 -080092 mStar = (ImageView) findViewById(R.id.star);
Michael Kolba2b2ba82010-08-04 17:54:03 -070093 mStopButton = (ImageView) findViewById(R.id.stop);
94 mSearchButton = findViewById(R.id.search);
95 mLockIcon = (ImageView) findViewById(R.id.lock);
Michael Kolb513286f2010-09-09 12:55:12 -070096 mGoButton = findViewById(R.id.go);
Michael Kolbb7b115e2010-09-25 16:59:37 -070097 mClearButton = findViewById(R.id.clear);
Michael Kolb31d469b2010-12-09 20:49:54 -080098 mVoiceSearch = findViewById(R.id.voicesearch);
Michael Kolbc7485ae2010-09-03 10:10:58 -070099 mProgressView = (PageProgressView) findViewById(R.id.progress);
Michael Kolb31d469b2010-12-09 20:49:54 -0800100 mUrlContainer = findViewById(R.id.urlbar_focused);
Michael Kolb81b6f832010-12-12 12:44:27 -0800101
Michael Kolba2b2ba82010-08-04 17:54:03 -0700102 mBackButton.setOnClickListener(this);
103 mForwardButton.setOnClickListener(this);
104 mStar.setOnClickListener(this);
105 mAllButton.setOnClickListener(this);
106 mStopButton.setOnClickListener(this);
107 mSearchButton.setOnClickListener(this);
Michael Kolb513286f2010-09-09 12:55:12 -0700108 mGoButton.setOnClickListener(this);
Michael Kolbb7b115e2010-09-25 16:59:37 -0700109 mClearButton.setOnClickListener(this);
Michael Kolb31d469b2010-12-09 20:49:54 -0800110 mUrlContainer.setOnClickListener(this);
111 mUrlInput.setUrlInputListener(this);
112 mUrlInput.setContainer(mUrlContainer);
113 mUrlInput.setController(mUiController);
114 mUrlInput.setOnFocusChangeListener(this);
115 mUrlInput.setSelectAllOnFocus(true);
116 mUrlInput.addTextChangedListener(this);
117 setUrlMode(false);
118 }
119
120 @Override
121 public void onFocusChange(View view, boolean hasFocus) {
122 setUrlMode(hasFocus);
Michael Kolbc7485ae2010-09-03 10:10:58 -0700123 }
Michael Kolbb7b115e2010-09-25 16:59:37 -0700124
Leon Scroggins4cd97792010-12-03 15:31:56 -0500125 public void setCurrentUrlIsBookmark(boolean isBookmark) {
Michael Kolb31d469b2010-12-09 20:49:54 -0800126 mStar.setActivated(isBookmark);
Leon Scroggins4cd97792010-12-03 15:31:56 -0500127 }
128
Michael Kolb81b6f832010-12-12 12:44:27 -0800129 /**
130 * called from the Ui when the user wants to edit
131 * Note: only the fake titlebar will get this callback
132 * independent of which input field started the edit mode
133 * @param clearInput clear the input field
134 */
135 void onEditUrl(boolean clearInput) {
136 mUrlInput.requestFocusFromTouch();
137 if (clearInput) {
138 mUrlInput.setText("");
139 }
140 }
141
142 boolean isEditingUrl() {
143 return mUrlInput.hasFocus();
144 }
145
Michael Kolba2b2ba82010-08-04 17:54:03 -0700146 @Override
147 public void onClick(View v) {
Michael Kolb81b6f832010-12-12 12:44:27 -0800148 if (mUrlInput == v) {
149 if (!mUrlInput.hasFocus()) {
150 mUi.editUrl(false);
151 }
152 } else if (mBackButton == v) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700153 mUiController.getCurrentTopWebView().goBack();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700154 } else if (mForwardButton == v) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700155 mUiController.getCurrentTopWebView().goForward();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700156 } else if (mStar == v) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700157 mUiController.bookmarkCurrentPage(
Leon Scroggins88d08032010-10-21 15:17:10 -0400158 AddBookmarkPage.DEFAULT_FOLDER_ID);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700159 } else if (mAllButton == v) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700160 mUiController.bookmarksOrHistoryPicker(false);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700161 } else if (mSearchButton == v) {
Michael Kolb81b6f832010-12-12 12:44:27 -0800162 mUi.editUrl(true);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700163 } else if (mStopButton == v) {
164 stopOrRefresh();
Michael Kolb513286f2010-09-09 12:55:12 -0700165 } else if (mGoButton == v) {
Michael Kolb31d469b2010-12-09 20:49:54 -0800166 if (!TextUtils.isEmpty(mUrlInput.getText())) {
167 onAction(mUrlInput.getText().toString(), null,
Michael Kolb257cc2c2010-12-09 09:45:52 -0800168 UrlInputView.TYPED);
Michael Kolb1ce78132010-09-23 15:50:53 -0700169 }
Michael Kolbb7b115e2010-09-25 16:59:37 -0700170 } else if (mClearButton == v) {
Michael Kolb31d469b2010-12-09 20:49:54 -0800171 clearOrClose();
Michael Kolbfe251992010-07-08 15:41:55 -0700172 }
173 }
174
Michael Kolb3f65c382010-08-20 15:31:16 -0700175 int getHeightWithoutProgress() {
176 return mContainer.getHeight();
177 }
178
Michael Kolba2b2ba82010-08-04 17:54:03 -0700179 @Override
180 void setFavicon(Bitmap icon) { }
Michael Kolbfe251992010-07-08 15:41:55 -0700181
Michael Kolb31d469b2010-12-09 20:49:54 -0800182 private void clearOrClose() {
183 if (TextUtils.isEmpty(mUrlInput.getText())) {
184 // close
185 setUrlMode(false);
186 } else {
187 // clear
188 mUrlInput.setText("");
189 }
190 }
191
Michael Kolbfe251992010-07-08 15:41:55 -0700192 // UrlInputListener implementation
193
Michael Kolb81b6f832010-12-12 12:44:27 -0800194 /**
195 * callback from suggestion dropdown
196 * user selected a suggestion
197 */
Michael Kolbfe251992010-07-08 15:41:55 -0700198 @Override
Michael Kolb257cc2c2010-12-09 09:45:52 -0800199 public void onAction(String text, String extra, String source) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700200 mUiController.getCurrentTopWebView().requestFocus();
Michael Kolb66706532010-12-12 19:50:22 -0800201 mUi.hideFakeTitleBar();
Michael Kolbfe251992010-07-08 15:41:55 -0700202 Intent i = new Intent();
203 i.setAction(Intent.ACTION_SEARCH);
204 i.putExtra(SearchManager.QUERY, text);
John Reck40f720e2010-11-10 11:57:04 -0800205 if (extra != null) {
206 i.putExtra(SearchManager.EXTRA_DATA_KEY, extra);
207 }
Michael Kolb257cc2c2010-12-09 09:45:52 -0800208 if (source != null) {
209 Bundle appData = new Bundle();
210 appData.putString(com.android.common.Search.SOURCE, source);
211 i.putExtra(SearchManager.APP_DATA, appData);
212 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700213 mUiController.handleNewIntent(i);
Michael Kolbb7b115e2010-09-25 16:59:37 -0700214 setUrlMode(false);
Michael Kolb513286f2010-09-09 12:55:12 -0700215 setDisplayTitle(text);
Michael Kolbfe251992010-07-08 15:41:55 -0700216 }
217
218 @Override
219 public void onDismiss() {
Michael Kolbb6bc32c2010-12-10 11:51:54 -0800220 WebView top = mUiController.getCurrentTopWebView();
221 if (top != null) {
222 mUiController.getCurrentTopWebView().requestFocus();
223 }
Michael Kolb66706532010-12-12 19:50:22 -0800224 mUi.hideFakeTitleBar();
Michael Kolbb7b115e2010-09-25 16:59:37 -0700225 setUrlMode(false);
Michael Kolbb6bc32c2010-12-10 11:51:54 -0800226 // if top != null current must be set
227 if (top != null) {
228 setDisplayTitle(mUiController.getCurrentWebView().getUrl());
229 }
Michael Kolb513286f2010-09-09 12:55:12 -0700230 }
231
Michael Kolb81b6f832010-12-12 12:44:27 -0800232 /**
233 * callback from the suggestion dropdown
234 * copy text to input field and stay in edit mode
235 */
Michael Kolb513286f2010-09-09 12:55:12 -0700236 @Override
237 public void onEdit(String text) {
Michael Kolb7b20ddd2010-10-14 15:03:28 -0700238 setDisplayTitle(text, true);
Michael Kolb513286f2010-09-09 12:55:12 -0700239 if (text != null) {
Michael Kolb31d469b2010-12-09 20:49:54 -0800240 mUrlInput.setSelection(text.length());
Michael Kolb513286f2010-09-09 12:55:12 -0700241 }
242 }
243
Michael Kolb81b6f832010-12-12 12:44:27 -0800244 void setUrlMode(boolean focused) {
Michael Kolbb7b115e2010-09-25 16:59:37 -0700245 if (focused) {
Michael Kolb31d469b2010-12-09 20:49:54 -0800246 mUrlInput.setDropDownWidth(mUrlContainer.getWidth());
247 mUrlInput.setDropDownHorizontalOffset(-mUrlInput.getLeft());
Michael Kolbb7b115e2010-09-25 16:59:37 -0700248 mSearchButton.setVisibility(View.GONE);
Michael Kolb31d469b2010-12-09 20:49:54 -0800249 mStar.setVisibility(View.GONE);
250 mClearButton.setVisibility(View.VISIBLE);
251 updateSearchMode();
Michael Kolbb7b115e2010-09-25 16:59:37 -0700252 } else {
Michael Kolb31d469b2010-12-09 20:49:54 -0800253 mUrlInput.clearFocus();
Michael Kolbb7b115e2010-09-25 16:59:37 -0700254 mSearchButton.setVisibility(View.VISIBLE);
255 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 Kolbb7b115e2010-09-25 16:59:37 -0700259 }
260 }
261
Michael Kolba2b2ba82010-08-04 17:54:03 -0700262 private void stopOrRefresh() {
263 if (mInLoad) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700264 mUiController.stopLoading();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700265 } else {
Michael Kolb8233fac2010-10-26 16:08:53 -0700266 mUiController.getCurrentTopWebView().reload();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700267 }
Leon Scroggins571b3762010-05-26 10:25:01 -0400268 }
269
270 /**
Michael Kolbfe251992010-07-08 15:41:55 -0700271 * Update the progress, from 0 to 100.
Leon Scroggins571b3762010-05-26 10:25:01 -0400272 */
Michael Kolbfe251992010-07-08 15:41:55 -0700273 @Override
Michael Kolba2b2ba82010-08-04 17:54:03 -0700274 void setProgress(int newProgress) {
275 if (newProgress >= PROGRESS_MAX) {
Michael Kolbb7b115e2010-09-25 16:59:37 -0700276 mProgressView.setProgress(PageProgressView.MAX_PROGRESS);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700277 mProgressView.setVisibility(View.GONE);
278 mInLoad = false;
279 mStopButton.setImageDrawable(mReloadDrawable);
280 } else {
281 if (!mInLoad) {
282 mProgressView.setVisibility(View.VISIBLE);
283 mInLoad = true;
284 mStopButton.setImageDrawable(mStopDrawable);
285 }
Michael Kolbb7b115e2010-09-25 16:59:37 -0700286 mProgressView.setProgress(newProgress * PageProgressView.MAX_PROGRESS
287 / PROGRESS_MAX);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700288 }
Michael Kolbfe251992010-07-08 15:41:55 -0700289 }
290
Michael Kolb31d469b2010-12-09 20:49:54 -0800291 private void updateSearchMode() {
292 setSearchMode(TextUtils.isEmpty(mUrlInput.getText()));
293 }
294
295 private void setSearchMode(boolean voiceSearchEnabled) {
296 mVoiceSearch.setVisibility(voiceSearchEnabled ? View.VISIBLE :
297 View.GONE);
298 mGoButton.setVisibility(voiceSearchEnabled ? View.GONE :
299 View.VISIBLE);
300 }
301
Michael Kolbfe251992010-07-08 15:41:55 -0700302 @Override
Leon Scroggins571b3762010-05-26 10:25:01 -0400303 /* package */ void setDisplayTitle(String title) {
Michael Kolb31d469b2010-12-09 20:49:54 -0800304 mUrlInput.setText(title, false);
Leon Scroggins571b3762010-05-26 10:25:01 -0400305 }
306
Michael Kolb7b20ddd2010-10-14 15:03:28 -0700307 void setDisplayTitle(String title, boolean filter) {
Michael Kolb31d469b2010-12-09 20:49:54 -0800308 mUrlInput.setText(title, filter);
Michael Kolb7b20ddd2010-10-14 15:03:28 -0700309 }
310
Michael Kolb31d469b2010-12-09 20:49:54 -0800311 // UrlInput text watcher
Leon Scroggins4cd97792010-12-03 15:31:56 -0500312
Michael Kolb31d469b2010-12-09 20:49:54 -0800313 @Override
314 public void afterTextChanged(Editable s) {
315 if (mUrlInput.hasFocus()) {
Michael Kolb81b6f832010-12-12 12:44:27 -0800316 // check if input field is empty and adjust voice search state
Michael Kolb31d469b2010-12-09 20:49:54 -0800317 updateSearchMode();
Leon Scroggins4cd97792010-12-03 15:31:56 -0500318 }
319 }
Michael Kolb31d469b2010-12-09 20:49:54 -0800320
321 @Override
322 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
323 }
324
325 @Override
326 public void onTextChanged(CharSequence s, int start, int before, int count) {
327 }
328
Leon Scroggins571b3762010-05-26 10:25:01 -0400329}