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