Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.browser; |
| 18 | |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 19 | import com.android.browser.SuggestionsAdapter.CompletionListener; |
John Reck | ad37330 | 2010-12-17 15:28:13 -0800 | [diff] [blame] | 20 | import com.android.browser.SuggestionsAdapter.SuggestItem; |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 21 | |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 22 | import android.content.Context; |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 23 | import android.content.res.Configuration; |
John Reck | dcda1d5 | 2010-11-29 10:05:54 -0800 | [diff] [blame] | 24 | import android.text.TextUtils; |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 25 | import android.util.AttributeSet; |
| 26 | import android.view.KeyEvent; |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 27 | import android.view.View; |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 28 | import android.view.View.OnFocusChangeListener; |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 29 | import android.view.inputmethod.InputMethodManager; |
John Reck | ad37330 | 2010-12-17 15:28:13 -0800 | [diff] [blame] | 30 | import android.widget.AdapterView; |
| 31 | import android.widget.AdapterView.OnItemClickListener; |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 32 | import android.widget.AutoCompleteTextView; |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 33 | import android.widget.TextView; |
Michael Kolb | ed21774 | 2010-08-10 17:52:34 -0700 | [diff] [blame] | 34 | import android.widget.TextView.OnEditorActionListener; |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 35 | |
Michael Kolb | cfa3af5 | 2010-12-14 10:36:11 -0800 | [diff] [blame] | 36 | import java.util.List; |
| 37 | |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 38 | /** |
| 39 | * url/search input view |
| 40 | * handling suggestions |
| 41 | */ |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 42 | public class UrlInputView extends AutoCompleteTextView |
Michael Kolb | ba99c5d | 2010-11-29 14:57:41 -0800 | [diff] [blame] | 43 | implements OnFocusChangeListener, OnEditorActionListener, |
John Reck | ad37330 | 2010-12-17 15:28:13 -0800 | [diff] [blame] | 44 | CompletionListener, OnItemClickListener { |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 45 | |
Michael Kolb | 257cc2c | 2010-12-09 09:45:52 -0800 | [diff] [blame] | 46 | |
| 47 | static final String TYPED = "browser-type"; |
| 48 | static final String SUGGESTED = "browser-suggest"; |
| 49 | |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 50 | private UrlInputListener mListener; |
| 51 | private InputMethodManager mInputManager; |
| 52 | private SuggestionsAdapter mAdapter; |
Michael Kolb | c7485ae | 2010-09-03 10:10:58 -0700 | [diff] [blame] | 53 | private OnFocusChangeListener mWrappedFocusListener; |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 54 | private View mContainer; |
| 55 | private boolean mLandscape; |
Michael Kolb | cfa3af5 | 2010-12-14 10:36:11 -0800 | [diff] [blame] | 56 | private boolean mInVoiceMode; |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 57 | |
| 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 Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 74 | mInputManager = (InputMethodManager) ctx.getSystemService(Context.INPUT_METHOD_SERVICE); |
Michael Kolb | ed21774 | 2010-08-10 17:52:34 -0700 | [diff] [blame] | 75 | setOnEditorActionListener(this); |
Michael Kolb | c7485ae | 2010-09-03 10:10:58 -0700 | [diff] [blame] | 76 | super.setOnFocusChangeListener(this); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 77 | mAdapter = new SuggestionsAdapter(ctx, this); |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 78 | setAdapter(mAdapter); |
Michael Kolb | ba99c5d | 2010-11-29 14:57:41 -0800 | [diff] [blame] | 79 | setSelectAllOnFocus(true); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 80 | onConfigurationChanged(ctx.getResources().getConfiguration()); |
John Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 81 | setThreshold(1); |
John Reck | ad37330 | 2010-12-17 15:28:13 -0800 | [diff] [blame] | 82 | setOnItemClickListener(this); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Michael Kolb | ba99c5d | 2010-11-29 14:57:41 -0800 | [diff] [blame] | 85 | void setController(UiController controller) { |
| 86 | UrlSelectionActionMode urlSelectionMode |
| 87 | = new UrlSelectionActionMode(controller); |
| 88 | setCustomSelectionActionModeCallback(urlSelectionMode); |
| 89 | } |
| 90 | |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 91 | void setContainer(View container) { |
| 92 | mContainer = container; |
| 93 | } |
| 94 | |
Michael Kolb | cfa3af5 | 2010-12-14 10:36:11 -0800 | [diff] [blame] | 95 | void setVoiceResults(List<String> voiceResults) { |
| 96 | mAdapter.setVoiceResults(voiceResults); |
| 97 | mInVoiceMode = (voiceResults != null); |
| 98 | } |
| 99 | |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 100 | @Override |
| 101 | protected void onConfigurationChanged(Configuration config) { |
John Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 102 | super.onConfigurationChanged(config); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 103 | mLandscape = (config.orientation & |
Michael Kolb | 31d469b | 2010-12-09 20:49:54 -0800 | [diff] [blame] | 104 | Configuration.ORIENTATION_LANDSCAPE) != 0; |
John Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 105 | mAdapter.setLandscapeMode(mLandscape); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 106 | if (isPopupShowing() && (getVisibility() == View.VISIBLE)) { |
John Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 107 | setupDropDown(); |
John Reck | ad37330 | 2010-12-17 15:28:13 -0800 | [diff] [blame] | 108 | performFiltering(getText(), 0); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 109 | } |
| 110 | } |
| 111 | |
| 112 | @Override |
| 113 | public void showDropDown() { |
John Reck | 35defff | 2010-11-11 14:06:45 -0800 | [diff] [blame] | 114 | 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 Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 125 | int width = mContainer.getWidth(); |
Michael Kolb | c5998c2 | 2010-10-10 16:04:35 -0700 | [diff] [blame] | 126 | if (width != getDropDownWidth()) { |
| 127 | setDropDownWidth(width); |
| 128 | } |
| 129 | if (getLeft() != -getDropDownHorizontalOffset()) { |
| 130 | setDropDownHorizontalOffset(-getLeft()); |
| 131 | } |
Michael Kolb | 31d469b | 2010-12-09 20:49:54 -0800 | [diff] [blame] | 132 | setDropDownVerticalOffset(8); |
Michael Kolb | 513286f | 2010-09-09 12:55:12 -0700 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | @Override |
Michael Kolb | c7485ae | 2010-09-03 10:10:58 -0700 | [diff] [blame] | 136 | public void setOnFocusChangeListener(OnFocusChangeListener focusListener) { |
| 137 | mWrappedFocusListener = focusListener; |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 138 | } |
| 139 | |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 140 | @Override |
Michael Kolb | ed21774 | 2010-08-10 17:52:34 -0700 | [diff] [blame] | 141 | public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { |
Michael Kolb | 257cc2c | 2010-12-09 09:45:52 -0800 | [diff] [blame] | 142 | finishInput(getText().toString(), null, TYPED); |
Michael Kolb | ed21774 | 2010-08-10 17:52:34 -0700 | [diff] [blame] | 143 | return true; |
| 144 | } |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 145 | |
Michael Kolb | c7485ae | 2010-09-03 10:10:58 -0700 | [diff] [blame] | 146 | @Override |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 147 | public void onFocusChange(View v, boolean hasFocus) { |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 148 | if (hasFocus) { |
| 149 | forceIme(); |
Michael Kolb | cfa3af5 | 2010-12-14 10:36:11 -0800 | [diff] [blame] | 150 | if (mInVoiceMode) { |
| 151 | performFiltering(getText().toString(), 0); |
| 152 | showDropDown(); |
| 153 | } |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 154 | } else { |
Michael Kolb | 257cc2c | 2010-12-09 09:45:52 -0800 | [diff] [blame] | 155 | finishInput(null, null, null); |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 156 | } |
Michael Kolb | c7485ae | 2010-09-03 10:10:58 -0700 | [diff] [blame] | 157 | if (mWrappedFocusListener != null) { |
| 158 | mWrappedFocusListener.onFocusChange(v, hasFocus); |
| 159 | } |
Michael Kolb | a2b2ba8 | 2010-08-04 17:54:03 -0700 | [diff] [blame] | 160 | } |
| 161 | |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 162 | public void setUrlInputListener(UrlInputListener listener) { |
| 163 | mListener = listener; |
| 164 | } |
| 165 | |
| 166 | public void forceIme() { |
| 167 | mInputManager.showSoftInput(this, 0); |
| 168 | } |
| 169 | |
Michael Kolb | 257cc2c | 2010-12-09 09:45:52 -0800 | [diff] [blame] | 170 | private void finishInput(String url, String extra, String source) { |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 171 | this.dismissDropDown(); |
| 172 | mInputManager.hideSoftInputFromWindow(getWindowToken(), 0); |
John Reck | dcda1d5 | 2010-11-29 10:05:54 -0800 | [diff] [blame] | 173 | if (TextUtils.isEmpty(url)) { |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 174 | mListener.onDismiss(); |
| 175 | } else { |
Michael Kolb | 257cc2c | 2010-12-09 09:45:52 -0800 | [diff] [blame] | 176 | mListener.onAction(url, extra, source); |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 177 | } |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 178 | } |
| 179 | |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 180 | // Completion Listener |
| 181 | |
| 182 | @Override |
| 183 | public void onSearch(String search) { |
| 184 | mListener.onEdit(search); |
| 185 | } |
| 186 | |
| 187 | @Override |
John Reck | 40f720e | 2010-11-10 11:57:04 -0800 | [diff] [blame] | 188 | public void onSelect(String url, String extra) { |
Michael Kolb | 257cc2c | 2010-12-09 09:45:52 -0800 | [diff] [blame] | 189 | finishInput(url, extra, SUGGESTED); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 190 | } |
| 191 | |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 192 | @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 Kolb | 257cc2c | 2010-12-09 09:45:52 -0800 | [diff] [blame] | 196 | finishInput(null, null, null); |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 197 | return true; |
| 198 | } |
| 199 | return super.onKeyPreIme(keyCode, evt); |
| 200 | } |
| 201 | |
| 202 | interface UrlInputListener { |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 203 | |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 204 | public void onDismiss(); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 205 | |
Michael Kolb | 257cc2c | 2010-12-09 09:45:52 -0800 | [diff] [blame] | 206 | public void onAction(String text, String extra, String source); |
Michael Kolb | 21ce4d2 | 2010-09-15 14:55:05 -0700 | [diff] [blame] | 207 | |
Michael Kolb | 513286f | 2010-09-09 12:55:12 -0700 | [diff] [blame] | 208 | public void onEdit(String text); |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 209 | |
| 210 | } |
| 211 | |
John Reck | ad37330 | 2010-12-17 15:28:13 -0800 | [diff] [blame] | 212 | @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 Reck | 1605bef | 2011-01-10 18:11:18 -0800 | [diff] [blame] | 220 | public void setReverseResults(boolean reverse) { |
| 221 | mAdapter.setReverseResults(reverse); |
| 222 | } |
| 223 | |
Michael Kolb | fe25199 | 2010-07-08 15:41:55 -0700 | [diff] [blame] | 224 | } |