1 /*
2  * Copyright (C) 2022 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.consent;
18 
19 import android.app.adservices.consent.ConsentParcel;
20 
21 import com.android.internal.annotations.VisibleForTesting;
22 
23 /** Represents a PP API type. */
24 public enum AdServicesApiType {
25     TOPICS,
26     FLEDGE,
27     MEASUREMENTS,
28     ALL_API,
29     UNKNOWN;
30 
31     // to support scenario when GA UX is on but the consent is server from PP API
32     @VisibleForTesting static final String CONSENT_ALL = "CONSENT";
33 
34     @VisibleForTesting static final String CONSENT_ALL_APPSEARCH = "CONSENT-ALL";
35     @VisibleForTesting static final String CONSENT_FLEDGE = "CONSENT-FLEDGE";
36     @VisibleForTesting static final String CONSENT_MEASUREMENT = "CONSENT-MEASUREMENT";
37     @VisibleForTesting static final String CONSENT_TOPICS = "CONSENT-TOPICS";
38 
39 
40     /** Maps the {@link AdServicesApiType} to Consent API type integer. */
toConsentApiType()41     public int toConsentApiType() {
42         switch (this) {
43             case TOPICS:
44                 return ConsentParcel.TOPICS;
45             case FLEDGE:
46                 return ConsentParcel.FLEDGE;
47             case MEASUREMENTS:
48                 return ConsentParcel.MEASUREMENT;
49             case ALL_API:
50                 return ConsentParcel.ALL_API;
51             default:
52                 return ConsentParcel.UNKNOWN;
53         }
54     }
55 
56     /**
57      * Maps the {@link AdServicesApiType} to {@link String} which represents the key in the PP API
58      * datastore.
59      *
60      * @return key which can be used to retrieved data for {@link AdServicesApiType} from PP API
61      *     datastore.
62      */
toPpApiDatastoreKey()63     public String toPpApiDatastoreKey() {
64         switch (this) {
65             case TOPICS:
66                 return CONSENT_TOPICS;
67             case FLEDGE:
68                 return CONSENT_FLEDGE;
69             case MEASUREMENTS:
70                 return CONSENT_MEASUREMENT;
71             case ALL_API:
72                 return CONSENT_ALL;
73             default:
74                 throw new IllegalStateException("AdServicesApiType doesn't exist.");
75         }
76     }
77 
78     /** Maps the {@link AdServicesApiType} to AppSearch storage Key. */
toAppSearchKey()79     public String toAppSearchKey() {
80         switch (this) {
81             case TOPICS:
82                 return CONSENT_TOPICS;
83             case FLEDGE:
84                 return CONSENT_FLEDGE;
85             case MEASUREMENTS:
86                 return CONSENT_MEASUREMENT;
87             case ALL_API:
88                 return CONSENT_ALL_APPSEARCH;
89             default:
90                 throw new IllegalStateException("AdServicesApiType doesn't exist.");
91         }
92     }
93 
94     /** Gets AdserviceApiType from datastore key. */
fromPpApiDatastoreKey(String ppApiDatastoreKey)95     public static AdServicesApiType fromPpApiDatastoreKey(String ppApiDatastoreKey) {
96         for (AdServicesApiType apiType : AdServicesApiType.values()) {
97             if (apiType.toPpApiDatastoreKey().equals(ppApiDatastoreKey)) {
98                 return apiType;
99             }
100         }
101         return UNKNOWN;
102     }
103 }
104