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 package com.android.adservices.appsetid;
17 
18 import static com.android.adservices.service.stats.AdServicesStatsLog.AD_SERVICES_API_CALLED__API_CLASS__APPSETID;
19 
20 import android.app.Service;
21 import android.content.Intent;
22 import android.os.IBinder;
23 
24 import com.android.adservices.LogUtil;
25 import com.android.adservices.service.FlagsFactory;
26 import com.android.adservices.service.appsetid.AppSetIdServiceImpl;
27 import com.android.adservices.service.appsetid.AppSetIdWorker;
28 import com.android.adservices.service.common.AppImportanceFilter;
29 import com.android.adservices.service.common.Throttler;
30 import com.android.adservices.service.stats.AdServicesLoggerImpl;
31 import com.android.adservices.shared.util.Clock;
32 
33 import java.io.FileDescriptor;
34 import java.io.PrintWriter;
35 import java.util.Objects;
36 
37 /** AppSetId Service */
38 public class AppSetIdService extends Service {
39 
40     /** The binder service. This field must only be accessed on the main thread. */
41     private AppSetIdServiceImpl mAppSetIdService;
42 
43     @Override
onCreate()44     public void onCreate() {
45         super.onCreate();
46 
47         if (FlagsFactory.getFlags().getAppSetIdKillSwitch()) {
48             LogUtil.e("AppSetId API is disabled");
49             return;
50         }
51 
52         AppImportanceFilter appImportanceFilter =
53                 AppImportanceFilter.create(
54                         this,
55                         AD_SERVICES_API_CALLED__API_CLASS__APPSETID,
56                         () -> FlagsFactory.getFlags().getForegroundStatuslLevelForValidation());
57 
58         if (mAppSetIdService == null) {
59             mAppSetIdService =
60                     new AppSetIdServiceImpl(
61                             this,
62                             AppSetIdWorker.getInstance(),
63                             AdServicesLoggerImpl.getInstance(),
64                             Clock.getInstance(),
65                             FlagsFactory.getFlags(),
66                             Throttler.getInstance(FlagsFactory.getFlags()),
67                             appImportanceFilter);
68         }
69     }
70 
71     @Override
onBind(Intent intent)72     public IBinder onBind(Intent intent) {
73         if (FlagsFactory.getFlags().getAppSetIdKillSwitch()) {
74             LogUtil.e("AppSetId API is disabled");
75             // Return null so that clients can not bind to the service.
76             return null;
77         }
78 
79         return Objects.requireNonNull(mAppSetIdService);
80     }
81 
82     @Override
dump(FileDescriptor fd, PrintWriter writer, String[] args)83     public void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
84         super.dump(fd, writer, args);
85     }
86 }
87