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