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.ui;
18 
19 
20 import android.content.Context;
21 import android.os.Build;
22 
23 import androidx.annotation.RequiresApi;
24 import androidx.fragment.app.FragmentActivity;
25 
26 import com.android.adservices.service.FlagsFactory;
27 import com.android.adservices.service.consent.ConsentManager;
28 import com.android.adservices.service.ui.data.UxStatesManager;
29 import com.android.adservices.service.ui.ux.collection.PrivacySandboxUxCollection;
30 
31 import java.util.stream.Stream;
32 
33 /** Utility class for notification related logic. */
34 @RequiresApi(Build.VERSION_CODES.S)
35 public class UxUtil {
36 
37     /** Returns whether the device is an EEA device. */
isEeaDevice(FragmentActivity fragmentActivity)38     public static boolean isEeaDevice(FragmentActivity fragmentActivity) {
39         return FlagsFactory.getFlags().getConsentNotificationActivityDebugMode()
40                 ? fragmentActivity
41                         .getIntent()
42                         .getBooleanExtra("isEUDevice", UxStatesManager.getInstance().isEeaDevice())
43                 : !ConsentManager.getInstance().isAdIdEnabled()
44                         || UxStatesManager.getInstance().isEeaDevice();
45     }
46 
47     /** Returns if UXStates should be used. */
isUxStatesReady(Context context)48     public static boolean isUxStatesReady(Context context) {
49         PrivacySandboxUxCollection ux = getUx(context);
50         return FlagsFactory.getFlags().getEnableAdServicesSystemApi()
51                 && ux != null
52                 && ux != PrivacySandboxUxCollection.UNSUPPORTED_UX;
53     }
54 
55     /** Returns the current UX. */
getUx(Context context)56     public static PrivacySandboxUxCollection getUx(Context context) {
57         if (FlagsFactory.getFlags().getConsentNotificationActivityDebugMode()) {
58             return Stream.of(PrivacySandboxUxCollection.values())
59                     .filter(ux -> ux.toString().equals(FlagsFactory.getFlags().getDebugUx()))
60                     .findFirst()
61                     .orElse(PrivacySandboxUxCollection.UNSUPPORTED_UX);
62         } else {
63             return UxStatesManager.getInstance().getUx();
64         }
65     }
66 
67     /** Returns the specified UX flag. */
getFlag(String uxFlagKey)68     public static boolean getFlag(String uxFlagKey) {
69         return UxStatesManager.getInstance().getFlag(uxFlagKey);
70     }
71 
72     /**
73      * PAS could be enabled but user may not have received notification, so user would see GA UX
74      * instead of PAS UX. Before the notification card is shown the notification has not been
75      * displayed yet, so if the calling context is related to this then we should only look at the
76      * PAS UX flag.
77      *
78      * @param beforeNotificationShown True if the calling context is logic before PAS notification
79      *     shown has been recorded.
80      * @return True if user will see PAS UX for settings/notification, otherwise false.
81      */
pasUxIsActive(boolean beforeNotificationShown)82     public static boolean pasUxIsActive(boolean beforeNotificationShown) {
83         return UxStatesManager.getInstance().pasUxIsActive(beforeNotificationShown);
84     }
85 }
86