blob: b87ea78603295eef64ff66d599060ad9ca8e4bc1 [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())) {
140 onAction(mUrlFocused.getText().toString());
141 }
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
157 public void onAction(String text) {
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);
163 mBrowserActivity.onNewIntent(i);
Michael Kolbb7b115e2010-09-25 16:59:37 -0700164 setUrlMode(false);
Michael Kolb513286f2010-09-09 12:55:12 -0700165 setDisplayTitle(text);
Michael Kolbfe251992010-07-08 15:41:55 -0700166 }
167
168 @Override
169 public void onDismiss() {
Michael Kolba2b2ba82010-08-04 17:54:03 -0700170 mBrowserActivity.getTabControl().getCurrentTopWebView().requestFocus();
171 mBrowserActivity.hideFakeTitleBar();
Michael Kolbb7b115e2010-09-25 16:59:37 -0700172 setUrlMode(false);
Michael Kolb513286f2010-09-09 12:55:12 -0700173 setDisplayTitle(mBrowserActivity.getTabControl().getCurrentWebView().getUrl());
Michael Kolb513286f2010-09-09 12:55:12 -0700174 }
175
176 @Override
177 public void onEdit(String text) {
Michael Kolb7b20ddd2010-10-14 15:03:28 -0700178 setDisplayTitle(text, true);
Michael Kolb513286f2010-09-09 12:55:12 -0700179 if (text != null) {
180 mUrlFocused.setSelection(text.length());
181 }
182 }
183
Michael Kolbb7b115e2010-09-25 16:59:37 -0700184 private void setUrlMode(boolean focused) {
185 swapUrlContainer(focused);
186 if (focused) {
187 mSearchButton.setVisibility(View.GONE);
188 mGoButton.setVisibility(View.VISIBLE);
189 } else {
190 mSearchButton.setVisibility(View.VISIBLE);
191 mGoButton.setVisibility(View.GONE);
192 }
193 }
194
Michael Kolb513286f2010-09-09 12:55:12 -0700195 private void swapUrlContainer(boolean focus) {
196 mUnfocusContainer.setVisibility(focus ? View.GONE : View.VISIBLE);
197 mFocusContainer.setVisibility(focus ? View.VISIBLE : View.GONE);
Leon Scroggins571b3762010-05-26 10:25:01 -0400198 }
199
200 @Override
201 public void createContextMenu(ContextMenu menu) {
202 MenuInflater inflater = mBrowserActivity.getMenuInflater();
203 inflater.inflate(R.menu.title_context, menu);
204 mBrowserActivity.onCreateContextMenu(menu, this, null);
205 }
206
Michael Kolba2b2ba82010-08-04 17:54:03 -0700207 private void search() {
Michael Kolb513286f2010-09-09 12:55:12 -0700208 setDisplayTitle("");
209 mUrlUnfocused.requestFocus();
Michael Kolbfe251992010-07-08 15:41:55 -0700210 }
211
Michael Kolba2b2ba82010-08-04 17:54:03 -0700212 private void stopOrRefresh() {
213 if (mInLoad) {
214 mBrowserActivity.stopLoading();
215 } else {
216 mBrowserActivity.getTopWindow().reload();
217 }
Leon Scroggins571b3762010-05-26 10:25:01 -0400218 }
219
220 /**
Michael Kolbfe251992010-07-08 15:41:55 -0700221 * Update the progress, from 0 to 100.
Leon Scroggins571b3762010-05-26 10:25:01 -0400222 */
Michael Kolbfe251992010-07-08 15:41:55 -0700223 @Override
Michael Kolba2b2ba82010-08-04 17:54:03 -0700224 void setProgress(int newProgress) {
225 if (newProgress >= PROGRESS_MAX) {
Michael Kolbb7b115e2010-09-25 16:59:37 -0700226 mProgressView.setProgress(PageProgressView.MAX_PROGRESS);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700227 mProgressView.setVisibility(View.GONE);
228 mInLoad = false;
229 mStopButton.setImageDrawable(mReloadDrawable);
230 } else {
231 if (!mInLoad) {
232 mProgressView.setVisibility(View.VISIBLE);
233 mInLoad = true;
234 mStopButton.setImageDrawable(mStopDrawable);
235 }
Michael Kolbb7b115e2010-09-25 16:59:37 -0700236 mProgressView.setProgress(newProgress * PageProgressView.MAX_PROGRESS
237 / PROGRESS_MAX);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700238 }
Michael Kolbfe251992010-07-08 15:41:55 -0700239 }
240
241 @Override
Leon Scroggins571b3762010-05-26 10:25:01 -0400242 /* package */ void setDisplayTitle(String title) {
Michael Kolb21ce4d22010-09-15 14:55:05 -0700243 mUrlFocused.setText(title, false);
Michael Kolb513286f2010-09-09 12:55:12 -0700244 mUrlUnfocused.setText(title);
Leon Scroggins571b3762010-05-26 10:25:01 -0400245 }
246
Michael Kolb7b20ddd2010-10-14 15:03:28 -0700247 void setDisplayTitle(String title, boolean filter) {
248 mUrlFocused.setText(title, filter);
249 mUrlUnfocused.setText(title);
250 }
251
Leon Scroggins571b3762010-05-26 10:25:01 -0400252}