blob: 7e54710c941fd789091be65566f47b0b0b5feacd [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 Scroggins571b3762010-05-26 10:25:01 -040029import android.view.ContextMenu;
30import android.view.LayoutInflater;
31import android.view.MenuInflater;
32import android.view.View;
Michael Kolba2b2ba82010-08-04 17:54:03 -070033import android.view.View.OnClickListener;
Michael Kolbc7485ae2010-09-03 10:10:58 -070034import android.view.View.OnFocusChangeListener;
Leon Scroggins571b3762010-05-26 10:25:01 -040035import android.widget.ImageView;
Michael Kolb513286f2010-09-09 12:55:12 -070036import android.widget.TextView;
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 Kolbc7485ae2010-09-03 10:10:58 -070042 implements UrlInputListener, OnClickListener, OnFocusChangeListener {
Leon Scroggins571b3762010-05-26 10:25:01 -040043
Michael Kolbfe251992010-07-08 15:41:55 -070044 private static final int PROGRESS_MAX = 100;
Leon Scroggins571b3762010-05-26 10:25:01 -040045
Michael Kolb8233fac2010-10-26 16:08:53 -070046 private Activity mActivity;
47 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;
55 private View mStar;
56 private View mSearchButton;
Michael Kolb513286f2010-09-09 12:55:12 -070057 private View mFocusContainer;
58 private View mUnfocusContainer;
59 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 Kolbc7485ae2010-09-03 10:10:58 -070063 private PageProgressView mProgressView;
Michael Kolb513286f2010-09-09 12:55:12 -070064 private UrlInputView mUrlFocused;
65 private TextView mUrlUnfocused;
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);
70 mActivity = activity;
71 mUiController = controller;
72 Resources resources = activity.getResources();
Michael Kolbc7485ae2010-09-03 10:10:58 -070073 mStopDrawable = resources.getDrawable(R.drawable.ic_stop_normal);
74 mReloadDrawable = resources.getDrawable(R.drawable.ic_refresh_normal);
Michael Kolb8233fac2010-10-26 16:08:53 -070075 rebuildLayout(activity, true);
Michael Kolbfe251992010-07-08 15:41:55 -070076 }
Leon Scroggins571b3762010-05-26 10:25:01 -040077
Michael Kolbfe251992010-07-08 15:41:55 -070078 private void rebuildLayout(Context context, boolean rebuildData) {
Michael Kolbfe251992010-07-08 15:41:55 -070079 LayoutInflater factory = LayoutInflater.from(context);
Michael Kolba2b2ba82010-08-04 17:54:03 -070080 factory.inflate(R.layout.url_bar, this);
Michael Kolbfe251992010-07-08 15:41:55 -070081
Michael Kolb3f65c382010-08-20 15:31:16 -070082 mContainer = findViewById(R.id.taburlbar);
Michael Kolb513286f2010-09-09 12:55:12 -070083 mUrlFocused = (UrlInputView) findViewById(R.id.url_focused);
84 mUrlUnfocused = (TextView) findViewById(R.id.url_unfocused);
Michael Kolbfe251992010-07-08 15:41:55 -070085 mAllButton = findViewById(R.id.all_btn);
86 // TODO: Change enabled states based on whether you can go
Leon Scroggins571b3762010-05-26 10:25:01 -040087 // back/forward. Probably should be done inside onPageStarted.
88 mBackButton = findViewById(R.id.back);
89 mForwardButton = findViewById(R.id.forward);
90 mStar = findViewById(R.id.star);
Michael Kolba2b2ba82010-08-04 17:54:03 -070091 mStopButton = (ImageView) findViewById(R.id.stop);
92 mSearchButton = findViewById(R.id.search);
93 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 Kolbc7485ae2010-09-03 10:10:58 -070096 mProgressView = (PageProgressView) findViewById(R.id.progress);
Michael Kolb513286f2010-09-09 12:55:12 -070097 mFocusContainer = findViewById(R.id.urlbar_focused);
98 mUnfocusContainer = findViewById(R.id.urlbar_unfocused);
Michael Kolbfe251992010-07-08 15:41:55 -070099
Michael Kolba2b2ba82010-08-04 17:54:03 -0700100 mBackButton.setOnClickListener(this);
101 mForwardButton.setOnClickListener(this);
102 mStar.setOnClickListener(this);
103 mAllButton.setOnClickListener(this);
104 mStopButton.setOnClickListener(this);
105 mSearchButton.setOnClickListener(this);
Michael Kolb513286f2010-09-09 12:55:12 -0700106 mGoButton.setOnClickListener(this);
Michael Kolbb7b115e2010-09-25 16:59:37 -0700107 mClearButton.setOnClickListener(this);
Michael Kolb513286f2010-09-09 12:55:12 -0700108 mUrlFocused.setUrlInputListener(this);
109 mUrlUnfocused.setOnFocusChangeListener(this);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700110 mUrlFocused.setContainer(mFocusContainer);
Michael Kolb0506f2d2010-10-14 16:20:16 -0700111 mUnfocusContainer.setOnClickListener(this);
Michael Kolbc7485ae2010-09-03 10:10:58 -0700112 }
Michael Kolbb7b115e2010-09-25 16:59:37 -0700113
Michael Kolbc7485ae2010-09-03 10:10:58 -0700114 public void onFocusChange(View v, boolean hasFocus) {
Michael Kolb513286f2010-09-09 12:55:12 -0700115 if (hasFocus) {
Michael Kolbb7b115e2010-09-25 16:59:37 -0700116 setUrlMode(true);
Michael Kolb513286f2010-09-09 12:55:12 -0700117 mUrlFocused.selectAll();
118 mUrlFocused.requestFocus();
119 mUrlFocused.setDropDownWidth(mUnfocusContainer.getWidth());
120 mUrlFocused.setDropDownHorizontalOffset(-mUrlFocused.getLeft());
121 }
Michael Kolbfe251992010-07-08 15:41:55 -0700122 }
123
Michael Kolba2b2ba82010-08-04 17:54:03 -0700124 @Override
125 public void onClick(View v) {
Michael Kolb0506f2d2010-10-14 16:20:16 -0700126 if (mUnfocusContainer == v) {
127 mUrlUnfocused.requestFocus();
128 } else if (mBackButton == v) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700129 mUiController.getCurrentTopWebView().goBack();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700130 } else if (mForwardButton == v) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700131 mUiController.getCurrentTopWebView().goForward();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700132 } else if (mStar == v) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700133 mUiController.bookmarkCurrentPage(
Leon Scroggins88d08032010-10-21 15:17:10 -0400134 AddBookmarkPage.DEFAULT_FOLDER_ID);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700135 } else if (mAllButton == v) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700136 mUiController.bookmarksOrHistoryPicker(false);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700137 } else if (mSearchButton == v) {
138 search();
139 } else if (mStopButton == v) {
140 stopOrRefresh();
Michael Kolb513286f2010-09-09 12:55:12 -0700141 } else if (mGoButton == v) {
Michael Kolb1ce78132010-09-23 15:50:53 -0700142 if (!TextUtils.isEmpty(mUrlFocused.getText())) {
John Reck40f720e2010-11-10 11:57:04 -0800143 onAction(mUrlFocused.getText().toString(), null);
Michael Kolb1ce78132010-09-23 15:50:53 -0700144 }
Michael Kolbb7b115e2010-09-25 16:59:37 -0700145 } else if (mClearButton == v) {
146 mUrlFocused.setText("");
Michael Kolbfe251992010-07-08 15:41:55 -0700147 }
148 }
149
Michael Kolb3f65c382010-08-20 15:31:16 -0700150 int getHeightWithoutProgress() {
151 return mContainer.getHeight();
152 }
153
Michael Kolba2b2ba82010-08-04 17:54:03 -0700154 @Override
155 void setFavicon(Bitmap icon) { }
Michael Kolbfe251992010-07-08 15:41:55 -0700156
157 // UrlInputListener implementation
158
159 @Override
John Reck40f720e2010-11-10 11:57:04 -0800160 public void onAction(String text, String extra) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700161 mUiController.getCurrentTopWebView().requestFocus();
162 ((BaseUi) mUiController.getUi()).hideFakeTitleBar();
Michael Kolbfe251992010-07-08 15:41:55 -0700163 Intent i = new Intent();
164 i.setAction(Intent.ACTION_SEARCH);
165 i.putExtra(SearchManager.QUERY, text);
John Reck40f720e2010-11-10 11:57:04 -0800166 if (extra != null) {
167 i.putExtra(SearchManager.EXTRA_DATA_KEY, extra);
168 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700169 mUiController.handleNewIntent(i);
Michael Kolbb7b115e2010-09-25 16:59:37 -0700170 setUrlMode(false);
Michael Kolb513286f2010-09-09 12:55:12 -0700171 setDisplayTitle(text);
Michael Kolbfe251992010-07-08 15:41:55 -0700172 }
173
174 @Override
175 public void onDismiss() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700176 mUiController.getCurrentTopWebView().requestFocus();
177 ((BaseUi) mUiController.getUi()).hideFakeTitleBar();
Michael Kolbb7b115e2010-09-25 16:59:37 -0700178 setUrlMode(false);
Michael Kolb8233fac2010-10-26 16:08:53 -0700179 setDisplayTitle(mUiController.getCurrentWebView().getUrl());
Michael Kolb513286f2010-09-09 12:55:12 -0700180 }
181
182 @Override
183 public void onEdit(String text) {
Michael Kolb7b20ddd2010-10-14 15:03:28 -0700184 setDisplayTitle(text, true);
Michael Kolb513286f2010-09-09 12:55:12 -0700185 if (text != null) {
186 mUrlFocused.setSelection(text.length());
187 }
188 }
189
Michael Kolbb7b115e2010-09-25 16:59:37 -0700190 private void setUrlMode(boolean focused) {
191 swapUrlContainer(focused);
192 if (focused) {
193 mSearchButton.setVisibility(View.GONE);
194 mGoButton.setVisibility(View.VISIBLE);
195 } else {
196 mSearchButton.setVisibility(View.VISIBLE);
197 mGoButton.setVisibility(View.GONE);
198 }
199 }
200
Michael Kolb513286f2010-09-09 12:55:12 -0700201 private void swapUrlContainer(boolean focus) {
202 mUnfocusContainer.setVisibility(focus ? View.GONE : View.VISIBLE);
203 mFocusContainer.setVisibility(focus ? View.VISIBLE : View.GONE);
Leon Scroggins571b3762010-05-26 10:25:01 -0400204 }
205
206 @Override
207 public void createContextMenu(ContextMenu menu) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700208 MenuInflater inflater = mActivity.getMenuInflater();
Leon Scroggins571b3762010-05-26 10:25:01 -0400209 inflater.inflate(R.menu.title_context, menu);
Michael Kolb8233fac2010-10-26 16:08:53 -0700210 mActivity.onCreateContextMenu(menu, this, null);
Leon Scroggins571b3762010-05-26 10:25:01 -0400211 }
212
Michael Kolba2b2ba82010-08-04 17:54:03 -0700213 private void search() {
Michael Kolb513286f2010-09-09 12:55:12 -0700214 setDisplayTitle("");
215 mUrlUnfocused.requestFocus();
Michael Kolbfe251992010-07-08 15:41:55 -0700216 }
217
Michael Kolba2b2ba82010-08-04 17:54:03 -0700218 private void stopOrRefresh() {
219 if (mInLoad) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700220 mUiController.stopLoading();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700221 } else {
Michael Kolb8233fac2010-10-26 16:08:53 -0700222 mUiController.getCurrentTopWebView().reload();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700223 }
Leon Scroggins571b3762010-05-26 10:25:01 -0400224 }
225
226 /**
Michael Kolbfe251992010-07-08 15:41:55 -0700227 * Update the progress, from 0 to 100.
Leon Scroggins571b3762010-05-26 10:25:01 -0400228 */
Michael Kolbfe251992010-07-08 15:41:55 -0700229 @Override
Michael Kolba2b2ba82010-08-04 17:54:03 -0700230 void setProgress(int newProgress) {
231 if (newProgress >= PROGRESS_MAX) {
Michael Kolbb7b115e2010-09-25 16:59:37 -0700232 mProgressView.setProgress(PageProgressView.MAX_PROGRESS);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700233 mProgressView.setVisibility(View.GONE);
234 mInLoad = false;
235 mStopButton.setImageDrawable(mReloadDrawable);
236 } else {
237 if (!mInLoad) {
238 mProgressView.setVisibility(View.VISIBLE);
239 mInLoad = true;
240 mStopButton.setImageDrawable(mStopDrawable);
241 }
Michael Kolbb7b115e2010-09-25 16:59:37 -0700242 mProgressView.setProgress(newProgress * PageProgressView.MAX_PROGRESS
243 / PROGRESS_MAX);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700244 }
Michael Kolbfe251992010-07-08 15:41:55 -0700245 }
246
247 @Override
Leon Scroggins571b3762010-05-26 10:25:01 -0400248 /* package */ void setDisplayTitle(String title) {
Michael Kolb21ce4d22010-09-15 14:55:05 -0700249 mUrlFocused.setText(title, false);
Michael Kolb513286f2010-09-09 12:55:12 -0700250 mUrlUnfocused.setText(title);
Leon Scroggins571b3762010-05-26 10:25:01 -0400251 }
252
Michael Kolb7b20ddd2010-10-14 15:03:28 -0700253 void setDisplayTitle(String title, boolean filter) {
254 mUrlFocused.setText(title, filter);
255 mUrlUnfocused.setText(title);
256 }
257
Leon Scroggins571b3762010-05-26 10:25:01 -0400258}