1 /*
2  * Copyright (C) 2020 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.car;
18 
19 import android.content.Intent;
20 import android.content.pm.PackageManager;
21 import android.content.pm.ResolveInfo;
22 import android.text.TextUtils;
23 import android.util.Log;
24 import android.view.View;
25 
26 import com.android.settings.intelligence.R;
27 import com.android.settings.intelligence.search.AppSearchResult;
28 import com.android.settings.intelligence.search.SearchResult;
29 
30 import java.util.List;
31 
32 /**
33  * ViewHolder for intent based search results.
34  */
35 public class CarIntentSearchViewHolder extends CarSearchViewHolder {
36     private static final String TAG = "CarIntentSearchViewHolder";
37     private static final int REQUEST_CODE_NO_OP = 0;
38 
CarIntentSearchViewHolder(View view)39     public CarIntentSearchViewHolder(View view) {
40         super(view);
41     }
42 
43     @Override
onBind(CarSearchFragment fragment, SearchResult result)44     public void onBind(CarSearchFragment fragment, SearchResult result) {
45         mTitle.setText(result.title);
46         if (result instanceof AppSearchResult) {
47             AppSearchResult appResult = (AppSearchResult) result;
48             PackageManager pm = fragment.getActivity().getPackageManager();
49             mIcon.setImageDrawable(appResult.info.loadIcon(pm));
50         } else {
51             mIcon.setImageDrawable(result.icon);
52         }
53         bindBreadcrumbView(result);
54 
55         itemView.setOnClickListener(v -> {
56             fragment.onSearchResultClicked(/* resultViewHolder= */ this, result);
57             Intent intent = result.payload.getIntent();
58             if (result instanceof AppSearchResult) {
59                 AppSearchResult appResult = (AppSearchResult) result;
60                 fragment.getActivity().startActivity(intent);
61             } else {
62                 PackageManager pm = fragment.getActivity().getPackageManager();
63                 List<ResolveInfo> info = pm.queryIntentActivities(intent, /* flags= */ 0);
64                 if (info != null && !info.isEmpty()) {
65                     fragment.startActivityForResult(intent, REQUEST_CODE_NO_OP);
66                 } else {
67                     Log.e(TAG, "Cannot launch search result, title: "
68                             + result.title + ", " + intent);
69                 }
70             }
71         });
72     }
73 
bindBreadcrumbView(SearchResult result)74     private void bindBreadcrumbView(SearchResult result) {
75         if (result.breadcrumbs == null || result.breadcrumbs.isEmpty()) {
76             mSummary.setVisibility(View.GONE);
77             return;
78         }
79         String breadcrumb = result.breadcrumbs.get(0);
80         int count = result.breadcrumbs.size();
81         for (int i = 1; i < count; i++) {
82             breadcrumb = mContext.getString(R.string.search_breadcrumb_connector,
83                     breadcrumb, result.breadcrumbs.get(i));
84         }
85         if (breadcrumb == null || TextUtils.isEmpty(breadcrumb.trim())) {
86             mSummary.setVisibility(View.GONE);
87         } else {
88             mSummary.setText(breadcrumb);
89             mSummary.setVisibility(View.VISIBLE);
90         }
91     }
92 }
93