blob: 42effe15e16995de2bcac19ccde7ba8dd353bb0c [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 Kolb1ce78132010-09-23 15:50:53 -070029import android.text.TextUtils;
Leon Scroggins4cd97792010-12-03 15:31:56 -050030import android.util.AttributeSet;
Leon Scroggins571b3762010-05-26 10:25:01 -040031import android.view.LayoutInflater;
Leon Scroggins571b3762010-05-26 10:25:01 -040032import android.view.View;
Michael Kolba2b2ba82010-08-04 17:54:03 -070033import android.view.View.OnClickListener;
Leon Scroggins4cd97792010-12-03 15:31:56 -050034import android.widget.CheckBox;
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 Kolbba99c5d2010-11-29 14:57:41 -080042 implements UrlInputListener, OnClickListener {
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 UiController mUiController;
47
Michael Kolba2b2ba82010-08-04 17:54:03 -070048 private Drawable mStopDrawable;
49 private Drawable mReloadDrawable;
Michael Kolbc7485ae2010-09-03 10:10:58 -070050
Michael Kolb3f65c382010-08-20 15:31:16 -070051 private View mContainer;
Michael Kolba2b2ba82010-08-04 17:54:03 -070052 private View mBackButton;
53 private View mForwardButton;
Leon Scroggins4cd97792010-12-03 15:31:56 -050054 private CheckBox mStar;
Michael Kolba2b2ba82010-08-04 17:54:03 -070055 private View mSearchButton;
Michael Kolb513286f2010-09-09 12:55:12 -070056 private View mFocusContainer;
57 private View mUnfocusContainer;
58 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 Kolbc7485ae2010-09-03 10:10:58 -070062 private PageProgressView mProgressView;
Michael Kolb513286f2010-09-09 12:55:12 -070063 private UrlInputView mUrlFocused;
64 private TextView mUrlUnfocused;
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 Kolb513286f2010-09-09 12:55:12 -070081 mUrlFocused = (UrlInputView) findViewById(R.id.url_focused);
82 mUrlUnfocused = (TextView) findViewById(R.id.url_unfocused);
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);
Leon Scroggins4cd97792010-12-03 15:31:56 -050088 mStar = (CheckBox) 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 Kolbc7485ae2010-09-03 10:10:58 -070094 mProgressView = (PageProgressView) findViewById(R.id.progress);
Michael Kolb513286f2010-09-09 12:55:12 -070095 mFocusContainer = findViewById(R.id.urlbar_focused);
96 mUnfocusContainer = findViewById(R.id.urlbar_unfocused);
Michael Kolbfe251992010-07-08 15:41:55 -070097
Michael Kolba2b2ba82010-08-04 17:54:03 -070098 mBackButton.setOnClickListener(this);
99 mForwardButton.setOnClickListener(this);
100 mStar.setOnClickListener(this);
101 mAllButton.setOnClickListener(this);
102 mStopButton.setOnClickListener(this);
103 mSearchButton.setOnClickListener(this);
Michael Kolb513286f2010-09-09 12:55:12 -0700104 mGoButton.setOnClickListener(this);
Michael Kolbb7b115e2010-09-25 16:59:37 -0700105 mClearButton.setOnClickListener(this);
Michael Kolb513286f2010-09-09 12:55:12 -0700106 mUrlFocused.setUrlInputListener(this);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700107 mUrlFocused.setContainer(mFocusContainer);
Michael Kolbba99c5d2010-11-29 14:57:41 -0800108 mUrlFocused.setController(mUiController);
Michael Kolb0506f2d2010-10-14 16:20:16 -0700109 mUnfocusContainer.setOnClickListener(this);
Michael Kolbc7485ae2010-09-03 10:10:58 -0700110 }
Michael Kolbb7b115e2010-09-25 16:59:37 -0700111
Leon Scroggins4cd97792010-12-03 15:31:56 -0500112 public void setCurrentUrlIsBookmark(boolean isBookmark) {
113 mStar.setChecked(isBookmark);
114 }
115
Michael Kolba2b2ba82010-08-04 17:54:03 -0700116 @Override
117 public void onClick(View v) {
Michael Kolb0506f2d2010-10-14 16:20:16 -0700118 if (mUnfocusContainer == v) {
Michael Kolbba99c5d2010-11-29 14:57:41 -0800119 setUrlMode(true);
Michael Kolb0506f2d2010-10-14 16:20:16 -0700120 } else if (mBackButton == v) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700121 mUiController.getCurrentTopWebView().goBack();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700122 } else if (mForwardButton == v) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700123 mUiController.getCurrentTopWebView().goForward();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700124 } else if (mStar == v) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700125 mUiController.bookmarkCurrentPage(
Leon Scroggins88d08032010-10-21 15:17:10 -0400126 AddBookmarkPage.DEFAULT_FOLDER_ID);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700127 } else if (mAllButton == v) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700128 mUiController.bookmarksOrHistoryPicker(false);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700129 } else if (mSearchButton == v) {
130 search();
131 } else if (mStopButton == v) {
132 stopOrRefresh();
Michael Kolb513286f2010-09-09 12:55:12 -0700133 } else if (mGoButton == v) {
Michael Kolb1ce78132010-09-23 15:50:53 -0700134 if (!TextUtils.isEmpty(mUrlFocused.getText())) {
Michael Kolb257cc2c2010-12-09 09:45:52 -0800135 onAction(mUrlFocused.getText().toString(), null,
136 UrlInputView.TYPED);
Michael Kolb1ce78132010-09-23 15:50:53 -0700137 }
Michael Kolbb7b115e2010-09-25 16:59:37 -0700138 } else if (mClearButton == v) {
139 mUrlFocused.setText("");
Michael Kolbfe251992010-07-08 15:41:55 -0700140 }
141 }
142
Michael Kolb3f65c382010-08-20 15:31:16 -0700143 int getHeightWithoutProgress() {
144 return mContainer.getHeight();
145 }
146
Michael Kolba2b2ba82010-08-04 17:54:03 -0700147 @Override
148 void setFavicon(Bitmap icon) { }
Michael Kolbfe251992010-07-08 15:41:55 -0700149
150 // UrlInputListener implementation
151
152 @Override
Michael Kolb257cc2c2010-12-09 09:45:52 -0800153 public void onAction(String text, String extra, String source) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700154 mUiController.getCurrentTopWebView().requestFocus();
155 ((BaseUi) mUiController.getUi()).hideFakeTitleBar();
Michael Kolbfe251992010-07-08 15:41:55 -0700156 Intent i = new Intent();
157 i.setAction(Intent.ACTION_SEARCH);
158 i.putExtra(SearchManager.QUERY, text);
John Reck40f720e2010-11-10 11:57:04 -0800159 if (extra != null) {
160 i.putExtra(SearchManager.EXTRA_DATA_KEY, extra);
161 }
Michael Kolb257cc2c2010-12-09 09:45:52 -0800162 if (source != null) {
163 Bundle appData = new Bundle();
164 appData.putString(com.android.common.Search.SOURCE, source);
165 i.putExtra(SearchManager.APP_DATA, appData);
166 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700167 mUiController.handleNewIntent(i);
Michael Kolbb7b115e2010-09-25 16:59:37 -0700168 setUrlMode(false);
Michael Kolb513286f2010-09-09 12:55:12 -0700169 setDisplayTitle(text);
Michael Kolbfe251992010-07-08 15:41:55 -0700170 }
171
172 @Override
173 public void onDismiss() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700174 mUiController.getCurrentTopWebView().requestFocus();
175 ((BaseUi) mUiController.getUi()).hideFakeTitleBar();
Michael Kolbb7b115e2010-09-25 16:59:37 -0700176 setUrlMode(false);
Michael Kolb8233fac2010-10-26 16:08:53 -0700177 setDisplayTitle(mUiController.getCurrentWebView().getUrl());
Michael Kolb513286f2010-09-09 12:55:12 -0700178 }
179
180 @Override
181 public void onEdit(String text) {
Michael Kolb7b20ddd2010-10-14 15:03:28 -0700182 setDisplayTitle(text, true);
Michael Kolb513286f2010-09-09 12:55:12 -0700183 if (text != null) {
184 mUrlFocused.setSelection(text.length());
185 }
186 }
187
Michael Kolbb7b115e2010-09-25 16:59:37 -0700188 private void setUrlMode(boolean focused) {
189 swapUrlContainer(focused);
190 if (focused) {
Michael Kolbba99c5d2010-11-29 14:57:41 -0800191 mUrlFocused.selectAll();
192 mUrlFocused.requestFocus();
193 mUrlFocused.setDropDownWidth(mUnfocusContainer.getWidth());
194 mUrlFocused.setDropDownHorizontalOffset(-mUrlFocused.getLeft());
Michael Kolbb7b115e2010-09-25 16:59:37 -0700195 mSearchButton.setVisibility(View.GONE);
196 mGoButton.setVisibility(View.VISIBLE);
197 } else {
198 mSearchButton.setVisibility(View.VISIBLE);
199 mGoButton.setVisibility(View.GONE);
200 }
201 }
202
Michael Kolb513286f2010-09-09 12:55:12 -0700203 private void swapUrlContainer(boolean focus) {
204 mUnfocusContainer.setVisibility(focus ? View.GONE : View.VISIBLE);
205 mFocusContainer.setVisibility(focus ? View.VISIBLE : View.GONE);
Leon Scroggins571b3762010-05-26 10:25:01 -0400206 }
207
Michael Kolba2b2ba82010-08-04 17:54:03 -0700208 private void search() {
Michael Kolb513286f2010-09-09 12:55:12 -0700209 setDisplayTitle("");
Michael Kolbffce12c2010-12-06 19:05:25 -0800210 setUrlMode(true);
Michael Kolbfe251992010-07-08 15:41:55 -0700211 }
212
Michael Kolba2b2ba82010-08-04 17:54:03 -0700213 private void stopOrRefresh() {
214 if (mInLoad) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700215 mUiController.stopLoading();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700216 } else {
Michael Kolb8233fac2010-10-26 16:08:53 -0700217 mUiController.getCurrentTopWebView().reload();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700218 }
Leon Scroggins571b3762010-05-26 10:25:01 -0400219 }
220
221 /**
Michael Kolbfe251992010-07-08 15:41:55 -0700222 * Update the progress, from 0 to 100.
Leon Scroggins571b3762010-05-26 10:25:01 -0400223 */
Michael Kolbfe251992010-07-08 15:41:55 -0700224 @Override
Michael Kolba2b2ba82010-08-04 17:54:03 -0700225 void setProgress(int newProgress) {
226 if (newProgress >= PROGRESS_MAX) {
Michael Kolbb7b115e2010-09-25 16:59:37 -0700227 mProgressView.setProgress(PageProgressView.MAX_PROGRESS);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700228 mProgressView.setVisibility(View.GONE);
229 mInLoad = false;
230 mStopButton.setImageDrawable(mReloadDrawable);
231 } else {
232 if (!mInLoad) {
233 mProgressView.setVisibility(View.VISIBLE);
234 mInLoad = true;
235 mStopButton.setImageDrawable(mStopDrawable);
236 }
Michael Kolbb7b115e2010-09-25 16:59:37 -0700237 mProgressView.setProgress(newProgress * PageProgressView.MAX_PROGRESS
238 / PROGRESS_MAX);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700239 }
Michael Kolbfe251992010-07-08 15:41:55 -0700240 }
241
242 @Override
Leon Scroggins571b3762010-05-26 10:25:01 -0400243 /* package */ void setDisplayTitle(String title) {
Michael Kolb21ce4d22010-09-15 14:55:05 -0700244 mUrlFocused.setText(title, false);
Michael Kolb513286f2010-09-09 12:55:12 -0700245 mUrlUnfocused.setText(title);
Leon Scroggins571b3762010-05-26 10:25:01 -0400246 }
247
Michael Kolb7b20ddd2010-10-14 15:03:28 -0700248 void setDisplayTitle(String title, boolean filter) {
249 mUrlFocused.setText(title, filter);
250 mUrlUnfocused.setText(title);
251 }
252
Leon Scroggins4cd97792010-12-03 15:31:56 -0500253 /**
254 * Custom CheckBox which does not toggle when pressed. Used by mStar.
255 */
256 public static class CustomCheck extends CheckBox {
257 public CustomCheck(Context context) {
258 super(context);
259 }
260
261 public CustomCheck(Context context, AttributeSet attrs) {
262 super(context, attrs);
263 }
264
265 public CustomCheck(Context context, AttributeSet attrs, int defStyle) {
266 super(context, attrs, defStyle);
267 }
268
269 @Override
270 public void toggle() {
271 // Do nothing
272 }
273 }
Leon Scroggins571b3762010-05-26 10:25:01 -0400274}