blob: 428a0f2ab69ed68d3bcc6880827f59afe70834fb [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 Reckad373302010-12-17 15:28:13 -080020import com.android.browser.SuggestionsAdapter.SuggestItem;
Michael Kolb21ce4d22010-09-15 14:55:05 -070021
Michael Kolbfe251992010-07-08 15:41:55 -070022import android.content.Context;
Michael Kolb21ce4d22010-09-15 14:55:05 -070023import android.content.res.Configuration;
John Reckdcda1d52010-11-29 10:05:54 -080024import android.text.TextUtils;
Michael Kolbfe251992010-07-08 15:41:55 -070025import android.util.AttributeSet;
26import android.view.KeyEvent;
Michael Kolbfe251992010-07-08 15:41:55 -070027import android.view.View;
Michael Kolba2b2ba82010-08-04 17:54:03 -070028import android.view.View.OnFocusChangeListener;
Michael Kolbfe251992010-07-08 15:41:55 -070029import android.view.inputmethod.InputMethodManager;
John Reckad373302010-12-17 15:28:13 -080030import android.widget.AdapterView;
31import android.widget.AdapterView.OnItemClickListener;
Michael Kolbfe251992010-07-08 15:41:55 -070032import android.widget.AutoCompleteTextView;
Michael Kolbfe251992010-07-08 15:41:55 -070033import android.widget.TextView;
Michael Kolbed217742010-08-10 17:52:34 -070034import android.widget.TextView.OnEditorActionListener;
Michael Kolbfe251992010-07-08 15:41:55 -070035
Michael Kolbcfa3af52010-12-14 10:36:11 -080036import java.util.List;
37
Michael Kolbfe251992010-07-08 15:41:55 -070038/**
39 * url/search input view
40 * handling suggestions
41 */
Michael Kolba2b2ba82010-08-04 17:54:03 -070042public class UrlInputView extends AutoCompleteTextView
Michael Kolbba99c5d2010-11-29 14:57:41 -080043 implements OnFocusChangeListener, OnEditorActionListener,
John Reckad373302010-12-17 15:28:13 -080044 CompletionListener, OnItemClickListener {
Michael Kolbfe251992010-07-08 15:41:55 -070045
Michael Kolb257cc2c2010-12-09 09:45:52 -080046
47 static final String TYPED = "browser-type";
48 static final String SUGGESTED = "browser-suggest";
49
Michael Kolbfe251992010-07-08 15:41:55 -070050 private UrlInputListener mListener;
51 private InputMethodManager mInputManager;
52 private SuggestionsAdapter mAdapter;
Michael Kolbc7485ae2010-09-03 10:10:58 -070053 private OnFocusChangeListener mWrappedFocusListener;
Michael Kolb21ce4d22010-09-15 14:55:05 -070054 private View mContainer;
55 private boolean mLandscape;
Michael Kolbcfa3af52010-12-14 10:36:11 -080056 private boolean mInVoiceMode;
Michael Kolbfe251992010-07-08 15:41:55 -070057
58 public UrlInputView(Context context, AttributeSet attrs, int defStyle) {
59 super(context, attrs, defStyle);
60 init(context);
61 }
62
63 public UrlInputView(Context context, AttributeSet attrs) {
64 super(context, attrs);
65 init(context);
66 }
67
68 public UrlInputView(Context context) {
69 super(context);
70 init(context);
71 }
72
73 private void init(Context ctx) {
Michael Kolbfe251992010-07-08 15:41:55 -070074 mInputManager = (InputMethodManager) ctx.getSystemService(Context.INPUT_METHOD_SERVICE);
Michael Kolbed217742010-08-10 17:52:34 -070075 setOnEditorActionListener(this);
Michael Kolbc7485ae2010-09-03 10:10:58 -070076 super.setOnFocusChangeListener(this);
Michael Kolb21ce4d22010-09-15 14:55:05 -070077 mAdapter = new SuggestionsAdapter(ctx, this);
Michael Kolbfe251992010-07-08 15:41:55 -070078 setAdapter(mAdapter);
Michael Kolbba99c5d2010-11-29 14:57:41 -080079 setSelectAllOnFocus(true);
Michael Kolb21ce4d22010-09-15 14:55:05 -070080 onConfigurationChanged(ctx.getResources().getConfiguration());
John Reck35defff2010-11-11 14:06:45 -080081 setThreshold(1);
John Reckad373302010-12-17 15:28:13 -080082 setOnItemClickListener(this);
Michael Kolb21ce4d22010-09-15 14:55:05 -070083 }
84
Michael Kolbba99c5d2010-11-29 14:57:41 -080085 void setController(UiController controller) {
86 UrlSelectionActionMode urlSelectionMode
87 = new UrlSelectionActionMode(controller);
88 setCustomSelectionActionModeCallback(urlSelectionMode);
89 }
90
Michael Kolb21ce4d22010-09-15 14:55:05 -070091 void setContainer(View container) {
92 mContainer = container;
93 }
94
Michael Kolbcfa3af52010-12-14 10:36:11 -080095 void setVoiceResults(List<String> voiceResults) {
96 mAdapter.setVoiceResults(voiceResults);
97 mInVoiceMode = (voiceResults != null);
98 }
99
Michael Kolb21ce4d22010-09-15 14:55:05 -0700100 @Override
101 protected void onConfigurationChanged(Configuration config) {
John Reck35defff2010-11-11 14:06:45 -0800102 super.onConfigurationChanged(config);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700103 mLandscape = (config.orientation &
Michael Kolb31d469b2010-12-09 20:49:54 -0800104 Configuration.ORIENTATION_LANDSCAPE) != 0;
John Reck35defff2010-11-11 14:06:45 -0800105 mAdapter.setLandscapeMode(mLandscape);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700106 if (isPopupShowing() && (getVisibility() == View.VISIBLE)) {
John Reck35defff2010-11-11 14:06:45 -0800107 setupDropDown();
John Reckad373302010-12-17 15:28:13 -0800108 performFiltering(getText(), 0);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700109 }
110 }
111
112 @Override
113 public void showDropDown() {
John Reck35defff2010-11-11 14:06:45 -0800114 setupDropDown();
115 super.showDropDown();
116 }
117
118 @Override
119 public void dismissDropDown() {
120 super.dismissDropDown();
121 mAdapter.clearCache();
122 }
123
124 private void setupDropDown() {
Michael Kolb21ce4d22010-09-15 14:55:05 -0700125 int width = mContainer.getWidth();
Michael Kolbc5998c22010-10-10 16:04:35 -0700126 if (width != getDropDownWidth()) {
127 setDropDownWidth(width);
128 }
129 if (getLeft() != -getDropDownHorizontalOffset()) {
130 setDropDownHorizontalOffset(-getLeft());
131 }
Michael Kolb31d469b2010-12-09 20:49:54 -0800132 setDropDownVerticalOffset(8);
Michael Kolb513286f2010-09-09 12:55:12 -0700133 }
134
135 @Override
Michael Kolbc7485ae2010-09-03 10:10:58 -0700136 public void setOnFocusChangeListener(OnFocusChangeListener focusListener) {
137 mWrappedFocusListener = focusListener;
Michael Kolbfe251992010-07-08 15:41:55 -0700138 }
139
Michael Kolba2b2ba82010-08-04 17:54:03 -0700140 @Override
Michael Kolbed217742010-08-10 17:52:34 -0700141 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
Michael Kolb257cc2c2010-12-09 09:45:52 -0800142 finishInput(getText().toString(), null, TYPED);
Michael Kolbed217742010-08-10 17:52:34 -0700143 return true;
144 }
Michael Kolb21ce4d22010-09-15 14:55:05 -0700145
Michael Kolbc7485ae2010-09-03 10:10:58 -0700146 @Override
Michael Kolba2b2ba82010-08-04 17:54:03 -0700147 public void onFocusChange(View v, boolean hasFocus) {
Michael Kolba2b2ba82010-08-04 17:54:03 -0700148 if (hasFocus) {
149 forceIme();
Michael Kolbcfa3af52010-12-14 10:36:11 -0800150 if (mInVoiceMode) {
151 performFiltering(getText().toString(), 0);
152 showDropDown();
153 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700154 } else {
Michael Kolb257cc2c2010-12-09 09:45:52 -0800155 finishInput(null, null, null);
Michael Kolba2b2ba82010-08-04 17:54:03 -0700156 }
Michael Kolbc7485ae2010-09-03 10:10:58 -0700157 if (mWrappedFocusListener != null) {
158 mWrappedFocusListener.onFocusChange(v, hasFocus);
159 }
Michael Kolba2b2ba82010-08-04 17:54:03 -0700160 }
161
Michael Kolbfe251992010-07-08 15:41:55 -0700162 public void setUrlInputListener(UrlInputListener listener) {
163 mListener = listener;
164 }
165
166 public void forceIme() {
167 mInputManager.showSoftInput(this, 0);
168 }
169
Michael Kolb257cc2c2010-12-09 09:45:52 -0800170 private void finishInput(String url, String extra, String source) {
Michael Kolbfe251992010-07-08 15:41:55 -0700171 this.dismissDropDown();
172 mInputManager.hideSoftInputFromWindow(getWindowToken(), 0);
John Reckdcda1d52010-11-29 10:05:54 -0800173 if (TextUtils.isEmpty(url)) {
Michael Kolbfe251992010-07-08 15:41:55 -0700174 mListener.onDismiss();
175 } else {
Michael Kolb257cc2c2010-12-09 09:45:52 -0800176 mListener.onAction(url, extra, source);
Michael Kolbfe251992010-07-08 15:41:55 -0700177 }
Michael Kolbfe251992010-07-08 15:41:55 -0700178 }
179
Michael Kolb21ce4d22010-09-15 14:55:05 -0700180 // Completion Listener
181
182 @Override
183 public void onSearch(String search) {
184 mListener.onEdit(search);
185 }
186
187 @Override
John Reck40f720e2010-11-10 11:57:04 -0800188 public void onSelect(String url, String extra) {
Michael Kolb257cc2c2010-12-09 09:45:52 -0800189 finishInput(url, extra, SUGGESTED);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700190 }
191
Michael Kolbfe251992010-07-08 15:41:55 -0700192 @Override
193 public boolean onKeyPreIme(int keyCode, KeyEvent evt) {
194 if (keyCode == KeyEvent.KEYCODE_BACK) {
195 // catch back key in order to do slightly more cleanup than usual
Michael Kolb257cc2c2010-12-09 09:45:52 -0800196 finishInput(null, null, null);
Michael Kolbfe251992010-07-08 15:41:55 -0700197 return true;
198 }
199 return super.onKeyPreIme(keyCode, evt);
200 }
201
202 interface UrlInputListener {
Michael Kolb21ce4d22010-09-15 14:55:05 -0700203
Michael Kolbfe251992010-07-08 15:41:55 -0700204 public void onDismiss();
Michael Kolb21ce4d22010-09-15 14:55:05 -0700205
Michael Kolb257cc2c2010-12-09 09:45:52 -0800206 public void onAction(String text, String extra, String source);
Michael Kolb21ce4d22010-09-15 14:55:05 -0700207
Michael Kolb513286f2010-09-09 12:55:12 -0700208 public void onEdit(String text);
Michael Kolbfe251992010-07-08 15:41:55 -0700209
210 }
211
John Reckad373302010-12-17 15:28:13 -0800212 @Override
213 public void onItemClick(
214 AdapterView<?> parent, View view, int position, long id) {
215 SuggestItem item = mAdapter.getItem(position);
216 onSelect((TextUtils.isEmpty(item.url) ? item.title : item.url),
217 item.extra);
218 }
219
John Reck1605bef2011-01-10 18:11:18 -0800220 public void setReverseResults(boolean reverse) {
221 mAdapter.setReverseResults(reverse);
222 }
223
Michael Kolbfe251992010-07-08 15:41:55 -0700224}