blob: e830727e6bb6d1ade0d330f6f69dc352e89d3585 [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 Scrogginsa81a7642009-08-31 17:05:41 -040048 private Drawable 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);
68
Leon Scrogginsa81a7642009-08-31 17:05:41 -040069 mRtButton = (ImageView) findViewById(R.id.rt_btn);
70 mRtButton.setOnClickListener(new View.OnClickListener() {
71 public void onClick(View v) {
72 if (mInLoad) {
73 if (mWebView != null) {
74 mWebView.stopLoading();
75 }
76 } else {
Leon Scroggins30444232009-09-04 18:36:20 -040077 mBrowserActivity.bookmarksOrHistoryPicker(false);
Leon Scrogginsa81a7642009-08-31 17:05:41 -040078 }
79 }
80 });
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -040081 Resources resources = context.getResources();
82 mCircularProgress = (Drawable) resources.getDrawable(
Leon Scroggins62e8f942009-09-03 15:08:54 -040083 com.android.internal.R.drawable.search_spinner);
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -040084 mIconDimension = (int) TypedValue.applyDimension(
85 TypedValue.COMPLEX_UNIT_DIP, 20f,
86 resources.getDisplayMetrics());
87 mCircularProgress.setBounds(0, 0, mIconDimension, mIconDimension);
Leon Scroggins81db3662009-06-04 17:45:11 -040088 mHorizontalProgress = (ProgressBar) findViewById(
89 R.id.progress_horizontal);
Leon Scroggins1f005d32009-08-10 17:36:42 -040090 mWebView = webview;
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -040091 mGenericFavicon = context.getResources().getDrawable(
92 R.drawable.app_web_browser_sm);
Leon Scrogginse4b3bda2009-06-09 15:46:41 -040093 }
94
Leon Scroggins1f005d32009-08-10 17:36:42 -040095 /**
96 * Return the WebView associated with this TitleBar.
97 */
98 /* package */ WebView getWebView() {
99 return mWebView;
Leon Scroggins81db3662009-06-04 17:45:11 -0400100 }
101
Leon Scroggins1f005d32009-08-10 17:36:42 -0400102 /**
Leon Scroggins1f005d32009-08-10 17:36:42 -0400103 * Return whether the associated WebView is currently loading. Needed to
104 * determine whether a click should stop the load or close the tab.
105 */
106 /* package */ boolean isInLoad() {
107 return mInLoad;
108 }
109
110 /**
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400111 * Set a new Bitmap for the Favicon.
Leon Scroggins1f005d32009-08-10 17:36:42 -0400112 */
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400113 /* package */ void setFavicon(Bitmap icon) {
114 Drawable[] array = new Drawable[3];
115 array[0] = new PaintDrawable(Color.BLACK);
116 PaintDrawable p = new PaintDrawable(Color.WHITE);
117 array[1] = p;
118 if (icon == null) {
119 array[2] = mGenericFavicon;
120 } else {
121 array[2] = new BitmapDrawable(icon);
Leon Scrogginsa81a7642009-08-31 17:05:41 -0400122 }
Leon Scroggins3bbb6ca2009-09-09 12:51:10 -0400123 LayerDrawable d = new LayerDrawable(array);
124 d.setLayerInset(1, 1, 1, 1, 1);
125 d.setLayerInset(2, 2, 2, 2, 2);
126 d.setBounds(0, 0, mIconDimension, mIconDimension);
Leon Scroggins62e8f942009-09-03 15:08:54 -0400127 Drawable progress = mInLoad ? mCircularProgress : null;
128 mTitle.setCompoundDrawables(d, null, progress, null);
Leon Scrogginsa81a7642009-08-31 17:05:41 -0400129 mFavicon = d;
Leon Scroggins81db3662009-06-04 17:45:11 -0400130 }
131
Leon Scroggins1f005d32009-08-10 17:36:42 -0400132 /**
133 * Set the Drawable for the lock icon, or null to hide it.
134 */
Leon Scroggins81db3662009-06-04 17:45:11 -0400135 /* package */ void setLock(Drawable d) {
Leon Scroggins62e8f942009-09-03 15:08:54 -0400136 if (null == d) {
137 mLockIcon.setVisibility(View.GONE);
138 } else {
139 mLockIcon.setImageDrawable(d);
140 mLockIcon.setVisibility(View.VISIBLE);
Leon Scroggins81db3662009-06-04 17:45:11 -0400141 }
142 }
143
Leon Scroggins1f005d32009-08-10 17:36:42 -0400144 /**
145 * Update the progress, from 0 to 100.
146 */
Leon Scroggins81db3662009-06-04 17:45:11 -0400147 /* package */ void setProgress(int newProgress) {
Leon Scroggins62e8f942009-09-03 15:08:54 -0400148 if (newProgress == mHorizontalProgress.getMax()) {
149 mTitle.setCompoundDrawables(mFavicon, null, null, null);
150 ((Animatable) mCircularProgress).stop();
151 mHorizontalProgress.setVisibility(View.INVISIBLE);
Leon Scrogginsa81a7642009-08-31 17:05:41 -0400152 if (mBookmarkDrawable != null) {
153 mRtButton.setImageDrawable(mBookmarkDrawable);
Leon Scroggins1f005d32009-08-10 17:36:42 -0400154 }
Leon Scroggins81db3662009-06-04 17:45:11 -0400155 mInLoad = false;
156 } else {
Leon Scroggins81db3662009-06-04 17:45:11 -0400157 mHorizontalProgress.setProgress(newProgress);
Leon Scroggins62e8f942009-09-03 15:08:54 -0400158 if (!mInLoad) {
159 mTitle.setCompoundDrawables(mFavicon, null, mCircularProgress,
160 null);
161 ((Animatable) mCircularProgress).start();
162 mHorizontalProgress.setVisibility(View.VISIBLE);
163 if (mBookmarkDrawable == null) {
164 mBookmarkDrawable = mRtButton.getDrawable();
165 }
166 if (mStopDrawable == null) {
167 mRtButton.setImageResource(
168 com.android.internal.R.drawable.ic_menu_stop);
169 mStopDrawable = mRtButton.getDrawable();
170 } else {
171 mRtButton.setImageDrawable(mStopDrawable);
172 }
173 mInLoad = true;
Leon Scrogginse4b3bda2009-06-09 15:46:41 -0400174 }
Leon Scroggins81db3662009-06-04 17:45:11 -0400175 }
176 }
177
Leon Scroggins1f005d32009-08-10 17:36:42 -0400178 /**
179 * Update the title and url.
180 */
Leon Scroggins81db3662009-06-04 17:45:11 -0400181 /* package */ void setTitleAndUrl(CharSequence title, CharSequence url) {
Leon Scrogginsa81a7642009-08-31 17:05:41 -0400182 if (url == null) {
183 mTitle.setText(R.string.title_bar_loading);
Leon Scrogginsd7c3dd52009-07-20 13:38:47 -0400184 } else {
Leon Scrogginsc62e9082009-09-03 10:20:44 -0400185 mTitle.setText(url.toString());
Leon Scrogginsd7c3dd52009-07-20 13:38:47 -0400186 }
Leon Scroggins81db3662009-06-04 17:45:11 -0400187 }
188
189 /* package */ void setToTabPicker() {
190 mTitle.setText(R.string.tab_picker_title);
191 setFavicon(null);
192 setLock(null);
Leon Scroggins81db3662009-06-04 17:45:11 -0400193 mHorizontalProgress.setVisibility(View.GONE);
Leon Scroggins81db3662009-06-04 17:45:11 -0400194 }
195}