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.MAINTENANCE_TASK_JOB_ID; 20 21 import android.app.job.JobParameters; 22 23 import com.android.adservices.shared.spe.framework.AbstractJobService; 24 import com.android.adservices.shared.spe.framework.JobServiceFactory; 25 import com.android.internal.annotations.VisibleForTesting; 26 import com.android.ondevicepersonalization.internal.util.LoggerFactory; 27 import com.android.ondevicepersonalization.services.Flags; 28 import com.android.ondevicepersonalization.services.FlagsFactory; 29 30 /** The ODP's implementation of {@link AbstractJobService}. */ 31 public final class OdpJobService extends AbstractJobService { 32 private static final LoggerFactory.Logger sLogger = LoggerFactory.getLogger(); 33 34 @Override getJobServiceFactory()35 protected JobServiceFactory getJobServiceFactory() { 36 return OdpJobServiceFactory.getInstance(this); 37 } 38 39 @Override onStartJob(JobParameters params)40 public boolean onStartJob(JobParameters params) { 41 int jobId = params.getJobId(); 42 43 // Switch to the legacy job scheduling if SPE is disabled. Since job ID remains the same, 44 // the scheduled job will be cancelled and rescheduled with the legacy method. 45 // 46 // And after the job is rescheduled, it will execute once instantly so don't log execution 47 // stats here. 48 if (shouldRescheduleWithLegacyMethod(jobId)) { 49 sLogger.d( 50 "SPE is disabled. Reschedule SPE job instance of jobId=%d with its legacy" 51 + " JobService scheduling method.", 52 jobId); 53 54 OdpJobServiceFactory factory = (OdpJobServiceFactory) getJobServiceFactory(); 55 factory.rescheduleJobWithLegacyMethod(this, jobId); 56 57 return false; 58 } 59 60 return super.onStartJob(params); 61 } 62 63 // Determine whether we should cancel and reschedule current job with the legacy JobService 64 // class. It could happen when SPE has a production issue so SPE gets disabled. 65 // 66 // The first batch job to migrate is, 67 // - OnDevicePersonalizationMaintenanceJobService, job ID = 1005. 68 @VisibleForTesting shouldRescheduleWithLegacyMethod(int jobId)69 boolean shouldRescheduleWithLegacyMethod(int jobId) { 70 Flags flags = FlagsFactory.getFlags(); 71 72 if (jobId == MAINTENANCE_TASK_JOB_ID && !flags.getSpePilotJobEnabled()) { 73 return true; 74 } 75 76 return false; 77 } 78 } 79