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_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.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 event reporting jobs. The actual job execution logic is part of 45 * {@link EventReportingJobHandler} 46 */ 47 public final class EventReportingJobService extends JobService { 48 private static final int MEASUREMENT_EVENT_MAIN_REPORTING_JOB_ID = 49 MEASUREMENT_EVENT_MAIN_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 EventReportingJobService job because it's running in ExtServices on" 65 + " 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_EVENT_MAIN_REPORTING_JOB_ID); 73 74 if (FlagsFactory.getFlags().getMeasurementJobEventReportingKillSwitch()) { 75 LogUtil.e("EventReportingJobService 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("EventReportingJobService.onStartJob: "); 82 sBlockingExecutor.execute( 83 () -> { 84 long maxEventReportUploadRetryWindowMs = 85 SystemHealthParams.MAX_EVENT_REPORT_UPLOAD_RETRY_WINDOW_MS; 86 boolean success = 87 new EventReportingJobHandler( 88 EnrollmentDao.getInstance(getApplicationContext()), 89 DatastoreManagerFactory.getDatastoreManager( 90 getApplicationContext()), 91 ReportingStatus.UploadMethod.REGULAR) 92 .performScheduledPendingReportsInWindow( 93 System.currentTimeMillis() 94 - maxEventReportUploadRetryWindowMs, 95 System.currentTimeMillis()); 96 97 AdservicesJobServiceLogger.getInstance(EventReportingJobService.this) 98 .recordJobFinished( 99 MEASUREMENT_EVENT_MAIN_REPORTING_JOB_ID, success, !success); 100 101 jobFinished(params, !success); 102 }); 103 return true; 104 } 105 106 @Override onStopJob(JobParameters params)107 public boolean onStopJob(JobParameters params) { 108 LogUtil.d("EventReportingJobService.onStopJob"); 109 boolean shouldRetry = true; 110 111 AdservicesJobServiceLogger.getInstance(this) 112 .recordOnStopJob(params, MEASUREMENT_EVENT_MAIN_REPORTING_JOB_ID, shouldRetry); 113 return shouldRetry; 114 } 115 116 /** Schedules {@link EventReportingJobService} */ 117 @VisibleForTesting schedule(Context context, JobScheduler jobScheduler)118 static void schedule(Context context, JobScheduler jobScheduler) { 119 final JobInfo job = 120 new JobInfo.Builder( 121 MEASUREMENT_EVENT_MAIN_REPORTING_JOB_ID, 122 new ComponentName(context, EventReportingJobService.class)) 123 .setRequiresDeviceIdle(true) 124 .setRequiresBatteryNotLow(true) 125 .setRequiredNetworkType(JobInfo.NETWORK_TYPE_UNMETERED) 126 .setPeriodic(AdServicesConfig.getMeasurementEventMainReportingJobPeriodMs()) 127 .setPersisted(true) 128 .build(); 129 jobScheduler.schedule(job); 130 } 131 132 /** 133 * Schedule Event Reporting Job if it is not already scheduled 134 * 135 * @param context the context 136 * @param forceSchedule flag to indicate whether to force rescheduling the job. 137 */ scheduleIfNeeded(Context context, boolean forceSchedule)138 public static void scheduleIfNeeded(Context context, boolean forceSchedule) { 139 if (FlagsFactory.getFlags().getMeasurementJobEventReportingKillSwitch()) { 140 LogUtil.d("EventReportingJobService is disabled, skip scheduling"); 141 return; 142 } 143 144 final JobScheduler jobScheduler = context.getSystemService(JobScheduler.class); 145 if (jobScheduler == null) { 146 LogUtil.e("JobScheduler not found"); 147 return; 148 } 149 150 final JobInfo job = jobScheduler.getPendingJob(MEASUREMENT_EVENT_MAIN_REPORTING_JOB_ID); 151 // Schedule if it hasn't been scheduled already or force rescheduling 152 if (job == null || forceSchedule) { 153 schedule(context, jobScheduler); 154 LogUtil.d("Scheduled EventReportingJobService"); 155 } else { 156 LogUtil.d("EventReportingJobService already scheduled, skipping reschedule"); 157 } 158 } 159 skipAndCancelBackgroundJob(final JobParameters params, int skipReason)160 private boolean skipAndCancelBackgroundJob(final JobParameters params, int skipReason) { 161 final JobScheduler jobScheduler = this.getSystemService(JobScheduler.class); 162 if (jobScheduler != null) { 163 jobScheduler.cancel(MEASUREMENT_EVENT_MAIN_REPORTING_JOB_ID); 164 } 165 166 AdservicesJobServiceLogger.getInstance(this) 167 .recordJobSkipped(MEASUREMENT_EVENT_MAIN_REPORTING_JOB_ID, skipReason); 168 169 // Tell the JobScheduler that the job has completed and does not need to be rescheduled. 170 jobFinished(params, false); 171 172 // Returning false means that this job has completed its work. 173 return false; 174 } 175 } 176