Car Search RecyclerView Framework am: 11cbea113c

Change-Id: I953feccd8d7219a2551d3e347e6ee4804bfd9958
diff --git a/src/com/android/settings/intelligence/search/CarSearchFragment.java b/src/com/android/settings/intelligence/search/CarSearchFragment.java
deleted file mode 100644
index 6b2f880..0000000
--- a/src/com/android/settings/intelligence/search/CarSearchFragment.java
+++ /dev/null
@@ -1,90 +0,0 @@
-/*
- * Copyright (C) 2020 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.settings.intelligence.search;
-
-import static com.android.car.ui.core.CarUi.requireInsets;
-import static com.android.car.ui.core.CarUi.requireToolbar;
-
-import android.os.Bundle;
-
-import com.android.car.ui.preference.PreferenceFragment;
-import com.android.car.ui.toolbar.ToolbarController;
-import com.android.settings.intelligence.R;
-import com.android.car.ui.toolbar.MenuItem;
-import com.android.car.ui.toolbar.Toolbar;
-
-import java.util.Collections;
-import java.util.List;
-
-/**
- * Search fragment for car settings
- */
-public class CarSearchFragment extends PreferenceFragment {
-
-    private MenuItem mClearHistoryButton;
-
-    @Override
-    public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
-        setPreferencesFromResource(R.xml.car_search_fragment, rootKey);
-    }
-
-    protected ToolbarController getToolbar() {
-        return requireToolbar(requireActivity());
-    }
-
-    protected List<MenuItem> getToolbarMenuItems() {
-        return Collections.singletonList(mClearHistoryButton);
-    }
-
-    @Override
-    public void onCreate(Bundle savedInstanceState) {
-        super.onCreate(savedInstanceState);
-
-        mClearHistoryButton = new MenuItem.Builder(getContext())
-                .setTitle(R.string.search_clear_history)
-                .setDisplayBehavior(MenuItem.DisplayBehavior.NEVER)
-                .setOnClickListener(i -> onClearHistoryButtonClicked())
-                .build();
-    }
-
-    @Override
-    public void onActivityCreated(Bundle savedInstanceState) {
-        super.onActivityCreated(savedInstanceState);
-        ToolbarController toolbar = getToolbar();
-        if (toolbar != null) {
-            List<MenuItem> items = getToolbarMenuItems();
-            toolbar.setTitle(getPreferenceScreen().getTitle());
-            toolbar.setMenuItems(items);
-            toolbar.setNavButtonMode(Toolbar.NavButtonMode.BACK);
-            toolbar.setState(Toolbar.State.SUBPAGE);
-            toolbar.setState(Toolbar.State.SEARCH);
-            toolbar.setSearchHint(R.string.abc_search_hint);
-            toolbar.registerOnSearchListener(this::onQueryTextChange);
-            toolbar.setShowMenuItemsWhileSearching(true);
-        }
-    }
-
-    @Override
-    public void onStart() {
-        super.onStart();
-        onCarUiInsetsChanged(requireInsets(requireActivity()));
-    }
-
-    private void onQueryTextChange(String query) {}
-
-    private void onClearHistoryButtonClicked() {}
-}
diff --git a/src/com/android/settings/intelligence/search/SearchActivity.java b/src/com/android/settings/intelligence/search/SearchActivity.java
index cd43644..8b8bc42 100644
--- a/src/com/android/settings/intelligence/search/SearchActivity.java
+++ b/src/com/android/settings/intelligence/search/SearchActivity.java
@@ -26,6 +26,7 @@
 import android.view.WindowManager;
 
 import com.android.settings.intelligence.R;
