The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 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 android.webkit; |
| 18 | |
Leon Scroggins | 241403f | 2009-09-16 18:26:17 -0400 | [diff] [blame] | 19 | import com.android.internal.widget.EditableInputConnection; |
| 20 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 21 | import android.content.Context; |
Leon Scroggins | 0857767 | 2009-07-16 13:55:35 -0400 | [diff] [blame] | 22 | import android.graphics.Canvas; |
| 23 | import android.graphics.Color; |
| 24 | import android.graphics.ColorFilter; |
| 25 | import android.graphics.Paint; |
| 26 | import android.graphics.PixelFormat; |
| 27 | import android.graphics.Rect; |
| 28 | import android.graphics.drawable.Drawable; |
Leon Scroggins | d69b701 | 2011-03-09 16:18:28 -0500 | [diff] [blame] | 29 | import android.os.Bundle; |
Ben Murdoch | 5791438 | 2010-11-16 11:50:39 +0000 | [diff] [blame] | 30 | import android.os.Handler; |
| 31 | import android.os.Message; |
Leon Scroggins | d69b701 | 2011-03-09 16:18:28 -0500 | [diff] [blame] | 32 | import android.os.ResultReceiver; |
Leon Scroggins | 1ca5626 | 2010-11-18 14:03:03 -0500 | [diff] [blame] | 33 | import android.text.BoringLayout.Metrics; |
| 34 | import android.text.DynamicLayout; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | import android.text.Editable; |
| 36 | import android.text.InputFilter; |
Leon Scroggins | eb51f3e | 2010-05-25 11:13:47 -0400 | [diff] [blame] | 37 | import android.text.Layout; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 38 | import android.text.Selection; |
| 39 | import android.text.Spannable; |
Leon Scroggins | 0857767 | 2009-07-16 13:55:35 -0400 | [diff] [blame] | 40 | import android.text.TextPaint; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 41 | import android.text.TextUtils; |
| 42 | import android.text.method.MovementMethod; |
Leon Scroggins | 0f5ad84 | 2009-07-17 15:08:34 -0400 | [diff] [blame] | 43 | import android.text.method.Touch; |
Cary Clark | 243ea06 | 2009-06-25 10:49:32 -0400 | [diff] [blame] | 44 | import android.util.Log; |
Leon Scroggins | c35f4ac | 2010-11-10 08:59:05 -0500 | [diff] [blame] | 45 | import android.util.TypedValue; |
Leon Scroggins | 0857767 | 2009-07-16 13:55:35 -0400 | [diff] [blame] | 46 | import android.view.Gravity; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 47 | import android.view.KeyCharacterMap; |
| 48 | import android.view.KeyEvent; |
| 49 | import android.view.MotionEvent; |
| 50 | import android.view.View; |
Leon Scroggins | 7ae3419 | 2009-09-16 14:12:52 -0400 | [diff] [blame] | 51 | import android.view.ViewConfiguration; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 52 | import android.view.ViewGroup; |
Leon Scroggins | 0ca7088 | 2009-06-26 17:45:29 -0400 | [diff] [blame] | 53 | import android.view.inputmethod.EditorInfo; |
| 54 | import android.view.inputmethod.InputMethodManager; |
| 55 | import android.view.inputmethod.InputConnection; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 56 | import android.widget.AbsoluteLayout.LayoutParams; |
Ben Murdoch | 62275a4 | 2010-09-07 11:27:28 +0100 | [diff] [blame] | 57 | import android.widget.AdapterView; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 58 | import android.widget.ArrayAdapter; |
| 59 | import android.widget.AutoCompleteTextView; |
| 60 | import android.widget.TextView; |
| 61 | |
| 62 | import java.util.ArrayList; |
| 63 | |
Leon Scroggins | c35f4ac | 2010-11-10 08:59:05 -0500 | [diff] [blame] | 64 | import junit.framework.Assert; |
| 65 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 66 | /** |
Leon Scroggins | d3465f6 | 2009-06-02 10:57:54 -0400 | [diff] [blame] | 67 | * WebTextView is a specialized version of EditText used by WebView |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 68 | * to overlay html textfields (and textareas) to use our standard |
| 69 | * text editing. |
| 70 | */ |
Leon Scroggins | 03e0119 | 2011-02-11 10:19:22 -0500 | [diff] [blame] | 71 | /* package */ class WebTextView extends AutoCompleteTextView |
| 72 | implements AdapterView.OnItemClickListener { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 73 | |
Cary Clark | 243ea06 | 2009-06-25 10:49:32 -0400 | [diff] [blame] | 74 | static final String LOGTAG = "webtextview"; |
| 75 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 76 | private WebView mWebView; |
| 77 | private boolean mSingle; |
| 78 | private int mWidthSpec; |
| 79 | private int mHeightSpec; |
| 80 | private int mNodePointer; |
| 81 | // FIXME: This is a hack for blocking unmatched key ups, in particular |
| 82 | // on the enter key. The method for blocking unmatched key ups prevents |
| 83 | // the shift key from working properly. |
| 84 | private boolean mGotEnterDown; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 85 | private int mMaxLength; |
| 86 | // Keep track of the text before the change so we know whether we actually |
| 87 | // need to send down the DOM events. |
| 88 | private String mPreChange; |
Leon Scroggins | 0857767 | 2009-07-16 13:55:35 -0400 | [diff] [blame] | 89 | private Drawable mBackground; |
Leon Scroggins | 0f5ad84 | 2009-07-17 15:08:34 -0400 | [diff] [blame] | 90 | // Variables for keeping track of the touch down, to send to the WebView |
| 91 | // when a drag starts |
| 92 | private float mDragStartX; |
| 93 | private float mDragStartY; |
| 94 | private long mDragStartTime; |
| 95 | private boolean mDragSent; |
Leon Scroggins | 72543e1 | 2009-07-23 15:29:45 -0400 | [diff] [blame] | 96 | // True if the most recent drag event has caused either the TextView to |
| 97 | // scroll or the web page to scroll. Gets reset after a touch down. |
| 98 | private boolean mScrolled; |
Leon Scroggins | 6679f2f | 2009-08-12 18:48:10 -0400 | [diff] [blame] | 99 | // Whether or not a selection change was generated from webkit. If it was, |
| 100 | // we do not need to pass the selection back to webkit. |
| 101 | private boolean mFromWebKit; |
Leon Scroggins | bb107bd | 2009-10-23 16:18:42 -0400 | [diff] [blame] | 102 | // Whether or not a selection change was generated from the WebTextView |
| 103 | // gaining focus. If it is, we do not want to pass it to webkit. This |
| 104 | // selection comes from the MovementMethod, but we behave differently. If |
| 105 | // WebTextView gained focus from a touch, webkit will determine the |
| 106 | // selection. |
| 107 | private boolean mFromFocusChange; |
| 108 | // Whether or not a selection change was generated from setInputType. We |
| 109 | // do not want to pass this change to webkit. |
| 110 | private boolean mFromSetInputType; |
Leon Scroggins | 6679f2f | 2009-08-12 18:48:10 -0400 | [diff] [blame] | 111 | private boolean mGotTouchDown; |
Leon Scroggins | ed90811 | 2009-12-01 10:45:24 -0500 | [diff] [blame] | 112 | // Keep track of whether a long press has happened. Only meaningful after |
| 113 | // an ACTION_DOWN MotionEvent |
| 114 | private boolean mHasPerformedLongClick; |
Cary Clark | 0c5924e | 2009-09-30 10:24:19 -0400 | [diff] [blame] | 115 | private boolean mInSetTextAndKeepSelection; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 116 | // Array to store the final character added in onTextChanged, so that its |
| 117 | // KeyEvents may be determined. |
| 118 | private char[] mCharacter = new char[1]; |
| 119 | // This is used to reset the length filter when on a textfield |
| 120 | // with no max length. |
| 121 | // FIXME: This can be replaced with TextView.NO_FILTERS if that |
| 122 | // is made public/protected. |
| 123 | private static final InputFilter[] NO_FILTERS = new InputFilter[0]; |
Leon Scroggins III | e742c08 | 2010-09-02 15:48:55 -0400 | [diff] [blame] | 124 | // For keeping track of the fact that the delete key was pressed, so |
| 125 | // we can simply pass a delete key instead of calling deleteSelection. |
| 126 | private boolean mGotDelete; |
| 127 | private int mDelSelStart; |
| 128 | private int mDelSelEnd; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 129 | |
Ben Murdoch | 62275a4 | 2010-09-07 11:27:28 +0100 | [diff] [blame] | 130 | // Keep in sync with native constant in |
| 131 | // external/webkit/WebKit/android/WebCoreSupport/autofill/WebAutoFill.cpp |
| 132 | /* package */ static final int FORM_NOT_AUTOFILLABLE = -1; |
| 133 | |
| 134 | private boolean mAutoFillable; // Is this textview part of an autofillable form? |
| 135 | private int mQueryId; |
Ben Murdoch | 5791438 | 2010-11-16 11:50:39 +0000 | [diff] [blame] | 136 | private boolean mAutoFillProfileIsSet; |
Leon Scroggins | 4077723 | 2011-01-18 16:49:20 -0500 | [diff] [blame] | 137 | // Used to determine whether onFocusChanged was called as a result of |
| 138 | // calling remove(). |
| 139 | private boolean mInsideRemove; |
Leon Scroggins | d69b701 | 2011-03-09 16:18:28 -0500 | [diff] [blame] | 140 | private class MyResultReceiver extends ResultReceiver { |
| 141 | @Override |
| 142 | protected void onReceiveResult(int resultCode, Bundle resultData) { |
| 143 | if (resultCode == InputMethodManager.RESULT_SHOWN |
| 144 | && mWebView != null) { |
| 145 | mWebView.revealSelection(); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * @param handler |
| 151 | */ |
| 152 | public MyResultReceiver(Handler handler) { |
| 153 | super(handler); |
| 154 | } |
| 155 | } |
| 156 | private MyResultReceiver mReceiver; |
Ben Murdoch | 62275a4 | 2010-09-07 11:27:28 +0100 | [diff] [blame] | 157 | |
Leon Scroggins | b5ce0e0 | 2010-11-01 13:20:24 -0400 | [diff] [blame] | 158 | // Types used with setType. Keep in sync with CachedInput.h |
| 159 | private static final int NORMAL_TEXT_FIELD = 0; |
| 160 | private static final int TEXT_AREA = 1; |
| 161 | private static final int PASSWORD = 2; |
| 162 | private static final int SEARCH = 3; |
| 163 | private static final int EMAIL = 4; |
| 164 | private static final int NUMBER = 5; |
| 165 | private static final int TELEPHONE = 6; |
| 166 | private static final int URL = 7; |
| 167 | |
Ben Murdoch | 5791438 | 2010-11-16 11:50:39 +0000 | [diff] [blame] | 168 | private static final int AUTOFILL_FORM = 100; |
| 169 | private Handler mHandler; |
| 170 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 171 | /** |
Leon Scroggins | d3465f6 | 2009-06-02 10:57:54 -0400 | [diff] [blame] | 172 | * Create a new WebTextView. |
| 173 | * @param context The Context for this WebTextView. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 174 | * @param webView The WebView that created this. |
| 175 | */ |
Ben Murdoch | 62275a4 | 2010-09-07 11:27:28 +0100 | [diff] [blame] | 176 | /* package */ WebTextView(Context context, WebView webView, int autoFillQueryId) { |
Leon Scroggins | e265532 | 2010-02-16 16:09:59 -0500 | [diff] [blame] | 177 | super(context, null, com.android.internal.R.attr.webTextViewStyle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 178 | mWebView = webView; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 179 | mMaxLength = -1; |
Ben Murdoch | 62275a4 | 2010-09-07 11:27:28 +0100 | [diff] [blame] | 180 | setAutoFillable(autoFillQueryId); |
Leon Scroggins | c35f4ac | 2010-11-10 08:59:05 -0500 | [diff] [blame] | 181 | // Turn on subpixel text, and turn off kerning, so it better matches |
| 182 | // the text in webkit. |
| 183 | TextPaint paint = getPaint(); |
| 184 | int flags = paint.getFlags() & ~Paint.DEV_KERN_TEXT_FLAG |
| 185 | | Paint.SUBPIXEL_TEXT_FLAG | Paint.DITHER_FLAG; |
| 186 | paint.setFlags(flags); |
| 187 | |
| 188 | // Set the text color to black, regardless of the theme. This ensures |
| 189 | // that other applications that use embedded WebViews will properly |
| 190 | // display the text in password textfields. |
Leon Scroggins | c26dc9f | 2010-11-12 12:01:34 -0500 | [diff] [blame] | 191 | setTextColor(DebugFlags.DRAW_WEBTEXTVIEW ? Color.RED : Color.BLACK); |
Leon Scroggins | c35f4ac | 2010-11-10 08:59:05 -0500 | [diff] [blame] | 192 | // This helps to align the text better with the text in the web page. |
| 193 | setIncludeFontPadding(false); |
Ben Murdoch | 5791438 | 2010-11-16 11:50:39 +0000 | [diff] [blame] | 194 | |
| 195 | mHandler = new Handler() { |
| 196 | @Override |
| 197 | public void handleMessage(Message msg) { |
| 198 | switch (msg.what) { |
| 199 | case AUTOFILL_FORM: |
| 200 | mWebView.autoFillForm(mQueryId); |
| 201 | break; |
| 202 | } |
| 203 | } |
| 204 | }; |
Leon Scroggins | d69b701 | 2011-03-09 16:18:28 -0500 | [diff] [blame] | 205 | mReceiver = new MyResultReceiver(mHandler); |
Ben Murdoch | 62275a4 | 2010-09-07 11:27:28 +0100 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | public void setAutoFillable(int queryId) { |
Ben Murdoch | f39b2cf | 2010-09-09 10:26:46 +0100 | [diff] [blame] | 209 | mAutoFillable = mWebView.getSettings().getAutoFillEnabled() |
| 210 | && (queryId != FORM_NOT_AUTOFILLABLE); |
Ben Murdoch | 62275a4 | 2010-09-07 11:27:28 +0100 | [diff] [blame] | 211 | mQueryId = queryId; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | @Override |
| 215 | public boolean dispatchKeyEvent(KeyEvent event) { |
| 216 | if (event.isSystem()) { |
| 217 | return super.dispatchKeyEvent(event); |
| 218 | } |
| 219 | // Treat ACTION_DOWN and ACTION MULTIPLE the same |
| 220 | boolean down = event.getAction() != KeyEvent.ACTION_UP; |
| 221 | int keyCode = event.getKeyCode(); |
Leon Scroggins | 4890feb | 2009-07-02 10:37:10 -0400 | [diff] [blame] | 222 | |
| 223 | boolean isArrowKey = false; |
| 224 | switch(keyCode) { |
| 225 | case KeyEvent.KEYCODE_DPAD_LEFT: |
| 226 | case KeyEvent.KEYCODE_DPAD_RIGHT: |
| 227 | case KeyEvent.KEYCODE_DPAD_UP: |
| 228 | case KeyEvent.KEYCODE_DPAD_DOWN: |
Leon Scroggins | 4890feb | 2009-07-02 10:37:10 -0400 | [diff] [blame] | 229 | isArrowKey = true; |
| 230 | break; |
| 231 | } |
Leon Scroggins | bb107bd | 2009-10-23 16:18:42 -0400 | [diff] [blame] | 232 | |
Leon Scroggins | d5ff5db | 2010-02-08 13:07:41 -0500 | [diff] [blame] | 233 | if (KeyEvent.KEYCODE_TAB == keyCode) { |
| 234 | if (down) { |
| 235 | onEditorAction(EditorInfo.IME_ACTION_NEXT); |
| 236 | } |
| 237 | return true; |
| 238 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 239 | Spannable text = (Spannable) getText(); |
Leon Scroggins III | 3d54084 | 2010-08-27 14:44:37 -0400 | [diff] [blame] | 240 | int oldStart = Selection.getSelectionStart(text); |
| 241 | int oldEnd = Selection.getSelectionEnd(text); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 242 | // Normally the delete key's dom events are sent via onTextChanged. |
Leon Scroggins III | 3d54084 | 2010-08-27 14:44:37 -0400 | [diff] [blame] | 243 | // However, if the cursor is at the beginning of the field, which |
| 244 | // includes the case where it has zero length, then the text is not |
| 245 | // changed, so send the events immediately. |
Leon Scroggins III | e742c08 | 2010-09-02 15:48:55 -0400 | [diff] [blame] | 246 | if (KeyEvent.KEYCODE_DEL == keyCode) { |
| 247 | if (oldStart == 0 && oldEnd == 0) { |
| 248 | sendDomEvent(event); |
| 249 | return true; |
| 250 | } |
| 251 | if (down) { |
| 252 | mGotDelete = true; |
| 253 | mDelSelStart = oldStart; |
| 254 | mDelSelEnd = oldEnd; |
| 255 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 256 | } |
| 257 | |
Cary Clark | aa86ac8 | 2010-12-28 11:30:18 -0500 | [diff] [blame] | 258 | if (mSingle && (KeyEvent.KEYCODE_ENTER == keyCode |
| 259 | || KeyEvent.KEYCODE_NUMPAD_ENTER == keyCode)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 260 | if (isPopupShowing()) { |
| 261 | return super.dispatchKeyEvent(event); |
| 262 | } |
| 263 | if (!down) { |
| 264 | // Hide the keyboard, since the user has just submitted this |
| 265 | // form. The submission happens thanks to the two calls |
| 266 | // to sendDomEvent. |
| 267 | InputMethodManager.getInstance(mContext) |
| 268 | .hideSoftInputFromWindow(getWindowToken(), 0); |
| 269 | sendDomEvent(new KeyEvent(KeyEvent.ACTION_DOWN, keyCode)); |
| 270 | sendDomEvent(event); |
| 271 | } |
| 272 | return super.dispatchKeyEvent(event); |
| 273 | } else if (KeyEvent.KEYCODE_DPAD_CENTER == keyCode) { |
| 274 | // Note that this handles center key and trackball. |
| 275 | if (isPopupShowing()) { |
| 276 | return super.dispatchKeyEvent(event); |
| 277 | } |
| 278 | // Center key should be passed to a potential onClick |
| 279 | if (!down) { |
Leon Scroggins | 1d96ca0 | 2009-10-23 11:49:03 -0400 | [diff] [blame] | 280 | mWebView.centerKeyPressOnTextField(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 281 | } |
| 282 | // Pass to super to handle longpress. |
| 283 | return super.dispatchKeyEvent(event); |
| 284 | } |
| 285 | |
| 286 | // Ensure there is a layout so arrow keys are handled properly. |
| 287 | if (getLayout() == null) { |
| 288 | measure(mWidthSpec, mHeightSpec); |
| 289 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 290 | |
Leon Scroggins III | 3d54084 | 2010-08-27 14:44:37 -0400 | [diff] [blame] | 291 | int oldLength = text.length(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 292 | boolean maxedOut = mMaxLength != -1 && oldLength == mMaxLength; |
| 293 | // If we are at max length, and there is a selection rather than a |
| 294 | // cursor, we need to store the text to compare later, since the key |
| 295 | // may have changed the string. |
| 296 | String oldText; |
| 297 | if (maxedOut && oldEnd != oldStart) { |
| 298 | oldText = text.toString(); |
| 299 | } else { |
| 300 | oldText = ""; |
| 301 | } |
| 302 | if (super.dispatchKeyEvent(event)) { |
Leon Scroggins | d3465f6 | 2009-06-02 10:57:54 -0400 | [diff] [blame] | 303 | // If the WebTextView handled the key it was either an alphanumeric |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 304 | // key, a delete, or a movement within the text. All of those are |
| 305 | // ok to pass to javascript. |
| 306 | |
| 307 | // UNLESS there is a max length determined by the html. In that |
| 308 | // case, if the string was already at the max length, an |
| 309 | // alphanumeric key will be erased by the LengthFilter, |
| 310 | // so do not pass down to javascript, and instead |
| 311 | // return true. If it is an arrow key or a delete key, we can go |
| 312 | // ahead and pass it down. |
Cary Clark | aa86ac8 | 2010-12-28 11:30:18 -0500 | [diff] [blame] | 313 | if (KeyEvent.KEYCODE_ENTER == keyCode |
| 314 | || KeyEvent.KEYCODE_NUMPAD_ENTER == keyCode) { |
Cary Clark | d6982c9 | 2009-05-29 11:02:22 -0400 | [diff] [blame] | 315 | // For multi-line text boxes, newlines will |
| 316 | // trigger onTextChanged for key down (which will send both |
| 317 | // key up and key down) but not key up. |
| 318 | mGotEnterDown = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 319 | } |
| 320 | if (maxedOut && !isArrowKey && keyCode != KeyEvent.KEYCODE_DEL) { |
| 321 | if (oldEnd == oldStart) { |
| 322 | // Return true so the key gets dropped. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 323 | return true; |
| 324 | } else if (!oldText.equals(getText().toString())) { |
| 325 | // FIXME: This makes the text work properly, but it |
| 326 | // does not pass down the key event, so it may not |
| 327 | // work for a textfield that has the type of |
| 328 | // behavior of GoogleSuggest. That said, it is |
| 329 | // unlikely that a site would combine the two in |
| 330 | // one textfield. |
| 331 | Spannable span = (Spannable) getText(); |
| 332 | int newStart = Selection.getSelectionStart(span); |
| 333 | int newEnd = Selection.getSelectionEnd(span); |
| 334 | mWebView.replaceTextfieldText(0, oldLength, span.toString(), |
| 335 | newStart, newEnd); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 336 | return true; |
| 337 | } |
| 338 | } |
Leon Scroggins | 17c3bfc | 2009-04-14 10:57:13 -0700 | [diff] [blame] | 339 | /* FIXME: |
| 340 | * In theory, we would like to send the events for the arrow keys. |
| 341 | * However, the TextView can arbitrarily change the selection (i.e. |
| 342 | * long press followed by using the trackball). Therefore, we keep |
| 343 | * in sync with the TextView via onSelectionChanged. If we also |
| 344 | * send the DOM event, we lose the correct selection. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 345 | if (isArrowKey) { |
| 346 | // Arrow key does not change the text, but we still want to send |
| 347 | // the DOM events. |
| 348 | sendDomEvent(event); |
| 349 | } |
Leon Scroggins | 17c3bfc | 2009-04-14 10:57:13 -0700 | [diff] [blame] | 350 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 351 | return true; |
| 352 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 353 | // Ignore the key up event for newlines. This prevents |
| 354 | // multiple newlines in the native textarea. |
| 355 | if (mGotEnterDown && !down) { |
| 356 | return true; |
| 357 | } |
| 358 | // if it is a navigation key, pass it to WebView |
Cary Clark | d6982c9 | 2009-05-29 11:02:22 -0400 | [diff] [blame] | 359 | if (isArrowKey) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 360 | // WebView check the trackballtime in onKeyDown to avoid calling |
Leon Scroggins | d3465f6 | 2009-06-02 10:57:54 -0400 | [diff] [blame] | 361 | // native from both trackball and key handling. As this is called |
| 362 | // from WebTextView, we always want WebView to check with native. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 363 | // Reset trackballtime to ensure it. |
| 364 | mWebView.resetTrackballTime(); |
| 365 | return down ? mWebView.onKeyDown(keyCode, event) : mWebView |
| 366 | .onKeyUp(keyCode, event); |
| 367 | } |
| 368 | return false; |
| 369 | } |
| 370 | |
Leon Scroggins | fcf5776 | 2010-12-14 18:09:23 -0500 | [diff] [blame] | 371 | void ensureLayout() { |
| 372 | if (getLayout() == null) { |
| 373 | // Ensure we have a Layout |
| 374 | measure(mWidthSpec, mHeightSpec); |
| 375 | LayoutParams params = (LayoutParams) getLayoutParams(); |
| 376 | if (params != null) { |
| 377 | layout(params.x, params.y, params.x + params.width, |
| 378 | params.y + params.height); |
| 379 | } |
| 380 | } |
| 381 | } |
| 382 | |
Leon Scroggins | d69b701 | 2011-03-09 16:18:28 -0500 | [diff] [blame] | 383 | /* package */ ResultReceiver getResultReceiver() { return mReceiver; } |
| 384 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 385 | /** |
Leon Scroggins | d3465f6 | 2009-06-02 10:57:54 -0400 | [diff] [blame] | 386 | * Determine whether this WebTextView currently represents the node |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 387 | * represented by ptr. |
| 388 | * @param ptr Pointer to a node to compare to. |
Leon Scroggins | d3465f6 | 2009-06-02 10:57:54 -0400 | [diff] [blame] | 389 | * @return boolean Whether this WebTextView already represents the node |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 390 | * pointed to by ptr. |
| 391 | */ |
| 392 | /* package */ boolean isSameTextField(int ptr) { |
| 393 | return ptr == mNodePointer; |
| 394 | } |
| 395 | |
Leon Scroggins | 2a30744 | 2010-10-15 14:35:21 -0400 | [diff] [blame] | 396 | /** |
Leon Scroggins | d518865 | 2011-01-19 17:01:55 -0500 | [diff] [blame] | 397 | * Ensure that the underlying text field/area is lined up with the WebTextView. |
Leon Scroggins | 2a30744 | 2010-10-15 14:35:21 -0400 | [diff] [blame] | 398 | */ |
| 399 | private void lineUpScroll() { |
Leon Scroggins | e8aa954 | 2010-10-18 10:09:07 -0400 | [diff] [blame] | 400 | Layout layout = getLayout(); |
| 401 | if (mWebView != null && layout != null) { |
Leon Scroggins | d518865 | 2011-01-19 17:01:55 -0500 | [diff] [blame] | 402 | if (mSingle) { |
| 403 | // textfields only need to be lined up horizontally. |
| 404 | float maxScrollX = layout.getLineRight(0) - getWidth(); |
| 405 | if (DebugFlags.WEB_TEXT_VIEW) { |
| 406 | Log.v(LOGTAG, "onTouchEvent x=" + mScrollX + " y=" |
| 407 | + mScrollY + " maxX=" + maxScrollX); |
| 408 | } |
| 409 | mWebView.scrollFocusedTextInputX(maxScrollX > 0 ? |
| 410 | mScrollX / maxScrollX : 0); |
| 411 | } else { |
| 412 | // textareas only need to be lined up vertically. |
| 413 | mWebView.scrollFocusedTextInputY(mScrollY); |
Leon Scroggins | 2a30744 | 2010-10-15 14:35:21 -0400 | [diff] [blame] | 414 | } |
Leon Scroggins | 2a30744 | 2010-10-15 14:35:21 -0400 | [diff] [blame] | 415 | } |
| 416 | } |
| 417 | |
Leon Scroggins | 1ca5626 | 2010-11-18 14:03:03 -0500 | [diff] [blame] | 418 | @Override |
| 419 | protected void makeNewLayout(int w, int hintWidth, Metrics boring, |
| 420 | Metrics hintBoring, int ellipsisWidth, boolean bringIntoView) { |
| 421 | // Necessary to get a Layout to work with, and to do the other work that |
| 422 | // makeNewLayout does. |
| 423 | super.makeNewLayout(w, hintWidth, boring, hintBoring, ellipsisWidth, |
| 424 | bringIntoView); |
| 425 | |
| 426 | // For fields that do not draw, create a layout which is altered so that |
| 427 | // the text lines up. |
| 428 | if (DebugFlags.DRAW_WEBTEXTVIEW || willNotDraw()) { |
| 429 | float lineHeight = -1; |
| 430 | if (mWebView != null) { |
| 431 | float height = mWebView.nativeFocusCandidateLineHeight(); |
| 432 | if (height != -1) { |
| 433 | lineHeight = height * mWebView.getScale(); |
| 434 | } |
| 435 | } |
| 436 | CharSequence text = getText(); |
| 437 | // Copy from the existing Layout. |
Leon Scroggins | e11ce83 | 2011-03-03 17:36:40 -0500 | [diff] [blame] | 438 | mLayout = new WebTextViewLayout(text, text, getPaint(), mLayout.getWidth(), |
Leon Scroggins | 1ca5626 | 2010-11-18 14:03:03 -0500 | [diff] [blame] | 439 | mLayout.getAlignment(), mLayout.getSpacingMultiplier(), |
| 440 | mLayout.getSpacingAdd(), false, null, ellipsisWidth, |
| 441 | lineHeight); |
| 442 | } |
Leon Scroggins | d518865 | 2011-01-19 17:01:55 -0500 | [diff] [blame] | 443 | lineUpScroll(); |
Leon Scroggins | 1ca5626 | 2010-11-18 14:03:03 -0500 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | /** |
| 447 | * Custom layout which figures out its line spacing. If -1 is passed in for |
| 448 | * the height, it will use the ascent and descent from the paint to |
| 449 | * determine the line spacing. Otherwise it will use the spacing provided. |
| 450 | */ |
| 451 | private static class WebTextViewLayout extends DynamicLayout { |
| 452 | private float mLineHeight; |
| 453 | private float mDifference; |
| 454 | public WebTextViewLayout(CharSequence base, CharSequence display, |
| 455 | TextPaint paint, |
| 456 | int width, Alignment align, |
| 457 | float spacingMult, float spacingAdd, |
| 458 | boolean includepad, |
| 459 | TextUtils.TruncateAt ellipsize, int ellipsizedWidth, |
| 460 | float lineHeight) { |
| 461 | super(base, display, paint, width, align, spacingMult, spacingAdd, |
| 462 | includepad, ellipsize, ellipsizedWidth); |
| 463 | float paintLineHeight = paint.descent() - paint.ascent(); |
| 464 | if (lineHeight == -1f) { |
| 465 | mLineHeight = paintLineHeight; |
| 466 | mDifference = 0f; |
| 467 | } else { |
| 468 | mLineHeight = lineHeight; |
| 469 | // Through trial and error, I found this calculation to improve |
| 470 | // the accuracy of line placement. |
| 471 | mDifference = (lineHeight - paintLineHeight) / 2; |
| 472 | } |
| 473 | } |
| 474 | |
| 475 | @Override |
| 476 | public int getLineTop(int line) { |
| 477 | return Math.round(mLineHeight * line - mDifference); |
| 478 | } |
| 479 | } |
| 480 | |
Leon Scroggins | 0ca7088 | 2009-06-26 17:45:29 -0400 | [diff] [blame] | 481 | @Override public InputConnection onCreateInputConnection( |
| 482 | EditorInfo outAttrs) { |
| 483 | InputConnection connection = super.onCreateInputConnection(outAttrs); |
| 484 | if (mWebView != null) { |
| 485 | // Use the name of the textfield + the url. Use backslash as an |
| 486 | // arbitrary separator. |
| 487 | outAttrs.fieldName = mWebView.nativeFocusCandidateName() + "\\" |
| 488 | + mWebView.getUrl(); |
| 489 | } |
| 490 | return connection; |
| 491 | } |
| 492 | |
Leon Scroggins III | dfbb1f4 | 2010-04-14 15:40:56 -0400 | [diff] [blame] | 493 | /** |
| 494 | * In general, TextView makes a call to InputMethodManager.updateSelection |
| 495 | * in onDraw. However, in the general case of WebTextView, we do not draw. |
| 496 | * This method is called by WebView.onDraw to take care of the part that |
| 497 | * needs to be called. |
| 498 | */ |
| 499 | /* package */ void onDrawSubstitute() { |
| 500 | if (!willNotDraw()) { |
| 501 | // If the WebTextView is set to draw, such as in the case of a |
| 502 | // password, onDraw calls updateSelection(), so this code path is |
| 503 | // unnecessary. |
| 504 | return; |
| 505 | } |
| 506 | // This code is copied from TextView.onDraw(). That code does not get |
| 507 | // executed, however, because the WebTextView does not draw, allowing |
| 508 | // webkit's drawing to show through. |
| 509 | InputMethodManager imm = InputMethodManager.peekInstance(); |
| 510 | if (imm != null && imm.isActive(this)) { |
| 511 | Spannable sp = (Spannable) getText(); |
| 512 | int selStart = Selection.getSelectionStart(sp); |
| 513 | int selEnd = Selection.getSelectionEnd(sp); |
| 514 | int candStart = EditableInputConnection.getComposingSpanStart(sp); |
| 515 | int candEnd = EditableInputConnection.getComposingSpanEnd(sp); |
| 516 | imm.updateSelection(this, selStart, selEnd, candStart, candEnd); |
| 517 | } |
Leon Scroggins | c826a69 | 2010-11-01 18:00:55 -0400 | [diff] [blame] | 518 | updateCursorControllerPositions(); |
Leon Scroggins III | dfbb1f4 | 2010-04-14 15:40:56 -0400 | [diff] [blame] | 519 | } |
| 520 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 521 | @Override |
Leon Scroggins | 69ec5c2 | 2010-04-22 16:27:21 -0400 | [diff] [blame] | 522 | protected void onDraw(Canvas canvas) { |
| 523 | // onDraw should only be called for password fields. If WebTextView is |
| 524 | // still drawing, but is no longer corresponding to a password field, |
| 525 | // remove it. |
Leon Scroggins | c26dc9f | 2010-11-12 12:01:34 -0500 | [diff] [blame] | 526 | if (!DebugFlags.DRAW_WEBTEXTVIEW && (mWebView == null |
| 527 | || !mWebView.nativeFocusCandidateIsPassword() |
| 528 | || !isSameTextField(mWebView.nativeFocusCandidatePointer()))) { |
Leon Scroggins | 69ec5c2 | 2010-04-22 16:27:21 -0400 | [diff] [blame] | 529 | // Although calling remove() would seem to make more sense here, |
| 530 | // changing it to not be a password field will make it not draw. |
| 531 | // Other code will make sure that it is removed completely, but this |
| 532 | // way the user will not see it. |
| 533 | setInPassword(false); |
| 534 | } else { |
| 535 | super.onDraw(canvas); |
| 536 | } |
| 537 | } |
| 538 | |
| 539 | @Override |
Leon Scroggins | 0105828 | 2009-07-30 16:33:56 -0400 | [diff] [blame] | 540 | public void onEditorAction(int actionCode) { |
| 541 | switch (actionCode) { |
| 542 | case EditorInfo.IME_ACTION_NEXT: |
Leon Scroggins | 1be4098 | 2010-03-01 11:20:31 -0500 | [diff] [blame] | 543 | if (mWebView.nativeMoveCursorToNextTextInput()) { |
Leon Scroggins | 1be4098 | 2010-03-01 11:20:31 -0500 | [diff] [blame] | 544 | // Preemptively rebuild the WebTextView, so that the action will |
| 545 | // be set properly. |
| 546 | mWebView.rebuildWebTextView(); |
| 547 | setDefaultSelection(); |
| 548 | mWebView.invalidate(); |
| 549 | } |
Leon Scroggins | 0105828 | 2009-07-30 16:33:56 -0400 | [diff] [blame] | 550 | break; |
| 551 | case EditorInfo.IME_ACTION_DONE: |
| 552 | super.onEditorAction(actionCode); |
| 553 | break; |
| 554 | case EditorInfo.IME_ACTION_GO: |
Leon Scroggins | 9e3e2d3 | 2009-12-04 14:50:18 -0500 | [diff] [blame] | 555 | case EditorInfo.IME_ACTION_SEARCH: |
Leon Scroggins | 0105828 | 2009-07-30 16:33:56 -0400 | [diff] [blame] | 556 | // Send an enter and hide the soft keyboard |
| 557 | InputMethodManager.getInstance(mContext) |
| 558 | .hideSoftInputFromWindow(getWindowToken(), 0); |
| 559 | sendDomEvent(new KeyEvent(KeyEvent.ACTION_DOWN, |
| 560 | KeyEvent.KEYCODE_ENTER)); |
| 561 | sendDomEvent(new KeyEvent(KeyEvent.ACTION_UP, |
| 562 | KeyEvent.KEYCODE_ENTER)); |
| 563 | |
| 564 | default: |
| 565 | break; |
| 566 | } |
| 567 | } |
| 568 | |
| 569 | @Override |
Leon Scroggins | bb107bd | 2009-10-23 16:18:42 -0400 | [diff] [blame] | 570 | protected void onFocusChanged(boolean focused, int direction, |
| 571 | Rect previouslyFocusedRect) { |
| 572 | mFromFocusChange = true; |
| 573 | super.onFocusChanged(focused, direction, previouslyFocusedRect); |
Leon Scroggins | 4077723 | 2011-01-18 16:49:20 -0500 | [diff] [blame] | 574 | if (focused) { |
| 575 | mWebView.setActive(true); |
| 576 | } else if (!mInsideRemove) { |
| 577 | mWebView.setActive(false); |
| 578 | } |
Leon Scroggins | bb107bd | 2009-10-23 16:18:42 -0400 | [diff] [blame] | 579 | mFromFocusChange = false; |
| 580 | } |
| 581 | |
Leon Scroggins | 03e0119 | 2011-02-11 10:19:22 -0500 | [diff] [blame] | 582 | // AdapterView.OnItemClickListener implementation |
| 583 | |
| 584 | @Override |
| 585 | public void onItemClick(AdapterView<?> parent, View view, int position, long id) { |
| 586 | if (id == 0 && position == 0) { |
| 587 | // Blank out the text box while we wait for WebCore to fill the form. |
| 588 | replaceText(""); |
| 589 | WebSettings settings = mWebView.getSettings(); |
| 590 | if (mAutoFillProfileIsSet) { |
| 591 | // Call a webview method to tell WebCore to autofill the form. |
| 592 | mWebView.autoFillForm(mQueryId); |
| 593 | } else { |
| 594 | // There is no autofill profile setup yet and the user has |
| 595 | // elected to try and set one up. Call through to the |
| 596 | // embedder to action that. |
| 597 | mWebView.getWebChromeClient().setupAutoFill( |
| 598 | mHandler.obtainMessage(AUTOFILL_FORM)); |
| 599 | } |
| 600 | } |
| 601 | } |
| 602 | |
Leon Scroggins | bb107bd | 2009-10-23 16:18:42 -0400 | [diff] [blame] | 603 | @Override |
Leon Scroggins | 2a30744 | 2010-10-15 14:35:21 -0400 | [diff] [blame] | 604 | protected void onScrollChanged(int l, int t, int oldl, int oldt) { |
| 605 | super.onScrollChanged(l, t, oldl, oldt); |
| 606 | lineUpScroll(); |
| 607 | } |
| 608 | |
| 609 | @Override |
Leon Scroggins | 17c3bfc | 2009-04-14 10:57:13 -0700 | [diff] [blame] | 610 | protected void onSelectionChanged(int selStart, int selEnd) { |
Leon Scroggins | bb107bd | 2009-10-23 16:18:42 -0400 | [diff] [blame] | 611 | if (!mFromWebKit && !mFromFocusChange && !mFromSetInputType |
Leon Scroggins III | dfbb1f4 | 2010-04-14 15:40:56 -0400 | [diff] [blame] | 612 | && mWebView != null && !mInSetTextAndKeepSelection) { |
Cary Clark | 243ea06 | 2009-06-25 10:49:32 -0400 | [diff] [blame] | 613 | if (DebugFlags.WEB_TEXT_VIEW) { |
| 614 | Log.v(LOGTAG, "onSelectionChanged selStart=" + selStart |
| 615 | + " selEnd=" + selEnd); |
| 616 | } |
Leon Scroggins | 17c3bfc | 2009-04-14 10:57:13 -0700 | [diff] [blame] | 617 | mWebView.setSelection(selStart, selEnd); |
Leon Scroggins | 2a30744 | 2010-10-15 14:35:21 -0400 | [diff] [blame] | 618 | lineUpScroll(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 619 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 620 | } |
Leon Scroggins | 17c3bfc | 2009-04-14 10:57:13 -0700 | [diff] [blame] | 621 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 622 | @Override |
| 623 | protected void onTextChanged(CharSequence s,int start,int before,int count){ |
| 624 | super.onTextChanged(s, start, before, count); |
| 625 | String postChange = s.toString(); |
| 626 | // Prevent calls to setText from invoking onTextChanged (since this will |
| 627 | // mean we are on a different textfield). Also prevent the change when |
Leon Scroggins | d3465f6 | 2009-06-02 10:57:54 -0400 | [diff] [blame] | 628 | // going from a textfield with a string of text to one with a smaller |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 629 | // limit on text length from registering the onTextChanged event. |
| 630 | if (mPreChange == null || mPreChange.equals(postChange) || |
| 631 | (mMaxLength > -1 && mPreChange.length() > mMaxLength && |
| 632 | mPreChange.substring(0, mMaxLength).equals(postChange))) { |
| 633 | return; |
| 634 | } |
| 635 | mPreChange = postChange; |
Leon Scroggins | 62459c0 | 2010-03-09 15:14:03 -0500 | [diff] [blame] | 636 | if (0 == count) { |
| 637 | if (before > 0) { |
Leon Scroggins III | e742c08 | 2010-09-02 15:48:55 -0400 | [diff] [blame] | 638 | // For this and all changes to the text, update our cache |
| 639 | updateCachedTextfield(); |
| 640 | if (mGotDelete) { |
| 641 | mGotDelete = false; |
| 642 | int oldEnd = start + before; |
| 643 | if (mDelSelEnd == oldEnd |
| 644 | && (mDelSelStart == start |
| 645 | || (mDelSelStart == oldEnd && before == 1))) { |
| 646 | // If the selection is set up properly before the |
| 647 | // delete, send the DOM events. |
| 648 | sendDomEvent(new KeyEvent(KeyEvent.ACTION_DOWN, |
| 649 | KeyEvent.KEYCODE_DEL)); |
| 650 | sendDomEvent(new KeyEvent(KeyEvent.ACTION_UP, |
| 651 | KeyEvent.KEYCODE_DEL)); |
| 652 | return; |
| 653 | } |
| 654 | } |
Leon Scroggins | 62459c0 | 2010-03-09 15:14:03 -0500 | [diff] [blame] | 655 | // This was simply a delete or a cut, so just delete the |
| 656 | // selection. |
| 657 | mWebView.deleteSelection(start, start + before); |
Leon Scroggins | 62459c0 | 2010-03-09 15:14:03 -0500 | [diff] [blame] | 658 | } |
Leon Scroggins III | e742c08 | 2010-09-02 15:48:55 -0400 | [diff] [blame] | 659 | mGotDelete = false; |
Leon Scroggins | 62459c0 | 2010-03-09 15:14:03 -0500 | [diff] [blame] | 660 | // before should never be negative, so whether it was a cut |
| 661 | // (handled above), or before is 0, in which case nothing has |
| 662 | // changed, we should return. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 663 | return; |
| 664 | } |
Leon Scroggins III | e742c08 | 2010-09-02 15:48:55 -0400 | [diff] [blame] | 665 | // Ensure that this flag gets cleared, since with autocorrect on, a |
| 666 | // delete key press may have a more complex result than deleting one |
| 667 | // character or the existing selection, so it will not get cleared |
| 668 | // above. |
| 669 | mGotDelete = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 670 | // Find the last character being replaced. If it can be represented by |
| 671 | // events, we will pass them to native (after replacing the beginning |
| 672 | // of the changed text), so we can see javascript events. |
| 673 | // Otherwise, replace the text being changed (including the last |
| 674 | // character) in the textfield. |
| 675 | TextUtils.getChars(s, start + count - 1, start + count, mCharacter, 0); |
Jeff Brown | 6b53e8d | 2010-11-10 16:03:06 -0800 | [diff] [blame] | 676 | KeyCharacterMap kmap = KeyCharacterMap.load(KeyCharacterMap.VIRTUAL_KEYBOARD); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 677 | KeyEvent[] events = kmap.getEvents(mCharacter); |
| 678 | boolean cannotUseKeyEvents = null == events; |
| 679 | int charactersFromKeyEvents = cannotUseKeyEvents ? 0 : 1; |
| 680 | if (count > 1 || cannotUseKeyEvents) { |
| 681 | String replace = s.subSequence(start, |
| 682 | start + count - charactersFromKeyEvents).toString(); |
| 683 | mWebView.replaceTextfieldText(start, start + before, replace, |
| 684 | start + count - charactersFromKeyEvents, |
| 685 | start + count - charactersFromKeyEvents); |
| 686 | } else { |
Leon Scroggins | d3465f6 | 2009-06-02 10:57:54 -0400 | [diff] [blame] | 687 | // This corrects the selection which may have been affected by the |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 688 | // trackball or auto-correct. |
Cary Clark | 243ea06 | 2009-06-25 10:49:32 -0400 | [diff] [blame] | 689 | if (DebugFlags.WEB_TEXT_VIEW) { |
| 690 | Log.v(LOGTAG, "onTextChanged start=" + start |
| 691 | + " start + before=" + (start + before)); |
| 692 | } |
Cary Clark | 0c5924e | 2009-09-30 10:24:19 -0400 | [diff] [blame] | 693 | if (!mInSetTextAndKeepSelection) { |
| 694 | mWebView.setSelection(start, start + before); |
| 695 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 696 | } |
Leon Scroggins | 6e3835f | 2009-03-25 15:15:24 -0700 | [diff] [blame] | 697 | if (!cannotUseKeyEvents) { |
| 698 | int length = events.length; |
| 699 | for (int i = 0; i < length; i++) { |
| 700 | // We never send modifier keys to native code so don't send them |
| 701 | // here either. |
| 702 | if (!KeyEvent.isModifierKey(events[i].getKeyCode())) { |
| 703 | sendDomEvent(events[i]); |
| 704 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 705 | } |
| 706 | } |
Leon Scroggins | 6e3835f | 2009-03-25 15:15:24 -0700 | [diff] [blame] | 707 | updateCachedTextfield(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 708 | } |
Leon Scroggins | d3465f6 | 2009-06-02 10:57:54 -0400 | [diff] [blame] | 709 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 710 | @Override |
Leon Scroggins | 0f5ad84 | 2009-07-17 15:08:34 -0400 | [diff] [blame] | 711 | public boolean onTouchEvent(MotionEvent event) { |
Leon Scroggins | 416a3c7 | 2009-07-22 13:05:43 -0400 | [diff] [blame] | 712 | switch (event.getAction()) { |
Leon Scroggins | 0f5ad84 | 2009-07-17 15:08:34 -0400 | [diff] [blame] | 713 | case MotionEvent.ACTION_DOWN: |
Leon Scroggins | 416a3c7 | 2009-07-22 13:05:43 -0400 | [diff] [blame] | 714 | super.onTouchEvent(event); |
Leon Scroggins | 0f5ad84 | 2009-07-17 15:08:34 -0400 | [diff] [blame] | 715 | // This event may be the start of a drag, so store it to pass to the |
| 716 | // WebView if it is. |
| 717 | mDragStartX = event.getX(); |
| 718 | mDragStartY = event.getY(); |
| 719 | mDragStartTime = event.getEventTime(); |
| 720 | mDragSent = false; |
Leon Scroggins | 72543e1 | 2009-07-23 15:29:45 -0400 | [diff] [blame] | 721 | mScrolled = false; |
Leon Scroggins | 6679f2f | 2009-08-12 18:48:10 -0400 | [diff] [blame] | 722 | mGotTouchDown = true; |
Leon Scroggins | ed90811 | 2009-12-01 10:45:24 -0500 | [diff] [blame] | 723 | mHasPerformedLongClick = false; |
Leon Scroggins | 0f5ad84 | 2009-07-17 15:08:34 -0400 | [diff] [blame] | 724 | break; |
| 725 | case MotionEvent.ACTION_MOVE: |
Leon Scroggins | ed90811 | 2009-12-01 10:45:24 -0500 | [diff] [blame] | 726 | if (mHasPerformedLongClick) { |
| 727 | mGotTouchDown = false; |
| 728 | return false; |
| 729 | } |
Leon Scroggins | 7ae3419 | 2009-09-16 14:12:52 -0400 | [diff] [blame] | 730 | int slop = ViewConfiguration.get(mContext).getScaledTouchSlop(); |
Leon Scroggins | 416a3c7 | 2009-07-22 13:05:43 -0400 | [diff] [blame] | 731 | Spannable buffer = getText(); |
| 732 | int initialScrollX = Touch.getInitialScrollX(this, buffer); |
| 733 | int initialScrollY = Touch.getInitialScrollY(this, buffer); |
| 734 | super.onTouchEvent(event); |
Leon Scroggins | d3997e5 | 2009-09-21 14:15:18 -0400 | [diff] [blame] | 735 | int dx = Math.abs(mScrollX - initialScrollX); |
| 736 | int dy = Math.abs(mScrollY - initialScrollY); |
| 737 | // Use a smaller slop when checking to see if we've moved far enough |
| 738 | // to scroll the text, because experimentally, slop has shown to be |
| 739 | // to big for the case of a small textfield. |
| 740 | int smallerSlop = slop/2; |
| 741 | if (dx > smallerSlop || dy > smallerSlop) { |
Leon Scroggins | 2a30744 | 2010-10-15 14:35:21 -0400 | [diff] [blame] | 742 | // Scrolling is handled in onScrollChanged. |
Leon Scroggins | 72543e1 | 2009-07-23 15:29:45 -0400 | [diff] [blame] | 743 | mScrolled = true; |
Leon Scroggins | ed90811 | 2009-12-01 10:45:24 -0500 | [diff] [blame] | 744 | cancelLongPress(); |
Leon Scroggins | 0f5ad84 | 2009-07-17 15:08:34 -0400 | [diff] [blame] | 745 | return true; |
| 746 | } |
Leon Scroggins | 7ae3419 | 2009-09-16 14:12:52 -0400 | [diff] [blame] | 747 | if (Math.abs((int) event.getX() - mDragStartX) < slop |
| 748 | && Math.abs((int) event.getY() - mDragStartY) < slop) { |
| 749 | // If the user has not scrolled further than slop, we should not |
| 750 | // send the drag. Instead, do nothing, and when the user lifts |
| 751 | // their finger, we will change the selection. |
| 752 | return true; |
| 753 | } |
Leon Scroggins | 0f5ad84 | 2009-07-17 15:08:34 -0400 | [diff] [blame] | 754 | if (mWebView != null) { |
| 755 | // Only want to set the initial state once. |
| 756 | if (!mDragSent) { |
| 757 | mWebView.initiateTextFieldDrag(mDragStartX, mDragStartY, |
| 758 | mDragStartTime); |
| 759 | mDragSent = true; |
| 760 | } |
Leon Scroggins | 416a3c7 | 2009-07-22 13:05:43 -0400 | [diff] [blame] | 761 | boolean scrolled = mWebView.textFieldDrag(event); |
| 762 | if (scrolled) { |
Leon Scroggins | 72543e1 | 2009-07-23 15:29:45 -0400 | [diff] [blame] | 763 | mScrolled = true; |
Leon Scroggins | 416a3c7 | 2009-07-22 13:05:43 -0400 | [diff] [blame] | 764 | cancelLongPress(); |
| 765 | return true; |
| 766 | } |
Leon Scroggins | 0f5ad84 | 2009-07-17 15:08:34 -0400 | [diff] [blame] | 767 | } |
| 768 | return false; |
| 769 | case MotionEvent.ACTION_UP: |
| 770 | case MotionEvent.ACTION_CANCEL: |
Leon Scroggins | 23cb86a | 2010-10-12 09:05:40 -0400 | [diff] [blame] | 771 | super.onTouchEvent(event); |
Leon Scroggins | ed90811 | 2009-12-01 10:45:24 -0500 | [diff] [blame] | 772 | if (mHasPerformedLongClick) { |
| 773 | mGotTouchDown = false; |
| 774 | return false; |
| 775 | } |
Leon Scroggins | 72543e1 | 2009-07-23 15:29:45 -0400 | [diff] [blame] | 776 | if (!mScrolled) { |
| 777 | // If the page scrolled, or the TextView scrolled, we do not |
Leon Scroggins | 6679f2f | 2009-08-12 18:48:10 -0400 | [diff] [blame] | 778 | // want to change the selection |
| 779 | cancelLongPress(); |
| 780 | if (mGotTouchDown && mWebView != null) { |
| 781 | mWebView.touchUpOnTextField(event); |
| 782 | } |
Leon Scroggins | 416a3c7 | 2009-07-22 13:05:43 -0400 | [diff] [blame] | 783 | } |
Leon Scroggins | 0f5ad84 | 2009-07-17 15:08:34 -0400 | [diff] [blame] | 784 | // Necessary for the WebView to reset its state |
| 785 | if (mWebView != null && mDragSent) { |
| 786 | mWebView.onTouchEvent(event); |
| 787 | } |
Leon Scroggins | 6679f2f | 2009-08-12 18:48:10 -0400 | [diff] [blame] | 788 | mGotTouchDown = false; |
Leon Scroggins | 0f5ad84 | 2009-07-17 15:08:34 -0400 | [diff] [blame] | 789 | break; |
| 790 | default: |
| 791 | break; |
| 792 | } |
| 793 | return true; |
| 794 | } |
| 795 | |
| 796 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 797 | public boolean onTrackballEvent(MotionEvent event) { |
| 798 | if (isPopupShowing()) { |
| 799 | return super.onTrackballEvent(event); |
| 800 | } |
| 801 | if (event.getAction() != MotionEvent.ACTION_MOVE) { |
| 802 | return false; |
| 803 | } |
Leon Scroggins | 86e1fa6 | 2011-01-10 17:52:41 -0500 | [diff] [blame] | 804 | Spannable text = getText(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 805 | MovementMethod move = getMovementMethod(); |
| 806 | if (move != null && getLayout() != null && |
| 807 | move.onTrackballEvent(this, text, event)) { |
Leon Scroggins | 17c3bfc | 2009-04-14 10:57:13 -0700 | [diff] [blame] | 808 | // Selection is changed in onSelectionChanged |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 809 | return true; |
| 810 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 811 | return false; |
| 812 | } |
| 813 | |
Leon Scroggins | ed90811 | 2009-12-01 10:45:24 -0500 | [diff] [blame] | 814 | @Override |
| 815 | public boolean performLongClick() { |
| 816 | mHasPerformedLongClick = true; |
| 817 | return super.performLongClick(); |
| 818 | } |
| 819 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 820 | /** |
Leon Scroggins | d3465f6 | 2009-06-02 10:57:54 -0400 | [diff] [blame] | 821 | * Remove this WebTextView from its host WebView, and return |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 822 | * focus to the host. |
| 823 | */ |
| 824 | /* package */ void remove() { |
| 825 | // hide the soft keyboard when the edit text is out of focus |
Leon Scroggins III | 71d17e4 | 2010-09-02 12:53:08 -0400 | [diff] [blame] | 826 | InputMethodManager imm = InputMethodManager.getInstance(mContext); |
| 827 | if (imm.isActive(this)) { |
| 828 | imm.hideSoftInputFromWindow(getWindowToken(), 0); |
| 829 | } |
Leon Scroggins | 4077723 | 2011-01-18 16:49:20 -0500 | [diff] [blame] | 830 | mInsideRemove = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 831 | mWebView.removeView(this); |
| 832 | mWebView.requestFocus(); |
Leon Scroggins | 4077723 | 2011-01-18 16:49:20 -0500 | [diff] [blame] | 833 | mInsideRemove = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 834 | } |
| 835 | |
Leon Scroggins | 200c13d | 2010-05-14 15:35:42 -0400 | [diff] [blame] | 836 | @Override |
Leon Scroggins | d518865 | 2011-01-19 17:01:55 -0500 | [diff] [blame] | 837 | public boolean requestRectangleOnScreen(Rect rectangle, boolean immediate) { |
| 838 | // Do nothing, since webkit will put the textfield on screen. |
Leon Scroggins | 200c13d | 2010-05-14 15:35:42 -0400 | [diff] [blame] | 839 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 840 | } |
| 841 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 842 | /** |
| 843 | * Send the DOM events for the specified event. |
| 844 | * @param event KeyEvent to be translated into a DOM event. |
| 845 | */ |
| 846 | private void sendDomEvent(KeyEvent event) { |
| 847 | mWebView.passToJavaScript(getText().toString(), event); |
| 848 | } |
| 849 | |
| 850 | /** |
| 851 | * Always use this instead of setAdapter, as this has features specific to |
Leon Scroggins | d3465f6 | 2009-06-02 10:57:54 -0400 | [diff] [blame] | 852 | * the WebTextView. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 853 | */ |
| 854 | public void setAdapterCustom(AutoCompleteAdapter adapter) { |
| 855 | if (adapter != null) { |
Leon Scroggins | aa7b9d7 | 2009-12-07 13:38:07 -0500 | [diff] [blame] | 856 | setInputType(getInputType() |
| 857 | | EditorInfo.TYPE_TEXT_FLAG_AUTO_COMPLETE); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 858 | adapter.setTextView(this); |
Leon Scroggins | 03e0119 | 2011-02-11 10:19:22 -0500 | [diff] [blame] | 859 | if (mAutoFillable) { |
| 860 | setOnItemClickListener(this); |
| 861 | } else { |
| 862 | setOnItemClickListener(null); |
| 863 | } |
| 864 | showDropDown(); |
| 865 | } else { |
| 866 | dismissDropDown(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 867 | } |
| 868 | super.setAdapter(adapter); |
| 869 | } |
| 870 | |
| 871 | /** |
| 872 | * This is a special version of ArrayAdapter which changes its text size |
| 873 | * to match the text size of its host TextView. |
| 874 | */ |
| 875 | public static class AutoCompleteAdapter extends ArrayAdapter<String> { |
| 876 | private TextView mTextView; |
| 877 | |
| 878 | public AutoCompleteAdapter(Context context, ArrayList<String> entries) { |
| 879 | super(context, com.android.internal.R.layout |
Leon Scroggins | 60344de | 2010-12-13 16:08:04 -0500 | [diff] [blame] | 880 | .web_text_view_dropdown, entries); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 881 | } |
| 882 | |
| 883 | /** |
| 884 | * {@inheritDoc} |
| 885 | */ |
Leon Scroggins | 03e0119 | 2011-02-11 10:19:22 -0500 | [diff] [blame] | 886 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 887 | public View getView(int position, View convertView, ViewGroup parent) { |
| 888 | TextView tv = |
| 889 | (TextView) super.getView(position, convertView, parent); |
| 890 | if (tv != null && mTextView != null) { |
| 891 | tv.setTextSize(mTextView.getTextSize()); |
| 892 | } |
| 893 | return tv; |
| 894 | } |
| 895 | |
| 896 | /** |
| 897 | * Set the TextView so we can match its text size. |
| 898 | */ |
| 899 | private void setTextView(TextView tv) { |
| 900 | mTextView = tv; |
| 901 | } |
| 902 | } |
| 903 | |
| 904 | /** |
Leon Scroggins | bb107bd | 2009-10-23 16:18:42 -0400 | [diff] [blame] | 905 | * Sets the selection when the user clicks on a textfield or textarea with |
| 906 | * the trackball or center key, or starts typing into it without clicking on |
| 907 | * it. |
| 908 | */ |
| 909 | /* package */ void setDefaultSelection() { |
| 910 | Spannable text = (Spannable) getText(); |
| 911 | int selection = mSingle ? text.length() : 0; |
Leon Scroggins | cfc2915 | 2009-12-02 10:12:57 -0500 | [diff] [blame] | 912 | if (Selection.getSelectionStart(text) == selection |
| 913 | && Selection.getSelectionEnd(text) == selection) { |
| 914 | // The selection of the UI copy is set correctly, but the |
| 915 | // WebTextView still needs to inform the webkit thread to set the |
| 916 | // selection. Normally that is done in onSelectionChanged, but |
| 917 | // onSelectionChanged will not be called because the UI copy is not |
| 918 | // changing. (This can happen when the WebTextView takes focus. |
| 919 | // That onSelectionChanged was blocked because the selection set |
| 920 | // when focusing is not necessarily the desirable selection for |
| 921 | // WebTextView.) |
| 922 | if (mWebView != null) { |
| 923 | mWebView.setSelection(selection, selection); |
| 924 | } |
| 925 | } else { |
| 926 | Selection.setSelection(text, selection, selection); |
| 927 | } |
Leon Scroggins III | b24e18b | 2010-04-20 17:07:28 -0400 | [diff] [blame] | 928 | if (mWebView != null) mWebView.incrementTextGeneration(); |
Leon Scroggins | bb107bd | 2009-10-23 16:18:42 -0400 | [diff] [blame] | 929 | } |
| 930 | |
| 931 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 932 | * Determine whether to use the system-wide password disguising method, |
| 933 | * or to use none. |
| 934 | * @param inPassword True if the textfield is a password field. |
| 935 | */ |
| 936 | /* package */ void setInPassword(boolean inPassword) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 937 | if (inPassword) { |
The Android Open Source Project | b2a3dd8 | 2009-03-09 11:52:12 -0700 | [diff] [blame] | 938 | setInputType(EditorInfo.TYPE_CLASS_TEXT | EditorInfo. |
Leon Scroggins | b5ce0e0 | 2010-11-01 13:20:24 -0400 | [diff] [blame] | 939 | TYPE_TEXT_VARIATION_WEB_PASSWORD); |
Leon Scroggins | 0857767 | 2009-07-16 13:55:35 -0400 | [diff] [blame] | 940 | createBackground(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 941 | } |
Leon Scroggins | 0857767 | 2009-07-16 13:55:35 -0400 | [diff] [blame] | 942 | // For password fields, draw the WebTextView. For others, just show |
| 943 | // webkit's drawing. |
Leon Scroggins | c26dc9f | 2010-11-12 12:01:34 -0500 | [diff] [blame] | 944 | if (!DebugFlags.DRAW_WEBTEXTVIEW) { |
| 945 | setWillNotDraw(!inPassword); |
| 946 | } |
Leon Scroggins | 0857767 | 2009-07-16 13:55:35 -0400 | [diff] [blame] | 947 | setBackgroundDrawable(inPassword ? mBackground : null); |
Leon Scroggins | 0857767 | 2009-07-16 13:55:35 -0400 | [diff] [blame] | 948 | } |
| 949 | |
| 950 | /** |
| 951 | * Private class used for the background of a password textfield. |
| 952 | */ |
| 953 | private static class OutlineDrawable extends Drawable { |
Leon Scroggins | 6e9b327 | 2010-12-10 17:32:25 -0500 | [diff] [blame] | 954 | private Paint mBackgroundPaint; |
| 955 | private Paint mOutlinePaint; |
| 956 | private float[] mLines; |
| 957 | public OutlineDrawable() { |
| 958 | mBackgroundPaint = new Paint(Paint.ANTI_ALIAS_FLAG); |
| 959 | mBackgroundPaint.setColor(Color.WHITE); |
| 960 | |
| 961 | mOutlinePaint = new Paint(Paint.ANTI_ALIAS_FLAG); |
| 962 | mOutlinePaint.setColor(Color.BLACK); |
| 963 | mOutlinePaint.setStyle(Paint.Style.STROKE); |
| 964 | |
| 965 | mLines = new float[16]; |
| 966 | } |
| 967 | @Override |
| 968 | public void setBounds(int left, int top, int right, int bottom) { |
| 969 | super.setBounds(left, top, right, bottom); |
Leon Scroggins | 6e0e0ec | 2011-03-01 13:17:37 -0500 | [diff] [blame] | 970 | bottom--; |
| 971 | right -= 2; |
Leon Scroggins | 6e9b327 | 2010-12-10 17:32:25 -0500 | [diff] [blame] | 972 | // Top line |
| 973 | mLines[0] = left; |
| 974 | mLines[1] = top + 1; |
| 975 | mLines[2] = right; |
| 976 | mLines[3] = top + 1; |
| 977 | // Right line |
| 978 | mLines[4] = right; |
| 979 | mLines[5] = top; |
| 980 | mLines[6] = right; |
| 981 | mLines[7] = bottom; |
| 982 | // Bottom line |
| 983 | mLines[8] = left; |
| 984 | mLines[9] = bottom; |
| 985 | mLines[10] = right; |
| 986 | mLines[11] = bottom; |
| 987 | // Left line |
| 988 | mLines[12] = left + 1; |
| 989 | mLines[13] = top; |
| 990 | mLines[14] = left + 1; |
| 991 | mLines[15] = bottom; |
| 992 | } |
| 993 | @Override |
Leon Scroggins | 0857767 | 2009-07-16 13:55:35 -0400 | [diff] [blame] | 994 | public void draw(Canvas canvas) { |
Leon Scroggins | 0857767 | 2009-07-16 13:55:35 -0400 | [diff] [blame] | 995 | // Draw the background. |
Leon Scroggins | 6e9b327 | 2010-12-10 17:32:25 -0500 | [diff] [blame] | 996 | canvas.drawRect(getBounds(), mBackgroundPaint); |
Leon Scroggins | 0857767 | 2009-07-16 13:55:35 -0400 | [diff] [blame] | 997 | // Draw the outline. |
Leon Scroggins | 6e9b327 | 2010-12-10 17:32:25 -0500 | [diff] [blame] | 998 | canvas.drawLines(mLines, mOutlinePaint); |
Leon Scroggins | 0857767 | 2009-07-16 13:55:35 -0400 | [diff] [blame] | 999 | } |
| 1000 | // Always want it to be opaque. |
Leon Scroggins | 6e9b327 | 2010-12-10 17:32:25 -0500 | [diff] [blame] | 1001 | @Override |
Leon Scroggins | 0857767 | 2009-07-16 13:55:35 -0400 | [diff] [blame] | 1002 | public int getOpacity() { |
| 1003 | return PixelFormat.OPAQUE; |
| 1004 | } |
| 1005 | // These are needed because they are abstract in Drawable. |
Leon Scroggins | 6e9b327 | 2010-12-10 17:32:25 -0500 | [diff] [blame] | 1006 | @Override |
Leon Scroggins | 0857767 | 2009-07-16 13:55:35 -0400 | [diff] [blame] | 1007 | public void setAlpha(int alpha) { } |
Leon Scroggins | 6e9b327 | 2010-12-10 17:32:25 -0500 | [diff] [blame] | 1008 | @Override |
Leon Scroggins | 0857767 | 2009-07-16 13:55:35 -0400 | [diff] [blame] | 1009 | public void setColorFilter(ColorFilter cf) { } |
| 1010 | } |
| 1011 | |
| 1012 | /** |
| 1013 | * Create a background for the WebTextView and set up the paint for drawing |
| 1014 | * the text. This way, we can see the password transformation of the |
| 1015 | * system, which (optionally) shows the actual text before changing to dots. |
| 1016 | * The background is necessary to hide the webkit-drawn text beneath. |
| 1017 | */ |
| 1018 | private void createBackground() { |
| 1019 | if (mBackground != null) { |
| 1020 | return; |
| 1021 | } |
| 1022 | mBackground = new OutlineDrawable(); |
| 1023 | |
| 1024 | setGravity(Gravity.CENTER_VERTICAL); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1025 | } |
| 1026 | |
Leon Scroggins | bb107bd | 2009-10-23 16:18:42 -0400 | [diff] [blame] | 1027 | @Override |
| 1028 | public void setInputType(int type) { |
| 1029 | mFromSetInputType = true; |
| 1030 | super.setInputType(type); |
| 1031 | mFromSetInputType = false; |
| 1032 | } |
| 1033 | |
Leon Scroggins | aa7b9d7 | 2009-12-07 13:38:07 -0500 | [diff] [blame] | 1034 | private void setMaxLength(int maxLength) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1035 | mMaxLength = maxLength; |
| 1036 | if (-1 == maxLength) { |
| 1037 | setFilters(NO_FILTERS); |
| 1038 | } else { |
| 1039 | setFilters(new InputFilter[] { |
| 1040 | new InputFilter.LengthFilter(maxLength) }); |
| 1041 | } |
| 1042 | } |
| 1043 | |
| 1044 | /** |
| 1045 | * Set the pointer for this node so it can be determined which node this |
Leon Scroggins | d3465f6 | 2009-06-02 10:57:54 -0400 | [diff] [blame] | 1046 | * WebTextView represents. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1047 | * @param ptr Integer representing the pointer to the node which this |
Leon Scroggins | d3465f6 | 2009-06-02 10:57:54 -0400 | [diff] [blame] | 1048 | * WebTextView represents. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1049 | */ |
| 1050 | /* package */ void setNodePointer(int ptr) { |
| 1051 | mNodePointer = ptr; |
| 1052 | } |
| 1053 | |
| 1054 | /** |
Leon Scroggins | d3465f6 | 2009-06-02 10:57:54 -0400 | [diff] [blame] | 1055 | * Determine the position and size of WebTextView, and add it to the |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1056 | * WebView's view heirarchy. All parameters are presumed to be in |
| 1057 | * view coordinates. Also requests Focus and sets the cursor to not |
| 1058 | * request to be in view. |
| 1059 | * @param x x-position of the textfield. |
| 1060 | * @param y y-position of the textfield. |
| 1061 | * @param width width of the textfield. |
| 1062 | * @param height height of the textfield. |
| 1063 | */ |
| 1064 | /* package */ void setRect(int x, int y, int width, int height) { |
| 1065 | LayoutParams lp = (LayoutParams) getLayoutParams(); |
| 1066 | if (null == lp) { |
| 1067 | lp = new LayoutParams(width, height, x, y); |
| 1068 | } else { |
| 1069 | lp.x = x; |
| 1070 | lp.y = y; |
| 1071 | lp.width = width; |
| 1072 | lp.height = height; |
| 1073 | } |
| 1074 | if (getParent() == null) { |
| 1075 | mWebView.addView(this, lp); |
| 1076 | } else { |
| 1077 | setLayoutParams(lp); |
| 1078 | } |
| 1079 | // Set up a measure spec so a layout can always be recreated. |
| 1080 | mWidthSpec = MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY); |
| 1081 | mHeightSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1082 | } |
| 1083 | |
| 1084 | /** |
Leon Scroggins | 6679f2f | 2009-08-12 18:48:10 -0400 | [diff] [blame] | 1085 | * Set the selection, and disable our onSelectionChanged action. |
| 1086 | */ |
| 1087 | /* package */ void setSelectionFromWebKit(int start, int end) { |
Leon Scroggins | ef92e7a | 2009-09-02 17:47:29 -0400 | [diff] [blame] | 1088 | if (start < 0 || end < 0) return; |
| 1089 | Spannable text = (Spannable) getText(); |
| 1090 | int length = text.length(); |
| 1091 | if (start > length || end > length) return; |
Leon Scroggins | 6679f2f | 2009-08-12 18:48:10 -0400 | [diff] [blame] | 1092 | mFromWebKit = true; |
Leon Scroggins | ef92e7a | 2009-09-02 17:47:29 -0400 | [diff] [blame] | 1093 | Selection.setSelection(text, start, end); |
Leon Scroggins | 6679f2f | 2009-08-12 18:48:10 -0400 | [diff] [blame] | 1094 | mFromWebKit = false; |
| 1095 | } |
| 1096 | |
| 1097 | /** |
Leon Scroggins | c35f4ac | 2010-11-10 08:59:05 -0500 | [diff] [blame] | 1098 | * Update the text size according to the size of the focus candidate's text |
| 1099 | * size in mWebView. Should only be called from mWebView. |
| 1100 | */ |
| 1101 | /* package */ void updateTextSize() { |
| 1102 | Assert.assertNotNull("updateTextSize should only be called from " |
| 1103 | + "mWebView, so mWebView should never be null!", mWebView); |
| 1104 | // Note that this is approximately WebView.contentToViewDimension, |
| 1105 | // without being rounded. |
| 1106 | float size = mWebView.nativeFocusCandidateTextSize() |
| 1107 | * mWebView.getScale(); |
| 1108 | setTextSize(TypedValue.COMPLEX_UNIT_PX, size); |
| 1109 | } |
| 1110 | |
| 1111 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1112 | * Set the text to the new string, but use the old selection, making sure |
| 1113 | * to keep it within the new string. |
| 1114 | * @param text The new text to place in the textfield. |
| 1115 | */ |
| 1116 | /* package */ void setTextAndKeepSelection(String text) { |
| 1117 | mPreChange = text.toString(); |
Leon Scroggins | a51063c | 2011-01-21 17:14:36 -0500 | [diff] [blame] | 1118 | Editable edit = getText(); |
Leon Scroggins | 3fcf486 | 2010-04-13 10:33:41 -0400 | [diff] [blame] | 1119 | int selStart = Selection.getSelectionStart(edit); |
| 1120 | int selEnd = Selection.getSelectionEnd(edit); |
Cary Clark | 0c5924e | 2009-09-30 10:24:19 -0400 | [diff] [blame] | 1121 | mInSetTextAndKeepSelection = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1122 | edit.replace(0, edit.length(), text); |
Leon Scroggins | 3fcf486 | 2010-04-13 10:33:41 -0400 | [diff] [blame] | 1123 | int newLength = edit.length(); |
| 1124 | if (selStart > newLength) selStart = newLength; |
| 1125 | if (selEnd > newLength) selEnd = newLength; |
| 1126 | Selection.setSelection(edit, selStart, selEnd); |
Cary Clark | 0c5924e | 2009-09-30 10:24:19 -0400 | [diff] [blame] | 1127 | mInSetTextAndKeepSelection = false; |
Leon Scroggins | a51063c | 2011-01-21 17:14:36 -0500 | [diff] [blame] | 1128 | InputMethodManager imm = InputMethodManager.peekInstance(); |
| 1129 | if (imm != null && imm.isActive(this)) { |
| 1130 | // Since the text has changed, do not allow the IME to replace the |
| 1131 | // existing text as though it were a completion. |
| 1132 | imm.restartInput(this); |
| 1133 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1134 | updateCachedTextfield(); |
| 1135 | } |
Leon Scroggins | d3465f6 | 2009-06-02 10:57:54 -0400 | [diff] [blame] | 1136 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1137 | /** |
Leon Scroggins | aa7b9d7 | 2009-12-07 13:38:07 -0500 | [diff] [blame] | 1138 | * Called by WebView.rebuildWebTextView(). Based on the type of the <input> |
| 1139 | * element, set up the WebTextView, its InputType, and IME Options properly. |
Leon Scroggins | b5ce0e0 | 2010-11-01 13:20:24 -0400 | [diff] [blame] | 1140 | * @param type int corresponding to enum "Type" defined in CachedInput.h. |
Leon Scroggins | aa7b9d7 | 2009-12-07 13:38:07 -0500 | [diff] [blame] | 1141 | * Does not correspond to HTMLInputElement::InputType so this |
| 1142 | * is unaffected if that changes, and also because that has no |
| 1143 | * type corresponding to textarea (which is its own tag). |
| 1144 | */ |
| 1145 | /* package */ void setType(int type) { |
| 1146 | if (mWebView == null) return; |
| 1147 | boolean single = true; |
| 1148 | boolean inPassword = false; |
| 1149 | int maxLength = -1; |
Leon Scroggins | b5ce0e0 | 2010-11-01 13:20:24 -0400 | [diff] [blame] | 1150 | int inputType = EditorInfo.TYPE_CLASS_TEXT |
| 1151 | | EditorInfo.TYPE_TEXT_VARIATION_WEB_EDIT_TEXT; |
Leon Scroggins | 2edd682 | 2010-01-12 11:36:13 -0500 | [diff] [blame] | 1152 | int imeOptions = EditorInfo.IME_FLAG_NO_EXTRACT_UI |
| 1153 | | EditorInfo.IME_FLAG_NO_FULLSCREEN; |
Leon Scroggins | b5ce0e0 | 2010-11-01 13:20:24 -0400 | [diff] [blame] | 1154 | if (TEXT_AREA != type |
| 1155 | && mWebView.nativeFocusCandidateHasNextTextfield()) { |
| 1156 | imeOptions |= EditorInfo.IME_FLAG_NAVIGATE_NEXT; |
| 1157 | } |
Leon Scroggins | aa7b9d7 | 2009-12-07 13:38:07 -0500 | [diff] [blame] | 1158 | switch (type) { |
Leon Scroggins | b5ce0e0 | 2010-11-01 13:20:24 -0400 | [diff] [blame] | 1159 | case NORMAL_TEXT_FIELD: |
Leon Scroggins | af314e2 | 2010-02-09 17:06:59 -0500 | [diff] [blame] | 1160 | imeOptions |= EditorInfo.IME_ACTION_GO; |
| 1161 | break; |
Leon Scroggins | b5ce0e0 | 2010-11-01 13:20:24 -0400 | [diff] [blame] | 1162 | case TEXT_AREA: |
Leon Scroggins | aa7b9d7 | 2009-12-07 13:38:07 -0500 | [diff] [blame] | 1163 | single = false; |
Leon Scroggins | b5ce0e0 | 2010-11-01 13:20:24 -0400 | [diff] [blame] | 1164 | inputType |= EditorInfo.TYPE_TEXT_FLAG_MULTI_LINE |
Leon Scroggins | aa7b9d7 | 2009-12-07 13:38:07 -0500 | [diff] [blame] | 1165 | | EditorInfo.TYPE_TEXT_FLAG_CAP_SENTENCES |
| 1166 | | EditorInfo.TYPE_TEXT_FLAG_AUTO_CORRECT; |
Leon Scroggins | 2edd682 | 2010-01-12 11:36:13 -0500 | [diff] [blame] | 1167 | imeOptions |= EditorInfo.IME_ACTION_NONE; |
Leon Scroggins | aa7b9d7 | 2009-12-07 13:38:07 -0500 | [diff] [blame] | 1168 | break; |
Leon Scroggins | b5ce0e0 | 2010-11-01 13:20:24 -0400 | [diff] [blame] | 1169 | case PASSWORD: |
Leon Scroggins | aa7b9d7 | 2009-12-07 13:38:07 -0500 | [diff] [blame] | 1170 | inPassword = true; |
Leon Scroggins | d5ff5db | 2010-02-08 13:07:41 -0500 | [diff] [blame] | 1171 | imeOptions |= EditorInfo.IME_ACTION_GO; |
Leon Scroggins | aa7b9d7 | 2009-12-07 13:38:07 -0500 | [diff] [blame] | 1172 | break; |
Leon Scroggins | b5ce0e0 | 2010-11-01 13:20:24 -0400 | [diff] [blame] | 1173 | case SEARCH: |
Leon Scroggins | 2edd682 | 2010-01-12 11:36:13 -0500 | [diff] [blame] | 1174 | imeOptions |= EditorInfo.IME_ACTION_SEARCH; |
Leon Scroggins | aa7b9d7 | 2009-12-07 13:38:07 -0500 | [diff] [blame] | 1175 | break; |
Leon Scroggins | b5ce0e0 | 2010-11-01 13:20:24 -0400 | [diff] [blame] | 1176 | case EMAIL: |
| 1177 | inputType = EditorInfo.TYPE_TEXT_VARIATION_WEB_EMAIL_ADDRESS; |
Leon Scroggins | d5ff5db | 2010-02-08 13:07:41 -0500 | [diff] [blame] | 1178 | imeOptions |= EditorInfo.IME_ACTION_GO; |
Leon Scroggins | aa7b9d7 | 2009-12-07 13:38:07 -0500 | [diff] [blame] | 1179 | break; |
Leon Scroggins | b5ce0e0 | 2010-11-01 13:20:24 -0400 | [diff] [blame] | 1180 | case NUMBER: |
Leon Scroggins | d5ff5db | 2010-02-08 13:07:41 -0500 | [diff] [blame] | 1181 | inputType |= EditorInfo.TYPE_CLASS_NUMBER; |
| 1182 | // Number and telephone do not have both a Tab key and an |
| 1183 | // action, so set the action to NEXT |
| 1184 | imeOptions |= EditorInfo.IME_ACTION_NEXT; |
Leon Scroggins | aa7b9d7 | 2009-12-07 13:38:07 -0500 | [diff] [blame] | 1185 | break; |
Leon Scroggins | b5ce0e0 | 2010-11-01 13:20:24 -0400 | [diff] [blame] | 1186 | case TELEPHONE: |
Leon Scroggins | d5ff5db | 2010-02-08 13:07:41 -0500 | [diff] [blame] | 1187 | inputType |= EditorInfo.TYPE_CLASS_PHONE; |
| 1188 | imeOptions |= EditorInfo.IME_ACTION_NEXT; |
Leon Scroggins | aa7b9d7 | 2009-12-07 13:38:07 -0500 | [diff] [blame] | 1189 | break; |
Leon Scroggins | b5ce0e0 | 2010-11-01 13:20:24 -0400 | [diff] [blame] | 1190 | case URL: |
Leon Scroggins | d5ff5db | 2010-02-08 13:07:41 -0500 | [diff] [blame] | 1191 | // TYPE_TEXT_VARIATION_URI prevents Tab key from showing, so |
| 1192 | // exclude it for now. |
| 1193 | imeOptions |= EditorInfo.IME_ACTION_GO; |
Leon Scroggins | aa7b9d7 | 2009-12-07 13:38:07 -0500 | [diff] [blame] | 1194 | break; |
| 1195 | default: |
Leon Scroggins | af314e2 | 2010-02-09 17:06:59 -0500 | [diff] [blame] | 1196 | imeOptions |= EditorInfo.IME_ACTION_GO; |
Leon Scroggins | aa7b9d7 | 2009-12-07 13:38:07 -0500 | [diff] [blame] | 1197 | break; |
| 1198 | } |
Leon Scroggins | 3a50339 | 2010-01-06 17:04:38 -0500 | [diff] [blame] | 1199 | setHint(null); |
Leon Scroggins | aa7b9d7 | 2009-12-07 13:38:07 -0500 | [diff] [blame] | 1200 | if (single) { |
Leon Scroggins | 3a50339 | 2010-01-06 17:04:38 -0500 | [diff] [blame] | 1201 | mWebView.requestLabel(mWebView.nativeFocusCandidateFramePointer(), |
| 1202 | mNodePointer); |
Leon Scroggins | aa7b9d7 | 2009-12-07 13:38:07 -0500 | [diff] [blame] | 1203 | maxLength = mWebView.nativeFocusCandidateMaxLength(); |
Leon Scroggins | ae0238c | 2011-01-05 15:12:55 -0500 | [diff] [blame] | 1204 | boolean autoComplete = mWebView.nativeFocusCandidateIsAutoComplete(); |
| 1205 | if (type != PASSWORD && (mAutoFillable || autoComplete)) { |
Leon Scroggins | aa7b9d7 | 2009-12-07 13:38:07 -0500 | [diff] [blame] | 1206 | String name = mWebView.nativeFocusCandidateName(); |
| 1207 | if (name != null && name.length() > 0) { |
Leon Scroggins | ae0238c | 2011-01-05 15:12:55 -0500 | [diff] [blame] | 1208 | mWebView.requestFormData(name, mNodePointer, mAutoFillable, |
| 1209 | autoComplete); |
Leon Scroggins | aa7b9d7 | 2009-12-07 13:38:07 -0500 | [diff] [blame] | 1210 | } |
| 1211 | } |
Leon Scroggins | aa7b9d7 | 2009-12-07 13:38:07 -0500 | [diff] [blame] | 1212 | } |
| 1213 | mSingle = single; |
| 1214 | setMaxLength(maxLength); |
| 1215 | setHorizontallyScrolling(single); |
| 1216 | setInputType(inputType); |
Leon Scroggins | 2edd682 | 2010-01-12 11:36:13 -0500 | [diff] [blame] | 1217 | setImeOptions(imeOptions); |
Leon Scroggins | aa7b9d7 | 2009-12-07 13:38:07 -0500 | [diff] [blame] | 1218 | setInPassword(inPassword); |
| 1219 | AutoCompleteAdapter adapter = null; |
| 1220 | setAdapterCustom(adapter); |
| 1221 | } |
| 1222 | |
| 1223 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1224 | * Update the cache to reflect the current text. |
| 1225 | */ |
| 1226 | /* package */ void updateCachedTextfield() { |
| 1227 | mWebView.updateCachedTextfield(getText().toString()); |
| 1228 | } |
Ben Murdoch | 5791438 | 2010-11-16 11:50:39 +0000 | [diff] [blame] | 1229 | |
| 1230 | /* package */ void setAutoFillProfileIsSet(boolean autoFillProfileIsSet) { |
| 1231 | mAutoFillProfileIsSet = autoFillProfileIsSet; |
| 1232 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1233 | } |