• 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_KILL_SWITCH_ON;
20 import static com.android.adservices.spe.AdServicesJobInfo.MEASUREMENT_DELETE_UNINSTALLED_JOB;
21 
22 import android.app.job.JobInfo;
23 import android.app.job.JobParameters;
24 import android.app.job.JobScheduler;
25 import android.app.job.JobService;
26 import android.content.ComponentName;
27 import android.content.Context;
28 import android.os.Build;
29 
30 import androidx.annotation.RequiresApi;
31 
32 import com.android.adservices.LogUtil;
33 import com.android.adservices.LoggerFactory;
34 import com.android.adservices.concurrency.AdServicesExecutors;
35 import com.android.adservices.service.Flags;
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 @RequiresApi(Build.VERSION_CODES.S)
47 public final class DeleteUninstalledJobService extends JobService {
48     private static final int MEASUREMENT_DELETE_UNINSTALLED_JOB_ID =
49             MEASUREMENT_DELETE_UNINSTALLED_JOB.getJobId();
50     private static final Executor sBackgroundExecutor = AdServicesExecutors.getBackgroundExecutor();
51 
52     @Override
onStartJob(JobParameters params)53     public boolean onStartJob(JobParameters params) {
54         // Always ensure that the first thing this job does is check if it should be running, and
55         // cancel itself if it's not supposed to be.
56         if (ServiceCompatUtils.shouldDisableExtServicesJobOnTPlus(this)) {
57             LogUtil.d(
58                     "Disabling DeleteUninstalledJobService job because it's running in ExtServices"
59                             + " on T+");
60             return skipAndCancelBackgroundJob(params, /* skipReason=*/ 0, /* doRecord=*/ false);
61         }
62 
63         AdServicesJobServiceLogger.getInstance()
64                 .recordOnStartJob(MEASUREMENT_DELETE_UNINSTALLED_JOB_ID);
65 
66         if (FlagsFactory.getFlags().getMeasurementJobDeleteUninstalledKillSwitch()) {
67             LoggerFactory.getMeasurementLogger().e("DeleteUninstalledJobService is disabled");
68             return skipAndCancelBackgroundJob(
69                     params,
70                     AD_SERVICES_BACKGROUND_JOBS_EXECUTION_REPORTED__EXECUTION_RESULT_CODE__SKIP_FOR_KILL_SWITCH_ON,
71                     /* doRecord=*/ true);
72         }
73 
74         LoggerFactory.getMeasurementLogger().d("DeleteUninstalledJobService.onStartJob");
75         sBackgroundExecutor.execute(
76                 () -> {
77                     MeasurementImpl.getInstance().deleteAllUninstalledMeasurementData();
78 
79                     boolean shouldRetry = false;
80                     AdServicesJobServiceLogger.getInstance()
81                             .recordJobFinished(
82                                     MEASUREMENT_DELETE_UNINSTALLED_JOB_ID,
83                                     /* isSuccessful */ true,
84                                     shouldRetry);
85                     jobFinished(params, shouldRetry);
86                 });
87         return true;
88     }
89 
90     @Override
onStopJob(JobParameters params)91     public boolean onStopJob(JobParameters params) {
92         LoggerFactory.getMeasurementLogger().d("DeleteUninstalledJobService.onStopJob");
93         boolean shouldRetry = false;
94 
95         AdServicesJobServiceLogger.getInstance()
96                 .recordOnStopJob(params, MEASUREMENT_DELETE_UNINSTALLED_JOB_ID, shouldRetry);
97         return shouldRetry;
98     }
99 
100     /** Schedule the job. */
101     @VisibleForTesting
schedule(JobScheduler jobScheduler, JobInfo jobInfo)102     static void schedule(JobScheduler jobScheduler, JobInfo jobInfo) {
103         jobScheduler.schedule(jobInfo);
104     }
105 
106     /**
107      * Schedule Delete Uninstalled Job Service if it is not already scheduled.
108      *
109      * @param context the context
110      * @param forceSchedule flag to indicate whether to force rescheduling the job.
111      */
112     // TODO(b/311183933): Remove passed in Context from static method.
113     @SuppressWarnings("AvoidStaticContext")
scheduleIfNeeded(Context context, boolean forceSchedule)114     public static void scheduleIfNeeded(Context context, boolean forceSchedule) {
115         Flags flags = FlagsFactory.getFlags();
116         if (flags.getMeasurementJobDeleteUninstalledKillSwitch()) {
117             LoggerFactory.getMeasurementLogger()
118                     .e("DeleteUninstalledJobService is disabled, skip scheduling");
119             return;
120         }
121 
122         final JobScheduler jobScheduler = context.getSystemService(JobScheduler.class);
123         if (jobScheduler == null) {
124             LoggerFactory.getMeasurementLogger().e("JobScheduler not found");
125             return;
126         }
127 
128         final JobInfo scheduledJob =
129                 jobScheduler.getPendingJob(MEASUREMENT_DELETE_UNINSTALLED_JOB_ID);
130         // Schedule if it hasn't been scheduled already or force rescheduling.
131         JobInfo jobInfo = buildJobInfo(context, flags);
132         if (forceSchedule || !jobInfo.equals(scheduledJob)) {
133             schedule(jobScheduler, jobInfo);
134             LoggerFactory.getMeasurementLogger().d("Scheduled DeleteUninstalledJobService");
135         } else {
136             LoggerFactory.getMeasurementLogger()
137                     .d("DeleteUninstalledJobService already scheduled, skipping reschedule");
138         }
139     }
140 
141     // TODO(b/311183933): Remove passed in Context from static method.
142     @SuppressWarnings("AvoidStaticContext")
buildJobInfo(Context context, Flags flags)143     private static JobInfo buildJobInfo(Context context, Flags flags) {
144         return new JobInfo.Builder(
145                         MEASUREMENT_DELETE_UNINSTALLED_JOB_ID,
146                         new ComponentName(context, DeleteUninstalledJobService.class))
147                 .setPeriodic(flags.getMeasurementDeleteUninstalledJobPeriodMs())
148                 .setPersisted(flags.getMeasurementDeleteUninstalledJobPersisted())
149                 .build();
150     }
151 
skipAndCancelBackgroundJob( final JobParameters params, int skipReason, boolean doRecord)152     private boolean skipAndCancelBackgroundJob(
153             final JobParameters params, int skipReason, boolean doRecord) {
154         final JobScheduler jobScheduler = this.getSystemService(JobScheduler.class);
155         if (jobScheduler != null) {
156             jobScheduler.cancel(MEASUREMENT_DELETE_UNINSTALLED_JOB_ID);
157         }
158 
159         if (doRecord) {
160             AdServicesJobServiceLogger.getInstance()
161                     .recordJobSkipped(MEASUREMENT_DELETE_UNINSTALLED_JOB_ID, skipReason);
162         }
163 
164         // Tell the JobScheduler that the job has completed and does not need to be rescheduled.
165         jobFinished(params, /* wantsReschedule */ false);
166 
167         // Returning false meas that this job has completed its work.
168         return false;
169     }
170 }
171