blob: 6a1b340ed9b1f241def2d2315436f3b8b057f45f [file] [log] [blame]
John Reck0f602f32011-07-07 15:38:43 -07001/*
2 * Copyright (C) 2011 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 */
16package com.android.browser;
17
18import android.app.SearchManager;
19import android.content.Context;
20import android.content.Intent;
21import android.graphics.Bitmap;
22import android.graphics.drawable.Drawable;
23import android.os.Bundle;
24import android.speech.RecognizerResultsIntent;
25import android.util.AttributeSet;
26import android.view.KeyEvent;
John Reck0f602f32011-07-07 15:38:43 -070027import android.view.View;
28import android.view.View.OnClickListener;
29import android.view.View.OnFocusChangeListener;
John Reckfa6177a2011-11-15 14:51:11 -080030import android.webkit.WebView;
John Reck0f602f32011-07-07 15:38:43 -070031import android.widget.ImageView;
32import android.widget.LinearLayout;
John Reck0f602f32011-07-07 15:38:43 -070033
34import com.android.browser.UI.DropdownChangeListener;
35import com.android.browser.UrlInputView.UrlInputListener;
36import com.android.browser.autocomplete.SuggestedTextController.TextChangeWatcher;
37
38import java.util.List;
39
John Reck42229bc2011-08-19 13:26:43 -070040public class NavigationBarBase extends LinearLayout implements
41 OnClickListener, UrlInputListener, OnFocusChangeListener,
42 TextChangeWatcher {
John Reck0f602f32011-07-07 15:38:43 -070043
44 protected BaseUi mBaseUi;
45 protected TitleBar mTitleBar;
46 protected UiController mUiController;
47 protected UrlInputView mUrlInput;
48 protected boolean mInVoiceMode = false;
49
50 private ImageView mFavicon;
51 private ImageView mLockIcon;
John Reck0f602f32011-07-07 15:38:43 -070052
53 public NavigationBarBase(Context context) {
54 super(context);
55 }
56
57 public NavigationBarBase(Context context, AttributeSet attrs) {
58 super(context, attrs);
59 }
60
61 public NavigationBarBase(Context context, AttributeSet attrs, int defStyle) {
62 super(context, attrs, defStyle);
63 }
64
65 @Override
66 protected void onFinishInflate() {
67 super.onFinishInflate();
68 mLockIcon = (ImageView) findViewById(R.id.lock);
69 mFavicon = (ImageView) findViewById(R.id.favicon);
70 mUrlInput = (UrlInputView) findViewById(R.id.url);
71 mUrlInput.setUrlInputListener(this);
John Reck0f602f32011-07-07 15:38:43 -070072 mUrlInput.setOnFocusChangeListener(this);
73 mUrlInput.setSelectAllOnFocus(true);
74 mUrlInput.addQueryTextWatcher(this);
75 }
76
77 public void setTitleBar(TitleBar titleBar) {
78 mTitleBar = titleBar;
79 mBaseUi = mTitleBar.getUi();
80 mUiController = mTitleBar.getUiController();
Michael Kolbb2e91fd2011-07-14 13:47:29 -070081 mUrlInput.setController(mUiController);
John Reck0f602f32011-07-07 15:38:43 -070082 }
83
84 public void setLock(Drawable d) {
85 if (mLockIcon == null) return;
86 if (d == null) {
87 mLockIcon.setVisibility(View.GONE);
88 } else {
89 mLockIcon.setImageDrawable(d);
90 mLockIcon.setVisibility(View.VISIBLE);
91 }
92 }
93
94 public void setFavicon(Bitmap icon) {
95 if (mFavicon == null) return;
96 mFavicon.setImageDrawable(mBaseUi.getFaviconDrawable(icon));
97 }
98
John Reck0f602f32011-07-07 15:38:43 -070099 @Override
100 public void onClick(View v) {
John Reck0f602f32011-07-07 15:38:43 -0700101 }
102
103 @Override
104 public void onFocusChange(View view, boolean hasFocus) {
105 // if losing focus and not in touch mode, leave as is
106 if (hasFocus || view.isInTouchMode() || mUrlInput.needsUpdate()) {
107 setFocusState(hasFocus);
108 }
109 if (hasFocus) {
110 mBaseUi.showTitleBar();
111 mUrlInput.forceIme();
112 if (mInVoiceMode) {
113 mUrlInput.forceFilter();
114 }
115 } else if (!mUrlInput.needsUpdate()) {
116 mUrlInput.dismissDropDown();
117 mUrlInput.hideIME();
118 if (mUrlInput.getText().length() == 0) {
119 Tab currentTab = mUiController.getTabControl().getCurrentTab();
120 if (currentTab != null) {
John Reck434e9f82011-08-10 18:16:52 -0700121 setDisplayTitle(currentTab.getUrl());
John Reck0f602f32011-07-07 15:38:43 -0700122 }
123 }
124 mBaseUi.suggestHideTitleBar();
125 }
126 mUrlInput.clearNeedsUpdate();
127 }
128
129 protected void setFocusState(boolean focus) {
130 }
131
132 protected void setSearchMode(boolean voiceSearchEnabled) {}
133
134 public boolean isEditingUrl() {
135 return mUrlInput.hasFocus();
136 }
137
138 void stopEditingUrl() {
139 mUrlInput.clearFocus();
140 }
141
142 void setDisplayTitle(String title) {
143 if (!isEditingUrl()) {
144 mUrlInput.setText(title, false);
145 }
146 }
147
148 // UrlInput text watcher
149
150 @Override
151 public void onTextChanged(String newText) {
152 if (mUrlInput.hasFocus()) {
153 // clear voice mode when user types
154 setInVoiceMode(false, null);
155 }
156 }
157
158 // voicesearch
159
160 public void setInVoiceMode(boolean voicemode, List<String> voiceResults) {
161 mInVoiceMode = voicemode;
162 mUrlInput.setVoiceResults(voiceResults);
163 }
164
165 void setIncognitoMode(boolean incognito) {
166 mUrlInput.setIncognitoMode(incognito);
167 }
168
169 void clearCompletions() {
170 mUrlInput.setSuggestedText(null);
171 }
172
173 // UrlInputListener implementation
174
175 /**
176 * callback from suggestion dropdown
177 * user selected a suggestion
178 */
179 @Override
180 public void onAction(String text, String extra, String source) {
John Reckfa6177a2011-11-15 14:51:11 -0800181 WebView currentTopWebView = mUiController.getCurrentTopWebView();
182 if (currentTopWebView != null) {
183 currentTopWebView.requestFocus();
184 }
John Reck0f602f32011-07-07 15:38:43 -0700185 if (UrlInputView.TYPED.equals(source)) {
186 String url = UrlUtils.smartUrlFilter(text, false);
187 Tab t = mBaseUi.getActiveTab();
188 // Only shortcut javascript URIs for now, as there is special
189 // logic in UrlHandler for other schemas
190 if (url != null && t != null && url.startsWith("javascript:")) {
191 mUiController.loadUrl(t, url);
192 setDisplayTitle(text);
193 return;
194 }
195 }
196 Intent i = new Intent();
197 String action = null;
198 if (UrlInputView.VOICE.equals(source)) {
199 action = RecognizerResultsIntent.ACTION_VOICE_SEARCH_RESULTS;
200 source = null;
201 } else {
202 action = Intent.ACTION_SEARCH;
203 }
204 i.setAction(action);
205 i.putExtra(SearchManager.QUERY, text);
206 if (extra != null) {
207 i.putExtra(SearchManager.EXTRA_DATA_KEY, extra);
208 }
209 if (source != null) {
210 Bundle appData = new Bundle();
211 appData.putString(com.android.common.Search.SOURCE, source);
212 i.putExtra(SearchManager.APP_DATA, appData);
213 }
214 mUiController.handleNewIntent(i);
215 setDisplayTitle(text);
216 }
217
218 @Override
219 public void onDismiss() {
220 final Tab currentTab = mBaseUi.getActiveTab();
221 mBaseUi.hideTitleBar();
222 post(new Runnable() {
223 public void run() {
224 clearFocus();
225 if ((currentTab != null) && !mInVoiceMode) {
226 setDisplayTitle(currentTab.getUrl());
227 }
228 }
229 });
230 }
231
232 /**
233 * callback from the suggestion dropdown
234 * copy text to input field and stay in edit mode
235 */
236 @Override
237 public void onCopySuggestion(String text) {
238 mUrlInput.setText(text, true);
239 if (text != null) {
240 mUrlInput.setSelection(text.length());
241 }
242 }
243
244 public void setCurrentUrlIsBookmark(boolean isBookmark) {
245 }
246
247 @Override
248 public boolean dispatchKeyEventPreIme(KeyEvent evt) {
249 if (evt.getKeyCode() == KeyEvent.KEYCODE_BACK) {
250 // catch back key in order to do slightly more cleanup than usual
251 mUrlInput.clearFocus();
252 return true;
253 }
254 return super.dispatchKeyEventPreIme(evt);
255 }
256
257 void registerDropdownChangeListener(DropdownChangeListener d) {
258 mUrlInput.registerDropdownChangeListener(d);
259 }
260
261 /**
262 * called from the Ui when the user wants to edit
263 * @param clearInput clear the input field
264 */
265 void startEditingUrl(boolean clearInput) {
266 // editing takes preference of progress
267 setVisibility(View.VISIBLE);
268 if (mTitleBar.useQuickControls()) {
269 mTitleBar.getProgressView().setVisibility(View.GONE);
270 }
271 if (!mUrlInput.hasFocus()) {
272 mUrlInput.requestFocus();
273 }
274 if (clearInput) {
275 mUrlInput.setText("");
276 } else if (mInVoiceMode) {
277 mUrlInput.showDropDown();
278 }
279 }
280
281 public void onProgressStarted() {
282 }
283
284 public void onProgressStopped() {
285 }
286
John Reck58891902011-08-11 17:48:53 -0700287 public boolean isMenuShowing() {
John Reck42229bc2011-08-19 13:26:43 -0700288 return false;
John Reck58891902011-08-11 17:48:53 -0700289 }
290
John Reck419f6b42011-08-16 16:10:51 -0700291 public void onTabDataChanged(Tab tab) {
292 }
293
John Reck0f602f32011-07-07 15:38:43 -0700294}