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.customaudience;
18 
19 import android.app.Service;
20 import android.content.Intent;
21 import android.os.Build;
22 import android.os.IBinder;
23 
24 import androidx.annotation.RequiresApi;
25 
26 import com.android.adservices.LoggerFactory;
27 import com.android.adservices.download.MddJob;
28 import com.android.adservices.service.Flags;
29 import com.android.adservices.service.FlagsFactory;
30 import com.android.adservices.service.common.PackageChangedReceiver;
31 import com.android.adservices.service.consent.AdServicesApiType;
32 import com.android.adservices.service.consent.ConsentManager;
33 import com.android.adservices.service.customaudience.CustomAudienceServiceImpl;
34 import com.android.internal.annotations.VisibleForTesting;
35 
36 import java.util.Objects;
37 
38 /** Custom Audience Service */
39 @RequiresApi(Build.VERSION_CODES.S)
40 public class CustomAudienceService extends Service {
41     private static final LoggerFactory.Logger sLogger = LoggerFactory.getFledgeLogger();
42 
43     /** The binder service. This field will only be accessed on the main thread. */
44     private CustomAudienceServiceImpl mCustomAudienceService;
45 
46     private Flags mFlags;
47 
CustomAudienceService()48     public CustomAudienceService() {
49         this(FlagsFactory.getFlags());
50     }
51 
52     @VisibleForTesting
CustomAudienceService(Flags flags)53     CustomAudienceService(Flags flags) {
54         this.mFlags = flags;
55     }
56 
57     @Override
onCreate()58     public void onCreate() {
59         super.onCreate();
60         if (mFlags.getFledgeCustomAudienceServiceKillSwitch()) {
61             sLogger.e("Custom Audience API is disabled");
62             return;
63         }
64 
65         if (mCustomAudienceService == null) {
66             mCustomAudienceService = CustomAudienceServiceImpl.create(this);
67         }
68 
69         if (hasUserConsent()) {
70             PackageChangedReceiver.enableReceiver(this, mFlags);
71             MddJob.scheduleAllMddJobs();
72         }
73     }
74 
75     @Override
onBind(Intent intent)76     public IBinder onBind(Intent intent) {
77         if (mFlags.getFledgeCustomAudienceServiceKillSwitch()) {
78             sLogger.e("Custom Audience API is disabled");
79             // Return null so that clients can not bind to the service.
80             return null;
81         }
82         return Objects.requireNonNull(mCustomAudienceService);
83     }
84 
85     /** Returns {@code true} if the Privacy Sandbox has user consent */
hasUserConsent()86     private boolean hasUserConsent() {
87         return ConsentManager.getInstance().getConsent(AdServicesApiType.FLEDGE).isGiven();
88     }
89 }
90