blob: 02bba3cbe2d771d6bc71146a5f910c44aa2bde82 [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 Kolbfe251992010-07-08 15:41:55 -070019import android.content.Context;
Michael Kolb21ce4d22010-09-15 14:55:05 -070020import android.content.res.Configuration;
John Reck8d021aa2011-09-06 15:30:11 -070021import android.content.res.TypedArray;
Narayan Kamath5119edd2011-02-23 15:49:17 +000022import android.graphics.Rect;
John Reck8d021aa2011-09-06 15:30:11 -070023import android.graphics.drawable.Drawable;
Narayan Kamathf3174a52011-11-17 14:43:32 +000024import android.text.Editable;
John Reckdcda1d52010-11-29 10:05:54 -080025import android.text.TextUtils;
Narayan Kamathf3174a52011-11-17 14:43:32 +000026import android.text.TextWatcher;
Michael Kolbfe251992010-07-08 15:41:55 -070027import android.util.AttributeSet;
John Reckb77bdc42011-01-24 13:24:48 -080028import android.util.Patterns;
Michael Kolbfe251992010-07-08 15:41:55 -070029import android.view.KeyEvent;
Michael Kolb305b1c52011-06-21 16:16:22 -070030import android.view.MotionEvent;
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;
Narayan Kamathf3174a52011-11-17 14:43:32 +000035import 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 Kolb305b1c52011-06-21 16:16:22 -070039import com.android.browser.SuggestionsAdapter.CompletionListener;
40import com.android.browser.SuggestionsAdapter.SuggestItem;
Michael Kolb305b1c52011-06-21 16:16:22 -070041import com.android.browser.search.SearchEngine;
42import com.android.browser.search.SearchEngineInfo;
43import com.android.browser.search.SearchEngines;
John Reck8d021aa2011-09-06 15:30:11 -070044import com.android.internal.R;
Michael Kolb305b1c52011-06-21 16:16:22 -070045
Michael Kolbcfa3af52010-12-14 10:36:11 -080046import java.util.List;
47
Michael Kolbfe251992010-07-08 15:41:55 -070048/**
49 * url/search input view
50 * handling suggestions
51 */
Narayan Kamathf3174a52011-11-17 14:43:32 +000052public class UrlInputView extends AutoCompleteTextView
Michael Kolbdc2ee1b2011-02-14 14:34:40 -080053 implements OnEditorActionListener,
Narayan Kamathf3174a52011-11-17 14:43:32 +000054 CompletionListener, OnItemClickListener, TextWatcher {
Michael Kolb257cc2c2010-12-09 09:45:52 -080055
56 static final String TYPED = "browser-type";
57 static final String SUGGESTED = "browser-suggest";
58
Michael Kolb94ec5272011-08-31 16:23:57 -070059 static final int POST_DELAY = 100;
60
Michael Kolb305b1c52011-06-21 16:16:22 -070061 static interface StateListener {
62 static final int STATE_NORMAL = 0;
63 static final int STATE_HIGHLIGHTED = 1;
64 static final int STATE_EDITED = 2;
65
66 public void onStateChanged(int state);
67 }
68
Michael Kolbfe251992010-07-08 15:41:55 -070069 private UrlInputListener mListener;
70 private InputMethodManager mInputManager;
71 private SuggestionsAdapter mAdapter;
Michael Kolb21ce4d22010-09-15 14:55:05 -070072 private View mContainer;
73 private boolean mLandscape;
John Reckb77bdc42011-01-24 13:24:48 -080074 private boolean mIncognitoMode;
Michael Kolbdc2ee1b2011-02-14 14:34:40 -080075 private boolean mNeedsUpdate;
Michael Kolbfe251992010-07-08 15:41:55 -070076
Michael Kolb305b1c52011-06-21 16:16:22 -070077 private int mState;
78 private StateListener mStateListener;
John Reck8d021aa2011-09-06 15:30:11 -070079 private Rect mPopupPadding;
Michael Kolb305b1c52011-06-21 16:16:22 -070080
Michael Kolbfe251992010-07-08 15:41:55 -070081 public UrlInputView(Context context, AttributeSet attrs, int defStyle) {
82 super(context, attrs, defStyle);
John Reck8d021aa2011-09-06 15:30:11 -070083 TypedArray a = context.obtainStyledAttributes(
84 attrs, com.android.internal.R.styleable.PopupWindow,
85 R.attr.autoCompleteTextViewStyle, 0);
86
87 Drawable popupbg = a.getDrawable(R.styleable.PopupWindow_popupBackground);
88 a.recycle();
89 mPopupPadding = new Rect();
90 popupbg.getPadding(mPopupPadding);
Michael Kolbfe251992010-07-08 15:41:55 -070091 init(context);
92 }
93
94 public UrlInputView(Context context, AttributeSet attrs) {
John Reck8d021aa2011-09-06 15:30:11 -070095 this(context, attrs, R.attr.autoCompleteTextViewStyle);
Michael Kolbfe251992010-07-08 15:41:55 -070096 }
97
98 public UrlInputView(Context context) {
John Reck8d021aa2011-09-06 15:30:11 -070099 this(context, null);
Michael Kolbfe251992010-07-08 15:41:55 -0700100 }
101
102 private void init(Context ctx) {
Michael Kolbfe251992010-07-08 15:41:55 -0700103 mInputManager = (InputMethodManager) ctx.getSystemService(Context.INPUT_METHOD_SERVICE);
Michael Kolbed217742010-08-10 17:52:34 -0700104 setOnEditorActionListener(this);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700105 mAdapter = new SuggestionsAdapter(ctx, this);
Michael Kolbfe251992010-07-08 15:41:55 -0700106 setAdapter(mAdapter);
Michael Kolbba99c5d2010-11-29 14:57:41 -0800107 setSelectAllOnFocus(true);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700108 onConfigurationChanged(ctx.getResources().getConfiguration());
John Reck35defff2010-11-11 14:06:45 -0800109 setThreshold(1);
John Reck87369ea2011-01-23 15:20:38 -0800110 setOnItemClickListener(this);
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800111 mNeedsUpdate = false;
Narayan Kamathf3174a52011-11-17 14:43:32 +0000112 addTextChangedListener(this);
Narayan Kamath5119edd2011-02-23 15:49:17 +0000113
Michael Kolb305b1c52011-06-21 16:16:22 -0700114 mState = StateListener.STATE_NORMAL;
115 }
116
117 protected void onFocusChanged(boolean focused, int direction, Rect prevRect) {
118 super.onFocusChanged(focused, direction, prevRect);
Michael Kolb94ec5272011-08-31 16:23:57 -0700119 int state = -1;
Michael Kolb305b1c52011-06-21 16:16:22 -0700120 if (focused) {
121 if (hasSelection()) {
Michael Kolb94ec5272011-08-31 16:23:57 -0700122 state = StateListener.STATE_HIGHLIGHTED;
Michael Kolb305b1c52011-06-21 16:16:22 -0700123 } else {
Michael Kolb94ec5272011-08-31 16:23:57 -0700124 state = StateListener.STATE_EDITED;
Michael Kolb305b1c52011-06-21 16:16:22 -0700125 }
126 } else {
127 // reset the selection state
Michael Kolb94ec5272011-08-31 16:23:57 -0700128 state = StateListener.STATE_NORMAL;
Michael Kolb305b1c52011-06-21 16:16:22 -0700129 }
Michael Kolb94ec5272011-08-31 16:23:57 -0700130 final int s = state;
131 post(new Runnable() {
132 public void run() {
133 changeState(s);
134 }
135 });
Michael Kolb305b1c52011-06-21 16:16:22 -0700136 }
137
138 @Override
139 public boolean onTouchEvent(MotionEvent evt) {
140 boolean hasSelection = hasSelection();
141 boolean res = super.onTouchEvent(evt);
142 if ((MotionEvent.ACTION_DOWN == evt.getActionMasked())
143 && hasSelection) {
Michael Kolb94ec5272011-08-31 16:23:57 -0700144 postDelayed(new Runnable() {
145 public void run() {
146 changeState(StateListener.STATE_EDITED);
147 }}, POST_DELAY);
Michael Kolb305b1c52011-06-21 16:16:22 -0700148 }
149 return res;
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800150 }
151
152 /**
153 * check if focus change requires a title bar update
154 */
155 boolean needsUpdate() {
156 return mNeedsUpdate;
157 }
158
159 /**
160 * clear the focus change needs title bar update flag
161 */
162 void clearNeedsUpdate() {
163 mNeedsUpdate = false;
Michael Kolb21ce4d22010-09-15 14:55:05 -0700164 }
165
Michael Kolbba99c5d2010-11-29 14:57:41 -0800166 void setController(UiController controller) {
167 UrlSelectionActionMode urlSelectionMode
168 = new UrlSelectionActionMode(controller);
169 setCustomSelectionActionModeCallback(urlSelectionMode);
170 }
171
Michael Kolb21ce4d22010-09-15 14:55:05 -0700172 void setContainer(View container) {
173 mContainer = container;
174 }
175
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800176 public void setUrlInputListener(UrlInputListener listener) {
177 mListener = listener;
178 }
179
Michael Kolb305b1c52011-06-21 16:16:22 -0700180 public void setStateListener(StateListener listener) {
181 mStateListener = listener;
182 // update listener
183 changeState(mState);
184 }
185
186 private void changeState(int newState) {
187 mState = newState;
188 if (mStateListener != null) {
189 mStateListener.onStateChanged(mState);
190 }
191 }
192
Michael Kolb5e8f2b92011-07-19 13:22:32 -0700193 int getState() {
194 return mState;
195 }
196
Michael Kolb21ce4d22010-09-15 14:55:05 -0700197 @Override
198 protected void onConfigurationChanged(Configuration config) {
John Reck35defff2010-11-11 14:06:45 -0800199 super.onConfigurationChanged(config);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700200 mLandscape = (config.orientation &
Michael Kolb31d469b2010-12-09 20:49:54 -0800201 Configuration.ORIENTATION_LANDSCAPE) != 0;
John Reck35defff2010-11-11 14:06:45 -0800202 mAdapter.setLandscapeMode(mLandscape);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700203 if (isPopupShowing() && (getVisibility() == View.VISIBLE)) {
John Reck35defff2010-11-11 14:06:45 -0800204 setupDropDown();
Narayan Kamathf3174a52011-11-17 14:43:32 +0000205 performFiltering(getText(), 0);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700206 }
207 }
208
209 @Override
210 public void showDropDown() {
John Reck35defff2010-11-11 14:06:45 -0800211 setupDropDown();
212 super.showDropDown();
213 }
214
215 @Override
216 public void dismissDropDown() {
217 super.dismissDropDown();
218 mAdapter.clearCache();
219 }
220
221 private void setupDropDown() {
John Reck92026732011-02-15 10:12:30 -0800222 int width = mContainer != null ? mContainer.getWidth() : getWidth();
John Reck8d021aa2011-09-06 15:30:11 -0700223 width += mPopupPadding.left + mPopupPadding.right;
Michael Kolbc5998c22010-10-10 16:04:35 -0700224 if (width != getDropDownWidth()) {
225 setDropDownWidth(width);
226 }
John Reck8d021aa2011-09-06 15:30:11 -0700227 int left = getLeft();
228 left += mPopupPadding.left;
229 if (left != -getDropDownHorizontalOffset()) {
230 setDropDownHorizontalOffset(-left);
Michael Kolbc5998c22010-10-10 16:04:35 -0700231 }
Michael Kolb513286f2010-09-09 12:55:12 -0700232 }
233
234 @Override
Michael Kolbed217742010-08-10 17:52:34 -0700235 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
Narayan Kamathf3174a52011-11-17 14:43:32 +0000236 finishInput(getText().toString(), null, TYPED);
Michael Kolbed217742010-08-10 17:52:34 -0700237 return true;
238 }
Michael Kolb21ce4d22010-09-15 14:55:05 -0700239
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800240 void forceFilter() {
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800241 showDropDown();
Michael Kolba2b2ba82010-08-04 17:54:03 -0700242 }
243
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800244 void hideIME() {
245 mInputManager.hideSoftInputFromWindow(getWindowToken(), 0);
246 }
247
Michael Kolb4bb6fcb2012-04-13 14:25:27 -0700248 void showIME() {
249 mInputManager.focusIn(this);
250 mInputManager.showSoftInput(this, 0);
251 }
252
Michael Kolb257cc2c2010-12-09 09:45:52 -0800253 private void finishInput(String url, String extra, String source) {
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800254 mNeedsUpdate = true;
255 dismissDropDown();
Michael Kolbfe251992010-07-08 15:41:55 -0700256 mInputManager.hideSoftInputFromWindow(getWindowToken(), 0);
John Reckdcda1d52010-11-29 10:05:54 -0800257 if (TextUtils.isEmpty(url)) {
Michael Kolbfe251992010-07-08 15:41:55 -0700258 mListener.onDismiss();
259 } else {
John Reckb77bdc42011-01-24 13:24:48 -0800260 if (mIncognitoMode && isSearch(url)) {
261 // To prevent logging, intercept this request
262 // TODO: This is a quick hack, refactor this
263 SearchEngine searchEngine = BrowserSettings.getInstance()
264 .getSearchEngine();
265 if (searchEngine == null) return;
266 SearchEngineInfo engineInfo = SearchEngines
267 .getSearchEngineInfo(mContext, searchEngine.getName());
268 if (engineInfo == null) return;
269 url = engineInfo.getSearchUriForQuery(url);
270 // mLister.onAction can take it from here without logging
271 }
Michael Kolb257cc2c2010-12-09 09:45:52 -0800272 mListener.onAction(url, extra, source);
Michael Kolbfe251992010-07-08 15:41:55 -0700273 }
Michael Kolbfe251992010-07-08 15:41:55 -0700274 }
275
John Reckb77bdc42011-01-24 13:24:48 -0800276 boolean isSearch(String inUrl) {
277 String url = UrlUtils.fixUrl(inUrl).trim();
278 if (TextUtils.isEmpty(url)) return false;
279
280 if (Patterns.WEB_URL.matcher(url).matches()
281 || UrlUtils.ACCEPTED_URI_SCHEMA.matcher(url).matches()) {
282 return false;
283 }
284 return true;
285 }
286
Michael Kolb21ce4d22010-09-15 14:55:05 -0700287 // Completion Listener
288
289 @Override
290 public void onSearch(String search) {
Michael Kolb7cdc4902011-02-03 17:54:40 -0800291 mListener.onCopySuggestion(search);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700292 }
293
294 @Override
Michael Kolbbd2dd642011-01-13 13:01:30 -0800295 public void onSelect(String url, int type, String extra) {
Michael Kolb5ff5c8b2012-05-03 11:37:58 -0700296 finishInput(url, extra, SUGGESTED);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700297 }
298
Michael Kolbfe251992010-07-08 15:41:55 -0700299 @Override
John Reck87369ea2011-01-23 15:20:38 -0800300 public void onItemClick(
301 AdapterView<?> parent, View view, int position, long id) {
302 SuggestItem item = mAdapter.getItem(position);
Narayan Kamath5119edd2011-02-23 15:49:17 +0000303 onSelect(SuggestionsAdapter.getSuggestionUrl(item), item.type, item.extra);
John Reck87369ea2011-01-23 15:20:38 -0800304 }
305
Michael Kolbfe251992010-07-08 15:41:55 -0700306 interface UrlInputListener {
Michael Kolb21ce4d22010-09-15 14:55:05 -0700307
Michael Kolbfe251992010-07-08 15:41:55 -0700308 public void onDismiss();
Michael Kolb21ce4d22010-09-15 14:55:05 -0700309
Michael Kolb257cc2c2010-12-09 09:45:52 -0800310 public void onAction(String text, String extra, String source);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700311
Michael Kolb7cdc4902011-02-03 17:54:40 -0800312 public void onCopySuggestion(String text);
Michael Kolbfe251992010-07-08 15:41:55 -0700313
314 }
315
John Reck117f07d2011-01-24 09:39:03 -0800316 public void setIncognitoMode(boolean incognito) {
John Reckb77bdc42011-01-24 13:24:48 -0800317 mIncognitoMode = incognito;
318 mAdapter.setIncognitoMode(mIncognitoMode);
John Reck117f07d2011-01-24 09:39:03 -0800319 }
320
Michael Kolbdc2ee1b2011-02-14 14:34:40 -0800321 @Override
322 public boolean onKeyDown(int keyCode, KeyEvent evt) {
323 if (keyCode == KeyEvent.KEYCODE_ESCAPE && !isInTouchMode()) {
324 finishInput(null, null, null);
325 return true;
326 }
327 return super.onKeyDown(keyCode, evt);
328 }
329
Narayan Kamath80aad8d2011-02-23 12:01:13 +0000330 public SuggestionsAdapter getAdapter() {
331 return mAdapter;
332 }
Narayan Kamath5119edd2011-02-23 15:49:17 +0000333
Michael Kolb74e60c22011-04-30 16:43:47 -0700334 /*
335 * no-op to prevent scrolling of webview when embedded titlebar
336 * gets edited
337 */
338 @Override
339 public boolean requestRectangleOnScreen(Rect rect, boolean immediate) {
340 return false;
341 }
Michael Kolb305b1c52011-06-21 16:16:22 -0700342
343 @Override
Narayan Kamathf3174a52011-11-17 14:43:32 +0000344 public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
345
346 @Override
347 public void onTextChanged(CharSequence s, int start, int before, int count) {
Michael Kolb305b1c52011-06-21 16:16:22 -0700348 if (StateListener.STATE_HIGHLIGHTED == mState) {
349 changeState(StateListener.STATE_EDITED);
350 }
351 }
352
Narayan Kamathf3174a52011-11-17 14:43:32 +0000353 @Override
354 public void afterTextChanged(Editable s) { }
355
Michael Kolbfe251992010-07-08 15:41:55 -0700356}