1 /*
2  * Copyright (C) 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 package com.android.wallpaper.widget.floatingsheetcontent
17 
18 import android.content.Context
19 import android.view.LayoutInflater
20 import android.view.View
21 import android.widget.FrameLayout
22 import androidx.annotation.LayoutRes
23 
24 /**
25  * Object to host content view for floating sheet to display.
26  *
27  * The view would be created in the constructor.
28  *
29  * @param <T> the floating sheet content type </T>
30  *
31  * TODO: refactoring FloatingSheetContent b/258468645
32  */
33 abstract class FloatingSheetContent<T : View>(private val context: Context) {
34 
35     lateinit var contentView: T
36     private var isVisible = false
37 
38     /** Gets the view id to inflate. */
39     @get:LayoutRes abstract val viewId: Int
40 
41     /** Gets called when the content view is created. */
onViewCreatednull42     abstract fun onViewCreated(view: T)
43 
44     /** Gets called when the current content view is going to recreate. */
45     open fun onRecreateView(oldView: T) {}
46 
collapsenull47     open fun collapse() {}
48 
initViewnull49     fun initView() {
50         contentView = createView()
51         setVisibility(true)
52     }
53 
recreateViewnull54     fun recreateView() {
55         // Inform that the view is going to recreate.
56         onRecreateView(contentView)
57         // Create a new view with the given context.
58         contentView = createView()
59         setVisibility(isVisible)
60     }
61 
createViewnull62     private fun createView(): T {
63         @Suppress("UNCHECKED_CAST")
64         val contentView = LayoutInflater.from(context).inflate(viewId, null) as T
65         onViewCreated(contentView)
66         contentView.isFocusable = true
67         return contentView
68     }
69 
setVisibilitynull70     open fun setVisibility(isVisible: Boolean) {
71         this.isVisible = isVisible
72         contentView.visibility = if (this.isVisible) FrameLayout.VISIBLE else FrameLayout.GONE
73     }
74 }
75