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