blob: 5326280f82f9c57acffe1cbc6d465b9fd6745964 [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;
Leon Scroggins571b3762010-05-26 10:25:01 -040036import android.widget.ImageView;
Leon Scroggins571b3762010-05-26 10:25:01 -040037
Leon Scroggins571b3762010-05-26 10:25:01 -040038/**
Michael Kolbfe251992010-07-08 15:41:55 -070039 * tabbed title bar for xlarge screen browser
Leon Scroggins571b3762010-05-26 10:25:01 -040040 */
Michael Kolbfe251992010-07-08 15:41:55 -070041public class TitleBarXLarge extends TitleBarBase
Michael Kolb31d469b2010-12-09 20:49:54 -080042 implements UrlInputListener, OnClickListener, OnFocusChangeListener,
43 TextWatcher {
Leon Scroggins571b3762010-05-26 10:25:01 -040044
Michael Kolbfe251992010-07-08 15:41:55 -070045 private static final int PROGRESS_MAX = 100;
Leon Scroggins571b3762010-05-26 10:25:01 -040046
Michael Kolb8233fac2010-10-26 16:08:53 -070047 private UiController mUiController;
48
Michael Kolba2b2ba82010-08-04 17:54:03 -070049 private Drawable mStopDrawable;
50 private Drawable mReloadDrawable;
Michael Kolbc7485ae2010-09-03 10:10:58 -070051
Michael Kolb3f65c382010-08-20 15:31:16 -070052 private View mContainer;
Michael Kolba2b2ba82010-08-04 17:54:03 -070053 private View mBackButton;
54 private View mForwardButton;
Michael Kolb31d469b2010-12-09 20:49:54 -080055 private ImageView mStar;
Michael Kolba2b2ba82010-08-04 17:54:03 -070056 private View mSearchButton;
Michael Kolb31d469b2010-12-09 20:49:54 -080057 private View mUrlContainer;
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 Kolb31d469b2010-12-09 20:49:54 -080062 private View mVoiceSearch;
Michael Kolbc7485ae2010-09-03 10:10:58 -070063 private PageProgressView mProgressView;
Michael Kolb31d469b2010-12-09 20:49:54 -080064 private UrlInputView mUrlInput;
Michael Kolba2b2ba82010-08-04 17:54:03 -070065 private boolean mInLoad;
Michael Kolbfe251992010-07-08 15:41:55 -070066
Michael Kolb8233fac2010-10-26 16:08:53 -070067 public TitleBarXLarge(Activity activity, UiController controller) {
68 super(activity);
Michael Kolb8233fac2010-10-26 16:08:53 -070069 mUiController = controller;
70 Resources resources = activity.getResources();
Michael Kolbc7485ae2010-09-03 10:10:58 -070071 mStopDrawable = resources.getDrawable(R.drawable.ic_stop_normal);
72 mReloadDrawable = resources.getDrawable(R.drawable.ic_refresh_normal);
Michael Kolb8233fac2010-10-26 16:08:53 -070073 rebuildLayout(activity, true);
Michael Kolbfe251992010-07-08 15:41:55 -070074 }
Leon Scroggins571b3762010-05-26 10:25:01 -040075
Michael Kolbfe251992010-07-08 15:41:55 -070076 private void rebuildLayout(Context context, boolean rebuildData) {
Michael Kolbfe251992010-07-08 15:41:55 -070077 LayoutInflater factory = LayoutInflater.from(context);
Michael Kolba2b2ba82010-08-04 17:54:03 -070078 factory.inflate(R.layout.url_bar, this);
Michael Kolbfe251992010-07-08 15:41:55 -070079
Michael Kolb3f65c382010-08-20 15:31:16 -070080 mContainer = findViewById(R.id.taburlbar);
Michael Kolb31d469b2010-12-09 20:49:54 -080081 mUrlInput = (UrlInputView) findViewById(R.id.url_focused);
Michael Kolbfe251992010-07-08 15:41:55 -070082 mAllButton = findViewById(R.id.all_btn);
83 // TODO: Change enabled states based on whether you can go
Leon Scroggins571b3762010-05-26 10:25:01 -040084 // back/forward. Probably should be done inside onPageStarted.
85 mBackButton = findViewById(R.id.back);
86 mForwardButton = findViewById(R.id.forward);
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);
89 mSearchButton = findViewById(R.id.search);
90 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 Kolb31d469b2010-12-09 20:49:54 -080093 mVoiceSearch = findViewById(R.id.voicesearch);
Michael Kolbc7485ae2010-09-03 10:10:58 -070094 mProgressView = (PageProgressView) findViewById(R.id.progress);
Michael Kolb31d469b2010-12-09 20:49:54 -080095 mUrlContainer = findViewById(R.id.urlbar_focused);
Michael Kolba2b2ba82010-08-04 17:54:03 -070096 mBackButton.setOnClickListener(this);
97 mForwardButton.setOnClickListener(this);
98 mStar.setOnClickListener(this);
99 mAllButton.setOnClickListener(this);
100 mStopButton.setOnClickListener(this);
101 mSearchButton.setOnClickListener(this);
Michael Kolb513286f2010-09-09 12:55:12 -0700102 mGoButton.setOnClickListener(this);
Michael Kolbb7b115e2010-09-25 16:59:37 -0700103 mClearButton.setOnClickListener(this);
Michael Kolb31d469b2010-12-09 20:49:54 -0800104 mUrlContainer.setOnClickListener(this);
105 mUrlInput.setUrlInputListener(this);
106 mUrlInput.setContainer(mUrlContainer);
107 mUrlInput.setController(mUiController);
108 mUrlInput.setOnFocusChangeListener(this);
109 mUrlInput.setSelectAllOnFocus(true);
110 mUrlInput.addTextChangedListener(this);
111 setUrlMode(false);
112 }
113
114 @Override
115 public void onFocusChange(View view, boolean hasFocus) {
116 setUrlMode(hasFocus);
Michael Kolbc7485ae2010-09-03 10:10:58 -0700117 }
Michael Kolbb7b115e2010-09-25 16:59:37 -0700118
Leon Scroggins4cd97792010-12-03 15:31:56 -0500119 public void setCurrentUrlIsBookmark(boolean isBookmark) {
Michael Kolb31d469b2010-12-09 20:49:54 -0800120 mStar.setActivated(isBookmark);
Leon Scroggins4cd97792010-12-03 15:31:56 -0500121 }
122
Michael Kolba2b2ba82010-08-04 17:54:03 -0700123 @Override
124 public void onClick(View v) {
Michael Kolb31d469b2010-12-09 20:49:54 -0800125 if (mBackButton == v) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700126 mUiController.getCurrentTopWebView().goBack();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700127 } else if (mForwardButton == v) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700128 mUiController.getCurrentTopWebView().goForward();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700129 } else if (mStar == v) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700130 mUiController.bookmarkCurrentPage(
Leon Scroggins88d08032010-10-21 15:17:10 -0400131 AddBookmarkPage.DEFAULT_FOLDER_ID);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700132 } else if (mAllButton == v) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700133 mUiController.bookmarksOrHistoryPicker(false);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700134 } else if (mSearchButton == v) {
135 search();
136 } else if (mStopButton == v) {
137 stopOrRefresh();
Michael Kolb513286f2010-09-09 12:55:12 -0700138 } else if (mGoButton == v) {
Michael Kolb31d469b2010-12-09 20:49:54 -0800139 if (!TextUtils.isEmpty(mUrlInput.getText())) {
140 onAction(mUrlInput.getText().toString(), null,
Michael Kolb257cc2c2010-12-09 09:45:52 -0800141 UrlInputView.TYPED);
Michael Kolb1ce78132010-09-23 15:50:53 -0700142 }
Michael Kolbb7b115e2010-09-25 16:59:37 -0700143 } else if (mClearButton == v) {
Michael Kolb31d469b2010-12-09 20:49:54 -0800144 clearOrClose();
Michael Kolbfe251992010-07-08 15:41:55 -0700145 }
146 }
147
Michael Kolb3f65c382010-08-20 15:31:16 -0700148 int getHeightWithoutProgress() {
149 return mContainer.getHeight();
150 }
151
Michael Kolba2b2ba82010-08-04 17:54:03 -0700152 @Override
153 void setFavicon(Bitmap icon) { }
Michael Kolbfe251992010-07-08 15:41:55 -0700154
Michael Kolb31d469b2010-12-09 20:49:54 -0800155 private void clearOrClose() {
156 if (TextUtils.isEmpty(mUrlInput.getText())) {
157 // close
158 setUrlMode(false);
159 } else {
160 // clear
161 mUrlInput.setText("");
162 }
163 }
164
Michael Kolbfe251992010-07-08 15:41:55 -0700165 // UrlInputListener implementation
166
167 @Override
Michael Kolb257cc2c2010-12-09 09:45:52 -0800168 public void onAction(String text, String extra, String source) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700169 mUiController.getCurrentTopWebView().requestFocus();
170 ((BaseUi) mUiController.getUi()).hideFakeTitleBar();
Michael Kolbfe251992010-07-08 15:41:55 -0700171 Intent i = new Intent();
172 i.setAction(Intent.ACTION_SEARCH);
173 i.putExtra(SearchManager.QUERY, text);
John Reck40f720e2010-11-10 11:57:04 -0800174 if (extra != null) {
175 i.putExtra(SearchManager.EXTRA_DATA_KEY, extra);
176 }
Michael Kolb257cc2c2010-12-09 09:45:52 -0800177 if (source != null) {
178 Bundle appData = new Bundle();
179 appData.putString(com.android.common.Search.SOURCE, source);
180 i.putExtra(SearchManager.APP_DATA, appData);
181 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700182 mUiController.handleNewIntent(i);
Michael Kolbb7b115e2010-09-25 16:59:37 -0700183 setUrlMode(false);
Michael Kolb513286f2010-09-09 12:55:12 -0700184 setDisplayTitle(text);
Michael Kolbfe251992010-07-08 15:41:55 -0700185 }
186
187 @Override
188 public void onDismiss() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700189 mUiController.getCurrentTopWebView().requestFocus();
190 ((BaseUi) mUiController.getUi()).hideFakeTitleBar();
Michael Kolbb7b115e2010-09-25 16:59:37 -0700191 setUrlMode(false);
Michael Kolb8233fac2010-10-26 16:08:53 -0700192 setDisplayTitle(mUiController.getCurrentWebView().getUrl());
Michael Kolb513286f2010-09-09 12:55:12 -0700193 }
194
195 @Override
196 public void onEdit(String text) {
Michael Kolb7b20ddd2010-10-14 15:03:28 -0700197 setDisplayTitle(text, true);
Michael Kolb513286f2010-09-09 12:55:12 -0700198 if (text != null) {
Michael Kolb31d469b2010-12-09 20:49:54 -0800199 mUrlInput.setSelection(text.length());
Michael Kolb513286f2010-09-09 12:55:12 -0700200 }
201 }
202
Michael Kolbb7b115e2010-09-25 16:59:37 -0700203 private void setUrlMode(boolean focused) {
Michael Kolbb7b115e2010-09-25 16:59:37 -0700204 if (focused) {
Michael Kolb31d469b2010-12-09 20:49:54 -0800205 mUrlInput.setDropDownWidth(mUrlContainer.getWidth());
206 mUrlInput.setDropDownHorizontalOffset(-mUrlInput.getLeft());
Michael Kolbb7b115e2010-09-25 16:59:37 -0700207 mSearchButton.setVisibility(View.GONE);
Michael Kolb31d469b2010-12-09 20:49:54 -0800208 mStar.setVisibility(View.GONE);
209 mClearButton.setVisibility(View.VISIBLE);
210 updateSearchMode();
Michael Kolbb7b115e2010-09-25 16:59:37 -0700211 } else {
Michael Kolb31d469b2010-12-09 20:49:54 -0800212 mUrlInput.clearFocus();
Michael Kolbb7b115e2010-09-25 16:59:37 -0700213 mSearchButton.setVisibility(View.VISIBLE);
214 mGoButton.setVisibility(View.GONE);
Michael Kolb31d469b2010-12-09 20:49:54 -0800215 mVoiceSearch.setVisibility(View.GONE);
216 mStar.setVisibility(View.VISIBLE);
217 mClearButton.setVisibility(View.GONE);
Michael Kolbb7b115e2010-09-25 16:59:37 -0700218 }
219 }
220
Michael Kolba2b2ba82010-08-04 17:54:03 -0700221 private void search() {
Michael Kolb31d469b2010-12-09 20:49:54 -0800222 mUrlInput.requestFocus();
Michael Kolb513286f2010-09-09 12:55:12 -0700223 setDisplayTitle("");
Michael Kolbffce12c2010-12-06 19:05:25 -0800224 setUrlMode(true);
Michael Kolbfe251992010-07-08 15:41:55 -0700225 }
226
Michael Kolba2b2ba82010-08-04 17:54:03 -0700227 private void stopOrRefresh() {
228 if (mInLoad) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700229 mUiController.stopLoading();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700230 } else {
Michael Kolb8233fac2010-10-26 16:08:53 -0700231 mUiController.getCurrentTopWebView().reload();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700232 }
Leon Scroggins571b3762010-05-26 10:25:01 -0400233 }
234
235 /**
Michael Kolbfe251992010-07-08 15:41:55 -0700236 * Update the progress, from 0 to 100.
Leon Scroggins571b3762010-05-26 10:25:01 -0400237 */
Michael Kolbfe251992010-07-08 15:41:55 -0700238 @Override
Michael Kolba2b2ba82010-08-04 17:54:03 -0700239 void setProgress(int newProgress) {
240 if (newProgress >= PROGRESS_MAX) {
Michael Kolbb7b115e2010-09-25 16:59:37 -0700241 mProgressView.setProgress(PageProgressView.MAX_PROGRESS);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700242 mProgressView.setVisibility(View.GONE);
243 mInLoad = false;
244 mStopButton.setImageDrawable(mReloadDrawable);
245 } else {
246 if (!mInLoad) {
247 mProgressView.setVisibility(View.VISIBLE);
248 mInLoad = true;
249 mStopButton.setImageDrawable(mStopDrawable);
250 }
Michael Kolbb7b115e2010-09-25 16:59:37 -0700251 mProgressView.setProgress(newProgress * PageProgressView.MAX_PROGRESS
252 / PROGRESS_MAX);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700253 }
Michael Kolbfe251992010-07-08 15:41:55 -0700254 }
255
Michael Kolb31d469b2010-12-09 20:49:54 -0800256 private void updateSearchMode() {
257 setSearchMode(TextUtils.isEmpty(mUrlInput.getText()));
258 }
259
260 private void setSearchMode(boolean voiceSearchEnabled) {
261 mVoiceSearch.setVisibility(voiceSearchEnabled ? View.VISIBLE :
262 View.GONE);
263 mGoButton.setVisibility(voiceSearchEnabled ? View.GONE :
264 View.VISIBLE);
265 }
266
Michael Kolbfe251992010-07-08 15:41:55 -0700267 @Override
Leon Scroggins571b3762010-05-26 10:25:01 -0400268 /* package */ void setDisplayTitle(String title) {
Michael Kolb31d469b2010-12-09 20:49:54 -0800269 mUrlInput.setText(title, false);
Leon Scroggins571b3762010-05-26 10:25:01 -0400270 }
271
Michael Kolb7b20ddd2010-10-14 15:03:28 -0700272 void setDisplayTitle(String title, boolean filter) {
Michael Kolb31d469b2010-12-09 20:49:54 -0800273 mUrlInput.setText(title, filter);
Michael Kolb7b20ddd2010-10-14 15:03:28 -0700274 }
275
Michael Kolb31d469b2010-12-09 20:49:54 -0800276 // UrlInput text watcher
Leon Scroggins4cd97792010-12-03 15:31:56 -0500277
Michael Kolb31d469b2010-12-09 20:49:54 -0800278 @Override
279 public void afterTextChanged(Editable s) {
280 if (mUrlInput.hasFocus()) {
281 // check if url input is empty and adjust voice search state
282 updateSearchMode();
Leon Scroggins4cd97792010-12-03 15:31:56 -0500283 }
284 }
Michael Kolb31d469b2010-12-09 20:49:54 -0800285
286 @Override
287 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
288 }
289
290 @Override
291 public void onTextChanged(CharSequence s, int start, int before, int count) {
292 }
293
Leon Scroggins571b3762010-05-26 10:25:01 -0400294}