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.FlagsConstants.KEY_CONSENT_ALREADY_INTERACTED_FIX_ENABLE;
20 import static com.android.adservices.service.FlagsConstants.KEY_PAS_UX_ENABLED;
21 import static com.android.adservices.service.consent.ConsentManager.MANUAL_INTERACTIONS_RECORDED;
22 
23 import android.content.Context;
24 import android.os.Build;
25 
26 import androidx.annotation.RequiresApi;
27 
28 import com.android.adservices.service.consent.AdServicesApiConsent;
29 import com.android.adservices.service.consent.AdServicesApiType;
30 import com.android.adservices.service.consent.ConsentManager;
31 import com.android.adservices.service.stats.UiStatsLogger;
32 import com.android.adservices.service.ui.data.UxStatesManager;
33 import com.android.adservices.service.ui.enrollment.base.PrivacySandboxEnrollmentChannel;
34 import com.android.adservices.service.ui.ux.collection.PrivacySandboxUxCollection;
35 
36 /** Enrollment channel for checking if user has already enrolled in an UX. */
37 @RequiresApi(Build.VERSION_CODES.S)
38 public class AlreadyEnrolledChannel implements PrivacySandboxEnrollmentChannel {
39 
40     /** Determines if user has already enrolled in a particular UX. */
isEligible( PrivacySandboxUxCollection uxCollection, ConsentManager consentManager, UxStatesManager uxStatesManager)41     public boolean isEligible(
42             PrivacySandboxUxCollection uxCollection,
43             ConsentManager consentManager,
44             UxStatesManager uxStatesManager) {
45         switch (uxCollection) {
46             case GA_UX:
47                 if (uxStatesManager.getFlag(KEY_PAS_UX_ENABLED)) {
48                     // PreNotificationManualUsers should be in PasReconsentNotificationChannel.
49                     return consentManager.wasPasNotificationDisplayed()
50                             || isManuallyOptedOutOfPaAndMsmt(consentManager);
51                 }
52                 return consentManager.wasGaUxNotificationDisplayed()
53                         || isPreNotificationManualUser(consentManager, uxStatesManager);
54             case BETA_UX:
55                 return consentManager.wasNotificationDisplayed();
56             case U18_UX:
57             case RVC_UX:
58                 return consentManager.wasU18NotificationDisplayed();
59             default:
60                 // Unsupported and non-valid UXs can never have enrollment channels.
61                 return false;
62         }
63     }
64 
isManuallyOptedOutOfPaAndMsmt(ConsentManager consentManager)65     private static boolean isManuallyOptedOutOfPaAndMsmt(ConsentManager consentManager) {
66         return consentManager.wasGaUxNotificationDisplayed()
67                 && consentManager.getUserManualInteractionWithConsent()
68                         == MANUAL_INTERACTIONS_RECORDED
69                 && consentManager
70                         .getConsent(AdServicesApiType.FLEDGE)
71                         .equals(AdServicesApiConsent.REVOKED)
72                 && consentManager
73                         .getConsent(AdServicesApiType.MEASUREMENTS)
74                         .equals(AdServicesApiConsent.REVOKED);
75     }
76 
isPreNotificationManualUser( ConsentManager consentManager, UxStatesManager uxStatesManager)77     private static boolean isPreNotificationManualUser(
78             ConsentManager consentManager, UxStatesManager uxStatesManager) {
79         return uxStatesManager.getFlag(KEY_CONSENT_ALREADY_INTERACTED_FIX_ENABLE)
80                 && consentManager.getUserManualInteractionWithConsent()
81                         == MANUAL_INTERACTIONS_RECORDED;
82     }
83 
84     /** No-Op if the user has already enrolled. */
enroll(Context context, ConsentManager consentManager)85     public void enroll(Context context, ConsentManager consentManager) {
86         if (!consentManager.wasGaUxNotificationDisplayed()
87                 && isPreNotificationManualUser(consentManager, UxStatesManager.getInstance())) {
88             UiStatsLogger.logRequestedNotificationIneligible();
89             consentManager.recordGaUxNotificationDisplayed(true);
90         }
91     }
92 }
93