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.common;
18 
19 import android.app.Service;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.os.Build;
23 import android.os.IBinder;
24 import android.os.Trace;
25 
26 import androidx.annotation.RequiresApi;
27 
28 import com.android.adservices.LogUtil;
29 import com.android.adservices.service.FlagsFactory;
30 import com.android.adservices.service.adid.AdIdWorker;
31 import com.android.adservices.service.common.AdServicesCommonServiceImpl;
32 import com.android.adservices.service.common.AdServicesSyncUtil;
33 import com.android.adservices.service.stats.AdServicesLoggerImpl;
34 import com.android.adservices.service.ui.UxEngine;
35 import com.android.adservices.service.ui.data.UxStatesManager;
36 import com.android.adservices.shared.util.Clock;
37 import com.android.adservices.ui.notifications.ConsentNotificationTrigger;
38 
39 import java.util.Objects;
40 import java.util.function.BiConsumer;
41 
42 /** Common service for work that applies to all PPAPIs. */
43 // TODO(b/269798827): Enable for R.
44 @RequiresApi(Build.VERSION_CODES.S)
45 public class AdServicesCommonService extends Service {
46 
47     /** The binder service. This field must only be accessed on the main thread. */
48     private AdServicesCommonServiceImpl mAdServicesCommonService;
49 
50     @Override
onCreate()51     public void onCreate() {
52         super.onCreate();
53 
54         Trace.beginSection("AdServicesCommonService#Initialization");
55         if (mAdServicesCommonService == null) {
56             mAdServicesCommonService =
57                     new AdServicesCommonServiceImpl(
58                             this,
59                             FlagsFactory.getFlags(),
60                             UxEngine.getInstance(this),
61                             UxStatesManager.getInstance(),
62                             AdIdWorker.getInstance(),
63                             AdServicesLoggerImpl.getInstance(),
64                             Clock.getInstance());
65         }
66         LogUtil.d("created adservices common service");
67         try {
68             AdServicesSyncUtil.getInstance()
69                     .register(
70                             new BiConsumer<Context, Boolean>() {
71                                 @Override
72                                 public void accept(
73                                         Context context, Boolean shouldDisplayEuNotification) {
74                                     LogUtil.d(
75                                             "running trigger command with "
76                                                     + shouldDisplayEuNotification);
77                                     ConsentNotificationTrigger.showConsentNotification(
78                                             context, shouldDisplayEuNotification);
79                                 }
80                             });
81         } catch (Exception e) {
82             LogUtil.e(
83                     "getting exception when register consumer in AdServicesSyncUtil of "
84                             + e.getMessage());
85         }
86         Trace.endSection();
87     }
88 
89     @Override
onBind(Intent intent)90     public IBinder onBind(Intent intent) {
91         return Objects.requireNonNull(mAdServicesCommonService);
92     }
93 }
94