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.annotation.RequiresApi;
21 import android.content.Context;
22 import android.os.Build;
23 
24 import androidx.fragment.app.FragmentActivity;
25 
26 import com.android.adservices.service.ui.ux.collection.PrivacySandboxUxCollection;
27 
28 /**
29  * Activities and Action Delegates should implement this interface to ensure they implement all
30  * existing modes of AdServices.
31  */
32 @RequiresApi(Build.VERSION_CODES.S)
33 public interface UxSelector {
34     enum EndUserUx {
35         UNKNOWN,
36         GA,
37         U18,
38         RVC,
39         GA_WITH_PAS
40     }
41 
42     /**
43      * This method will be called in during initialization of class to determine which ux to choose.
44      *
45      * @param fragmentActivity unused.
46      * @param context current context.
47      * @return Ux that end user should see.
48      */
initWithUx(FragmentActivity fragmentActivity, Context context)49     default EndUserUx initWithUx(FragmentActivity fragmentActivity, Context context) {
50         return initWithUx(context, false);
51     }
52 
53     /**
54      * This method will be called in during initialization of class to determine which ux to choose.
55      *
56      * @param context current context.
57      * @param beforePasUxActive if the current activity is before PAS UX is active, so it is part of
58      *     the process of activating PAS UX and should be shown if flag is on.
59      * @return Ux that end user should see.
60      */
initWithUx(Context context, boolean beforePasUxActive)61     default EndUserUx initWithUx(Context context, boolean beforePasUxActive) {
62         EndUserUx endUserUx = getEndUserUx(context, beforePasUxActive);
63         switch (endUserUx) {
64             case U18:
65                 initU18();
66                 break;
67             case GA:
68                 initGA();
69                 break;
70             case RVC:
71                 initRvc();
72                 break;
73             case GA_WITH_PAS:
74                 initGaUxWithPas();
75                 break;
76             default:
77                 initGA();
78         }
79         return endUserUx;
80     }
81 
82     /**
83      * Returns the UX that the end user should be seeing currently.
84      *
85      * @param context current Context.
86      * @return Ux that end user should see.
87      */
getEndUserUx(Context context)88     default EndUserUx getEndUserUx(Context context) {
89         return getEndUserUx(context, false);
90     }
91 
92     /**
93      * Returns the UX that the end user should be seeing currently.
94      *
95      * @param context current Context.
96      * @param beforePasUxActive if the current context is before PAS UX is active.
97      * @return Ux that end user should see.
98      */
getEndUserUx(Context context, boolean beforePasUxActive)99     default EndUserUx getEndUserUx(Context context, boolean beforePasUxActive) {
100         switch (UxUtil.getUx(context)) {
101             case U18_UX:
102                 return EndUserUx.U18;
103             case GA_UX:
104                 if (UxUtil.pasUxIsActive(beforePasUxActive)) {
105                     // ROW UI views should be updated only once notification is sent.
106                     // EEA UI views should be updated only once notification is opened.
107                     return EndUserUx.GA_WITH_PAS;
108                 }
109                 return EndUserUx.GA;
110             case RVC_UX:
111                 return EndUserUx.RVC;
112             default:
113                 // TODO: log some warning or error
114                 return EndUserUx.GA;
115         }
116     }
117 
118     /**
119      * This method will be called in {@link #initWithUx} if app is in {@link
120      * PrivacySandboxUxCollection#GA_UX} mode and PAS Ux feature is disabled.
121      */
initGA()122     void initGA();
123 
124     /**
125      * This method will be called in {@link #initWithUx} if app is in {@link
126      * PrivacySandboxUxCollection#U18_UX} mode.
127      */
initU18()128     void initU18();
129 
130     /**
131      * This method will be called in {@link #initWithUx} if app is in {@link
132      * PrivacySandboxUxCollection#RVC_UX} mode.
133      */
initRvc()134     void initRvc();
135 
136     /**
137      * This method will be called in {@link #initWithUx} if app is in {@link
138      * PrivacySandboxUxCollection#GA_UX} mode and PAS Ux feature is enabled.
139      */
initGaUxWithPas()140     void initGaUxWithPas();
141 }
142