blob: b652e15caa735abcf6f5b0e3d5e6d02c4545fa89 [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 Kolbc7485ae2010-09-03 10:10:58 -0700108 }
Michael Kolbb7b115e2010-09-25 16:59:37 -0700109
Michael Kolbc7485ae2010-09-03 10:10:58 -0700110 public void onFocusChange(View v, boolean hasFocus) {
Michael Kolb513286f2010-09-09 12:55:12 -0700111 if (hasFocus) {
Michael Kolbb7b115e2010-09-25 16:59:37 -0700112 setUrlMode(true);
Michael Kolb513286f2010-09-09 12:55:12 -0700113 mUrlFocused.selectAll();
114 mUrlFocused.requestFocus();
115 mUrlFocused.setDropDownWidth(mUnfocusContainer.getWidth());
116 mUrlFocused.setDropDownHorizontalOffset(-mUrlFocused.getLeft());
117 }
Michael Kolbfe251992010-07-08 15:41:55 -0700118 }
119
Michael Kolba2b2ba82010-08-04 17:54:03 -0700120 @Override
121 public void onClick(View v) {
122 if (mBackButton == v) {
123 mBrowserActivity.getTopWindow().goBack();
124 } else if (mForwardButton == v) {
125 mBrowserActivity.getTopWindow().goForward();
126 } else if (mStar == v) {
Michael Kolbc7485ae2010-09-03 10:10:58 -0700127 mBrowserActivity.promptAddOrInstallBookmark(mStar);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700128 } else if (mAllButton == v) {
Michael Kolb68792c82010-08-09 16:39:18 -0700129 mBrowserActivity.bookmarksOrHistoryPicker(false, false);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700130 } else if (mSearchButton == v) {
131 search();
132 } else if (mStopButton == v) {
133 stopOrRefresh();
Michael Kolb513286f2010-09-09 12:55:12 -0700134 } else if (mGoButton == v) {
Michael Kolb1ce78132010-09-23 15:50:53 -0700135 if (!TextUtils.isEmpty(mUrlFocused.getText())) {
136 onAction(mUrlFocused.getText().toString());
137 }
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
153 public void onAction(String text) {
Michael Kolba2b2ba82010-08-04 17:54:03 -0700154 mBrowserActivity.getTabControl().getCurrentTopWebView().requestFocus();
155 mBrowserActivity.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);
159 mBrowserActivity.onNewIntent(i);
Michael Kolbb7b115e2010-09-25 16:59:37 -0700160 setUrlMode(false);
Michael Kolb513286f2010-09-09 12:55:12 -0700161 setDisplayTitle(text);
Michael Kolbfe251992010-07-08 15:41:55 -0700162 }
163
164 @Override
165 public void onDismiss() {
Michael Kolba2b2ba82010-08-04 17:54:03 -0700166 mBrowserActivity.getTabControl().getCurrentTopWebView().requestFocus();
167 mBrowserActivity.hideFakeTitleBar();
Michael Kolbb7b115e2010-09-25 16:59:37 -0700168 setUrlMode(false);
Michael Kolb513286f2010-09-09 12:55:12 -0700169 setDisplayTitle(mBrowserActivity.getTabControl().getCurrentWebView().getUrl());
Michael Kolb513286f2010-09-09 12:55:12 -0700170 }
171
172 @Override
173 public void onEdit(String text) {
174 setDisplayTitle(text);
175 if (text != null) {
176 mUrlFocused.setSelection(text.length());
177 }
178 }
179
Michael Kolbb7b115e2010-09-25 16:59:37 -0700180 private void setUrlMode(boolean focused) {
181 swapUrlContainer(focused);
182 if (focused) {
183 mSearchButton.setVisibility(View.GONE);
184 mGoButton.setVisibility(View.VISIBLE);
185 } else {
186 mSearchButton.setVisibility(View.VISIBLE);
187 mGoButton.setVisibility(View.GONE);
188 }
189 }
190
Michael Kolb513286f2010-09-09 12:55:12 -0700191 private void swapUrlContainer(boolean focus) {
192 mUnfocusContainer.setVisibility(focus ? View.GONE : View.VISIBLE);
193 mFocusContainer.setVisibility(focus ? View.VISIBLE : View.GONE);
Leon Scroggins571b3762010-05-26 10:25:01 -0400194 }
195
196 @Override
197 public void createContextMenu(ContextMenu menu) {
198 MenuInflater inflater = mBrowserActivity.getMenuInflater();
199 inflater.inflate(R.menu.title_context, menu);
200 mBrowserActivity.onCreateContextMenu(menu, this, null);
201 }
202
Michael Kolba2b2ba82010-08-04 17:54:03 -0700203 private void search() {
Michael Kolb513286f2010-09-09 12:55:12 -0700204 setDisplayTitle("");
205 mUrlUnfocused.requestFocus();
Michael Kolbfe251992010-07-08 15:41:55 -0700206 }
207
Michael Kolba2b2ba82010-08-04 17:54:03 -0700208 private void stopOrRefresh() {
209 if (mInLoad) {
210 mBrowserActivity.stopLoading();
211 } else {
212 mBrowserActivity.getTopWindow().reload();
213 }
Leon Scroggins571b3762010-05-26 10:25:01 -0400214 }
215
216 /**
Michael Kolbfe251992010-07-08 15:41:55 -0700217 * Update the progress, from 0 to 100.
Leon Scroggins571b3762010-05-26 10:25:01 -0400218 */
Michael Kolbfe251992010-07-08 15:41:55 -0700219 @Override
Michael Kolba2b2ba82010-08-04 17:54:03 -0700220 void setProgress(int newProgress) {
221 if (newProgress >= PROGRESS_MAX) {
Michael Kolbb7b115e2010-09-25 16:59:37 -0700222 mProgressView.setProgress(PageProgressView.MAX_PROGRESS);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700223 mProgressView.setVisibility(View.GONE);
224 mInLoad = false;
225 mStopButton.setImageDrawable(mReloadDrawable);
226 } else {
227 if (!mInLoad) {
228 mProgressView.setVisibility(View.VISIBLE);
229 mInLoad = true;
230 mStopButton.setImageDrawable(mStopDrawable);
231 }
Michael Kolbb7b115e2010-09-25 16:59:37 -0700232 mProgressView.setProgress(newProgress * PageProgressView.MAX_PROGRESS
233 / PROGRESS_MAX);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700234 }
Michael Kolbfe251992010-07-08 15:41:55 -0700235 }
236
237 @Override
Leon Scroggins571b3762010-05-26 10:25:01 -0400238 /* package */ void setDisplayTitle(String title) {
Michael Kolb21ce4d22010-09-15 14:55:05 -0700239 mUrlFocused.setText(title, false);
Michael Kolb513286f2010-09-09 12:55:12 -0700240 mUrlUnfocused.setText(title);
Leon Scroggins571b3762010-05-26 10:25:01 -0400241 }
242
243}