1 /*
2  * Copyright 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 androidx.privacysandbox.ads.adservices.java.adid
18 
19 import android.adservices.common.AdServicesPermissions
20 import android.content.Context
21 import android.os.LimitExceededException
22 import androidx.annotation.DoNotInline
23 import androidx.annotation.RequiresPermission
24 import androidx.privacysandbox.ads.adservices.adid.AdId
25 import androidx.privacysandbox.ads.adservices.adid.AdIdManager
26 import androidx.privacysandbox.ads.adservices.java.internal.asListenableFuture
27 import com.google.common.util.concurrent.ListenableFuture
28 import kotlinx.coroutines.CoroutineScope
29 import kotlinx.coroutines.Dispatchers
30 import kotlinx.coroutines.async
31 
32 /**
33  * AdId Manager provides APIs for app and ad-SDKs to access advertising ID. The advertising ID is a
34  * unique, per-device, user-resettable ID for advertising. It gives users better controls and
35  * provides developers with a simple, standard system to continue to monetize their apps via
36  * personalized ads (formerly known as interest-based ads). This class can be used by Java clients.
37  */
38 abstract class AdIdManagerFutures internal constructor() {
39     /**
40      * Return the AdId.
41      *
42      * @throws SecurityException if caller is not authorized to call this API.
43      * @throws IllegalStateException if this API is not available.
44      * @throws LimitExceededException if rate limit was reached.
45      */
46     @RequiresPermission(AdServicesPermissions.ACCESS_ADSERVICES_AD_ID)
getAdIdAsyncnull47     abstract fun getAdIdAsync(): ListenableFuture<AdId>
48 
49     private class Api33Ext4JavaImpl(private val mAdIdManager: AdIdManager) : AdIdManagerFutures() {
50         @DoNotInline
51         @RequiresPermission(AdServicesPermissions.ACCESS_ADSERVICES_AD_ID)
52         override fun getAdIdAsync(): ListenableFuture<AdId> {
53             return CoroutineScope(Dispatchers.Default)
54                 .async { mAdIdManager.getAdId() }
55                 .asListenableFuture()
56         }
57     }
58 
59     companion object {
60         /**
61          * Creates [AdIdManagerFutures].
62          *
63          * @return AdIdManagerFutures object. If the device is running an incompatible build, the
64          *   value returned is null.
65          */
66         @JvmStatic
fromnull67         fun from(context: Context): AdIdManagerFutures? {
68             return AdIdManager.obtain(context)?.let { Api33Ext4JavaImpl(it) }
69         }
70     }
71 }
72