1 /* 2 * Copyright (C) 2021 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.launcher3.widget.picker.search; 18 19 import static android.view.View.GONE; 20 import static android.view.View.VISIBLE; 21 22 import android.text.Editable; 23 import android.text.TextWatcher; 24 import android.util.Log; 25 import android.view.KeyEvent; 26 import android.view.View; 27 import android.widget.ImageButton; 28 29 import com.android.launcher3.ExtendedEditText; 30 import com.android.launcher3.search.SearchAlgorithm; 31 import com.android.launcher3.search.SearchCallback; 32 import com.android.launcher3.widget.model.WidgetsListBaseEntry; 33 34 import java.util.ArrayList; 35 36 /** 37 * Controller for a search bar with an edit text and a cancel button. 38 */ 39 public class WidgetsSearchBarController implements TextWatcher, 40 SearchCallback<WidgetsListBaseEntry>, View.OnKeyListener { 41 private static final String TAG = "WidgetsSearchBarController"; 42 private static final boolean DEBUG = false; 43 44 protected SearchAlgorithm<WidgetsListBaseEntry> mSearchAlgorithm; 45 protected ExtendedEditText mInput; 46 protected ImageButton mCancelButton; 47 protected SearchModeListener mSearchModeListener; 48 protected String mQuery; 49 WidgetsSearchBarController( SearchAlgorithm<WidgetsListBaseEntry> algo, ExtendedEditText editText, ImageButton cancelButton, SearchModeListener searchModeListener)50 public WidgetsSearchBarController( 51 SearchAlgorithm<WidgetsListBaseEntry> algo, ExtendedEditText editText, 52 ImageButton cancelButton, SearchModeListener searchModeListener) { 53 mSearchAlgorithm = algo; 54 mInput = editText; 55 mInput.addTextChangedListener(this); 56 mInput.setOnKeyListener(this); 57 mCancelButton = cancelButton; 58 mCancelButton.setOnClickListener(v -> clearSearchResult()); 59 mSearchModeListener = searchModeListener; 60 } 61 62 @Override afterTextChanged(final Editable s)63 public void afterTextChanged(final Editable s) { 64 mQuery = s.toString(); 65 if (mQuery.isEmpty()) { 66 mSearchAlgorithm.cancel(/* interruptActiveRequests= */ true); 67 mSearchModeListener.exitSearchMode(); 68 mCancelButton.setVisibility(GONE); 69 } else { 70 mSearchAlgorithm.cancel(/* interruptActiveRequests= */ false); 71 mSearchModeListener.enterSearchMode(true); 72 mSearchAlgorithm.doSearch(mQuery, this); 73 mCancelButton.setVisibility(VISIBLE); 74 } 75 } 76 77 @Override beforeTextChanged(CharSequence charSequence, int i, int i1, int i2)78 public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { 79 // Do nothing. 80 } 81 82 @Override onTextChanged(CharSequence charSequence, int i, int i1, int i2)83 public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { 84 // Do nothing. 85 } 86 87 @Override onSearchResult(String query, ArrayList<WidgetsListBaseEntry> items)88 public void onSearchResult(String query, ArrayList<WidgetsListBaseEntry> items) { 89 if (DEBUG) { 90 Log.d(TAG, "onSearchResult query: " + query + " items: " + items); 91 } 92 mSearchModeListener.onSearchResults(items); 93 } 94 95 @Override clearSearchResult()96 public void clearSearchResult() { 97 // Any existing search session will be cancelled by setting text to empty. 98 mInput.setText(""); 99 } 100 101 /** 102 * Cleans up after search is no longer needed. 103 */ onDestroy()104 public void onDestroy() { 105 mSearchAlgorithm.destroy(); 106 } 107 108 @Override onKey(View view, int keyCode, KeyEvent event)109 public boolean onKey(View view, int keyCode, KeyEvent event) { 110 if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_UP) { 111 clearFocus(); 112 return true; 113 } 114 return false; 115 } 116 117 /** 118 * Clears focus from edit text. 119 */ clearFocus()120 public void clearFocus() { 121 mInput.clearFocus(); 122 mInput.hideKeyboard(); 123 } 124 } 125