summaryrefslogtreecommitdiffstats
path: root/src/com/android/launcher3/allapps/DefaultAppSearchController.java
blob: 83b920589baf46c61589169dca858b2eebf86e1d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/*
 * Copyright (C) 2015 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.launcher3.allapps;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.TextView;
import com.android.launcher3.R;
import com.android.launcher3.Utilities;
import com.android.launcher3.util.Thunk;

import java.util.List;


/**
 * The default search controller.
 */
final class DefaultAppSearchController extends AllAppsSearchBarController
        implements TextWatcher, TextView.OnEditorActionListener, View.OnClickListener {

    private static final boolean ALLOW_SINGLE_APP_LAUNCH = true;

    private static final int FADE_IN_DURATION = 175;
    private static final int FADE_OUT_DURATION = 100;
    private static final int SEARCH_TRANSLATION_X_DP = 18;

    private final Context mContext;
    @Thunk final InputMethodManager mInputMethodManager;

    private DefaultAppSearchAlgorithm mSearchManager;

    private ViewGroup mContainerView;
    private View mSearchView;
    @Thunk View mSearchBarContainerView;
    private View mSearchButtonView;
    private View mDismissSearchButtonView;
    @Thunk AllAppsSearchEditView mSearchBarEditView;
    @Thunk AllAppsRecyclerView mAppsRecyclerView;
    @Thunk Runnable mFocusRecyclerViewRunnable = new Runnable() {
        @Override
        public void run() {
            mAppsRecyclerView.requestFocus();
        }
    };

    public DefaultAppSearchController(Context context, ViewGroup containerView,
            AllAppsRecyclerView appsRecyclerView) {
        mContext = context;
        mInputMethodManager = (InputMethodManager)
                mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
        mContainerView = containerView;
        mAppsRecyclerView = appsRecyclerView;
    }

    @Override
    public View getView(ViewGroup parent) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        mSearchView = inflater.inflate(R.layout.all_apps_search_bar, parent, false);
        mSearchView.setOnClickListener(this);

        mSearchButtonView = mSearchView.findViewById(R.id.search_button);
        mSearchBarContainerView = mSearchView.findViewById(R.id.search_container);
        mDismissSearchButtonView = mSearchBarContainerView.findViewById(R.id.dismiss_search_button);
        mDismissSearchButtonView.setOnClickListener(this);
        mSearchBarEditView = (AllAppsSearchEditView)
                mSearchBarContainerView.findViewById(R.id.search_box_input);
        mSearchBarEditView.addTextChangedListener(this);
        mSearchBarEditView.setOnEditorActionListener(this);
        mSearchBarEditView.setOnBackKeyListener(
                new AllAppsSearchEditView.OnBackKeyListener() {
                    @Override
                    public void onBackKey() {
                        // Only hide the search field if there is no query, or if there
                        // are no filtered results
                        String query = Utilities.trim(
                                mSearchBarEditView.getEditableText().toString());
                        if (query.isEmpty() || mApps.hasNoFilteredResults()) {
                            hideSearchField(true, mFocusRecyclerViewRunnable);
                        }
                    }
                });
        return mSearchView;
    }

    @Override
    public void focusSearchField() {
        mSearchBarEditView.requestFocus();
        showSearchField();
    }

    @Override
    public boolean isSearchFieldFocused() {
        return mSearchBarEditView.isFocused();
    }

    @Override
    protected void onInitialize() {
        mSearchManager = new DefaultAppSearchAlgorithm(mApps.getApps());
    }

    @Override
    public void reset() {
        hideSearchField(false, null);
    }

    @Override
    public boolean shouldShowPredictionBar() {
        return false;
    }

    @Override
    public void onClick(View v) {
        if (v == mSearchView) {
            showSearchField();
        } else if (v == mDismissSearchButtonView) {
            hideSearchField(true, mFocusRecyclerViewRunnable);
        }
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        // Do nothing
    }

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

    @Override
    public void afterTextChanged(final Editable s) {
        String query = s.toString();
        if (query.isEmpty()) {
            mSearchManager.cancel(true);
            mCb.clearSearchResult();
        } else {
            mSearchManager.cancel(false);
            mSearchManager.doSearch(query, mCb);
        }
    }

    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        // Skip if we disallow app-launch-on-enter
        if (!ALLOW_SINGLE_APP_LAUNCH) {
            return false;
        }
        // Skip if it's not the right action
        if (actionId != EditorInfo.IME_ACTION_DONE) {
            return false;
        }
        // Skip if there isn't exactly one item
        if (mApps.getSize() != 1) {
            return false;
        }
        // If there is exactly one icon, then quick-launch it
        List<AlphabeticalAppsList.AdapterItem> items = mApps.getAdapterItems();
        for (int i = 0; i < items.size(); i++) {
            AlphabeticalAppsList.AdapterItem item = items.get(i);
            if (item.viewType == AllAppsGridAdapter.ICON_VIEW_TYPE) {
                mAppsRecyclerView.getChildAt(i).performClick();
                mInputMethodManager.hideSoftInputFromWindow(
                        mContainerView.getWindowToken(), 0);
                return true;
            }
        }
        return false;
    }

    /**
     * Focuses the search field.
     */
    private void showSearchField() {
        // Show the search bar and focus the search
        final int translationX = Utilities.pxFromDp(SEARCH_TRANSLATION_X_DP,
                mContext.getResources().getDisplayMetrics());
        mSearchBarContainerView.setVisibility(View.VISIBLE);
        mSearchBarContainerView.setAlpha(0f);
        mSearchBarContainerView.setTranslationX(translationX);
        mSearchBarContainerView.animate()
                .alpha(1f)
                .translationX(0)
                .setDuration(FADE_IN_DURATION)
                .withLayer()
                .withEndAction(new Runnable() {
                    @Override
                    public void run() {
                        mSearchBarEditView.requestFocus();
                        mInputMethodManager.showSoftInput(mSearchBarEditView,
                                InputMethodManager.SHOW_IMPLICIT);
                    }
                });
        mSearchButtonView.animate()
                .alpha(0f)
                .translationX(-translationX)
                .setDuration(FADE_OUT_DURATION)
                .withLayer();
    }

    /**
     * Unfocuses the search field.
     */
    @Thunk void hideSearchField(boolean animated, final Runnable postAnimationRunnable) {
        mSearchManager.cancel(true);

        final boolean resetTextField = mSearchBarEditView.getText().toString().length() > 0;
        final int translationX = Utilities.pxFromDp(SEARCH_TRANSLATION_X_DP,
                mContext.getResources().getDisplayMetrics());
        if (animated) {
            // Hide the search bar and focus the recycler view
            mSearchBarContainerView.animate()
                    .alpha(0f)
                    .translationX(0)
                    .setDuration(FADE_IN_DURATION)
                    .withLayer()
                    .withEndAction(new Runnable() {
                        @Override
                        public void run() {
                            mSearchBarContainerView.setVisibility(View.INVISIBLE);
                            if (resetTextField) {
                                mSearchBarEditView.setText("");
                            }
                            mCb.clearSearchResult();
                            if (postAnimationRunnable != null) {
                                postAnimationRunnable.run();
                            }
                        }
                    });
            mSearchButtonView.setTranslationX(-translationX);
            mSearchButtonView.animate()
                    .alpha(1f)
                    .translationX(0)
                    .setDuration(FADE_OUT_DURATION)
                    .withLayer();
        } else {
            mSearchBarContainerView.setVisibility(View.INVISIBLE);
            if (resetTextField) {
                mSearchBarEditView.setText("");
            }
            mCb.clearSearchResult();
            mSearchButtonView.setAlpha(1f);
            mSearchButtonView.setTranslationX(0f);
            if (postAnimationRunnable != null) {
                postAnimationRunnable.run();
            }
        }
        mInputMethodManager.hideSoftInputFromWindow(mContainerView.getWindowToken(), 0);
    }
}