1 /* 2 * Copyright (C) 2019 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.homepage.contextualcards.slices; 18 19 import android.app.PendingIntent; 20 import android.app.settings.SettingsEnums; 21 import android.content.Context; 22 import android.util.Log; 23 import android.view.View; 24 import android.widget.ImageView; 25 import android.widget.LinearLayout; 26 import android.widget.TextView; 27 28 import androidx.recyclerview.widget.RecyclerView; 29 import androidx.slice.Slice; 30 import androidx.slice.SliceMetadata; 31 import androidx.slice.core.SliceAction; 32 import androidx.slice.widget.EventInfo; 33 34 import com.android.settings.R; 35 import com.android.settings.homepage.contextualcards.ContextualCard; 36 import com.android.settings.homepage.contextualcards.logging.ContextualCardLogUtils; 37 import com.android.settings.overlay.FeatureFactory; 38 import com.android.settingslib.core.instrumentation.MetricsFeatureProvider; 39 40 /** 41 * Card renderer helper for {@link ContextualCard} built as slice half card. 42 */ 43 class SliceHalfCardRendererHelper { 44 private static final String TAG = "SliceHCRendererHelper"; 45 46 private final Context mContext; 47 SliceHalfCardRendererHelper(Context context)48 SliceHalfCardRendererHelper(Context context) { 49 mContext = context; 50 } 51 createViewHolder(View view)52 RecyclerView.ViewHolder createViewHolder(View view) { 53 return new HalfCardViewHolder(view); 54 } 55 bindView(RecyclerView.ViewHolder holder, ContextualCard card, Slice slice)56 void bindView(RecyclerView.ViewHolder holder, ContextualCard card, Slice slice) { 57 final HalfCardViewHolder view = (HalfCardViewHolder) holder; 58 final SliceMetadata sliceMetadata = SliceMetadata.from(mContext, slice); 59 final SliceAction primaryAction = sliceMetadata.getPrimaryAction(); 60 view.icon.setImageDrawable(primaryAction.getIcon().loadDrawable(mContext)); 61 view.title.setText(primaryAction.getTitle()); 62 view.content.setOnClickListener(v -> { 63 try { 64 primaryAction.getAction().send(); 65 } catch (PendingIntent.CanceledException e) { 66 Log.w(TAG, "Failed to start intent " + primaryAction.getTitle()); 67 } 68 final String log = ContextualCardLogUtils.buildCardClickLog(card, 0 /* row */, 69 EventInfo.ACTION_TYPE_CONTENT, view.getAdapterPosition()); 70 71 final MetricsFeatureProvider metricsFeatureProvider = 72 FeatureFactory.getFactory(mContext).getMetricsFeatureProvider(); 73 74 metricsFeatureProvider.action(mContext, 75 SettingsEnums.ACTION_CONTEXTUAL_CARD_CLICK, log); 76 }); 77 } 78 79 static class HalfCardViewHolder extends RecyclerView.ViewHolder { 80 public final LinearLayout content; 81 public final ImageView icon; 82 public final TextView title; 83 HalfCardViewHolder(View itemView)84 public HalfCardViewHolder(View itemView) { 85 super(itemView); 86 content = itemView.findViewById(R.id.content); 87 icon = itemView.findViewById(android.R.id.icon); 88 title = itemView.findViewById(android.R.id.title); 89 } 90 } 91 } 92