blob: f03e2dab468f1b3a043726e44dc7d1f9376eeb28 [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;
Leon Scroggins1f005d32009-08-10 17:36:42 -040037 private Drawable mCloseDrawable;
38 private ImageView mRtButton;
Leon Scroggins81db3662009-06-04 17:45:11 -040039 private ProgressBar mCircularProgress;
40 private ProgressBar mHorizontalProgress;
Leon Scrogginsa81a7642009-08-31 17:05:41 -040041 private Drawable mFavicon;
42 private Drawable mLockIcon;
43 private Drawable mStopDrawable;
44 private Drawable mBookmarkDrawable;
Leon Scroggins81db3662009-06-04 17:45:11 -040045 private boolean mInLoad;
Leon Scroggins1f005d32009-08-10 17:36:42 -040046 private WebView mWebView;
Leon Scrogginsa81a7642009-08-31 17:05:41 -040047 private BrowserActivity mBrowserActivity;
Leon Scroggins81db3662009-06-04 17:45:11 -040048
Leon Scrogginsa81a7642009-08-31 17:05:41 -040049 public TitleBar(Context context, WebView webview, BrowserActivity ba) {
Leon Scroggins1f005d32009-08-10 17:36:42 -040050 super(context, null);
Leon Scroggins81db3662009-06-04 17:45:11 -040051 LayoutInflater factory = LayoutInflater.from(context);
52 factory.inflate(R.layout.title_bar, this);
Leon Scrogginsa81a7642009-08-31 17:05:41 -040053 mBrowserActivity = ba;
Leon Scroggins81db3662009-06-04 17:45:11 -040054
55 mTitle = (TextView) findViewById(R.id.title);
Leon Scrogginsa81a7642009-08-31 17:05:41 -040056 mTitle.setCompoundDrawablePadding(5);
Leon Scroggins81db3662009-06-04 17:45:11 -040057
Leon Scrogginsa81a7642009-08-31 17:05:41 -040058 mRtButton = (ImageView) findViewById(R.id.rt_btn);
59 mRtButton.setOnClickListener(new View.OnClickListener() {
60 public void onClick(View v) {
61 if (mInLoad) {
62 if (mWebView != null) {
63 mWebView.stopLoading();
64 }
65 } else {
66 mBrowserActivity.bookmarksOrHistoryPicker(false, false);
67 }
68 }
69 });
Leon Scroggins81db3662009-06-04 17:45:11 -040070 mCircularProgress = (ProgressBar) findViewById(R.id.progress_circular);
71 mHorizontalProgress = (ProgressBar) findViewById(
72 R.id.progress_horizontal);
Leon Scroggins1f005d32009-08-10 17:36:42 -040073 mWebView = webview;
Leon Scrogginse4b3bda2009-06-09 15:46:41 -040074 }
75
Leon Scroggins1f005d32009-08-10 17:36:42 -040076 /**
77 * Return the WebView associated with this TitleBar.
78 */
79 /* package */ WebView getWebView() {
80 return mWebView;
Leon Scroggins81db3662009-06-04 17:45:11 -040081 }
82
Leon Scroggins1f005d32009-08-10 17:36:42 -040083 /**
Leon Scroggins1f005d32009-08-10 17:36:42 -040084 * Return whether the associated WebView is currently loading. Needed to
85 * determine whether a click should stop the load or close the tab.
86 */
87 /* package */ boolean isInLoad() {
88 return mInLoad;
89 }
90
91 /**
92 * Set a new Drawable for the Favicon.
93 */
Leon Scroggins81db3662009-06-04 17:45:11 -040094 /* package */ void setFavicon(Drawable d) {
Leon Scrogginsa81a7642009-08-31 17:05:41 -040095 if (d != null) {
96 d.setBounds(0, 0, 20, 20);
97 }
98 mTitle.setCompoundDrawables(d, null, mLockIcon, null);
99 mFavicon = d;
Leon Scroggins81db3662009-06-04 17:45:11 -0400100 }
101
Leon Scroggins1f005d32009-08-10 17:36:42 -0400102 /**
103 * Set the Drawable for the lock icon, or null to hide it.
104 */
Leon Scroggins81db3662009-06-04 17:45:11 -0400105 /* package */ void setLock(Drawable d) {
Leon Scrogginsa81a7642009-08-31 17:05:41 -0400106 if (d != null) {
107 d.setBounds(0, 0, 20, 20);
Leon Scroggins81db3662009-06-04 17:45:11 -0400108 }
Leon Scrogginsa81a7642009-08-31 17:05:41 -0400109 mTitle.setCompoundDrawables(mFavicon, null, d, null);
110 mLockIcon = d;
Leon Scroggins81db3662009-06-04 17:45:11 -0400111 }
112
Leon Scroggins1f005d32009-08-10 17:36:42 -0400113 /**
114 * Update the progress, from 0 to 100.
115 */
Leon Scroggins81db3662009-06-04 17:45:11 -0400116 /* package */ void setProgress(int newProgress) {
117 if (newProgress == mCircularProgress.getMax()) {
118 mCircularProgress.setVisibility(View.GONE);
119 mHorizontalProgress.setVisibility(View.GONE);
Leon Scrogginsa81a7642009-08-31 17:05:41 -0400120 if (mBookmarkDrawable != null) {
121 mRtButton.setImageDrawable(mBookmarkDrawable);
Leon Scroggins1f005d32009-08-10 17:36:42 -0400122 }
Leon Scroggins81db3662009-06-04 17:45:11 -0400123 mInLoad = false;
124 } else {
125 mCircularProgress.setProgress(newProgress);
126 mHorizontalProgress.setProgress(newProgress);
127 mCircularProgress.setVisibility(View.VISIBLE);
128 mHorizontalProgress.setVisibility(View.VISIBLE);
Leon Scrogginsa81a7642009-08-31 17:05:41 -0400129 if (mBookmarkDrawable == null) {
130 mBookmarkDrawable = mRtButton.getDrawable();
Leon Scrogginse4b3bda2009-06-09 15:46:41 -0400131 }
Leon Scrogginsa81a7642009-08-31 17:05:41 -0400132 if (mStopDrawable == null) {
133 mRtButton.setImageResource(
134 com.android.internal.R.drawable.ic_menu_stop);
135 mStopDrawable = mRtButton.getDrawable();
136 } else {
137 mRtButton.setImageDrawable(mStopDrawable);
138 }
Leon Scroggins81db3662009-06-04 17:45:11 -0400139 mInLoad = true;
140 }
141 }
142
Leon Scroggins1f005d32009-08-10 17:36:42 -0400143 /**
144 * Update the title and url.
145 */
Leon Scroggins81db3662009-06-04 17:45:11 -0400146 /* package */ void setTitleAndUrl(CharSequence title, CharSequence url) {
Leon Scrogginsa81a7642009-08-31 17:05:41 -0400147 if (url == null) {
148 mTitle.setText(R.string.title_bar_loading);
Leon Scrogginsd7c3dd52009-07-20 13:38:47 -0400149 } else {
Leon Scrogginsc62e9082009-09-03 10:20:44 -0400150 mTitle.setText(url.toString());
Leon Scrogginsd7c3dd52009-07-20 13:38:47 -0400151 }
Leon Scroggins81db3662009-06-04 17:45:11 -0400152 }
153
154 /* package */ void setToTabPicker() {
155 mTitle.setText(R.string.tab_picker_title);
156 setFavicon(null);
157 setLock(null);
158 mCircularProgress.setVisibility(View.GONE);
159 mHorizontalProgress.setVisibility(View.GONE);
Leon Scroggins81db3662009-06-04 17:45:11 -0400160 }
161}