Merge "Refactor SearchAlgorithm and SearchCallback interface to work on generic search result type." into sc-dev

This commit is contained in:
Alina Zaidi authored and Android (Google) Code Review committed 2021-02-26 15:44:37 +00:00
commit 4ffd4f03c6
5 files changed
+70 -45

No files matched your search

@@ -33,10 +33,10 @@ import com.android.launcher3.Launcher;
import com.android.launcher3.Utilities;
import com.android.launcher3.allapps.AllAppsGridAdapter.AdapterItem;
import com.android.launcher3.config.FeatureFlags;
import com.android.launcher3.search.SearchAlgorithm;
import com.android.launcher3.search.SearchCallback;
import com.android.launcher3.util.PackageManagerHelper;
import java.util.ArrayList;
/**
* An interface to a search box that AllApps can command.
*/
@@ -45,11 +45,11 @@ public class AllAppsSearchBarController
OnFocusChangeListener {
protected BaseDraggingActivity mLauncher;
protected Callbacks mCb;
protected SearchCallback<AdapterItem> mCallback;
protected ExtendedEditText mInput;
protected String mQuery;
protected SearchAlgorithm mSearchAlgorithm;
protected SearchAlgorithm<AdapterItem> mSearchAlgorithm;
public void setVisibility(int visibility) {
mInput.setVisibility(visibility);
@@ -59,9 +59,9 @@ public class AllAppsSearchBarController
* Sets the references to the apps model and the search result callback.
*/
public final void initialize(
SearchAlgorithm searchAlgorithm, ExtendedEditText input,
BaseDraggingActivity launcher, Callbacks cb) {
mCb = cb;
SearchAlgorithm<AdapterItem> searchAlgorithm, ExtendedEditText input,
BaseDraggingActivity launcher, SearchCallback<AdapterItem> callback) {
mCallback = callback;
mLauncher = launcher;
mInput = input;
@@ -87,10 +87,10 @@ public class AllAppsSearchBarController
mQuery = s.toString();
if (mQuery.isEmpty()) {
mSearchAlgorithm.cancel(true);
mCb.clearSearchResult();
mCallback.clearSearchResult();
} else {
mSearchAlgorithm.cancel(false);
mSearchAlgorithm.doSearch(mQuery, mCb);
mSearchAlgorithm.doSearch(mQuery, mCallback);
}
}
@@ -100,7 +100,7 @@ public class AllAppsSearchBarController
}
// If play store continues auto updating an app, we want to show partial result.
mSearchAlgorithm.cancel(false);
mSearchAlgorithm.doSearch(mQuery, mCb);
mSearchAlgorithm.doSearch(mQuery, mCallback);
}
@Override
@@ -153,7 +153,7 @@ public class AllAppsSearchBarController
* Resets the search bar state.
*/
public void reset() {
mCb.clearSearchResult();
mCallback.clearSearchResult();
mInput.reset();
mQuery = null;
}
@@ -171,31 +171,4 @@ public class AllAppsSearchBarController
public boolean isSearchFieldFocused() {
return mInput.isFocused();
}
/**
* Callback for getting search results.
*/
public interface Callbacks {
/**
* Called when the search from primary source is complete.
*
* @param items sorted list of search result adapter items
*/
void onSearchResult(String query, ArrayList<AdapterItem> items);
/**
* Called when the search from secondary source is complete.
*
* @param items sorted list of search result adapter items
*/
void onAppendSearchResult(String query, ArrayList<AdapterItem> items);
/**
* Called when the search results should be cleared.
*/
void clearSearchResult();
}
}
@@ -47,6 +47,7 @@ import com.android.launcher3.allapps.AlphabeticalAppsList;
import com.android.launcher3.allapps.SearchUiManager;
import com.android.launcher3.anim.PropertySetter;
import com.android.launcher3.config.FeatureFlags;
import com.android.launcher3.search.SearchCallback;
import java.util.ArrayList;
@@ -54,7 +55,7 @@ import java.util.ArrayList;
* Layout to contain the All-apps search UI.
*/
public class AppsSearchContainerLayout extends ExtendedEditText
implements SearchUiManager, AllAppsSearchBarController.Callbacks,
implements SearchUiManager, SearchCallback<AdapterItem>,
AllAppsStore.OnUpdateListener, Insettable {
private final BaseDraggingActivity mLauncher;
@@ -19,14 +19,17 @@ import android.content.Context;
import android.os.Handler;
import com.android.launcher3.LauncherAppState;
import com.android.launcher3.allapps.AllAppsGridAdapter.AdapterItem;
import com.android.launcher3.model.data.AppInfo;
import com.android.launcher3.search.SearchAlgorithm;
import com.android.launcher3.search.SearchCallback;
import java.text.Collator;
/**
* The default search implementation.
*/
public class DefaultAppSearchAlgorithm implements SearchAlgorithm {
public class DefaultAppSearchAlgorithm implements SearchAlgorithm<AdapterItem> {
protected final Handler mResultHandler;
private final AppsSearchPipeline mAppsSearchPipeline;
@@ -45,7 +48,7 @@ public class DefaultAppSearchAlgorithm implements SearchAlgorithm {
@Override
public void doSearch(final String query,
final AllAppsSearchBarController.Callbacks callback) {
final SearchCallback<AdapterItem> callback) {
mAppsSearchPipeline.query(query,
results -> mResultHandler.post(
() -> callback.onSearchResult(query, results)),
@@ -13,17 +13,19 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.launcher3.allapps.search;
package com.android.launcher3.search;
/**
* An interface for handling search.
*
* @param <T> Search Result type
*/
public interface SearchAlgorithm {
public interface SearchAlgorithm<T> {
/**
* Performs search and sends the result to the callback.
* Performs search and sends the result to {@link SearchCallback}.
*/
void doSearch(String query, AllAppsSearchBarController.Callbacks callback);
void doSearch(String query, SearchCallback<T> callback);
/**
* Cancels any active request.
@@ -0,0 +1,46 @@
/*
* Copyright (C) 2017 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.search;
import java.util.ArrayList;
/**
* An interface for receiving search results.
*
* @param <T> Search Result type
*/
public interface SearchCallback<T> {
/**
* Called when the search from primary source is complete.
*
* @param items list of search results
*/
void onSearchResult(String query, ArrayList<T> items);
/**
* Called when the search from secondary source is complete.
*
* @param items list of search results
*/
void onAppendSearchResult(String query, ArrayList<T> items);
/**
* Called when the search results should be cleared.
*/
void clearSearchResult();
}