• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.enrollment;
18 
19 import android.content.Context;
20 import android.content.SharedPreferences;
21 
22 import com.android.adservices.service.stats.AdServicesEnrollmentTransactionStats;
23 import com.android.adservices.service.stats.AdServicesLogger;
24 import com.android.adservices.shared.common.ApplicationContextSingleton;
25 import com.android.internal.annotations.VisibleForTesting;
26 
27 /** Util class for all enrollment-related classes */
28 public class EnrollmentUtil {
29     private static EnrollmentUtil sSingleton;
30     private final Context mContext;
31 
32     @VisibleForTesting public static final String ENROLLMENT_SHARED_PREF = "adservices_enrollment";
33     @VisibleForTesting public static final String BUILD_ID = "build_id";
34     @VisibleForTesting public static final String FILE_GROUP_STATUS = "file_group_status";
35 
EnrollmentUtil(Context context)36     private EnrollmentUtil(Context context) {
37         mContext = context;
38     }
39 
40     /** Returns an instance of the EnrollmentDao given a context. */
getInstance()41     public static EnrollmentUtil getInstance() {
42         synchronized (EnrollmentUtil.class) {
43             if (sSingleton == null) {
44                 sSingleton = new EnrollmentUtil(ApplicationContextSingleton.get());
45             }
46             return sSingleton;
47         }
48     }
49 
50     /** Get build ID from shared preference */
getBuildId()51     public int getBuildId() {
52         SharedPreferences prefs = getPrefs();
53         return prefs.getInt(BUILD_ID, /* defaultValue */ -1);
54     }
55 
56     /** Get file group status from shared preference */
getFileGroupStatus()57     public int getFileGroupStatus() {
58         SharedPreferences prefs = getPrefs();
59         return prefs.getInt(FILE_GROUP_STATUS, /* defaultValue */ 0);
60     }
61 
convertBuildIdStringToInt(String buildIdString)62     private int convertBuildIdStringToInt(String buildIdString) {
63         if (buildIdString == null) {
64             return -1;
65         }
66         try {
67             int buildId = Integer.parseInt(buildIdString);
68             return buildId;
69         } catch (NumberFormatException e) {
70             return -1;
71         }
72     }
73 
getQueryParameterValue(String queryParameter)74     private String getQueryParameterValue(String queryParameter) {
75         if (queryParameter == null) {
76             return "";
77         }
78         return queryParameter;
79     }
80 
81     /** Log EnrollmentData atom metrics for enrollment database transactions */
logEnrollmentDataStats( AdServicesLogger logger, int type, boolean isSuccessful, Integer buildId)82     public void logEnrollmentDataStats(
83             AdServicesLogger logger, int type, boolean isSuccessful, Integer buildId) {
84         logger.logEnrollmentDataStats(type, isSuccessful, buildId);
85     }
86 
87     /** Log EnrollmentFileDownload atom metrics for enrollment data downloading */
logEnrollmentFileDownloadStats( AdServicesLogger logger, boolean isSuccessful, String buildIdString)88     public void logEnrollmentFileDownloadStats(
89             AdServicesLogger logger, boolean isSuccessful, String buildIdString) {
90         logger.logEnrollmentFileDownloadStats(
91                 isSuccessful, convertBuildIdStringToInt(buildIdString));
92     }
93 
94     /** Log EnrollmentData atom metrics for enrollment database queries */
logEnrollmentMatchStats( AdServicesLogger logger, boolean isSuccessful, Integer buildId)95     public void logEnrollmentMatchStats(
96             AdServicesLogger logger, boolean isSuccessful, Integer buildId) {
97         logger.logEnrollmentMatchStats(isSuccessful, buildId);
98     }
99 
100     /** Log EnrollmentFailed atom metrics for enrollment-related status_caller_not_found errors */
logEnrollmentFailedStats( AdServicesLogger logger, Integer buildId, Integer dataFileGroupStatus, int enrollmentRecordCount, String queryParameter, int errorCause)101     public void logEnrollmentFailedStats(
102             AdServicesLogger logger,
103             Integer buildId,
104             Integer dataFileGroupStatus,
105             int enrollmentRecordCount,
106             String queryParameter,
107             int errorCause) {
108         logger.logEnrollmentFailedStats(
109                 buildId,
110                 dataFileGroupStatus,
111                 enrollmentRecordCount,
112                 getQueryParameterValue(queryParameter),
113                 errorCause);
114     }
115 
116     /** Helper method to initialize transaction stats */
initTransactionStatsBuilder( AdServicesEnrollmentTransactionStats.TransactionType type, int transactionParameterCount, int dataSourceRecordCount)117     public AdServicesEnrollmentTransactionStats.Builder initTransactionStatsBuilder(
118             AdServicesEnrollmentTransactionStats.TransactionType type,
119             int transactionParameterCount,
120             int dataSourceRecordCount) {
121 
122         return AdServicesEnrollmentTransactionStats.builder()
123                 .setTransactionType(type)
124                 .setTransactionParameterCount(transactionParameterCount)
125                 .setDataSourceRecordCountPre(dataSourceRecordCount);
126     }
127 
128     /** Log EnrollmentTransactionStats atom metrics for enrollment-related tracking */
logTransactionStats( AdServicesLogger logger, AdServicesEnrollmentTransactionStats.Builder statsBuilder, AdServicesEnrollmentTransactionStats.TransactionStatus status, int queryResultCount, int transactionResultCount, int dataSourceRecordCount, int latencyMs)129     public void logTransactionStats(
130             AdServicesLogger logger,
131             AdServicesEnrollmentTransactionStats.Builder statsBuilder,
132             AdServicesEnrollmentTransactionStats.TransactionStatus status,
133             int queryResultCount,
134             int transactionResultCount,
135             int dataSourceRecordCount,
136             int latencyMs) {
137         statsBuilder.setDataSourceRecordCountPost(dataSourceRecordCount);
138         logger.logEnrollmentTransactionStats(
139                 statsBuilder
140                         .setTransactionStatus(status)
141                         .setEnrollmentFileBuildId(getBuildId())
142                         .setQueryResultCount(queryResultCount)
143                         .setTransactionResultCount(transactionResultCount)
144                         .setLatencyMs(latencyMs)
145                         .build());
146     }
147 
148     /**
149      * Log EnrollmentTransactionStats atom metrics for enrollment-related tracking where no result
150      * is expected
151      */
logTransactionStatsNoResult( AdServicesLogger logger, AdServicesEnrollmentTransactionStats.Builder statsBuilder, AdServicesEnrollmentTransactionStats.TransactionStatus status, int dataSourceRecordCount, int latencyMs)152     public void logTransactionStatsNoResult(
153             AdServicesLogger logger,
154             AdServicesEnrollmentTransactionStats.Builder statsBuilder,
155             AdServicesEnrollmentTransactionStats.TransactionStatus status,
156             int dataSourceRecordCount,
157             int latencyMs) {
158         logTransactionStats(
159                 logger,
160                 statsBuilder,
161                 status,
162                 /* queryResultCount= */ 0,
163                 /* transactionResultCount= */ 0,
164                 dataSourceRecordCount,
165                 latencyMs);
166     }
167 
168     @SuppressWarnings("AvoidSharedPreferences") // Legacy usage
getPrefs()169     private SharedPreferences getPrefs() {
170         return mContext.getSharedPreferences(ENROLLMENT_SHARED_PREF, Context.MODE_PRIVATE);
171     }
172 }
173