• 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 
25 import androidx.annotation.RequiresApi;
26 
27 import com.android.adservices.LogUtil;
28 import com.android.adservices.service.FlagsFactory;
29 import com.android.adservices.service.common.AdServicesCommonServiceImpl;
30 import com.android.adservices.service.common.AdServicesSyncUtil;
31 import com.android.adservices.ui.notifications.ConsentNotificationTrigger;
32 
33 import java.util.Objects;
34 import java.util.function.BiConsumer;
35 
36 /** Common service for work that applies to all PPAPIs. */
37 // TODO(b/269798827): Enable for R.
38 @RequiresApi(Build.VERSION_CODES.S)
39 public class AdServicesCommonService extends Service {
40 
41     /** The binder service. This field must only be accessed on the main thread. */
42     private AdServicesCommonServiceImpl mAdServicesCommonService;
43 
44     @Override
onCreate()45     public void onCreate() {
46         super.onCreate();
47         if (mAdServicesCommonService == null) {
48             mAdServicesCommonService =
49                     new AdServicesCommonServiceImpl(this, FlagsFactory.getFlags());
50         }
51         LogUtil.i("created adservices common service");
52         try {
53             AdServicesSyncUtil.getInstance()
54                     .register(
55                             new BiConsumer<Context, Boolean>() {
56                                 @Override
57                                 public void accept(
58                                         Context context, Boolean shouldDisplayEuNotification) {
59                                     LogUtil.i(
60                                             "running trigger command with "
61                                                     + shouldDisplayEuNotification);
62                                     ConsentNotificationTrigger.showConsentNotification(
63                                             context, shouldDisplayEuNotification);
64                                 }
65                             });
66         } catch (Exception e) {
67             LogUtil.e(
68                     "getting exception when register consumer in AdServicesSyncUtil of "
69                             + e.getMessage());
70         }
71     }
72 
73     @Override
onBind(Intent intent)74     public IBinder onBind(Intent intent) {
75         return Objects.requireNonNull(mAdServicesCommonService);
76     }
77 }
78