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.adselection; 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.MaintenanceJobService; 31 import com.android.adservices.service.adselection.AdSelectionServiceImpl; 32 import com.android.adservices.service.adselection.encryption.BackgroundKeyFetchJobService; 33 import com.android.adservices.service.common.PackageChangedReceiver; 34 import com.android.adservices.service.consent.AdServicesApiType; 35 import com.android.adservices.service.consent.ConsentManager; 36 import com.android.internal.annotations.VisibleForTesting; 37 38 import java.util.Objects; 39 40 /** Ad Selection Service */ 41 @RequiresApi(Build.VERSION_CODES.S) 42 public class AdSelectionService 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 AdSelectionServiceImpl mAdSelectionService; 47 48 private Flags mFlags; 49 AdSelectionService()50 public AdSelectionService() { 51 this(FlagsFactory.getFlags()); 52 } 53 54 @VisibleForTesting AdSelectionService(final Flags flags)55 AdSelectionService(final Flags flags) { 56 this.mFlags = flags; 57 } 58 59 @Override onCreate()60 public void onCreate() { 61 super.onCreate(); 62 if (mFlags.getFledgeSelectAdsKillSwitch()) { 63 sLogger.e("Ad Selection Service APIs are disabled"); 64 return; 65 } 66 67 if (mAdSelectionService == null) { 68 mAdSelectionService = AdSelectionServiceImpl.create(this); 69 } 70 71 if (hasUserConsent()) { 72 PackageChangedReceiver.enableReceiver(this, mFlags); 73 MddJob.scheduleAllMddJobs(); 74 MaintenanceJobService.scheduleIfNeeded(this, /* forceSchedule */ false); 75 BackgroundKeyFetchJobService.scheduleIfNeeded(this, mFlags, /* forceSchedule */ false); 76 } 77 } 78 79 @Override onBind(Intent intent)80 public IBinder onBind(Intent intent) { 81 if (mFlags.getFledgeSelectAdsKillSwitch()) { 82 sLogger.e("Ad Selection Service APIs are disabled"); 83 // Return null so that clients can not bind to the service. 84 return null; 85 } 86 return Objects.requireNonNull(mAdSelectionService); 87 } 88 89 @Override onDestroy()90 public void onDestroy() { 91 if (mAdSelectionService != null) { 92 mAdSelectionService.destroy(); 93 } 94 } 95 96 /** @return {@code true} if the Privacy Sandbox has user consent */ hasUserConsent()97 private boolean hasUserConsent() { 98 return ConsentManager.getInstance().getConsent(AdServicesApiType.FLEDGE).isGiven(); 99 } 100 } 101