blob: c52c0749e61e384905d7d90c2ee5a7ab3f374116 [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 Kolbc7485ae2010-09-03 10:10:58 -0700104 }
105
106 public void onFocusChange(View v, boolean hasFocus) {
Michael Kolb513286f2010-09-09 12:55:12 -0700107 if (hasFocus) {
108 swapUrlContainer(true);
109 mUrlFocused.selectAll();
110 mUrlFocused.requestFocus();
111 mUrlFocused.setDropDownWidth(mUnfocusContainer.getWidth());
112 mUrlFocused.setDropDownHorizontalOffset(-mUrlFocused.getLeft());
113 }
Michael Kolbfe251992010-07-08 15:41:55 -0700114 }
115
Michael Kolba2b2ba82010-08-04 17:54:03 -0700116 @Override
117 public void onClick(View v) {
118 if (mBackButton == v) {
119 mBrowserActivity.getTopWindow().goBack();
120 } else if (mForwardButton == v) {
121 mBrowserActivity.getTopWindow().goForward();
122 } else if (mStar == v) {
Michael Kolbc7485ae2010-09-03 10:10:58 -0700123 mBrowserActivity.promptAddOrInstallBookmark(mStar);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700124 } else if (mAllButton == v) {
Michael Kolb68792c82010-08-09 16:39:18 -0700125 mBrowserActivity.bookmarksOrHistoryPicker(false, false);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700126 } else if (mSearchButton == v) {
127 search();
128 } else if (mStopButton == v) {
129 stopOrRefresh();
Michael Kolb513286f2010-09-09 12:55:12 -0700130 } else if (mGoButton == v) {
Michael Kolb1ce78132010-09-23 15:50:53 -0700131 if (!TextUtils.isEmpty(mUrlFocused.getText())) {
132 onAction(mUrlFocused.getText().toString());
133 }
Michael Kolbfe251992010-07-08 15:41:55 -0700134 }
135 }
136
Michael Kolb3f65c382010-08-20 15:31:16 -0700137 int getHeightWithoutProgress() {
138 return mContainer.getHeight();
139 }
140
Michael Kolba2b2ba82010-08-04 17:54:03 -0700141 @Override
142 void setFavicon(Bitmap icon) { }
Michael Kolbfe251992010-07-08 15:41:55 -0700143
144 // UrlInputListener implementation
145
146 @Override
147 public void onAction(String text) {
Michael Kolba2b2ba82010-08-04 17:54:03 -0700148 mBrowserActivity.getTabControl().getCurrentTopWebView().requestFocus();
149 mBrowserActivity.hideFakeTitleBar();
Michael Kolbfe251992010-07-08 15:41:55 -0700150 Intent i = new Intent();
151 i.setAction(Intent.ACTION_SEARCH);
152 i.putExtra(SearchManager.QUERY, text);
153 mBrowserActivity.onNewIntent(i);
Michael Kolb513286f2010-09-09 12:55:12 -0700154 swapUrlContainer(false);
155 setDisplayTitle(text);
Michael Kolbfe251992010-07-08 15:41:55 -0700156 }
157
158 @Override
159 public void onDismiss() {
Michael Kolba2b2ba82010-08-04 17:54:03 -0700160 mBrowserActivity.getTabControl().getCurrentTopWebView().requestFocus();
161 mBrowserActivity.hideFakeTitleBar();
Michael Kolb513286f2010-09-09 12:55:12 -0700162 setDisplayTitle(mBrowserActivity.getTabControl().getCurrentWebView().getUrl());
163 swapUrlContainer(false);
164 }
165
166 @Override
167 public void onEdit(String text) {
168 setDisplayTitle(text);
169 if (text != null) {
170 mUrlFocused.setSelection(text.length());
171 }
172 }
173
174 private void swapUrlContainer(boolean focus) {
175 mUnfocusContainer.setVisibility(focus ? View.GONE : View.VISIBLE);
176 mFocusContainer.setVisibility(focus ? View.VISIBLE : View.GONE);
Leon Scroggins571b3762010-05-26 10:25:01 -0400177 }
178
179 @Override
180 public void createContextMenu(ContextMenu menu) {
181 MenuInflater inflater = mBrowserActivity.getMenuInflater();
182 inflater.inflate(R.menu.title_context, menu);
183 mBrowserActivity.onCreateContextMenu(menu, this, null);
184 }
185
Michael Kolba2b2ba82010-08-04 17:54:03 -0700186 private void search() {
Michael Kolb513286f2010-09-09 12:55:12 -0700187 setDisplayTitle("");
188 mUrlUnfocused.requestFocus();
Michael Kolbfe251992010-07-08 15:41:55 -0700189 }
190
Michael Kolba2b2ba82010-08-04 17:54:03 -0700191 private void stopOrRefresh() {
192 if (mInLoad) {
193 mBrowserActivity.stopLoading();
194 } else {
195 mBrowserActivity.getTopWindow().reload();
196 }
Leon Scroggins571b3762010-05-26 10:25:01 -0400197 }
198
199 /**
Michael Kolbfe251992010-07-08 15:41:55 -0700200 * Update the progress, from 0 to 100.
Leon Scroggins571b3762010-05-26 10:25:01 -0400201 */
Michael Kolbfe251992010-07-08 15:41:55 -0700202 @Override
Michael Kolba2b2ba82010-08-04 17:54:03 -0700203 void setProgress(int newProgress) {
204 if (newProgress >= PROGRESS_MAX) {
205 mProgressView.setVisibility(View.GONE);
206 mInLoad = false;
207 mStopButton.setImageDrawable(mReloadDrawable);
208 } else {
209 if (!mInLoad) {
210 mProgressView.setVisibility(View.VISIBLE);
211 mInLoad = true;
212 mStopButton.setImageDrawable(mStopDrawable);
213 }
Michael Kolbc7485ae2010-09-03 10:10:58 -0700214 mProgressView.setProgress(newProgress*10000/PROGRESS_MAX);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700215 }
Michael Kolbfe251992010-07-08 15:41:55 -0700216 }
217
218 @Override
Leon Scroggins571b3762010-05-26 10:25:01 -0400219 /* package */ void setDisplayTitle(String title) {
Michael Kolb513286f2010-09-09 12:55:12 -0700220 mUrlFocused.setText(title);
221 mUrlUnfocused.setText(title);
Leon Scroggins571b3762010-05-26 10:25:01 -0400222 }
223
224}