blob: f534a032f1dd13c19b8d2bc2b7a1ea90fdbc05a4 [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;
20import android.graphics.drawable.Drawable;
Leon Scrogginse4b3bda2009-06-09 15:46:41 -040021import android.util.AttributeSet;
22import android.view.LayoutInflater;
23import android.view.View;
Leon Scroggins81db3662009-06-04 17:45:11 -040024import android.webkit.WebView;
25import android.widget.ImageView;
26import android.widget.LinearLayout;
27import android.widget.ProgressBar;
28import android.widget.TextView;
Leon Scroggins81db3662009-06-04 17:45:11 -040029
Leon Scrogginse4b3bda2009-06-09 15:46:41 -040030public class TitleBar extends LinearLayout {
Leon Scroggins81db3662009-06-04 17:45:11 -040031 private TextView mTitle;
32 private TextView mUrl;
Leon Scrogginse4b3bda2009-06-09 15:46:41 -040033 private ImageView mLftButton;
34 private Drawable mBookmarkDrawable;
35 private View mRtButton;
Leon Scroggins81db3662009-06-04 17:45:11 -040036 private View mDivider;
37 private ProgressBar mCircularProgress;
38 private ProgressBar mHorizontalProgress;
39 private ImageView mFavicon;
40 private ImageView mLockIcon;
41 private boolean mInLoad;
Leon Scrogginsd7c3dd52009-07-20 13:38:47 -040042 private boolean mTitleSet;
Leon Scroggins81db3662009-06-04 17:45:11 -040043
Leon Scrogginse4b3bda2009-06-09 15:46:41 -040044 public TitleBar(Context context) {
45 this(context, null);
46 }
Leon Scroggins81db3662009-06-04 17:45:11 -040047
Leon Scrogginse4b3bda2009-06-09 15:46:41 -040048 public TitleBar(Context context, AttributeSet attrs) {
49 super(context, attrs);
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 Scrogginse4b3bda2009-06-09 15:46:41 -040056 mLftButton = (ImageView) findViewById(R.id.lft_button);
57 mRtButton = findViewById(R.id.rt_button);
Leon Scroggins81db3662009-06-04 17:45:11 -040058
59 mCircularProgress = (ProgressBar) findViewById(R.id.progress_circular);
60 mHorizontalProgress = (ProgressBar) findViewById(
61 R.id.progress_horizontal);
62 mFavicon = (ImageView) findViewById(R.id.favicon);
63 mLockIcon = (ImageView) findViewById(R.id.lock_icon);
64 mDivider = findViewById(R.id.divider);
Leon Scrogginse4b3bda2009-06-09 15:46:41 -040065 }
66
67 /* package */ void setBrowserActivity(final BrowserActivity activity) {
68 mLftButton.setOnClickListener(new View.OnClickListener() {
69 public void onClick(View v) {
70 if (mInLoad) {
71 WebView webView = activity.getTopWindow();
72 if (webView != null) {
73 webView.stopLoading();
74 }
75 } else {
76 activity.bookmarksOrHistoryPicker(false);
77 }
78 }
79 });
80 mRtButton.setOnClickListener(new View.OnClickListener() {
81 public void onClick(View v) {
82 WebView webView = activity.getTopWindow();
83 if (webView != null) {
84 webView.zoomScrollOut();
85 }
86 }
87 });
Leon Scroggins81db3662009-06-04 17:45:11 -040088 setOnClickListener(new View.OnClickListener() {
89 public void onClick(View v) {
Leon Scrogginse4b3bda2009-06-09 15:46:41 -040090 activity.onSearchRequested();
Leon Scroggins81db3662009-06-04 17:45:11 -040091 }
92 });
93 }
94
95 /* package */ void setFavicon(Drawable d) {
96 mFavicon.setImageDrawable(d);
97 }
98
99 /* package */ void setLock(Drawable d) {
100 if (d == null) {
101 mLockIcon.setVisibility(View.GONE);
102 } else {
103 mLockIcon.setImageDrawable(d);
104 mLockIcon.setVisibility(View.VISIBLE);
105 }
106 }
107
108 /* package */ void setProgress(int newProgress) {
109 if (newProgress == mCircularProgress.getMax()) {
110 mCircularProgress.setVisibility(View.GONE);
111 mHorizontalProgress.setVisibility(View.GONE);
Leon Scroggins81db3662009-06-04 17:45:11 -0400112 mDivider.setVisibility(View.VISIBLE);
Leon Scrogginse4b3bda2009-06-09 15:46:41 -0400113 mRtButton.setVisibility(View.VISIBLE);
114 mLftButton.setImageDrawable(mBookmarkDrawable);
Leon Scroggins81db3662009-06-04 17:45:11 -0400115 mInLoad = false;
Leon Scrogginsd7c3dd52009-07-20 13:38:47 -0400116 if (!mTitleSet) {
117 mTitle.setText(mUrl.getText());
118 mUrl.setText(null);
119 mTitleSet = true;
120 }
Leon Scroggins81db3662009-06-04 17:45:11 -0400121 } else {
122 mCircularProgress.setProgress(newProgress);
123 mHorizontalProgress.setProgress(newProgress);
124 mCircularProgress.setVisibility(View.VISIBLE);
125 mHorizontalProgress.setVisibility(View.VISIBLE);
126 mDivider.setVisibility(View.GONE);
Leon Scrogginse4b3bda2009-06-09 15:46:41 -0400127 mRtButton.setVisibility(View.GONE);
128 if (mBookmarkDrawable == null) {
129 // The drawable was assigned in the xml file, so it already
130 // exists. Keep a pointer to it when we switch to the resource
131 // so we can easily switch back.
132 mBookmarkDrawable = mLftButton.getDrawable();
133 }
134 mLftButton.setImageResource(
135 com.android.internal.R.drawable.ic_menu_stop);
Leon Scroggins81db3662009-06-04 17:45:11 -0400136 mInLoad = true;
137 }
138 }
139
140 /* package */ void setTitleAndUrl(CharSequence title, CharSequence url) {
Leon Scroggins176215d2009-06-15 12:38:58 -0400141 if (url != null) {
142 url = BrowserActivity.buildTitleUrl(url.toString());
143 }
Leon Scrogginsd7c3dd52009-07-20 13:38:47 -0400144 if (null == title) {
145 if (mInLoad) {
146 mTitleSet = false;
147 mTitle.setText(R.string.title_bar_loading);
148 } else {
149 // If the page has no title, put the url in the title space
150 // and leave the url blank.
151 mTitle.setText(url);
152 mUrl.setText(null);
153 mTitleSet = true;
154 return;
155 }
156 } else {
157 mTitle.setText(title);
158 mTitleSet = true;
159 }
Leon Scroggins176215d2009-06-15 12:38:58 -0400160 mUrl.setText(url);
Leon Scroggins81db3662009-06-04 17:45:11 -0400161 }
162
163 /* package */ void setToTabPicker() {
164 mTitle.setText(R.string.tab_picker_title);
165 setFavicon(null);
166 setLock(null);
167 mCircularProgress.setVisibility(View.GONE);
168 mHorizontalProgress.setVisibility(View.GONE);
169 }
170}