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.adservices.shared.spe;
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_JOB_NOT_CONFIGURED;
21 import static com.android.adservices.service.stats.AdServicesStatsLog.AD_SERVICES_BACKGROUND_JOBS_EXECUTION_REPORTED__EXECUTION_RESULT_CODE__SKIP_FOR_KILL_SWITCH_ON;
22 import static com.android.adservices.service.stats.AdServicesStatsLog.AD_SERVICES_BACKGROUND_JOBS_EXECUTION_REPORTED__EXECUTION_RESULT_CODE__SKIP_FOR_USER_CONSENT_REVOKED;
23 import static com.android.adservices.service.stats.AdServicesStatsLog.AD_SERVICES_ERROR_REPORTED__ERROR_CODE__SPE_JOB_SCHEDULER_IS_UNAVAILABLE;
24 import static com.android.adservices.service.stats.AdServicesStatsLog.BACKGROUND_JOB_SCHEDULING_REPORTED__MODULE_NAME__UNKNOWN_MODULE_NAME;
25 import static com.android.adservices.service.stats.AdServicesStatsLog.BACKGROUND_JOB_SCHEDULING_REPORTED__RESULT_CODE__SCHEDULING_RESULT_CODE_FAILED;
26 import static com.android.adservices.service.stats.AdServicesStatsLog.BACKGROUND_JOB_SCHEDULING_REPORTED__RESULT_CODE__SCHEDULING_RESULT_CODE_SKIPPED;
27 import static com.android.adservices.service.stats.AdServicesStatsLog.BACKGROUND_JOB_SCHEDULING_REPORTED__RESULT_CODE__SCHEDULING_RESULT_CODE_SUCCESSFUL;
28 import static com.android.adservices.service.stats.AdServicesStatsLog.BACKGROUND_JOB_SCHEDULING_REPORTED__SCHEDULER_TYPE__SCHEDULER_TYPE_JOB_SCHEDULER;
29 import static com.android.adservices.service.stats.AdServicesStatsLog.BACKGROUND_JOB_SCHEDULING_REPORTED__SCHEDULER_TYPE__SCHEDULER_TYPE_SPE;
30 
31 import android.annotation.IntDef;
32 import android.app.job.JobParameters;
33 
34 import java.lang.annotation.Retention;
35 import java.lang.annotation.RetentionPolicy;
36 
37 /** Class to store constants used by background jobs. */
38 public final class JobServiceConstants {
39     /**
40      * Unavailable stop reason, used when {@link
41      * android.app.job.JobService#onStopJob(JobParameters)} is not invoked in an execution.
42      *
43      * <p>Use the value of {@link JobParameters#STOP_REASON_UNDEFINED} in case API version is lower
44      * than S.
45      */
46     public static final int UNAVAILABLE_STOP_REASON = 0;
47 
48     /** The shared preference file name for background jobs */
49     public static final String SHARED_PREFS_BACKGROUND_JOBS = "PPAPI_Background_Jobs";
50 
51     /** The suffix to compose the key to store job start timestamp */
52     public static final String SHARED_PREFS_START_TIMESTAMP_SUFFIX = "_job_start_timestamp";
53 
54     /** The suffix to compose the key to store job stop timestamp */
55     public static final String SHARED_PREFS_STOP_TIMESTAMP_SUFFIX = "_job_stop_timestamp";
56 
57     /** The suffix to compose the key to store job execution period */
58     public static final String SHARED_PREFS_EXEC_PERIOD_SUFFIX = "_job_execution_period";
59 
60     /** A utility string key that should NEVER be fetched */
61     public static final String UNAVAILABLE_KEY = "unavailableKey";
62 
63     /**
64      * Value of the execution start timestamp when it's unavailable to achieve. For example, the
65      * shared preference key doesn't exist.
66      */
67     public static final long UNAVAILABLE_JOB_EXECUTION_START_TIMESTAMP = -1L;
68 
69     /**
70      * Value of the execution stop timestamp when it's unavailable to achieve. For example, the
71      * shared preference key doesn't exist.
72      */
73     public static final long UNAVAILABLE_JOB_EXECUTION_STOP_TIMESTAMP = -1L;
74 
75     /**
76      * Value of the execution period when it's unavailable to achieve, such as in the first
77      * execution.
78      */
79     public static final long UNAVAILABLE_JOB_EXECUTION_PERIOD = -1L;
80 
81     /**
82      * Value of the execution latency if it cannot be computed, such as an open-end execution caused
83      * by system or device issue.
84      */
85     public static final long UNAVAILABLE_JOB_LATENCY = -1L;
86 
87     /** The number of milliseconds per minute. */
88     public static final int MILLISECONDS_PER_MINUTE = 60 * 1000;
89 
90     /** Maximum possible percentage for percentage variables. */
91     public static final int MAX_PERCENTAGE = 100;
92 
93     /** A shorter version of error code for job scheduler is not available. */
94     public static final int ERROR_CODE_JOB_SCHEDULER_IS_UNAVAILABLE =
95             AD_SERVICES_ERROR_REPORTED__ERROR_CODE__SPE_JOB_SCHEDULER_IS_UNAVAILABLE;
96 
97     @IntDef(
98             value = {
99                 JOB_ENABLED_STATUS_ENABLED,
100                 JOB_ENABLED_STATUS_DISABLED_FOR_KILL_SWITCH_ON,
101                 JOB_ENABLED_STATUS_DISABLED_FOR_USER_CONSENT_REVOKED,
102                 JOB_ENABLED_STATUS_DISABLED_FOR_BACK_COMPAT_OTA,
103             })
104     @Retention(RetentionPolicy.SOURCE)
105     public @interface JobEnablementStatus {}
106 
107     /** The job is enabled. */
108     public static final int JOB_ENABLED_STATUS_ENABLED = -1;
109 
110     /** The job is disabled due to kill switch is ON. */
111     public static final int JOB_ENABLED_STATUS_DISABLED_FOR_KILL_SWITCH_ON =
112             AD_SERVICES_BACKGROUND_JOBS_EXECUTION_REPORTED__EXECUTION_RESULT_CODE__SKIP_FOR_KILL_SWITCH_ON;
113 
114     /** The job is disabled due to user consent is revoked. */
115     public static final int JOB_ENABLED_STATUS_DISABLED_FOR_USER_CONSENT_REVOKED =
116             AD_SERVICES_BACKGROUND_JOBS_EXECUTION_REPORTED__EXECUTION_RESULT_CODE__SKIP_FOR_USER_CONSENT_REVOKED;
117 
118     /** The job is disabled due to ExtServices' job running on AdServices */
119     public static final int JOB_ENABLED_STATUS_DISABLED_FOR_BACK_COMPAT_OTA =
120             AD_SERVICES_BACKGROUND_JOBS_EXECUTION_REPORTED__EXECUTION_RESULT_CODE__SKIP_FOR_EXTSERVICES_JOB_ON_TPLUS;
121 
122     /** The job is disabled due to misconfiguration in SPE framework. */
123     public static final int SKIP_REASON_JOB_NOT_CONFIGURED =
124             AD_SERVICES_BACKGROUND_JOBS_EXECUTION_REPORTED__EXECUTION_RESULT_CODE__SKIP_FOR_JOB_NOT_CONFIGURED;
125 
126     /** Shorter version for scheduler type - SPE. */
127     public static final int SCHEDULER_TYPE_SPE =
128             BACKGROUND_JOB_SCHEDULING_REPORTED__SCHEDULER_TYPE__SCHEDULER_TYPE_SPE;
129 
130     /** Shorter version for scheduler type - Job Scheduler. */
131     public static final int SCHEDULER_TYPE_JOB_SCHEDULER =
132             BACKGROUND_JOB_SCHEDULING_REPORTED__SCHEDULER_TYPE__SCHEDULER_TYPE_JOB_SCHEDULER;
133 
134     /** Shorter version for unknown module name. */
135     public static final int EXECUTION_LOGGING_UNKNOWN_MODULE_NAME =
136             BACKGROUND_JOB_SCHEDULING_REPORTED__MODULE_NAME__UNKNOWN_MODULE_NAME;
137 
138     /** Shorter version for unknown module name. */
139     public static final int SCHEDULING_LOGGING_UNKNOWN_MODULE_NAME =
140             BACKGROUND_JOB_SCHEDULING_REPORTED__MODULE_NAME__UNKNOWN_MODULE_NAME;
141 
142     @IntDef(
143             value = {
144                 SCHEDULING_RESULT_CODE_SUCCESSFUL,
145                 SCHEDULING_RESULT_CODE_FAILED,
146                 SCHEDULING_RESULT_CODE_SKIPPED,
147             })
148     @Retention(RetentionPolicy.SOURCE)
149     public @interface JobSchedulingResultCode {}
150 
151     public static final int SCHEDULING_RESULT_CODE_SUCCESSFUL =
152             BACKGROUND_JOB_SCHEDULING_REPORTED__RESULT_CODE__SCHEDULING_RESULT_CODE_SUCCESSFUL;
153 
154     public static final int SCHEDULING_RESULT_CODE_FAILED =
155             BACKGROUND_JOB_SCHEDULING_REPORTED__RESULT_CODE__SCHEDULING_RESULT_CODE_FAILED;
156 
157     public static final int SCHEDULING_RESULT_CODE_SKIPPED =
158             BACKGROUND_JOB_SCHEDULING_REPORTED__RESULT_CODE__SCHEDULING_RESULT_CODE_SKIPPED;
159 }
160