blob: cf61c45cd8153d3a67be55ae80752860268eecab [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;
Narayan Kamathf3174a52011-11-17 14:43:32 +000025import android.text.Editable;
26import android.text.TextWatcher;
John Reck0f602f32011-07-07 15:38:43 -070027import android.util.AttributeSet;
28import android.view.KeyEvent;
John Reck0f602f32011-07-07 15:38:43 -070029import android.view.View;
30import android.view.View.OnClickListener;
31import android.view.View.OnFocusChangeListener;
John Reckfa6177a2011-11-15 14:51:11 -080032import android.webkit.WebView;
John Reck0f602f32011-07-07 15:38:43 -070033import android.widget.ImageView;
34import android.widget.LinearLayout;
John Reck0f602f32011-07-07 15:38:43 -070035
John Reck0f602f32011-07-07 15:38:43 -070036import com.android.browser.UrlInputView.UrlInputListener;
John Reck0f602f32011-07-07 15:38:43 -070037
38import java.util.List;
39
John Reck42229bc2011-08-19 13:26:43 -070040public class NavigationBarBase extends LinearLayout implements
41 OnClickListener, UrlInputListener, OnFocusChangeListener,
Narayan Kamathf3174a52011-11-17 14:43:32 +000042 TextWatcher {
John Reck0f602f32011-07-07 15:38:43 -070043
44 protected BaseUi mBaseUi;
45 protected TitleBar mTitleBar;
46 protected UiController mUiController;
47 protected UrlInputView mUrlInput;
John Reck0f602f32011-07-07 15:38:43 -070048
49 private ImageView mFavicon;
50 private ImageView mLockIcon;
John Reck0f602f32011-07-07 15:38:43 -070051
52 public NavigationBarBase(Context context) {
53 super(context);
54 }
55
56 public NavigationBarBase(Context context, AttributeSet attrs) {
57 super(context, attrs);
58 }
59
60 public NavigationBarBase(Context context, AttributeSet attrs, int defStyle) {
61 super(context, attrs, defStyle);
62 }
63
64 @Override
65 protected void onFinishInflate() {
66 super.onFinishInflate();
67 mLockIcon = (ImageView) findViewById(R.id.lock);
68 mFavicon = (ImageView) findViewById(R.id.favicon);
69 mUrlInput = (UrlInputView) findViewById(R.id.url);
70 mUrlInput.setUrlInputListener(this);
John Reck0f602f32011-07-07 15:38:43 -070071 mUrlInput.setOnFocusChangeListener(this);
72 mUrlInput.setSelectAllOnFocus(true);
Narayan Kamathf3174a52011-11-17 14:43:32 +000073 mUrlInput.addTextChangedListener(this);
John Reck0f602f32011-07-07 15:38:43 -070074 }
75
76 public void setTitleBar(TitleBar titleBar) {
77 mTitleBar = titleBar;
78 mBaseUi = mTitleBar.getUi();
79 mUiController = mTitleBar.getUiController();
Michael Kolbb2e91fd2011-07-14 13:47:29 -070080 mUrlInput.setController(mUiController);
John Reck0f602f32011-07-07 15:38:43 -070081 }
82
83 public void setLock(Drawable d) {
84 if (mLockIcon == null) return;
85 if (d == null) {
86 mLockIcon.setVisibility(View.GONE);
87 } else {
88 mLockIcon.setImageDrawable(d);
89 mLockIcon.setVisibility(View.VISIBLE);
90 }
91 }
92
93 public void setFavicon(Bitmap icon) {
94 if (mFavicon == null) return;
95 mFavicon.setImageDrawable(mBaseUi.getFaviconDrawable(icon));
96 }
97
John Reck0f602f32011-07-07 15:38:43 -070098 @Override
99 public void onClick(View v) {
John Reck0f602f32011-07-07 15:38:43 -0700100 }
101
102 @Override
103 public void onFocusChange(View view, boolean hasFocus) {
104 // if losing focus and not in touch mode, leave as is
105 if (hasFocus || view.isInTouchMode() || mUrlInput.needsUpdate()) {
106 setFocusState(hasFocus);
107 }
108 if (hasFocus) {
109 mBaseUi.showTitleBar();
John Reck0f602f32011-07-07 15:38:43 -0700110 } else if (!mUrlInput.needsUpdate()) {
111 mUrlInput.dismissDropDown();
112 mUrlInput.hideIME();
113 if (mUrlInput.getText().length() == 0) {
114 Tab currentTab = mUiController.getTabControl().getCurrentTab();
115 if (currentTab != null) {
John Reck434e9f82011-08-10 18:16:52 -0700116 setDisplayTitle(currentTab.getUrl());
John Reck0f602f32011-07-07 15:38:43 -0700117 }
118 }
119 mBaseUi.suggestHideTitleBar();
120 }
121 mUrlInput.clearNeedsUpdate();
122 }
123
124 protected void setFocusState(boolean focus) {
125 }
126
John Reck0f602f32011-07-07 15:38:43 -0700127 public boolean isEditingUrl() {
128 return mUrlInput.hasFocus();
129 }
130
131 void stopEditingUrl() {
132 mUrlInput.clearFocus();
133 }
134
135 void setDisplayTitle(String title) {
136 if (!isEditingUrl()) {
137 mUrlInput.setText(title, false);
138 }
139 }
140
John Reck0f602f32011-07-07 15:38:43 -0700141 void setIncognitoMode(boolean incognito) {
142 mUrlInput.setIncognitoMode(incognito);
143 }
144
145 void clearCompletions() {
Narayan Kamathf3174a52011-11-17 14:43:32 +0000146 mUrlInput.dismissDropDown();
John Reck0f602f32011-07-07 15:38:43 -0700147 }
148
149 // UrlInputListener implementation
150
151 /**
152 * callback from suggestion dropdown
153 * user selected a suggestion
154 */
155 @Override
156 public void onAction(String text, String extra, String source) {
John Reckfa6177a2011-11-15 14:51:11 -0800157 WebView currentTopWebView = mUiController.getCurrentTopWebView();
158 if (currentTopWebView != null) {
159 currentTopWebView.requestFocus();
160 }
John Reck0f602f32011-07-07 15:38:43 -0700161 if (UrlInputView.TYPED.equals(source)) {
162 String url = UrlUtils.smartUrlFilter(text, false);
163 Tab t = mBaseUi.getActiveTab();
164 // Only shortcut javascript URIs for now, as there is special
165 // logic in UrlHandler for other schemas
166 if (url != null && t != null && url.startsWith("javascript:")) {
167 mUiController.loadUrl(t, url);
168 setDisplayTitle(text);
169 return;
170 }
171 }
172 Intent i = new Intent();
Michael Kolb5ff5c8b2012-05-03 11:37:58 -0700173 String action = Intent.ACTION_SEARCH;
John Reck0f602f32011-07-07 15:38:43 -0700174 i.setAction(action);
175 i.putExtra(SearchManager.QUERY, text);
176 if (extra != null) {
177 i.putExtra(SearchManager.EXTRA_DATA_KEY, extra);
178 }
179 if (source != null) {
180 Bundle appData = new Bundle();
181 appData.putString(com.android.common.Search.SOURCE, source);
182 i.putExtra(SearchManager.APP_DATA, appData);
183 }
184 mUiController.handleNewIntent(i);
185 setDisplayTitle(text);
186 }
187
188 @Override
189 public void onDismiss() {
190 final Tab currentTab = mBaseUi.getActiveTab();
191 mBaseUi.hideTitleBar();
192 post(new Runnable() {
193 public void run() {
194 clearFocus();
Michael Kolb5ff5c8b2012-05-03 11:37:58 -0700195 if (currentTab != null) {
John Reck0f602f32011-07-07 15:38:43 -0700196 setDisplayTitle(currentTab.getUrl());
197 }
198 }
199 });
200 }
201
202 /**
203 * callback from the suggestion dropdown
204 * copy text to input field and stay in edit mode
205 */
206 @Override
207 public void onCopySuggestion(String text) {
208 mUrlInput.setText(text, true);
209 if (text != null) {
210 mUrlInput.setSelection(text.length());
211 }
212 }
213
214 public void setCurrentUrlIsBookmark(boolean isBookmark) {
215 }
216
217 @Override
218 public boolean dispatchKeyEventPreIme(KeyEvent evt) {
219 if (evt.getKeyCode() == KeyEvent.KEYCODE_BACK) {
220 // catch back key in order to do slightly more cleanup than usual
221 mUrlInput.clearFocus();
222 return true;
223 }
224 return super.dispatchKeyEventPreIme(evt);
225 }
226
John Reck0f602f32011-07-07 15:38:43 -0700227 /**
228 * called from the Ui when the user wants to edit
229 * @param clearInput clear the input field
230 */
Michael Kolb1f9b3562012-04-24 14:38:34 -0700231 void startEditingUrl(boolean clearInput, boolean forceIME) {
John Reck0f602f32011-07-07 15:38:43 -0700232 // editing takes preference of progress
233 setVisibility(View.VISIBLE);
234 if (mTitleBar.useQuickControls()) {
235 mTitleBar.getProgressView().setVisibility(View.GONE);
236 }
237 if (!mUrlInput.hasFocus()) {
238 mUrlInput.requestFocus();
239 }
240 if (clearInput) {
241 mUrlInput.setText("");
John Reck0f602f32011-07-07 15:38:43 -0700242 }
Michael Kolb1f9b3562012-04-24 14:38:34 -0700243 if (forceIME) {
Michael Kolb4bb6fcb2012-04-13 14:25:27 -0700244 mUrlInput.showIME();
245 }
John Reck0f602f32011-07-07 15:38:43 -0700246 }
247
248 public void onProgressStarted() {
249 }
250
251 public void onProgressStopped() {
252 }
253
John Reck58891902011-08-11 17:48:53 -0700254 public boolean isMenuShowing() {
John Reck42229bc2011-08-19 13:26:43 -0700255 return false;
John Reck58891902011-08-11 17:48:53 -0700256 }
257
John Reck419f6b42011-08-16 16:10:51 -0700258 public void onTabDataChanged(Tab tab) {
259 }
260
Narayan Kamathf3174a52011-11-17 14:43:32 +0000261 @Override
262 public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
263
264 @Override
Michael Kolb5ff5c8b2012-05-03 11:37:58 -0700265 public void onTextChanged(CharSequence s, int start, int before, int count) { }
Narayan Kamathf3174a52011-11-17 14:43:32 +0000266
267 @Override
268 public void afterTextChanged(Editable s) { }
John Reck0f602f32011-07-07 15:38:43 -0700269}