blob: 7310f75014002b4ccb99cb4f10747b58a41c3c4d [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 Kolbfe251992010-07-08 15:41:55 -070021import android.app.SearchManager;
Leon Scroggins571b3762010-05-26 10:25:01 -040022import android.content.Context;
Michael Kolbfe251992010-07-08 15:41:55 -070023import android.content.Intent;
Leon Scroggins571b3762010-05-26 10:25:01 -040024import android.content.res.Resources;
Michael Kolbfe251992010-07-08 15:41:55 -070025import android.graphics.Bitmap;
Leon Scroggins571b3762010-05-26 10:25:01 -040026import android.graphics.drawable.Drawable;
Michael Kolb1ce78132010-09-23 15:50:53 -070027import android.text.TextUtils;
Leon Scroggins571b3762010-05-26 10:25:01 -040028import android.view.ContextMenu;
29import android.view.LayoutInflater;
30import android.view.MenuInflater;
31import android.view.View;
Michael Kolba2b2ba82010-08-04 17:54:03 -070032import android.view.View.OnClickListener;
Michael Kolbc7485ae2010-09-03 10:10:58 -070033import android.view.View.OnFocusChangeListener;
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 Kolbc7485ae2010-09-03 10:10:58 -070041 implements UrlInputListener, OnClickListener, OnFocusChangeListener {
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 Kolba2b2ba82010-08-04 17:54:03 -070045 private BrowserActivity mBrowserActivity;
46 private Drawable mStopDrawable;
47 private Drawable mReloadDrawable;
Michael Kolbc7485ae2010-09-03 10:10:58 -070048
Michael Kolbfe251992010-07-08 15:41:55 -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;
53 private View mStar;
54 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 Kolba2b2ba82010-08-04 17:54:03 -070066 public TitleBarXLarge(BrowserActivity context) {
Leon Scroggins571b3762010-05-26 10:25:01 -040067 super(context);
Leon Scroggins571b3762010-05-26 10:25:01 -040068 mBrowserActivity = context;
Michael Kolbfe251992010-07-08 15:41:55 -070069 Resources resources = context.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 Kolbfe251992010-07-08 15:41:55 -070072 rebuildLayout(context, 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);
87 mStar = 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);
106 mUrlUnfocused.setOnFocusChangeListener(this);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700107 mUrlFocused.setContainer(mFocusContainer);
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
Michael Kolbc7485ae2010-09-03 10:10:58 -0700111 public void onFocusChange(View v, boolean hasFocus) {
Michael Kolb513286f2010-09-09 12:55:12 -0700112 if (hasFocus) {
Michael Kolbb7b115e2010-09-25 16:59:37 -0700113 setUrlMode(true);
Michael Kolb513286f2010-09-09 12:55:12 -0700114 mUrlFocused.selectAll();
115 mUrlFocused.requestFocus();
116 mUrlFocused.setDropDownWidth(mUnfocusContainer.getWidth());
117 mUrlFocused.setDropDownHorizontalOffset(-mUrlFocused.getLeft());
118 }
Michael Kolbfe251992010-07-08 15:41:55 -0700119 }
120
Michael Kolba2b2ba82010-08-04 17:54:03 -0700121 @Override
122 public void onClick(View v) {
Michael Kolb0506f2d2010-10-14 16:20:16 -0700123 if (mUnfocusContainer == v) {
124 mUrlUnfocused.requestFocus();
125 } else if (mBackButton == v) {
Michael Kolba2b2ba82010-08-04 17:54:03 -0700126 mBrowserActivity.getTopWindow().goBack();
127 } else if (mForwardButton == v) {
128 mBrowserActivity.getTopWindow().goForward();
129 } else if (mStar == v) {
Michael Kolbc7485ae2010-09-03 10:10:58 -0700130 mBrowserActivity.promptAddOrInstallBookmark(mStar);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700131 } else if (mAllButton == v) {
Michael Kolbe421c242010-10-04 19:29:01 -0700132 mBrowserActivity.bookmarksOrHistoryPicker(false);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700133 } else if (mSearchButton == v) {
134 search();
135 } else if (mStopButton == v) {
136 stopOrRefresh();
Michael Kolb513286f2010-09-09 12:55:12 -0700137 } else if (mGoButton == v) {
Michael Kolb1ce78132010-09-23 15:50:53 -0700138 if (!TextUtils.isEmpty(mUrlFocused.getText())) {
139 onAction(mUrlFocused.getText().toString());
140 }
Michael Kolbb7b115e2010-09-25 16:59:37 -0700141 } else if (mClearButton == v) {
142 mUrlFocused.setText("");
Michael Kolbfe251992010-07-08 15:41:55 -0700143 }
144 }
145
Michael Kolb3f65c382010-08-20 15:31:16 -0700146 int getHeightWithoutProgress() {
147 return mContainer.getHeight();
148 }
149
Michael Kolba2b2ba82010-08-04 17:54:03 -0700150 @Override
151 void setFavicon(Bitmap icon) { }
Michael Kolbfe251992010-07-08 15:41:55 -0700152
153 // UrlInputListener implementation
154
155 @Override
156 public void onAction(String text) {
Michael Kolba2b2ba82010-08-04 17:54:03 -0700157 mBrowserActivity.getTabControl().getCurrentTopWebView().requestFocus();
158 mBrowserActivity.hideFakeTitleBar();
Michael Kolbfe251992010-07-08 15:41:55 -0700159 Intent i = new Intent();
160 i.setAction(Intent.ACTION_SEARCH);
161 i.putExtra(SearchManager.QUERY, text);
162 mBrowserActivity.onNewIntent(i);
Michael Kolbb7b115e2010-09-25 16:59:37 -0700163 setUrlMode(false);
Michael Kolb513286f2010-09-09 12:55:12 -0700164 setDisplayTitle(text);
Michael Kolbfe251992010-07-08 15:41:55 -0700165 }
166
167 @Override
168 public void onDismiss() {
Michael Kolba2b2ba82010-08-04 17:54:03 -0700169 mBrowserActivity.getTabControl().getCurrentTopWebView().requestFocus();
170 mBrowserActivity.hideFakeTitleBar();
Michael Kolbb7b115e2010-09-25 16:59:37 -0700171 setUrlMode(false);
Michael Kolb513286f2010-09-09 12:55:12 -0700172 setDisplayTitle(mBrowserActivity.getTabControl().getCurrentWebView().getUrl());
Michael Kolb513286f2010-09-09 12:55:12 -0700173 }
174
175 @Override
176 public void onEdit(String text) {
Michael Kolb7b20ddd2010-10-14 15:03:28 -0700177 setDisplayTitle(text, true);
Michael Kolb513286f2010-09-09 12:55:12 -0700178 if (text != null) {
179 mUrlFocused.setSelection(text.length());
180 }
181 }
182
Michael Kolbb7b115e2010-09-25 16:59:37 -0700183 private void setUrlMode(boolean focused) {
184 swapUrlContainer(focused);
185 if (focused) {
186 mSearchButton.setVisibility(View.GONE);
187 mGoButton.setVisibility(View.VISIBLE);
188 } else {
189 mSearchButton.setVisibility(View.VISIBLE);
190 mGoButton.setVisibility(View.GONE);
191 }
192 }
193
Michael Kolb513286f2010-09-09 12:55:12 -0700194 private void swapUrlContainer(boolean focus) {
195 mUnfocusContainer.setVisibility(focus ? View.GONE : View.VISIBLE);
196 mFocusContainer.setVisibility(focus ? View.VISIBLE : View.GONE);
Leon Scroggins571b3762010-05-26 10:25:01 -0400197 }
198
199 @Override
200 public void createContextMenu(ContextMenu menu) {
201 MenuInflater inflater = mBrowserActivity.getMenuInflater();
202 inflater.inflate(R.menu.title_context, menu);
203 mBrowserActivity.onCreateContextMenu(menu, this, null);
204 }
205
Michael Kolba2b2ba82010-08-04 17:54:03 -0700206 private void search() {
Michael Kolb513286f2010-09-09 12:55:12 -0700207 setDisplayTitle("");
208 mUrlUnfocused.requestFocus();
Michael Kolbfe251992010-07-08 15:41:55 -0700209 }
210
Michael Kolba2b2ba82010-08-04 17:54:03 -0700211 private void stopOrRefresh() {
212 if (mInLoad) {
213 mBrowserActivity.stopLoading();
214 } else {
215 mBrowserActivity.getTopWindow().reload();
216 }
Leon Scroggins571b3762010-05-26 10:25:01 -0400217 }
218
219 /**
Michael Kolbfe251992010-07-08 15:41:55 -0700220 * Update the progress, from 0 to 100.
Leon Scroggins571b3762010-05-26 10:25:01 -0400221 */
Michael Kolbfe251992010-07-08 15:41:55 -0700222 @Override
Michael Kolba2b2ba82010-08-04 17:54:03 -0700223 void setProgress(int newProgress) {
224 if (newProgress >= PROGRESS_MAX) {
Michael Kolbb7b115e2010-09-25 16:59:37 -0700225 mProgressView.setProgress(PageProgressView.MAX_PROGRESS);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700226 mProgressView.setVisibility(View.GONE);
227 mInLoad = false;
228 mStopButton.setImageDrawable(mReloadDrawable);
229 } else {
230 if (!mInLoad) {
231 mProgressView.setVisibility(View.VISIBLE);
232 mInLoad = true;
233 mStopButton.setImageDrawable(mStopDrawable);
234 }
Michael Kolbb7b115e2010-09-25 16:59:37 -0700235 mProgressView.setProgress(newProgress * PageProgressView.MAX_PROGRESS
236 / PROGRESS_MAX);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700237 }
Michael Kolbfe251992010-07-08 15:41:55 -0700238 }
239
240 @Override
Leon Scroggins571b3762010-05-26 10:25:01 -0400241 /* package */ void setDisplayTitle(String title) {
Michael Kolb21ce4d22010-09-15 14:55:05 -0700242 mUrlFocused.setText(title, false);
Michael Kolb513286f2010-09-09 12:55:12 -0700243 mUrlUnfocused.setText(title);
Leon Scroggins571b3762010-05-26 10:25:01 -0400244 }
245
Michael Kolb7b20ddd2010-10-14 15:03:28 -0700246 void setDisplayTitle(String title, boolean filter) {
247 mUrlFocused.setText(title, filter);
248 mUrlUnfocused.setText(title);
249 }
250
Leon Scroggins571b3762010-05-26 10:25:01 -0400251}