• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.measurement.reporting;
18 
19 import static com.android.adservices.service.stats.AdServicesStatsLog.AD_SERVICES_BACKGROUND_JOBS_EXECUTION_REPORTED__EXECUTION_RESULT_CODE__SKIP_FOR_EXTSERVICES_JOB_ON_TPLUS;
20 import static com.android.adservices.service.stats.AdServicesStatsLog.AD_SERVICES_BACKGROUND_JOBS_EXECUTION_REPORTED__EXECUTION_RESULT_CODE__SKIP_FOR_KILL_SWITCH_ON;
21 import static com.android.adservices.spe.AdservicesJobInfo.MEASUREMENT_AGGREGATE_FALLBACK_REPORTING_JOB;
22 
23 import android.app.job.JobInfo;
24 import android.app.job.JobParameters;
25 import android.app.job.JobScheduler;
26 import android.app.job.JobService;
27 import android.content.ComponentName;
28 import android.content.Context;
29 
30 import com.android.adservices.LogUtil;
31 import com.android.adservices.concurrency.AdServicesExecutors;
32 import com.android.adservices.data.enrollment.EnrollmentDao;
33 import com.android.adservices.data.measurement.DatastoreManagerFactory;
34 import com.android.adservices.service.AdServicesConfig;
35 import com.android.adservices.service.FlagsFactory;
36 import com.android.adservices.service.common.compat.ServiceCompatUtils;
37 import com.android.adservices.service.measurement.SystemHealthParams;
38 import com.android.adservices.spe.AdservicesJobServiceLogger;
39 import com.android.internal.annotations.VisibleForTesting;
40 
41 import java.util.concurrent.Executor;
42 
43 /**
44  * Main service for scheduling aggregate reporting jobs. The actual job execution logic is part of
45  * {@link AggregateReportingJobHandler}
46  */
47 public final class AggregateFallbackReportingJobService extends JobService {
48     private static final int MEASUREMENT_AGGREGATE_FALLBACK_REPORTING_JOB_ID =
49             MEASUREMENT_AGGREGATE_FALLBACK_REPORTING_JOB.getJobId();
50 
51     private static final Executor sBlockingExecutor = AdServicesExecutors.getBlockingExecutor();
52 
53     @Override
onCreate()54     public void onCreate() {
55         super.onCreate();
56     }
57 
58     @Override
onStartJob(JobParameters params)59     public boolean onStartJob(JobParameters params) {
60         // Always ensure that the first thing this job does is check if it should be running, and
61         // cancel itself if it's not supposed to be.
62         if (ServiceCompatUtils.shouldDisableExtServicesJobOnTPlus(this)) {
63             LogUtil.d(
64                     "Disabling AggregateFallbackReportingJobService job because it's running in"
65                             + " ExtServices on T+");
66             return skipAndCancelBackgroundJob(
67                     params,
68                     AD_SERVICES_BACKGROUND_JOBS_EXECUTION_REPORTED__EXECUTION_RESULT_CODE__SKIP_FOR_EXTSERVICES_JOB_ON_TPLUS);
69         }
70 
71         AdservicesJobServiceLogger.getInstance(this)
72                 .recordOnStartJob(MEASUREMENT_AGGREGATE_FALLBACK_REPORTING_JOB_ID);
73 
74         if (FlagsFactory.getFlags().getMeasurementJobAggregateFallbackReportingKillSwitch()) {
75             LogUtil.e("AggregateFallbackReportingJobService is disabled");
76             return skipAndCancelBackgroundJob(
77                     params,
78                     AD_SERVICES_BACKGROUND_JOBS_EXECUTION_REPORTED__EXECUTION_RESULT_CODE__SKIP_FOR_KILL_SWITCH_ON);
79         }
80 
81         LogUtil.d("AggregateFallbackReportingJobService.onStartJob");
82         sBlockingExecutor.execute(
83                 () -> {
84                     final long windowStartTime =
85                             System.currentTimeMillis()
86                                     - SystemHealthParams
87                                             .MAX_AGGREGATE_REPORT_UPLOAD_RETRY_WINDOW_MS;
88                     final long windowEndTime =
89                             System.currentTimeMillis()
90                                     - AdServicesConfig
91                                             .getMeasurementAggregateMainReportingJobPeriodMs();
92                     final boolean success =
93                             new AggregateReportingJobHandler(
94                                             EnrollmentDao.getInstance(getApplicationContext()),
95                                             DatastoreManagerFactory.getDatastoreManager(
96                                                     getApplicationContext()),
97                                             ReportingStatus.UploadMethod.FALLBACK)
98                                     .performScheduledPendingReportsInWindow(
99                                             windowStartTime, windowEndTime);
100 
101                     AdservicesJobServiceLogger.getInstance(
102                                     AggregateFallbackReportingJobService.this)
103                             .recordJobFinished(
104                                     MEASUREMENT_AGGREGATE_FALLBACK_REPORTING_JOB_ID,
105                                     success,
106                                     !success);
107 
108                     jobFinished(params, !success);
109                 });
110         return true;
111     }
112 
113     @Override
onStopJob(JobParameters params)114     public boolean onStopJob(JobParameters params) {
115         LogUtil.d("AggregateFallbackReportingJobService.onStopJob");
116         boolean shouldRetry = false;
117 
118         AdservicesJobServiceLogger.getInstance(this)
119                 .recordOnStopJob(
120                         params, MEASUREMENT_AGGREGATE_FALLBACK_REPORTING_JOB_ID, shouldRetry);
121         return shouldRetry;
122     }
123 
124     /** Schedules {@link AggregateFallbackReportingJobService} */
125     @VisibleForTesting
schedule(Context context, JobScheduler jobScheduler)126     static void schedule(Context context, JobScheduler jobScheduler) {
127         final JobInfo job =
128                 new JobInfo.Builder(
129                                 MEASUREMENT_AGGREGATE_FALLBACK_REPORTING_JOB_ID,
130                                 new ComponentName(
131                                         context, AggregateFallbackReportingJobService.class))
132                         .setRequiresDeviceIdle(true)
133                         .setRequiresBatteryNotLow(true)
134                         .setPeriodic(
135                                 AdServicesConfig
136                                         .getMeasurementAggregateFallbackReportingJobPeriodMs())
137                         .setPersisted(true)
138                         .build();
139         jobScheduler.schedule(job);
140     }
141 
142     /**
143      * Schedule Aggregate Fallback Reporting Job if it is not already scheduled
144      *
145      * @param context the context
146      * @param forceSchedule flag to indicate whether to force rescheduling the job.
147      */
scheduleIfNeeded(Context context, boolean forceSchedule)148     public static void scheduleIfNeeded(Context context, boolean forceSchedule) {
149         if (FlagsFactory.getFlags().getMeasurementJobAggregateFallbackReportingKillSwitch()) {
150             LogUtil.d("AggregateFallbackReportingJobService is disabled, skip scheduling");
151             return;
152         }
153 
154         final JobScheduler jobScheduler = context.getSystemService(JobScheduler.class);
155         if (jobScheduler == null) {
156             LogUtil.e("JobScheduler not found");
157             return;
158         }
159 
160         final JobInfo job =
161                 jobScheduler.getPendingJob(MEASUREMENT_AGGREGATE_FALLBACK_REPORTING_JOB_ID);
162         // Schedule if it hasn't been scheduled already or force rescheduling
163         if (job == null || forceSchedule) {
164             schedule(context, jobScheduler);
165             LogUtil.d("Scheduled AggregateFallbackReportingJobService");
166         } else {
167             LogUtil.d(
168                     "AggregateFallbackReportingJobService already scheduled, skipping reschedule");
169         }
170     }
171 
skipAndCancelBackgroundJob(final JobParameters params, int skipReason)172     private boolean skipAndCancelBackgroundJob(final JobParameters params, int skipReason) {
173         final JobScheduler jobScheduler = this.getSystemService(JobScheduler.class);
174         if (jobScheduler != null) {
175             jobScheduler.cancel(MEASUREMENT_AGGREGATE_FALLBACK_REPORTING_JOB_ID);
176         }
177 
178         AdservicesJobServiceLogger.getInstance(this)
179                 .recordJobSkipped(MEASUREMENT_AGGREGATE_FALLBACK_REPORTING_JOB_ID, skipReason);
180 
181         // Tell the JobScheduler that the job has completed and does not need to be rescheduled.
182         jobFinished(params, false);
183 
184         // Returning false means that this job has completed its work.
185         return false;
186     }
187 }
188