+import com.android.settings.intelligence.search.car.CarSearchFragment;
 
 public class SearchActivity extends FragmentActivity {
 
diff --git a/src/com/android/settings/intelligence/search/SearchCommon.java b/src/com/android/settings/intelligence/search/SearchCommon.java
new file mode 100644
index 0000000..4666837
--- /dev/null
+++ b/src/com/android/settings/intelligence/search/SearchCommon.java
@@ -0,0 +1,57 @@
+/*
+ * Copyright (C) 2020 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.settings.intelligence.search;
+
+/**
+ * Shared constant variables and identifiers.
+ */
+public class SearchCommon {
+    /**
+     * Instance state key for the currently entered query.
+     */
+    public static final String STATE_QUERY = "state_query";
+    /**
+     * Instance state key for whether or not saved queries are being shown.
+     */
+    public static final String STATE_SHOWING_SAVED_QUERY = "state_showing_saved_query";
+    /**
+     * Instance state key for whether or not a query has been entered yet.
+     */
+    public static final String STATE_NEVER_ENTERED_QUERY = "state_never_entered_query";
+
+    /**
+     * Identifier constants for the search loaders.
+     */
+    public static final class SearchLoaderId {
+        /**
+         * Loader identifier to get search results.
+         */
+        public static final int SEARCH_RESULT = 1;
+        /**
+         * Loader identifier to save an entered query.
+         */
+        public static final int SAVE_QUERY_TASK = 2;
+        /**
+         * Loader identifier to remove an entered query.
+         */
+        public static final int REMOVE_QUERY_TASK = 3;
+        /**
+         * Loader identifier to get currently saved queries.
+         */
+        public static final int SAVED_QUERIES = 4;
+    }
+}
diff --git a/src/com/android/settings/intelligence/search/SearchFragment.java b/src/com/android/settings/intelligence/search/SearchFragment.java
index adb5fd5..4642b16 100644
--- a/src/com/android/settings/intelligence/search/SearchFragment.java
+++ b/src/com/android/settings/intelligence/search/SearchFragment.java
@@ -64,21 +64,6 @@
         LoaderManager.LoaderCallbacks<List<? extends SearchResult>>, IndexingCallback {
     private static final String TAG = "SearchFragment";
 
-    // State values
-    private static final String STATE_QUERY = "state_query";
-    private static final String STATE_SHOWING_SAVED_QUERY = "state_showing_saved_query";
-    private static final String STATE_NEVER_ENTERED_QUERY = "state_never_entered_query";
-
-    public static final class SearchLoaderId {
-        // Search Query IDs
-        public static final int SEARCH_RESULT = 1;
-
-        // Saved Query IDs
-        public static final int SAVE_QUERY_TASK = 2;
-        public static final int REMOVE_QUERY_TASK = 3;
-        public static final int SAVED_QUERIES = 4;
-    }
-
     @VisibleForTesting
     String mQuery;
 
@@ -134,9 +119,9 @@
         mSearchFeatureProvider.initFeedbackButton();
 
         if (savedInstanceState != null) {
-            mQuery = savedInstanceState.getString(STATE_QUERY);
-            mNeverEnteredQuery = savedInstanceState.getBoolean(STATE_NEVER_ENTERED_QUERY);
-            mShowingSavedQuery = savedInstanceState.getBoolean(STATE_SHOWING_SAVED_QUERY);
+            mQuery = savedInstanceState.getString(SearchCommon.STATE_QUERY);
+            mNeverEnteredQuery = savedInstanceState.getBoolean(SearchCommon.STATE_NEVER_ENTERED_QUERY);
+            mShowingSavedQuery = savedInstanceState.getBoolean(SearchCommon.STATE_SHOWING_SAVED_QUERY);
         } else {
             mShowingSavedQuery = true;
         }
@@ -208,9 +193,9 @@
     @Override
     public void onSaveInstanceState(Bundle outState) {
         super.onSaveInstanceState(outState);
-        outState.putString(STATE_QUERY, mQuery);
-        outState.putBoolean(STATE_NEVER_ENTERED_QUERY, mNeverEnteredQuery);
-        outState.putBoolean(STATE_SHOWING_SAVED_QUERY, mShowingSavedQuery);
+        outState.putString(SearchCommon.STATE_QUERY, mQuery);
+        outState.putBoolean(SearchCommon.STATE_NEVER_ENTERED_QUERY, mNeverEnteredQuery);
+        outState.putBoolean(SearchCommon.STATE_SHOWING_SAVED_QUERY, mShowingSavedQuery);
     }
 
     @Override
@@ -238,7 +223,7 @@
 
         if (isEmptyQuery) {
             final LoaderManager loaderManager = getLoaderManager();
-            loaderManager.destroyLoader(SearchLoaderId.SEARCH_RESULT);
+            loaderManager.destroyLoader(SearchCommon.SearchLoaderId.SEARCH_RESULT);
             mShowingSavedQuery = true;
             mSavedQueryController.loadSavedQueries();
             mSearchFeatureProvider.hideFeedbackButton(getView());
@@ -263,7 +248,7 @@
         final Activity activity = getActivity();
 
         switch (id) {
-            case SearchLoaderId.SEARCH_RESULT:
+            case SearchCommon.SearchLoaderId.SEARCH_RESULT:
                 return mSearchFeatureProvider.getSearchResultLoader(activity, mQuery);
             default:
                 return null;
@@ -292,7 +277,7 @@
             mSavedQueryController.loadSavedQueries();
         } else {
             final LoaderManager loaderManager = getLoaderManager();
-            loaderManager.initLoader(SearchLoaderId.SEARCH_RESULT, null /* args */,
+            loaderManager.initLoader(SearchCommon.SearchLoaderId.SEARCH_RESULT, null /* args */,
                     this /* callback */);
         }
 
@@ -338,8 +323,8 @@
     private void restartLoaders() {
         mShowingSavedQuery = false;
         final LoaderManager loaderManager = getLoaderManager();
-        loaderManager.restartLoader(
-                SearchLoaderId.SEARCH_RESULT, null /* args */, this /* callback */);
+        loaderManager.restartLoader(SearchCommon.SearchLoaderId.SEARCH_RESULT,
+                null /* args */, this /* callback */);
     }
 
     public String getQuery() {
diff --git a/src/com/android/settings/intelligence/search/car/CarIntentSearchViewHolder.java b/src/com/android/settings/intelligence/search/car/CarIntentSearchViewHolder.java
new file mode 100644
index 0000000..ba8a165
--- /dev/null
+++ b/src/com/android/settings/intelligence/search/car/CarIntentSearchViewHolder.java
@@ -0,0 +1,80 @@
+/*
+ * Copyright (C) 2020 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.settings.intelligence.search.car;
+
+import android.content.Intent;
+import android.content.pm.PackageManager;
+import android.content.pm.ResolveInfo;
+import android.util.Log;
+import android.view.View;
+
+import com.android.settings.intelligence.R;
+import com.android.settings.intelligence.search.AppSearchResult;
+import com.android.settings.intelligence.search.SearchResult;
+
+import java.util.List;
+
+/**
+ * ViewHolder for intent based search results.
+ */
+public class CarIntentSearchViewHolder extends CarSearchViewHolder {
+    private static final String TAG = "CarIntentSearchViewHolder";
+    private static final int REQUEST_CODE_NO_OP = 0;
+
+    public CarIntentSearchViewHolder(View view) {
+        super(view);
+    }
+
+    @Override
+    public void onBind(CarSearchFragment fragment, SearchResult result) {
+        mTitle.setText(result.title);
+        if (result instanceof AppSearchResult) {
+            AppSearchResult appResult = (AppSearchResult) result;
+            PackageManager pm = fragment.getActivity().getPackageManager();
+            mIcon.setImageDrawable(appResult.info.loadIcon(pm));
+        } else {
+            mIcon.setImageDrawable(result.icon);
+        }
+        if (result.breadcrumbs != null && !result.breadcrumbs.isEmpty()) {
+            String breadcrumb = result.breadcrumbs.get(0);
+            int count = result.breadcrumbs.size();
+            for (int i = 1; i < count; i++) {
+                breadcrumb = mContext.getString(R.string.search_breadcrumb_connector,
+                        breadcrumb, result.breadcrumbs.get(i));
+            }
+            mSummary.setText(breadcrumb);
+        }
+
+        itemView.setOnClickListener(v -> {
+            fragment.onSearchResultClicked(/* resultViewHolder= */ this, result);
+            Intent intent = result.payload.getIntent();
+            if (result instanceof AppSearchResult) {
+                AppSearchResult appResult = (AppSearchResult) result;
+                fragment.getActivity().startActivity(intent);
+            } else {
+                PackageManager pm = fragment.getActivity().getPackageManager();
+                List<ResolveInfo> info = pm.queryIntentActivities(intent, /* flags= */ 0);
+                if (info != null && !info.isEmpty()) {
+                    fragment.startActivityForResult(intent, REQUEST_CODE_NO_OP);
+                } else {
+                    Log.e(TAG, "Cannot launch search result, title: "
+                            + result.title + ", " + intent);
+                }
+            }
+        });
+    }
+}
diff --git a/src/com/android/settings/intelligence/search/car/CarSearchFragment.java b/src/com/android/settings/intelligence/search/car/CarSearchFragment.java
new file mode 100644
index 0000000..544bab9
--- /dev/null
+++ b/src/com/android/settings/intelligence/search/car/CarSearchFragment.java
@@ -0,0 +1,285 @@
+/*
+ * Copyright (C) 2020 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.settings.intelligence.search.car;
+
+import static com.android.car.ui.core.CarUi.requireInsets;
+import static com.android.car.ui.core.CarUi.requireToolbar;
+
+import android.app.Activity;
+import android.content.Context;
+import android.os.Bundle;
+import android.text.TextUtils;
+import android.view.View;
+import android.view.inputmethod.InputMethodManager;
+
+import androidx.annotation.NonNull;
+import androidx.loader.app.LoaderManager;
+import androidx.loader.content.Loader;
+import androidx.recyclerview.widget.RecyclerView;
+
+import com.android.car.ui.preference.PreferenceFragment;
+import com.android.car.ui.toolbar.ToolbarController;
+import com.android.settings.intelligence.R;
+import com.android.car.ui.toolbar.MenuItem;
+import com.android.car.ui.toolbar.Toolbar;
+import com.android.settings.intelligence.overlay.FeatureFactory;
+import com.android.settings.intelligence.search.SearchCommon;
+import com.android.settings.intelligence.search.SearchFeatureProvider;
+import com.android.settings.intelligence.search.SearchResult;
+import com.android.settings.intelligence.search.indexing.IndexingCallback;
+import com.android.settings.intelligence.search.savedqueries.car.CarSavedQueryController;
+import com.android.settings.intelligence.search.savedqueries.car.CarSavedQueryViewHolder;
+
+import java.util.Collections;
+import java.util.List;
+
+/**
+ * Search fragment for car settings.
+ */
+public class CarSearchFragment extends PreferenceFragment implements
+        LoaderManager.LoaderCallbacks<List<? extends SearchResult>>, IndexingCallback {
+
+    private SearchFeatureProvider mSearchFeatureProvider;
+
+    private ToolbarController mToolbar;
+    private MenuItem mClearHistoryButton;
+    private RecyclerView mRecyclerView;
+
+    private String mQuery;
+    private boolean mShowingSavedQuery;
+
+    private CarSearchResultsAdapter mSearchAdapter;
+    private CarSavedQueryController mSavedQueryController;
+
+    private final RecyclerView.OnScrollListener mScrollListener =
+            new RecyclerView.OnScrollListener() {
+                @Override
+                public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
+                    if (dy != 0) {
+                        hideKeyboard();
+                    }
+                }
+            };
+
+    @Override
+    public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
+        setPreferencesFromResource(R.xml.car_search_fragment, rootKey);
+    }
+
+    protected ToolbarController getToolbar() {
+        return requireToolbar(requireActivity());
+    }
+
+    protected List<MenuItem> getToolbarMenuItems() {
+        return Collections.singletonList(mClearHistoryButton);
+    }
+
+    @Override
+    public void onAttach(Context context) {
+        super.onAttach(context);
+        mSearchFeatureProvider = FeatureFactory.get(context).searchFeatureProvider();
+    }
+
+    @Override
+    public void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+
+        mClearHistoryButton = new MenuItem.Builder(getContext())
+                .setTitle(R.string.search_clear_history)
+                .setDisplayBehavior(MenuItem.DisplayBehavior.NEVER)
+                .setOnClickListener(i -> onClearHistoryButtonClicked())
+                .build();
+
+        if (savedInstanceState != null) {
+            mQuery = savedInstanceState.getString(SearchCommon.STATE_QUERY);
+            mShowingSavedQuery = savedInstanceState.getBoolean(
+                    SearchCommon.STATE_SHOWING_SAVED_QUERY);
+        } else {
+            mShowingSavedQuery = true;
+        }
+
+        LoaderManager loaderManager = getLoaderManager();
+        mSearchAdapter = new CarSearchResultsAdapter(/* fragment= */ this);
+        mSavedQueryController = new CarSavedQueryController(
+                getContext(), loaderManager, mSearchAdapter);
+        mSearchFeatureProvider.updateIndexAsync(getContext(), /* indexingCallback= */ this);
+    }
+
+    @Override
+    public void onActivityCreated(Bundle savedInstanceState) {
+        super.onActivityCreated(savedInstanceState);
+        mToolbar = getToolbar();
+        if (mToolbar != null) {
+            List<MenuItem> items = getToolbarMenuItems();
+            mToolbar.setTitle(getPreferenceScreen().getTitle());
+            mToolbar.setMenuItems(items);
+            mToolbar.setNavButtonMode(Toolbar.NavButtonMode.BACK);
+            mToolbar.setState(Toolbar.State.SUBPAGE);
+            mToolbar.setState(Toolbar.State.SEARCH);
+            mToolbar.setSearchHint(R.string.abc_search_hint);
+            mToolbar.registerOnSearchListener(this::onQueryTextChange);
+            mToolbar.registerOnSearchCompletedListener(this::onSearchComplete);
+            mToolbar.setShowMenuItemsWhileSearching(true);
+            mToolbar.setSearchQuery(mQuery);
+        }
+        mRecyclerView = getListView();
+        if (mRecyclerView != null) {
+            mRecyclerView.setAdapter(mSearchAdapter);
+            mRecyclerView.addOnScrollListener(mScrollListener);
+        }
+    }
+
+    @Override
+    public void onStart() {
+        super.onStart();
+        onCarUiInsetsChanged(requireInsets(requireActivity()));
+    }
+
+    @Override
+    public void onSaveInstanceState(@NonNull Bundle outState) {
+        super.onSaveInstanceState(outState);
+        outState.putString(SearchCommon.STATE_QUERY, mQuery);
+        outState.putBoolean(SearchCommon.STATE_SHOWING_SAVED_QUERY, mShowingSavedQuery);
+    }
+
+    private void onQueryTextChange(String query) {
+        if (TextUtils.equals(query, mQuery)) {
+            return;
+        }
+        boolean isEmptyQuery = TextUtils.isEmpty(query);
+
+        mQuery = query;
+
+        // If indexing is not finished, register the query text, but don't search.
+        if (!mSearchFeatureProvider.isIndexingComplete(getActivity())) {
+            if (!isEmptyQuery) {
+                mToolbar.showProgressBar();
+            } else {
+                mToolbar.hideProgressBar();
+            }
+            return;
+        }
+
+        if (isEmptyQuery) {
+            LoaderManager loaderManager = getLoaderManager();
+            loaderManager.destroyLoader(SearchCommon.SearchLoaderId.SEARCH_RESULT);
+            mShowingSavedQuery = true;
+            mSavedQueryController.loadSavedQueries();
+        } else {
+            restartLoaders();
+        }
+    }
+
+    private void onSearchComplete() {
+        if (!TextUtils.isEmpty(mQuery)) {
+            mSavedQueryController.saveQuery(mQuery);
+        }
+    }
+
+    private void onClearHistoryButtonClicked() {
+        mSavedQueryController.removeQueries();
+    }
+
+    /**
+     * Gets called when a saved query is clicked.
+     */
+    public void onSavedQueryClicked(CarSavedQueryViewHolder vh, CharSequence query) {
+        String queryString = query.toString();
+        mToolbar.setSearchQuery(queryString);
+        onQueryTextChange(queryString);
+    }
+
+    /**
+     * Gets called when a search result is clicked.
+     */
+    public void onSearchResultClicked(CarSearchViewHolder resultViewHolder, SearchResult result) {
+        mSearchFeatureProvider.searchResultClicked(getContext(), mQuery, result);
+        mSavedQueryController.saveQuery(mQuery);
+    }
+
+    @Override
+    public Loader<List<? extends SearchResult>> onCreateLoader(int id, Bundle args) {
+        Activity activity = getActivity();
+
+        if (id == SearchCommon.SearchLoaderId.SEARCH_RESULT) {
+            return mSearchFeatureProvider.getSearchResultLoader(activity, mQuery);
+        }
+        return null;
+    }
+
+    @Override
+    public void onLoadFinished(Loader<List<? extends SearchResult>> loader,
+            List<? extends SearchResult> data) {
+        mSearchAdapter.postSearchResults(data);
+        mRecyclerView.scrollToPosition(0);
+    }
+
+    @Override
+    public void onLoaderReset(Loader<List<? extends SearchResult>> loader) {
+    }
+
+    /**
+     * Gets called when Indexing is completed.
+     */
+    @Override
+    public void onIndexingFinished() {
+        if (getActivity() == null) {
+            return;
+        }
+        mToolbar.hideProgressBar();
+        if (mShowingSavedQuery) {
+            mSavedQueryController.loadSavedQueries();
+        } else {
+            LoaderManager loaderManager = getLoaderManager();
+            loaderManager.initLoader(SearchCommon.SearchLoaderId.SEARCH_RESULT,
+                    /* args= */ null, /* callback= */ this);
+        }
+        requery();
+    }
+
+    private void requery() {
+        if (TextUtils.isEmpty(mQuery)) {
+            return;
+        }
+        String query = mQuery;
+        mQuery = "";
+        onQueryTextChange(query);
+    }
+
+    private void restartLoaders() {
+        mShowingSavedQuery = false;
+        LoaderManager loaderManager = getLoaderManager();
+        loaderManager.restartLoader(SearchCommon.SearchLoaderId.SEARCH_RESULT,
+                /* args= */ null, /* callback= */ this);
+    }
+
+    private void hideKeyboard() {
+        Activity activity = getActivity();
+        if (activity != null) {
+            View view = activity.getCurrentFocus();
+            InputMethodManager imm = (InputMethodManager)
+                    activity.getSystemService(Context.INPUT_METHOD_SERVICE);
+            if (imm.isActive(view)) {
+                imm.hideSoftInputFromWindow(view.getWindowToken(), /* flags= */ 0);
+            }
+        }
+
+        if (mRecyclerView != null) {
+            mRecyclerView.requestFocus();
+        }
+    }
+}
diff --git a/src/com/android/settings/intelligence/search/car/CarSearchResultsAdapter.java b/src/com/android/settings/intelligence/search/car/CarSearchResultsAdapter.java
new file mode 100644
index 0000000..be35482
--- /dev/null
+++ b/src/com/android/settings/intelligence/search/car/CarSearchResultsAdapter.java
@@ -0,0 +1,122 @@
+/*
+ * Copyright (C) 2020 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.settings.intelligence.search.car;
+
+import android.content.Context;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.view.ViewGroup;
+
+import androidx.recyclerview.widget.DiffUtil;
+import androidx.recyclerview.widget.RecyclerView;
+
+import com.android.settings.intelligence.R;
+import com.android.settings.intelligence.search.ResultPayload;
+import com.android.settings.intelligence.search.SearchResult;
+import com.android.settings.intelligence.search.SearchResultDiffCallback;
+import com.android.settings.intelligence.search.savedqueries.car.CarSavedQueryViewHolder;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * RecyclerView Adapter for the car search results RecyclerView.
+ * The adapter uses the CarSearchViewHolder for its view contents.
+ */
+public class CarSearchResultsAdapter extends RecyclerView.Adapter<CarSearchViewHolder> {
+
+    private final CarSearchFragment mFragment;
+    private final List<SearchResult> mSearchResults;
+
+    public CarSearchResultsAdapter(CarSearchFragment fragment) {
+        mFragment = fragment;
+        mSearchResults = new ArrayList<>();
+
+        setHasStableIds(true);
+    }
+
+    @Override
+    public CarSearchViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
+        Context context = parent.getContext();
+        LayoutInflater inflater = LayoutInflater.from(context);
+        View view;
+        switch (viewType) {
+            case ResultPayload.PayloadType.INTENT:
+                view = inflater.inflate(R.layout.car_ui_preference, parent,
+                        /* attachToRoot= */ false);
+                return new CarIntentSearchViewHolder(view);
+            case ResultPayload.PayloadType.SAVED_QUERY:
+                view = inflater.inflate(R.layout.car_ui_preference, parent,
+                        /* attachToRoot= */ false);
+                return new CarSavedQueryViewHolder(view);
+            default:
+                return null;
+        }
+    }
+
+    @Override
+    public void onBindViewHolder(CarSearchViewHolder holder, int position) {
+        holder.onBind(mFragment, mSearchResults.get(position));
+    }
+
+    @Override
+    public long getItemId(int position) {
+        return mSearchResults.get(position).hashCode();
+    }
+
+    @Override
+    public int getItemViewType(int position) {
+        return mSearchResults.get(position).viewType;
+    }
+
+    @Override
+    public int getItemCount() {
+        return mSearchResults.size();
+    }
+
+    protected void postSearchResults(List<? extends SearchResult> newSearchResults) {
+        DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(
+                new SearchResultDiffCallback(mSearchResults, newSearchResults));
+        mSearchResults.clear();
+        mSearchResults.addAll(newSearchResults);
+        diffResult.dispatchUpdatesTo(/* adapter= */ this);
+    }
+
+    /**
+     * Displays recent searched queries.
+     */
+    public void displaySavedQuery(List<? extends SearchResult> data) {
+        clearResults();
+        mSearchResults.addAll(data);
+        notifyDataSetChanged();
+    }
+
+    /**
+     * Clear current search results.
+     */
+    public void clearResults() {
+        mSearchResults.clear();
+        notifyDataSetChanged();
+    }
+
+    /**
+     * Get current search results.
+     */
+    public List<SearchResult> getSearchResults() {
+        return mSearchResults;
+    }
+}
diff --git a/src/com/android/settings/intelligence/search/car/CarSearchViewHolder.java b/src/com/android/settings/intelligence/search/car/CarSearchViewHolder.java
new file mode 100644
index 0000000..2657d9a
--- /dev/null
+++ b/src/com/android/settings/intelligence/search/car/CarSearchViewHolder.java
@@ -0,0 +1,50 @@
+/*
+ * Copyright (C) 2020 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.settings.intelligence.search.car;
+
+import android.content.Context;
+import android.view.View;
+import android.widget.ImageView;
+import android.widget.TextView;
+
+import androidx.recyclerview.widget.RecyclerView;
+
+import com.android.settings.intelligence.search.SearchResult;
+
+/** The ViewHolder for the car search RecyclerView.
+ * There are multiple search result types with different UI requirements, such as Intent results
+ * and saved query results.
+ */
+public abstract class CarSearchViewHolder extends RecyclerView.ViewHolder {
+    protected Context mContext;
+    protected ImageView mIcon;
+    protected TextView mTitle;
+    protected TextView mSummary;
+
+    public CarSearchViewHolder(View view) {
+        super(view);
+        mContext = view.getContext();
+        mIcon = view.findViewById(android.R.id.icon);
+        mTitle = view.findViewById(android.R.id.title);
+        mSummary = view.findViewById(android.R.id.summary);
+    }
+
+    /**
+     * Update the ViewHolder data when bound.
+     */
+    public abstract void onBind(CarSearchFragment fragment, SearchResult result);
+}
diff --git a/src/com/android/settings/intelligence/search/savedqueries/SavedQueryController.java b/src/com/android/settings/intelligence/search/savedqueries/SavedQueryController.java
index ab0c11c..95dfde4 100644
--- a/src/com/android/settings/intelligence/search/savedqueries/SavedQueryController.java
+++ b/src/com/android/settings/intelligence/search/savedqueries/SavedQueryController.java
@@ -26,8 +26,8 @@
 
 import com.android.settings.intelligence.R;
 import com.android.settings.intelligence.overlay.FeatureFactory;
+import com.android.settings.intelligence.search.SearchCommon;
 import com.android.settings.intelligence.search.SearchFeatureProvider;
-import com.android.settings.intelligence.search.SearchFragment;
 import com.android.settings.intelligence.search.SearchResult;
 import com.android.settings.intelligence.search.SearchResultsAdapter;
 
@@ -59,11 +59,11 @@
     @Override
     public Loader onCreateLoader(int id, Bundle args) {
         switch (id) {
-            case SearchFragment.SearchLoaderId.SAVE_QUERY_TASK:
+            case SearchCommon.SearchLoaderId.SAVE_QUERY_TASK:
                 return new SavedQueryRecorder(mContext, args.getString(ARG_QUERY));
-            case SearchFragment.SearchLoaderId.REMOVE_QUERY_TASK:
+            case SearchCommon.SearchLoaderId.REMOVE_QUERY_TASK:
                 return new SavedQueryRemover(mContext);
-            case SearchFragment.SearchLoaderId.SAVED_QUERIES:
+            case SearchCommon.SearchLoaderId.SAVED_QUERIES:
                 return mSearchFeatureProvider.getSavedQueryLoader(mContext);
         }
         return null;
@@ -72,11 +72,11 @@
     @Override
     public void onLoadFinished(Loader loader, Object data) {
         switch (loader.getId()) {
-            case SearchFragment.SearchLoaderId.REMOVE_QUERY_TASK:
-                mLoaderManager.restartLoader(SearchFragment.SearchLoaderId.SAVED_QUERIES,
+            case SearchCommon.SearchLoaderId.REMOVE_QUERY_TASK:
+                mLoaderManager.restartLoader(SearchCommon.SearchLoaderId.SAVED_QUERIES,
                         null /* args */, this /* callback */);
                 break;
-            case SearchFragment.SearchLoaderId.SAVED_QUERIES:
+            case SearchCommon.SearchLoaderId.SAVED_QUERIES:
                 if (SearchFeatureProvider.DEBUG) {
                     Log.d(TAG, "Saved queries loaded");
                 }
@@ -107,7 +107,7 @@
     public void saveQuery(String query) {
         final Bundle args = new Bundle();
         args.putString(ARG_QUERY, query);
-        mLoaderManager.restartLoader(SearchFragment.SearchLoaderId.SAVE_QUERY_TASK, args,
+        mLoaderManager.restartLoader(SearchCommon.SearchLoaderId.SAVE_QUERY_TASK, args,
                 this /* callback */);
     }
 
@@ -116,7 +116,7 @@
      */
     public void removeQueries() {
         final Bundle args = new Bundle();
-        mLoaderManager.restartLoader(SearchFragment.SearchLoaderId.REMOVE_QUERY_TASK, args,
+        mLoaderManager.restartLoader(SearchCommon.SearchLoaderId.REMOVE_QUERY_TASK, args,
                 this /* callback */);
     }
 
@@ -124,7 +124,7 @@
         if (SearchFeatureProvider.DEBUG) {
             Log.d(TAG, "loading saved queries");
         }
-        mLoaderManager.restartLoader(SearchFragment.SearchLoaderId.SAVED_QUERIES, null /* args */,
+        mLoaderManager.restartLoader(SearchCommon.SearchLoaderId.SAVED_QUERIES, null /* args */,
                 this /* callback */);
     }
 }
diff --git a/src/com/android/settings/intelligence/search/savedqueries/car/CarSavedQueryController.java b/src/com/android/settings/intelligence/search/savedqueries/car/CarSavedQueryController.java
new file mode 100644
index 0000000..fa222a0
--- /dev/null
+++ b/src/com/android/settings/intelligence/search/savedqueries/car/CarSavedQueryController.java
@@ -0,0 +1,133 @@
+/*
+ * Copyright (C) 2020 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.settings.intelligence.search.savedqueries.car;
+
+import android.content.Context;
+import android.os.Bundle;
+import android.util.Log;
+import android.view.MenuItem;
+
+import androidx.loader.app.LoaderManager;
+import androidx.loader.content.Loader;
+
+import com.android.settings.intelligence.overlay.FeatureFactory;
+import com.android.settings.intelligence.search.SearchCommon;
+import com.android.settings.intelligence.search.SearchFeatureProvider;
+import com.android.settings.intelligence.search.SearchResult;
+import com.android.settings.intelligence.search.car.CarSearchResultsAdapter;
+import com.android.settings.intelligence.search.savedqueries.SavedQueryRecorder;
+import com.android.settings.intelligence.search.savedqueries.SavedQueryRemover;
+
+import java.util.List;
+
+/**
+ * Helper class for managing saved queries.
+ */
+public class CarSavedQueryController implements LoaderManager.LoaderCallbacks,
+        MenuItem.OnMenuItemClickListener {
+
+    private static final String ARG_QUERY = "remove_query";
+    private static final String TAG = "CarSearchSavedQueryCtrl";
+
+    private static final int MENU_SEARCH_HISTORY = 1000;
+
+    private final Context mContext;
+    private final LoaderManager mLoaderManager;
+    private final SearchFeatureProvider mSearchFeatureProvider;
+    private final CarSearchResultsAdapter mResultAdapter;
+
+    public CarSavedQueryController(Context context, LoaderManager loaderManager,
+            CarSearchResultsAdapter resultsAdapter) {
+        mContext = context;
+        mLoaderManager = loaderManager;
+        mResultAdapter = resultsAdapter;
+        mSearchFeatureProvider = FeatureFactory.get(context)
+                .searchFeatureProvider();
+    }
+
+    @Override
+    public Loader onCreateLoader(int id, Bundle args) {
+        switch (id) {
+            case SearchCommon.SearchLoaderId.SAVE_QUERY_TASK:
+                return new SavedQueryRecorder(mContext, args.getString(ARG_QUERY));
+            case SearchCommon.SearchLoaderId.REMOVE_QUERY_TASK:
+                return new SavedQueryRemover(mContext);
+            case SearchCommon.SearchLoaderId.SAVED_QUERIES:
+                return mSearchFeatureProvider.getSavedQueryLoader(mContext);
+        }
+        return null;
+    }
+
+    @Override
+    public void onLoadFinished(Loader loader, Object data) {
+        switch (loader.getId()) {
+            case SearchCommon.SearchLoaderId.REMOVE_QUERY_TASK:
+                mLoaderManager.restartLoader(SearchCommon.SearchLoaderId.SAVED_QUERIES,
+                        /* args= */ null, /* callback= */ this);
+                break;
+            case SearchCommon.SearchLoaderId.SAVED_QUERIES:
+                if (SearchFeatureProvider.DEBUG) {
+                    Log.d(TAG, "Saved queries loaded");
+                }
+                mResultAdapter.displaySavedQuery((List<SearchResult>) data);
+                break;
+        }
+    }
+
+    @Override
+    public void onLoaderReset(Loader loader) {
+    }
+
+    @Override
+    public boolean onMenuItemClick(MenuItem item) {
+        if (item.getItemId() != MENU_SEARCH_HISTORY) {
+            return false;
+        }
+        removeQueries();
+        return true;
+    }
+
+    /**
+     * Save a query to the DB.
+     */
+    public void saveQuery(String query) {
+        Bundle args = new Bundle();
+        args.putString(ARG_QUERY, query);
+        mLoaderManager.restartLoader(SearchCommon.SearchLoaderId.SAVE_QUERY_TASK, args,
+                /* callback= */ this);
+    }
+
+    /**
+     * Remove all saved queries from the DB.
+     */
+    public void removeQueries() {
+        Bundle args = new Bundle();
+        mLoaderManager.restartLoader(SearchCommon.SearchLoaderId.REMOVE_QUERY_TASK, args,
+                /* callback= */ this);
+    }
+
+    /**
+     * Load the saved queries from the DB.
+     */
+    public void loadSavedQueries() {
+        if (SearchFeatureProvider.DEBUG) {
+            Log.d(TAG, "loading saved queries");
+        }
+        mLoaderManager.restartLoader(SearchCommon.SearchLoaderId.SAVED_QUERIES,
+                /* args= */ null, /* callback= */ this);
+    }
+}
\ No newline at end of file
diff --git a/src/com/android/settings/intelligence/search/savedqueries/car/CarSavedQueryViewHolder.java b/src/com/android/settings/intelligence/search/savedqueries/car/CarSavedQueryViewHolder.java
new file mode 100644
index 0000000..00cff25
--- /dev/null
+++ b/src/com/android/settings/intelligence/search/savedqueries/car/CarSavedQueryViewHolder.java
@@ -0,0 +1,43 @@
+/*
+ * Copyright (C) 2020 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.settings.intelligence.search.savedqueries.car;
+
+import android.view.View;
+
+import com.android.settings.intelligence.R;
+import com.android.settings.intelligence.search.car.CarSearchFragment;
+import com.android.settings.intelligence.search.car.CarSearchViewHolder;
+import com.android.settings.intelligence.search.SearchResult;
+
+/**
+ * ViewHolder for saved queries from past searches.
+ */
+public class CarSavedQueryViewHolder extends CarSearchViewHolder {
+
+    public CarSavedQueryViewHolder(View view) {
+        super(view);
+    }
+
+    @Override
+    public void onBind(CarSearchFragment fragment, SearchResult result) {
+        mTitle.setText(result.title);
+        mIcon.setImageResource(R.drawable.ic_restore);
+        itemView.setOnClickListener(v -> {
+            fragment.onSavedQueryClicked(CarSavedQueryViewHolder.this, result.title);
+        });
+    }
+}