blob: c1189362d3ed5fb08a308b0a7a6a1081126dba4f [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 Scroggins1f005d32009-08-10 17:36:42 -040020import android.graphics.Rect;
Leon Scroggins62e8f942009-09-03 15:08:54 -040021import android.graphics.drawable.Animatable;
Leon Scroggins81db3662009-06-04 17:45:11 -040022import android.graphics.drawable.Drawable;
Leon Scrogginse4b3bda2009-06-09 15:46:41 -040023import android.util.AttributeSet;
24import android.view.LayoutInflater;
25import android.view.View;
Leon Scroggins81db3662009-06-04 17:45:11 -040026import android.webkit.WebView;
27import android.widget.ImageView;
28import android.widget.LinearLayout;
29import android.widget.ProgressBar;
30import android.widget.TextView;
Leon Scroggins81db3662009-06-04 17:45:11 -040031
Leon Scroggins1f005d32009-08-10 17:36:42 -040032/**
33 * This class represents a title bar for a particular "tab" or "window" in the
34 * browser.
35 */
Leon Scrogginse4b3bda2009-06-09 15:46:41 -040036public class TitleBar extends LinearLayout {
Leon Scroggins81db3662009-06-04 17:45:11 -040037 private TextView mTitle;
Leon Scroggins1f005d32009-08-10 17:36:42 -040038 private Drawable mCloseDrawable;
39 private ImageView mRtButton;
Leon Scroggins62e8f942009-09-03 15:08:54 -040040 private Drawable mCircularProgress;
Leon Scroggins81db3662009-06-04 17:45:11 -040041 private ProgressBar mHorizontalProgress;
Leon Scrogginsa81a7642009-08-31 17:05:41 -040042 private Drawable mFavicon;
Leon Scroggins62e8f942009-09-03 15:08:54 -040043 private ImageView mLockIcon;
Leon Scrogginsa81a7642009-08-31 17:05:41 -040044 private Drawable mStopDrawable;
45 private Drawable mBookmarkDrawable;
Leon Scroggins81db3662009-06-04 17:45:11 -040046 private boolean mInLoad;
Leon Scroggins1f005d32009-08-10 17:36:42 -040047 private WebView mWebView;
Leon Scrogginsa81a7642009-08-31 17:05:41 -040048 private BrowserActivity mBrowserActivity;
Leon Scroggins81db3662009-06-04 17:45:11 -040049
Leon Scrogginsa81a7642009-08-31 17:05:41 -040050 public TitleBar(Context context, WebView webview, BrowserActivity ba) {
Leon Scroggins1f005d32009-08-10 17:36:42 -040051 super(context, null);
Leon Scroggins81db3662009-06-04 17:45:11 -040052 LayoutInflater factory = LayoutInflater.from(context);
53 factory.inflate(R.layout.title_bar, this);
Leon Scrogginsa81a7642009-08-31 17:05:41 -040054 mBrowserActivity = ba;
Leon Scroggins81db3662009-06-04 17:45:11 -040055
56 mTitle = (TextView) findViewById(R.id.title);
Leon Scrogginsa81a7642009-08-31 17:05:41 -040057 mTitle.setCompoundDrawablePadding(5);
Leon Scroggins81db3662009-06-04 17:45:11 -040058
Leon Scroggins62e8f942009-09-03 15:08:54 -040059 mLockIcon = (ImageView) findViewById(R.id.lock);
60
Leon Scrogginsa81a7642009-08-31 17:05:41 -040061 mRtButton = (ImageView) findViewById(R.id.rt_btn);
62 mRtButton.setOnClickListener(new View.OnClickListener() {
63 public void onClick(View v) {
64 if (mInLoad) {
65 if (mWebView != null) {
66 mWebView.stopLoading();
67 }
68 } else {
69 mBrowserActivity.bookmarksOrHistoryPicker(false, false);
70 }
71 }
72 });
Leon Scroggins62e8f942009-09-03 15:08:54 -040073 mCircularProgress = (Drawable) context.getResources().getDrawable(
74 com.android.internal.R.drawable.search_spinner);
75 mCircularProgress.setBounds(0,0,20,20);
Leon Scroggins81db3662009-06-04 17:45:11 -040076 mHorizontalProgress = (ProgressBar) findViewById(
77 R.id.progress_horizontal);
Leon Scroggins1f005d32009-08-10 17:36:42 -040078 mWebView = webview;
Leon Scrogginse4b3bda2009-06-09 15:46:41 -040079 }
80
Leon Scroggins1f005d32009-08-10 17:36:42 -040081 /**
82 * Return the WebView associated with this TitleBar.
83 */
84 /* package */ WebView getWebView() {
85 return mWebView;
Leon Scroggins81db3662009-06-04 17:45:11 -040086 }
87
Leon Scroggins1f005d32009-08-10 17:36:42 -040088 /**
Leon Scroggins1f005d32009-08-10 17:36:42 -040089 * Return whether the associated WebView is currently loading. Needed to
90 * determine whether a click should stop the load or close the tab.
91 */
92 /* package */ boolean isInLoad() {
93 return mInLoad;
94 }
95
96 /**
97 * Set a new Drawable for the Favicon.
98 */
Leon Scroggins81db3662009-06-04 17:45:11 -040099 /* package */ void setFavicon(Drawable d) {
Leon Scrogginsa81a7642009-08-31 17:05:41 -0400100 if (d != null) {
101 d.setBounds(0, 0, 20, 20);
102 }
Leon Scroggins62e8f942009-09-03 15:08:54 -0400103 Drawable progress = mInLoad ? mCircularProgress : null;
104 mTitle.setCompoundDrawables(d, null, progress, null);
Leon Scrogginsa81a7642009-08-31 17:05:41 -0400105 mFavicon = d;
Leon Scroggins81db3662009-06-04 17:45:11 -0400106 }
107
Leon Scroggins1f005d32009-08-10 17:36:42 -0400108 /**
109 * Set the Drawable for the lock icon, or null to hide it.
110 */
Leon Scroggins81db3662009-06-04 17:45:11 -0400111 /* package */ void setLock(Drawable d) {
Leon Scroggins62e8f942009-09-03 15:08:54 -0400112 if (null == d) {
113 mLockIcon.setVisibility(View.GONE);
114 } else {
115 mLockIcon.setImageDrawable(d);
116 mLockIcon.setVisibility(View.VISIBLE);
Leon Scroggins81db3662009-06-04 17:45:11 -0400117 }
118 }
119
Leon Scroggins1f005d32009-08-10 17:36:42 -0400120 /**
121 * Update the progress, from 0 to 100.
122 */
Leon Scroggins81db3662009-06-04 17:45:11 -0400123 /* package */ void setProgress(int newProgress) {
Leon Scroggins62e8f942009-09-03 15:08:54 -0400124 if (newProgress == mHorizontalProgress.getMax()) {
125 mTitle.setCompoundDrawables(mFavicon, null, null, null);
126 ((Animatable) mCircularProgress).stop();
127 mHorizontalProgress.setVisibility(View.INVISIBLE);
Leon Scrogginsa81a7642009-08-31 17:05:41 -0400128 if (mBookmarkDrawable != null) {
129 mRtButton.setImageDrawable(mBookmarkDrawable);
Leon Scroggins1f005d32009-08-10 17:36:42 -0400130 }
Leon Scroggins81db3662009-06-04 17:45:11 -0400131 mInLoad = false;
132 } else {
Leon Scroggins81db3662009-06-04 17:45:11 -0400133 mHorizontalProgress.setProgress(newProgress);
Leon Scroggins62e8f942009-09-03 15:08:54 -0400134 if (!mInLoad) {
135 mTitle.setCompoundDrawables(mFavicon, null, mCircularProgress,
136 null);
137 ((Animatable) mCircularProgress).start();
138 mHorizontalProgress.setVisibility(View.VISIBLE);
139 if (mBookmarkDrawable == null) {
140 mBookmarkDrawable = mRtButton.getDrawable();
141 }
142 if (mStopDrawable == null) {
143 mRtButton.setImageResource(
144 com.android.internal.R.drawable.ic_menu_stop);
145 mStopDrawable = mRtButton.getDrawable();
146 } else {
147 mRtButton.setImageDrawable(mStopDrawable);
148 }
149 mInLoad = true;
Leon Scrogginse4b3bda2009-06-09 15:46:41 -0400150 }
Leon Scroggins81db3662009-06-04 17:45:11 -0400151 }
152 }
153
Leon Scroggins1f005d32009-08-10 17:36:42 -0400154 /**
155 * Update the title and url.
156 */
Leon Scroggins81db3662009-06-04 17:45:11 -0400157 /* package */ void setTitleAndUrl(CharSequence title, CharSequence url) {
Leon Scrogginsa81a7642009-08-31 17:05:41 -0400158 if (url == null) {
159 mTitle.setText(R.string.title_bar_loading);
Leon Scrogginsd7c3dd52009-07-20 13:38:47 -0400160 } else {
Leon Scrogginsc62e9082009-09-03 10:20:44 -0400161 mTitle.setText(url.toString());
Leon Scrogginsd7c3dd52009-07-20 13:38:47 -0400162 }
Leon Scroggins81db3662009-06-04 17:45:11 -0400163 }
164
165 /* package */ void setToTabPicker() {
166 mTitle.setText(R.string.tab_picker_title);
167 setFavicon(null);
168 setLock(null);
Leon Scroggins81db3662009-06-04 17:45:11 -0400169 mHorizontalProgress.setVisibility(View.GONE);
Leon Scroggins81db3662009-06-04 17:45:11 -0400170 }
171}