blob: 23e412dd1d05082251c8e641849ea5ee8946da3a [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;
John Reckb77bdc42011-01-24 13:24:48 -080021import com.android.browser.search.SearchEngine;
22import com.android.browser.search.SearchEngineInfo;
23import com.android.browser.search.SearchEngines;
Michael Kolb21ce4d22010-09-15 14:55:05 -070024
Michael Kolbfe251992010-07-08 15:41:55 -070025import android.content.Context;
Michael Kolb21ce4d22010-09-15 14:55:05 -070026import android.content.res.Configuration;
John Reckdcda1d52010-11-29 10:05:54 -080027import android.text.TextUtils;
Michael Kolbfe251992010-07-08 15:41:55 -070028import android.util.AttributeSet;
John Reckb77bdc42011-01-24 13:24:48 -080029import android.util.Patterns;
Michael Kolbfe251992010-07-08 15:41:55 -070030import android.view.KeyEvent;
Michael Kolbfe251992010-07-08 15:41:55 -070031import android.view.View;
Michael Kolba2b2ba82010-08-04 17:54:03 -070032import android.view.View.OnFocusChangeListener;
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.AutoCompleteTextView;
Michael Kolbfe251992010-07-08 15:41:55 -070037import android.widget.TextView;
Michael Kolbed217742010-08-10 17:52:34 -070038import android.widget.TextView.OnEditorActionListener;
Michael Kolbfe251992010-07-08 15:41:55 -070039
Michael Kolbcfa3af52010-12-14 10:36:11 -080040import java.util.List;
41
Michael Kolbfe251992010-07-08 15:41:55 -070042/**
43 * url/search input view
44 * handling suggestions
45 */
Michael Kolba2b2ba82010-08-04 17:54:03 -070046public class UrlInputView extends AutoCompleteTextView
Michael Kolbba99c5d2010-11-29 14:57:41 -080047 implements OnFocusChangeListener, OnEditorActionListener,
John Reck87369ea2011-01-23 15:20:38 -080048 CompletionListener, OnItemClickListener {
Michael Kolbfe251992010-07-08 15:41:55 -070049
Michael Kolb257cc2c2010-12-09 09:45:52 -080050
51 static final String TYPED = "browser-type";
52 static final String SUGGESTED = "browser-suggest";
Michael Kolbbd2dd642011-01-13 13:01:30 -080053 static final String VOICE = "voice-search";
Michael Kolb257cc2c2010-12-09 09:45:52 -080054
Michael Kolbfe251992010-07-08 15:41:55 -070055 private UrlInputListener mListener;
56 private InputMethodManager mInputManager;
57 private SuggestionsAdapter mAdapter;
Michael Kolbc7485ae2010-09-03 10:10:58 -070058 private OnFocusChangeListener mWrappedFocusListener;
Michael Kolb21ce4d22010-09-15 14:55:05 -070059 private View mContainer;
60 private boolean mLandscape;
Michael Kolbcfa3af52010-12-14 10:36:11 -080061 private boolean mInVoiceMode;
John Reckb77bdc42011-01-24 13:24:48 -080062 private boolean mIncognitoMode;
Michael Kolb91902d52011-01-26 17:43:48 -080063 private int mVOffset;
Michael Kolbfe251992010-07-08 15:41:55 -070064
65 public UrlInputView(Context context, AttributeSet attrs, int defStyle) {
66 super(context, attrs, defStyle);
67 init(context);
68 }
69
70 public UrlInputView(Context context, AttributeSet attrs) {
71 super(context, attrs);
72 init(context);
73 }
74
75 public UrlInputView(Context context) {
76 super(context);
77 init(context);
78 }
79
80 private void init(Context ctx) {
Michael Kolbfe251992010-07-08 15:41:55 -070081 mInputManager = (InputMethodManager) ctx.getSystemService(Context.INPUT_METHOD_SERVICE);
Michael Kolbed217742010-08-10 17:52:34 -070082 setOnEditorActionListener(this);
Michael Kolbc7485ae2010-09-03 10:10:58 -070083 super.setOnFocusChangeListener(this);
Michael Kolb21ce4d22010-09-15 14:55:05 -070084 mAdapter = new SuggestionsAdapter(ctx, this);
Michael Kolbfe251992010-07-08 15:41:55 -070085 setAdapter(mAdapter);
Michael Kolbba99c5d2010-11-29 14:57:41 -080086 setSelectAllOnFocus(true);
Michael Kolb21ce4d22010-09-15 14:55:05 -070087 onConfigurationChanged(ctx.getResources().getConfiguration());
John Reck35defff2010-11-11 14:06:45 -080088 setThreshold(1);
John Reck87369ea2011-01-23 15:20:38 -080089 setOnItemClickListener(this);
Michael Kolb91902d52011-01-26 17:43:48 -080090 mVOffset = 0;
Michael Kolb21ce4d22010-09-15 14:55:05 -070091 }
92
Michael Kolbba99c5d2010-11-29 14:57:41 -080093 void setController(UiController controller) {
94 UrlSelectionActionMode urlSelectionMode
95 = new UrlSelectionActionMode(controller);
96 setCustomSelectionActionModeCallback(urlSelectionMode);
97 }
98
Michael Kolb91902d52011-01-26 17:43:48 -080099 void setUseQuickControls(boolean useQuickControls) {
100 mVOffset = (useQuickControls
101 ? (int) getResources().getDimension(R.dimen.dropdown_offset)
102 : 0);
103 mAdapter.setReverseResults(useQuickControls);
104 }
105
Michael Kolb21ce4d22010-09-15 14:55:05 -0700106 void setContainer(View container) {
107 mContainer = container;
108 }
109
Michael Kolbcfa3af52010-12-14 10:36:11 -0800110 void setVoiceResults(List<String> voiceResults) {
111 mAdapter.setVoiceResults(voiceResults);
112 mInVoiceMode = (voiceResults != null);
113 }
114
Michael Kolb21ce4d22010-09-15 14:55:05 -0700115 @Override
116 protected void onConfigurationChanged(Configuration config) {
John Reck35defff2010-11-11 14:06:45 -0800117 super.onConfigurationChanged(config);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700118 mLandscape = (config.orientation &
Michael Kolb31d469b2010-12-09 20:49:54 -0800119 Configuration.ORIENTATION_LANDSCAPE) != 0;
John Reck35defff2010-11-11 14:06:45 -0800120 mAdapter.setLandscapeMode(mLandscape);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700121 if (isPopupShowing() && (getVisibility() == View.VISIBLE)) {
John Reck35defff2010-11-11 14:06:45 -0800122 setupDropDown();
John Reckad373302010-12-17 15:28:13 -0800123 performFiltering(getText(), 0);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700124 }
125 }
126
127 @Override
128 public void showDropDown() {
John Reck35defff2010-11-11 14:06:45 -0800129 setupDropDown();
130 super.showDropDown();
131 }
132
133 @Override
134 public void dismissDropDown() {
135 super.dismissDropDown();
136 mAdapter.clearCache();
137 }
138
139 private void setupDropDown() {
Michael Kolb21ce4d22010-09-15 14:55:05 -0700140 int width = mContainer.getWidth();
Michael Kolbc5998c22010-10-10 16:04:35 -0700141 if (width != getDropDownWidth()) {
142 setDropDownWidth(width);
143 }
144 if (getLeft() != -getDropDownHorizontalOffset()) {
145 setDropDownHorizontalOffset(-getLeft());
146 }
Michael Kolb91902d52011-01-26 17:43:48 -0800147 setDropDownVerticalOffset(mVOffset);
Michael Kolb513286f2010-09-09 12:55:12 -0700148 }
149
150 @Override
Michael Kolbc7485ae2010-09-03 10:10:58 -0700151 public void setOnFocusChangeListener(OnFocusChangeListener focusListener) {
152 mWrappedFocusListener = focusListener;
Michael Kolbfe251992010-07-08 15:41:55 -0700153 }
154
Michael Kolba2b2ba82010-08-04 17:54:03 -0700155 @Override
Michael Kolbed217742010-08-10 17:52:34 -0700156 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
Michael Kolb257cc2c2010-12-09 09:45:52 -0800157 finishInput(getText().toString(), null, TYPED);
Michael Kolbed217742010-08-10 17:52:34 -0700158 return true;
159 }
Michael Kolb21ce4d22010-09-15 14:55:05 -0700160
Michael Kolbc7485ae2010-09-03 10:10:58 -0700161 @Override
Michael Kolba2b2ba82010-08-04 17:54:03 -0700162 public void onFocusChange(View v, boolean hasFocus) {
Michael Kolba2b2ba82010-08-04 17:54:03 -0700163 if (hasFocus) {
164 forceIme();
Michael Kolbcfa3af52010-12-14 10:36:11 -0800165 if (mInVoiceMode) {
166 performFiltering(getText().toString(), 0);
167 showDropDown();
168 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700169 }
Michael Kolbc7485ae2010-09-03 10:10:58 -0700170 if (mWrappedFocusListener != null) {
171 mWrappedFocusListener.onFocusChange(v, hasFocus);
172 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700173 }
174
Michael Kolb47171d82011-01-28 10:34:15 -0800175 void stopEditing() {
176 finishInput(null, null, null);
177 }
178
Michael Kolbfe251992010-07-08 15:41:55 -0700179 public void setUrlInputListener(UrlInputListener listener) {
180 mListener = listener;
181 }
182
183 public void forceIme() {
184 mInputManager.showSoftInput(this, 0);
185 }
186
Michael Kolb257cc2c2010-12-09 09:45:52 -0800187 private void finishInput(String url, String extra, String source) {
Michael Kolbfe251992010-07-08 15:41:55 -0700188 this.dismissDropDown();
189 mInputManager.hideSoftInputFromWindow(getWindowToken(), 0);
John Reckdcda1d52010-11-29 10:05:54 -0800190 if (TextUtils.isEmpty(url)) {
Michael Kolbfe251992010-07-08 15:41:55 -0700191 mListener.onDismiss();
192 } else {
John Reckb77bdc42011-01-24 13:24:48 -0800193 if (mIncognitoMode && isSearch(url)) {
194 // To prevent logging, intercept this request
195 // TODO: This is a quick hack, refactor this
196 SearchEngine searchEngine = BrowserSettings.getInstance()
197 .getSearchEngine();
198 if (searchEngine == null) return;
199 SearchEngineInfo engineInfo = SearchEngines
200 .getSearchEngineInfo(mContext, searchEngine.getName());
201 if (engineInfo == null) return;
202 url = engineInfo.getSearchUriForQuery(url);
203 // mLister.onAction can take it from here without logging
204 }
Michael Kolb257cc2c2010-12-09 09:45:52 -0800205 mListener.onAction(url, extra, source);
Michael Kolbfe251992010-07-08 15:41:55 -0700206 }
Michael Kolbfe251992010-07-08 15:41:55 -0700207 }
208
John Reckb77bdc42011-01-24 13:24:48 -0800209 boolean isSearch(String inUrl) {
210 String url = UrlUtils.fixUrl(inUrl).trim();
211 if (TextUtils.isEmpty(url)) return false;
212
213 if (Patterns.WEB_URL.matcher(url).matches()
214 || UrlUtils.ACCEPTED_URI_SCHEMA.matcher(url).matches()) {
215 return false;
216 }
217 return true;
218 }
219
Michael Kolb21ce4d22010-09-15 14:55:05 -0700220 // Completion Listener
221
222 @Override
223 public void onSearch(String search) {
224 mListener.onEdit(search);
225 }
226
227 @Override
Michael Kolbbd2dd642011-01-13 13:01:30 -0800228 public void onSelect(String url, int type, String extra) {
229 finishInput(url, extra, (type == SuggestionsAdapter.TYPE_VOICE_SEARCH)
230 ? VOICE : SUGGESTED);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700231 }
232
Michael Kolbfe251992010-07-08 15:41:55 -0700233 @Override
234 public boolean onKeyPreIme(int keyCode, KeyEvent evt) {
235 if (keyCode == KeyEvent.KEYCODE_BACK) {
236 // catch back key in order to do slightly more cleanup than usual
Michael Kolb257cc2c2010-12-09 09:45:52 -0800237 finishInput(null, null, null);
Michael Kolbfe251992010-07-08 15:41:55 -0700238 return true;
239 }
240 return super.onKeyPreIme(keyCode, evt);
241 }
242
John Reck87369ea2011-01-23 15:20:38 -0800243 @Override
244 public void onItemClick(
245 AdapterView<?> parent, View view, int position, long id) {
246 SuggestItem item = mAdapter.getItem(position);
247 onSelect((TextUtils.isEmpty(item.url) ? item.title : item.url),
248 item.type, item.extra);
249 }
250
Michael Kolbfe251992010-07-08 15:41:55 -0700251 interface UrlInputListener {
Michael Kolb21ce4d22010-09-15 14:55:05 -0700252
Michael Kolbfe251992010-07-08 15:41:55 -0700253 public void onDismiss();
Michael Kolb21ce4d22010-09-15 14:55:05 -0700254
Michael Kolb257cc2c2010-12-09 09:45:52 -0800255 public void onAction(String text, String extra, String source);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700256
Michael Kolb513286f2010-09-09 12:55:12 -0700257 public void onEdit(String text);
Michael Kolbfe251992010-07-08 15:41:55 -0700258
259 }
260
John Reck117f07d2011-01-24 09:39:03 -0800261 public void setIncognitoMode(boolean incognito) {
John Reckb77bdc42011-01-24 13:24:48 -0800262 mIncognitoMode = incognito;
263 mAdapter.setIncognitoMode(mIncognitoMode);
John Reck117f07d2011-01-24 09:39:03 -0800264 }
265
Michael Kolbfe251992010-07-08 15:41:55 -0700266}