blob: da39307f41650f9e20ac0cb70cb92e3c6d5fe105 [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 Kolbc7485ae2010-09-03 10:10:58 -070060 private PageProgressView mProgressView;
Michael Kolb513286f2010-09-09 12:55:12 -070061 private UrlInputView mUrlFocused;
62 private TextView mUrlUnfocused;
Michael Kolba2b2ba82010-08-04 17:54:03 -070063 private boolean mInLoad;
Michael Kolbfe251992010-07-08 15:41:55 -070064
Michael Kolba2b2ba82010-08-04 17:54:03 -070065 public TitleBarXLarge(BrowserActivity context) {
Leon Scroggins571b3762010-05-26 10:25:01 -040066 super(context);
Leon Scroggins571b3762010-05-26 10:25:01 -040067 mBrowserActivity = context;
Michael Kolbfe251992010-07-08 15:41:55 -070068 Resources resources = context.getResources();
Michael Kolbc7485ae2010-09-03 10:10:58 -070069 mStopDrawable = resources.getDrawable(R.drawable.ic_stop_normal);
70 mReloadDrawable = resources.getDrawable(R.drawable.ic_refresh_normal);
Michael Kolbfe251992010-07-08 15:41:55 -070071 rebuildLayout(context, true);
Michael Kolbfe251992010-07-08 15:41:55 -070072 }
Leon Scroggins571b3762010-05-26 10:25:01 -040073
Michael Kolbfe251992010-07-08 15:41:55 -070074 private void rebuildLayout(Context context, boolean rebuildData) {
Michael Kolbfe251992010-07-08 15:41:55 -070075 LayoutInflater factory = LayoutInflater.from(context);
Michael Kolba2b2ba82010-08-04 17:54:03 -070076 factory.inflate(R.layout.url_bar, this);
Michael Kolbfe251992010-07-08 15:41:55 -070077
Michael Kolb3f65c382010-08-20 15:31:16 -070078 mContainer = findViewById(R.id.taburlbar);
Michael Kolb513286f2010-09-09 12:55:12 -070079 mUrlFocused = (UrlInputView) findViewById(R.id.url_focused);
80 mUrlUnfocused = (TextView) findViewById(R.id.url_unfocused);
Michael Kolbfe251992010-07-08 15:41:55 -070081 mAllButton = findViewById(R.id.all_btn);
82 // TODO: Change enabled states based on whether you can go
Leon Scroggins571b3762010-05-26 10:25:01 -040083 // back/forward. Probably should be done inside onPageStarted.
84 mBackButton = findViewById(R.id.back);
85 mForwardButton = findViewById(R.id.forward);
86 mStar = findViewById(R.id.star);
Michael Kolba2b2ba82010-08-04 17:54:03 -070087 mStopButton = (ImageView) findViewById(R.id.stop);
88 mSearchButton = findViewById(R.id.search);
89 mLockIcon = (ImageView) findViewById(R.id.lock);
Michael Kolb513286f2010-09-09 12:55:12 -070090 mGoButton = findViewById(R.id.go);
Michael Kolbc7485ae2010-09-03 10:10:58 -070091 mProgressView = (PageProgressView) findViewById(R.id.progress);
Michael Kolb513286f2010-09-09 12:55:12 -070092 mFocusContainer = findViewById(R.id.urlbar_focused);
93 mUnfocusContainer = findViewById(R.id.urlbar_unfocused);
Michael Kolbfe251992010-07-08 15:41:55 -070094
Michael Kolba2b2ba82010-08-04 17:54:03 -070095 mBackButton.setOnClickListener(this);
96 mForwardButton.setOnClickListener(this);
97 mStar.setOnClickListener(this);
98 mAllButton.setOnClickListener(this);
99 mStopButton.setOnClickListener(this);
100 mSearchButton.setOnClickListener(this);
Michael Kolb513286f2010-09-09 12:55:12 -0700101 mGoButton.setOnClickListener(this);
102 mUrlFocused.setUrlInputListener(this);
103 mUrlUnfocused.setOnFocusChangeListener(this);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700104 mUrlFocused.setContainer(mFocusContainer);
Michael Kolbc7485ae2010-09-03 10:10:58 -0700105 }
106
107 public void onFocusChange(View v, boolean hasFocus) {
Michael Kolb513286f2010-09-09 12:55:12 -0700108 if (hasFocus) {
109 swapUrlContainer(true);
110 mUrlFocused.selectAll();
111 mUrlFocused.requestFocus();
112 mUrlFocused.setDropDownWidth(mUnfocusContainer.getWidth());
113 mUrlFocused.setDropDownHorizontalOffset(-mUrlFocused.getLeft());
114 }
Michael Kolbfe251992010-07-08 15:41:55 -0700115 }
116
Michael Kolba2b2ba82010-08-04 17:54:03 -0700117 @Override
118 public void onClick(View v) {
119 if (mBackButton == v) {
120 mBrowserActivity.getTopWindow().goBack();
121 } else if (mForwardButton == v) {
122 mBrowserActivity.getTopWindow().goForward();
123 } else if (mStar == v) {
Michael Kolbc7485ae2010-09-03 10:10:58 -0700124 mBrowserActivity.promptAddOrInstallBookmark(mStar);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700125 } else if (mAllButton == v) {
Michael Kolb68792c82010-08-09 16:39:18 -0700126 mBrowserActivity.bookmarksOrHistoryPicker(false, false);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700127 } else if (mSearchButton == v) {
128 search();
129 } else if (mStopButton == v) {
130 stopOrRefresh();
Michael Kolb513286f2010-09-09 12:55:12 -0700131 } else if (mGoButton == v) {
Michael Kolb1ce78132010-09-23 15:50:53 -0700132 if (!TextUtils.isEmpty(mUrlFocused.getText())) {
133 onAction(mUrlFocused.getText().toString());
134 }
Michael Kolbfe251992010-07-08 15:41:55 -0700135 }
136 }
137
Michael Kolb3f65c382010-08-20 15:31:16 -0700138 int getHeightWithoutProgress() {
139 return mContainer.getHeight();
140 }
141
Michael Kolba2b2ba82010-08-04 17:54:03 -0700142 @Override
143 void setFavicon(Bitmap icon) { }
Michael Kolbfe251992010-07-08 15:41:55 -0700144
145 // UrlInputListener implementation
146
147 @Override
148 public void onAction(String text) {
Michael Kolba2b2ba82010-08-04 17:54:03 -0700149 mBrowserActivity.getTabControl().getCurrentTopWebView().requestFocus();
150 mBrowserActivity.hideFakeTitleBar();
Michael Kolbfe251992010-07-08 15:41:55 -0700151 Intent i = new Intent();
152 i.setAction(Intent.ACTION_SEARCH);
153 i.putExtra(SearchManager.QUERY, text);
154 mBrowserActivity.onNewIntent(i);
Michael Kolb513286f2010-09-09 12:55:12 -0700155 swapUrlContainer(false);
156 setDisplayTitle(text);
Michael Kolbfe251992010-07-08 15:41:55 -0700157 }
158
159 @Override
160 public void onDismiss() {
Michael Kolba2b2ba82010-08-04 17:54:03 -0700161 mBrowserActivity.getTabControl().getCurrentTopWebView().requestFocus();
162 mBrowserActivity.hideFakeTitleBar();
Michael Kolb513286f2010-09-09 12:55:12 -0700163 setDisplayTitle(mBrowserActivity.getTabControl().getCurrentWebView().getUrl());
164 swapUrlContainer(false);
165 }
166
167 @Override
168 public void onEdit(String text) {
169 setDisplayTitle(text);
170 if (text != null) {
171 mUrlFocused.setSelection(text.length());
172 }
173 }
174
175 private void swapUrlContainer(boolean focus) {
176 mUnfocusContainer.setVisibility(focus ? View.GONE : View.VISIBLE);
177 mFocusContainer.setVisibility(focus ? View.VISIBLE : View.GONE);
Leon Scroggins571b3762010-05-26 10:25:01 -0400178 }
179
180 @Override
181 public void createContextMenu(ContextMenu menu) {
182 MenuInflater inflater = mBrowserActivity.getMenuInflater();
183 inflater.inflate(R.menu.title_context, menu);
184 mBrowserActivity.onCreateContextMenu(menu, this, null);
185 }
186
Michael Kolba2b2ba82010-08-04 17:54:03 -0700187 private void search() {
Michael Kolb513286f2010-09-09 12:55:12 -0700188 setDisplayTitle("");
189 mUrlUnfocused.requestFocus();
Michael Kolbfe251992010-07-08 15:41:55 -0700190 }
191
Michael Kolba2b2ba82010-08-04 17:54:03 -0700192 private void stopOrRefresh() {
193 if (mInLoad) {
194 mBrowserActivity.stopLoading();
195 } else {
196 mBrowserActivity.getTopWindow().reload();
197 }
Leon Scroggins571b3762010-05-26 10:25:01 -0400198 }
199
200 /**
Michael Kolbfe251992010-07-08 15:41:55 -0700201 * Update the progress, from 0 to 100.
Leon Scroggins571b3762010-05-26 10:25:01 -0400202 */
Michael Kolbfe251992010-07-08 15:41:55 -0700203 @Override
Michael Kolba2b2ba82010-08-04 17:54:03 -0700204 void setProgress(int newProgress) {
205 if (newProgress >= PROGRESS_MAX) {
206 mProgressView.setVisibility(View.GONE);
207 mInLoad = false;
208 mStopButton.setImageDrawable(mReloadDrawable);
209 } else {
210 if (!mInLoad) {
211 mProgressView.setVisibility(View.VISIBLE);
212 mInLoad = true;
213 mStopButton.setImageDrawable(mStopDrawable);
214 }
Michael Kolbc7485ae2010-09-03 10:10:58 -0700215 mProgressView.setProgress(newProgress*10000/PROGRESS_MAX);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700216 }
Michael Kolbfe251992010-07-08 15:41:55 -0700217 }
218
219 @Override
Leon Scroggins571b3762010-05-26 10:25:01 -0400220 /* package */ void setDisplayTitle(String title) {
Michael Kolb21ce4d22010-09-15 14:55:05 -0700221 mUrlFocused.setText(title, false);
Michael Kolb513286f2010-09-09 12:55:12 -0700222 mUrlUnfocused.setText(title);
Leon Scroggins571b3762010-05-26 10:25:01 -0400223 }
224
225}