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

Commit ccec37e1 authored by Ralston Da Silva's avatar Ralston Da Silva
Browse files

Removing hidden api usage from AutoCompleteTextView

doBeforeTextChanged() and doAfterTextChanged() were being called to
trigger a refresh of the autocomplete suggestions when the refresh
was not automatically triggered by text input.

An androidx usage example is in SearchView, where we need to trigger
the autocomplete results manually.
https://cs.corp.google.com/aosp-kitkat/frameworks/support/v7/appcompat/src/android/support/v7/widget/SearchView.java?rcl=197708fc4fc0a6a5ede2da2479613382b561f028&l=1199
https://cs.corp.google.com/aosp-kitkat/frameworks/support/v7/appcompat/src/android/support/v7/widget/SearchView.java?rcl=197708fc4fc0a6a5ede2da2479613382b561f028&l=895

I feel this is a valid use case and added a new public api refreshAutoCompleteResults()

Bug: 123768711
Bug: 123768433

Test: Added new CTS tests for newly added api

Change-Id: Icfa6287889b4f63c00aa2ad6450e47942a9adda5
parent 2fb540e0
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -55248,6 +55248,7 @@ package android.widget {
    method public void performCompletion();
    method protected void performFiltering(CharSequence, int);
    method public void performValidation();
    method public final void refreshAutoCompleteResults();
    method protected void replaceText(CharSequence);
    method public <T extends android.widget.ListAdapter & android.widget.Filterable> void setAdapter(T);
    method public void setCompletionHint(CharSequence);
+51 −29
Original line number Diff line number Diff line
@@ -126,7 +126,7 @@ public class AutoCompleteTextView extends EditText implements Filter.FilterListe
    private boolean mDropDownDismissedOnCompletion = true;

    private int mLastKeyCode = KeyEvent.KEYCODE_UNKNOWN;
    private boolean mOpenBefore;
    private MyWatcher mAutoCompleteTextWatcher;

    private Validator mValidator = null;

@@ -302,7 +302,8 @@ public class AutoCompleteTextView extends EditText implements Filter.FilterListe

        setFocusable(true);

        addTextChangedListener(new MyWatcher());
        mAutoCompleteTextWatcher = new MyWatcher();
        addTextChangedListener(mAutoCompleteTextWatcher);

        mPassThroughClickListener = new PassThroughClickListener();
        super.setOnClickListener(mPassThroughClickListener);
@@ -872,24 +873,13 @@ public class AutoCompleteTextView extends EditText implements Filter.FilterListe
        return getText().length() >= mThreshold;
    }

    /**
     * This is used to watch for edits to the text view.  Note that we call
     * to methods on the auto complete text view class so that we can access
     * private vars without going through thunks.
     */


    /** This is used to watch for edits to the text view. */
    private class MyWatcher implements TextWatcher {
        public void afterTextChanged(Editable s) {
            doAfterTextChanged();
        }
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            doBeforeTextChanged();
        }
        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }
    }
        private boolean mOpenBefore;

    @UnsupportedAppUsage
    void doBeforeTextChanged() {
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            if (mBlockCompletion) return;

            // when text is changed, inserted or deleted, we attempt to show
@@ -898,19 +888,51 @@ public class AutoCompleteTextView extends EditText implements Filter.FilterListe
            if (DEBUG) Log.v(TAG, "before text changed: open=" + mOpenBefore);
        }

    @UnsupportedAppUsage
    void doAfterTextChanged() {
        public void afterTextChanged(Editable s) {
            if (mBlockCompletion) return;

            // if the list was open before the keystroke, but closed afterwards,
            // then something in the keystroke processing (an input filter perhaps)
            // called performCompletion() and we shouldn't do any more processing.
        if (DEBUG) Log.v(TAG, "after text changed: openBefore=" + mOpenBefore
            if (DEBUG) {
                Log.v(TAG, "after text changed: openBefore=" + mOpenBefore
                        + " open=" + isPopupShowing());
        if (mOpenBefore && !isPopupShowing()) {
            return;
            }

            if (mOpenBefore && !isPopupShowing()) return;

            refreshAutoCompleteResults();
        }

        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }
    }

    /**
     * This function is deprecated. Please use {@link #refreshAutoCompleteResults} instead.
     * Note: Remove {@link #mAutoCompleteTextWatcher} after removing this function.
     */
    @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P)
    void doBeforeTextChanged() {
        mAutoCompleteTextWatcher.beforeTextChanged(null, 0, 0, 0);
    }

    /**
     * This function is deprecated. Please use {@link #refreshAutoCompleteResults} instead.
     * Note: Remove {@link #mAutoCompleteTextWatcher} after removing this function.
     */
    @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P)
    void doAfterTextChanged() {
        mAutoCompleteTextWatcher.afterTextChanged(null);
    }

    /**
     * Refreshes the auto complete results. You usually shouldn't have to manually refresh the
     * AutoCompleteResults as this is done automatically whenever the text changes. However if the
     * results are not available and have to be fetched, you can call this function after fetching
     * the results.
     */
    public final void refreshAutoCompleteResults() {
        // the drop down is shown only when a minimum number of characters
        // was typed in the text view
        if (enoughToFilter()) {