blob: f39c7698ebb10fc8b904e3d02436010c4e883e69 [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) {
Leon Scroggins88d08032010-10-21 15:17:10 -0400130 mBrowserActivity.bookmarkCurrentPage(
131 AddBookmarkPage.DEFAULT_FOLDER_ID);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700132 } else if (mAllButton == v) {
Michael Kolbe421c242010-10-04 19:29:01 -0700133 mBrowserActivity.bookmarksOrHistoryPicker(false);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700134 } else if (mSearchButton == v) {
135 search();
136 } else if (mStopButton == v) {
137 stopOrRefresh();
Michael Kolb513286f2010-09-09 12:55:12 -0700138 } else if (mGoButton == v) {
Michael Kolb1ce78132010-09-23 15:50:53 -0700139 if (!TextUtils.isEmpty(mUrlFocused.getText())) {
John Reck40f720e2010-11-10 11:57:04 -0800140 onAction(mUrlFocused.getText().toString(), null);
Michael Kolb1ce78132010-09-23 15:50:53 -0700141 }
Michael Kolbb7b115e2010-09-25 16:59:37 -0700142 } else if (mClearButton == v) {
143 mUrlFocused.setText("");
Michael Kolbfe251992010-07-08 15:41:55 -0700144 }
145 }
146
Michael Kolb3f65c382010-08-20 15:31:16 -0700147 int getHeightWithoutProgress() {
148 return mContainer.getHeight();
149 }
150
Michael Kolba2b2ba82010-08-04 17:54:03 -0700151 @Override
152 void setFavicon(Bitmap icon) { }
Michael Kolbfe251992010-07-08 15:41:55 -0700153
154 // UrlInputListener implementation
155
156 @Override
John Reck40f720e2010-11-10 11:57:04 -0800157 public void onAction(String text, String extra) {
Michael Kolba2b2ba82010-08-04 17:54:03 -0700158 mBrowserActivity.getTabControl().getCurrentTopWebView().requestFocus();
159 mBrowserActivity.hideFakeTitleBar();
Michael Kolbfe251992010-07-08 15:41:55 -0700160 Intent i = new Intent();
161 i.setAction(Intent.ACTION_SEARCH);
162 i.putExtra(SearchManager.QUERY, text);
John Reck40f720e2010-11-10 11:57:04 -0800163 if (extra != null) {
164 i.putExtra(SearchManager.EXTRA_DATA_KEY, extra);
165 }
Michael Kolbfe251992010-07-08 15:41:55 -0700166 mBrowserActivity.onNewIntent(i);
Michael Kolbb7b115e2010-09-25 16:59:37 -0700167 setUrlMode(false);
Michael Kolb513286f2010-09-09 12:55:12 -0700168 setDisplayTitle(text);
Michael Kolbfe251992010-07-08 15:41:55 -0700169 }
170
171 @Override
172 public void onDismiss() {
Michael Kolba2b2ba82010-08-04 17:54:03 -0700173 mBrowserActivity.getTabControl().getCurrentTopWebView().requestFocus();
174 mBrowserActivity.hideFakeTitleBar();
Michael Kolbb7b115e2010-09-25 16:59:37 -0700175 setUrlMode(false);
Michael Kolb513286f2010-09-09 12:55:12 -0700176 setDisplayTitle(mBrowserActivity.getTabControl().getCurrentWebView().getUrl());
Michael Kolb513286f2010-09-09 12:55:12 -0700177 }
178
179 @Override
180 public void onEdit(String text) {
Michael Kolb7b20ddd2010-10-14 15:03:28 -0700181 setDisplayTitle(text, true);
Michael Kolb513286f2010-09-09 12:55:12 -0700182 if (text != null) {
183 mUrlFocused.setSelection(text.length());
184 }
185 }
186
Michael Kolbb7b115e2010-09-25 16:59:37 -0700187 private void setUrlMode(boolean focused) {
188 swapUrlContainer(focused);
189 if (focused) {
190 mSearchButton.setVisibility(View.GONE);
191 mGoButton.setVisibility(View.VISIBLE);
192 } else {
193 mSearchButton.setVisibility(View.VISIBLE);
194 mGoButton.setVisibility(View.GONE);
195 }
196 }
197
Michael Kolb513286f2010-09-09 12:55:12 -0700198 private void swapUrlContainer(boolean focus) {
199 mUnfocusContainer.setVisibility(focus ? View.GONE : View.VISIBLE);
200 mFocusContainer.setVisibility(focus ? View.VISIBLE : View.GONE);
Leon Scroggins571b3762010-05-26 10:25:01 -0400201 }
202
203 @Override
204 public void createContextMenu(ContextMenu menu) {
205 MenuInflater inflater = mBrowserActivity.getMenuInflater();
206 inflater.inflate(R.menu.title_context, menu);
207 mBrowserActivity.onCreateContextMenu(menu, this, null);
208 }
209
Michael Kolba2b2ba82010-08-04 17:54:03 -0700210 private void search() {
Michael Kolb513286f2010-09-09 12:55:12 -0700211 setDisplayTitle("");
212 mUrlUnfocused.requestFocus();
Michael Kolbfe251992010-07-08 15:41:55 -0700213 }
214
Michael Kolba2b2ba82010-08-04 17:54:03 -0700215 private void stopOrRefresh() {
216 if (mInLoad) {
217 mBrowserActivity.stopLoading();
218 } else {
219 mBrowserActivity.getTopWindow().reload();
220 }
Leon Scroggins571b3762010-05-26 10:25:01 -0400221 }
222
223 /**
Michael Kolbfe251992010-07-08 15:41:55 -0700224 * Update the progress, from 0 to 100.
Leon Scroggins571b3762010-05-26 10:25:01 -0400225 */
Michael Kolbfe251992010-07-08 15:41:55 -0700226 @Override
Michael Kolba2b2ba82010-08-04 17:54:03 -0700227 void setProgress(int newProgress) {
228 if (newProgress >= PROGRESS_MAX) {
Michael Kolbb7b115e2010-09-25 16:59:37 -0700229 mProgressView.setProgress(PageProgressView.MAX_PROGRESS);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700230 mProgressView.setVisibility(View.GONE);
231 mInLoad = false;
232 mStopButton.setImageDrawable(mReloadDrawable);
233 } else {
234 if (!mInLoad) {
235 mProgressView.setVisibility(View.VISIBLE);
236 mInLoad = true;
237 mStopButton.setImageDrawable(mStopDrawable);
238 }
Michael Kolbb7b115e2010-09-25 16:59:37 -0700239 mProgressView.setProgress(newProgress * PageProgressView.MAX_PROGRESS
240 / PROGRESS_MAX);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700241 }
Michael Kolbfe251992010-07-08 15:41:55 -0700242 }
243
244 @Override
Leon Scroggins571b3762010-05-26 10:25:01 -0400245 /* package */ void setDisplayTitle(String title) {
Michael Kolb21ce4d22010-09-15 14:55:05 -0700246 mUrlFocused.setText(title, false);
Michael Kolb513286f2010-09-09 12:55:12 -0700247 mUrlUnfocused.setText(title);
Leon Scroggins571b3762010-05-26 10:25:01 -0400248 }
249
Michael Kolb7b20ddd2010-10-14 15:03:28 -0700250 void setDisplayTitle(String title, boolean filter) {
251 mUrlFocused.setText(title, filter);
252 mUrlUnfocused.setText(title);
253 }
254
Leon Scroggins571b3762010-05-26 10:25:01 -0400255}