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

Commit 311882ab authored by Daniel Lehmann's avatar Daniel Lehmann
Browse files

Make search button focus the search field while in search mode

 - Also removed a totally unused class
 - Also turned an enum into an abstract class

Bug:5188996
Change-Id: I0dde1e49b360459f71196b151160a25d24d21343
parent 4858f5fd
Loading
Loading
Loading
Loading
+8 −3
Original line number Original line Diff line number Diff line
@@ -42,11 +42,13 @@ import android.widget.SearchView.OnQueryTextListener;
public class ActionBarAdapter implements OnQueryTextListener, OnCloseListener {
public class ActionBarAdapter implements OnQueryTextListener, OnCloseListener {


    public interface Listener {
    public interface Listener {
        public enum Action {
        public abstract class Action {
            CHANGE_SEARCH_QUERY, START_SEARCH_MODE, STOP_SEARCH_MODE
            public static final int CHANGE_SEARCH_QUERY = 0;
            public static final int START_SEARCH_MODE = 1;
            public static final int STOP_SEARCH_MODE = 2;
        }
        }


        void onAction(Action action);
        void onAction(int action);


        /**
        /**
         * Called when the user selects a tab.  The new tab can be obtained using
         * Called when the user selects a tab.  The new tab can be obtained using
@@ -238,6 +240,9 @@ public class ActionBarAdapter implements OnQueryTextListener, OnCloseListener {
            } else {
            } else {
                mSearchView.setQuery(null, false);
                mSearchView.setQuery(null, false);
            }
            }
        } else if (flag) {
            // Everything is already set up. Still make sure the keyboard is up
            if (mSearchView != null) setFocusOnSearchView();
        }
        }
    }
    }


+4 −4
Original line number Original line Diff line number Diff line
@@ -615,20 +615,20 @@ public class PeopleActivity extends ContactsActivity
     * Handler for action bar actions.
     * Handler for action bar actions.
     */
     */
    @Override
    @Override
    public void onAction(Action action) {
    public void onAction(int action) {
        switch (action) {
        switch (action) {
            case START_SEARCH_MODE:
            case ActionBarAdapter.Listener.Action.START_SEARCH_MODE:
                // Tell the fragments that we're in the search mode
                // Tell the fragments that we're in the search mode
                configureFragments(false /* from request */);
                configureFragments(false /* from request */);
                updateFragmentsVisibility();
                updateFragmentsVisibility();
                invalidateOptionsMenu();
                invalidateOptionsMenu();
                break;
                break;
            case STOP_SEARCH_MODE:
            case ActionBarAdapter.Listener.Action.STOP_SEARCH_MODE:
                setQueryTextToFragment("");
                setQueryTextToFragment("");
                updateFragmentsVisibility();
                updateFragmentsVisibility();
                invalidateOptionsMenu();
                invalidateOptionsMenu();
                break;
                break;
            case CHANGE_SEARCH_QUERY:
            case ActionBarAdapter.Listener.Action.CHANGE_SEARCH_QUERY:
                setQueryTextToFragment(mActionBarAdapter.getQueryString());
                setQueryTextToFragment(mActionBarAdapter.getQueryString());
                break;
                break;
            default:
            default:
+0 −141
Original line number Original line Diff line number Diff line
/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.contacts.widget;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

/**
 * A custom text editor that helps automatically dismiss the activity along with the soft
 * keyboard.
 */
public class SearchEditText extends EditText implements OnEditorActionListener, TextWatcher {

    private boolean mMaginfyingGlassEnabled = true;
    private Drawable mMagnifyingGlass;
    private OnFilterTextListener mListener;

    private boolean mMagnifyingGlassShown;

    public interface OnFilterTextListener {
        void onFilterChange(String queryString);
        void onCancelSearch();
    }

    public SearchEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        addTextChangedListener(this);
        setOnEditorActionListener(this);
        mMagnifyingGlass = getCompoundDrawables()[2];
        setCompoundDrawables(null, null, null, null);
    }

    public boolean isMaginfyingGlassEnabled() {
        return mMaginfyingGlassEnabled;
    }

    public void setMaginfyingGlassEnabled(boolean flag) {
        this.mMaginfyingGlassEnabled = flag;
    }

    public void setOnFilterTextListener(OnFilterTextListener listener) {
        this.mListener = listener;
    }

    /**
     * Conditionally shows a magnifying glass icon on the right side of the text field
     * when the text it empty.
     */
    @Override
    public boolean onPreDraw() {
        boolean emptyText = TextUtils.isEmpty(getText());
        if (mMagnifyingGlassShown != emptyText) {
            mMagnifyingGlassShown = emptyText;
            if (mMagnifyingGlassShown && mMaginfyingGlassEnabled) {
                setCompoundDrawables(null, null, mMagnifyingGlass, null);
            } else {
                setCompoundDrawables(null, null, null, null);
            }
            return false;
        }
        return super.onPreDraw();
    }

    /**
     * Dismisses the search UI along with the keyboard if the filter text is empty.
     */
    @Override
    public boolean onKeyPreIme(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && TextUtils.isEmpty(getText()) && mListener != null) {
            mListener.onCancelSearch();
            return true;
        }
        return false;
    }

    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    }

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

    /**
     * Event handler for search UI.
     */
    public void afterTextChanged(Editable s) {
        if (mListener != null) {
            mListener.onFilterChange(trim(s));
        }
    }

    private String trim(Editable s) {
        return s.toString().trim();
    }

    /**
     * Event handler for search UI.
     */
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_DONE) {
            hideSoftKeyboard();
            if (TextUtils.isEmpty(trim(getText())) && mListener != null) {
                mListener.onCancelSearch();
            }
            return true;
        }
        return false;
    }

    private void hideSoftKeyboard() {
        // Hide soft keyboard, if visible
        InputMethodManager inputMethodManager = (InputMethodManager)
                getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(getWindowToken(), 0);
    }

}