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.common; 18 19 import static com.android.adservices.spe.AdServicesJobInfo.AD_PACKAGE_DENY_PRE_PROCESS_JOB; 20 import static com.android.adservices.spe.AdServicesJobInfo.COBALT_LOGGING_JOB; 21 import static com.android.adservices.spe.AdServicesJobInfo.CONSENT_NOTIFICATION_JOB; 22 import static com.android.adservices.spe.AdServicesJobInfo.ENCRYPTION_KEY_PERIODIC_JOB; 23 import static com.android.adservices.spe.AdServicesJobInfo.FLEDGE_AD_SELECTION_DEBUG_REPORT_SENDER_JOB; 24 import static com.android.adservices.spe.AdServicesJobInfo.FLEDGE_BACKGROUND_FETCH_JOB; 25 import static com.android.adservices.spe.AdServicesJobInfo.FLEDGE_KANON_SIGN_JOIN_BACKGROUND_JOB; 26 import static com.android.adservices.spe.AdServicesJobInfo.MAINTENANCE_JOB; 27 import static com.android.adservices.spe.AdServicesJobInfo.MEASUREMENT_AGGREGATE_FALLBACK_REPORTING_JOB; 28 import static com.android.adservices.spe.AdServicesJobInfo.MEASUREMENT_AGGREGATE_MAIN_REPORTING_JOB; 29 import static com.android.adservices.spe.AdServicesJobInfo.MEASUREMENT_ASYNC_REGISTRATION_FALLBACK_JOB; 30 import static com.android.adservices.spe.AdServicesJobInfo.MEASUREMENT_ASYNC_REGISTRATION_JOB; 31 import static com.android.adservices.spe.AdServicesJobInfo.MEASUREMENT_ATTRIBUTION_FALLBACK_JOB; 32 import static com.android.adservices.spe.AdServicesJobInfo.MEASUREMENT_ATTRIBUTION_JOB; 33 import static com.android.adservices.spe.AdServicesJobInfo.MEASUREMENT_DEBUG_REPORTING_FALLBACK_JOB; 34 import static com.android.adservices.spe.AdServicesJobInfo.MEASUREMENT_DELETE_EXPIRED_JOB; 35 import static com.android.adservices.spe.AdServicesJobInfo.MEASUREMENT_DELETE_UNINSTALLED_JOB; 36 import static com.android.adservices.spe.AdServicesJobInfo.MEASUREMENT_EVENT_FALLBACK_REPORTING_JOB; 37 import static com.android.adservices.spe.AdServicesJobInfo.MEASUREMENT_EVENT_MAIN_REPORTING_JOB; 38 import static com.android.adservices.spe.AdServicesJobInfo.MEASUREMENT_IMMEDIATE_AGGREGATE_REPORTING_JOB; 39 import static com.android.adservices.spe.AdServicesJobInfo.MEASUREMENT_REPORTING_JOB; 40 import static com.android.adservices.spe.AdServicesJobInfo.MEASUREMENT_VERBOSE_DEBUG_REPORTING_FALLBACK_JOB; 41 import static com.android.adservices.spe.AdServicesJobInfo.PERIODIC_SIGNALS_ENCODING_JOB; 42 import static com.android.adservices.spe.AdServicesJobInfo.TOPICS_EPOCH_JOB; 43 44 import android.annotation.NonNull; 45 import android.app.job.JobScheduler; 46 import android.content.Context; 47 import android.os.Build; 48 49 import androidx.annotation.RequiresApi; 50 51 import com.android.adservices.cobalt.CobaltJobService; 52 import com.android.adservices.download.MddJob; 53 import com.android.adservices.download.MddJobService; 54 import com.android.adservices.service.Flags; 55 import com.android.adservices.service.FlagsFactory; 56 import com.android.adservices.service.MaintenanceJobService; 57 import com.android.adservices.service.adselection.debug.DebugReportSenderJobService; 58 import com.android.adservices.service.consent.AdServicesApiType; 59 import com.android.adservices.service.encryptionkey.EncryptionKeyJobService; 60 import com.android.adservices.service.measurement.DeleteExpiredJobService; 61 import com.android.adservices.service.measurement.DeleteUninstalledJobService; 62 import com.android.adservices.service.measurement.attribution.AttributionFallbackJobService; 63 import com.android.adservices.service.measurement.attribution.AttributionJobService; 64 import com.android.adservices.service.measurement.registration.AsyncRegistrationFallbackJob; 65 import com.android.adservices.service.measurement.registration.AsyncRegistrationQueueJobService; 66 import com.android.adservices.service.measurement.reporting.AggregateFallbackReportingJobService; 67 import com.android.adservices.service.measurement.reporting.AggregateReportingJobService; 68 import com.android.adservices.service.measurement.reporting.DebugReportingFallbackJobService; 69 import com.android.adservices.service.measurement.reporting.EventFallbackReportingJobService; 70 import com.android.adservices.service.measurement.reporting.EventReportingJobService; 71 import com.android.adservices.service.measurement.reporting.ImmediateAggregateReportingJobService; 72 import com.android.adservices.service.measurement.reporting.ReportingJobService; 73 import com.android.adservices.service.measurement.reporting.VerboseDebugReportingFallbackJobService; 74 import com.android.adservices.service.topics.EpochJob; 75 import com.android.adservices.service.topics.EpochJobService; 76 77 import java.util.Objects; 78 79 /** Provides functionality to schedule or unschedule all relevant background jobs. */ 80 @RequiresApi(Build.VERSION_CODES.S) 81 // TODO(b/311183933): Remove passed in Context from static method. 82 @SuppressWarnings("AvoidStaticContext") 83 public class BackgroundJobsManager { 84 /** 85 * Tries to schedule all the relevant background jobs. 86 * 87 * @param context application context. 88 */ scheduleAllBackgroundJobs(@onNull Context context)89 public static void scheduleAllBackgroundJobs(@NonNull Context context) { 90 scheduleFledgeBackgroundJobs(context); 91 92 scheduleTopicsBackgroundJobs(context); 93 94 // TODO(b/296146348): Remove MDD Background Jobs from scheduleAllBackgroundJobs 95 scheduleMddBackgroundJobs(); 96 97 scheduleMeasurementBackgroundJobs(context); 98 } 99 100 /** 101 * Tries to schedule all the relevant background jobs per api. 102 * 103 * @param context application context. 104 */ scheduleJobsPerApi(@onNull Context context, AdServicesApiType apiType)105 public static void scheduleJobsPerApi(@NonNull Context context, AdServicesApiType apiType) { 106 switch (apiType) { 107 case FLEDGE: 108 scheduleFledgeBackgroundJobs(context); 109 break; 110 case TOPICS: 111 scheduleTopicsBackgroundJobs(context); 112 break; 113 case MEASUREMENTS: 114 scheduleMeasurementBackgroundJobs(context); 115 break; 116 } 117 } 118 119 /** Tries to unschedule all the relevant background jobs per api. */ unscheduleJobsPerApi( @onNull JobScheduler jobScheduler, AdServicesApiType apiType)120 public static void unscheduleJobsPerApi( 121 @NonNull JobScheduler jobScheduler, AdServicesApiType apiType) { 122 switch (apiType) { 123 case FLEDGE: 124 unscheduleFledgeBackgroundJobs(jobScheduler); 125 break; 126 case TOPICS: 127 unscheduleTopicsBackgroundJobs(jobScheduler); 128 break; 129 case MEASUREMENTS: 130 unscheduleMeasurementBackgroundJobs(jobScheduler); 131 break; 132 } 133 } 134 135 /** 136 * Tries to schedule all the Fledge related background jobs if the FledgeSelectAdsKillSwitch is 137 * disabled. 138 * 139 * <p>Those services are: 140 * 141 * <ul> 142 * <li>{@link MaintenanceJobService} 143 * </ul> 144 * 145 * @param context application context. 146 */ scheduleFledgeBackgroundJobs(@onNull Context context)147 public static void scheduleFledgeBackgroundJobs(@NonNull Context context) { 148 Flags flags = FlagsFactory.getFlags(); 149 if (!flags.getFledgeSelectAdsKillSwitch()) { 150 MaintenanceJobService.scheduleIfNeeded(context, /* forceSchedule= */ false); 151 if (flags.getFledgeEventLevelDebugReportingEnabled()) { 152 DebugReportSenderJobService.scheduleIfNeeded(context, false); 153 } 154 } 155 } 156 157 /** 158 * Tries to schedule all the Topics related background jobs if the TopicsKillSwitch is disabled. 159 * 160 * <p>Those services are: 161 * 162 * <ul> 163 * <li>{@link EpochJobService} 164 * <li>{@link MaintenanceJobService} 165 * <li>{@link MddJobService} 166 * <li>{@link EncryptionKeyJobService} 167 * <li>{@link CobaltJobService} 168 * </ul> 169 * 170 * @param context application context. 171 */ scheduleTopicsBackgroundJobs(@onNull Context context)172 public static void scheduleTopicsBackgroundJobs(@NonNull Context context) { 173 if (!FlagsFactory.getFlags().getTopicsKillSwitch()) { 174 EpochJob.schedule(); 175 MaintenanceJobService.scheduleIfNeeded(context, /* forceSchedule= */ false); 176 scheduleMddBackgroundJobs(); 177 scheduleEncryptionKeyBackgroundJobs(context); 178 scheduleCobaltBackgroundJob(context); 179 scheduleAdPackageDenyPreProcessBackgroundJob(); 180 } 181 } 182 183 /** 184 * Tries to schedule all the Mdd related background jobs if the MddBackgroundTaskKillSwitch is 185 * disabled. 186 */ scheduleMddBackgroundJobs()187 public static void scheduleMddBackgroundJobs() { 188 if (!FlagsFactory.getFlags().getMddBackgroundTaskKillSwitch()) { 189 MddJob.scheduleAllMddJobs(); 190 } 191 } 192 193 /** 194 * Tries to schedule EncryptionKey related background jobs if the 195 * EncryptionKeyPeriodicFetchKillSwitch is disabled. 196 * 197 * @param context application context. 198 */ scheduleEncryptionKeyBackgroundJobs(@onNull Context context)199 public static void scheduleEncryptionKeyBackgroundJobs(@NonNull Context context) { 200 if (!FlagsFactory.getFlags().getEncryptionKeyPeriodicFetchKillSwitch()) { 201 EncryptionKeyJobService.scheduleIfNeeded(context, /* forceSchedule */ false); 202 } 203 } 204 205 /** 206 * Tries to schedule all the Measurement related background jobs if the MeasurementKillSwitch is 207 * disabled. 208 * 209 * <p>Those services are: 210 * 211 * <ul> 212 * <li>{@link AggregateReportingJobService} 213 * <li>{@link AggregateFallbackReportingJobService} 214 * <li>{@link ImmediateAggregateReportingJobService} 215 * <li>{@link ReportingJobService} 216 * <li>{@link AttributionJobService} 217 * <li>{@link EventReportingJobService} 218 * <li>{@link EventFallbackReportingJobService} 219 * <li>{@link DeleteExpiredJobService} 220 * <li>{@link DeleteUninstalledJobService} 221 * <li>{@link AsyncRegistrationQueueJobService} 222 * <li>{@link AsyncRegistrationFallbackJob} 223 * <li>{@link MddJobService} 224 * <li>{@link EncryptionKeyJobService} 225 * <li>{@link CobaltJobService} 226 * </ul> 227 * 228 * @param context application context. 229 */ scheduleMeasurementBackgroundJobs(@onNull Context context)230 public static void scheduleMeasurementBackgroundJobs(@NonNull Context context) { 231 if (FlagsFactory.getFlags().getMeasurementEnabled()) { 232 AggregateReportingJobService.scheduleIfNeeded(context, /* forceSchedule= */ false); 233 AggregateFallbackReportingJobService.scheduleIfNeeded( 234 context, /* forceSchedule= */ false); 235 ImmediateAggregateReportingJobService.scheduleIfNeeded( 236 context, /* forceSchedule= */ false); 237 ReportingJobService.scheduleIfNeeded(context, /* forceSchedule= */ false); 238 AttributionJobService.scheduleIfNeeded(context, /* forceSchedule= */ false); 239 AttributionFallbackJobService.scheduleIfNeeded(context, /* forceSchedule= */ false); 240 EventReportingJobService.scheduleIfNeeded(context, /* forceSchedule= */ false); 241 EventFallbackReportingJobService.scheduleIfNeeded(context, /* forceSchedule= */ false); 242 DeleteExpiredJobService.scheduleIfNeeded(context, /* forceSchedule= */ false); 243 DeleteUninstalledJobService.scheduleIfNeeded(context, /* forceSchedule= */ false); 244 AsyncRegistrationQueueJobService.scheduleIfNeeded(context, /* forceSchedule= */ false); 245 AsyncRegistrationFallbackJob.schedule(); 246 VerboseDebugReportingFallbackJobService.scheduleIfNeeded( 247 context, /* forceSchedule= */ false); 248 DebugReportingFallbackJobService.scheduleIfNeeded(context, /* forceSchedule= */ false); 249 scheduleMddBackgroundJobs(); 250 scheduleEncryptionKeyBackgroundJobs(context); 251 scheduleCobaltBackgroundJob(context); 252 scheduleAdPackageDenyPreProcessBackgroundJob(); 253 } 254 } 255 256 /** 257 * Tries to schedule Cobalt logging related background jobs if the CobaltLoggingEnabled is true. 258 * 259 * @param context application context 260 */ scheduleCobaltBackgroundJob(Context context)261 public static void scheduleCobaltBackgroundJob(Context context) { 262 if (FlagsFactory.getFlags().getCobaltLoggingEnabled()) { 263 CobaltJobService.scheduleIfNeeded(context, /* forceSchedule= */ false); 264 } 265 } 266 267 /** Tries to schedule Package Deny related background jobs */ scheduleAdPackageDenyPreProcessBackgroundJob()268 public static void scheduleAdPackageDenyPreProcessBackgroundJob() { 269 if (FlagsFactory.getFlags().getEnablePackageDenyBgJob()) { 270 AdPackageDenyPreProcessJobService.scheduleIfNeeded(); 271 } 272 } 273 274 /** 275 * Tries to unschedule all the relevant background jobs. 276 * 277 * @param jobScheduler Job scheduler to cancel the jobs. 278 */ unscheduleAllBackgroundJobs(@onNull JobScheduler jobScheduler)279 public static void unscheduleAllBackgroundJobs(@NonNull JobScheduler jobScheduler) { 280 Objects.requireNonNull(jobScheduler); 281 282 unscheduleTopicsBackgroundJobs(jobScheduler); 283 284 unscheduleMeasurementBackgroundJobs(jobScheduler); 285 286 unscheduleFledgeBackgroundJobs(jobScheduler); 287 288 unscheduleMaintenanceJobs(jobScheduler); 289 290 jobScheduler.cancel(CONSENT_NOTIFICATION_JOB.getJobId()); 291 292 MddJob.unscheduleAllJobs(jobScheduler); 293 294 jobScheduler.cancel(ENCRYPTION_KEY_PERIODIC_JOB.getJobId()); 295 296 unscheduleCobaltJob(jobScheduler); 297 298 unscheduleAdPackageDenyPreProcessJob(jobScheduler); 299 } 300 301 /** 302 * Tries to unschedule all the Measurement related background jobs. 303 * 304 * @param jobScheduler Job scheduler to cancel the jobs. 305 */ unscheduleMeasurementBackgroundJobs(@onNull JobScheduler jobScheduler)306 public static void unscheduleMeasurementBackgroundJobs(@NonNull JobScheduler jobScheduler) { 307 Objects.requireNonNull(jobScheduler); 308 309 jobScheduler.cancel(MEASUREMENT_EVENT_MAIN_REPORTING_JOB.getJobId()); 310 jobScheduler.cancel(MEASUREMENT_DELETE_EXPIRED_JOB.getJobId()); 311 jobScheduler.cancel(MEASUREMENT_DELETE_UNINSTALLED_JOB.getJobId()); 312 jobScheduler.cancel(MEASUREMENT_ATTRIBUTION_JOB.getJobId()); 313 jobScheduler.cancel(MEASUREMENT_ATTRIBUTION_FALLBACK_JOB.getJobId()); 314 jobScheduler.cancel(MEASUREMENT_EVENT_FALLBACK_REPORTING_JOB.getJobId()); 315 jobScheduler.cancel(MEASUREMENT_AGGREGATE_MAIN_REPORTING_JOB.getJobId()); 316 jobScheduler.cancel(MEASUREMENT_AGGREGATE_FALLBACK_REPORTING_JOB.getJobId()); 317 jobScheduler.cancel(MEASUREMENT_IMMEDIATE_AGGREGATE_REPORTING_JOB.getJobId()); 318 jobScheduler.cancel(MEASUREMENT_REPORTING_JOB.getJobId()); 319 jobScheduler.cancel(MEASUREMENT_ASYNC_REGISTRATION_JOB.getJobId()); 320 jobScheduler.cancel(MEASUREMENT_ASYNC_REGISTRATION_FALLBACK_JOB.getJobId()); 321 jobScheduler.cancel(MEASUREMENT_VERBOSE_DEBUG_REPORTING_FALLBACK_JOB.getJobId()); 322 jobScheduler.cancel(MEASUREMENT_DEBUG_REPORTING_FALLBACK_JOB.getJobId()); 323 } 324 325 /** 326 * Tries to unschedule all the Topics related background jobs. 327 * 328 * @param jobScheduler Job scheduler to cancel the jobs. 329 */ unscheduleTopicsBackgroundJobs(@onNull JobScheduler jobScheduler)330 public static void unscheduleTopicsBackgroundJobs(@NonNull JobScheduler jobScheduler) { 331 Objects.requireNonNull(jobScheduler); 332 333 jobScheduler.cancel(TOPICS_EPOCH_JOB.getJobId()); 334 } 335 336 /** 337 * Tries to unschedule all the Fledge related background jobs. 338 * 339 * @param jobScheduler Job scheduler to cancel the jobs. 340 */ unscheduleFledgeBackgroundJobs(@onNull JobScheduler jobScheduler)341 public static void unscheduleFledgeBackgroundJobs(@NonNull JobScheduler jobScheduler) { 342 Objects.requireNonNull(jobScheduler); 343 344 jobScheduler.cancel(FLEDGE_BACKGROUND_FETCH_JOB.getJobId()); 345 jobScheduler.cancel(FLEDGE_AD_SELECTION_DEBUG_REPORT_SENDER_JOB.getJobId()); 346 jobScheduler.cancel(PERIODIC_SIGNALS_ENCODING_JOB.getJobId()); 347 jobScheduler.cancel(FLEDGE_KANON_SIGN_JOIN_BACKGROUND_JOB.getJobId()); 348 } 349 350 /** 351 * Tries to unschedule all the maintenance background jobs. 352 * 353 * @param jobScheduler Job scheduler to cancel the jobs. 354 */ unscheduleMaintenanceJobs(@onNull JobScheduler jobScheduler)355 public static void unscheduleMaintenanceJobs(@NonNull JobScheduler jobScheduler) { 356 Objects.requireNonNull(jobScheduler); 357 358 jobScheduler.cancel(MAINTENANCE_JOB.getJobId()); 359 } 360 361 /** Tries to unschedule Cobalt background job. */ unscheduleCobaltJob(@onNull JobScheduler jobScheduler)362 public static void unscheduleCobaltJob(@NonNull JobScheduler jobScheduler) { 363 Objects.requireNonNull(jobScheduler); 364 365 jobScheduler.cancel(COBALT_LOGGING_JOB.getJobId()); 366 } 367 368 /** Tries to unschedule Package Deny background job. */ unscheduleAdPackageDenyPreProcessJob(@onNull JobScheduler jobScheduler)369 public static void unscheduleAdPackageDenyPreProcessJob(@NonNull JobScheduler jobScheduler) { 370 Objects.requireNonNull(jobScheduler); 371 372 jobScheduler.cancel(AD_PACKAGE_DENY_PRE_PROCESS_JOB.getJobId()); 373 } 374 } 375