blob: 99e86c464c3e4bf3404feda075094e9dfde08286 [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;
49
Michael Kolba2b2ba82010-08-04 17:54:03 -070050 private Drawable mStopDrawable;
51 private Drawable mReloadDrawable;
Michael Kolbc7485ae2010-09-03 10:10:58 -070052
Michael Kolb3f65c382010-08-20 15:31:16 -070053 private View mContainer;
Michael Kolba2b2ba82010-08-04 17:54:03 -070054 private View mBackButton;
55 private View mForwardButton;
Michael Kolb31d469b2010-12-09 20:49:54 -080056 private ImageView mStar;
Michael Kolba2b2ba82010-08-04 17:54:03 -070057 private View mSearchButton;
Michael Kolb31d469b2010-12-09 20:49:54 -080058 private View mUrlContainer;
Michael Kolb513286f2010-09-09 12:55:12 -070059 private View mGoButton;
Michael Kolba2b2ba82010-08-04 17:54:03 -070060 private ImageView mStopButton;
Michael Kolba2b2ba82010-08-04 17:54:03 -070061 private View mAllButton;
Michael Kolbb7b115e2010-09-25 16:59:37 -070062 private View mClearButton;
Michael Kolb31d469b2010-12-09 20:49:54 -080063 private View mVoiceSearch;
Michael Kolbc7485ae2010-09-03 10:10:58 -070064 private PageProgressView mProgressView;
Michael Kolb31d469b2010-12-09 20:49:54 -080065 private UrlInputView mUrlInput;
Michael Kolba2b2ba82010-08-04 17:54:03 -070066 private boolean mInLoad;
Michael Kolbfe251992010-07-08 15:41:55 -070067
Michael Kolb8233fac2010-10-26 16:08:53 -070068 public TitleBarXLarge(Activity activity, UiController controller) {
69 super(activity);
Michael Kolb8233fac2010-10-26 16:08:53 -070070 mUiController = controller;
71 Resources resources = activity.getResources();
Michael Kolbc7485ae2010-09-03 10:10:58 -070072 mStopDrawable = resources.getDrawable(R.drawable.ic_stop_normal);
73 mReloadDrawable = resources.getDrawable(R.drawable.ic_refresh_normal);
Michael Kolb8233fac2010-10-26 16:08:53 -070074 rebuildLayout(activity, true);
Michael Kolbfe251992010-07-08 15:41:55 -070075 }
Leon Scroggins571b3762010-05-26 10:25:01 -040076
Michael Kolbfe251992010-07-08 15:41:55 -070077 private void rebuildLayout(Context context, boolean rebuildData) {
Michael Kolbfe251992010-07-08 15:41:55 -070078 LayoutInflater factory = LayoutInflater.from(context);
Michael Kolba2b2ba82010-08-04 17:54:03 -070079 factory.inflate(R.layout.url_bar, this);
Michael Kolbfe251992010-07-08 15:41:55 -070080
Michael Kolb3f65c382010-08-20 15:31:16 -070081 mContainer = findViewById(R.id.taburlbar);
Michael Kolb31d469b2010-12-09 20:49:54 -080082 mUrlInput = (UrlInputView) findViewById(R.id.url_focused);
Michael Kolbfe251992010-07-08 15:41:55 -070083 mAllButton = findViewById(R.id.all_btn);
84 // TODO: Change enabled states based on whether you can go
Leon Scroggins571b3762010-05-26 10:25:01 -040085 // back/forward. Probably should be done inside onPageStarted.
86 mBackButton = findViewById(R.id.back);
87 mForwardButton = findViewById(R.id.forward);
Michael Kolb31d469b2010-12-09 20:49:54 -080088 mStar = (ImageView) findViewById(R.id.star);
Michael Kolba2b2ba82010-08-04 17:54:03 -070089 mStopButton = (ImageView) findViewById(R.id.stop);
90 mSearchButton = findViewById(R.id.search);
91 mLockIcon = (ImageView) findViewById(R.id.lock);
Michael Kolb513286f2010-09-09 12:55:12 -070092 mGoButton = findViewById(R.id.go);
Michael Kolbb7b115e2010-09-25 16:59:37 -070093 mClearButton = findViewById(R.id.clear);
Michael Kolb31d469b2010-12-09 20:49:54 -080094 mVoiceSearch = findViewById(R.id.voicesearch);
Michael Kolbc7485ae2010-09-03 10:10:58 -070095 mProgressView = (PageProgressView) findViewById(R.id.progress);
Michael Kolb31d469b2010-12-09 20:49:54 -080096 mUrlContainer = findViewById(R.id.urlbar_focused);
Michael Kolba2b2ba82010-08-04 17:54:03 -070097 mBackButton.setOnClickListener(this);
98 mForwardButton.setOnClickListener(this);
99 mStar.setOnClickListener(this);
100 mAllButton.setOnClickListener(this);
101 mStopButton.setOnClickListener(this);
102 mSearchButton.setOnClickListener(this);
Michael Kolb513286f2010-09-09 12:55:12 -0700103 mGoButton.setOnClickListener(this);
Michael Kolbb7b115e2010-09-25 16:59:37 -0700104 mClearButton.setOnClickListener(this);
Michael Kolb31d469b2010-12-09 20:49:54 -0800105 mUrlContainer.setOnClickListener(this);
106 mUrlInput.setUrlInputListener(this);
107 mUrlInput.setContainer(mUrlContainer);
108 mUrlInput.setController(mUiController);
109 mUrlInput.setOnFocusChangeListener(this);
110 mUrlInput.setSelectAllOnFocus(true);
111 mUrlInput.addTextChangedListener(this);
112 setUrlMode(false);
113 }
114
115 @Override
116 public void onFocusChange(View view, boolean hasFocus) {
117 setUrlMode(hasFocus);
Michael Kolbc7485ae2010-09-03 10:10:58 -0700118 }
Michael Kolbb7b115e2010-09-25 16:59:37 -0700119
Leon Scroggins4cd97792010-12-03 15:31:56 -0500120 public void setCurrentUrlIsBookmark(boolean isBookmark) {
Michael Kolb31d469b2010-12-09 20:49:54 -0800121 mStar.setActivated(isBookmark);
Leon Scroggins4cd97792010-12-03 15:31:56 -0500122 }
123
Michael Kolba2b2ba82010-08-04 17:54:03 -0700124 @Override
125 public void onClick(View v) {
Michael Kolb31d469b2010-12-09 20:49:54 -0800126 if (mBackButton == v) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700127 mUiController.getCurrentTopWebView().goBack();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700128 } else if (mForwardButton == v) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700129 mUiController.getCurrentTopWebView().goForward();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700130 } else if (mStar == v) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700131 mUiController.bookmarkCurrentPage(
Leon Scroggins88d08032010-10-21 15:17:10 -0400132 AddBookmarkPage.DEFAULT_FOLDER_ID);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700133 } else if (mAllButton == v) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700134 mUiController.bookmarksOrHistoryPicker(false);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700135 } else if (mSearchButton == v) {
136 search();
137 } else if (mStopButton == v) {
138 stopOrRefresh();
Michael Kolb513286f2010-09-09 12:55:12 -0700139 } else if (mGoButton == v) {
Michael Kolb31d469b2010-12-09 20:49:54 -0800140 if (!TextUtils.isEmpty(mUrlInput.getText())) {
141 onAction(mUrlInput.getText().toString(), null,
Michael Kolb257cc2c2010-12-09 09:45:52 -0800142 UrlInputView.TYPED);
Michael Kolb1ce78132010-09-23 15:50:53 -0700143 }
Michael Kolbb7b115e2010-09-25 16:59:37 -0700144 } else if (mClearButton == v) {
Michael Kolb31d469b2010-12-09 20:49:54 -0800145 clearOrClose();
Michael Kolbfe251992010-07-08 15:41:55 -0700146 }
147 }
148
Michael Kolb3f65c382010-08-20 15:31:16 -0700149 int getHeightWithoutProgress() {
150 return mContainer.getHeight();
151 }
152
Michael Kolba2b2ba82010-08-04 17:54:03 -0700153 @Override
154 void setFavicon(Bitmap icon) { }
Michael Kolbfe251992010-07-08 15:41:55 -0700155
Michael Kolb31d469b2010-12-09 20:49:54 -0800156 private void clearOrClose() {
157 if (TextUtils.isEmpty(mUrlInput.getText())) {
158 // close
159 setUrlMode(false);
160 } else {
161 // clear
162 mUrlInput.setText("");
163 }
164 }
165
Michael Kolbfe251992010-07-08 15:41:55 -0700166 // UrlInputListener implementation
167
168 @Override
Michael Kolb257cc2c2010-12-09 09:45:52 -0800169 public void onAction(String text, String extra, String source) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700170 mUiController.getCurrentTopWebView().requestFocus();
171 ((BaseUi) mUiController.getUi()).hideFakeTitleBar();
Michael Kolbfe251992010-07-08 15:41:55 -0700172 Intent i = new Intent();
173 i.setAction(Intent.ACTION_SEARCH);
174 i.putExtra(SearchManager.QUERY, text);
John Reck40f720e2010-11-10 11:57:04 -0800175 if (extra != null) {
176 i.putExtra(SearchManager.EXTRA_DATA_KEY, extra);
177 }
Michael Kolb257cc2c2010-12-09 09:45:52 -0800178 if (source != null) {
179 Bundle appData = new Bundle();
180 appData.putString(com.android.common.Search.SOURCE, source);
181 i.putExtra(SearchManager.APP_DATA, appData);
182 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700183 mUiController.handleNewIntent(i);
Michael Kolbb7b115e2010-09-25 16:59:37 -0700184 setUrlMode(false);
Michael Kolb513286f2010-09-09 12:55:12 -0700185 setDisplayTitle(text);
Michael Kolbfe251992010-07-08 15:41:55 -0700186 }
187
188 @Override
189 public void onDismiss() {
Michael Kolbb6bc32c2010-12-10 11:51:54 -0800190 WebView top = mUiController.getCurrentTopWebView();
191 if (top != null) {
192 mUiController.getCurrentTopWebView().requestFocus();
193 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700194 ((BaseUi) mUiController.getUi()).hideFakeTitleBar();
Michael Kolbb7b115e2010-09-25 16:59:37 -0700195 setUrlMode(false);
Michael Kolbb6bc32c2010-12-10 11:51:54 -0800196 // if top != null current must be set
197 if (top != null) {
198 setDisplayTitle(mUiController.getCurrentWebView().getUrl());
199 }
Michael Kolb513286f2010-09-09 12:55:12 -0700200 }
201
202 @Override
203 public void onEdit(String text) {
Michael Kolb7b20ddd2010-10-14 15:03:28 -0700204 setDisplayTitle(text, true);
Michael Kolb513286f2010-09-09 12:55:12 -0700205 if (text != null) {
Michael Kolb31d469b2010-12-09 20:49:54 -0800206 mUrlInput.setSelection(text.length());
Michael Kolb513286f2010-09-09 12:55:12 -0700207 }
208 }
209
Michael Kolbb7b115e2010-09-25 16:59:37 -0700210 private void setUrlMode(boolean focused) {
Michael Kolbb7b115e2010-09-25 16:59:37 -0700211 if (focused) {
Michael Kolb31d469b2010-12-09 20:49:54 -0800212 mUrlInput.setDropDownWidth(mUrlContainer.getWidth());
213 mUrlInput.setDropDownHorizontalOffset(-mUrlInput.getLeft());
Michael Kolbb7b115e2010-09-25 16:59:37 -0700214 mSearchButton.setVisibility(View.GONE);
Michael Kolb31d469b2010-12-09 20:49:54 -0800215 mStar.setVisibility(View.GONE);
216 mClearButton.setVisibility(View.VISIBLE);
217 updateSearchMode();
Michael Kolbb7b115e2010-09-25 16:59:37 -0700218 } else {
Michael Kolb31d469b2010-12-09 20:49:54 -0800219 mUrlInput.clearFocus();
Michael Kolbb7b115e2010-09-25 16:59:37 -0700220 mSearchButton.setVisibility(View.VISIBLE);
221 mGoButton.setVisibility(View.GONE);
Michael Kolb31d469b2010-12-09 20:49:54 -0800222 mVoiceSearch.setVisibility(View.GONE);
223 mStar.setVisibility(View.VISIBLE);
224 mClearButton.setVisibility(View.GONE);
Michael Kolbb7b115e2010-09-25 16:59:37 -0700225 }
226 }
227
Michael Kolba2b2ba82010-08-04 17:54:03 -0700228 private void search() {
Michael Kolb31d469b2010-12-09 20:49:54 -0800229 mUrlInput.requestFocus();
Michael Kolb513286f2010-09-09 12:55:12 -0700230 setDisplayTitle("");
Michael Kolbffce12c2010-12-06 19:05:25 -0800231 setUrlMode(true);
Michael Kolbfe251992010-07-08 15:41:55 -0700232 }
233
Michael Kolba2b2ba82010-08-04 17:54:03 -0700234 private void stopOrRefresh() {
235 if (mInLoad) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700236 mUiController.stopLoading();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700237 } else {
Michael Kolb8233fac2010-10-26 16:08:53 -0700238 mUiController.getCurrentTopWebView().reload();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700239 }
Leon Scroggins571b3762010-05-26 10:25:01 -0400240 }
241
242 /**
Michael Kolbfe251992010-07-08 15:41:55 -0700243 * Update the progress, from 0 to 100.
Leon Scroggins571b3762010-05-26 10:25:01 -0400244 */
Michael Kolbfe251992010-07-08 15:41:55 -0700245 @Override
Michael Kolba2b2ba82010-08-04 17:54:03 -0700246 void setProgress(int newProgress) {
247 if (newProgress >= PROGRESS_MAX) {
Michael Kolbb7b115e2010-09-25 16:59:37 -0700248 mProgressView.setProgress(PageProgressView.MAX_PROGRESS);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700249 mProgressView.setVisibility(View.GONE);
250 mInLoad = false;
251 mStopButton.setImageDrawable(mReloadDrawable);
252 } else {
253 if (!mInLoad) {
254 mProgressView.setVisibility(View.VISIBLE);
255 mInLoad = true;
256 mStopButton.setImageDrawable(mStopDrawable);
257 }
Michael Kolbb7b115e2010-09-25 16:59:37 -0700258 mProgressView.setProgress(newProgress * PageProgressView.MAX_PROGRESS
259 / PROGRESS_MAX);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700260 }
Michael Kolbfe251992010-07-08 15:41:55 -0700261 }
262
Michael Kolb31d469b2010-12-09 20:49:54 -0800263 private void updateSearchMode() {
264 setSearchMode(TextUtils.isEmpty(mUrlInput.getText()));
265 }
266
267 private void setSearchMode(boolean voiceSearchEnabled) {
268 mVoiceSearch.setVisibility(voiceSearchEnabled ? View.VISIBLE :
269 View.GONE);
270 mGoButton.setVisibility(voiceSearchEnabled ? View.GONE :
271 View.VISIBLE);
272 }
273
Michael Kolbfe251992010-07-08 15:41:55 -0700274 @Override
Leon Scroggins571b3762010-05-26 10:25:01 -0400275 /* package */ void setDisplayTitle(String title) {
Michael Kolb31d469b2010-12-09 20:49:54 -0800276 mUrlInput.setText(title, false);
Leon Scroggins571b3762010-05-26 10:25:01 -0400277 }
278
Michael Kolb7b20ddd2010-10-14 15:03:28 -0700279 void setDisplayTitle(String title, boolean filter) {
Michael Kolb31d469b2010-12-09 20:49:54 -0800280 mUrlInput.setText(title, filter);
Michael Kolb7b20ddd2010-10-14 15:03:28 -0700281 }
282
Michael Kolb31d469b2010-12-09 20:49:54 -0800283 // UrlInput text watcher
Leon Scroggins4cd97792010-12-03 15:31:56 -0500284
Michael Kolb31d469b2010-12-09 20:49:54 -0800285 @Override
286 public void afterTextChanged(Editable s) {
287 if (mUrlInput.hasFocus()) {
288 // check if url input is empty and adjust voice search state
289 updateSearchMode();
Leon Scroggins4cd97792010-12-03 15:31:56 -0500290 }
291 }
Michael Kolb31d469b2010-12-09 20:49:54 -0800292
293 @Override
294 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
295 }
296
297 @Override
298 public void onTextChanged(CharSequence s, int start, int before, int count) {
299 }
300
Leon Scroggins571b3762010-05-26 10:25:01 -0400301}