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.topics 18 19 import android.adservices.common.AdServicesPermissions 20 import android.content.Context 21 import androidx.annotation.DoNotInline 22 import androidx.annotation.RequiresPermission 23 import androidx.privacysandbox.ads.adservices.java.internal.asListenableFuture 24 import androidx.privacysandbox.ads.adservices.topics.GetTopicsRequest 25 import androidx.privacysandbox.ads.adservices.topics.GetTopicsResponse 26 import androidx.privacysandbox.ads.adservices.topics.TopicsManager 27 import androidx.privacysandbox.ads.adservices.topics.TopicsManager.Companion.obtain 28 import com.google.common.util.concurrent.ListenableFuture 29 import kotlinx.coroutines.CoroutineScope 30 import kotlinx.coroutines.Dispatchers 31 import kotlinx.coroutines.async 32 33 /** 34 * This provides APIs for App and Ad-Sdks to get the user interest topics in a privacy preserving 35 * way. This class can be used by Java clients. 36 */ 37 abstract class TopicsManagerFutures internal constructor() { 38 /** 39 * Returns the topics. 40 * 41 * @param request The GetTopicsRequest for obtaining Topics. 42 * @return ListenableFuture to get the Topics response. 43 */ 44 @RequiresPermission(AdServicesPermissions.ACCESS_ADSERVICES_TOPICS) getTopicsAsyncnull45 abstract fun getTopicsAsync(request: GetTopicsRequest): ListenableFuture<GetTopicsResponse> 46 47 private class CommonApiJavaImpl(private val mTopicsManager: TopicsManager) : 48 TopicsManagerFutures() { 49 @DoNotInline 50 @RequiresPermission(AdServicesPermissions.ACCESS_ADSERVICES_TOPICS) 51 override fun getTopicsAsync( 52 request: GetTopicsRequest 53 ): ListenableFuture<GetTopicsResponse> { 54 return CoroutineScope(Dispatchers.Main) 55 .async { mTopicsManager.getTopics(request) } 56 .asListenableFuture() 57 } 58 } 59 60 companion object { 61 /** 62 * Creates [TopicsManagerFutures]. 63 * 64 * @return TopicsManagerFutures object. If the device is running an incompatible build, the 65 * value returned is null. 66 */ 67 @JvmStatic fromnull68 fun from(context: Context): TopicsManagerFutures? { 69 return obtain(context)?.let { CommonApiJavaImpl(it) } 70 } 71 } 72 } 73