Home | History | Annotate | Download | only in search
      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 package com.android.settings.intelligence.search;
     18 
     19 import android.content.Context;
     20 import android.content.pm.PackageManager;
     21 import android.support.v7.widget.RecyclerView;
     22 import android.text.TextUtils;
     23 import android.view.View;
     24 import android.widget.ImageView;
     25 import android.widget.TextView;
     26 
     27 import com.android.settings.intelligence.R;
     28 import com.android.settings.intelligence.overlay.FeatureFactory;
     29 
     30 /**
     31  * The ViewHolder for the Search RecyclerView.
     32  * There are multiple search result types in the same Recycler view with different UI requirements.
     33  * Some examples include Intent results, Inline results, and Help articles.
     34  */
     35 public abstract class SearchViewHolder extends RecyclerView.ViewHolder {
     36 
     37     private final String DYNAMIC_PLACEHOLDER = "%s";
     38 
     39     private final String mPlaceholderSummary;
     40 
     41     public final TextView titleView;
     42     public final TextView summaryView;
     43     public final TextView breadcrumbView;
     44     public final ImageView iconView;
     45 
     46     protected final SearchFeatureProvider mSearchFeatureProvider;
     47 
     48     public SearchViewHolder(View view) {
     49         super(view);
     50         final FeatureFactory featureFactory = FeatureFactory
     51                 .get(view.getContext().getApplicationContext());
     52         mSearchFeatureProvider = featureFactory.searchFeatureProvider();
     53         titleView = view.findViewById(android.R.id.title);
     54         summaryView = view.findViewById(android.R.id.summary);
     55         iconView = view.findViewById(android.R.id.icon);
     56         breadcrumbView = view.findViewById(R.id.breadcrumb);
     57 
     58         mPlaceholderSummary = view.getContext().getString(R.string.summary_placeholder);
     59     }
     60 
     61     public abstract int getClickActionMetricName();
     62 
     63     public void onBind(SearchFragment fragment, SearchResult result) {
     64         titleView.setText(result.title);
     65         // TODO (b/36101902) remove check for DYNAMIC_PLACEHOLDER
     66         if (TextUtils.isEmpty(result.summary)
     67                 || TextUtils.equals(result.summary, mPlaceholderSummary)
     68                 || TextUtils.equals(result.summary, DYNAMIC_PLACEHOLDER)) {
     69             summaryView.setVisibility(View.GONE);
     70         } else {
     71             summaryView.setText(result.summary);
     72             summaryView.setVisibility(View.VISIBLE);
     73         }
     74 
     75         if (result instanceof AppSearchResult) {
     76             AppSearchResult appResult = (AppSearchResult) result;
     77             PackageManager pm = fragment.getActivity().getPackageManager();
     78             iconView.setImageDrawable(appResult.info.loadIcon(pm));
     79         } else {
     80             // Valid even when result.icon is null.
     81             iconView.setImageDrawable(result.icon);
     82         }
     83 
     84         bindBreadcrumbView(result);
     85     }
     86 
     87     private void bindBreadcrumbView(SearchResult result) {
     88         if (result.breadcrumbs == null || result.breadcrumbs.isEmpty()) {
     89             breadcrumbView.setVisibility(View.GONE);
     90             return;
     91         }
     92         final Context context = breadcrumbView.getContext();
     93         String breadcrumb = result.breadcrumbs.get(0);
     94         final int count = result.breadcrumbs.size();
     95         for (int i = 1; i < count; i++) {
     96             breadcrumb = context.getString(R.string.search_breadcrumb_connector,
     97                     breadcrumb, result.breadcrumbs.get(i));
     98         }
     99         breadcrumbView.setText(breadcrumb);
    100         breadcrumbView.setVisibility(View.VISIBLE);
    101     }
    102 }