blob: 7f4f10345bf43e870fd926ec82094e1ab233b8c4 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
17package android.webkit;
18
Leon Scroggins241403f2009-09-16 18:26:17 -040019import com.android.internal.widget.EditableInputConnection;
20
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021import android.content.Context;
Leon Scroggins08577672009-07-16 13:55:35 -040022import android.graphics.Canvas;
23import android.graphics.Color;
24import android.graphics.ColorFilter;
25import android.graphics.Paint;
26import android.graphics.PixelFormat;
27import android.graphics.Rect;
28import android.graphics.drawable.Drawable;
Leon Scrogginsd69b7012011-03-09 16:18:28 -050029import android.os.Bundle;
Ben Murdoch57914382010-11-16 11:50:39 +000030import android.os.Handler;
31import android.os.Message;
Leon Scrogginsd69b7012011-03-09 16:18:28 -050032import android.os.ResultReceiver;
Leon Scroggins1ca56262010-11-18 14:03:03 -050033import android.text.BoringLayout.Metrics;
34import android.text.DynamicLayout;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035import android.text.Editable;
36import android.text.InputFilter;
Leon Scrogginseb51f3e2010-05-25 11:13:47 -040037import android.text.Layout;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038import android.text.Selection;
39import android.text.Spannable;
Leon Scroggins08577672009-07-16 13:55:35 -040040import android.text.TextPaint;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041import android.text.TextUtils;
42import android.text.method.MovementMethod;
Leon Scroggins0f5ad842009-07-17 15:08:34 -040043import android.text.method.Touch;
Cary Clark243ea062009-06-25 10:49:32 -040044import android.util.Log;
Leon Scrogginsc35f4ac2010-11-10 08:59:05 -050045import android.util.TypedValue;
Leon Scroggins08577672009-07-16 13:55:35 -040046import android.view.Gravity;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047import android.view.KeyCharacterMap;
48import android.view.KeyEvent;
49import android.view.MotionEvent;
50import android.view.View;
Leon Scroggins7ae34192009-09-16 14:12:52 -040051import android.view.ViewConfiguration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052import android.view.ViewGroup;
Leon Scroggins0ca70882009-06-26 17:45:29 -040053import android.view.inputmethod.EditorInfo;
Leon Scroggins0ca70882009-06-26 17:45:29 -040054import android.view.inputmethod.InputConnection;
Gilles Debunne52c7ba12011-03-10 10:50:09 -080055import android.view.inputmethod.InputMethodManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056import android.widget.AbsoluteLayout.LayoutParams;
Ben Murdoch62275a42010-09-07 11:27:28 +010057import android.widget.AdapterView;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058import android.widget.ArrayAdapter;
59import android.widget.AutoCompleteTextView;
60import android.widget.TextView;
61
62import java.util.ArrayList;
63
Leon Scrogginsc35f4ac2010-11-10 08:59:05 -050064import junit.framework.Assert;
65
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066/**
Leon Scrogginsd3465f62009-06-02 10:57:54 -040067 * WebTextView is a specialized version of EditText used by WebView
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068 * to overlay html textfields (and textareas) to use our standard
69 * text editing.
70 */
Leon Scroggins03e01192011-02-11 10:19:22 -050071/* package */ class WebTextView extends AutoCompleteTextView
72 implements AdapterView.OnItemClickListener {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073
Cary Clark243ea062009-06-25 10:49:32 -040074 static final String LOGTAG = "webtextview";
75
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076 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 Project9066cfe2009-03-03 19:31:44 -080085 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 Scroggins08577672009-07-16 13:55:35 -040089 private Drawable mBackground;
Leon Scroggins0f5ad842009-07-17 15:08:34 -040090 // 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 Scroggins72543e12009-07-23 15:29:45 -040096 // 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 Scroggins6679f2f2009-08-12 18:48:10 -040099 // 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 Scrogginsbb107bd2009-10-23 16:18:42 -0400102 // 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 Scroggins6679f2f2009-08-12 18:48:10 -0400111 private boolean mGotTouchDown;
Leon Scrogginsed908112009-12-01 10:45:24 -0500112 // Keep track of whether a long press has happened. Only meaningful after
113 // an ACTION_DOWN MotionEvent
114 private boolean mHasPerformedLongClick;
Cary Clark0c5924e2009-09-30 10:24:19 -0400115 private boolean mInSetTextAndKeepSelection;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116 // 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 IIIe742c082010-09-02 15:48:55 -0400124 // 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 Project9066cfe2009-03-03 19:31:44 -0800129
Ben Murdoch62275a42010-09-07 11:27:28 +0100130 // 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 Murdoch57914382010-11-16 11:50:39 +0000136 private boolean mAutoFillProfileIsSet;
Leon Scroggins40777232011-01-18 16:49:20 -0500137 // Used to determine whether onFocusChanged was called as a result of
138 // calling remove().
139 private boolean mInsideRemove;
Leon Scrogginsd69b7012011-03-09 16:18:28 -0500140 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 Murdoch62275a42010-09-07 11:27:28 +0100157
Leon Scrogginsb5ce0e02010-11-01 13:20:24 -0400158 // 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 Murdoch57914382010-11-16 11:50:39 +0000168 private static final int AUTOFILL_FORM = 100;
169 private Handler mHandler;
170
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800171 /**
Leon Scrogginsd3465f62009-06-02 10:57:54 -0400172 * Create a new WebTextView.
173 * @param context The Context for this WebTextView.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174 * @param webView The WebView that created this.
175 */
Ben Murdoch62275a42010-09-07 11:27:28 +0100176 /* package */ WebTextView(Context context, WebView webView, int autoFillQueryId) {
Leon Scrogginse2655322010-02-16 16:09:59 -0500177 super(context, null, com.android.internal.R.attr.webTextViewStyle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800178 mWebView = webView;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800179 mMaxLength = -1;
Ben Murdoch62275a42010-09-07 11:27:28 +0100180 setAutoFillable(autoFillQueryId);
Leon Scrogginsc35f4ac2010-11-10 08:59:05 -0500181 // 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 Scrogginsc26dc9f2010-11-12 12:01:34 -0500191 setTextColor(DebugFlags.DRAW_WEBTEXTVIEW ? Color.RED : Color.BLACK);
Leon Scrogginsc35f4ac2010-11-10 08:59:05 -0500192 // This helps to align the text better with the text in the web page.
193 setIncludeFontPadding(false);
Ben Murdoch57914382010-11-16 11:50:39 +0000194
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 Scrogginsd69b7012011-03-09 16:18:28 -0500205 mReceiver = new MyResultReceiver(mHandler);
Ben Murdoch62275a42010-09-07 11:27:28 +0100206 }
207
208 public void setAutoFillable(int queryId) {
Ben Murdochf39b2cf2010-09-09 10:26:46 +0100209 mAutoFillable = mWebView.getSettings().getAutoFillEnabled()
210 && (queryId != FORM_NOT_AUTOFILLABLE);
Ben Murdoch62275a42010-09-07 11:27:28 +0100211 mQueryId = queryId;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800212 }
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 Scroggins4890feb2009-07-02 10:37:10 -0400222
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 Scroggins4890feb2009-07-02 10:37:10 -0400229 isArrowKey = true;
230 break;
231 }
Leon Scrogginsbb107bd2009-10-23 16:18:42 -0400232
Leon Scrogginsd5ff5db2010-02-08 13:07:41 -0500233 if (KeyEvent.KEYCODE_TAB == keyCode) {
234 if (down) {
235 onEditorAction(EditorInfo.IME_ACTION_NEXT);
236 }
237 return true;
238 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800239 Spannable text = (Spannable) getText();
Leon Scroggins III3d540842010-08-27 14:44:37 -0400240 int oldStart = Selection.getSelectionStart(text);
241 int oldEnd = Selection.getSelectionEnd(text);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800242 // Normally the delete key's dom events are sent via onTextChanged.
Leon Scroggins III3d540842010-08-27 14:44:37 -0400243 // 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 IIIe742c082010-09-02 15:48:55 -0400246 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 Project9066cfe2009-03-03 19:31:44 -0800256 }
257
Cary Clarkaa86ac82010-12-28 11:30:18 -0500258 if (mSingle && (KeyEvent.KEYCODE_ENTER == keyCode
259 || KeyEvent.KEYCODE_NUMPAD_ENTER == keyCode)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260 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 Scroggins1d96ca02009-10-23 11:49:03 -0400280 mWebView.centerKeyPressOnTextField();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800281 }
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 Project9066cfe2009-03-03 19:31:44 -0800290
Leon Scroggins III3d540842010-08-27 14:44:37 -0400291 int oldLength = text.length();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800292 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 Scrogginsd3465f62009-06-02 10:57:54 -0400303 // If the WebTextView handled the key it was either an alphanumeric
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800304 // 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 Clarkaa86ac82010-12-28 11:30:18 -0500313 if (KeyEvent.KEYCODE_ENTER == keyCode
314 || KeyEvent.KEYCODE_NUMPAD_ENTER == keyCode) {
Cary Clarkd6982c92009-05-29 11:02:22 -0400315 // 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 Project9066cfe2009-03-03 19:31:44 -0800319 }
320 if (maxedOut && !isArrowKey && keyCode != KeyEvent.KEYCODE_DEL) {
321 if (oldEnd == oldStart) {
322 // Return true so the key gets dropped.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800323 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 Project9066cfe2009-03-03 19:31:44 -0800336 return true;
337 }
338 }
Leon Scroggins17c3bfc2009-04-14 10:57:13 -0700339 /* 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 Project9066cfe2009-03-03 19:31:44 -0800345 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 Scroggins17c3bfc2009-04-14 10:57:13 -0700350 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800351 return true;
352 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800353 // 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 Clarkd6982c92009-05-29 11:02:22 -0400359 if (isArrowKey) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800360 // WebView check the trackballtime in onKeyDown to avoid calling
Leon Scrogginsd3465f62009-06-02 10:57:54 -0400361 // 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 Project9066cfe2009-03-03 19:31:44 -0800363 // 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 Scrogginsfcf57762010-12-14 18:09:23 -0500371 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 Scrogginsd69b7012011-03-09 16:18:28 -0500383 /* package */ ResultReceiver getResultReceiver() { return mReceiver; }
384
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800385 /**
Leon Scrogginsd3465f62009-06-02 10:57:54 -0400386 * Determine whether this WebTextView currently represents the node
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800387 * represented by ptr.
388 * @param ptr Pointer to a node to compare to.
Leon Scrogginsd3465f62009-06-02 10:57:54 -0400389 * @return boolean Whether this WebTextView already represents the node
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800390 * pointed to by ptr.
391 */
392 /* package */ boolean isSameTextField(int ptr) {
393 return ptr == mNodePointer;
394 }
395
Leon Scroggins2a307442010-10-15 14:35:21 -0400396 /**
Leon Scrogginsd5188652011-01-19 17:01:55 -0500397 * Ensure that the underlying text field/area is lined up with the WebTextView.
Leon Scroggins2a307442010-10-15 14:35:21 -0400398 */
399 private void lineUpScroll() {
Leon Scrogginse8aa9542010-10-18 10:09:07 -0400400 Layout layout = getLayout();
401 if (mWebView != null && layout != null) {
Leon Scrogginsd5188652011-01-19 17:01:55 -0500402 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 Scroggins2a307442010-10-15 14:35:21 -0400414 }
Leon Scroggins2a307442010-10-15 14:35:21 -0400415 }
416 }
417
Leon Scroggins1ca56262010-11-18 14:03:03 -0500418 @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 Scrogginse11ce832011-03-03 17:36:40 -0500438 mLayout = new WebTextViewLayout(text, text, getPaint(), mLayout.getWidth(),
Leon Scroggins1ca56262010-11-18 14:03:03 -0500439 mLayout.getAlignment(), mLayout.getSpacingMultiplier(),
440 mLayout.getSpacingAdd(), false, null, ellipsisWidth,
441 lineHeight);
442 }
Leon Scrogginsd5188652011-01-19 17:01:55 -0500443 lineUpScroll();
Leon Scroggins1ca56262010-11-18 14:03:03 -0500444 }
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 Scroggins0ca70882009-06-26 17:45:29 -0400481 @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 IIIdfbb1f42010-04-14 15:40:56 -0400493 /**
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 }
518 }
519
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800520 @Override
Leon Scroggins69ec5c22010-04-22 16:27:21 -0400521 protected void onDraw(Canvas canvas) {
522 // onDraw should only be called for password fields. If WebTextView is
523 // still drawing, but is no longer corresponding to a password field,
524 // remove it.
Leon Scrogginsc26dc9f2010-11-12 12:01:34 -0500525 if (!DebugFlags.DRAW_WEBTEXTVIEW && (mWebView == null
526 || !mWebView.nativeFocusCandidateIsPassword()
527 || !isSameTextField(mWebView.nativeFocusCandidatePointer()))) {
Leon Scroggins69ec5c22010-04-22 16:27:21 -0400528 // Although calling remove() would seem to make more sense here,
529 // changing it to not be a password field will make it not draw.
530 // Other code will make sure that it is removed completely, but this
531 // way the user will not see it.
532 setInPassword(false);
533 } else {
534 super.onDraw(canvas);
535 }
536 }
537
538 @Override
Leon Scroggins01058282009-07-30 16:33:56 -0400539 public void onEditorAction(int actionCode) {
540 switch (actionCode) {
541 case EditorInfo.IME_ACTION_NEXT:
Leon Scroggins1be40982010-03-01 11:20:31 -0500542 if (mWebView.nativeMoveCursorToNextTextInput()) {
Leon Scroggins1be40982010-03-01 11:20:31 -0500543 // Preemptively rebuild the WebTextView, so that the action will
544 // be set properly.
545 mWebView.rebuildWebTextView();
546 setDefaultSelection();
547 mWebView.invalidate();
548 }
Leon Scroggins01058282009-07-30 16:33:56 -0400549 break;
550 case EditorInfo.IME_ACTION_DONE:
551 super.onEditorAction(actionCode);
552 break;
553 case EditorInfo.IME_ACTION_GO:
Leon Scroggins9e3e2d32009-12-04 14:50:18 -0500554 case EditorInfo.IME_ACTION_SEARCH:
Leon Scroggins01058282009-07-30 16:33:56 -0400555 // Send an enter and hide the soft keyboard
556 InputMethodManager.getInstance(mContext)
557 .hideSoftInputFromWindow(getWindowToken(), 0);
558 sendDomEvent(new KeyEvent(KeyEvent.ACTION_DOWN,
559 KeyEvent.KEYCODE_ENTER));
560 sendDomEvent(new KeyEvent(KeyEvent.ACTION_UP,
561 KeyEvent.KEYCODE_ENTER));
562
563 default:
564 break;
565 }
566 }
567
568 @Override
Leon Scrogginsbb107bd2009-10-23 16:18:42 -0400569 protected void onFocusChanged(boolean focused, int direction,
570 Rect previouslyFocusedRect) {
571 mFromFocusChange = true;
572 super.onFocusChanged(focused, direction, previouslyFocusedRect);
Leon Scroggins40777232011-01-18 16:49:20 -0500573 if (focused) {
574 mWebView.setActive(true);
575 } else if (!mInsideRemove) {
576 mWebView.setActive(false);
577 }
Leon Scrogginsbb107bd2009-10-23 16:18:42 -0400578 mFromFocusChange = false;
579 }
580
Leon Scroggins03e01192011-02-11 10:19:22 -0500581 // AdapterView.OnItemClickListener implementation
582
583 @Override
584 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
585 if (id == 0 && position == 0) {
586 // Blank out the text box while we wait for WebCore to fill the form.
587 replaceText("");
588 WebSettings settings = mWebView.getSettings();
589 if (mAutoFillProfileIsSet) {
590 // Call a webview method to tell WebCore to autofill the form.
591 mWebView.autoFillForm(mQueryId);
592 } else {
593 // There is no autofill profile setup yet and the user has
594 // elected to try and set one up. Call through to the
595 // embedder to action that.
596 mWebView.getWebChromeClient().setupAutoFill(
597 mHandler.obtainMessage(AUTOFILL_FORM));
598 }
599 }
600 }
601
Leon Scrogginsbb107bd2009-10-23 16:18:42 -0400602 @Override
Leon Scroggins2a307442010-10-15 14:35:21 -0400603 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
604 super.onScrollChanged(l, t, oldl, oldt);
605 lineUpScroll();
606 }
607
608 @Override
Leon Scroggins17c3bfc2009-04-14 10:57:13 -0700609 protected void onSelectionChanged(int selStart, int selEnd) {
Leon Scrogginsbb107bd2009-10-23 16:18:42 -0400610 if (!mFromWebKit && !mFromFocusChange && !mFromSetInputType
Leon Scroggins IIIdfbb1f42010-04-14 15:40:56 -0400611 && mWebView != null && !mInSetTextAndKeepSelection) {
Cary Clark243ea062009-06-25 10:49:32 -0400612 if (DebugFlags.WEB_TEXT_VIEW) {
613 Log.v(LOGTAG, "onSelectionChanged selStart=" + selStart
614 + " selEnd=" + selEnd);
615 }
Leon Scroggins17c3bfc2009-04-14 10:57:13 -0700616 mWebView.setSelection(selStart, selEnd);
Leon Scroggins2a307442010-10-15 14:35:21 -0400617 lineUpScroll();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800618 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800619 }
Leon Scroggins17c3bfc2009-04-14 10:57:13 -0700620
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800621 @Override
622 protected void onTextChanged(CharSequence s,int start,int before,int count){
623 super.onTextChanged(s, start, before, count);
624 String postChange = s.toString();
625 // Prevent calls to setText from invoking onTextChanged (since this will
626 // mean we are on a different textfield). Also prevent the change when
Leon Scrogginsd3465f62009-06-02 10:57:54 -0400627 // going from a textfield with a string of text to one with a smaller
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800628 // limit on text length from registering the onTextChanged event.
629 if (mPreChange == null || mPreChange.equals(postChange) ||
630 (mMaxLength > -1 && mPreChange.length() > mMaxLength &&
631 mPreChange.substring(0, mMaxLength).equals(postChange))) {
632 return;
633 }
634 mPreChange = postChange;
Leon Scroggins62459c02010-03-09 15:14:03 -0500635 if (0 == count) {
636 if (before > 0) {
Leon Scroggins IIIe742c082010-09-02 15:48:55 -0400637 // For this and all changes to the text, update our cache
638 updateCachedTextfield();
639 if (mGotDelete) {
640 mGotDelete = false;
641 int oldEnd = start + before;
642 if (mDelSelEnd == oldEnd
643 && (mDelSelStart == start
644 || (mDelSelStart == oldEnd && before == 1))) {
645 // If the selection is set up properly before the
646 // delete, send the DOM events.
647 sendDomEvent(new KeyEvent(KeyEvent.ACTION_DOWN,
648 KeyEvent.KEYCODE_DEL));
649 sendDomEvent(new KeyEvent(KeyEvent.ACTION_UP,
650 KeyEvent.KEYCODE_DEL));
651 return;
652 }
653 }
Leon Scroggins62459c02010-03-09 15:14:03 -0500654 // This was simply a delete or a cut, so just delete the
655 // selection.
656 mWebView.deleteSelection(start, start + before);
Leon Scroggins62459c02010-03-09 15:14:03 -0500657 }
Leon Scroggins IIIe742c082010-09-02 15:48:55 -0400658 mGotDelete = false;
Leon Scroggins62459c02010-03-09 15:14:03 -0500659 // before should never be negative, so whether it was a cut
660 // (handled above), or before is 0, in which case nothing has
661 // changed, we should return.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800662 return;
663 }
Leon Scroggins IIIe742c082010-09-02 15:48:55 -0400664 // Ensure that this flag gets cleared, since with autocorrect on, a
665 // delete key press may have a more complex result than deleting one
666 // character or the existing selection, so it will not get cleared
667 // above.
668 mGotDelete = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800669 // Find the last character being replaced. If it can be represented by
670 // events, we will pass them to native (after replacing the beginning
671 // of the changed text), so we can see javascript events.
672 // Otherwise, replace the text being changed (including the last
673 // character) in the textfield.
674 TextUtils.getChars(s, start + count - 1, start + count, mCharacter, 0);
Jeff Brown6b53e8d2010-11-10 16:03:06 -0800675 KeyCharacterMap kmap = KeyCharacterMap.load(KeyCharacterMap.VIRTUAL_KEYBOARD);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800676 KeyEvent[] events = kmap.getEvents(mCharacter);
677 boolean cannotUseKeyEvents = null == events;
678 int charactersFromKeyEvents = cannotUseKeyEvents ? 0 : 1;
679 if (count > 1 || cannotUseKeyEvents) {
680 String replace = s.subSequence(start,
681 start + count - charactersFromKeyEvents).toString();
682 mWebView.replaceTextfieldText(start, start + before, replace,
683 start + count - charactersFromKeyEvents,
684 start + count - charactersFromKeyEvents);
685 } else {
Leon Scrogginsd3465f62009-06-02 10:57:54 -0400686 // This corrects the selection which may have been affected by the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800687 // trackball or auto-correct.
Cary Clark243ea062009-06-25 10:49:32 -0400688 if (DebugFlags.WEB_TEXT_VIEW) {
689 Log.v(LOGTAG, "onTextChanged start=" + start
690 + " start + before=" + (start + before));
691 }
Cary Clark0c5924e2009-09-30 10:24:19 -0400692 if (!mInSetTextAndKeepSelection) {
693 mWebView.setSelection(start, start + before);
694 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800695 }
Leon Scroggins6e3835f2009-03-25 15:15:24 -0700696 if (!cannotUseKeyEvents) {
697 int length = events.length;
698 for (int i = 0; i < length; i++) {
699 // We never send modifier keys to native code so don't send them
700 // here either.
701 if (!KeyEvent.isModifierKey(events[i].getKeyCode())) {
702 sendDomEvent(events[i]);
703 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800704 }
705 }
Leon Scroggins6e3835f2009-03-25 15:15:24 -0700706 updateCachedTextfield();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800707 }
Leon Scrogginsd3465f62009-06-02 10:57:54 -0400708
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800709 @Override
Leon Scroggins0f5ad842009-07-17 15:08:34 -0400710 public boolean onTouchEvent(MotionEvent event) {
Leon Scroggins416a3c72009-07-22 13:05:43 -0400711 switch (event.getAction()) {
Leon Scroggins0f5ad842009-07-17 15:08:34 -0400712 case MotionEvent.ACTION_DOWN:
Leon Scroggins416a3c72009-07-22 13:05:43 -0400713 super.onTouchEvent(event);
Leon Scroggins0f5ad842009-07-17 15:08:34 -0400714 // This event may be the start of a drag, so store it to pass to the
715 // WebView if it is.
716 mDragStartX = event.getX();
717 mDragStartY = event.getY();
718 mDragStartTime = event.getEventTime();
719 mDragSent = false;
Leon Scroggins72543e12009-07-23 15:29:45 -0400720 mScrolled = false;
Leon Scroggins6679f2f2009-08-12 18:48:10 -0400721 mGotTouchDown = true;
Leon Scrogginsed908112009-12-01 10:45:24 -0500722 mHasPerformedLongClick = false;
Leon Scroggins0f5ad842009-07-17 15:08:34 -0400723 break;
724 case MotionEvent.ACTION_MOVE:
Leon Scrogginsed908112009-12-01 10:45:24 -0500725 if (mHasPerformedLongClick) {
726 mGotTouchDown = false;
727 return false;
728 }
Leon Scroggins7ae34192009-09-16 14:12:52 -0400729 int slop = ViewConfiguration.get(mContext).getScaledTouchSlop();
Leon Scroggins416a3c72009-07-22 13:05:43 -0400730 Spannable buffer = getText();
731 int initialScrollX = Touch.getInitialScrollX(this, buffer);
732 int initialScrollY = Touch.getInitialScrollY(this, buffer);
733 super.onTouchEvent(event);
Leon Scrogginsd3997e52009-09-21 14:15:18 -0400734 int dx = Math.abs(mScrollX - initialScrollX);
735 int dy = Math.abs(mScrollY - initialScrollY);
736 // Use a smaller slop when checking to see if we've moved far enough
737 // to scroll the text, because experimentally, slop has shown to be
738 // to big for the case of a small textfield.
739 int smallerSlop = slop/2;
740 if (dx > smallerSlop || dy > smallerSlop) {
Leon Scroggins2a307442010-10-15 14:35:21 -0400741 // Scrolling is handled in onScrollChanged.
Leon Scroggins72543e12009-07-23 15:29:45 -0400742 mScrolled = true;
Leon Scrogginsed908112009-12-01 10:45:24 -0500743 cancelLongPress();
Leon Scroggins0f5ad842009-07-17 15:08:34 -0400744 return true;
745 }
Leon Scroggins7ae34192009-09-16 14:12:52 -0400746 if (Math.abs((int) event.getX() - mDragStartX) < slop
747 && Math.abs((int) event.getY() - mDragStartY) < slop) {
748 // If the user has not scrolled further than slop, we should not
749 // send the drag. Instead, do nothing, and when the user lifts
750 // their finger, we will change the selection.
751 return true;
752 }
Leon Scroggins0f5ad842009-07-17 15:08:34 -0400753 if (mWebView != null) {
754 // Only want to set the initial state once.
755 if (!mDragSent) {
756 mWebView.initiateTextFieldDrag(mDragStartX, mDragStartY,
757 mDragStartTime);
758 mDragSent = true;
759 }
Leon Scroggins416a3c72009-07-22 13:05:43 -0400760 boolean scrolled = mWebView.textFieldDrag(event);
761 if (scrolled) {
Leon Scroggins72543e12009-07-23 15:29:45 -0400762 mScrolled = true;
Leon Scroggins416a3c72009-07-22 13:05:43 -0400763 cancelLongPress();
764 return true;
765 }
Leon Scroggins0f5ad842009-07-17 15:08:34 -0400766 }
767 return false;
768 case MotionEvent.ACTION_UP:
769 case MotionEvent.ACTION_CANCEL:
Leon Scroggins23cb86a2010-10-12 09:05:40 -0400770 super.onTouchEvent(event);
Leon Scrogginsed908112009-12-01 10:45:24 -0500771 if (mHasPerformedLongClick) {
772 mGotTouchDown = false;
773 return false;
774 }
Leon Scroggins72543e12009-07-23 15:29:45 -0400775 if (!mScrolled) {
776 // If the page scrolled, or the TextView scrolled, we do not
Leon Scroggins6679f2f2009-08-12 18:48:10 -0400777 // want to change the selection
778 cancelLongPress();
779 if (mGotTouchDown && mWebView != null) {
780 mWebView.touchUpOnTextField(event);
781 }
Leon Scroggins416a3c72009-07-22 13:05:43 -0400782 }
Leon Scroggins0f5ad842009-07-17 15:08:34 -0400783 // Necessary for the WebView to reset its state
784 if (mWebView != null && mDragSent) {
785 mWebView.onTouchEvent(event);
786 }
Leon Scroggins6679f2f2009-08-12 18:48:10 -0400787 mGotTouchDown = false;
Leon Scroggins0f5ad842009-07-17 15:08:34 -0400788 break;
789 default:
790 break;
791 }
792 return true;
793 }
794
795 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800796 public boolean onTrackballEvent(MotionEvent event) {
797 if (isPopupShowing()) {
798 return super.onTrackballEvent(event);
799 }
800 if (event.getAction() != MotionEvent.ACTION_MOVE) {
801 return false;
802 }
Leon Scroggins86e1fa62011-01-10 17:52:41 -0500803 Spannable text = getText();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800804 MovementMethod move = getMovementMethod();
805 if (move != null && getLayout() != null &&
806 move.onTrackballEvent(this, text, event)) {
Leon Scroggins17c3bfc2009-04-14 10:57:13 -0700807 // Selection is changed in onSelectionChanged
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800808 return true;
809 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800810 return false;
811 }
812
Leon Scrogginsed908112009-12-01 10:45:24 -0500813 @Override
814 public boolean performLongClick() {
815 mHasPerformedLongClick = true;
816 return super.performLongClick();
817 }
818
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800819 /**
Leon Scrogginsd3465f62009-06-02 10:57:54 -0400820 * Remove this WebTextView from its host WebView, and return
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800821 * focus to the host.
822 */
823 /* package */ void remove() {
824 // hide the soft keyboard when the edit text is out of focus
Leon Scroggins III71d17e42010-09-02 12:53:08 -0400825 InputMethodManager imm = InputMethodManager.getInstance(mContext);
826 if (imm.isActive(this)) {
827 imm.hideSoftInputFromWindow(getWindowToken(), 0);
828 }
Leon Scroggins40777232011-01-18 16:49:20 -0500829 mInsideRemove = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800830 mWebView.removeView(this);
831 mWebView.requestFocus();
Leon Scroggins40777232011-01-18 16:49:20 -0500832 mInsideRemove = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800833 }
834
Leon Scroggins200c13d2010-05-14 15:35:42 -0400835 @Override
Leon Scrogginsd5188652011-01-19 17:01:55 -0500836 public boolean requestRectangleOnScreen(Rect rectangle, boolean immediate) {
837 // Do nothing, since webkit will put the textfield on screen.
Leon Scroggins200c13d2010-05-14 15:35:42 -0400838 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800839 }
840
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800841 /**
842 * Send the DOM events for the specified event.
843 * @param event KeyEvent to be translated into a DOM event.
844 */
845 private void sendDomEvent(KeyEvent event) {
846 mWebView.passToJavaScript(getText().toString(), event);
847 }
848
849 /**
850 * Always use this instead of setAdapter, as this has features specific to
Leon Scrogginsd3465f62009-06-02 10:57:54 -0400851 * the WebTextView.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800852 */
853 public void setAdapterCustom(AutoCompleteAdapter adapter) {
854 if (adapter != null) {
Leon Scrogginsaa7b9d72009-12-07 13:38:07 -0500855 setInputType(getInputType()
856 | EditorInfo.TYPE_TEXT_FLAG_AUTO_COMPLETE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800857 adapter.setTextView(this);
Leon Scroggins03e01192011-02-11 10:19:22 -0500858 if (mAutoFillable) {
859 setOnItemClickListener(this);
860 } else {
861 setOnItemClickListener(null);
862 }
863 showDropDown();
864 } else {
865 dismissDropDown();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800866 }
867 super.setAdapter(adapter);
868 }
869
870 /**
871 * This is a special version of ArrayAdapter which changes its text size
872 * to match the text size of its host TextView.
873 */
874 public static class AutoCompleteAdapter extends ArrayAdapter<String> {
875 private TextView mTextView;
876
877 public AutoCompleteAdapter(Context context, ArrayList<String> entries) {
878 super(context, com.android.internal.R.layout
Leon Scroggins60344de2010-12-13 16:08:04 -0500879 .web_text_view_dropdown, entries);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800880 }
881
882 /**
883 * {@inheritDoc}
884 */
Leon Scroggins03e01192011-02-11 10:19:22 -0500885 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800886 public View getView(int position, View convertView, ViewGroup parent) {
887 TextView tv =
888 (TextView) super.getView(position, convertView, parent);
889 if (tv != null && mTextView != null) {
890 tv.setTextSize(mTextView.getTextSize());
891 }
892 return tv;
893 }
894
895 /**
896 * Set the TextView so we can match its text size.
897 */
898 private void setTextView(TextView tv) {
899 mTextView = tv;
900 }
901 }
902
903 /**
Leon Scrogginsbb107bd2009-10-23 16:18:42 -0400904 * Sets the selection when the user clicks on a textfield or textarea with
905 * the trackball or center key, or starts typing into it without clicking on
906 * it.
907 */
908 /* package */ void setDefaultSelection() {
909 Spannable text = (Spannable) getText();
910 int selection = mSingle ? text.length() : 0;
Leon Scrogginscfc29152009-12-02 10:12:57 -0500911 if (Selection.getSelectionStart(text) == selection
912 && Selection.getSelectionEnd(text) == selection) {
913 // The selection of the UI copy is set correctly, but the
914 // WebTextView still needs to inform the webkit thread to set the
915 // selection. Normally that is done in onSelectionChanged, but
916 // onSelectionChanged will not be called because the UI copy is not
917 // changing. (This can happen when the WebTextView takes focus.
918 // That onSelectionChanged was blocked because the selection set
919 // when focusing is not necessarily the desirable selection for
920 // WebTextView.)
921 if (mWebView != null) {
922 mWebView.setSelection(selection, selection);
923 }
924 } else {
925 Selection.setSelection(text, selection, selection);
926 }
Leon Scroggins IIIb24e18b2010-04-20 17:07:28 -0400927 if (mWebView != null) mWebView.incrementTextGeneration();
Leon Scrogginsbb107bd2009-10-23 16:18:42 -0400928 }
929
930 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800931 * Determine whether to use the system-wide password disguising method,
932 * or to use none.
933 * @param inPassword True if the textfield is a password field.
934 */
935 /* package */ void setInPassword(boolean inPassword) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800936 if (inPassword) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -0700937 setInputType(EditorInfo.TYPE_CLASS_TEXT | EditorInfo.
Leon Scrogginsb5ce0e02010-11-01 13:20:24 -0400938 TYPE_TEXT_VARIATION_WEB_PASSWORD);
Leon Scroggins08577672009-07-16 13:55:35 -0400939 createBackground();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800940 }
Leon Scroggins08577672009-07-16 13:55:35 -0400941 // For password fields, draw the WebTextView. For others, just show
942 // webkit's drawing.
Leon Scrogginsc26dc9f2010-11-12 12:01:34 -0500943 if (!DebugFlags.DRAW_WEBTEXTVIEW) {
944 setWillNotDraw(!inPassword);
945 }
Leon Scroggins08577672009-07-16 13:55:35 -0400946 setBackgroundDrawable(inPassword ? mBackground : null);
Leon Scroggins08577672009-07-16 13:55:35 -0400947 }
948
949 /**
950 * Private class used for the background of a password textfield.
951 */
952 private static class OutlineDrawable extends Drawable {
Leon Scroggins6e9b3272010-12-10 17:32:25 -0500953 private Paint mBackgroundPaint;
954 private Paint mOutlinePaint;
955 private float[] mLines;
956 public OutlineDrawable() {
957 mBackgroundPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
958 mBackgroundPaint.setColor(Color.WHITE);
959
960 mOutlinePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
961 mOutlinePaint.setColor(Color.BLACK);
962 mOutlinePaint.setStyle(Paint.Style.STROKE);
963
964 mLines = new float[16];
965 }
966 @Override
967 public void setBounds(int left, int top, int right, int bottom) {
968 super.setBounds(left, top, right, bottom);
Leon Scroggins6e0e0ec2011-03-01 13:17:37 -0500969 bottom--;
970 right -= 2;
Leon Scroggins6e9b3272010-12-10 17:32:25 -0500971 // Top line
972 mLines[0] = left;
973 mLines[1] = top + 1;
974 mLines[2] = right;
975 mLines[3] = top + 1;
976 // Right line
977 mLines[4] = right;
978 mLines[5] = top;
979 mLines[6] = right;
980 mLines[7] = bottom;
981 // Bottom line
982 mLines[8] = left;
983 mLines[9] = bottom;
984 mLines[10] = right;
985 mLines[11] = bottom;
986 // Left line
987 mLines[12] = left + 1;
988 mLines[13] = top;
989 mLines[14] = left + 1;
990 mLines[15] = bottom;
991 }
992 @Override
Leon Scroggins08577672009-07-16 13:55:35 -0400993 public void draw(Canvas canvas) {
Leon Scroggins08577672009-07-16 13:55:35 -0400994 // Draw the background.
Leon Scroggins6e9b3272010-12-10 17:32:25 -0500995 canvas.drawRect(getBounds(), mBackgroundPaint);
Leon Scroggins08577672009-07-16 13:55:35 -0400996 // Draw the outline.
Leon Scroggins6e9b3272010-12-10 17:32:25 -0500997 canvas.drawLines(mLines, mOutlinePaint);
Leon Scroggins08577672009-07-16 13:55:35 -0400998 }
999 // Always want it to be opaque.
Leon Scroggins6e9b3272010-12-10 17:32:25 -05001000 @Override
Leon Scroggins08577672009-07-16 13:55:35 -04001001 public int getOpacity() {
1002 return PixelFormat.OPAQUE;
1003 }
1004 // These are needed because they are abstract in Drawable.
Leon Scroggins6e9b3272010-12-10 17:32:25 -05001005 @Override
Leon Scroggins08577672009-07-16 13:55:35 -04001006 public void setAlpha(int alpha) { }
Leon Scroggins6e9b3272010-12-10 17:32:25 -05001007 @Override
Leon Scroggins08577672009-07-16 13:55:35 -04001008 public void setColorFilter(ColorFilter cf) { }
1009 }
1010
1011 /**
1012 * Create a background for the WebTextView and set up the paint for drawing
1013 * the text. This way, we can see the password transformation of the
1014 * system, which (optionally) shows the actual text before changing to dots.
1015 * The background is necessary to hide the webkit-drawn text beneath.
1016 */
1017 private void createBackground() {
1018 if (mBackground != null) {
1019 return;
1020 }
1021 mBackground = new OutlineDrawable();
1022
1023 setGravity(Gravity.CENTER_VERTICAL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001024 }
1025
Leon Scrogginsbb107bd2009-10-23 16:18:42 -04001026 @Override
1027 public void setInputType(int type) {
1028 mFromSetInputType = true;
1029 super.setInputType(type);
1030 mFromSetInputType = false;
1031 }
1032
Leon Scrogginsaa7b9d72009-12-07 13:38:07 -05001033 private void setMaxLength(int maxLength) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001034 mMaxLength = maxLength;
1035 if (-1 == maxLength) {
1036 setFilters(NO_FILTERS);
1037 } else {
1038 setFilters(new InputFilter[] {
1039 new InputFilter.LengthFilter(maxLength) });
1040 }
1041 }
1042
1043 /**
1044 * Set the pointer for this node so it can be determined which node this
Leon Scrogginsd3465f62009-06-02 10:57:54 -04001045 * WebTextView represents.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001046 * @param ptr Integer representing the pointer to the node which this
Leon Scrogginsd3465f62009-06-02 10:57:54 -04001047 * WebTextView represents.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001048 */
1049 /* package */ void setNodePointer(int ptr) {
1050 mNodePointer = ptr;
1051 }
1052
1053 /**
Leon Scrogginsd3465f62009-06-02 10:57:54 -04001054 * Determine the position and size of WebTextView, and add it to the
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001055 * WebView's view heirarchy. All parameters are presumed to be in
1056 * view coordinates. Also requests Focus and sets the cursor to not
1057 * request to be in view.
1058 * @param x x-position of the textfield.
1059 * @param y y-position of the textfield.
1060 * @param width width of the textfield.
1061 * @param height height of the textfield.
1062 */
1063 /* package */ void setRect(int x, int y, int width, int height) {
1064 LayoutParams lp = (LayoutParams) getLayoutParams();
1065 if (null == lp) {
1066 lp = new LayoutParams(width, height, x, y);
1067 } else {
1068 lp.x = x;
1069 lp.y = y;
1070 lp.width = width;
1071 lp.height = height;
1072 }
1073 if (getParent() == null) {
1074 mWebView.addView(this, lp);
1075 } else {
1076 setLayoutParams(lp);
1077 }
1078 // Set up a measure spec so a layout can always be recreated.
1079 mWidthSpec = MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY);
1080 mHeightSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001081 }
1082
1083 /**
Leon Scroggins6679f2f2009-08-12 18:48:10 -04001084 * Set the selection, and disable our onSelectionChanged action.
1085 */
1086 /* package */ void setSelectionFromWebKit(int start, int end) {
Leon Scrogginsef92e7a2009-09-02 17:47:29 -04001087 if (start < 0 || end < 0) return;
1088 Spannable text = (Spannable) getText();
1089 int length = text.length();
1090 if (start > length || end > length) return;
Leon Scroggins6679f2f2009-08-12 18:48:10 -04001091 mFromWebKit = true;
Leon Scrogginsef92e7a2009-09-02 17:47:29 -04001092 Selection.setSelection(text, start, end);
Leon Scroggins6679f2f2009-08-12 18:48:10 -04001093 mFromWebKit = false;
1094 }
1095
1096 /**
Leon Scrogginsc35f4ac2010-11-10 08:59:05 -05001097 * Update the text size according to the size of the focus candidate's text
1098 * size in mWebView. Should only be called from mWebView.
1099 */
1100 /* package */ void updateTextSize() {
1101 Assert.assertNotNull("updateTextSize should only be called from "
1102 + "mWebView, so mWebView should never be null!", mWebView);
1103 // Note that this is approximately WebView.contentToViewDimension,
1104 // without being rounded.
1105 float size = mWebView.nativeFocusCandidateTextSize()
1106 * mWebView.getScale();
1107 setTextSize(TypedValue.COMPLEX_UNIT_PX, size);
1108 }
1109
1110 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001111 * Set the text to the new string, but use the old selection, making sure
1112 * to keep it within the new string.
1113 * @param text The new text to place in the textfield.
1114 */
1115 /* package */ void setTextAndKeepSelection(String text) {
1116 mPreChange = text.toString();
Leon Scrogginsa51063c2011-01-21 17:14:36 -05001117 Editable edit = getText();
Leon Scroggins3fcf4862010-04-13 10:33:41 -04001118 int selStart = Selection.getSelectionStart(edit);
1119 int selEnd = Selection.getSelectionEnd(edit);
Cary Clark0c5924e2009-09-30 10:24:19 -04001120 mInSetTextAndKeepSelection = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001121 edit.replace(0, edit.length(), text);
Leon Scroggins3fcf4862010-04-13 10:33:41 -04001122 int newLength = edit.length();
1123 if (selStart > newLength) selStart = newLength;
1124 if (selEnd > newLength) selEnd = newLength;
1125 Selection.setSelection(edit, selStart, selEnd);
Cary Clark0c5924e2009-09-30 10:24:19 -04001126 mInSetTextAndKeepSelection = false;
Leon Scrogginsa51063c2011-01-21 17:14:36 -05001127 InputMethodManager imm = InputMethodManager.peekInstance();
1128 if (imm != null && imm.isActive(this)) {
1129 // Since the text has changed, do not allow the IME to replace the
1130 // existing text as though it were a completion.
1131 imm.restartInput(this);
1132 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001133 updateCachedTextfield();
1134 }
Leon Scrogginsd3465f62009-06-02 10:57:54 -04001135
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001136 /**
Leon Scrogginsaa7b9d72009-12-07 13:38:07 -05001137 * Called by WebView.rebuildWebTextView(). Based on the type of the <input>
1138 * element, set up the WebTextView, its InputType, and IME Options properly.
Leon Scrogginsb5ce0e02010-11-01 13:20:24 -04001139 * @param type int corresponding to enum "Type" defined in CachedInput.h.
Leon Scrogginsaa7b9d72009-12-07 13:38:07 -05001140 * Does not correspond to HTMLInputElement::InputType so this
1141 * is unaffected if that changes, and also because that has no
1142 * type corresponding to textarea (which is its own tag).
1143 */
1144 /* package */ void setType(int type) {
1145 if (mWebView == null) return;
1146 boolean single = true;
1147 boolean inPassword = false;
1148 int maxLength = -1;
Leon Scrogginsb5ce0e02010-11-01 13:20:24 -04001149 int inputType = EditorInfo.TYPE_CLASS_TEXT
1150 | EditorInfo.TYPE_TEXT_VARIATION_WEB_EDIT_TEXT;
Leon Scroggins2edd6822010-01-12 11:36:13 -05001151 int imeOptions = EditorInfo.IME_FLAG_NO_EXTRACT_UI
1152 | EditorInfo.IME_FLAG_NO_FULLSCREEN;
Leon Scrogginsb5ce0e02010-11-01 13:20:24 -04001153 if (TEXT_AREA != type
1154 && mWebView.nativeFocusCandidateHasNextTextfield()) {
1155 imeOptions |= EditorInfo.IME_FLAG_NAVIGATE_NEXT;
1156 }
Leon Scrogginsaa7b9d72009-12-07 13:38:07 -05001157 switch (type) {
Leon Scrogginsb5ce0e02010-11-01 13:20:24 -04001158 case NORMAL_TEXT_FIELD:
Leon Scrogginsaf314e22010-02-09 17:06:59 -05001159 imeOptions |= EditorInfo.IME_ACTION_GO;
1160 break;
Leon Scrogginsb5ce0e02010-11-01 13:20:24 -04001161 case TEXT_AREA:
Leon Scrogginsaa7b9d72009-12-07 13:38:07 -05001162 single = false;
Leon Scrogginsb5ce0e02010-11-01 13:20:24 -04001163 inputType |= EditorInfo.TYPE_TEXT_FLAG_MULTI_LINE
Leon Scrogginsaa7b9d72009-12-07 13:38:07 -05001164 | EditorInfo.TYPE_TEXT_FLAG_CAP_SENTENCES
1165 | EditorInfo.TYPE_TEXT_FLAG_AUTO_CORRECT;
Leon Scroggins2edd6822010-01-12 11:36:13 -05001166 imeOptions |= EditorInfo.IME_ACTION_NONE;
Leon Scrogginsaa7b9d72009-12-07 13:38:07 -05001167 break;
Leon Scrogginsb5ce0e02010-11-01 13:20:24 -04001168 case PASSWORD:
Leon Scrogginsaa7b9d72009-12-07 13:38:07 -05001169 inPassword = true;
Leon Scrogginsd5ff5db2010-02-08 13:07:41 -05001170 imeOptions |= EditorInfo.IME_ACTION_GO;
Leon Scrogginsaa7b9d72009-12-07 13:38:07 -05001171 break;
Leon Scrogginsb5ce0e02010-11-01 13:20:24 -04001172 case SEARCH:
Leon Scroggins2edd6822010-01-12 11:36:13 -05001173 imeOptions |= EditorInfo.IME_ACTION_SEARCH;
Leon Scrogginsaa7b9d72009-12-07 13:38:07 -05001174 break;
Leon Scrogginsb5ce0e02010-11-01 13:20:24 -04001175 case EMAIL:
1176 inputType = EditorInfo.TYPE_TEXT_VARIATION_WEB_EMAIL_ADDRESS;
Leon Scrogginsd5ff5db2010-02-08 13:07:41 -05001177 imeOptions |= EditorInfo.IME_ACTION_GO;
Leon Scrogginsaa7b9d72009-12-07 13:38:07 -05001178 break;
Leon Scrogginsb5ce0e02010-11-01 13:20:24 -04001179 case NUMBER:
Leon Scrogginsd5ff5db2010-02-08 13:07:41 -05001180 inputType |= EditorInfo.TYPE_CLASS_NUMBER;
1181 // Number and telephone do not have both a Tab key and an
1182 // action, so set the action to NEXT
1183 imeOptions |= EditorInfo.IME_ACTION_NEXT;
Leon Scrogginsaa7b9d72009-12-07 13:38:07 -05001184 break;
Leon Scrogginsb5ce0e02010-11-01 13:20:24 -04001185 case TELEPHONE:
Leon Scrogginsd5ff5db2010-02-08 13:07:41 -05001186 inputType |= EditorInfo.TYPE_CLASS_PHONE;
1187 imeOptions |= EditorInfo.IME_ACTION_NEXT;
Leon Scrogginsaa7b9d72009-12-07 13:38:07 -05001188 break;
Leon Scrogginsb5ce0e02010-11-01 13:20:24 -04001189 case URL:
Leon Scrogginsd5ff5db2010-02-08 13:07:41 -05001190 // TYPE_TEXT_VARIATION_URI prevents Tab key from showing, so
1191 // exclude it for now.
1192 imeOptions |= EditorInfo.IME_ACTION_GO;
Leon Scrogginsaa7b9d72009-12-07 13:38:07 -05001193 break;
1194 default:
Leon Scrogginsaf314e22010-02-09 17:06:59 -05001195 imeOptions |= EditorInfo.IME_ACTION_GO;
Leon Scrogginsaa7b9d72009-12-07 13:38:07 -05001196 break;
1197 }
Leon Scroggins3a503392010-01-06 17:04:38 -05001198 setHint(null);
Leon Scrogginsaa7b9d72009-12-07 13:38:07 -05001199 if (single) {
Leon Scroggins3a503392010-01-06 17:04:38 -05001200 mWebView.requestLabel(mWebView.nativeFocusCandidateFramePointer(),
1201 mNodePointer);
Leon Scrogginsaa7b9d72009-12-07 13:38:07 -05001202 maxLength = mWebView.nativeFocusCandidateMaxLength();
Leon Scrogginsae0238c2011-01-05 15:12:55 -05001203 boolean autoComplete = mWebView.nativeFocusCandidateIsAutoComplete();
1204 if (type != PASSWORD && (mAutoFillable || autoComplete)) {
Leon Scrogginsaa7b9d72009-12-07 13:38:07 -05001205 String name = mWebView.nativeFocusCandidateName();
1206 if (name != null && name.length() > 0) {
Leon Scrogginsae0238c2011-01-05 15:12:55 -05001207 mWebView.requestFormData(name, mNodePointer, mAutoFillable,
1208 autoComplete);
Leon Scrogginsaa7b9d72009-12-07 13:38:07 -05001209 }
1210 }
Leon Scrogginsaa7b9d72009-12-07 13:38:07 -05001211 }
1212 mSingle = single;
1213 setMaxLength(maxLength);
1214 setHorizontallyScrolling(single);
1215 setInputType(inputType);
Leon Scroggins2edd6822010-01-12 11:36:13 -05001216 setImeOptions(imeOptions);
Leon Scrogginsaa7b9d72009-12-07 13:38:07 -05001217 setInPassword(inPassword);
1218 AutoCompleteAdapter adapter = null;
1219 setAdapterCustom(adapter);
1220 }
1221
1222 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001223 * Update the cache to reflect the current text.
1224 */
1225 /* package */ void updateCachedTextfield() {
1226 mWebView.updateCachedTextfield(getText().toString());
1227 }
Ben Murdoch57914382010-11-16 11:50:39 +00001228
1229 /* package */ void setAutoFillProfileIsSet(boolean autoFillProfileIsSet) {
1230 mAutoFillProfileIsSet = autoFillProfileIsSet;
1231 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001232}