blob: 3408108e6378a978d4f8eafd4565494b26b281c5 [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 Kolb513286f2010-09-09 12:55:12 -070027import android.util.Log;
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) {
131 onAction(mUrlFocused.getText().toString());
Michael Kolbfe251992010-07-08 15:41:55 -0700132 }
133 }
134
Michael Kolb3f65c382010-08-20 15:31:16 -0700135 int getHeightWithoutProgress() {
136 return mContainer.getHeight();
137 }
138
Michael Kolba2b2ba82010-08-04 17:54:03 -0700139 @Override
140 void setFavicon(Bitmap icon) { }
Michael Kolbfe251992010-07-08 15:41:55 -0700141
142 // UrlInputListener implementation
143
144 @Override
145 public void onAction(String text) {
Michael Kolba2b2ba82010-08-04 17:54:03 -0700146 mBrowserActivity.getTabControl().getCurrentTopWebView().requestFocus();
147 mBrowserActivity.hideFakeTitleBar();
Michael Kolbfe251992010-07-08 15:41:55 -0700148 Intent i = new Intent();
149 i.setAction(Intent.ACTION_SEARCH);
150 i.putExtra(SearchManager.QUERY, text);
151 mBrowserActivity.onNewIntent(i);
Michael Kolb513286f2010-09-09 12:55:12 -0700152 swapUrlContainer(false);
153 setDisplayTitle(text);
Michael Kolbfe251992010-07-08 15:41:55 -0700154 }
155
156 @Override
157 public void onDismiss() {
Michael Kolba2b2ba82010-08-04 17:54:03 -0700158 mBrowserActivity.getTabControl().getCurrentTopWebView().requestFocus();
159 mBrowserActivity.hideFakeTitleBar();
Michael Kolb513286f2010-09-09 12:55:12 -0700160 setDisplayTitle(mBrowserActivity.getTabControl().getCurrentWebView().getUrl());
161 swapUrlContainer(false);
162 }
163
164 @Override
165 public void onEdit(String text) {
166 setDisplayTitle(text);
167 if (text != null) {
168 mUrlFocused.setSelection(text.length());
169 }
170 }
171
172 private void swapUrlContainer(boolean focus) {
173 mUnfocusContainer.setVisibility(focus ? View.GONE : View.VISIBLE);
174 mFocusContainer.setVisibility(focus ? View.VISIBLE : View.GONE);
Leon Scroggins571b3762010-05-26 10:25:01 -0400175 }
176
177 @Override
178 public void createContextMenu(ContextMenu menu) {
179 MenuInflater inflater = mBrowserActivity.getMenuInflater();
180 inflater.inflate(R.menu.title_context, menu);
181 mBrowserActivity.onCreateContextMenu(menu, this, null);
182 }
183
Michael Kolba2b2ba82010-08-04 17:54:03 -0700184 private void search() {
Michael Kolb513286f2010-09-09 12:55:12 -0700185 setDisplayTitle("");
186 mUrlUnfocused.requestFocus();
Michael Kolbfe251992010-07-08 15:41:55 -0700187 }
188
Michael Kolba2b2ba82010-08-04 17:54:03 -0700189 private void stopOrRefresh() {
190 if (mInLoad) {
191 mBrowserActivity.stopLoading();
192 } else {
193 mBrowserActivity.getTopWindow().reload();
194 }
Leon Scroggins571b3762010-05-26 10:25:01 -0400195 }
196
197 /**
Michael Kolbfe251992010-07-08 15:41:55 -0700198 * Update the progress, from 0 to 100.
Leon Scroggins571b3762010-05-26 10:25:01 -0400199 */
Michael Kolbfe251992010-07-08 15:41:55 -0700200 @Override
Michael Kolba2b2ba82010-08-04 17:54:03 -0700201 void setProgress(int newProgress) {
202 if (newProgress >= PROGRESS_MAX) {
203 mProgressView.setVisibility(View.GONE);
204 mInLoad = false;
205 mStopButton.setImageDrawable(mReloadDrawable);
206 } else {
207 if (!mInLoad) {
208 mProgressView.setVisibility(View.VISIBLE);
209 mInLoad = true;
210 mStopButton.setImageDrawable(mStopDrawable);
211 }
Michael Kolbc7485ae2010-09-03 10:10:58 -0700212 mProgressView.setProgress(newProgress*10000/PROGRESS_MAX);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700213 }
Michael Kolbfe251992010-07-08 15:41:55 -0700214 }
215
216 @Override
Leon Scroggins571b3762010-05-26 10:25:01 -0400217 /* package */ void setDisplayTitle(String title) {
Michael Kolb513286f2010-09-09 12:55:12 -0700218 mUrlFocused.setText(title);
219 mUrlUnfocused.setText(title);
Leon Scroggins571b3762010-05-26 10:25:01 -0400220 }
221
222}