1 /* 2 * Copyright (C) 2017 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 18 package com.android.settings.intelligence.search; 19 20 import android.content.Context; 21 import androidx.recyclerview.widget.DiffUtil; 22 import androidx.recyclerview.widget.RecyclerView; 23 import android.view.LayoutInflater; 24 import android.view.View; 25 import android.view.ViewGroup; 26 27 import com.android.settings.intelligence.R; 28 import com.android.settings.intelligence.search.savedqueries.SavedQueryViewHolder; 29 30 import java.util.ArrayList; 31 import java.util.List; 32 33 public class SearchResultsAdapter extends RecyclerView.Adapter<SearchViewHolder> { 34 35 private final SearchFragment mFragment; 36 private final List<SearchResult> mSearchResults; 37 SearchResultsAdapter(SearchFragment fragment)38 public SearchResultsAdapter(SearchFragment fragment) { 39 mFragment = fragment; 40 mSearchResults = new ArrayList<>(); 41 42 setHasStableIds(true); 43 } 44 45 @Override onCreateViewHolder(ViewGroup parent, int viewType)46 public SearchViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 47 final Context context = parent.getContext(); 48 final LayoutInflater inflater = LayoutInflater.from(context); 49 final View view; 50 switch (viewType) { 51 case ResultPayload.PayloadType.INTENT: 52 view = inflater.inflate(R.layout.search_intent_item, parent, false); 53 return new IntentSearchViewHolder(view); 54 case ResultPayload.PayloadType.INLINE_SWITCH: 55 // TODO (b/62807132) replace layout InlineSwitchViewHolder and return an 56 // InlineSwitchViewHolder. 57 view = inflater.inflate(R.layout.search_intent_item, parent, false); 58 return new IntentSearchViewHolder(view); 59 case ResultPayload.PayloadType.INLINE_LIST: 60 // TODO (b/62807132) build a inline-list view holder & layout. 61 view = inflater.inflate(R.layout.search_intent_item, parent, false); 62 return new IntentSearchViewHolder(view); 63 case ResultPayload.PayloadType.SAVED_QUERY: 64 view = inflater.inflate(R.layout.search_saved_query_item, parent, false); 65 return new SavedQueryViewHolder(view); 66 default: 67 return null; 68 } 69 } 70 71 @Override onBindViewHolder(SearchViewHolder holder, int position)72 public void onBindViewHolder(SearchViewHolder holder, int position) { 73 holder.onBind(mFragment, mSearchResults.get(position)); 74 } 75 76 @Override getItemId(int position)77 public long getItemId(int position) { 78 return mSearchResults.get(position).hashCode(); 79 } 80 81 @Override getItemViewType(int position)82 public int getItemViewType(int position) { 83 return mSearchResults.get(position).viewType; 84 } 85 86 @Override getItemCount()87 public int getItemCount() { 88 return mSearchResults.size(); 89 } 90 91 /** 92 * Displays recent searched queries. 93 */ displaySavedQuery(List<? extends SearchResult> data)94 public void displaySavedQuery(List<? extends SearchResult> data) { 95 clearResults(); 96 mSearchResults.addAll(data); 97 notifyDataSetChanged(); 98 } 99 clearResults()100 public void clearResults() { 101 mSearchResults.clear(); 102 notifyDataSetChanged(); 103 } 104 postSearchResults(List<? extends SearchResult> newSearchResults)105 public void postSearchResults(List<? extends SearchResult> newSearchResults) { 106 final DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff( 107 new SearchResultDiffCallback(mSearchResults, newSearchResults)); 108 mSearchResults.clear(); 109 mSearchResults.addAll(newSearchResults); 110 diffResult.dispatchUpdatesTo(this); 111 mFragment.onSearchResultsDisplayed(mSearchResults.size()); 112 } 113 getSearchResults()114 public List<SearchResult> getSearchResults() { 115 return mSearchResults; 116 } 117 } 118