1 /*
2  * Copyright (C) 2023 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.adservices.service.ui.enrollment.impl;
18 
19 import static com.android.adservices.service.consent.AdServicesApiType.FLEDGE;
20 import static com.android.adservices.service.consent.AdServicesApiType.MEASUREMENTS;
21 import static com.android.adservices.service.consent.AdServicesApiType.TOPICS;
22 import static com.android.adservices.service.consent.ConsentManager.NO_MANUAL_INTERACTIONS_RECORDED;
23 
24 import android.content.Context;
25 import android.content.SharedPreferences;
26 import android.os.Build;
27 import android.text.TextUtils;
28 
29 import androidx.annotation.RequiresApi;
30 
31 import com.android.adservices.LogUtil;
32 import com.android.adservices.service.FlagsFactory;
33 import com.android.adservices.service.consent.ConsentManager;
34 import com.android.adservices.service.ui.data.UxStatesManager;
35 import com.android.adservices.service.ui.enrollment.base.PrivacySandboxEnrollmentChannel;
36 import com.android.adservices.service.ui.ux.collection.PrivacySandboxUxCollection;
37 import com.android.modules.utils.build.SdkLevel;
38 
39 import java.util.Objects;
40 
41 /**
42  * Enrollment channel for resetting consent notification info, similar to the consent notification
43  * debug channel, this channel is only used for testing. Unlike the debug channel, this channel
44  * resets the consent data exactly once per token.
45  */
46 @RequiresApi(Build.VERSION_CODES.S)
47 public class ConsentNotificationResetChannel implements PrivacySandboxEnrollmentChannel {
48 
49     public static final String CONSENT_NOTIFICATION_RESET_TOKEN =
50             "CONSENT_NOTIFICATION_RESET_TOKEN";
51 
52     /** Determines if user is eligible for the consent notification reset channel */
isEligible( PrivacySandboxUxCollection uxCollection, ConsentManager consentManager, UxStatesManager uxStatesManager)53     public boolean isEligible(
54             PrivacySandboxUxCollection uxCollection,
55             ConsentManager consentManager,
56             UxStatesManager uxStatesManager) {
57         String currentConsentNotificationResetToken =
58                 FlagsFactory.getFlags().getConsentNotificationResetToken();
59         // Explicitly ignore default token and to avoid double-reset.
60         if (TextUtils.isEmpty(currentConsentNotificationResetToken)) {
61             return false;
62         }
63 
64         SharedPreferences uxSharedPreferences = uxStatesManager.getUxSharedPreferences();
65         String storedConsentNotificationResetToken = uxSharedPreferences.getString(
66                 CONSENT_NOTIFICATION_RESET_TOKEN, /* defValue= */ "");
67 
68         if (!Objects.equals(storedConsentNotificationResetToken,
69                 currentConsentNotificationResetToken)) {
70             SharedPreferences.Editor editor = uxSharedPreferences.edit();
71             editor.putString(CONSENT_NOTIFICATION_RESET_TOKEN,
72                     currentConsentNotificationResetToken);
73             // Consent notification reset can proceed only if the write operation succeeded.
74             return editor.commit();
75         }
76         return false;
77     }
78 
79     /** Perform enrollment logic for the reset channel. */
enroll(Context context, ConsentManager consentManager)80     public void enroll(Context context, ConsentManager consentManager) {
81         consentManager.recordUserManualInteractionWithConsent(NO_MANUAL_INTERACTIONS_RECORDED);
82         LogUtil.d("Reset measurement consent bit.");
83         consentManager.disable(context, MEASUREMENTS);
84         if (SdkLevel.isAtLeastS()) {
85             LogUtil.d("Reset adservice notification bit.");
86             consentManager.recordNotificationDisplayed(false);
87             consentManager.recordGaUxNotificationDisplayed(false);
88             LogUtil.d("Reset topics and fledge consent bit.");
89             consentManager.disable(context, TOPICS);
90             consentManager.disable(context, FLEDGE);
91         }
92         if (SdkLevel.isAtLeastT()) {
93             LogUtil.d("Reset pas notification bit.");
94             consentManager.recordPasNotificationDisplayed(false);
95             consentManager.recordPasNotificationOpened(false);
96         }
97 
98         consentManager.setU18NotificationDisplayed(false);
99         consentManager.setU18Account(false);
100         LogUtil.d("Consent data has been reset.");
101     }
102 }
103