1 /*
2  * Copyright 2023 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.activity.provider
18 
19 import android.os.Bundle
20 import android.os.IBinder
21 import androidx.privacysandbox.activity.core.ISdkActivityLauncher
22 import androidx.privacysandbox.activity.core.ISdkActivityLauncherCallback
23 import androidx.privacysandbox.activity.core.ProtocolConstants.SDK_ACTIVITY_LAUNCHER_BINDER_KEY
24 import androidx.privacysandbox.activity.core.SdkActivityLauncher
25 import kotlin.coroutines.resume
26 import kotlin.coroutines.resumeWithException
27 import kotlinx.coroutines.suspendCancellableCoroutine
28 
29 object SdkActivityLauncherFactory {
30 
31     /**
32      * Creates a [SdkActivityLauncher] using the given [launcherInfo] Bundle.
33      *
34      * You can create such a Bundle by calling
35      * [toLauncherInfo][androidx.privacysandbox.activity.client.toLauncherInfo]. A [launcherInfo] is
36      * expected to have a valid SdkActivityLauncher Binder with `"sdkActivityLauncherBinderKey"` for
37      * a key, [IllegalArgumentException] is thrown otherwise.
38      */
39     @JvmStatic
fromLauncherInfonull40     fun fromLauncherInfo(launcherInfo: Bundle): SdkActivityLauncher {
41         val remote: ISdkActivityLauncher? =
42             ISdkActivityLauncher.Stub.asInterface(
43                 launcherInfo.getBinder(SDK_ACTIVITY_LAUNCHER_BINDER_KEY)
44             )
45         requireNotNull(remote) { "Invalid SdkActivityLauncher info bundle." }
46         return SdkActivityLauncherProxy(remote)
47     }
48 
49     private class SdkActivityLauncherProxy(private val remote: ISdkActivityLauncher) :
50         SdkActivityLauncher {
launchSdkActivitynull51         override suspend fun launchSdkActivity(sdkActivityHandlerToken: IBinder): Boolean =
52             suspendCancellableCoroutine {
53                 remote.launchSdkActivity(
54                     sdkActivityHandlerToken,
55                     object : ISdkActivityLauncherCallback.Stub() {
56                         override fun onLaunchAccepted(sdkActivityHandlerToken: IBinder?) {
57                             it.resume(true)
58                         }
59 
60                         override fun onLaunchRejected(sdkActivityHandlerToken: IBinder?) {
61                             it.resume(false)
62                         }
63 
64                         override fun onLaunchError(message: String?) {
65                             it.resumeWithException(RuntimeException(message))
66                         }
67                     }
68                 )
69             }
70     }
71 }
72