blob: 169f9348dfb377746d6209d3ed21a8dcec52279f [file] [log] [blame]
Michael Kolba2b2ba82010-08-04 17:54:03 -07001/*
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 Kolbebba8b42010-09-30 12:57:59 -070019import com.android.browser.ScrollWebView.ScrollListener;
Michael Kolbebba8b42010-09-30 12:57:59 -070020
Michael Kolb8233fac2010-10-26 16:08:53 -070021import android.app.Activity;
Michael Kolba2b2ba82010-08-04 17:54:03 -070022import android.content.Context;
23import android.content.res.Resources;
24import android.graphics.Bitmap;
25import android.graphics.Color;
26import android.graphics.drawable.BitmapDrawable;
27import android.graphics.drawable.Drawable;
28import android.graphics.drawable.LayerDrawable;
29import android.graphics.drawable.PaintDrawable;
30import android.view.ContextMenu;
Michael Kolbed217742010-08-10 17:52:34 -070031import android.view.Gravity;
Michael Kolba2b2ba82010-08-04 17:54:03 -070032import android.view.LayoutInflater;
33import android.view.MenuInflater;
34import android.view.View;
35import android.view.View.OnClickListener;
36import android.webkit.WebView;
Michael Kolbed217742010-08-10 17:52:34 -070037import android.widget.ImageButton;
Michael Kolba2b2ba82010-08-04 17:54:03 -070038import android.widget.ImageView;
39import android.widget.LinearLayout;
40import android.widget.TextView;
41
Michael Kolba2b2ba82010-08-04 17:54:03 -070042import java.util.HashMap;
43import java.util.Map;
44
45/**
46 * tabbed title bar for xlarge screen browser
47 */
48public class TabBar extends LinearLayout
Michael Kolb8233fac2010-10-26 16:08:53 -070049 implements ScrollListener, OnClickListener {
Michael Kolba2b2ba82010-08-04 17:54:03 -070050
51 private static final int PROGRESS_MAX = 100;
52
Michael Kolb8233fac2010-10-26 16:08:53 -070053 private Activity mActivity;
54 private UiController mUiController;
55 private TabControl mTabControl;
56 private BaseUi mUi;
Michael Kolba2b2ba82010-08-04 17:54:03 -070057
Michael Kolbed217742010-08-10 17:52:34 -070058 private final int mTabWidthSelected;
59 private final int mTabWidthUnselected;
60
Michael Kolba2b2ba82010-08-04 17:54:03 -070061 private TabScrollView mTabs;
Michael Kolb8233fac2010-10-26 16:08:53 -070062
Michael Kolbebba8b42010-09-30 12:57:59 -070063 private ImageButton mNewTab;
64 private int mButtonWidth;
Michael Kolba2b2ba82010-08-04 17:54:03 -070065
66 private Map<Tab, TabViewData> mTabMap;
67
Michael Kolba2b2ba82010-08-04 17:54:03 -070068 private boolean mUserRequestedUrlbar;
69 private boolean mTitleVisible;
Michael Kolbed217742010-08-10 17:52:34 -070070 private boolean mShowUrlMode;
John Reckfb3017f2010-10-26 19:01:24 -070071 private boolean mHasReceivedTitle;
Michael Kolba2b2ba82010-08-04 17:54:03 -070072
John Reckaff60fb2010-10-25 13:47:58 -070073 private Drawable mGenericFavicon;
John Reckfb3017f2010-10-26 19:01:24 -070074 private String mLoadingText;
John Reckaff60fb2010-10-25 13:47:58 -070075
Michael Kolb8233fac2010-10-26 16:08:53 -070076 public TabBar(Activity activity, UiController controller, BaseUi ui) {
77 super(activity);
78 mActivity = activity;
79 mUiController = controller;
80 mTabControl = mUiController.getTabControl();
81 mUi = ui;
82 Resources res = activity.getResources();
Michael Kolbed217742010-08-10 17:52:34 -070083 mTabWidthSelected = (int) res.getDimension(R.dimen.tab_width_selected);
84 mTabWidthUnselected = (int) res.getDimension(R.dimen.tab_width_unselected);
Michael Kolba2b2ba82010-08-04 17:54:03 -070085
Michael Kolba2b2ba82010-08-04 17:54:03 -070086 mTabMap = new HashMap<Tab, TabViewData>();
Michael Kolb8233fac2010-10-26 16:08:53 -070087 Resources resources = activity.getResources();
88 LayoutInflater factory = LayoutInflater.from(activity);
Michael Kolba2b2ba82010-08-04 17:54:03 -070089 factory.inflate(R.layout.tab_bar, this);
90 mTabs = (TabScrollView) findViewById(R.id.tabs);
Michael Kolbebba8b42010-09-30 12:57:59 -070091 mNewTab = (ImageButton) findViewById(R.id.newtab);
92 mNewTab.setOnClickListener(this);
John Reckaff60fb2010-10-25 13:47:58 -070093 mGenericFavicon = res.getDrawable(R.drawable.app_web_browser_sm);
John Reckfb3017f2010-10-26 19:01:24 -070094 mLoadingText = res.getString(R.string.title_bar_loading);
Michael Kolba2b2ba82010-08-04 17:54:03 -070095
96 // TODO: Change enabled states based on whether you can go
97 // back/forward. Probably should be done inside onPageStarted.
98
Michael Kolba2b2ba82010-08-04 17:54:03 -070099 // build tabs
Michael Kolb8233fac2010-10-26 16:08:53 -0700100 int tabcount = mTabControl.getTabCount();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700101 for (int i = 0; i < tabcount; i++) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700102 Tab tab = mTabControl.getTab(i);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700103 TabViewData data = buildTab(tab);
104 TabView tv = buildView(data);
105 }
Michael Kolb8233fac2010-10-26 16:08:53 -0700106 mTabs.setSelectedTab(mTabControl.getCurrentIndex());
Michael Kolba2b2ba82010-08-04 17:54:03 -0700107
Michael Kolba2b2ba82010-08-04 17:54:03 -0700108 mUserRequestedUrlbar = false;
109 mTitleVisible = true;
Michael Kolbebba8b42010-09-30 12:57:59 -0700110 mButtonWidth = -1;
111 }
112
113 @Override
114 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
115 if (mButtonWidth == -1) {
116 mButtonWidth = mNewTab.getMeasuredWidth();
117 }
118 int sw = mTabs.getMeasuredWidth();
119 int w = right-left;
120 if (w-sw < mButtonWidth) {
121 sw = w - mButtonWidth;
122 }
123 mTabs.layout(0, 0, sw, bottom-top );
124 mNewTab.layout(sw, 0, sw+mButtonWidth, bottom-top);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700125 }
126
127 public void onClick(View view) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700128 mUi.hideComboView();
Michael Kolbebba8b42010-09-30 12:57:59 -0700129 if (mNewTab == view) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700130 mUiController.openTabToHomePage();
Michael Kolbebba8b42010-09-30 12:57:59 -0700131 } else if (mTabs.getSelectedTab() == view) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700132 if (mUi.isFakeTitleBarShowing() && !isLoading()) {
133 mUi.hideFakeTitleBar();
Michael Kolbed217742010-08-10 17:52:34 -0700134 } else {
135 showUrlBar();
136 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700137 } else {
Michael Kolbebba8b42010-09-30 12:57:59 -0700138 int ix = mTabs.getChildIndex(view);
139 if (ix >= 0) {
140 mTabs.setSelectedTab(ix);
Michael Kolb8233fac2010-10-26 16:08:53 -0700141 mUiController.switchToTab(ix);
Michael Kolbebba8b42010-09-30 12:57:59 -0700142 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700143 }
144 }
145
Michael Kolbed217742010-08-10 17:52:34 -0700146 private void showUrlBar() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700147 mUi.stopWebViewScrolling();
148 mUi.showFakeTitleBar();
Michael Kolbed217742010-08-10 17:52:34 -0700149 mUserRequestedUrlbar = true;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700150 }
151
Michael Kolbed217742010-08-10 17:52:34 -0700152 private void setShowUrlMode(boolean showUrl) {
153 mShowUrlMode = showUrl;
Michael Kolbed217742010-08-10 17:52:34 -0700154 }
155
156 // callback after fake titlebar is shown
157 void onShowTitleBar() {
158 setShowUrlMode(false);
159 }
160
161 // callback after fake titlebar is hidden
Michael Kolba2b2ba82010-08-04 17:54:03 -0700162 void onHideTitleBar() {
Michael Kolbed217742010-08-10 17:52:34 -0700163 setShowUrlMode(!mTitleVisible);
Michael Kolb8233fac2010-10-26 16:08:53 -0700164 Tab tab = mTabControl.getCurrentTab();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700165 tab.getWebView().requestFocus();
166 mUserRequestedUrlbar = false;
167 }
168
Michael Kolbed217742010-08-10 17:52:34 -0700169 // webview scroll listener
170
171 @Override
172 public void onScroll(boolean titleVisible) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700173 // isLoading is using the current tab, which initially might not be set yet
174 if (mTabControl.getCurrentTab() != null) {
175 mTitleVisible = titleVisible;
176 if (!mShowUrlMode && !mTitleVisible && !isLoading()) {
177 if (mUserRequestedUrlbar) {
178 mUi.hideFakeTitleBar();
179 } else {
180 setShowUrlMode(true);
181 }
182 } else if (mTitleVisible && !isLoading()) {
183 if (mShowUrlMode) {
184 setShowUrlMode(false);
185 }
Michael Kolbed217742010-08-10 17:52:34 -0700186 }
187 }
188 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700189
190 @Override
191 public void createContextMenu(ContextMenu menu) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700192 MenuInflater inflater = mActivity.getMenuInflater();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700193 inflater.inflate(R.menu.title_context, menu);
Michael Kolb8233fac2010-10-26 16:08:53 -0700194 mActivity.onCreateContextMenu(menu, this, null);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700195 }
196
197 private TabViewData buildTab(Tab tab) {
198 TabViewData data = new TabViewData(tab);
199 mTabMap.put(tab, data);
200 return data;
201 }
202
203 private TabView buildView(final TabViewData data) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700204 TabView tv = new TabView(mActivity, data);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700205 tv.setTag(data);
206 tv.setOnClickListener(this);
207 mTabs.addTab(tv);
208 return tv;
209 }
210
211 /**
212 * View used in the tab bar
213 */
214 class TabView extends LinearLayout implements OnClickListener {
215
216 TabViewData mTabData;
217 View mTabContent;
218 TextView mTitle;
Michael Kolbae62fd42010-08-18 16:33:28 -0700219 View mIncognito;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700220 ImageView mIconView;
221 ImageView mLock;
222 ImageView mClose;
223 boolean mSelected;
224 boolean mInLoad;
225
226 /**
227 * @param context
228 */
229 public TabView(Context context, TabViewData tab) {
230 super(context);
231 mTabData = tab;
Michael Kolbed217742010-08-10 17:52:34 -0700232 setGravity(Gravity.CENTER_VERTICAL);
233 setOrientation(LinearLayout.HORIZONTAL);
234 setBackgroundResource(R.drawable.tab_background);
Bjorn Bringertb1402a52010-10-12 10:53:12 +0100235 LayoutInflater inflater = LayoutInflater.from(getContext());
Michael Kolbed217742010-08-10 17:52:34 -0700236 mTabContent = inflater.inflate(R.layout.tab_title, this, true);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700237 mTitle = (TextView) mTabContent.findViewById(R.id.title);
238 mIconView = (ImageView) mTabContent.findViewById(R.id.favicon);
239 mLock = (ImageView) mTabContent.findViewById(R.id.lock);
240 mClose = (ImageView) mTabContent.findViewById(R.id.close);
241 mClose.setOnClickListener(this);
Michael Kolbae62fd42010-08-18 16:33:28 -0700242 mIncognito = mTabContent.findViewById(R.id.incognito);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700243 mSelected = false;
244 mInLoad = false;
245 // update the status
246 updateFromData();
247 }
248
249 @Override
250 public void onClick(View v) {
251 if (v == mClose) {
252 closeTab();
253 }
254 }
255
256 private void updateFromData() {
257 mTabData.mTabView = this;
258 if (mTabData.mUrl != null) {
259 setDisplayTitle(mTabData.mUrl);
260 }
261 if (mTabData.mTitle != null) {
262 setDisplayTitle(mTabData.mTitle);
263 }
264 setProgress(mTabData.mProgress);
265 if (mTabData.mIcon != null) {
266 setFavicon(mTabData.mIcon);
267 }
268 if (mTabData.mLock != null) {
269 setLock(mTabData.mLock);
270 }
Michael Kolbae62fd42010-08-18 16:33:28 -0700271 if (mTabData.mTab != null) {
272 mIncognito.setVisibility(
273 mTabData.mTab.getWebView().isPrivateBrowsingEnabled() ?
274 View.VISIBLE : View.GONE);
275 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700276 }
277
278 @Override
Michael Kolbb7b115e2010-09-25 16:59:37 -0700279 public void setActivated(boolean selected) {
Michael Kolba2b2ba82010-08-04 17:54:03 -0700280 mSelected = selected;
281 mClose.setVisibility(mSelected ? View.VISIBLE : View.GONE);
Michael Kolb8233fac2010-10-26 16:08:53 -0700282 mTitle.setTextAppearance(mActivity, mSelected ?
Michael Kolbc7485ae2010-09-03 10:10:58 -0700283 R.style.TabTitleSelected : R.style.TabTitleUnselected);
284 setHorizontalFadingEdgeEnabled(!mSelected);
285 setFadingEdgeLength(50);
Michael Kolbb7b115e2010-09-25 16:59:37 -0700286 super.setActivated(selected);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700287 setLayoutParams(new LayoutParams(selected ?
Michael Kolbed217742010-08-10 17:52:34 -0700288 mTabWidthSelected : mTabWidthUnselected,
Michael Kolba2b2ba82010-08-04 17:54:03 -0700289 LayoutParams.MATCH_PARENT));
290 }
291
292 void setDisplayTitle(String title) {
293 mTitle.setText(title);
294 }
295
296 void setFavicon(Drawable d) {
297 mIconView.setImageDrawable(d);
298 }
299
300 void setLock(Drawable d) {
301 if (null == d) {
302 mLock.setVisibility(View.GONE);
303 } else {
304 mLock.setImageDrawable(d);
305 mLock.setVisibility(View.VISIBLE);
306 }
307 }
308
Michael Kolba2b2ba82010-08-04 17:54:03 -0700309 void setProgress(int newProgress) {
310 if (newProgress >= PROGRESS_MAX) {
311 mInLoad = false;
312 } else {
313 if (!mInLoad && getWindowToken() != null) {
314 mInLoad = true;
315 }
316 }
317 }
318
319 private void closeTab() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700320 if (mTabData.mTab == mTabControl.getCurrentTab()) {
321 mUiController.closeCurrentTab();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700322 } else {
Michael Kolb8233fac2010-10-26 16:08:53 -0700323 mUiController.closeTab(mTabData.mTab);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700324 }
325 }
326
327 }
328
329 /**
330 * Store tab state within the title bar
331 */
332 class TabViewData {
333
334 Tab mTab;
335 TabView mTabView;
336 int mProgress;
337 Drawable mIcon;
338 Drawable mLock;
339 String mTitle;
340 String mUrl;
341
342 TabViewData(Tab tab) {
343 mTab = tab;
John Reckd8657622010-10-25 16:20:40 -0700344 WebView web = tab.getWebView();
345 if (web != null) {
346 setUrlAndTitle(web.getUrl(), web.getTitle());
347 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700348 }
349
350 void setUrlAndTitle(String url, String title) {
351 mUrl = url;
352 mTitle = title;
353 if (mTabView != null) {
354 if (title != null) {
355 mTabView.setDisplayTitle(title);
356 } else if (url != null) {
John Reckfb3017f2010-10-26 19:01:24 -0700357 mTabView.setDisplayTitle(UrlUtils.stripUrl(url));
Michael Kolba2b2ba82010-08-04 17:54:03 -0700358 }
359 }
360 }
361
362 void setProgress(int newProgress) {
363 mProgress = newProgress;
364 if (mTabView != null) {
365 mTabView.setProgress(mProgress);
366 }
367 }
368
369 void setFavicon(Bitmap icon) {
370 Drawable[] array = new Drawable[3];
371 array[0] = new PaintDrawable(Color.BLACK);
372 array[1] = new PaintDrawable(Color.WHITE);
373 if (icon == null) {
John Reckaff60fb2010-10-25 13:47:58 -0700374 array[2] = mGenericFavicon;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700375 } else {
376 array[2] = new BitmapDrawable(icon);
377 }
378 LayerDrawable d = new LayerDrawable(array);
379 d.setLayerInset(1, 1, 1, 1, 1);
380 d.setLayerInset(2, 2, 2, 2, 2);
381 mIcon = d;
382 if (mTabView != null) {
383 mTabView.setFavicon(mIcon);
384 }
385 }
386
387 }
388
389 // TabChangeListener implementation
390
Michael Kolb8233fac2010-10-26 16:08:53 -0700391 public void onSetActiveTab(Tab tab) {
392 mTabs.setSelectedTab(mTabControl.getTabIndex(tab));
Michael Kolba2b2ba82010-08-04 17:54:03 -0700393 TabViewData tvd = mTabMap.get(tab);
394 if (tvd != null) {
395 tvd.setProgress(tvd.mProgress);
396 // update the scroll state
397 WebView webview = tab.getWebView();
398 onScroll(webview.getVisibleTitleHeight() > 0);
399 }
400 }
401
Michael Kolba2b2ba82010-08-04 17:54:03 -0700402 public void onFavicon(Tab tab, Bitmap favicon) {
403 TabViewData tvd = mTabMap.get(tab);
404 if (tvd != null) {
405 tvd.setFavicon(favicon);
406 }
407 }
408
Michael Kolba2b2ba82010-08-04 17:54:03 -0700409 public void onNewTab(Tab tab) {
410 TabViewData tvd = buildTab(tab);
411 buildView(tvd);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700412 }
413
Michael Kolba2b2ba82010-08-04 17:54:03 -0700414 public void onProgress(Tab tab, int progress) {
415 TabViewData tvd = mTabMap.get(tab);
416 if (tvd != null) {
417 tvd.setProgress(progress);
418 }
419 }
420
Michael Kolba2b2ba82010-08-04 17:54:03 -0700421 public void onRemoveTab(Tab tab) {
422 TabViewData tvd = mTabMap.get(tab);
Michael Kolbe2752f72010-10-25 16:50:16 -0700423 if (tvd != null) {
424 TabView tv = tvd.mTabView;
425 if (tv != null) {
426 mTabs.removeTab(tv);
427 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700428 }
429 mTabMap.remove(tab);
430 }
431
Michael Kolba2b2ba82010-08-04 17:54:03 -0700432 public void onUrlAndTitle(Tab tab, String url, String title) {
John Reckfb3017f2010-10-26 19:01:24 -0700433 mHasReceivedTitle = true;
Michael Kolba2b2ba82010-08-04 17:54:03 -0700434 TabViewData tvd = mTabMap.get(tab);
435 if (tvd != null) {
436 tvd.setUrlAndTitle(url, title);
437 }
438 }
439
Michael Kolba2b2ba82010-08-04 17:54:03 -0700440 public void onPageFinished(Tab tab) {
John Reckfb3017f2010-10-26 19:01:24 -0700441 if (!mHasReceivedTitle) {
442 TabViewData tvd = mTabMap.get(tab);
443 if (tvd != null) {
444 tvd.setUrlAndTitle(tvd.mUrl, null);
445 }
446 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700447 }
448
John Reckfb3017f2010-10-26 19:01:24 -0700449 public void onPageStarted(Tab tab, String url, Bitmap favicon) {
450 mHasReceivedTitle = false;
John Reckaff60fb2010-10-25 13:47:58 -0700451 TabViewData tvd = mTabMap.get(tab);
452 if (tvd != null) {
Michael Kolb8233fac2010-10-26 16:08:53 -0700453 tvd.setUrlAndTitle(url, null);
John Reckaff60fb2010-10-25 13:47:58 -0700454 tvd.setFavicon(favicon);
John Reckfb3017f2010-10-26 19:01:24 -0700455 tvd.setUrlAndTitle(url, mLoadingText);
John Reckaff60fb2010-10-25 13:47:58 -0700456 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700457 }
458
Michael Kolba2b2ba82010-08-04 17:54:03 -0700459 private boolean isLoading() {
Michael Kolb8233fac2010-10-26 16:08:53 -0700460 TabViewData tvd = mTabMap.get(mTabControl.getCurrentTab());
461 if ((tvd != null) && (tvd.mTabView != null)) {
462 return tvd.mTabView.mInLoad;
463 } else {
464 return false;
465 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700466 }
467
Michael Kolba2b2ba82010-08-04 17:54:03 -0700468}