1 /*
2  * Copyright (C) 2018 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.conditional;
18 
19 import android.app.settings.SettingsEnums;
20 import android.content.Context;
21 import android.text.TextUtils;
22 import android.view.View;
23 import android.widget.Button;
24 import android.widget.ImageView;
25 import android.widget.TextView;
26 
27 import androidx.annotation.LayoutRes;
28 import androidx.recyclerview.widget.RecyclerView;
29 
30 import com.android.settings.R;
31 import com.android.settings.homepage.contextualcards.ContextualCard;
32 import com.android.settings.homepage.contextualcards.ContextualCardRenderer;
33 import com.android.settings.homepage.contextualcards.ControllerRendererPool;
34 import com.android.settings.overlay.FeatureFactory;
35 import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
36 
37 /**
38  * Card renderer for {@link ConditionalContextualCard}.
39  */
40 public class ConditionContextualCardRenderer implements ContextualCardRenderer {
41     @LayoutRes
42     public static final int VIEW_TYPE_HALF_WIDTH = R.layout.conditional_card_half_tile;
43     @LayoutRes
44     public static final int VIEW_TYPE_FULL_WIDTH = R.layout.conditional_card_full_tile;
45 
46     private final Context mContext;
47     private final ControllerRendererPool mControllerRendererPool;
48 
ConditionContextualCardRenderer(Context context, ControllerRendererPool controllerRendererPool)49     public ConditionContextualCardRenderer(Context context,
50             ControllerRendererPool controllerRendererPool) {
51         mContext = context;
52         mControllerRendererPool = controllerRendererPool;
53     }
54 
55     @Override
createViewHolder(View view, @LayoutRes int viewType)56     public RecyclerView.ViewHolder createViewHolder(View view, @LayoutRes int viewType) {
57         return new ConditionalCardHolder(view);
58     }
59 
60     @Override
bindView(RecyclerView.ViewHolder holder, ContextualCard contextualCard)61     public void bindView(RecyclerView.ViewHolder holder, ContextualCard contextualCard) {
62         final ConditionalCardHolder view = (ConditionalCardHolder) holder;
63         final ConditionalContextualCard card = (ConditionalContextualCard) contextualCard;
64         final MetricsFeatureProvider metricsFeatureProvider = FeatureFactory.getFactory(
65                 mContext).getMetricsFeatureProvider();
66 
67         metricsFeatureProvider.visible(mContext, SettingsEnums.SETTINGS_HOMEPAGE,
68                 card.getMetricsConstant(), 0);
69         initializePrimaryClick(view, card, metricsFeatureProvider);
70         initializeView(view, card);
71         initializeActionButton(view, card, metricsFeatureProvider);
72     }
73 
initializePrimaryClick(ConditionalCardHolder view, ConditionalContextualCard card, MetricsFeatureProvider metricsFeatureProvider)74     private void initializePrimaryClick(ConditionalCardHolder view, ConditionalContextualCard card,
75             MetricsFeatureProvider metricsFeatureProvider) {
76         view.itemView.findViewById(R.id.content).setOnClickListener(
77                 v -> {
78                     metricsFeatureProvider.action(mContext,
79                             SettingsEnums.ACTION_SETTINGS_CONDITION_CLICK,
80                             card.getMetricsConstant());
81                     mControllerRendererPool.getController(mContext,
82                             card.getCardType()).onPrimaryClick(card);
83                 });
84     }
85 
initializeView(ConditionalCardHolder view, ConditionalContextualCard card)86     private void initializeView(ConditionalCardHolder view, ConditionalContextualCard card) {
87         view.icon.setImageDrawable(card.getIconDrawable());
88         view.title.setText(card.getTitleText());
89         view.summary.setText(card.getSummaryText());
90     }
91 
initializeActionButton(ConditionalCardHolder view, ConditionalContextualCard card, MetricsFeatureProvider metricsFeatureProvider)92     private void initializeActionButton(ConditionalCardHolder view, ConditionalContextualCard card,
93             MetricsFeatureProvider metricsFeatureProvider) {
94         final CharSequence action = card.getActionText();
95         final boolean hasButtons = !TextUtils.isEmpty(action);
96 
97         final Button button = view.itemView.findViewById(R.id.first_action);
98         if (hasButtons) {
99             button.setVisibility(View.VISIBLE);
100             button.setText(action);
101             button.setOnClickListener(v -> {
102                 final Context viewContext = v.getContext();
103                 metricsFeatureProvider.action(
104                         viewContext, SettingsEnums.ACTION_SETTINGS_CONDITION_BUTTON,
105                         card.getMetricsConstant());
106                 mControllerRendererPool.getController(mContext, card.getCardType())
107                         .onActionClick(card);
108             });
109         } else {
110             button.setVisibility(View.GONE);
111         }
112     }
113 
114     public static class ConditionalCardHolder extends RecyclerView.ViewHolder {
115 
116         public final ImageView icon;
117         public final TextView title;
118         public final TextView summary;
119 
ConditionalCardHolder(View itemView)120         public ConditionalCardHolder(View itemView) {
121             super(itemView);
122             icon = itemView.findViewById(android.R.id.icon);
123             title = itemView.findViewById(android.R.id.title);
124             summary = itemView.findViewById(android.R.id.summary);
125         }
126     }
127 }
128