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.window.layout.adapter.extensions
18 
19 import android.app.Activity
20 import androidx.annotation.UiContext
21 import androidx.window.core.ConsumerAdapter
22 import androidx.window.core.ExtensionsUtil
23 import androidx.window.extensions.layout.WindowLayoutComponent
24 import androidx.window.layout.adapter.WindowBackend
25 
26 /**
27  * A wrapper around [WindowLayoutComponent] that ensures
28  * [WindowLayoutComponent.addWindowLayoutInfoListener] is called at most once per context while
29  * there are active listeners. Context has to be an [Activity] or a [UiContext] created with
30  * [Context#createWindowContext] or InputMethodService.
31  */
32 internal class ExtensionWindowBackend(private val backend: WindowBackend) :
33     WindowBackend by backend {
34 
35     companion object {
36 
37         /**
38          * Returns a new instance that is made to handle the vendor API level on the device. There
39          * should be a single instance per app and this is handled in
40          * [androidx.window.layout.WindowInfoTracker.getOrCreate].
41          */
newInstancenull42         fun newInstance(component: WindowLayoutComponent, adapter: ConsumerAdapter): WindowBackend {
43             val safeVendorApiLevel = ExtensionsUtil.safeVendorApiLevel
44             return when {
45                 safeVendorApiLevel >= 9 -> ExtensionWindowBackendApi9(component, adapter)
46                 safeVendorApiLevel >= 6 -> ExtensionWindowBackendApi6(component, adapter)
47                 safeVendorApiLevel >= 2 -> ExtensionWindowBackendApi2(component, adapter)
48                 safeVendorApiLevel == 1 -> ExtensionWindowBackendApi1(component, adapter)
49                 else -> ExtensionWindowBackendApi0()
50             }
51         }
52     }
53 }
54