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