• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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.common.compat;
18 
19 import static com.android.adservices.AdServicesCommon.ADEXTSERVICES_PACKAGE_NAME_SUFFIX;
20 
21 import android.content.Context;
22 
23 import androidx.annotation.NonNull;
24 
25 import com.android.modules.utils.build.SdkLevel;
26 
27 import java.util.Objects;
28 
29 /**
30  * Utility class that contains methods associated with {@link android.app.job.JobService} that need
31  * to be handled in a backward-compatible manner.
32  */
33 public final class ServiceCompatUtils {
ServiceCompatUtils()34     private ServiceCompatUtils() {
35         // Prevent instantiation
36     }
37 
38     /**
39      * Checks if the specified context is running within the ExtServices apex on a device running
40      * Android T or later.
41      *
42      * @param context the context to use to retrieve the package name.
43      * @return true if the device is running on Android T+ and the package name matches the
44      *     AdServices package within the ExtServices apex.
45      */
shouldDisableExtServicesJobOnTPlus(@onNull Context context)46     public static boolean shouldDisableExtServicesJobOnTPlus(@NonNull Context context) {
47         Objects.requireNonNull(context);
48 
49         // On S or lower, do not disable the job because it's the only one running.
50         if (!SdkLevel.isAtLeastT()) {
51             return false;
52         }
53 
54         // On T+, check if the job is in the ExtServices apk. This indicates that it's a holdover
55         // from an OTA from R/S, and should therefore be disabled.
56         String packageName = context.getPackageName();
57         return packageName != null && packageName.endsWith(ADEXTSERVICES_PACKAGE_NAME_SUFFIX);
58     }
59 }
60