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

Commit 87d61f22 authored by Bjorn Bringert's avatar Bjorn Bringert
Browse files

Handle BACK key with focus in search ACTV correctly

Fixes http://b/issue?id=2124700
"Back key kills pivoted-in search, should go back to QSB"

Change-Id: Ib54c5de158fc362faa3f0fef553ee403437635ae
parent 9352b9fb
Loading
Loading
Loading
Loading
+40 −75
Original line number Original line Diff line number Diff line
@@ -739,11 +739,6 @@ public class SearchDialog extends Dialog implements OnItemClickListener, OnItemS
            return false;
            return false;
        }
        }


        // handle back key to go back to previous searchable, etc.
        if (handleBackKey(keyCode, event)) {
            return true;
        }
        
        if (keyCode == KeyEvent.KEYCODE_SEARCH) {
        if (keyCode == KeyEvent.KEYCODE_SEARCH) {
            // Consume search key for later use.
            // Consume search key for later use.
            return true;
            return true;
@@ -757,7 +752,7 @@ public class SearchDialog extends Dialog implements OnItemClickListener, OnItemS
            return true;
            return true;
        }
        }


        return false;
        return super.onKeyDown(keyCode, event);
    }
    }
    
    
    @Override
    @Override
@@ -767,11 +762,6 @@ public class SearchDialog extends Dialog implements OnItemClickListener, OnItemS
            return false;
            return false;
        }
        }


        // handle back key to go back to previous searchable, etc.
        if (handleBackKey(keyCode, event)) {
            return true;
        }
        
        if (keyCode == KeyEvent.KEYCODE_SEARCH && event.isTracking()
        if (keyCode == KeyEvent.KEYCODE_SEARCH && event.isTracking()
                && !event.isCanceled()) {
                && !event.isCanceled()) {
            // If the search key is pressed, toggle between global and in-app search. If we are
            // If the search key is pressed, toggle between global and in-app search. If we are
@@ -780,7 +770,7 @@ public class SearchDialog extends Dialog implements OnItemClickListener, OnItemS
            return toggleGlobalSearch();
            return toggleGlobalSearch();
        }
        }


        return false;
        return super.onKeyUp(keyCode, event);
    }
    }
    
    
    /**
    /**
@@ -1487,6 +1477,13 @@ public class SearchDialog extends Dialog implements OnItemClickListener, OnItemS
        mSearchAutoComplete.setListSelection(index);
        mSearchAutoComplete.setListSelection(index);
    }
    }


    /**
     * Checks if there are any previous searchable components in the history stack.
     */
    private boolean hasPreviousComponent() {
        return mPreviousComponents != null && !mPreviousComponents.isEmpty();
    }

    /**
    /**
     * Saves the previous component that was searched, so that we can go
     * Saves the previous component that was searched, so that we can go
     * back to it.
     * back to it.
@@ -1505,14 +1502,10 @@ public class SearchDialog extends Dialog implements OnItemClickListener, OnItemS
     *         no previous component.
     *         no previous component.
     */
     */
    private ComponentName popPreviousComponent() {
    private ComponentName popPreviousComponent() {
        if (mPreviousComponents == null) {
        if (!hasPreviousComponent()) {
            return null;
            return null;
        }
        }
        int size = mPreviousComponents.size();
        return mPreviousComponents.remove(mPreviousComponents.size() - 1);
        if (size == 0) {
            return null;
        }
        return mPreviousComponents.remove(size - 1);
    }
    }
    
    
    /**
    /**
@@ -1520,13 +1513,12 @@ public class SearchDialog extends Dialog implements OnItemClickListener, OnItemS
     * 
     * 
     * @return <code>true</code> if there was a previous component that we could go back to.
     * @return <code>true</code> if there was a previous component that we could go back to.
     */
     */
    private boolean backToPreviousComponent(boolean doIt) {
    private boolean backToPreviousComponent() {
        ComponentName previous = popPreviousComponent();
        ComponentName previous = popPreviousComponent();
        if (previous == null) {
        if (previous == null) {
            return false;
            return false;
        }
        }


        if (doIt) {
        if (!show(previous, mAppSearchData, false)) {
        if (!show(previous, mAppSearchData, false)) {
            Log.w(LOG_TAG, "Failed to switch to source " + previous);
            Log.w(LOG_TAG, "Failed to switch to source " + previous);
            return false;
            return false;
@@ -1537,8 +1529,6 @@ public class SearchDialog extends Dialog implements OnItemClickListener, OnItemS
        // the source that we are now going back to?
        // the source that we are now going back to?
        String query = mSearchAutoComplete.getText().toString();
        String query = mSearchAutoComplete.getText().toString();
        setUserQuery(query);
        setUserQuery(query);
        }
        
        return true;
        return true;
    }
    }
    
    
@@ -1763,72 +1753,47 @@ public class SearchDialog extends Dialog implements OnItemClickListener, OnItemS
         */
         */
        @Override
        @Override
        public boolean onKeyPreIme(int keyCode, KeyEvent event) {
        public boolean onKeyPreIme(int keyCode, KeyEvent event) {
            if (DBG) Log.d(LOG_TAG, "onKeyPreIme(" + keyCode + "," + event + ")");
            if (mSearchDialog.mSearchable == null) {
            if (mSearchDialog.mSearchable == null) {
                return false;
                return false;
            }
            }
            if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (keyCode == KeyEvent.KEYCODE_BACK) {
                if (event.getAction() == KeyEvent.ACTION_DOWN
                if (event.getAction() == KeyEvent.ACTION_DOWN
                        && event.getRepeatCount() == 0) {
                        && event.getRepeatCount() == 0) {
                    // We release the back key, might we want to do
                    if (mSearchDialog.hasPreviousComponent() || isDismissingKeyboardPointless()) {
                    // something before the IME?
                    if (mSearchDialog.backToPreviousComponent(false)) {
                        getKeyDispatcherState().startTracking(event, this);
                        return true;
                    }
                    if (isInputMethodNotNeeded() ||
                            (isEmpty() && getDropDownChildCount() >= getAdapterCount())) {
                        getKeyDispatcherState().startTracking(event, this);
                        getKeyDispatcherState().startTracking(event, this);
                        return true;
                        return true;
                    }
                    }
                    return false; // will dismiss soft keyboard if necessary
                } else if (event.getAction() == KeyEvent.ACTION_UP
                } else if (event.getAction() == KeyEvent.ACTION_UP
                        && event.isTracking() && !event.isCanceled()) {
                        && event.isTracking() && !event.isCanceled()) {
                    if (mSearchDialog.backToPreviousComponent(true)) {
                    if (mSearchDialog.backToPreviousComponent()) {
                        return true;
                        return true;
                    }
                    } else if (isDismissingKeyboardPointless()) {
                    // If the drop-down obscures the keyboard, the user wouldn't see anything
                    // happening when pressing back, so we dismiss the entire dialog instead.
                    //
                    // also: if there is no text entered, we also want to dismiss the whole dialog,
                    // not just the soft keyboard.  the exception to this is if there are shortcuts
                    // that aren't displayed (e.g are being obscured by the soft keyboard); in that
                    // case we want to dismiss the soft keyboard so the user can see the rest of the
                    // shortcuts.
                    if (isInputMethodNotNeeded() ||
                            (isEmpty() && getDropDownChildCount() >= getAdapterCount())) {
                        mSearchDialog.cancel();
                        mSearchDialog.cancel();
                        return true;
                        return true;
                    }
                    }
                    return false; // will dismiss soft keyboard if necessary
                }
                }
            }
            }
            return false;
            return false;
        }
        }


        // If the drop-down obscures the keyboard, or if the drop-down shows all suggestions,
        // dismissing the keyboard is pointless, so we dismiss the entire dialog instead.
        private boolean isDismissingKeyboardPointless() {
            return (isInputMethodNotNeeded() || getDropDownChildCount() >= getAdapterCount());
        }

        private int getAdapterCount() {
        private int getAdapterCount() {
            final ListAdapter adapter = getAdapter();
            final ListAdapter adapter = getAdapter();
            return adapter == null ? 0 : adapter.getCount();
            return adapter == null ? 0 : adapter.getCount();
        }
        }
    }
    }


    protected boolean handleBackKey(int keyCode, KeyEvent event) {
    @Override
        if (keyCode == KeyEvent.KEYCODE_BACK) {
    public void onBackPressed() {
            if (event.getAction() == KeyEvent.ACTION_DOWN
        if (!backToPreviousComponent()) {
                    && event.getRepeatCount() == 0) {
                // Consume the event, to get an up at which point we execute.
                event.startTracking();
                return true;
            }
            if (event.getAction() == KeyEvent.ACTION_UP && event.isTracking()
                    && !event.isCanceled()) {
                if (backToPreviousComponent(true)) {
                    return true;
                }
            cancel();
            cancel();
        }
        }
            return true;
        }
        return false;
    }
    }


    /**
    /**