1 /*
2  * Copyright 2024 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.sdkruntime.testsdk
18 
19 import android.content.Context
20 import android.os.Binder
21 import android.os.Bundle
22 import androidx.privacysandbox.sdkruntime.core.LoadSdkCompatException
23 import androidx.privacysandbox.sdkruntime.core.SandboxedSdkCompat
24 import androidx.privacysandbox.sdkruntime.core.SandboxedSdkProviderCompat
25 
26 @Suppress("unused") // Reflection usage from tests in privacysandbox:sdkruntime:sdkruntime-client
27 class CompatProvider : SandboxedSdkProviderCompat() {
28     @JvmField var onLoadSdkBinder: Binder? = null
29 
30     @JvmField var lastOnLoadSdkParams: Bundle? = null
31 
32     @JvmField var isBeforeUnloadSdkCalled = false
33 
34     @Throws(LoadSdkCompatException::class)
onLoadSdknull35     override fun onLoadSdk(params: Bundle): SandboxedSdkCompat {
36         val result = SdkImpl(context!!)
37         onLoadSdkBinder = result
38 
39         lastOnLoadSdkParams = params
40         if (params.getBoolean("needFail", false)) {
41             throw LoadSdkCompatException(RuntimeException(), params)
42         }
43         return SandboxedSdkCompat(result)
44     }
45 
beforeUnloadSdknull46     override fun beforeUnloadSdk() {
47         isBeforeUnloadSdkCalled = true
48     }
49 
50     internal class SdkImpl(
51         @Suppress("MemberVisibilityCanBePrivate") // Reflection usage from LocalSdkTestUtils
52         val context: Context
53     ) : Binder()
54 }
55