Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 4a7199ae authored by Gilles Debunne's avatar Gilles Debunne
Browse files

Suggestions are dismissed by back key

Also changed back when in text selection mode: selection mode / suggestion are hidden
first, before the IME is dismissed.

Bug 4541805.

Change-Id: I71ee7fe2fdf9e882d059482aa29dd45ade3e5dbe
parent 2d6bb338
Loading
Loading
Loading
Loading
+53 −8
Original line number Diff line number Diff line
@@ -16,11 +16,6 @@

package android.widget;

import com.android.internal.util.FastMath;
import com.android.internal.widget.EditableInputConnection;

import org.xmlpull.v1.XmlPullParserException;

import android.R;
import android.content.ClipData;
import android.content.ClipData.Item;
@@ -132,6 +127,11 @@ import android.view.inputmethod.InputConnection;
import android.view.inputmethod.InputMethodManager;
import android.widget.RemoteViews.RemoteView;

import com.android.internal.util.FastMath;
import com.android.internal.widget.EditableInputConnection;

import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;
import java.lang.ref.WeakReference;
import java.text.BreakIterator;
@@ -4723,6 +4723,40 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
        return getExtendedPaddingTop() + voffset + mLayout.getLineBaseline(0);
    }

    @Override
    public boolean onKeyPreIme(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            boolean areSuggestionsShown = areSuggestionsShown();
            boolean isInSelectionMode = mSelectionActionMode != null;

            if (areSuggestionsShown || isInSelectionMode) {
                if (event.getAction() == KeyEvent.ACTION_DOWN && event.getRepeatCount() == 0) {
                    KeyEvent.DispatcherState state = getKeyDispatcherState();
                    if (state != null) {
                        state.startTracking(event, this);
                    }
                    return true;
                } else if (event.getAction() == KeyEvent.ACTION_UP) {
                    KeyEvent.DispatcherState state = getKeyDispatcherState();
                    if (state != null) {
                        state.handleUpEvent(event);
                    }
                    if (event.isTracking() && !event.isCanceled()) {
                        if (areSuggestionsShown) {
                            hideSuggestions();
                            return true;
                        }
                        if (isInSelectionMode) {
                            stopSelectionActionMode();
                            return true;
                        }
                    }
                }
            }
        }
        return super.onKeyPreIme(keyCode, event);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        int which = doKeyDown(keyCode, event, null);
@@ -4875,6 +4909,10 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener

                // Has to be done on key down (and not on key up) to correctly be intercepted.
            case KeyEvent.KEYCODE_BACK:
                if (areSuggestionsShown()) {
                    hideSuggestions();
                    return -1;
                }
                if (mSelectionActionMode != null) {
                    stopSelectionActionMode();
                    return -1;
@@ -7245,7 +7283,7 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
        // Because of View recycling in ListView, there is no easy way to know when a TextView with
        // selection becomes visible again. Until a better solution is found, stop text selection
        // mode (if any) as soon as this TextView is recycled.
        stopSelectionActionMode();
        hideControllers();
    }
    
    @Override
@@ -7520,8 +7558,7 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
                if (!selectAllGotFocus && hasSelection()) {
                    startSelectionActionMode();
                } else {
                    stopSelectionActionMode();
                    hideSuggestions();
                    hideControllers();
                    if (hasInsertionController() && !selectAllGotFocus && mText.length() > 0) {
                        getInsertionController().show();
                    }
@@ -8668,6 +8705,10 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
            mContainer.dismiss();
        }

        public boolean isShowing() {
            return mContainer.isShowing();
        }

        @Override
        public void onClick(View view) {
            if (view instanceof TextView) {
@@ -8794,6 +8835,10 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
        }
    }

    boolean areSuggestionsShown() {
        return mSuggestionsPopupWindow != null && mSuggestionsPopupWindow.isShowing();
    } 

    /**
     * Some parts of the text can have alternate suggestion text attached. This is typically done by
     * the IME by adding {@link SuggestionSpan}s to the text.