blob: 40c1bf6646555ca98cf2d1ea5f447aa7f1901a6c [file] [log] [blame]
Michael Kolbfe251992010-07-08 15:41:55 -07001/*
2 * Copyright (C) 2010 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
Michael Kolb21ce4d22010-09-15 14:55:05 -070019import com.android.browser.SuggestionsAdapter.CompletionListener;
John Reck87369ea2011-01-23 15:20:38 -080020import com.android.browser.SuggestionsAdapter.SuggestItem;
Narayan Kamath80aad8d2011-02-23 12:01:13 +000021import com.android.browser.autocomplete.SuggestiveAutoCompleteTextView;
John Reckb77bdc42011-01-24 13:24:48 -080022import com.android.browser.search.SearchEngine;
23import com.android.browser.search.SearchEngineInfo;
24import com.android.browser.search.SearchEngines;
Michael Kolb21ce4d22010-09-15 14:55:05 -070025
Michael Kolbfe251992010-07-08 15:41:55 -070026import android.content.Context;
Michael Kolb21ce4d22010-09-15 14:55:05 -070027import android.content.res.Configuration;
John Reckdcda1d52010-11-29 10:05:54 -080028import android.text.TextUtils;
Michael Kolbfe251992010-07-08 15:41:55 -070029import android.util.AttributeSet;
John Reckb77bdc42011-01-24 13:24:48 -080030import android.util.Patterns;
Michael Kolbfe251992010-07-08 15:41:55 -070031import android.view.KeyEvent;
Michael Kolbfe251992010-07-08 15:41:55 -070032import android.view.View;
Michael Kolbfe251992010-07-08 15:41:55 -070033import android.view.inputmethod.InputMethodManager;
John Reck87369ea2011-01-23 15:20:38 -080034import android.widget.AdapterView;
35import android.widget.AdapterView.OnItemClickListener;
Michael Kolbfe251992010-07-08 15:41:55 -070036import android.widget.TextView;
Michael Kolbed217742010-08-10 17:52:34 -070037import android.widget.TextView.OnEditorActionListener;
Michael Kolbfe251992010-07-08 15:41:55 -070038
Michael Kolbcfa3af52010-12-14 10:36:11 -080039import java.util.List;
40
Michael Kolbfe251992010-07-08 15:41:55 -070041/**
42 * url/search input view
43 * handling suggestions
44 */
Narayan Kamath80aad8d2011-02-23 12:01:13 +000045public class UrlInputView extends SuggestiveAutoCompleteTextView
Michael Kolbdc2ee1b2011-02-14 14:34:40 -080046 implements OnEditorActionListener,
John Reck87369ea2011-01-23 15:20:38 -080047 CompletionListener, OnItemClickListener {
Michael Kolbfe251992010-07-08 15:41:55 -070048
Michael Kolb257cc2c2010-12-09 09:45:52 -080049
50 static final String TYPED = "browser-type";
51 static final String SUGGESTED = "browser-suggest";
Michael Kolbbd2dd642011-01-13 13:01:30 -080052 static final String VOICE = "voice-search";
Michael Kolb257cc2c2010-12-09 09:45:52 -080053
Michael Kolbfe251992010-07-08 15:41:55 -070054 private UrlInputListener mListener;
55 private InputMethodManager mInputManager;
56 private SuggestionsAdapter mAdapter;
Michael Kolb21ce4d22010-09-15 14:55:05 -070057 private View mContainer;
58 private boolean mLandscape;
John Reckb77bdc42011-01-24 13:24:48 -080059 private boolean mIncognitoMode;
Michael Kolbdc2ee1b2011-02-14 14:34:40 -080060 private boolean mNeedsUpdate;
Michael Kolbfe251992010-07-08 15:41:55 -070061
62 public UrlInputView(Context context, AttributeSet attrs, int defStyle) {
63 super(context, attrs, defStyle);
64 init(context);
65 }
66
67 public UrlInputView(Context context, AttributeSet attrs) {
68 super(context, attrs);
69 init(context);
70 }
71
72 public UrlInputView(Context context) {
73 super(context);
74 init(context);
75 }
76
77 private void init(Context ctx) {
Michael Kolbfe251992010-07-08 15:41:55 -070078 mInputManager = (InputMethodManager) ctx.getSystemService(Context.INPUT_METHOD_SERVICE);
Michael Kolbed217742010-08-10 17:52:34 -070079 setOnEditorActionListener(this);
Michael Kolb21ce4d22010-09-15 14:55:05 -070080 mAdapter = new SuggestionsAdapter(ctx, this);
Michael Kolbfe251992010-07-08 15:41:55 -070081 setAdapter(mAdapter);
Michael Kolbba99c5d2010-11-29 14:57:41 -080082 setSelectAllOnFocus(true);
Michael Kolb21ce4d22010-09-15 14:55:05 -070083 onConfigurationChanged(ctx.getResources().getConfiguration());
John Reck35defff2010-11-11 14:06:45 -080084 setThreshold(1);
John Reck87369ea2011-01-23 15:20:38 -080085 setOnItemClickListener(this);
Michael Kolbdc2ee1b2011-02-14 14:34:40 -080086 mNeedsUpdate = false;
87 }
88
89 /**
90 * check if focus change requires a title bar update
91 */
92 boolean needsUpdate() {
93 return mNeedsUpdate;
94 }
95
96 /**
97 * clear the focus change needs title bar update flag
98 */
99 void clearNeedsUpdate() {
100 mNeedsUpdate = false;
Michael Kolb21ce4d22010-09-15 14:55:05 -0700101 }
102
Michael Kolbba99c5d2010-11-29 14:57:41 -0800103 void setController(UiController controller) {
104 UrlSelectionActionMode urlSelectionMode
105 = new UrlSelectionActionMode(controller);
106 setCustomSelectionActionModeCallback(urlSelectionMode);
107 }
108
Michael Kolb91902d52011-01-26 17:43:48 -0800109 void setUseQuickControls(boolean useQuickControls) {
Michael Kolb91902d52011-01-26 17:43:48 -0800110 mAdapter.setReverseResults(useQuickControls);
111 }
112
Michael Kolb21ce4d22010-09-15 14:55:05 -0700113 void setContainer(View container) {
114 mContainer = container;
115 }
116
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800117 public void setUrlInputListener(UrlInputListener listener) {
118 mListener = listener;
119 }
120
Michael Kolbcfa3af52010-12-14 10:36:11 -0800121 void setVoiceResults(List<String> voiceResults) {
122 mAdapter.setVoiceResults(voiceResults);
Michael Kolbcfa3af52010-12-14 10:36:11 -0800123 }
124
Michael Kolb21ce4d22010-09-15 14:55:05 -0700125 @Override
126 protected void onConfigurationChanged(Configuration config) {
John Reck35defff2010-11-11 14:06:45 -0800127 super.onConfigurationChanged(config);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700128 mLandscape = (config.orientation &
Michael Kolb31d469b2010-12-09 20:49:54 -0800129 Configuration.ORIENTATION_LANDSCAPE) != 0;
John Reck35defff2010-11-11 14:06:45 -0800130 mAdapter.setLandscapeMode(mLandscape);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700131 if (isPopupShowing() && (getVisibility() == View.VISIBLE)) {
John Reck35defff2010-11-11 14:06:45 -0800132 setupDropDown();
John Reckad373302010-12-17 15:28:13 -0800133 performFiltering(getText(), 0);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700134 }
135 }
136
137 @Override
138 public void showDropDown() {
John Reck35defff2010-11-11 14:06:45 -0800139 setupDropDown();
140 super.showDropDown();
141 }
142
143 @Override
144 public void dismissDropDown() {
145 super.dismissDropDown();
146 mAdapter.clearCache();
147 }
148
149 private void setupDropDown() {
John Reck92026732011-02-15 10:12:30 -0800150 int width = mContainer != null ? mContainer.getWidth() : getWidth();
Michael Kolbc5998c22010-10-10 16:04:35 -0700151 if (width != getDropDownWidth()) {
152 setDropDownWidth(width);
153 }
154 if (getLeft() != -getDropDownHorizontalOffset()) {
155 setDropDownHorizontalOffset(-getLeft());
156 }
Michael Kolb513286f2010-09-09 12:55:12 -0700157 }
158
159 @Override
Michael Kolbed217742010-08-10 17:52:34 -0700160 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
Michael Kolb257cc2c2010-12-09 09:45:52 -0800161 finishInput(getText().toString(), null, TYPED);
Michael Kolbed217742010-08-10 17:52:34 -0700162 return true;
163 }
Michael Kolb21ce4d22010-09-15 14:55:05 -0700164
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800165 void forceFilter() {
166 performFiltering(getText().toString(), 0);
167 showDropDown();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700168 }
169
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800170 void forceIme() {
John Reck92026732011-02-15 10:12:30 -0800171 mInputManager.focusIn(this);
Michael Kolbfe251992010-07-08 15:41:55 -0700172 mInputManager.showSoftInput(this, 0);
173 }
174
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800175 void hideIME() {
176 mInputManager.hideSoftInputFromWindow(getWindowToken(), 0);
177 }
178
Michael Kolb257cc2c2010-12-09 09:45:52 -0800179 private void finishInput(String url, String extra, String source) {
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800180 mNeedsUpdate = true;
181 dismissDropDown();
Michael Kolbfe251992010-07-08 15:41:55 -0700182 mInputManager.hideSoftInputFromWindow(getWindowToken(), 0);
John Reckdcda1d52010-11-29 10:05:54 -0800183 if (TextUtils.isEmpty(url)) {
Michael Kolbfe251992010-07-08 15:41:55 -0700184 mListener.onDismiss();
185 } else {
John Reckb77bdc42011-01-24 13:24:48 -0800186 if (mIncognitoMode && isSearch(url)) {
187 // To prevent logging, intercept this request
188 // TODO: This is a quick hack, refactor this
189 SearchEngine searchEngine = BrowserSettings.getInstance()
190 .getSearchEngine();
191 if (searchEngine == null) return;
192 SearchEngineInfo engineInfo = SearchEngines
193 .getSearchEngineInfo(mContext, searchEngine.getName());
194 if (engineInfo == null) return;
195 url = engineInfo.getSearchUriForQuery(url);
196 // mLister.onAction can take it from here without logging
197 }
Michael Kolb257cc2c2010-12-09 09:45:52 -0800198 mListener.onAction(url, extra, source);
Michael Kolbfe251992010-07-08 15:41:55 -0700199 }
Michael Kolbfe251992010-07-08 15:41:55 -0700200 }
201
John Reckb77bdc42011-01-24 13:24:48 -0800202 boolean isSearch(String inUrl) {
203 String url = UrlUtils.fixUrl(inUrl).trim();
204 if (TextUtils.isEmpty(url)) return false;
205
206 if (Patterns.WEB_URL.matcher(url).matches()
207 || UrlUtils.ACCEPTED_URI_SCHEMA.matcher(url).matches()) {
208 return false;
209 }
210 return true;
211 }
212
Michael Kolb21ce4d22010-09-15 14:55:05 -0700213 // Completion Listener
214
215 @Override
216 public void onSearch(String search) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800217 mListener.onCopySuggestion(search);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700218 }
219
220 @Override
Michael Kolbbd2dd642011-01-13 13:01:30 -0800221 public void onSelect(String url, int type, String extra) {
222 finishInput(url, extra, (type == SuggestionsAdapter.TYPE_VOICE_SEARCH)
223 ? VOICE : SUGGESTED);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700224 }
225
Michael Kolbfe251992010-07-08 15:41:55 -0700226 @Override
John Reck87369ea2011-01-23 15:20:38 -0800227 public void onItemClick(
228 AdapterView<?> parent, View view, int position, long id) {
229 SuggestItem item = mAdapter.getItem(position);
230 onSelect((TextUtils.isEmpty(item.url) ? item.title : item.url),
231 item.type, item.extra);
232 }
233
Michael Kolbfe251992010-07-08 15:41:55 -0700234 interface UrlInputListener {
Michael Kolb21ce4d22010-09-15 14:55:05 -0700235
Michael Kolbfe251992010-07-08 15:41:55 -0700236 public void onDismiss();
Michael Kolb21ce4d22010-09-15 14:55:05 -0700237
Michael Kolb257cc2c2010-12-09 09:45:52 -0800238 public void onAction(String text, String extra, String source);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700239
Michael Kolb7cdc4902011-02-03 17:54:40 -0800240 public void onCopySuggestion(String text);
Michael Kolbfe251992010-07-08 15:41:55 -0700241
242 }
243
John Reck117f07d2011-01-24 09:39:03 -0800244 public void setIncognitoMode(boolean incognito) {
John Reckb77bdc42011-01-24 13:24:48 -0800245 mIncognitoMode = incognito;
246 mAdapter.setIncognitoMode(mIncognitoMode);
John Reck117f07d2011-01-24 09:39:03 -0800247 }
248
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800249 @Override
250 public boolean onKeyDown(int keyCode, KeyEvent evt) {
251 if (keyCode == KeyEvent.KEYCODE_ESCAPE && !isInTouchMode()) {
252 finishInput(null, null, null);
253 return true;
254 }
255 return super.onKeyDown(keyCode, evt);
256 }
257
Narayan Kamath80aad8d2011-02-23 12:01:13 +0000258 public SuggestionsAdapter getAdapter() {
259 return mAdapter;
260 }
Michael Kolbfe251992010-07-08 15:41:55 -0700261}