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.slices;
18 
19 import android.app.settings.SettingsEnums;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.os.Build;
23 import android.text.TextUtils;
24 
25 import androidx.annotation.VisibleForTesting;
26 
27 import com.android.settings.R;
28 import com.android.settings.homepage.contextualcards.ContextualCard;
29 import com.android.settings.homepage.contextualcards.ContextualCardController;
30 import com.android.settings.homepage.contextualcards.ContextualCardFeatureProvider;
31 import com.android.settings.homepage.contextualcards.ContextualCardFeedbackDialog;
32 import com.android.settings.homepage.contextualcards.ContextualCardUpdateListener;
33 import com.android.settings.homepage.contextualcards.logging.ContextualCardLogUtils;
34 import com.android.settings.overlay.FeatureFactory;
35 import com.android.settingslib.core.instrumentation.MetricsFeatureProvider;
36 import com.android.settingslib.utils.ThreadUtils;
37 
38 /**
39  * Card controller for {@link ContextualCard} built as slices.
40  */
41 public class SliceContextualCardController implements ContextualCardController {
42 
43     private static final String TAG = "SliceCardController";
44 
45     private final Context mContext;
46 
47     private ContextualCardUpdateListener mCardUpdateListener;
48 
SliceContextualCardController(Context context)49     public SliceContextualCardController(Context context) {
50         mContext = context;
51     }
52 
53     @Override
getCardType()54     public int getCardType() {
55         return ContextualCard.CardType.SLICE;
56     }
57 
58     @Override
onPrimaryClick(ContextualCard card)59     public void onPrimaryClick(ContextualCard card) {
60 
61     }
62 
63     @Override
onActionClick(ContextualCard card)64     public void onActionClick(ContextualCard card) {
65 
66     }
67 
68     @Override
onDismissed(ContextualCard card)69     public void onDismissed(ContextualCard card) {
70         ThreadUtils.postOnBackgroundThread(() -> {
71             final ContextualCardFeatureProvider cardFeatureProvider =
72                     FeatureFactory.getFactory(mContext).getContextualCardFeatureProvider(mContext);
73             cardFeatureProvider.markCardAsDismissed(mContext, card.getName());
74         });
75         showFeedbackDialog(card);
76 
77         final MetricsFeatureProvider metricsFeatureProvider =
78                 FeatureFactory.getFactory(mContext).getMetricsFeatureProvider();
79 
80         metricsFeatureProvider.action(mContext,
81                 SettingsEnums.ACTION_CONTEXTUAL_CARD_DISMISS,
82                 ContextualCardLogUtils.buildCardDismissLog(card));
83     }
84 
85     @Override
setCardUpdateListener(ContextualCardUpdateListener listener)86     public void setCardUpdateListener(ContextualCardUpdateListener listener) {
87         mCardUpdateListener = listener;
88     }
89 
90     @VisibleForTesting
showFeedbackDialog(ContextualCard card)91     void showFeedbackDialog(ContextualCard card) {
92         final String email = mContext.getString(R.string.config_contextual_card_feedback_email);
93         if (!isFeedbackEnabled(email)) {
94             return;
95         }
96         final Intent feedbackIntent = new Intent(mContext, ContextualCardFeedbackDialog.class);
97         feedbackIntent.putExtra(ContextualCardFeedbackDialog.EXTRA_CARD_NAME,
98                 getSimpleCardName(card));
99         feedbackIntent.putExtra(ContextualCardFeedbackDialog.EXTRA_FEEDBACK_EMAIL, email);
100         feedbackIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
101         mContext.startActivity(feedbackIntent);
102     }
103 
104     @VisibleForTesting
isFeedbackEnabled(String email)105     boolean isFeedbackEnabled(String email) {
106         return !TextUtils.isEmpty(email) && Build.IS_DEBUGGABLE;
107     }
108 
getSimpleCardName(ContextualCard card)109     private String getSimpleCardName(ContextualCard card) {
110         final String[] split = card.getName().split("/");
111         return split[split.length - 1];
112     }
113 }
114