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 static com.android.settings.intelligence.search.indexing.DatabaseIndexingUtils.SEARCH_RESULT_TRAMPOLINE_ACTION;
20 
21 import android.content.Intent;
22 import android.content.pm.PackageManager;
23 import android.content.pm.ResolveInfo;
24 import android.util.Log;
25 import android.view.View;
26 
27 import androidx.annotation.VisibleForTesting;
28 
29 import com.android.settings.intelligence.nano.SettingsIntelligenceLogProto;
30 
31 import java.util.List;
32 
33 /**
34  * ViewHolder for intent based search results.
35  * The DatabaseResultTask is the primary use case for this ViewHolder.
36  */
37 public class IntentSearchViewHolder extends SearchViewHolder {
38 
39     private static final String TAG = "IntentSearchViewHolder";
40     @VisibleForTesting
41     static final int REQUEST_CODE_NO_OP = 0;
42 
IntentSearchViewHolder(View view)43     public IntentSearchViewHolder(View view) {
44         super(view);
45     }
46 
47     @Override
getClickActionMetricName()48     public int getClickActionMetricName() {
49         return SettingsIntelligenceLogProto.SettingsIntelligenceEvent.CLICK_SEARCH_RESULT;
50     }
51 
52     @Override
onBind(final SearchFragment fragment, final SearchResult result)53     public void onBind(final SearchFragment fragment, final SearchResult result) {
54         super.onBind(fragment, result);
55         final SearchViewHolder viewHolder = this;
56 
57         // TODO (b/64935342) add dynamic api
58         itemView.setOnClickListener(new View.OnClickListener() {
59             @Override
60             public void onClick(View v) {
61                 fragment.onSearchResultClicked(viewHolder, result);
62                 final Intent intent = result.payload.getIntent();
63                 // Use app user id to support work profile use case.
64                 if (result instanceof AppSearchResult) {
65                     if (SEARCH_RESULT_TRAMPOLINE_ACTION.equals(intent.getAction())) {
66                         fragment.startActivityForResult(intent, REQUEST_CODE_NO_OP);
67                     } else {
68                         fragment.startActivity(intent);
69                     }
70                 } else {
71                     final PackageManager pm = fragment.getActivity().getPackageManager();
72                     final List<ResolveInfo> info = pm.queryIntentActivities(intent, 0 /* flags */);
73                     if (info != null && !info.isEmpty()) {
74                         fragment.startActivityForResult(intent, REQUEST_CODE_NO_OP);
75                     } else {
76                         Log.e(TAG, "Cannot launch search result, title: "
77                                 + result.title + ", " + intent);
78                     }
79                 }
80             }
81         });
82     }
83 }
84