blob: cd3b2305025f62947744777497b8383f50cbbf62 [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 Kolb1ce78132010-09-23 15:50:53 -070028import android.text.TextUtils;
Leon Scroggins4cd97792010-12-03 15:31:56 -050029import android.util.AttributeSet;
Leon Scroggins571b3762010-05-26 10:25:01 -040030import android.view.LayoutInflater;
Leon Scroggins571b3762010-05-26 10:25:01 -040031import android.view.View;
Michael Kolba2b2ba82010-08-04 17:54:03 -070032import android.view.View.OnClickListener;
Leon Scroggins4cd97792010-12-03 15:31:56 -050033import android.widget.CheckBox;
Leon Scroggins571b3762010-05-26 10:25:01 -040034import android.widget.ImageView;
Michael Kolb513286f2010-09-09 12:55:12 -070035import android.widget.TextView;
Leon Scroggins571b3762010-05-26 10:25:01 -040036
Leon Scroggins571b3762010-05-26 10:25:01 -040037/**
Michael Kolbfe251992010-07-08 15:41:55 -070038 * tabbed title bar for xlarge screen browser
Leon Scroggins571b3762010-05-26 10:25:01 -040039 */
Michael Kolbfe251992010-07-08 15:41:55 -070040public class TitleBarXLarge extends TitleBarBase
Michael Kolbba99c5d2010-11-29 14:57:41 -080041 implements UrlInputListener, OnClickListener {
Leon Scroggins571b3762010-05-26 10:25:01 -040042
Michael Kolbfe251992010-07-08 15:41:55 -070043 private static final int PROGRESS_MAX = 100;
Leon Scroggins571b3762010-05-26 10:25:01 -040044
Michael Kolb8233fac2010-10-26 16:08:53 -070045 private UiController mUiController;
46
Michael Kolba2b2ba82010-08-04 17:54:03 -070047 private Drawable mStopDrawable;
48 private Drawable mReloadDrawable;
Michael Kolbc7485ae2010-09-03 10:10:58 -070049
Michael Kolb3f65c382010-08-20 15:31:16 -070050 private View mContainer;
Michael Kolba2b2ba82010-08-04 17:54:03 -070051 private View mBackButton;
52 private View mForwardButton;
Leon Scroggins4cd97792010-12-03 15:31:56 -050053 private CheckBox mStar;
Michael Kolba2b2ba82010-08-04 17:54:03 -070054 private View mSearchButton;
Michael Kolb513286f2010-09-09 12:55:12 -070055 private View mFocusContainer;
56 private View mUnfocusContainer;
57 private View mGoButton;
Michael Kolba2b2ba82010-08-04 17:54:03 -070058 private ImageView mStopButton;
Michael Kolba2b2ba82010-08-04 17:54:03 -070059 private View mAllButton;
Michael Kolbb7b115e2010-09-25 16:59:37 -070060 private View mClearButton;
Michael Kolbc7485ae2010-09-03 10:10:58 -070061 private PageProgressView mProgressView;
Michael Kolb513286f2010-09-09 12:55:12 -070062 private UrlInputView mUrlFocused;
63 private TextView mUrlUnfocused;
Michael Kolba2b2ba82010-08-04 17:54:03 -070064 private boolean mInLoad;
Michael Kolbfe251992010-07-08 15:41:55 -070065
Michael Kolb8233fac2010-10-26 16:08:53 -070066 public TitleBarXLarge(Activity activity, UiController controller) {
67 super(activity);
Michael Kolb8233fac2010-10-26 16:08:53 -070068 mUiController = controller;
69 Resources resources = activity.getResources();
Michael Kolbc7485ae2010-09-03 10:10:58 -070070 mStopDrawable = resources.getDrawable(R.drawable.ic_stop_normal);
71 mReloadDrawable = resources.getDrawable(R.drawable.ic_refresh_normal);
Michael Kolb8233fac2010-10-26 16:08:53 -070072 rebuildLayout(activity, true);
Michael Kolbfe251992010-07-08 15:41:55 -070073 }
Leon Scroggins571b3762010-05-26 10:25:01 -040074
Michael Kolbfe251992010-07-08 15:41:55 -070075 private void rebuildLayout(Context context, boolean rebuildData) {
Michael Kolbfe251992010-07-08 15:41:55 -070076 LayoutInflater factory = LayoutInflater.from(context);
Michael Kolba2b2ba82010-08-04 17:54:03 -070077 factory.inflate(R.layout.url_bar, this);
Michael Kolbfe251992010-07-08 15:41:55 -070078
Michael Kolb3f65c382010-08-20 15:31:16 -070079 mContainer = findViewById(R.id.taburlbar);
Michael Kolb513286f2010-09-09 12:55:12 -070080 mUrlFocused = (UrlInputView) findViewById(R.id.url_focused);
81 mUrlUnfocused = (TextView) findViewById(R.id.url_unfocused);
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);
Leon Scroggins4cd97792010-12-03 15:31:56 -050087 mStar = (CheckBox) 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 Kolbc7485ae2010-09-03 10:10:58 -070093 mProgressView = (PageProgressView) findViewById(R.id.progress);
Michael Kolb513286f2010-09-09 12:55:12 -070094 mFocusContainer = findViewById(R.id.urlbar_focused);
95 mUnfocusContainer = findViewById(R.id.urlbar_unfocused);
Michael Kolbfe251992010-07-08 15:41:55 -070096
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 Kolb513286f2010-09-09 12:55:12 -0700105 mUrlFocused.setUrlInputListener(this);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700106 mUrlFocused.setContainer(mFocusContainer);
Michael Kolbba99c5d2010-11-29 14:57:41 -0800107 mUrlFocused.setController(mUiController);
Michael Kolb0506f2d2010-10-14 16:20:16 -0700108 mUnfocusContainer.setOnClickListener(this);
Michael Kolbc7485ae2010-09-03 10:10:58 -0700109 }
Michael Kolbb7b115e2010-09-25 16:59:37 -0700110
Leon Scroggins4cd97792010-12-03 15:31:56 -0500111 public void setCurrentUrlIsBookmark(boolean isBookmark) {
112 mStar.setChecked(isBookmark);
113 }
114
Michael Kolba2b2ba82010-08-04 17:54:03 -0700115 @Override
116 public void onClick(View v) {
Michael Kolb0506f2d2010-10-14 16:20:16 -0700117 if (mUnfocusContainer == v) {
Michael Kolbba99c5d2010-11-29 14:57:41 -0800118 setUrlMode(true);
Michael Kolb0506f2d2010-10-14 16:20:16 -0700119 } else if (mBackButton == v) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700120 mUiController.getCurrentTopWebView().goBack();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700121 } else if (mForwardButton == v) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700122 mUiController.getCurrentTopWebView().goForward();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700123 } else if (mStar == v) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700124 mUiController.bookmarkCurrentPage(
Leon Scroggins88d08032010-10-21 15:17:10 -0400125 AddBookmarkPage.DEFAULT_FOLDER_ID);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700126 } else if (mAllButton == v) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700127 mUiController.bookmarksOrHistoryPicker(false);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700128 } else if (mSearchButton == v) {
129 search();
130 } else if (mStopButton == v) {
131 stopOrRefresh();
Michael Kolb513286f2010-09-09 12:55:12 -0700132 } else if (mGoButton == v) {
Michael Kolb1ce78132010-09-23 15:50:53 -0700133 if (!TextUtils.isEmpty(mUrlFocused.getText())) {
John Reck40f720e2010-11-10 11:57:04 -0800134 onAction(mUrlFocused.getText().toString(), null);
Michael Kolb1ce78132010-09-23 15:50:53 -0700135 }
Michael Kolbb7b115e2010-09-25 16:59:37 -0700136 } else if (mClearButton == v) {
137 mUrlFocused.setText("");
Michael Kolbfe251992010-07-08 15:41:55 -0700138 }
139 }
140
Michael Kolb3f65c382010-08-20 15:31:16 -0700141 int getHeightWithoutProgress() {
142 return mContainer.getHeight();
143 }
144
Michael Kolba2b2ba82010-08-04 17:54:03 -0700145 @Override
146 void setFavicon(Bitmap icon) { }
Michael Kolbfe251992010-07-08 15:41:55 -0700147
148 // UrlInputListener implementation
149
150 @Override
John Reck40f720e2010-11-10 11:57:04 -0800151 public void onAction(String text, String extra) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700152 mUiController.getCurrentTopWebView().requestFocus();
153 ((BaseUi) mUiController.getUi()).hideFakeTitleBar();
Michael Kolbfe251992010-07-08 15:41:55 -0700154 Intent i = new Intent();
155 i.setAction(Intent.ACTION_SEARCH);
156 i.putExtra(SearchManager.QUERY, text);
John Reck40f720e2010-11-10 11:57:04 -0800157 if (extra != null) {
158 i.putExtra(SearchManager.EXTRA_DATA_KEY, extra);
159 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700160 mUiController.handleNewIntent(i);
Michael Kolbb7b115e2010-09-25 16:59:37 -0700161 setUrlMode(false);
Michael Kolb513286f2010-09-09 12:55:12 -0700162 setDisplayTitle(text);
Michael Kolbfe251992010-07-08 15:41:55 -0700163 }
164
165 @Override
166 public void onDismiss() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700167 mUiController.getCurrentTopWebView().requestFocus();
168 ((BaseUi) mUiController.getUi()).hideFakeTitleBar();
Michael Kolbb7b115e2010-09-25 16:59:37 -0700169 setUrlMode(false);
Michael Kolb8233fac2010-10-26 16:08:53 -0700170 setDisplayTitle(mUiController.getCurrentWebView().getUrl());
Michael Kolb513286f2010-09-09 12:55:12 -0700171 }
172
173 @Override
174 public void onEdit(String text) {
Michael Kolb7b20ddd2010-10-14 15:03:28 -0700175 setDisplayTitle(text, true);
Michael Kolb513286f2010-09-09 12:55:12 -0700176 if (text != null) {
177 mUrlFocused.setSelection(text.length());
178 }
179 }
180
Michael Kolbb7b115e2010-09-25 16:59:37 -0700181 private void setUrlMode(boolean focused) {
182 swapUrlContainer(focused);
183 if (focused) {
Michael Kolbba99c5d2010-11-29 14:57:41 -0800184 mUrlFocused.selectAll();
185 mUrlFocused.requestFocus();
186 mUrlFocused.setDropDownWidth(mUnfocusContainer.getWidth());
187 mUrlFocused.setDropDownHorizontalOffset(-mUrlFocused.getLeft());
Michael Kolbb7b115e2010-09-25 16:59:37 -0700188 mSearchButton.setVisibility(View.GONE);
189 mGoButton.setVisibility(View.VISIBLE);
190 } else {
191 mSearchButton.setVisibility(View.VISIBLE);
192 mGoButton.setVisibility(View.GONE);
193 }
194 }
195
Michael Kolb513286f2010-09-09 12:55:12 -0700196 private void swapUrlContainer(boolean focus) {
197 mUnfocusContainer.setVisibility(focus ? View.GONE : View.VISIBLE);
198 mFocusContainer.setVisibility(focus ? View.VISIBLE : View.GONE);
Leon Scroggins571b3762010-05-26 10:25:01 -0400199 }
200
Michael Kolba2b2ba82010-08-04 17:54:03 -0700201 private void search() {
Michael Kolb513286f2010-09-09 12:55:12 -0700202 setDisplayTitle("");
203 mUrlUnfocused.requestFocus();
Michael Kolbfe251992010-07-08 15:41:55 -0700204 }
205
Michael Kolba2b2ba82010-08-04 17:54:03 -0700206 private void stopOrRefresh() {
207 if (mInLoad) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700208 mUiController.stopLoading();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700209 } else {
Michael Kolb8233fac2010-10-26 16:08:53 -0700210 mUiController.getCurrentTopWebView().reload();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700211 }
Leon Scroggins571b3762010-05-26 10:25:01 -0400212 }
213
214 /**
Michael Kolbfe251992010-07-08 15:41:55 -0700215 * Update the progress, from 0 to 100.
Leon Scroggins571b3762010-05-26 10:25:01 -0400216 */
Michael Kolbfe251992010-07-08 15:41:55 -0700217 @Override
Michael Kolba2b2ba82010-08-04 17:54:03 -0700218 void setProgress(int newProgress) {
219 if (newProgress >= PROGRESS_MAX) {
Michael Kolbb7b115e2010-09-25 16:59:37 -0700220 mProgressView.setProgress(PageProgressView.MAX_PROGRESS);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700221 mProgressView.setVisibility(View.GONE);
222 mInLoad = false;
223 mStopButton.setImageDrawable(mReloadDrawable);
224 } else {
225 if (!mInLoad) {
226 mProgressView.setVisibility(View.VISIBLE);
227 mInLoad = true;
228 mStopButton.setImageDrawable(mStopDrawable);
229 }
Michael Kolbb7b115e2010-09-25 16:59:37 -0700230 mProgressView.setProgress(newProgress * PageProgressView.MAX_PROGRESS
231 / PROGRESS_MAX);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700232 }
Michael Kolbfe251992010-07-08 15:41:55 -0700233 }
234
235 @Override
Leon Scroggins571b3762010-05-26 10:25:01 -0400236 /* package */ void setDisplayTitle(String title) {
Michael Kolb21ce4d22010-09-15 14:55:05 -0700237 mUrlFocused.setText(title, false);
Michael Kolb513286f2010-09-09 12:55:12 -0700238 mUrlUnfocused.setText(title);
Leon Scroggins571b3762010-05-26 10:25:01 -0400239 }
240
Michael Kolb7b20ddd2010-10-14 15:03:28 -0700241 void setDisplayTitle(String title, boolean filter) {
242 mUrlFocused.setText(title, filter);
243 mUrlUnfocused.setText(title);
244 }
245
Leon Scroggins4cd97792010-12-03 15:31:56 -0500246 /**
247 * Custom CheckBox which does not toggle when pressed. Used by mStar.
248 */
249 public static class CustomCheck extends CheckBox {
250 public CustomCheck(Context context) {
251 super(context);
252 }
253
254 public CustomCheck(Context context, AttributeSet attrs) {
255 super(context, attrs);
256 }
257
258 public CustomCheck(Context context, AttributeSet attrs, int defStyle) {
259 super(context, attrs, defStyle);
260 }
261
262 @Override
263 public void toggle() {
264 // Do nothing
265 }
266 }
Leon Scroggins571b3762010-05-26 10:25:01 -0400267}