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 android.view.View
23 import androidx.privacysandbox.sdkruntime.core.LoadSdkCompatException
24 import androidx.privacysandbox.sdkruntime.core.SandboxedSdkCompat
25 import androidx.privacysandbox.sdkruntime.core.SandboxedSdkProviderCompat
26 
27 @Suppress("unused") // Reflection usage from tests in privacysandbox:sdkruntime:sdkruntime-client
28 class CompatProvider : SandboxedSdkProviderCompat() {
29     @JvmField var onLoadSdkBinder: Binder? = null
30 
31     @JvmField var lastOnLoadSdkParams: Bundle? = null
32 
33     @JvmField var isBeforeUnloadSdkCalled = false
34 
35     @Throws(LoadSdkCompatException::class)
onLoadSdknull36     override fun onLoadSdk(params: Bundle): SandboxedSdkCompat {
37         val result = SdkImpl(context!!)
38         onLoadSdkBinder = result
39 
40         lastOnLoadSdkParams = params
41         if (params.getBoolean("needFail", false)) {
42             throw LoadSdkCompatException(RuntimeException(), params)
43         }
44         return SandboxedSdkCompat(result)
45     }
46 
beforeUnloadSdknull47     override fun beforeUnloadSdk() {
48         isBeforeUnloadSdkCalled = true
49     }
50 
51     @Deprecated("Deprecated in future version. Deprecating here to temporary pass max_dep_versions")
getViewnull52     override fun getView(windowContext: Context, params: Bundle, width: Int, height: Int): View {
53         return View(windowContext)
54     }
55 
56     internal class SdkImpl(
57         @Suppress("MemberVisibilityCanBePrivate") // Reflection usage from LocalSdkTestUtils
58         val context: Context
59     ) : Binder()
60 }
61