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