• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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;
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_DELETE_UNINSTALLED_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 import android.os.Build;
30 
31 import androidx.annotation.RequiresApi;
32 
33 import com.android.adservices.LogUtil;
34 import com.android.adservices.concurrency.AdServicesExecutors;
35 import com.android.adservices.service.AdServicesConfig;
36 import com.android.adservices.service.FlagsFactory;
37 import com.android.adservices.service.common.compat.ServiceCompatUtils;
38 import com.android.adservices.spe.AdservicesJobServiceLogger;
39 import com.android.internal.annotations.VisibleForTesting;
40 
41 import java.util.concurrent.Executor;
42 
43 /**
44  * Service for deleting data for uninstalled packages that the package change receiver has missed.
45  */
46 // TODO(b/269798827): Enable for R.
47 @RequiresApi(Build.VERSION_CODES.S)
48 public final class DeleteUninstalledJobService extends JobService {
49     private static final int MEASUREMENT_DELETE_UNINSTALLED_JOB_ID =
50             MEASUREMENT_DELETE_UNINSTALLED_JOB.getJobId();
51     private static final Executor sBackgroundExecutor = AdServicesExecutors.getBackgroundExecutor();
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 DeleteUninstalledJobService job because it's running in ExtServices"
65                             + " on 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_DELETE_UNINSTALLED_JOB_ID);
73 
74         if (FlagsFactory.getFlags().getMeasurementJobDeleteUninstalledKillSwitch()) {
75             LogUtil.e("DeleteUninstalledJobService 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("DeleteUninstalledJobService.onStartJob");
82         sBackgroundExecutor.execute(
83                 () -> {
84                     MeasurementImpl.getInstance(this).deleteAllUninstalledMeasurementData();
85 
86                     boolean shouldRetry = false;
87                     AdservicesJobServiceLogger.getInstance(DeleteUninstalledJobService.this)
88                             .recordJobFinished(
89                                     MEASUREMENT_DELETE_UNINSTALLED_JOB_ID,
90                                     /* isSuccessful */ true,
91                                     shouldRetry);
92                     jobFinished(params, shouldRetry);
93                 });
94         return true;
95     }
96 
97     @Override
onStopJob(JobParameters params)98     public boolean onStopJob(JobParameters params) {
99         LogUtil.d("DeleteUninstalledJobService.onStopJob");
100         boolean shouldRetry = false;
101 
102         AdservicesJobServiceLogger.getInstance(this)
103                 .recordOnStopJob(params, MEASUREMENT_DELETE_UNINSTALLED_JOB_ID, shouldRetry);
104         return shouldRetry;
105     }
106 
107     /** Schedule the job. */
108     @VisibleForTesting
schedule(Context context, JobScheduler jobScheduler)109     static void schedule(Context context, JobScheduler jobScheduler) {
110         final JobInfo job =
111                 new JobInfo.Builder(
112                                 MEASUREMENT_DELETE_UNINSTALLED_JOB_ID,
113                                 new ComponentName(context, DeleteUninstalledJobService.class))
114                         .setRequiresDeviceIdle(true)
115                         .setPeriodic(AdServicesConfig.getMeasurementDeleteExpiredJobPeriodMs())
116                         .setPersisted(true)
117                         .build();
118         jobScheduler.schedule(job);
119     }
120 
121     /**
122      * Schedule Delete Uninstalled Job Service if it is not already scheduled.
123      *
124      * @param context the context
125      * @param forceSchedule flag to indicate whether to force rescheduling the job.
126      */
scheduleIfNeeded(Context context, boolean forceSchedule)127     public static void scheduleIfNeeded(Context context, boolean forceSchedule) {
128         if (FlagsFactory.getFlags().getMeasurementJobDeleteUninstalledKillSwitch()) {
129             LogUtil.e("DeleteUninstalledJobService is disabled, skip scheduling");
130             return;
131         }
132 
133         final JobScheduler jobScheduler = context.getSystemService(JobScheduler.class);
134         if (jobScheduler == null) {
135             LogUtil.e("JobScheduler not found");
136             return;
137         }
138 
139         final JobInfo job = jobScheduler.getPendingJob(MEASUREMENT_DELETE_UNINSTALLED_JOB_ID);
140         // Schedule if it hasn't been scheduled already or force rescheduling.
141         if (job == null || forceSchedule) {
142             schedule(context, jobScheduler);
143             LogUtil.d("Scheduled DeleteUninstalledJobService");
144         } else {
145             LogUtil.d("DeleteUninstalledJobService already scheduled, skipping reschedule");
146         }
147     }
148 
skipAndCancelBackgroundJob(final JobParameters params, int skipReason)149     private boolean skipAndCancelBackgroundJob(final JobParameters params, int skipReason) {
150         final JobScheduler jobScheduler = this.getSystemService(JobScheduler.class);
151         if (jobScheduler != null) {
152             jobScheduler.cancel(MEASUREMENT_DELETE_UNINSTALLED_JOB_ID);
153         }
154 
155         AdservicesJobServiceLogger.getInstance(this)
156                 .recordJobSkipped(MEASUREMENT_DELETE_UNINSTALLED_JOB_ID, skipReason);
157 
158         // Tell the JobScheduler that the job has completed and does not need to be rescheduled.
159         jobFinished(params, /* wantsReschedule */ false);
160 
161         // Returning false meas that this job has completed its work.
162         return false;
163     }
164 }
165