blob: 02c9f4cd0e91a2e1b6c0a6d58e1c41cb3c7feda8 [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 Kolbfe251992010-07-08 15:41:55 -070063
64 public UrlInputView(Context context, AttributeSet attrs, int defStyle) {
65 super(context, attrs, defStyle);
66 init(context);
67 }
68
69 public UrlInputView(Context context, AttributeSet attrs) {
70 super(context, attrs);
71 init(context);
72 }
73
74 public UrlInputView(Context context) {
75 super(context);
76 init(context);
77 }
78
79 private void init(Context ctx) {
Michael Kolbfe251992010-07-08 15:41:55 -070080 mInputManager = (InputMethodManager) ctx.getSystemService(Context.INPUT_METHOD_SERVICE);
Michael Kolbed217742010-08-10 17:52:34 -070081 setOnEditorActionListener(this);
Michael Kolbc7485ae2010-09-03 10:10:58 -070082 super.setOnFocusChangeListener(this);
Michael Kolb21ce4d22010-09-15 14:55:05 -070083 mAdapter = new SuggestionsAdapter(ctx, this);
Michael Kolbfe251992010-07-08 15:41:55 -070084 setAdapter(mAdapter);
Michael Kolbba99c5d2010-11-29 14:57:41 -080085 setSelectAllOnFocus(true);
Michael Kolb21ce4d22010-09-15 14:55:05 -070086 onConfigurationChanged(ctx.getResources().getConfiguration());
John Reck35defff2010-11-11 14:06:45 -080087 setThreshold(1);
John Reck87369ea2011-01-23 15:20:38 -080088 setOnItemClickListener(this);
Michael Kolb21ce4d22010-09-15 14:55:05 -070089 }
90
Michael Kolbba99c5d2010-11-29 14:57:41 -080091 void setController(UiController controller) {
92 UrlSelectionActionMode urlSelectionMode
93 = new UrlSelectionActionMode(controller);
94 setCustomSelectionActionModeCallback(urlSelectionMode);
95 }
96
Michael Kolb21ce4d22010-09-15 14:55:05 -070097 void setContainer(View container) {
98 mContainer = container;
99 }
100
Michael Kolbcfa3af52010-12-14 10:36:11 -0800101 void setVoiceResults(List<String> voiceResults) {
102 mAdapter.setVoiceResults(voiceResults);
103 mInVoiceMode = (voiceResults != null);
104 }
105
Michael Kolb21ce4d22010-09-15 14:55:05 -0700106 @Override
107 protected void onConfigurationChanged(Configuration config) {
John Reck35defff2010-11-11 14:06:45 -0800108 super.onConfigurationChanged(config);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700109 mLandscape = (config.orientation &
Michael Kolb31d469b2010-12-09 20:49:54 -0800110 Configuration.ORIENTATION_LANDSCAPE) != 0;
John Reck35defff2010-11-11 14:06:45 -0800111 mAdapter.setLandscapeMode(mLandscape);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700112 if (isPopupShowing() && (getVisibility() == View.VISIBLE)) {
John Reck35defff2010-11-11 14:06:45 -0800113 setupDropDown();
John Reckad373302010-12-17 15:28:13 -0800114 performFiltering(getText(), 0);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700115 }
116 }
117
118 @Override
119 public void showDropDown() {
John Reck35defff2010-11-11 14:06:45 -0800120 setupDropDown();
121 super.showDropDown();
122 }
123
124 @Override
125 public void dismissDropDown() {
126 super.dismissDropDown();
127 mAdapter.clearCache();
128 }
129
130 private void setupDropDown() {
Michael Kolb21ce4d22010-09-15 14:55:05 -0700131 int width = mContainer.getWidth();
Michael Kolbc5998c22010-10-10 16:04:35 -0700132 if (width != getDropDownWidth()) {
133 setDropDownWidth(width);
134 }
135 if (getLeft() != -getDropDownHorizontalOffset()) {
136 setDropDownHorizontalOffset(-getLeft());
137 }
Michael Kolb31d469b2010-12-09 20:49:54 -0800138 setDropDownVerticalOffset(8);
Michael Kolb513286f2010-09-09 12:55:12 -0700139 }
140
141 @Override
Michael Kolbc7485ae2010-09-03 10:10:58 -0700142 public void setOnFocusChangeListener(OnFocusChangeListener focusListener) {
143 mWrappedFocusListener = focusListener;
Michael Kolbfe251992010-07-08 15:41:55 -0700144 }
145
Michael Kolba2b2ba82010-08-04 17:54:03 -0700146 @Override
Michael Kolbed217742010-08-10 17:52:34 -0700147 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
Michael Kolb257cc2c2010-12-09 09:45:52 -0800148 finishInput(getText().toString(), null, TYPED);
Michael Kolbed217742010-08-10 17:52:34 -0700149 return true;
150 }
Michael Kolb21ce4d22010-09-15 14:55:05 -0700151
Michael Kolbc7485ae2010-09-03 10:10:58 -0700152 @Override
Michael Kolba2b2ba82010-08-04 17:54:03 -0700153 public void onFocusChange(View v, boolean hasFocus) {
Michael Kolba2b2ba82010-08-04 17:54:03 -0700154 if (hasFocus) {
155 forceIme();
Michael Kolbcfa3af52010-12-14 10:36:11 -0800156 if (mInVoiceMode) {
157 performFiltering(getText().toString(), 0);
158 showDropDown();
159 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700160 } else {
Michael Kolb257cc2c2010-12-09 09:45:52 -0800161 finishInput(null, null, null);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700162 }
Michael Kolbc7485ae2010-09-03 10:10:58 -0700163 if (mWrappedFocusListener != null) {
164 mWrappedFocusListener.onFocusChange(v, hasFocus);
165 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700166 }
167
Michael Kolbfe251992010-07-08 15:41:55 -0700168 public void setUrlInputListener(UrlInputListener listener) {
169 mListener = listener;
170 }
171
172 public void forceIme() {
173 mInputManager.showSoftInput(this, 0);
174 }
175
Michael Kolb257cc2c2010-12-09 09:45:52 -0800176 private void finishInput(String url, String extra, String source) {
Michael Kolbfe251992010-07-08 15:41:55 -0700177 this.dismissDropDown();
178 mInputManager.hideSoftInputFromWindow(getWindowToken(), 0);
John Reckdcda1d52010-11-29 10:05:54 -0800179 if (TextUtils.isEmpty(url)) {
Michael Kolbfe251992010-07-08 15:41:55 -0700180 mListener.onDismiss();
181 } else {
John Reckb77bdc42011-01-24 13:24:48 -0800182 if (mIncognitoMode && isSearch(url)) {
183 // To prevent logging, intercept this request
184 // TODO: This is a quick hack, refactor this
185 SearchEngine searchEngine = BrowserSettings.getInstance()
186 .getSearchEngine();
187 if (searchEngine == null) return;
188 SearchEngineInfo engineInfo = SearchEngines
189 .getSearchEngineInfo(mContext, searchEngine.getName());
190 if (engineInfo == null) return;
191 url = engineInfo.getSearchUriForQuery(url);
192 // mLister.onAction can take it from here without logging
193 }
Michael Kolb257cc2c2010-12-09 09:45:52 -0800194 mListener.onAction(url, extra, source);
Michael Kolbfe251992010-07-08 15:41:55 -0700195 }
Michael Kolbfe251992010-07-08 15:41:55 -0700196 }
197
John Reckb77bdc42011-01-24 13:24:48 -0800198 boolean isSearch(String inUrl) {
199 String url = UrlUtils.fixUrl(inUrl).trim();
200 if (TextUtils.isEmpty(url)) return false;
201
202 if (Patterns.WEB_URL.matcher(url).matches()
203 || UrlUtils.ACCEPTED_URI_SCHEMA.matcher(url).matches()) {
204 return false;
205 }
206 return true;
207 }
208
Michael Kolb21ce4d22010-09-15 14:55:05 -0700209 // Completion Listener
210
211 @Override
212 public void onSearch(String search) {
213 mListener.onEdit(search);
214 }
215
216 @Override
Michael Kolbbd2dd642011-01-13 13:01:30 -0800217 public void onSelect(String url, int type, String extra) {
218 finishInput(url, extra, (type == SuggestionsAdapter.TYPE_VOICE_SEARCH)
219 ? VOICE : SUGGESTED);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700220 }
221
Michael Kolbfe251992010-07-08 15:41:55 -0700222 @Override
223 public boolean onKeyPreIme(int keyCode, KeyEvent evt) {
224 if (keyCode == KeyEvent.KEYCODE_BACK) {
225 // catch back key in order to do slightly more cleanup than usual
Michael Kolb257cc2c2010-12-09 09:45:52 -0800226 finishInput(null, null, null);
Michael Kolbfe251992010-07-08 15:41:55 -0700227 return true;
228 }
229 return super.onKeyPreIme(keyCode, evt);
230 }
231
John Reck87369ea2011-01-23 15:20:38 -0800232 @Override
233 public void onItemClick(
234 AdapterView<?> parent, View view, int position, long id) {
235 SuggestItem item = mAdapter.getItem(position);
236 onSelect((TextUtils.isEmpty(item.url) ? item.title : item.url),
237 item.type, item.extra);
238 }
239
Michael Kolbfe251992010-07-08 15:41:55 -0700240 interface UrlInputListener {
Michael Kolb21ce4d22010-09-15 14:55:05 -0700241
Michael Kolbfe251992010-07-08 15:41:55 -0700242 public void onDismiss();
Michael Kolb21ce4d22010-09-15 14:55:05 -0700243
Michael Kolb257cc2c2010-12-09 09:45:52 -0800244 public void onAction(String text, String extra, String source);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700245
Michael Kolb513286f2010-09-09 12:55:12 -0700246 public void onEdit(String text);
Michael Kolbfe251992010-07-08 15:41:55 -0700247
248 }
249
John Reck1605bef2011-01-10 18:11:18 -0800250 public void setReverseResults(boolean reverse) {
251 mAdapter.setReverseResults(reverse);
252 }
253
John Reck117f07d2011-01-24 09:39:03 -0800254 public void setIncognitoMode(boolean incognito) {
John Reckb77bdc42011-01-24 13:24:48 -0800255 mIncognitoMode = incognito;
256 mAdapter.setIncognitoMode(mIncognitoMode);
John Reck117f07d2011-01-24 09:39:03 -0800257 }
258
Michael Kolbfe251992010-07-08 15:41:55 -0700259}