1 /* 2 * Copyright (C) 2015 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 package com.android.launcher3.allapps.search; 17 18 import android.text.Editable; 19 import android.text.SpannableStringBuilder; 20 import android.text.TextUtils; 21 import android.text.TextWatcher; 22 import android.text.style.SuggestionSpan; 23 import android.util.Log; 24 import android.view.KeyEvent; 25 import android.view.View; 26 import android.view.View.OnFocusChangeListener; 27 import android.view.inputmethod.EditorInfo; 28 import android.widget.TextView; 29 import android.widget.TextView.OnEditorActionListener; 30 31 import com.android.launcher3.ExtendedEditText; 32 import com.android.launcher3.Utilities; 33 import com.android.launcher3.allapps.BaseAllAppsAdapter.AdapterItem; 34 import com.android.launcher3.config.FeatureFlags; 35 import com.android.launcher3.search.SearchAlgorithm; 36 import com.android.launcher3.search.SearchCallback; 37 import com.android.launcher3.views.ActivityContext; 38 39 /** 40 * An interface to a search box that AllApps can command. 41 */ 42 public class AllAppsSearchBarController 43 implements TextWatcher, OnEditorActionListener, ExtendedEditText.OnBackKeyListener, 44 OnFocusChangeListener { 45 46 private static final String TAG = "AllAppsSearchBarController"; 47 protected ActivityContext mLauncher; 48 protected SearchCallback<AdapterItem> mCallback; 49 protected ExtendedEditText mInput; 50 protected String mQuery; 51 private String[] mTextConversions; 52 53 protected SearchAlgorithm<AdapterItem> mSearchAlgorithm; 54 setVisibility(int visibility)55 public void setVisibility(int visibility) { 56 mInput.setVisibility(visibility); 57 } 58 59 /** 60 * Sets the references to the apps model and the search result callback. 61 */ initialize( SearchAlgorithm<AdapterItem> searchAlgorithm, ExtendedEditText input, ActivityContext launcher, SearchCallback<AdapterItem> callback)62 public final void initialize( 63 SearchAlgorithm<AdapterItem> searchAlgorithm, ExtendedEditText input, 64 ActivityContext launcher, SearchCallback<AdapterItem> callback) { 65 mCallback = callback; 66 mLauncher = launcher; 67 68 mInput = input; 69 mInput.addTextChangedListener(this); 70 mInput.setOnEditorActionListener(this); 71 mInput.setOnBackKeyListener(this); 72 mInput.addOnFocusChangeListener(this); 73 mSearchAlgorithm = searchAlgorithm; 74 } 75 76 @Override beforeTextChanged(CharSequence charSequence, int i, int i1, int i2)77 public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { 78 // Do nothing 79 } 80 81 @Override onTextChanged(CharSequence s, int start, int before, int count)82 public void onTextChanged(CharSequence s, int start, int before, int count) { 83 mTextConversions = extractTextConversions(s); 84 } 85 86 /** 87 * Extract text conversions from composing text and send them for search. 88 */ extractTextConversions(CharSequence text)89 public static String[] extractTextConversions(CharSequence text) { 90 if (text instanceof SpannableStringBuilder) { 91 SpannableStringBuilder spanned = (SpannableStringBuilder) text; 92 SuggestionSpan[] suggestionSpans = 93 spanned.getSpans(0, text.length(), SuggestionSpan.class); 94 if (suggestionSpans != null && suggestionSpans.length > 0) { 95 spanned.removeSpan(suggestionSpans[0]); 96 return suggestionSpans[0].getSuggestions(); 97 } 98 } 99 return null; 100 } 101 102 @Override afterTextChanged(final Editable s)103 public void afterTextChanged(final Editable s) { 104 mQuery = s.toString(); 105 if (mQuery.isEmpty()) { 106 mSearchAlgorithm.cancel(true); 107 mCallback.clearSearchResult(); 108 } else { 109 mSearchAlgorithm.cancel(false); 110 mSearchAlgorithm.doSearch(mQuery, mTextConversions, mCallback); 111 } 112 } 113 refreshSearchResult()114 public void refreshSearchResult() { 115 if (TextUtils.isEmpty(mQuery)) { 116 return; 117 } 118 // If play store continues auto updating an app, we want to show partial result. 119 mSearchAlgorithm.cancel(false); 120 mSearchAlgorithm.doSearch(mQuery, mCallback); 121 } 122 123 @Override onEditorAction(TextView v, int actionId, KeyEvent event)124 public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { 125 126 if (actionId == EditorInfo.IME_ACTION_SEARCH || actionId == EditorInfo.IME_ACTION_GO) { 127 Log.i(TAG, "User tapped ime search button"); 128 // selectFocusedView should return SearchTargetEvent that is passed onto onClick 129 return mLauncher.getAppsView().getMainAdapterProvider().launchHighlightedItem(); 130 } 131 return false; 132 } 133 134 @Override onBackKey()135 public boolean onBackKey() { 136 // Only hide the search field if there is no query 137 String query = Utilities.trim(mInput.getEditableText().toString()); 138 if (query.isEmpty()) { 139 reset(); 140 return true; 141 } 142 return false; 143 } 144 145 @Override onFocusChange(View view, boolean hasFocus)146 public void onFocusChange(View view, boolean hasFocus) { 147 if (!hasFocus && !FeatureFlags.ENABLE_DEVICE_SEARCH.get()) { 148 mInput.hideKeyboard(); 149 } 150 } 151 152 /** 153 * Resets the search bar state. 154 */ reset()155 public void reset() { 156 mCallback.clearSearchResult(); 157 mInput.reset(); 158 mInput.clearFocus(); 159 mQuery = null; 160 mInput.removeOnFocusChangeListener(this); 161 } 162 163 /** 164 * Focuses the search field to handle key events. 165 */ focusSearchField()166 public void focusSearchField() { 167 mInput.showKeyboard(); 168 } 169 170 /** 171 * Returns whether the search field is focused. 172 */ isSearchFieldFocused()173 public boolean isSearchFieldFocused() { 174 return mInput.isFocused(); 175 } 176 } 177