blob: 2ec21111b84c00ee67e8fb90a4d4d0a3ba0723bc [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 Kolbfe251992010-07-08 15:41:55 -070032import android.view.inputmethod.InputMethodManager;
John Reck87369ea2011-01-23 15:20:38 -080033import android.widget.AdapterView;
34import android.widget.AdapterView.OnItemClickListener;
Michael Kolbfe251992010-07-08 15:41:55 -070035import android.widget.AutoCompleteTextView;
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 */
Michael Kolba2b2ba82010-08-04 17:54:03 -070045public class UrlInputView extends AutoCompleteTextView
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 Kolb91902d52011-01-26 17:43:48 -080060 private int mVOffset;
Michael Kolbdc2ee1b2011-02-14 14:34:40 -080061 private boolean mNeedsUpdate;
Michael Kolbfe251992010-07-08 15:41:55 -070062
63 public UrlInputView(Context context, AttributeSet attrs, int defStyle) {
64 super(context, attrs, defStyle);
65 init(context);
66 }
67
68 public UrlInputView(Context context, AttributeSet attrs) {
69 super(context, attrs);
70 init(context);
71 }
72
73 public UrlInputView(Context context) {
74 super(context);
75 init(context);
76 }
77
78 private void init(Context ctx) {
Michael Kolbfe251992010-07-08 15:41:55 -070079 mInputManager = (InputMethodManager) ctx.getSystemService(Context.INPUT_METHOD_SERVICE);
Michael Kolbed217742010-08-10 17:52:34 -070080 setOnEditorActionListener(this);
Michael Kolb21ce4d22010-09-15 14:55:05 -070081 mAdapter = new SuggestionsAdapter(ctx, this);
Michael Kolbfe251992010-07-08 15:41:55 -070082 setAdapter(mAdapter);
Michael Kolbba99c5d2010-11-29 14:57:41 -080083 setSelectAllOnFocus(true);
Michael Kolb21ce4d22010-09-15 14:55:05 -070084 onConfigurationChanged(ctx.getResources().getConfiguration());
John Reck35defff2010-11-11 14:06:45 -080085 setThreshold(1);
John Reck87369ea2011-01-23 15:20:38 -080086 setOnItemClickListener(this);
Michael Kolb91902d52011-01-26 17:43:48 -080087 mVOffset = 0;
Michael Kolbdc2ee1b2011-02-14 14:34:40 -080088 mNeedsUpdate = false;
89 }
90
91 /**
92 * check if focus change requires a title bar update
93 */
94 boolean needsUpdate() {
95 return mNeedsUpdate;
96 }
97
98 /**
99 * clear the focus change needs title bar update flag
100 */
101 void clearNeedsUpdate() {
102 mNeedsUpdate = false;
Michael Kolb21ce4d22010-09-15 14:55:05 -0700103 }
104
Michael Kolbba99c5d2010-11-29 14:57:41 -0800105 void setController(UiController controller) {
106 UrlSelectionActionMode urlSelectionMode
107 = new UrlSelectionActionMode(controller);
108 setCustomSelectionActionModeCallback(urlSelectionMode);
109 }
110
Michael Kolb91902d52011-01-26 17:43:48 -0800111 void setUseQuickControls(boolean useQuickControls) {
112 mVOffset = (useQuickControls
113 ? (int) getResources().getDimension(R.dimen.dropdown_offset)
114 : 0);
115 mAdapter.setReverseResults(useQuickControls);
116 }
117
Michael Kolb21ce4d22010-09-15 14:55:05 -0700118 void setContainer(View container) {
119 mContainer = container;
120 }
121
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800122 public void setUrlInputListener(UrlInputListener listener) {
123 mListener = listener;
124 }
125
Michael Kolbcfa3af52010-12-14 10:36:11 -0800126 void setVoiceResults(List<String> voiceResults) {
127 mAdapter.setVoiceResults(voiceResults);
Michael Kolbcfa3af52010-12-14 10:36:11 -0800128 }
129
Michael Kolb21ce4d22010-09-15 14:55:05 -0700130 @Override
131 protected void onConfigurationChanged(Configuration config) {
John Reck35defff2010-11-11 14:06:45 -0800132 super.onConfigurationChanged(config);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700133 mLandscape = (config.orientation &
Michael Kolb31d469b2010-12-09 20:49:54 -0800134 Configuration.ORIENTATION_LANDSCAPE) != 0;
John Reck35defff2010-11-11 14:06:45 -0800135 mAdapter.setLandscapeMode(mLandscape);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700136 if (isPopupShowing() && (getVisibility() == View.VISIBLE)) {
John Reck35defff2010-11-11 14:06:45 -0800137 setupDropDown();
John Reckad373302010-12-17 15:28:13 -0800138 performFiltering(getText(), 0);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700139 }
140 }
141
142 @Override
143 public void showDropDown() {
John Reck35defff2010-11-11 14:06:45 -0800144 setupDropDown();
145 super.showDropDown();
146 }
147
148 @Override
149 public void dismissDropDown() {
150 super.dismissDropDown();
151 mAdapter.clearCache();
152 }
153
154 private void setupDropDown() {
John Reck92026732011-02-15 10:12:30 -0800155 int width = mContainer != null ? mContainer.getWidth() : getWidth();
Michael Kolbc5998c22010-10-10 16:04:35 -0700156 if (width != getDropDownWidth()) {
157 setDropDownWidth(width);
158 }
159 if (getLeft() != -getDropDownHorizontalOffset()) {
160 setDropDownHorizontalOffset(-getLeft());
161 }
Michael Kolb91902d52011-01-26 17:43:48 -0800162 setDropDownVerticalOffset(mVOffset);
Michael Kolb513286f2010-09-09 12:55:12 -0700163 }
164
165 @Override
Michael Kolbed217742010-08-10 17:52:34 -0700166 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
Michael Kolb257cc2c2010-12-09 09:45:52 -0800167 finishInput(getText().toString(), null, TYPED);
Michael Kolbed217742010-08-10 17:52:34 -0700168 return true;
169 }
Michael Kolb21ce4d22010-09-15 14:55:05 -0700170
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800171 void forceFilter() {
172 performFiltering(getText().toString(), 0);
173 showDropDown();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700174 }
175
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800176 void forceIme() {
John Reck92026732011-02-15 10:12:30 -0800177 mInputManager.focusIn(this);
Michael Kolbfe251992010-07-08 15:41:55 -0700178 mInputManager.showSoftInput(this, 0);
179 }
180
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800181 void hideIME() {
182 mInputManager.hideSoftInputFromWindow(getWindowToken(), 0);
183 }
184
Michael Kolb257cc2c2010-12-09 09:45:52 -0800185 private void finishInput(String url, String extra, String source) {
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800186 mNeedsUpdate = true;
187 dismissDropDown();
Michael Kolbfe251992010-07-08 15:41:55 -0700188 mInputManager.hideSoftInputFromWindow(getWindowToken(), 0);
John Reckdcda1d52010-11-29 10:05:54 -0800189 if (TextUtils.isEmpty(url)) {
Michael Kolbfe251992010-07-08 15:41:55 -0700190 mListener.onDismiss();
191 } else {
John Reckb77bdc42011-01-24 13:24:48 -0800192 if (mIncognitoMode && isSearch(url)) {
193 // To prevent logging, intercept this request
194 // TODO: This is a quick hack, refactor this
195 SearchEngine searchEngine = BrowserSettings.getInstance()
196 .getSearchEngine();
197 if (searchEngine == null) return;
198 SearchEngineInfo engineInfo = SearchEngines
199 .getSearchEngineInfo(mContext, searchEngine.getName());
200 if (engineInfo == null) return;
201 url = engineInfo.getSearchUriForQuery(url);
202 // mLister.onAction can take it from here without logging
203 }
Michael Kolb257cc2c2010-12-09 09:45:52 -0800204 mListener.onAction(url, extra, source);
Michael Kolbfe251992010-07-08 15:41:55 -0700205 }
Michael Kolbfe251992010-07-08 15:41:55 -0700206 }
207
John Reckb77bdc42011-01-24 13:24:48 -0800208 boolean isSearch(String inUrl) {
209 String url = UrlUtils.fixUrl(inUrl).trim();
210 if (TextUtils.isEmpty(url)) return false;
211
212 if (Patterns.WEB_URL.matcher(url).matches()
213 || UrlUtils.ACCEPTED_URI_SCHEMA.matcher(url).matches()) {
214 return false;
215 }
216 return true;
217 }
218
Michael Kolb21ce4d22010-09-15 14:55:05 -0700219 // Completion Listener
220
221 @Override
222 public void onSearch(String search) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800223 mListener.onCopySuggestion(search);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700224 }
225
226 @Override
Michael Kolbbd2dd642011-01-13 13:01:30 -0800227 public void onSelect(String url, int type, String extra) {
228 finishInput(url, extra, (type == SuggestionsAdapter.TYPE_VOICE_SEARCH)
229 ? VOICE : SUGGESTED);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700230 }
231
Michael Kolbfe251992010-07-08 15:41:55 -0700232 @Override
John Reck87369ea2011-01-23 15:20:38 -0800233 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 Kolb7cdc4902011-02-03 17:54:40 -0800246 public void onCopySuggestion(String text);
Michael Kolbfe251992010-07-08 15:41:55 -0700247
248 }
249
John Reck117f07d2011-01-24 09:39:03 -0800250 public void setIncognitoMode(boolean incognito) {
John Reckb77bdc42011-01-24 13:24:48 -0800251 mIncognitoMode = incognito;
252 mAdapter.setIncognitoMode(mIncognitoMode);
John Reck117f07d2011-01-24 09:39:03 -0800253 }
254
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800255 @Override
256 public boolean onKeyDown(int keyCode, KeyEvent evt) {
257 if (keyCode == KeyEvent.KEYCODE_ESCAPE && !isInTouchMode()) {
258 finishInput(null, null, null);
259 return true;
260 }
261 return super.onKeyDown(keyCode, evt);
262 }
263
Michael Kolbfe251992010-07-08 15:41:55 -0700264}