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.measurement.util.JobLockHolder.Type.EVENT_REPORTING; 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_MAIN_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.LoggerFactory; 32 import com.android.adservices.concurrency.AdServicesExecutors; 33 import com.android.adservices.data.measurement.DatastoreManagerFactory; 34 import com.android.adservices.service.Flags; 35 import com.android.adservices.service.FlagsFactory; 36 import com.android.adservices.service.common.compat.ServiceCompatUtils; 37 import com.android.adservices.service.measurement.util.JobLockHolder; 38 import com.android.adservices.service.stats.AdServicesLoggerImpl; 39 import com.android.adservices.spe.AdServicesJobServiceLogger; 40 import com.android.internal.annotations.VisibleForTesting; 41 42 import com.google.android.libraries.mobiledatadownload.internal.AndroidTimeSource; 43 import com.google.common.util.concurrent.ListeningExecutorService; 44 45 import java.util.concurrent.Future; 46 47 /** 48 * Main service for scheduling event reporting jobs. The actual job execution logic is part of 49 * {@link EventReportingJobHandler} 50 */ 51 public final class EventReportingJobService extends JobService { 52 private static final int MEASUREMENT_EVENT_MAIN_REPORTING_JOB_ID = 53 MEASUREMENT_EVENT_MAIN_REPORTING_JOB.getJobId(); 54 55 private static final ListeningExecutorService sBlockingExecutor = 56 AdServicesExecutors.getBlockingExecutor(); 57 private Future mExecutorFuture; 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 EventReportingJobService job because it's running in ExtServices on" 66 + " T+"); 67 return skipAndCancelBackgroundJob(params, /* skipReason=*/ 0, /* doRecord=*/ false); 68 } 69 70 AdServicesJobServiceLogger.getInstance() 71 .recordOnStartJob(MEASUREMENT_EVENT_MAIN_REPORTING_JOB_ID); 72 73 if (FlagsFactory.getFlags().getMeasurementJobEventReportingKillSwitch()) { 74 LoggerFactory.getMeasurementLogger().e("EventReportingJobService is disabled"); 75 return skipAndCancelBackgroundJob( 76 params, 77 AD_SERVICES_BACKGROUND_JOBS_EXECUTION_REPORTED__EXECUTION_RESULT_CODE__SKIP_FOR_KILL_SWITCH_ON, 78 /* doRecord=*/ true); 79 } 80 81 LoggerFactory.getMeasurementLogger().d("EventReportingJobService.onStartJob: "); 82 mExecutorFuture = 83 sBlockingExecutor.submit( 84 () -> { 85 processPendingReports(); 86 87 AdServicesJobServiceLogger.getInstance() 88 .recordJobFinished( 89 MEASUREMENT_EVENT_MAIN_REPORTING_JOB_ID, 90 /* isSuccessful= */ true, 91 /* shouldRetry= */ false); 92 93 jobFinished(params, /* wantsReschedule= */ false); 94 }); 95 return true; 96 } 97 98 @VisibleForTesting processPendingReports()99 void processPendingReports() { 100 JobLockHolder.getInstance(EVENT_REPORTING) 101 .runWithLock( 102 "EventReportingJobService", 103 () -> { 104 long maxEventReportUploadRetryWindowMs = 105 FlagsFactory.getFlags() 106 .getMeasurementMaxEventReportUploadRetryWindowMs(); 107 new EventReportingJobHandler( 108 DatastoreManagerFactory.getDatastoreManager(), 109 FlagsFactory.getFlags(), 110 AdServicesLoggerImpl.getInstance(), 111 ReportingStatus.ReportType.EVENT, 112 ReportingStatus.UploadMethod.REGULAR, 113 getApplicationContext(), 114 new AndroidTimeSource()) 115 .performScheduledPendingReportsInWindow( 116 System.currentTimeMillis() 117 - maxEventReportUploadRetryWindowMs, 118 System.currentTimeMillis()); 119 }); 120 } 121 122 @Override onStopJob(JobParameters params)123 public boolean onStopJob(JobParameters params) { 124 LoggerFactory.getMeasurementLogger().d("EventReportingJobService.onStopJob"); 125 boolean shouldRetry = true; 126 if (mExecutorFuture != null) { 127 shouldRetry = mExecutorFuture.cancel(/* mayInterruptIfRunning */ true); 128 } 129 AdServicesJobServiceLogger.getInstance() 130 .recordOnStopJob(params, MEASUREMENT_EVENT_MAIN_REPORTING_JOB_ID, shouldRetry); 131 return shouldRetry; 132 } 133 134 /** Schedules {@link EventReportingJobService} */ 135 @VisibleForTesting schedule(JobScheduler jobScheduler, JobInfo job)136 static void schedule(JobScheduler jobScheduler, JobInfo job) { 137 jobScheduler.schedule(job); 138 } 139 140 /** 141 * Schedule Event Reporting Job if it is not already scheduled 142 * 143 * @param context the context 144 * @param forceSchedule flag to indicate whether to force rescheduling the job. 145 */ 146 // TODO(b/311183933): Remove passed in Context from static method. 147 @SuppressWarnings("AvoidStaticContext") scheduleIfNeeded(Context context, boolean forceSchedule)148 public static void scheduleIfNeeded(Context context, boolean forceSchedule) { 149 Flags flags = FlagsFactory.getFlags(); 150 if (flags.getMeasurementJobEventReportingKillSwitch()) { 151 LoggerFactory.getMeasurementLogger() 152 .d("EventReportingJobService is disabled, skip scheduling"); 153 return; 154 } 155 156 final JobScheduler jobScheduler = context.getSystemService(JobScheduler.class); 157 if (jobScheduler == null) { 158 LoggerFactory.getMeasurementLogger().e("JobScheduler not found"); 159 return; 160 } 161 162 final JobInfo scheduledJob = 163 jobScheduler.getPendingJob(MEASUREMENT_EVENT_MAIN_REPORTING_JOB_ID); 164 // Schedule if it hasn't been scheduled already or force rescheduling 165 JobInfo jobInfo = buildJobInfo(context, flags); 166 if (forceSchedule || !jobInfo.equals(scheduledJob)) { 167 schedule(jobScheduler, jobInfo); 168 LoggerFactory.getMeasurementLogger().d("Scheduled EventReportingJobService"); 169 } else { 170 LoggerFactory.getMeasurementLogger() 171 .d("EventReportingJobService already scheduled, skipping reschedule"); 172 } 173 } 174 175 // TODO(b/311183933): Remove passed in Context from static method. 176 @SuppressWarnings("AvoidStaticContext") buildJobInfo(Context context, Flags flags)177 private static JobInfo buildJobInfo(Context context, Flags flags) { 178 return new JobInfo.Builder( 179 MEASUREMENT_EVENT_MAIN_REPORTING_JOB_ID, 180 new ComponentName(context, EventReportingJobService.class)) 181 .setRequiresBatteryNotLow( 182 flags.getMeasurementEventReportingJobRequiredBatteryNotLow()) 183 .setRequiredNetworkType(flags.getMeasurementEventReportingJobRequiredNetworkType()) 184 .setPeriodic(flags.getMeasurementEventMainReportingJobPeriodMs()) 185 .setPersisted(flags.getMeasurementEventReportingJobPersisted()) 186 .build(); 187 } 188 skipAndCancelBackgroundJob( final JobParameters params, int skipReason, boolean doRecord)189 private boolean skipAndCancelBackgroundJob( 190 final JobParameters params, int skipReason, boolean doRecord) { 191 final JobScheduler jobScheduler = this.getSystemService(JobScheduler.class); 192 if (jobScheduler != null) { 193 jobScheduler.cancel(MEASUREMENT_EVENT_MAIN_REPORTING_JOB_ID); 194 } 195 196 if (doRecord) { 197 AdServicesJobServiceLogger.getInstance() 198 .recordJobSkipped(MEASUREMENT_EVENT_MAIN_REPORTING_JOB_ID, skipReason); 199 } 200 201 // Tell the JobScheduler that the job has completed and does not need to be rescheduled. 202 jobFinished(params, false); 203 204 // Returning false means that this job has completed its work. 205 return false; 206 } 207 208 @VisibleForTesting getFutureForTesting()209 Future getFutureForTesting() { 210 return mExecutorFuture; 211 } 212 } 213