• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.MddJobService;
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.common.PackageChangedReceiver;
33 import com.android.adservices.service.consent.AdServicesApiType;
34 import com.android.adservices.service.consent.ConsentManager;
35 
36 import com.google.common.annotations.VisibleForTesting;
37 
38 import java.util.Objects;
39 
40 /** Ad Selection Service */
41 // TODO(b/269798827): Enable for R.
42 @RequiresApi(Build.VERSION_CODES.S)
43 public class AdSelectionService extends Service {
44     private static final LoggerFactory.Logger sLogger = LoggerFactory.getFledgeLogger();
45 
46     /** The binder service. This field will only be accessed on the main thread. */
47     private AdSelectionServiceImpl mAdSelectionService;
48 
49     private Flags mFlags;
50 
AdSelectionService()51     public AdSelectionService() {
52         this(FlagsFactory.getFlags());
53     }
54 
55     @VisibleForTesting
AdSelectionService(final Flags flags)56     AdSelectionService(final Flags flags) {
57         this.mFlags = flags;
58     }
59 
60     @Override
onCreate()61     public void onCreate() {
62         super.onCreate();
63         if (mFlags.getFledgeSelectAdsKillSwitch()) {
64             sLogger.e("Select Ads API is disabled");
65             return;
66         }
67 
68         if (mAdSelectionService == null) {
69             mAdSelectionService = AdSelectionServiceImpl.create(this);
70         }
71 
72         if (hasUserConsent()) {
73             PackageChangedReceiver.enableReceiver(this, mFlags);
74             MddJobService.scheduleIfNeeded(this, /* forceSchedule */ false);
75             MaintenanceJobService.scheduleIfNeeded(this, /* forceSchedule */ false);
76         }
77     }
78 
79     @Override
onBind(Intent intent)80     public IBinder onBind(Intent intent) {
81         if (mFlags.getFledgeSelectAdsKillSwitch()) {
82             sLogger.e("Select Ads API is 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         if (mFlags.getGaUxFeatureEnabled()) {
99             return ConsentManager.getInstance(this).getConsent(AdServicesApiType.FLEDGE).isGiven();
100         } else {
101             return ConsentManager.getInstance(this).getConsent().isGiven();
102         }
103     }
104 }
105