blob: 0aa09db4871c988a721ec6cf46944a88b5c78471 [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.LayoutInflater;
Leon Scroggins571b3762010-05-26 10:25:01 -040030import android.view.View;
Michael Kolba2b2ba82010-08-04 17:54:03 -070031import android.view.View.OnClickListener;
Leon Scroggins571b3762010-05-26 10:25:01 -040032import android.widget.ImageView;
Michael Kolb513286f2010-09-09 12:55:12 -070033import android.widget.TextView;
Leon Scroggins571b3762010-05-26 10:25:01 -040034
Leon Scroggins571b3762010-05-26 10:25:01 -040035/**
Michael Kolbfe251992010-07-08 15:41:55 -070036 * tabbed title bar for xlarge screen browser
Leon Scroggins571b3762010-05-26 10:25:01 -040037 */
Michael Kolbfe251992010-07-08 15:41:55 -070038public class TitleBarXLarge extends TitleBarBase
Michael Kolbba99c5d2010-11-29 14:57:41 -080039 implements UrlInputListener, OnClickListener {
Leon Scroggins571b3762010-05-26 10:25:01 -040040
Michael Kolbfe251992010-07-08 15:41:55 -070041 private static final int PROGRESS_MAX = 100;
Leon Scroggins571b3762010-05-26 10:25:01 -040042
Michael Kolb8233fac2010-10-26 16:08:53 -070043 private UiController mUiController;
44
Michael Kolba2b2ba82010-08-04 17:54:03 -070045 private Drawable mStopDrawable;
46 private Drawable mReloadDrawable;
Michael Kolbc7485ae2010-09-03 10:10:58 -070047
Michael Kolb3f65c382010-08-20 15:31:16 -070048 private View mContainer;
Michael Kolba2b2ba82010-08-04 17:54:03 -070049 private View mBackButton;
50 private View mForwardButton;
51 private View mStar;
52 private View mSearchButton;
Michael Kolb513286f2010-09-09 12:55:12 -070053 private View mFocusContainer;
54 private View mUnfocusContainer;
55 private View mGoButton;
Michael Kolba2b2ba82010-08-04 17:54:03 -070056 private ImageView mStopButton;
Michael Kolba2b2ba82010-08-04 17:54:03 -070057 private View mAllButton;
Michael Kolbb7b115e2010-09-25 16:59:37 -070058 private View mClearButton;
Michael Kolbc7485ae2010-09-03 10:10:58 -070059 private PageProgressView mProgressView;
Michael Kolb513286f2010-09-09 12:55:12 -070060 private UrlInputView mUrlFocused;
61 private TextView mUrlUnfocused;
Michael Kolba2b2ba82010-08-04 17:54:03 -070062 private boolean mInLoad;
Michael Kolbfe251992010-07-08 15:41:55 -070063
Michael Kolb8233fac2010-10-26 16:08:53 -070064 public TitleBarXLarge(Activity activity, UiController controller) {
65 super(activity);
Michael Kolb8233fac2010-10-26 16:08:53 -070066 mUiController = controller;
67 Resources resources = activity.getResources();
Michael Kolbc7485ae2010-09-03 10:10:58 -070068 mStopDrawable = resources.getDrawable(R.drawable.ic_stop_normal);
69 mReloadDrawable = resources.getDrawable(R.drawable.ic_refresh_normal);
Michael Kolb8233fac2010-10-26 16:08:53 -070070 rebuildLayout(activity, true);
Michael Kolbfe251992010-07-08 15:41:55 -070071 }
Leon Scroggins571b3762010-05-26 10:25:01 -040072
Michael Kolbfe251992010-07-08 15:41:55 -070073 private void rebuildLayout(Context context, boolean rebuildData) {
Michael Kolbfe251992010-07-08 15:41:55 -070074 LayoutInflater factory = LayoutInflater.from(context);
Michael Kolba2b2ba82010-08-04 17:54:03 -070075 factory.inflate(R.layout.url_bar, this);
Michael Kolbfe251992010-07-08 15:41:55 -070076
Michael Kolb3f65c382010-08-20 15:31:16 -070077 mContainer = findViewById(R.id.taburlbar);
Michael Kolb513286f2010-09-09 12:55:12 -070078 mUrlFocused = (UrlInputView) findViewById(R.id.url_focused);
79 mUrlUnfocused = (TextView) findViewById(R.id.url_unfocused);
Michael Kolbfe251992010-07-08 15:41:55 -070080 mAllButton = findViewById(R.id.all_btn);
81 // TODO: Change enabled states based on whether you can go
Leon Scroggins571b3762010-05-26 10:25:01 -040082 // back/forward. Probably should be done inside onPageStarted.
83 mBackButton = findViewById(R.id.back);
84 mForwardButton = findViewById(R.id.forward);
85 mStar = findViewById(R.id.star);
Michael Kolba2b2ba82010-08-04 17:54:03 -070086 mStopButton = (ImageView) findViewById(R.id.stop);
87 mSearchButton = findViewById(R.id.search);
88 mLockIcon = (ImageView) findViewById(R.id.lock);
Michael Kolb513286f2010-09-09 12:55:12 -070089 mGoButton = findViewById(R.id.go);
Michael Kolbb7b115e2010-09-25 16:59:37 -070090 mClearButton = findViewById(R.id.clear);
Michael Kolbc7485ae2010-09-03 10:10:58 -070091 mProgressView = (PageProgressView) findViewById(R.id.progress);
Michael Kolb513286f2010-09-09 12:55:12 -070092 mFocusContainer = findViewById(R.id.urlbar_focused);
93 mUnfocusContainer = findViewById(R.id.urlbar_unfocused);
Michael Kolbfe251992010-07-08 15:41:55 -070094
Michael Kolba2b2ba82010-08-04 17:54:03 -070095 mBackButton.setOnClickListener(this);
96 mForwardButton.setOnClickListener(this);
97 mStar.setOnClickListener(this);
98 mAllButton.setOnClickListener(this);
99 mStopButton.setOnClickListener(this);
100 mSearchButton.setOnClickListener(this);
Michael Kolb513286f2010-09-09 12:55:12 -0700101 mGoButton.setOnClickListener(this);
Michael Kolbb7b115e2010-09-25 16:59:37 -0700102 mClearButton.setOnClickListener(this);
Michael Kolb513286f2010-09-09 12:55:12 -0700103 mUrlFocused.setUrlInputListener(this);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700104 mUrlFocused.setContainer(mFocusContainer);
Michael Kolbba99c5d2010-11-29 14:57:41 -0800105 mUrlFocused.setController(mUiController);
Michael Kolb0506f2d2010-10-14 16:20:16 -0700106 mUnfocusContainer.setOnClickListener(this);
Michael Kolbc7485ae2010-09-03 10:10:58 -0700107 }
Michael Kolbb7b115e2010-09-25 16:59:37 -0700108
Michael Kolba2b2ba82010-08-04 17:54:03 -0700109 @Override
110 public void onClick(View v) {
Michael Kolb0506f2d2010-10-14 16:20:16 -0700111 if (mUnfocusContainer == v) {
Michael Kolbba99c5d2010-11-29 14:57:41 -0800112 setUrlMode(true);
Michael Kolb0506f2d2010-10-14 16:20:16 -0700113 } else if (mBackButton == v) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700114 mUiController.getCurrentTopWebView().goBack();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700115 } else if (mForwardButton == v) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700116 mUiController.getCurrentTopWebView().goForward();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700117 } else if (mStar == v) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700118 mUiController.bookmarkCurrentPage(
Leon Scroggins88d08032010-10-21 15:17:10 -0400119 AddBookmarkPage.DEFAULT_FOLDER_ID);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700120 } else if (mAllButton == v) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700121 mUiController.bookmarksOrHistoryPicker(false);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700122 } else if (mSearchButton == v) {
123 search();
124 } else if (mStopButton == v) {
125 stopOrRefresh();
Michael Kolb513286f2010-09-09 12:55:12 -0700126 } else if (mGoButton == v) {
Michael Kolb1ce78132010-09-23 15:50:53 -0700127 if (!TextUtils.isEmpty(mUrlFocused.getText())) {
John Reck40f720e2010-11-10 11:57:04 -0800128 onAction(mUrlFocused.getText().toString(), null);
Michael Kolb1ce78132010-09-23 15:50:53 -0700129 }
Michael Kolbb7b115e2010-09-25 16:59:37 -0700130 } else if (mClearButton == v) {
131 mUrlFocused.setText("");
Michael Kolbfe251992010-07-08 15:41:55 -0700132 }
133 }
134
Michael Kolb3f65c382010-08-20 15:31:16 -0700135 int getHeightWithoutProgress() {
136 return mContainer.getHeight();
137 }
138
Michael Kolba2b2ba82010-08-04 17:54:03 -0700139 @Override
140 void setFavicon(Bitmap icon) { }
Michael Kolbfe251992010-07-08 15:41:55 -0700141
142 // UrlInputListener implementation
143
144 @Override
John Reck40f720e2010-11-10 11:57:04 -0800145 public void onAction(String text, String extra) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700146 mUiController.getCurrentTopWebView().requestFocus();
147 ((BaseUi) mUiController.getUi()).hideFakeTitleBar();
Michael Kolbfe251992010-07-08 15:41:55 -0700148 Intent i = new Intent();
149 i.setAction(Intent.ACTION_SEARCH);
150 i.putExtra(SearchManager.QUERY, text);
John Reck40f720e2010-11-10 11:57:04 -0800151 if (extra != null) {
152 i.putExtra(SearchManager.EXTRA_DATA_KEY, extra);
153 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700154 mUiController.handleNewIntent(i);
Michael Kolbb7b115e2010-09-25 16:59:37 -0700155 setUrlMode(false);
Michael Kolb513286f2010-09-09 12:55:12 -0700156 setDisplayTitle(text);
Michael Kolbfe251992010-07-08 15:41:55 -0700157 }
158
159 @Override
160 public void onDismiss() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700161 mUiController.getCurrentTopWebView().requestFocus();
162 ((BaseUi) mUiController.getUi()).hideFakeTitleBar();
Michael Kolbb7b115e2010-09-25 16:59:37 -0700163 setUrlMode(false);
Michael Kolb8233fac2010-10-26 16:08:53 -0700164 setDisplayTitle(mUiController.getCurrentWebView().getUrl());
Michael Kolb513286f2010-09-09 12:55:12 -0700165 }
166
167 @Override
168 public void onEdit(String text) {
Michael Kolb7b20ddd2010-10-14 15:03:28 -0700169 setDisplayTitle(text, true);
Michael Kolb513286f2010-09-09 12:55:12 -0700170 if (text != null) {
171 mUrlFocused.setSelection(text.length());
172 }
173 }
174
Michael Kolbb7b115e2010-09-25 16:59:37 -0700175 private void setUrlMode(boolean focused) {
176 swapUrlContainer(focused);
177 if (focused) {
Michael Kolbba99c5d2010-11-29 14:57:41 -0800178 mUrlFocused.selectAll();
179 mUrlFocused.requestFocus();
180 mUrlFocused.setDropDownWidth(mUnfocusContainer.getWidth());
181 mUrlFocused.setDropDownHorizontalOffset(-mUrlFocused.getLeft());
Michael Kolbb7b115e2010-09-25 16:59:37 -0700182 mSearchButton.setVisibility(View.GONE);
183 mGoButton.setVisibility(View.VISIBLE);
184 } else {
185 mSearchButton.setVisibility(View.VISIBLE);
186 mGoButton.setVisibility(View.GONE);
187 }
188 }
189
Michael Kolb513286f2010-09-09 12:55:12 -0700190 private void swapUrlContainer(boolean focus) {
191 mUnfocusContainer.setVisibility(focus ? View.GONE : View.VISIBLE);
192 mFocusContainer.setVisibility(focus ? View.VISIBLE : View.GONE);
Leon Scroggins571b3762010-05-26 10:25:01 -0400193 }
194
Michael Kolba2b2ba82010-08-04 17:54:03 -0700195 private void search() {
Michael Kolb513286f2010-09-09 12:55:12 -0700196 setDisplayTitle("");
197 mUrlUnfocused.requestFocus();
Michael Kolbfe251992010-07-08 15:41:55 -0700198 }
199
Michael Kolba2b2ba82010-08-04 17:54:03 -0700200 private void stopOrRefresh() {
201 if (mInLoad) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700202 mUiController.stopLoading();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700203 } else {
Michael Kolb8233fac2010-10-26 16:08:53 -0700204 mUiController.getCurrentTopWebView().reload();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700205 }
Leon Scroggins571b3762010-05-26 10:25:01 -0400206 }
207
208 /**
Michael Kolbfe251992010-07-08 15:41:55 -0700209 * Update the progress, from 0 to 100.
Leon Scroggins571b3762010-05-26 10:25:01 -0400210 */
Michael Kolbfe251992010-07-08 15:41:55 -0700211 @Override
Michael Kolba2b2ba82010-08-04 17:54:03 -0700212 void setProgress(int newProgress) {
213 if (newProgress >= PROGRESS_MAX) {
Michael Kolbb7b115e2010-09-25 16:59:37 -0700214 mProgressView.setProgress(PageProgressView.MAX_PROGRESS);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700215 mProgressView.setVisibility(View.GONE);
216 mInLoad = false;
217 mStopButton.setImageDrawable(mReloadDrawable);
218 } else {
219 if (!mInLoad) {
220 mProgressView.setVisibility(View.VISIBLE);
221 mInLoad = true;
222 mStopButton.setImageDrawable(mStopDrawable);
223 }
Michael Kolbb7b115e2010-09-25 16:59:37 -0700224 mProgressView.setProgress(newProgress * PageProgressView.MAX_PROGRESS
225 / PROGRESS_MAX);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700226 }
Michael Kolbfe251992010-07-08 15:41:55 -0700227 }
228
229 @Override
Leon Scroggins571b3762010-05-26 10:25:01 -0400230 /* package */ void setDisplayTitle(String title) {
Michael Kolb21ce4d22010-09-15 14:55:05 -0700231 mUrlFocused.setText(title, false);
Michael Kolb513286f2010-09-09 12:55:12 -0700232 mUrlUnfocused.setText(title);
Leon Scroggins571b3762010-05-26 10:25:01 -0400233 }
234
Michael Kolb7b20ddd2010-10-14 15:03:28 -0700235 void setDisplayTitle(String title, boolean filter) {
236 mUrlFocused.setText(title, filter);
237 mUrlUnfocused.setText(title);
238 }
239
Leon Scroggins571b3762010-05-26 10:25:01 -0400240}