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