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 initViewnull47 fun initView() { 48 contentView = createView() 49 setVisibility(true) 50 } 51 recreateViewnull52 fun recreateView() { 53 // Inform that the view is going to recreate. 54 onRecreateView(contentView) 55 // Create a new view with the given context. 56 contentView = createView() 57 setVisibility(isVisible) 58 } 59 createViewnull60 private fun createView(): T { 61 @Suppress("UNCHECKED_CAST") 62 val contentView = LayoutInflater.from(context).inflate(viewId, null) as T 63 onViewCreated(contentView) 64 contentView.isFocusable = true 65 return contentView 66 } 67 setVisibilitynull68 open fun setVisibility(isVisible: Boolean) { 69 this.isVisible = isVisible 70 contentView.visibility = if (this.isVisible) FrameLayout.VISIBLE else FrameLayout.GONE 71 } 72 } 73