1 /* 2 * Copyright (C) 2024 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.ondevicepersonalization.services.sharedlibrary.spe; 18 19 import static com.android.ondevicepersonalization.services.OnDevicePersonalizationConfig.AGGREGATE_ERROR_DATA_REPORTING_JOB_ID; 20 import static com.android.ondevicepersonalization.services.OnDevicePersonalizationConfig.DOWNLOAD_PROCESSING_TASK_JOB_ID; 21 import static com.android.ondevicepersonalization.services.OnDevicePersonalizationConfig.MAINTENANCE_TASK_JOB_ID; 22 import static com.android.ondevicepersonalization.services.OnDevicePersonalizationConfig.MDD_CELLULAR_CHARGING_PERIODIC_TASK_JOB_ID; 23 import static com.android.ondevicepersonalization.services.OnDevicePersonalizationConfig.MDD_CHARGING_PERIODIC_TASK_JOB_ID; 24 import static com.android.ondevicepersonalization.services.OnDevicePersonalizationConfig.MDD_MAINTENANCE_PERIODIC_TASK_JOB_ID; 25 import static com.android.ondevicepersonalization.services.OnDevicePersonalizationConfig.MDD_WIFI_CHARGING_PERIODIC_TASK_JOB_ID; 26 import static com.android.ondevicepersonalization.services.OnDevicePersonalizationConfig.RESET_DATA_JOB_ID; 27 import static com.android.ondevicepersonalization.services.OnDevicePersonalizationConfig.USER_DATA_COLLECTION_ID; 28 29 import android.app.job.JobParameters; 30 31 import com.android.adservices.shared.spe.framework.AbstractJobService; 32 import com.android.adservices.shared.spe.framework.JobServiceFactory; 33 import com.android.internal.annotations.VisibleForTesting; 34 import com.android.ondevicepersonalization.internal.util.LoggerFactory; 35 import com.android.ondevicepersonalization.services.Flags; 36 import com.android.ondevicepersonalization.services.FlagsFactory; 37 38 /** The ODP's implementation of {@link AbstractJobService}. */ 39 public final class OdpJobService extends AbstractJobService { 40 private static final LoggerFactory.Logger sLogger = LoggerFactory.getLogger(); 41 42 @Override getJobServiceFactory()43 protected JobServiceFactory getJobServiceFactory() { 44 return OdpJobServiceFactory.getInstance(this); 45 } 46 47 @Override onStartJob(JobParameters params)48 public boolean onStartJob(JobParameters params) { 49 int jobId = params.getJobId(); 50 51 // Switch to the legacy job scheduling if SPE is disabled. Since job ID remains the same, 52 // the scheduled job will be cancelled and rescheduled with the legacy method. 53 // 54 // And after the job is rescheduled, it will execute once instantly so don't log execution 55 // stats here. 56 if (shouldRescheduleWithLegacyMethod(jobId)) { 57 sLogger.d( 58 "SPE is disabled. Reschedule SPE job instance of jobId=%d with its legacy" 59 + " JobService scheduling method.", 60 jobId); 61 62 OdpJobServiceFactory factory = (OdpJobServiceFactory) getJobServiceFactory(); 63 factory.rescheduleJobWithLegacyMethod(this, jobId, params.getExtras()); 64 65 return false; 66 } 67 68 return super.onStartJob(params); 69 } 70 71 // Determine whether we should cancel and reschedule current job with the legacy JobService 72 // class. It could happen when SPE has a production issue so SPE gets disabled. 73 // 74 // The first batch job to migrate is, 75 // - OnDevicePersonalizationMaintenanceJobService, job ID = 1005. 76 @VisibleForTesting shouldRescheduleWithLegacyMethod(int jobId)77 boolean shouldRescheduleWithLegacyMethod(int jobId) { 78 Flags flags = FlagsFactory.getFlags(); 79 80 switch (jobId) { 81 case AGGREGATE_ERROR_DATA_REPORTING_JOB_ID: 82 return !flags.getSpeOnAggregateErrorDataReportingJobEnabled(); 83 case DOWNLOAD_PROCESSING_TASK_JOB_ID: 84 return !flags.getSpeOnOdpDownloadProcessingJobEnabled(); 85 case MAINTENANCE_TASK_JOB_ID: 86 return !flags.getSpePilotJobEnabled(); 87 case MDD_CELLULAR_CHARGING_PERIODIC_TASK_JOB_ID: 88 case MDD_CHARGING_PERIODIC_TASK_JOB_ID: 89 case MDD_MAINTENANCE_PERIODIC_TASK_JOB_ID: 90 case MDD_WIFI_CHARGING_PERIODIC_TASK_JOB_ID: 91 return !flags.getSpeOnMddJobEnabled(); 92 case RESET_DATA_JOB_ID: 93 return !flags.getSpeOnResetDataJobEnabled(); 94 case USER_DATA_COLLECTION_ID: 95 return !flags.getSpeOnUserDataCollectionJobEnabled(); 96 default: 97 return false; 98 } 99 } 100 } 101