blob: d6b5f4df7016f64b18b03ecaab3049d2a3fbcd80 [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;
45 private StopProgressView mStopButton;
46 private ImageView mVoiceButton;
47 private boolean mInLoad;
48 private View mContainer;
49 private boolean mHasLockIcon;
50
51 public TitleBarPhone(Activity activity, UiController controller, PhoneUi ui) {
52 super(activity, controller, ui);
53 mActivity = activity;
54 initLayout(activity, R.layout.title_bar);
55 }
56
57 @Override
58 protected void initLayout(Context context, int layoutId) {
59 super.initLayout(context, layoutId);
60 mContainer = findViewById(R.id.taburlbar);
61 mLockIcon = (ImageView) findViewById(R.id.lock);
62 mFavicon = (ImageView) findViewById(R.id.favicon);
63 mStopButton = (StopProgressView) findViewById(R.id.stop);
64 mStopButton.setOnClickListener(this);
65 mVoiceButton = (ImageView) findViewById(R.id.voice);
66 mVoiceButton.setOnClickListener(this);
67 setFocusState(false);
68 }
69
70 @Override
71 public int getEmbeddedHeight() {
72 int height = mContainer.getHeight();
73 return height;
74 }
75
76 @Override
77 public void createContextMenu(ContextMenu menu) {
78 MenuInflater inflater = mActivity.getMenuInflater();
79 inflater.inflate(R.menu.title_context, menu);
80 mActivity.onCreateContextMenu(menu, this, null);
81 }
82
83 @Override
84 public void setInVoiceMode(boolean voicemode, List<String> voiceResults) {
85 super.setInVoiceMode(voicemode, voiceResults);
86 }
87
88 @Override
89 protected void setSearchMode(boolean voiceSearchEnabled) {
90 boolean showvoicebutton = voiceSearchEnabled &&
91 mUiController.supportsVoiceSearch();
92 mVoiceButton.setVisibility(showvoicebutton ? View.VISIBLE :
93 View.GONE);
94 }
95
96 @Override
97 protected void setFocusState(boolean focus) {
98 super.setFocusState(focus);
99 if (focus) {
100 mHasLockIcon = (mLockIcon.getVisibility() == View.VISIBLE);
101 mFavicon.setVisibility(View.GONE);
102 mLockIcon.setVisibility(View.GONE);
103 mStopButton.setVisibility(View.GONE);
104 mVoiceButton.setVisibility(View.VISIBLE);
105 } else {
106 mFavicon.setVisibility(View.VISIBLE);
107 mLockIcon.setVisibility(mHasLockIcon ? View.VISIBLE : View.GONE);
108 if (mInLoad) {
109 mStopButton.setVisibility(View.VISIBLE);
110 } else {
111 mStopButton.setVisibility(View.GONE);
112 }
113 mVoiceButton.setVisibility(View.GONE);
114 }
115 }
116
117 /**
118 * Update the progress, from 0 to 100.
119 */
120 @Override
121 void setProgress(int newProgress) {
122 if (newProgress >= PROGRESS_MAX) {
123 mInLoad = false;
124 setFocusState(mUrlInput.hasFocus());
125 } else {
126 if (!mInLoad) {
127 mInLoad = true;
128 setFocusState(mUrlInput.hasFocus());
129 }
130 }
131 }
132
133 /**
134 * Update the text displayed in the title bar.
135 * @param title String to display. If null, the new tab string will be
136 * shown.
137 */
138 @Override
139 void setDisplayTitle(String title) {
140 if (title == null) {
141 mUrlInput.setText(R.string.new_tab);
142 } else {
143 mUrlInput.setText(title);
144 }
145 }
146
147 @Override
148 public void onFocusChange(View v, boolean hasFocus) {
149 if (v == mUrlInput) {
150 if (hasFocus) {
151 mActivity.closeOptionsMenu();
152 }
153 }
154 super.onFocusChange(v, hasFocus);
Michael Kolbfdb70242011-03-24 09:41:11 -0700155 if (mUseQuickControls && !hasFocus) {
156 mBaseUi.hideTitleBar();
157 }
Michael Kolb11d19782011-03-20 10:17:40 -0700158 }
159
160 @Override
161 public void onClick(View v) {
162 if (v == mStopButton) {
163 mUiController.stopLoading();
164 } else if (v == mVoiceButton) {
165 mUiController.startVoiceSearch();
166 } else {
167 super.onClick(v);
168 }
169 }
170
Michael Kolbfdb70242011-03-24 09:41:11 -0700171 @Override
172 void startEditingUrl(boolean clearInput) {
173 // editing takes preference of progress
174 mContainer.setVisibility(View.VISIBLE);
175 if (!mUrlInput.hasFocus()) {
176 mUrlInput.requestFocus();
177 }
178 if (clearInput) {
179 mUrlInput.setText("");
180 } else if (mInVoiceMode) {
181 mUrlInput.showDropDown();
182 }
183 }
184
185 @Override
186 void setTitleGravity(int gravity) {
187 if (mUseQuickControls) {
188 FrameLayout.LayoutParams lp =
189 (FrameLayout.LayoutParams) getLayoutParams();
190 lp.gravity = gravity;
191 setLayoutParams(lp);
192 } else {
193 super.setTitleGravity(gravity);
194 }
195 }
196
197 @Override
198 protected void setUseQuickControls(boolean useQuickControls) {
199 mUseQuickControls = useQuickControls;
200 setLayoutParams(makeLayoutParams());
201 }
202
203 private ViewGroup.LayoutParams makeLayoutParams() {
204 if (mUseQuickControls) {
205 return new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT,
206 LayoutParams.WRAP_CONTENT);
207 } else {
208 return new AbsoluteLayout.LayoutParams(
209 LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT,
210 0, 0);
211 }
212 }
213
Michael Kolb11d19782011-03-20 10:17:40 -0700214}