1 /*
2  * Copyright (C) 2016 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 package com.android.settings.search2;
17 
18 import android.content.ComponentName;
19 import android.content.Intent;
20 import android.os.UserHandle;
21 import android.text.TextUtils;
22 import android.util.Pair;
23 import android.view.View;
24 
25 import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
26 import com.android.settings.SettingsActivity;
27 
28 /**
29  * ViewHolder for intent based search results.
30  * The DatabaseResultLoader is the primary use case for this ViewHolder.
31  */
32 public class IntentSearchViewHolder extends SearchViewHolder {
33 
IntentSearchViewHolder(View view)34     public IntentSearchViewHolder(View view) {
35         super(view);
36     }
37 
38     @Override
onBind(final SearchFragment fragment, final SearchResult result)39     public void onBind(final SearchFragment fragment, final SearchResult result) {
40         super.onBind(fragment, result);
41 
42         itemView.setOnClickListener(v -> {
43             fragment.onSearchResultClicked();
44             final Intent intent = ((IntentPayload) result.payload).intent;
45             final ComponentName cn = intent.getComponent();
46             final Pair<Integer, Object> rank = Pair.create(
47                     MetricsEvent.FIELD_SETTINGS_SERACH_RESULT_RANK, getAdapterPosition());
48             String resultName = intent.getStringExtra(SettingsActivity.EXTRA_SHOW_FRAGMENT);
49             if (TextUtils.isEmpty(resultName) && cn != null) {
50                 resultName = cn.flattenToString();
51             }
52             mMetricsFeatureProvider.action(v.getContext(),
53                     MetricsEvent.ACTION_CLICK_SETTINGS_SEARCH_RESULT,
54                     resultName, rank);
55             // Use app user id to support work profile use case.
56             if (result instanceof AppSearchResult) {
57                 AppSearchResult appResult = (AppSearchResult) result;
58                 UserHandle userHandle = appResult.getAppUserHandle();
59                 fragment.getActivity().startActivityAsUser(intent, userHandle);
60             } else {
61                 fragment.startActivity(intent);
62             }
63         });
64     }
65 }
66