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