blob: 3edd831f5bc50897a5b0b0561d21988ddc9ca25e [file] [log] [blame]
Michael Kolb11d19782011-03-20 10:17:40 -07001/*
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 com.android.browser.autocomplete.SuggestedTextController.TextChangeWatcher;
20import com.android.browser.view.StopProgressView;
21
22import android.app.Activity;
23import android.content.Context;
24import android.view.ContextMenu;
25import android.view.MenuInflater;
26import android.view.View;
27import android.view.View.OnClickListener;
28import android.view.View.OnFocusChangeListener;
Michael Kolbfdb70242011-03-24 09:41:11 -070029import android.view.ViewGroup;
30import android.widget.AbsoluteLayout;
31import android.widget.FrameLayout;
Michael Kolb11d19782011-03-20 10:17:40 -070032import android.widget.ImageView;
Michael Kolbfdb70242011-03-24 09:41:11 -070033import android.widget.RelativeLayout.LayoutParams;
Michael Kolb11d19782011-03-20 10:17:40 -070034
35import java.util.List;
36
37/**
38 * This class represents a title bar for a particular "tab" or "window" in the
39 * browser.
40 */
41public class TitleBarPhone extends TitleBarBase implements OnFocusChangeListener,
42 OnClickListener, TextChangeWatcher {
43
44 private Activity mActivity;
Michael Kolbc16c5952011-03-29 15:37:03 -070045 private ImageView mStopButton;
46 private PageProgressView mProgress;
Michael Kolb11d19782011-03-20 10:17:40 -070047 private ImageView mVoiceButton;
48 private boolean mInLoad;
49 private View mContainer;
50 private boolean mHasLockIcon;
51
52 public TitleBarPhone(Activity activity, UiController controller, PhoneUi ui) {
53 super(activity, controller, ui);
54 mActivity = activity;
55 initLayout(activity, R.layout.title_bar);
56 }
57
58 @Override
59 protected void initLayout(Context context, int layoutId) {
60 super.initLayout(context, layoutId);
61 mContainer = findViewById(R.id.taburlbar);
62 mLockIcon = (ImageView) findViewById(R.id.lock);
63 mFavicon = (ImageView) findViewById(R.id.favicon);
Michael Kolbc16c5952011-03-29 15:37:03 -070064 mStopButton = (ImageView) findViewById(R.id.stop);
Michael Kolb11d19782011-03-20 10:17:40 -070065 mStopButton.setOnClickListener(this);
Michael Kolbc16c5952011-03-29 15:37:03 -070066 mProgress = (PageProgressView) findViewById(R.id.progress);
Michael Kolb11d19782011-03-20 10:17:40 -070067 mVoiceButton = (ImageView) findViewById(R.id.voice);
68 mVoiceButton.setOnClickListener(this);
69 setFocusState(false);
70 }
71
72 @Override
73 public int getEmbeddedHeight() {
74 int height = mContainer.getHeight();
75 return height;
76 }
77
78 @Override
79 public void createContextMenu(ContextMenu menu) {
80 MenuInflater inflater = mActivity.getMenuInflater();
81 inflater.inflate(R.menu.title_context, menu);
82 mActivity.onCreateContextMenu(menu, this, null);
83 }
84
85 @Override
86 public void setInVoiceMode(boolean voicemode, List<String> voiceResults) {
87 super.setInVoiceMode(voicemode, voiceResults);
88 }
89
90 @Override
91 protected void setSearchMode(boolean voiceSearchEnabled) {
92 boolean showvoicebutton = voiceSearchEnabled &&
93 mUiController.supportsVoiceSearch();
94 mVoiceButton.setVisibility(showvoicebutton ? View.VISIBLE :
95 View.GONE);
96 }
97
98 @Override
99 protected void setFocusState(boolean focus) {
100 super.setFocusState(focus);
101 if (focus) {
102 mHasLockIcon = (mLockIcon.getVisibility() == View.VISIBLE);
103 mFavicon.setVisibility(View.GONE);
104 mLockIcon.setVisibility(View.GONE);
105 mStopButton.setVisibility(View.GONE);
106 mVoiceButton.setVisibility(View.VISIBLE);
107 } else {
108 mFavicon.setVisibility(View.VISIBLE);
109 mLockIcon.setVisibility(mHasLockIcon ? View.VISIBLE : View.GONE);
110 if (mInLoad) {
111 mStopButton.setVisibility(View.VISIBLE);
112 } else {
113 mStopButton.setVisibility(View.GONE);
114 }
115 mVoiceButton.setVisibility(View.GONE);
116 }
117 }
118
119 /**
120 * Update the progress, from 0 to 100.
121 */
122 @Override
123 void setProgress(int newProgress) {
Michael Kolbc16c5952011-03-29 15:37:03 -0700124 boolean blockvisuals = mUseQuickControls && isEditingUrl();
Michael Kolb11d19782011-03-20 10:17:40 -0700125 if (newProgress >= PROGRESS_MAX) {
126 mInLoad = false;
Michael Kolbc16c5952011-03-29 15:37:03 -0700127 if (!blockvisuals) {
128 mProgress.setProgress(PageProgressView.MAX_PROGRESS);
129 mProgress.setVisibility(View.GONE);
130 }
Michael Kolb11d19782011-03-20 10:17:40 -0700131 setFocusState(mUrlInput.hasFocus());
132 } else {
133 if (!mInLoad) {
134 mInLoad = true;
Michael Kolbc16c5952011-03-29 15:37:03 -0700135 if (!blockvisuals) {
136 mProgress.setVisibility(View.VISIBLE);
137 }
Michael Kolb11d19782011-03-20 10:17:40 -0700138 setFocusState(mUrlInput.hasFocus());
139 }
Michael Kolbc16c5952011-03-29 15:37:03 -0700140 mProgress.setProgress(newProgress * PageProgressView.MAX_PROGRESS
141 / PROGRESS_MAX);
Michael Kolb11d19782011-03-20 10:17:40 -0700142 }
143 }
144
145 /**
146 * Update the text displayed in the title bar.
147 * @param title String to display. If null, the new tab string will be
148 * shown.
149 */
150 @Override
151 void setDisplayTitle(String title) {
152 if (title == null) {
153 mUrlInput.setText(R.string.new_tab);
154 } else {
155 mUrlInput.setText(title);
156 }
157 }
158
159 @Override
160 public void onFocusChange(View v, boolean hasFocus) {
161 if (v == mUrlInput) {
162 if (hasFocus) {
163 mActivity.closeOptionsMenu();
164 }
165 }
166 super.onFocusChange(v, hasFocus);
Michael Kolbfdb70242011-03-24 09:41:11 -0700167 if (mUseQuickControls && !hasFocus) {
168 mBaseUi.hideTitleBar();
169 }
Michael Kolb11d19782011-03-20 10:17:40 -0700170 }
171
172 @Override
173 public void onClick(View v) {
174 if (v == mStopButton) {
175 mUiController.stopLoading();
176 } else if (v == mVoiceButton) {
177 mUiController.startVoiceSearch();
178 } else {
179 super.onClick(v);
180 }
181 }
182
Michael Kolbfdb70242011-03-24 09:41:11 -0700183 @Override
184 void startEditingUrl(boolean clearInput) {
185 // editing takes preference of progress
186 mContainer.setVisibility(View.VISIBLE);
187 if (!mUrlInput.hasFocus()) {
188 mUrlInput.requestFocus();
189 }
190 if (clearInput) {
191 mUrlInput.setText("");
192 } else if (mInVoiceMode) {
193 mUrlInput.showDropDown();
194 }
195 }
196
197 @Override
198 void setTitleGravity(int gravity) {
199 if (mUseQuickControls) {
200 FrameLayout.LayoutParams lp =
201 (FrameLayout.LayoutParams) getLayoutParams();
202 lp.gravity = gravity;
203 setLayoutParams(lp);
204 } else {
205 super.setTitleGravity(gravity);
206 }
207 }
208
209 @Override
210 protected void setUseQuickControls(boolean useQuickControls) {
211 mUseQuickControls = useQuickControls;
212 setLayoutParams(makeLayoutParams());
213 }
214
Michael Kolbc16c5952011-03-29 15:37:03 -0700215 void setShowProgressOnly(boolean progress) {
216 if (progress && !inAutoLogin()) {
217 mContainer.setVisibility(View.GONE);
218 } else {
219 mContainer.setVisibility(View.VISIBLE);
220 }
221 }
222
Michael Kolbfdb70242011-03-24 09:41:11 -0700223 private ViewGroup.LayoutParams makeLayoutParams() {
224 if (mUseQuickControls) {
225 return new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT,
226 LayoutParams.WRAP_CONTENT);
227 } else {
228 return new AbsoluteLayout.LayoutParams(
229 LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT,
230 0, 0);
231 }
232 }
233
Michael Kolb11d19782011-03-20 10:17:40 -0700234}