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