blob: 13590005b808c02567668e6efc17527022c89258 [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
Bijan Amirzada41242f22014-03-21 12:12:18 -070017package com.android.browser;
Michael Kolbfe251992010-07-08 15:41:55 -070018
Michael Kolbfe251992010-07-08 15:41:55 -070019import android.content.Context;
Michael Kolb21ce4d22010-09-15 14:55:05 -070020import android.content.res.Configuration;
Narayan Kamath5119edd2011-02-23 15:49:17 +000021import android.graphics.Rect;
John Reck8d021aa2011-09-06 15:30:11 -070022import android.graphics.drawable.Drawable;
Narayan Kamathf3174a52011-11-17 14:43:32 +000023import android.text.Editable;
John Reckdcda1d52010-11-29 10:05:54 -080024import android.text.TextUtils;
Narayan Kamathf3174a52011-11-17 14:43:32 +000025import android.text.TextWatcher;
Michael Kolbfe251992010-07-08 15:41:55 -070026import android.util.AttributeSet;
John Reckb77bdc42011-01-24 13:24:48 -080027import android.util.Patterns;
Michael Kolbfe251992010-07-08 15:41:55 -070028import android.view.KeyEvent;
Michael Kolb305b1c52011-06-21 16:16:22 -070029import android.view.MotionEvent;
Michael Kolbfe251992010-07-08 15:41:55 -070030import android.view.View;
Michael Kolbfe251992010-07-08 15:41:55 -070031import android.view.inputmethod.InputMethodManager;
John Reck87369ea2011-01-23 15:20:38 -080032import android.widget.AdapterView;
33import android.widget.AdapterView.OnItemClickListener;
Narayan Kamathf3174a52011-11-17 14:43:32 +000034import android.widget.AutoCompleteTextView;
Michael Kolbfe251992010-07-08 15:41:55 -070035import android.widget.TextView;
Michael Kolbed217742010-08-10 17:52:34 -070036import android.widget.TextView.OnEditorActionListener;
Michael Kolbfe251992010-07-08 15:41:55 -070037
Bijan Amirzada41242f22014-03-21 12:12:18 -070038import com.android.browser.SuggestionsAdapter.CompletionListener;
39import com.android.browser.SuggestionsAdapter.SuggestItem;
40import com.android.browser.reflect.ReflectHelper;
41import com.android.browser.search.SearchEngine;
42import com.android.browser.search.SearchEngineInfo;
43import com.android.browser.search.SearchEngines;
Michael Kolb305b1c52011-06-21 16:16:22 -070044
Michael Kolbcfa3af52010-12-14 10:36:11 -080045
Michael Kolbfe251992010-07-08 15:41:55 -070046/**
47 * url/search input view
48 * handling suggestions
49 */
Narayan Kamathf3174a52011-11-17 14:43:32 +000050public class UrlInputView extends AutoCompleteTextView
Michael Kolbdc2ee1b2011-02-14 14:34:40 -080051 implements OnEditorActionListener,
Narayan Kamathf3174a52011-11-17 14:43:32 +000052 CompletionListener, OnItemClickListener, TextWatcher {
Michael Kolb257cc2c2010-12-09 09:45:52 -080053
54 static final String TYPED = "browser-type";
55 static final String SUGGESTED = "browser-suggest";
56
Michael Kolb94ec5272011-08-31 16:23:57 -070057 static final int POST_DELAY = 100;
58
Michael Kolb305b1c52011-06-21 16:16:22 -070059 static interface StateListener {
60 static final int STATE_NORMAL = 0;
61 static final int STATE_HIGHLIGHTED = 1;
62 static final int STATE_EDITED = 2;
63
64 public void onStateChanged(int state);
65 }
66
Michael Kolbfe251992010-07-08 15:41:55 -070067 private UrlInputListener mListener;
68 private InputMethodManager mInputManager;
69 private SuggestionsAdapter mAdapter;
Michael Kolb21ce4d22010-09-15 14:55:05 -070070 private View mContainer;
71 private boolean mLandscape;
John Reckb77bdc42011-01-24 13:24:48 -080072 private boolean mIncognitoMode;
Michael Kolbdc2ee1b2011-02-14 14:34:40 -080073 private boolean mNeedsUpdate;
Michael Kolbfe251992010-07-08 15:41:55 -070074
Michael Kolb305b1c52011-06-21 16:16:22 -070075 private int mState;
76 private StateListener mStateListener;
John Reck8d021aa2011-09-06 15:30:11 -070077 private Rect mPopupPadding;
Michael Kolb305b1c52011-06-21 16:16:22 -070078
Michael Kolbfe251992010-07-08 15:41:55 -070079 public UrlInputView(Context context, AttributeSet attrs, int defStyle) {
80 super(context, attrs, defStyle);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080081 // SWE_TODO : HARDCODED a random background - clean up
82 /*
John Reck8d021aa2011-09-06 15:30:11 -070083 TypedArray a = context.obtainStyledAttributes(
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080084 attrs, R.styleable.PopupWindow,
John Reck8d021aa2011-09-06 15:30:11 -070085 R.attr.autoCompleteTextViewStyle, 0);
86
87 Drawable popupbg = a.getDrawable(R.styleable.PopupWindow_popupBackground);
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080088 a.recycle(); */
89 Drawable popupbg = context.getResources().getDrawable(android.R.drawable.editbox_background);
John Reck8d021aa2011-09-06 15:30:11 -070090 mPopupPadding = new Rect();
91 popupbg.getPadding(mPopupPadding);
Michael Kolbfe251992010-07-08 15:41:55 -070092 init(context);
93 }
94
95 public UrlInputView(Context context, AttributeSet attrs) {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -080096 // SWE_TODO : Needs Fix
97 //this(context, attrs, R.attr.autoCompleteTextViewStyle);
98 this(context, attrs, 0);
Michael Kolbfe251992010-07-08 15:41:55 -070099 }
100
101 public UrlInputView(Context context) {
John Reck8d021aa2011-09-06 15:30:11 -0700102 this(context, null);
Michael Kolbfe251992010-07-08 15:41:55 -0700103 }
104
105 private void init(Context ctx) {
Michael Kolbfe251992010-07-08 15:41:55 -0700106 mInputManager = (InputMethodManager) ctx.getSystemService(Context.INPUT_METHOD_SERVICE);
Michael Kolbed217742010-08-10 17:52:34 -0700107 setOnEditorActionListener(this);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700108 mAdapter = new SuggestionsAdapter(ctx, this);
Michael Kolbfe251992010-07-08 15:41:55 -0700109 setAdapter(mAdapter);
Michael Kolbba99c5d2010-11-29 14:57:41 -0800110 setSelectAllOnFocus(true);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700111 onConfigurationChanged(ctx.getResources().getConfiguration());
John Reck35defff2010-11-11 14:06:45 -0800112 setThreshold(1);
John Reck87369ea2011-01-23 15:20:38 -0800113 setOnItemClickListener(this);
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800114 mNeedsUpdate = false;
Narayan Kamathf3174a52011-11-17 14:43:32 +0000115 addTextChangedListener(this);
Narayan Kamath5119edd2011-02-23 15:49:17 +0000116
Michael Kolb305b1c52011-06-21 16:16:22 -0700117 mState = StateListener.STATE_NORMAL;
118 }
119
120 protected void onFocusChanged(boolean focused, int direction, Rect prevRect) {
121 super.onFocusChanged(focused, direction, prevRect);
Michael Kolb94ec5272011-08-31 16:23:57 -0700122 int state = -1;
Michael Kolb305b1c52011-06-21 16:16:22 -0700123 if (focused) {
124 if (hasSelection()) {
Michael Kolb94ec5272011-08-31 16:23:57 -0700125 state = StateListener.STATE_HIGHLIGHTED;
Michael Kolb305b1c52011-06-21 16:16:22 -0700126 } else {
Michael Kolb94ec5272011-08-31 16:23:57 -0700127 state = StateListener.STATE_EDITED;
Michael Kolb305b1c52011-06-21 16:16:22 -0700128 }
129 } else {
130 // reset the selection state
Michael Kolb94ec5272011-08-31 16:23:57 -0700131 state = StateListener.STATE_NORMAL;
Michael Kolb305b1c52011-06-21 16:16:22 -0700132 }
Michael Kolb94ec5272011-08-31 16:23:57 -0700133 final int s = state;
134 post(new Runnable() {
135 public void run() {
136 changeState(s);
137 }
138 });
Michael Kolb305b1c52011-06-21 16:16:22 -0700139 }
140
141 @Override
142 public boolean onTouchEvent(MotionEvent evt) {
143 boolean hasSelection = hasSelection();
144 boolean res = super.onTouchEvent(evt);
145 if ((MotionEvent.ACTION_DOWN == evt.getActionMasked())
146 && hasSelection) {
Michael Kolb94ec5272011-08-31 16:23:57 -0700147 postDelayed(new Runnable() {
148 public void run() {
149 changeState(StateListener.STATE_EDITED);
150 }}, POST_DELAY);
Michael Kolb305b1c52011-06-21 16:16:22 -0700151 }
152 return res;
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800153 }
154
155 /**
156 * check if focus change requires a title bar update
157 */
158 boolean needsUpdate() {
159 return mNeedsUpdate;
160 }
161
162 /**
163 * clear the focus change needs title bar update flag
164 */
165 void clearNeedsUpdate() {
166 mNeedsUpdate = false;
Michael Kolb21ce4d22010-09-15 14:55:05 -0700167 }
168
Michael Kolbba99c5d2010-11-29 14:57:41 -0800169 void setController(UiController controller) {
170 UrlSelectionActionMode urlSelectionMode
171 = new UrlSelectionActionMode(controller);
172 setCustomSelectionActionModeCallback(urlSelectionMode);
173 }
174
Michael Kolb21ce4d22010-09-15 14:55:05 -0700175 void setContainer(View container) {
176 mContainer = container;
177 }
178
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800179 public void setUrlInputListener(UrlInputListener listener) {
180 mListener = listener;
181 }
182
Michael Kolb305b1c52011-06-21 16:16:22 -0700183 public void setStateListener(StateListener listener) {
184 mStateListener = listener;
185 // update listener
186 changeState(mState);
187 }
188
189 private void changeState(int newState) {
190 mState = newState;
191 if (mStateListener != null) {
192 mStateListener.onStateChanged(mState);
193 }
194 }
195
Michael Kolb5e8f2b92011-07-19 13:22:32 -0700196 int getState() {
197 return mState;
198 }
199
Michael Kolb21ce4d22010-09-15 14:55:05 -0700200 @Override
201 protected void onConfigurationChanged(Configuration config) {
John Reck35defff2010-11-11 14:06:45 -0800202 super.onConfigurationChanged(config);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700203 mLandscape = (config.orientation &
Michael Kolb31d469b2010-12-09 20:49:54 -0800204 Configuration.ORIENTATION_LANDSCAPE) != 0;
John Reck35defff2010-11-11 14:06:45 -0800205 mAdapter.setLandscapeMode(mLandscape);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700206 if (isPopupShowing() && (getVisibility() == View.VISIBLE)) {
John Reck35defff2010-11-11 14:06:45 -0800207 setupDropDown();
Narayan Kamathf3174a52011-11-17 14:43:32 +0000208 performFiltering(getText(), 0);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700209 }
210 }
211
212 @Override
213 public void showDropDown() {
John Reck35defff2010-11-11 14:06:45 -0800214 setupDropDown();
215 super.showDropDown();
216 }
217
218 @Override
219 public void dismissDropDown() {
220 super.dismissDropDown();
221 mAdapter.clearCache();
222 }
223
224 private void setupDropDown() {
John Reck92026732011-02-15 10:12:30 -0800225 int width = mContainer != null ? mContainer.getWidth() : getWidth();
John Reck8d021aa2011-09-06 15:30:11 -0700226 width += mPopupPadding.left + mPopupPadding.right;
Michael Kolbc5998c22010-10-10 16:04:35 -0700227 if (width != getDropDownWidth()) {
228 setDropDownWidth(width);
229 }
John Reck8d021aa2011-09-06 15:30:11 -0700230 int left = getLeft();
231 left += mPopupPadding.left;
232 if (left != -getDropDownHorizontalOffset()) {
233 setDropDownHorizontalOffset(-left);
Michael Kolbc5998c22010-10-10 16:04:35 -0700234 }
Michael Kolb513286f2010-09-09 12:55:12 -0700235 }
236
237 @Override
Michael Kolbed217742010-08-10 17:52:34 -0700238 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
Narayan Kamathf3174a52011-11-17 14:43:32 +0000239 finishInput(getText().toString(), null, TYPED);
Michael Kolbed217742010-08-10 17:52:34 -0700240 return true;
241 }
Michael Kolb21ce4d22010-09-15 14:55:05 -0700242
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800243 void forceFilter() {
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800244 showDropDown();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700245 }
246
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800247 void hideIME() {
248 mInputManager.hideSoftInputFromWindow(getWindowToken(), 0);
249 }
250
Michael Kolb4bb6fcb2012-04-13 14:25:27 -0700251 void showIME() {
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800252 //mInputManager.focusIn(this);
253 Object[] params = {this};
254 Class[] type = new Class[] {View.class};
255 ReflectHelper.invokeMethod(mInputManager, "focusIn", type, params);
Michael Kolb4bb6fcb2012-04-13 14:25:27 -0700256 mInputManager.showSoftInput(this, 0);
257 }
258
Michael Kolb257cc2c2010-12-09 09:45:52 -0800259 private void finishInput(String url, String extra, String source) {
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800260 mNeedsUpdate = true;
261 dismissDropDown();
Michael Kolbfe251992010-07-08 15:41:55 -0700262 mInputManager.hideSoftInputFromWindow(getWindowToken(), 0);
John Reckdcda1d52010-11-29 10:05:54 -0800263 if (TextUtils.isEmpty(url)) {
Michael Kolbfe251992010-07-08 15:41:55 -0700264 mListener.onDismiss();
265 } else {
John Reckb77bdc42011-01-24 13:24:48 -0800266 if (mIncognitoMode && isSearch(url)) {
267 // To prevent logging, intercept this request
268 // TODO: This is a quick hack, refactor this
269 SearchEngine searchEngine = BrowserSettings.getInstance()
270 .getSearchEngine();
271 if (searchEngine == null) return;
272 SearchEngineInfo engineInfo = SearchEngines
Bijan Amirzada9b1e9882014-02-26 17:15:46 -0800273 .getSearchEngineInfo(getContext(), searchEngine.getName());
John Reckb77bdc42011-01-24 13:24:48 -0800274 if (engineInfo == null) return;
275 url = engineInfo.getSearchUriForQuery(url);
276 // mLister.onAction can take it from here without logging
277 }
Michael Kolb257cc2c2010-12-09 09:45:52 -0800278 mListener.onAction(url, extra, source);
Michael Kolbfe251992010-07-08 15:41:55 -0700279 }
Michael Kolbfe251992010-07-08 15:41:55 -0700280 }
281
John Reckb77bdc42011-01-24 13:24:48 -0800282 boolean isSearch(String inUrl) {
283 String url = UrlUtils.fixUrl(inUrl).trim();
284 if (TextUtils.isEmpty(url)) return false;
285
286 if (Patterns.WEB_URL.matcher(url).matches()
287 || UrlUtils.ACCEPTED_URI_SCHEMA.matcher(url).matches()) {
288 return false;
289 }
290 return true;
291 }
292
Michael Kolb21ce4d22010-09-15 14:55:05 -0700293 // Completion Listener
294
295 @Override
296 public void onSearch(String search) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800297 mListener.onCopySuggestion(search);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700298 }
299
300 @Override
Michael Kolbbd2dd642011-01-13 13:01:30 -0800301 public void onSelect(String url, int type, String extra) {
Michael Kolb5ff5c8b2012-05-03 11:37:58 -0700302 finishInput(url, extra, SUGGESTED);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700303 }
304
Michael Kolbfe251992010-07-08 15:41:55 -0700305 @Override
John Reck87369ea2011-01-23 15:20:38 -0800306 public void onItemClick(
307 AdapterView<?> parent, View view, int position, long id) {
308 SuggestItem item = mAdapter.getItem(position);
Narayan Kamath5119edd2011-02-23 15:49:17 +0000309 onSelect(SuggestionsAdapter.getSuggestionUrl(item), item.type, item.extra);
John Reck87369ea2011-01-23 15:20:38 -0800310 }
311
Michael Kolbfe251992010-07-08 15:41:55 -0700312 interface UrlInputListener {
Michael Kolb21ce4d22010-09-15 14:55:05 -0700313
Michael Kolbfe251992010-07-08 15:41:55 -0700314 public void onDismiss();
Michael Kolb21ce4d22010-09-15 14:55:05 -0700315
Michael Kolb257cc2c2010-12-09 09:45:52 -0800316 public void onAction(String text, String extra, String source);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700317
Michael Kolb7cdc4902011-02-03 17:54:40 -0800318 public void onCopySuggestion(String text);
Michael Kolbfe251992010-07-08 15:41:55 -0700319
320 }
321
John Reck117f07d2011-01-24 09:39:03 -0800322 public void setIncognitoMode(boolean incognito) {
John Reckb77bdc42011-01-24 13:24:48 -0800323 mIncognitoMode = incognito;
324 mAdapter.setIncognitoMode(mIncognitoMode);
John Reck117f07d2011-01-24 09:39:03 -0800325 }
326
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800327 @Override
328 public boolean onKeyDown(int keyCode, KeyEvent evt) {
329 if (keyCode == KeyEvent.KEYCODE_ESCAPE && !isInTouchMode()) {
330 finishInput(null, null, null);
331 return true;
332 }
333 return super.onKeyDown(keyCode, evt);
334 }
335
Narayan Kamath80aad8d2011-02-23 12:01:13 +0000336 public SuggestionsAdapter getAdapter() {
337 return mAdapter;
338 }
Narayan Kamath5119edd2011-02-23 15:49:17 +0000339
Michael Kolb74e60c22011-04-30 16:43:47 -0700340 /*
341 * no-op to prevent scrolling of webview when embedded titlebar
342 * gets edited
343 */
344 @Override
345 public boolean requestRectangleOnScreen(Rect rect, boolean immediate) {
346 return false;
347 }
Michael Kolb305b1c52011-06-21 16:16:22 -0700348
349 @Override
Narayan Kamathf3174a52011-11-17 14:43:32 +0000350 public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
351
352 @Override
353 public void onTextChanged(CharSequence s, int start, int before, int count) {
Michael Kolb305b1c52011-06-21 16:16:22 -0700354 if (StateListener.STATE_HIGHLIGHTED == mState) {
355 changeState(StateListener.STATE_EDITED);
356 }
357 }
358
Narayan Kamathf3174a52011-11-17 14:43:32 +0000359 @Override
360 public void afterTextChanged(Editable s) { }
361
Michael Kolbfe251992010-07-08 15:41:55 -0700362}