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.search;
     18 
     19 import android.content.Intent;
     20 import android.content.pm.PackageManager;
     21 import android.content.pm.ResolveInfo;
     22 import android.os.UserHandle;
     23 import android.util.Log;
     24 import android.view.View;
     25 
     26 import com.android.internal.logging.nano.MetricsProto;
     27 
     28 import java.util.List;
     29 
     30 /**
     31  * ViewHolder for intent based search results.
     32  * The DatabaseResultLoader is the primary use case for this ViewHolder.
     33  */
     34 public class IntentSearchViewHolder extends SearchViewHolder {
     35 
     36     private static final String TAG = "IntentSearchViewHolder";
     37 
     38     public IntentSearchViewHolder(View view) {
     39         super(view);
     40     }
     41 
     42     @Override
     43     public int getClickActionMetricName() {
     44         return MetricsProto.MetricsEvent.ACTION_CLICK_SETTINGS_SEARCH_RESULT;
     45     }
     46 
     47     @Override
     48     public void onBind(final SearchFragment fragment, final SearchResult result) {
     49         super.onBind(fragment, result);
     50 
     51         itemView.setOnClickListener(v -> {
     52             fragment.onSearchResultClicked(this, result);
     53             final Intent intent = result.payload.getIntent();
     54             // Use app user id to support work profile use case.
     55             if (result instanceof AppSearchResult) {
     56                 AppSearchResult appResult = (AppSearchResult) result;
     57                 UserHandle userHandle = appResult.getAppUserHandle();
     58                 fragment.getActivity().startActivityAsUser(intent, userHandle);
     59             } else {
     60                 final PackageManager pm = fragment.getActivity().getPackageManager();
     61                 final List<ResolveInfo> info = pm.queryIntentActivities(intent, 0 /* flags */);
     62                 if (info != null && !info.isEmpty()) {
     63                     fragment.startActivity(intent);
     64                 } else {
     65                     Log.e(TAG, "Cannot launch search result, title: "
     66                             + result.title + ", " + intent);
     67                 }
     68             }
     69         });
     70     }
     71 }
     72