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