• 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.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.DebugFlags;
30 import com.android.adservices.service.FlagsFactory;
31 import com.android.adservices.service.adid.AdIdWorker;
32 import com.android.adservices.service.common.AdServicesCommonServiceImpl;
33 import com.android.adservices.service.common.AdServicesSyncUtil;
34 import com.android.adservices.service.common.QuadConsumer;
35 import com.android.adservices.service.stats.AdServicesLoggerImpl;
36 import com.android.adservices.service.ui.UxEngine;
37 import com.android.adservices.service.ui.data.UxStatesManager;
38 import com.android.adservices.shared.util.Clock;
39 import com.android.adservices.ui.notifications.ConsentNotificationTrigger;
40 
41 import java.util.Objects;
42 import java.util.function.BiConsumer;
43 
44 /** Common service for work that applies to all PPAPIs. */
45 @RequiresApi(Build.VERSION_CODES.S)
46 public class AdServicesCommonService extends Service {
47 
48     /** The binder service. This field must only be accessed on the main thread. */
49     private AdServicesCommonServiceImpl mAdServicesCommonService;
50 
51     @Override
onCreate()52     public void onCreate() {
53         super.onCreate();
54 
55         Trace.beginSection("AdServicesCommonService#Initialization");
56         if (mAdServicesCommonService == null) {
57             mAdServicesCommonService =
58                     new AdServicesCommonServiceImpl(
59                             this,
60                             FlagsFactory.getFlags(),
61                             DebugFlags.getInstance(),
62                             UxEngine.getInstance(),
63                             UxStatesManager.getInstance(),
64                             AdIdWorker.getInstance(),
65                             AdServicesLoggerImpl.getInstance(),
66                             Clock.getInstance());
67         }
68         LogUtil.d("created adservices common service");
69         try {
70             AdServicesSyncUtil syncUtil = AdServicesSyncUtil.getInstance();
71             syncUtil.register(
72                     new BiConsumer<Context, Boolean>() {
73                         @Override
74                         public void accept(Context context, Boolean shouldDisplayEuNotification) {
75                             LogUtil.d(
76                                     "running trigger command with " + shouldDisplayEuNotification);
77                             ConsentNotificationTrigger.showConsentNotification(
78                                     context, shouldDisplayEuNotification);
79                         }
80                     });
81             boolean businessLogicMigrationFlag =
82                     FlagsFactory.getFlags().getAdServicesConsentBusinessLogicMigrationEnabled();
83             if (businessLogicMigrationFlag) {
84                 syncUtil.registerNotificationTriggerV2(
85                         new QuadConsumer<Context, Boolean, Boolean, Boolean>() {
86                             @Override
87                             public void accept(
88                                     Context context,
89                                     Boolean isRenotify,
90                                     Boolean isNewAdPersonalizationModuleEnabled,
91                                     Boolean isOngoingNotification) {
92                                 LogUtil.d(
93                                         "running V2 trigger command with:"
94                                                 + ", isRenotify: "
95                                                 + isRenotify
96                                                 + ", isNewAdPersonalizationModuleEnabled: "
97                                                 + isNewAdPersonalizationModuleEnabled
98                                                 + ", isOngoingNotification: "
99                                                 + isOngoingNotification);
100                                 ConsentNotificationTrigger.showConsentNotificationV2(
101                                         context,
102                                         isRenotify,
103                                         isNewAdPersonalizationModuleEnabled,
104                                         isOngoingNotification);
105                             }
106                         });
107             }
108         } catch (Exception e) {
109             LogUtil.e(
110                     "getting exception when register consumer in AdServicesSyncUtil of "
111                             + e.getMessage());
112         }
113         Trace.endSection();
114     }
115 
116     @Override
onBind(Intent intent)117     public IBinder onBind(Intent intent) {
118         return Objects.requireNonNull(mAdServicesCommonService);
119     }
120 }
121