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.settings.SettingsEnums;
20 import android.content.Context;
21 import android.view.View;
22 
23 import androidx.recyclerview.widget.RecyclerView;
24 import androidx.slice.Slice;
25 import androidx.slice.widget.SliceView;
26 
27 import com.android.settings.R;
28 import com.android.settings.homepage.contextualcards.ContextualCard;
29 import com.android.settings.homepage.contextualcards.logging.ContextualCardLogUtils;
30 import com.android.settings.overlay.FeatureFactory;
31 import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
32 
33 /**
34  * Card renderer helper for {@link ContextualCard} built as slice full card.
35  */
36 class SliceFullCardRendererHelper {
37 
38     private final Context mContext;
39 
SliceFullCardRendererHelper(Context context)40     SliceFullCardRendererHelper(Context context) {
41         mContext = context;
42     }
43 
createViewHolder(View view)44     RecyclerView.ViewHolder createViewHolder(View view) {
45         return new SliceViewHolder(view);
46     }
47 
bindView(RecyclerView.ViewHolder holder, ContextualCard card, Slice slice)48     void bindView(RecyclerView.ViewHolder holder, ContextualCard card, Slice slice) {
49         final SliceViewHolder cardHolder = (SliceViewHolder) holder;
50         cardHolder.sliceView.setScrollable(false);
51         cardHolder.sliceView.setTag(card.getSliceUri());
52         cardHolder.sliceView.setMode(SliceView.MODE_LARGE);
53         cardHolder.sliceView.setSlice(slice);
54         // Set this listener so we can log the interaction users make on the slice
55         cardHolder.sliceView.setOnSliceActionListener((eventInfo, sliceItem) -> {
56             final String log = ContextualCardLogUtils.buildCardClickLog(card, eventInfo.rowIndex,
57                     eventInfo.actionType, cardHolder.getAdapterPosition());
58 
59             final MetricsFeatureProvider metricsFeatureProvider =
60                     FeatureFactory.getFactory(mContext).getMetricsFeatureProvider();
61 
62             metricsFeatureProvider.action(mContext,
63                     SettingsEnums.ACTION_CONTEXTUAL_CARD_CLICK, log);
64         });
65 
66         // Customize slice view for Settings
67         cardHolder.sliceView.setShowTitleItems(true);
68         if (card.isLargeCard()) {
69             cardHolder.sliceView.setShowHeaderDivider(true);
70             cardHolder.sliceView.setShowActionDividers(true);
71         }
72     }
73 
74     static class SliceViewHolder extends RecyclerView.ViewHolder {
75         public final SliceView sliceView;
76 
SliceViewHolder(View view)77         public SliceViewHolder(View view) {
78             super(view);
79             sliceView = view.findViewById(R.id.slice_view);
80         }
81     }
82 }
83