• 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_EVENT_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  * Fallback service for scheduling reporting jobs (runs less frequently than the main service
45  * without a network type requirement). The actual job execution logic is part of {@link
46  * EventReportingJobHandler}
47  */
48 public final class EventFallbackReportingJobService extends JobService {
49     private static final int MEASUREMENT_EVENT_FALLBACK_REPORTING_JOB_ID =
50             MEASUREMENT_EVENT_FALLBACK_REPORTING_JOB.getJobId();
51 
52     private static final Executor sBlockingExecutor = AdServicesExecutors.getBlockingExecutor();
53 
54     @Override
onCreate()55     public void onCreate() {
56         super.onCreate();
57     }
58 
59     @Override
onStartJob(JobParameters params)60     public boolean onStartJob(JobParameters params) {
61         // Always ensure that the first thing this job does is check if it should be running, and
62         // cancel itself if it's not supposed to be.
63         if (ServiceCompatUtils.shouldDisableExtServicesJobOnTPlus(this)) {
64             LogUtil.d(
65                     "Disabling EventFallbackReportingJobService job because it's running in"
66                             + " ExtServices on T+");
67             return skipAndCancelBackgroundJob(
68                     params,
69                     AD_SERVICES_BACKGROUND_JOBS_EXECUTION_REPORTED__EXECUTION_RESULT_CODE__SKIP_FOR_EXTSERVICES_JOB_ON_TPLUS);
70         }
71 
72         AdservicesJobServiceLogger.getInstance(this)
73                 .recordOnStartJob(MEASUREMENT_EVENT_FALLBACK_REPORTING_JOB_ID);
74 
75         if (FlagsFactory.getFlags().getMeasurementJobEventFallbackReportingKillSwitch()) {
76             LogUtil.e("EventFallbackReportingJobService Job is disabled");
77             return skipAndCancelBackgroundJob(
78                     params,
79                     AD_SERVICES_BACKGROUND_JOBS_EXECUTION_REPORTED__EXECUTION_RESULT_CODE__SKIP_FOR_KILL_SWITCH_ON);
80         }
81 
82         LogUtil.d("EventFallbackReportingJobService.onStartJob");
83         sBlockingExecutor.execute(
84                 () -> {
85                     long maxEventReportUploadRetryWindowMs =
86                             SystemHealthParams.MAX_EVENT_REPORT_UPLOAD_RETRY_WINDOW_MS;
87                     long eventMainReportingJobPeriodMs =
88                             AdServicesConfig.getMeasurementEventMainReportingJobPeriodMs();
89                     boolean success =
90                             new EventReportingJobHandler(
91                                             EnrollmentDao.getInstance(getApplicationContext()),
92                                             DatastoreManagerFactory.getDatastoreManager(
93                                                     getApplicationContext()),
94                                             ReportingStatus.UploadMethod.FALLBACK)
95                                     .performScheduledPendingReportsInWindow(
96                                             System.currentTimeMillis()
97                                                     - maxEventReportUploadRetryWindowMs,
98                                             System.currentTimeMillis()
99                                                     - eventMainReportingJobPeriodMs);
100 
101                     AdservicesJobServiceLogger.getInstance(EventFallbackReportingJobService.this)
102                             .recordJobFinished(
103                                     MEASUREMENT_EVENT_FALLBACK_REPORTING_JOB_ID, success, !success);
104 
105                     jobFinished(params, !success);
106                 });
107         return true;
108     }
109 
110     @Override
onStopJob(JobParameters params)111     public boolean onStopJob(JobParameters params) {
112         LogUtil.d("EventFallbackReportingJobService.onStopJob");
113         boolean shouldRetry = false;
114 
115         AdservicesJobServiceLogger.getInstance(this)
116                 .recordOnStopJob(params, MEASUREMENT_EVENT_FALLBACK_REPORTING_JOB_ID, shouldRetry);
117         return shouldRetry;
118     }
119 
120     /** Schedules {@link EventFallbackReportingJobService} */
121     @VisibleForTesting
schedule(Context context, JobScheduler jobScheduler)122     static void schedule(Context context, JobScheduler jobScheduler) {
123         final JobInfo job =
124                 new JobInfo.Builder(
125                                 MEASUREMENT_EVENT_FALLBACK_REPORTING_JOB_ID,
126                                 new ComponentName(context, EventFallbackReportingJobService.class))
127                         .setRequiresDeviceIdle(true)
128                         .setRequiresBatteryNotLow(true)
129                         .setPeriodic(
130                                 AdServicesConfig.getMeasurementEventFallbackReportingJobPeriodMs())
131                         .setPersisted(true)
132                         .build();
133         jobScheduler.schedule(job);
134     }
135 
136     /**
137      * Schedule Event Fallback Reporting Job if it is not already scheduled
138      *
139      * @param context the context
140      * @param forceSchedule flag to indicate whether to force rescheduling the job.
141      */
scheduleIfNeeded(Context context, boolean forceSchedule)142     public static void scheduleIfNeeded(Context context, boolean forceSchedule) {
143         if (FlagsFactory.getFlags().getMeasurementJobEventFallbackReportingKillSwitch()) {
144             LogUtil.d("EventFallbackReportingJobService is disabled, skip scheduling");
145             return;
146         }
147 
148         final JobScheduler jobScheduler = context.getSystemService(JobScheduler.class);
149         if (jobScheduler == null) {
150             LogUtil.e("JobScheduler not found");
151             return;
152         }
153 
154         final JobInfo job = jobScheduler.getPendingJob(MEASUREMENT_EVENT_FALLBACK_REPORTING_JOB_ID);
155         // Schedule if it hasn't been scheduled already or force rescheduling
156         if (job == null || forceSchedule) {
157             schedule(context, jobScheduler);
158             LogUtil.d("Scheduled EventFallbackReportingJobService");
159         } else {
160             LogUtil.d("EventFallbackReportingJobService already scheduled, skipping reschedule");
161         }
162     }
163 
skipAndCancelBackgroundJob(final JobParameters params, int skipReason)164     private boolean skipAndCancelBackgroundJob(final JobParameters params, int skipReason) {
165         final JobScheduler jobScheduler = this.getSystemService(JobScheduler.class);
166         if (jobScheduler != null) {
167             jobScheduler.cancel(MEASUREMENT_EVENT_FALLBACK_REPORTING_JOB_ID);
168         }
169 
170         AdservicesJobServiceLogger.getInstance(this)
171                 .recordJobSkipped(MEASUREMENT_EVENT_FALLBACK_REPORTING_JOB_ID, skipReason);
172 
173         // Tell the JobScheduler that the job has completed and does not need to be rescheduled.
174         jobFinished(params, false);
175 
176         // Returning false means that this job has completed its work.
177         return false;
178     }
179 }
180