blob: 857aa38be4996c31761395e79140d42c420cb84a [file] [log] [blame]
Leon Scroggins81db3662009-06-04 17:45:11 -04001/*
2 * Copyright (C) 2009 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
19import android.content.Context;
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -040020import android.content.res.Resources;
21import android.graphics.Bitmap;
22import android.graphics.Color;
Leon Scroggins1f005d32009-08-10 17:36:42 -040023import android.graphics.Rect;
Leon Scroggins62e8f942009-09-03 15:08:54 -040024import android.graphics.drawable.Animatable;
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -040025import android.graphics.drawable.BitmapDrawable;
Leon Scroggins81db3662009-06-04 17:45:11 -040026import android.graphics.drawable.Drawable;
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -040027import android.graphics.drawable.LayerDrawable;
28import android.graphics.drawable.PaintDrawable;
29import android.util.TypedValue;
Leon Scrogginse4b3bda2009-06-09 15:46:41 -040030import android.view.LayoutInflater;
31import android.view.View;
Leon Scroggins81db3662009-06-04 17:45:11 -040032import android.webkit.WebView;
33import android.widget.ImageView;
34import android.widget.LinearLayout;
35import android.widget.ProgressBar;
36import android.widget.TextView;
Leon Scroggins81db3662009-06-04 17:45:11 -040037
Leon Scroggins1f005d32009-08-10 17:36:42 -040038/**
39 * This class represents a title bar for a particular "tab" or "window" in the
40 * browser.
41 */
Leon Scrogginse4b3bda2009-06-09 15:46:41 -040042public class TitleBar extends LinearLayout {
Leon Scroggins81db3662009-06-04 17:45:11 -040043 private TextView mTitle;
Leon Scroggins1f005d32009-08-10 17:36:42 -040044 private Drawable mCloseDrawable;
45 private ImageView mRtButton;
Leon Scroggins62e8f942009-09-03 15:08:54 -040046 private Drawable mCircularProgress;
Leon Scroggins81db3662009-06-04 17:45:11 -040047 private ProgressBar mHorizontalProgress;
Leon Scrogginsf4bb18a2009-09-11 18:37:53 -040048 private ImageView mFavicon;
Leon Scroggins62e8f942009-09-03 15:08:54 -040049 private ImageView mLockIcon;
Leon Scrogginsa81a7642009-08-31 17:05:41 -040050 private Drawable mStopDrawable;
51 private Drawable mBookmarkDrawable;
Leon Scroggins81db3662009-06-04 17:45:11 -040052 private boolean mInLoad;
Leon Scroggins1f005d32009-08-10 17:36:42 -040053 private WebView mWebView;
Leon Scrogginsa81a7642009-08-31 17:05:41 -040054 private BrowserActivity mBrowserActivity;
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -040055 private Drawable mGenericFavicon;
56 private int mIconDimension;
Leon Scroggins81db3662009-06-04 17:45:11 -040057
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -040058 public TitleBar(BrowserActivity context, WebView webview) {
Leon Scroggins1f005d32009-08-10 17:36:42 -040059 super(context, null);
Leon Scroggins81db3662009-06-04 17:45:11 -040060 LayoutInflater factory = LayoutInflater.from(context);
61 factory.inflate(R.layout.title_bar, this);
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -040062 mBrowserActivity = context;
Leon Scroggins81db3662009-06-04 17:45:11 -040063
64 mTitle = (TextView) findViewById(R.id.title);
Leon Scrogginsa81a7642009-08-31 17:05:41 -040065 mTitle.setCompoundDrawablePadding(5);
Leon Scroggins81db3662009-06-04 17:45:11 -040066
Leon Scroggins62e8f942009-09-03 15:08:54 -040067 mLockIcon = (ImageView) findViewById(R.id.lock);
Leon Scrogginsf4bb18a2009-09-11 18:37:53 -040068 mFavicon = (ImageView) findViewById(R.id.favicon);
Leon Scroggins62e8f942009-09-03 15:08:54 -040069
Leon Scrogginsa81a7642009-08-31 17:05:41 -040070 mRtButton = (ImageView) findViewById(R.id.rt_btn);
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -040071 Resources resources = context.getResources();
72 mCircularProgress = (Drawable) resources.getDrawable(
Leon Scroggins62e8f942009-09-03 15:08:54 -040073 com.android.internal.R.drawable.search_spinner);
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -040074 mIconDimension = (int) TypedValue.applyDimension(
75 TypedValue.COMPLEX_UNIT_DIP, 20f,
76 resources.getDisplayMetrics());
77 mCircularProgress.setBounds(0, 0, mIconDimension, mIconDimension);
Leon Scroggins81db3662009-06-04 17:45:11 -040078 mHorizontalProgress = (ProgressBar) findViewById(
79 R.id.progress_horizontal);
Leon Scroggins1f005d32009-08-10 17:36:42 -040080 mWebView = webview;
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -040081 mGenericFavicon = context.getResources().getDrawable(
82 R.drawable.app_web_browser_sm);
Leon Scrogginse4b3bda2009-06-09 15:46:41 -040083 }
84
Leon Scroggins1f005d32009-08-10 17:36:42 -040085 /**
86 * Return the WebView associated with this TitleBar.
87 */
88 /* package */ WebView getWebView() {
89 return mWebView;
Leon Scroggins81db3662009-06-04 17:45:11 -040090 }
91
Leon Scroggins1f005d32009-08-10 17:36:42 -040092 /**
Leon Scroggins1f005d32009-08-10 17:36:42 -040093 * Return whether the associated WebView is currently loading. Needed to
94 * determine whether a click should stop the load or close the tab.
95 */
96 /* package */ boolean isInLoad() {
97 return mInLoad;
98 }
99
100 /**
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400101 * Set a new Bitmap for the Favicon.
Leon Scroggins1f005d32009-08-10 17:36:42 -0400102 */
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400103 /* package */ void setFavicon(Bitmap icon) {
104 Drawable[] array = new Drawable[3];
105 array[0] = new PaintDrawable(Color.BLACK);
106 PaintDrawable p = new PaintDrawable(Color.WHITE);
107 array[1] = p;
108 if (icon == null) {
109 array[2] = mGenericFavicon;
110 } else {
111 array[2] = new BitmapDrawable(icon);
Leon Scrogginsa81a7642009-08-31 17:05:41 -0400112 }
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400113 LayerDrawable d = new LayerDrawable(array);
114 d.setLayerInset(1, 1, 1, 1, 1);
115 d.setLayerInset(2, 2, 2, 2, 2);
Leon Scrogginsf4bb18a2009-09-11 18:37:53 -0400116 mFavicon.setImageDrawable(d);
Leon Scroggins81db3662009-06-04 17:45:11 -0400117 }
118
Leon Scroggins1f005d32009-08-10 17:36:42 -0400119 /**
120 * Set the Drawable for the lock icon, or null to hide it.
121 */
Leon Scroggins81db3662009-06-04 17:45:11 -0400122 /* package */ void setLock(Drawable d) {
Leon Scroggins62e8f942009-09-03 15:08:54 -0400123 if (null == d) {
124 mLockIcon.setVisibility(View.GONE);
125 } else {
126 mLockIcon.setImageDrawable(d);
127 mLockIcon.setVisibility(View.VISIBLE);
Leon Scroggins81db3662009-06-04 17:45:11 -0400128 }
129 }
130
Leon Scroggins1f005d32009-08-10 17:36:42 -0400131 /**
132 * Update the progress, from 0 to 100.
133 */
Leon Scroggins81db3662009-06-04 17:45:11 -0400134 /* package */ void setProgress(int newProgress) {
Leon Scroggins62e8f942009-09-03 15:08:54 -0400135 if (newProgress == mHorizontalProgress.getMax()) {
Leon Scrogginsf4bb18a2009-09-11 18:37:53 -0400136 mTitle.setCompoundDrawables(null, null, null, null);
Leon Scroggins62e8f942009-09-03 15:08:54 -0400137 ((Animatable) mCircularProgress).stop();
138 mHorizontalProgress.setVisibility(View.INVISIBLE);
Leon Scrogginsa81a7642009-08-31 17:05:41 -0400139 if (mBookmarkDrawable != null) {
140 mRtButton.setImageDrawable(mBookmarkDrawable);
Leon Scroggins1f005d32009-08-10 17:36:42 -0400141 }
Leon Scroggins81db3662009-06-04 17:45:11 -0400142 mInLoad = false;
143 } else {
Leon Scroggins81db3662009-06-04 17:45:11 -0400144 mHorizontalProgress.setProgress(newProgress);
Leon Scroggins62e8f942009-09-03 15:08:54 -0400145 if (!mInLoad) {
Leon Scrogginsf4bb18a2009-09-11 18:37:53 -0400146 mTitle.setCompoundDrawables(null, null, mCircularProgress,
Leon Scroggins62e8f942009-09-03 15:08:54 -0400147 null);
148 ((Animatable) mCircularProgress).start();
149 mHorizontalProgress.setVisibility(View.VISIBLE);
150 if (mBookmarkDrawable == null) {
151 mBookmarkDrawable = mRtButton.getDrawable();
152 }
153 if (mStopDrawable == null) {
154 mRtButton.setImageResource(
155 com.android.internal.R.drawable.ic_menu_stop);
156 mStopDrawable = mRtButton.getDrawable();
157 } else {
158 mRtButton.setImageDrawable(mStopDrawable);
159 }
160 mInLoad = true;
Leon Scrogginse4b3bda2009-06-09 15:46:41 -0400161 }
Leon Scroggins81db3662009-06-04 17:45:11 -0400162 }
163 }
164
Leon Scroggins1f005d32009-08-10 17:36:42 -0400165 /**
166 * Update the title and url.
167 */
Leon Scroggins81db3662009-06-04 17:45:11 -0400168 /* package */ void setTitleAndUrl(CharSequence title, CharSequence url) {
Leon Scrogginsa81a7642009-08-31 17:05:41 -0400169 if (url == null) {
170 mTitle.setText(R.string.title_bar_loading);
Leon Scrogginsd7c3dd52009-07-20 13:38:47 -0400171 } else {
Leon Scrogginsc62e9082009-09-03 10:20:44 -0400172 mTitle.setText(url.toString());
Leon Scrogginsd7c3dd52009-07-20 13:38:47 -0400173 }
Leon Scroggins81db3662009-06-04 17:45:11 -0400174 }
175
176 /* package */ void setToTabPicker() {
177 mTitle.setText(R.string.tab_picker_title);
178 setFavicon(null);
179 setLock(null);
Leon Scroggins81db3662009-06-04 17:45:11 -0400180 mHorizontalProgress.setVisibility(View.GONE);
Leon Scroggins81db3662009-06-04 17:45:11 -0400181 }
182}