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.measurement; 17 18 import static com.android.adservices.service.stats.AdServicesStatsLog.AD_SERVICES_API_CALLED__API_CLASS__MEASUREMENT; 19 20 import android.app.Service; 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.download.MddJobService; 29 import com.android.adservices.service.Flags; 30 import com.android.adservices.service.FlagsFactory; 31 import com.android.adservices.service.common.AppImportanceFilter; 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 import com.android.adservices.service.measurement.DeleteExpiredJobService; 36 import com.android.adservices.service.measurement.DeleteUninstalledJobService; 37 import com.android.adservices.service.measurement.MeasurementServiceImpl; 38 import com.android.adservices.service.measurement.attribution.AttributionJobService; 39 import com.android.adservices.service.measurement.registration.AsyncRegistrationQueueJobService; 40 import com.android.adservices.service.measurement.reporting.AggregateFallbackReportingJobService; 41 import com.android.adservices.service.measurement.reporting.AggregateReportingJobService; 42 import com.android.adservices.service.measurement.reporting.EventFallbackReportingJobService; 43 import com.android.adservices.service.measurement.reporting.EventReportingJobService; 44 import com.android.adservices.service.stats.Clock; 45 46 import java.util.Objects; 47 48 /** Measurement Service */ 49 // TODO(b/269798827): Enable for R. 50 @RequiresApi(Build.VERSION_CODES.S) 51 public class MeasurementService extends Service { 52 53 /** The binder service. This field must only be accessed on the main thread. */ 54 private MeasurementServiceImpl mMeasurementService; 55 56 @Override onCreate()57 public void onCreate() { 58 super.onCreate(); 59 Flags flags = FlagsFactory.getFlags(); 60 if (flags.getMeasurementKillSwitch()) { 61 LogUtil.e("Measurement API is disabled"); 62 return; 63 } 64 65 if (mMeasurementService == null) { 66 final AppImportanceFilter appImportanceFilter = 67 AppImportanceFilter.create( 68 this, 69 AD_SERVICES_API_CALLED__API_CLASS__MEASUREMENT, 70 () -> FlagsFactory.getFlags().getForegroundStatuslLevelForValidation()); 71 72 mMeasurementService = 73 new MeasurementServiceImpl( 74 this, 75 Clock.SYSTEM_CLOCK, 76 ConsentManager.getInstance(this), 77 flags, 78 appImportanceFilter); 79 } 80 81 if (hasUserConsent()) { 82 PackageChangedReceiver.enableReceiver(this, flags); 83 schedulePeriodicJobsIfNeeded(); 84 } 85 } 86 87 @Override onBind(Intent intent)88 public IBinder onBind(Intent intent) { 89 if (FlagsFactory.getFlags().getMeasurementKillSwitch()) { 90 LogUtil.e("Measurement API is disabled"); 91 // Return null so that clients can not bind to the service. 92 return null; 93 } 94 return Objects.requireNonNull(mMeasurementService); 95 } 96 hasUserConsent()97 private boolean hasUserConsent() { 98 if (FlagsFactory.getFlags().getGaUxFeatureEnabled()) { 99 return ConsentManager.getInstance(this) 100 .getConsent(AdServicesApiType.MEASUREMENTS) 101 .isGiven(); 102 } else { 103 return ConsentManager.getInstance(this).getConsent().isGiven(); 104 } 105 } 106 schedulePeriodicJobsIfNeeded()107 private void schedulePeriodicJobsIfNeeded() { 108 AggregateReportingJobService.scheduleIfNeeded(this, false); 109 AggregateFallbackReportingJobService.scheduleIfNeeded(this, false); 110 AttributionJobService.scheduleIfNeeded(this, false); 111 EventReportingJobService.scheduleIfNeeded(this, false); 112 EventFallbackReportingJobService.scheduleIfNeeded(this, false); 113 DeleteExpiredJobService.scheduleIfNeeded(this, false); 114 DeleteUninstalledJobService.scheduleIfNeeded(this, false); 115 MddJobService.scheduleIfNeeded(this, false); 116 AsyncRegistrationQueueJobService.scheduleIfNeeded(this, false); 117 } 118 } 119