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.MddJobService; 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 35 import com.google.common.annotations.VisibleForTesting; 36 37 import java.util.Objects; 38 39 /** Custom Audience Service */ 40 // TODO(b/269798827): Enable for R. 41 @RequiresApi(Build.VERSION_CODES.S) 42 public class CustomAudienceService extends Service { 43 private static final LoggerFactory.Logger sLogger = LoggerFactory.getFledgeLogger(); 44 45 /** The binder service. This field will only be accessed on the main thread. */ 46 private CustomAudienceServiceImpl mCustomAudienceService; 47 48 private Flags mFlags; 49 CustomAudienceService()50 public CustomAudienceService() { 51 this(FlagsFactory.getFlags()); 52 } 53 54 @VisibleForTesting CustomAudienceService(Flags flags)55 CustomAudienceService(Flags flags) { 56 this.mFlags = flags; 57 } 58 59 @Override onCreate()60 public void onCreate() { 61 super.onCreate(); 62 if (mFlags.getFledgeCustomAudienceServiceKillSwitch()) { 63 sLogger.e("Custom Audience API is disabled"); 64 return; 65 } 66 67 if (mCustomAudienceService == null) { 68 mCustomAudienceService = CustomAudienceServiceImpl.create(this); 69 } 70 71 if (hasUserConsent()) { 72 PackageChangedReceiver.enableReceiver(this, mFlags); 73 MddJobService.scheduleIfNeeded(this, /* forceSchedule */ false); 74 } 75 } 76 77 @Override onBind(Intent intent)78 public IBinder onBind(Intent intent) { 79 if (mFlags.getFledgeCustomAudienceServiceKillSwitch()) { 80 sLogger.e("Custom Audience API is disabled"); 81 // Return null so that clients can not bind to the service. 82 return null; 83 } 84 return Objects.requireNonNull(mCustomAudienceService); 85 } 86 87 /** @return {@code true} if the Privacy Sandbox has user consent */ hasUserConsent()88 private boolean hasUserConsent() { 89 if (mFlags.getGaUxFeatureEnabled()) { 90 return ConsentManager.getInstance(this).getConsent(AdServicesApiType.FLEDGE).isGiven(); 91 } else { 92 return ConsentManager.getInstance(this).getConsent().isGiven(); 93 } 94 } 95 } 96