blob: 1d53e8d03339d93e6004b9e94be29683117ee24c [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 Scroggins81db3662009-06-04 17:45:11 -040021import android.graphics.drawable.Drawable;
Leon Scrogginse4b3bda2009-06-09 15:46:41 -040022import android.util.AttributeSet;
23import android.view.LayoutInflater;
24import android.view.View;
Leon Scroggins81db3662009-06-04 17:45:11 -040025import android.webkit.WebView;
26import android.widget.ImageView;
27import android.widget.LinearLayout;
28import android.widget.ProgressBar;
29import android.widget.TextView;
Leon Scroggins81db3662009-06-04 17:45:11 -040030
Leon Scroggins1f005d32009-08-10 17:36:42 -040031/**
32 * This class represents a title bar for a particular "tab" or "window" in the
33 * browser.
34 */
Leon Scrogginse4b3bda2009-06-09 15:46:41 -040035public class TitleBar extends LinearLayout {
Leon Scroggins81db3662009-06-04 17:45:11 -040036 private TextView mTitle;
37 private TextView mUrl;
Leon Scroggins1f005d32009-08-10 17:36:42 -040038 private Drawable mCloseDrawable;
39 private ImageView mRtButton;
Leon Scroggins81db3662009-06-04 17:45:11 -040040 private ProgressBar mCircularProgress;
41 private ProgressBar mHorizontalProgress;
42 private ImageView mFavicon;
Leon Scroggins1f005d32009-08-10 17:36:42 -040043 private ImageView mLockIcon; // FIXME: Needs to be below the favicon
Leon Scroggins81db3662009-06-04 17:45:11 -040044 private boolean mInLoad;
Leon Scrogginsd7c3dd52009-07-20 13:38:47 -040045 private boolean mTitleSet;
Leon Scroggins1f005d32009-08-10 17:36:42 -040046 private WebView mWebView;
Leon Scroggins81db3662009-06-04 17:45:11 -040047
Leon Scroggins1f005d32009-08-10 17:36:42 -040048 public TitleBar(Context context, WebView webview) {
49 super(context, null);
Leon Scroggins81db3662009-06-04 17:45:11 -040050 LayoutInflater factory = LayoutInflater.from(context);
51 factory.inflate(R.layout.title_bar, this);
52
53 mTitle = (TextView) findViewById(R.id.title);
54 mUrl = (TextView) findViewById(R.id.url);
55
Leon Scroggins1f005d32009-08-10 17:36:42 -040056 mRtButton = (ImageView) findViewById(R.id.rt_button);
Leon Scroggins81db3662009-06-04 17:45:11 -040057
58 mCircularProgress = (ProgressBar) findViewById(R.id.progress_circular);
59 mHorizontalProgress = (ProgressBar) findViewById(
60 R.id.progress_horizontal);
61 mFavicon = (ImageView) findViewById(R.id.favicon);
62 mLockIcon = (ImageView) findViewById(R.id.lock_icon);
Leon Scroggins1f005d32009-08-10 17:36:42 -040063 mWebView = webview;
Leon Scrogginse4b3bda2009-06-09 15:46:41 -040064 }
65
Leon Scroggins1f005d32009-08-10 17:36:42 -040066 /**
67 * Return the WebView associated with this TitleBar.
68 */
69 /* package */ WebView getWebView() {
70 return mWebView;
Leon Scroggins81db3662009-06-04 17:45:11 -040071 }
72
Leon Scroggins1f005d32009-08-10 17:36:42 -040073 /**
74 * Determine whether a point (from a touch) hits the right button.
75 */
76 /* package */ boolean hitRightButton(int x, int y) {
77 Rect hitRect = new Rect();
78 mRtButton.getHitRect(hitRect);
79 return hitRect.contains(x - getLeft(), y - getTop());
80 }
81
82 /**
83 * Return whether the associated WebView is currently loading. Needed to
84 * determine whether a click should stop the load or close the tab.
85 */
86 /* package */ boolean isInLoad() {
87 return mInLoad;
88 }
89
90 /**
91 * Set a new Drawable for the Favicon.
92 */
Leon Scroggins81db3662009-06-04 17:45:11 -040093 /* package */ void setFavicon(Drawable d) {
94 mFavicon.setImageDrawable(d);
95 }
96
Leon Scroggins1f005d32009-08-10 17:36:42 -040097 /**
98 * Set the Drawable for the lock icon, or null to hide it.
99 */
Leon Scroggins81db3662009-06-04 17:45:11 -0400100 /* package */ void setLock(Drawable d) {
101 if (d == null) {
102 mLockIcon.setVisibility(View.GONE);
103 } else {
104 mLockIcon.setImageDrawable(d);
105 mLockIcon.setVisibility(View.VISIBLE);
106 }
107 }
108
Leon Scroggins1f005d32009-08-10 17:36:42 -0400109 /**
110 * Update the progress, from 0 to 100.
111 */
Leon Scroggins81db3662009-06-04 17:45:11 -0400112 /* package */ void setProgress(int newProgress) {
113 if (newProgress == mCircularProgress.getMax()) {
114 mCircularProgress.setVisibility(View.GONE);
115 mHorizontalProgress.setVisibility(View.GONE);
Leon Scrogginse4b3bda2009-06-09 15:46:41 -0400116 mRtButton.setVisibility(View.VISIBLE);
Leon Scroggins1f005d32009-08-10 17:36:42 -0400117 mUrl.setVisibility(View.VISIBLE);
118 if (mCloseDrawable != null) {
119 mRtButton.setImageDrawable(mCloseDrawable);
120 }
Leon Scroggins81db3662009-06-04 17:45:11 -0400121 mInLoad = false;
Leon Scrogginsd7c3dd52009-07-20 13:38:47 -0400122 if (!mTitleSet) {
123 mTitle.setText(mUrl.getText());
124 mUrl.setText(null);
125 mTitleSet = true;
126 }
Leon Scroggins81db3662009-06-04 17:45:11 -0400127 } else {
128 mCircularProgress.setProgress(newProgress);
129 mHorizontalProgress.setProgress(newProgress);
130 mCircularProgress.setVisibility(View.VISIBLE);
131 mHorizontalProgress.setVisibility(View.VISIBLE);
Leon Scroggins1f005d32009-08-10 17:36:42 -0400132 mUrl.setVisibility(View.VISIBLE);
133 if (mCloseDrawable == null) {
Leon Scrogginse4b3bda2009-06-09 15:46:41 -0400134 // The drawable was assigned in the xml file, so it already
135 // exists. Keep a pointer to it when we switch to the resource
136 // so we can easily switch back.
Leon Scroggins1f005d32009-08-10 17:36:42 -0400137 mCloseDrawable = mRtButton.getDrawable();
Leon Scrogginse4b3bda2009-06-09 15:46:41 -0400138 }
Leon Scroggins1f005d32009-08-10 17:36:42 -0400139 mRtButton.setImageResource(
Leon Scrogginse4b3bda2009-06-09 15:46:41 -0400140 com.android.internal.R.drawable.ic_menu_stop);
Leon Scroggins81db3662009-06-04 17:45:11 -0400141 mInLoad = true;
142 }
143 }
144
Leon Scroggins1f005d32009-08-10 17:36:42 -0400145 /**
146 * Update the title and url.
147 */
Leon Scroggins81db3662009-06-04 17:45:11 -0400148 /* package */ void setTitleAndUrl(CharSequence title, CharSequence url) {
Leon Scroggins176215d2009-06-15 12:38:58 -0400149 if (url != null) {
150 url = BrowserActivity.buildTitleUrl(url.toString());
151 }
Leon Scrogginsd7c3dd52009-07-20 13:38:47 -0400152 if (null == title) {
153 if (mInLoad) {
154 mTitleSet = false;
155 mTitle.setText(R.string.title_bar_loading);
156 } else {
157 // If the page has no title, put the url in the title space
158 // and leave the url blank.
159 mTitle.setText(url);
160 mUrl.setText(null);
161 mTitleSet = true;
162 return;
163 }
164 } else {
165 mTitle.setText(title);
166 mTitleSet = true;
167 }
Leon Scroggins176215d2009-06-15 12:38:58 -0400168 mUrl.setText(url);
Leon Scroggins81db3662009-06-04 17:45:11 -0400169 }
170
171 /* package */ void setToTabPicker() {
172 mTitle.setText(R.string.tab_picker_title);
173 setFavicon(null);
174 setLock(null);
175 mCircularProgress.setVisibility(View.GONE);
176 mHorizontalProgress.setVisibility(View.GONE);
Leon Scroggins1f005d32009-08-10 17:36:42 -0400177 mUrl.setVisibility(View.INVISIBLE);
Leon Scroggins81db3662009-06-04 17:45:11 -0400178 }
179}