1 /* 2 * Copyright (C) 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 com.android.systemui.statusbar.phone 18 19 import android.annotation.CallSuper 20 import android.content.Context 21 import android.os.Bundle 22 import android.view.View 23 import android.view.ViewGroup 24 import androidx.activity.OnBackPressedDispatcher 25 import androidx.activity.OnBackPressedDispatcherOwner 26 import androidx.activity.setViewTreeOnBackPressedDispatcherOwner 27 import androidx.lifecycle.Lifecycle 28 import androidx.lifecycle.LifecycleOwner 29 import androidx.lifecycle.LifecycleRegistry 30 import androidx.lifecycle.setViewTreeLifecycleOwner 31 import androidx.savedstate.SavedStateRegistry 32 import androidx.savedstate.SavedStateRegistryController 33 import androidx.savedstate.SavedStateRegistryOwner 34 import androidx.savedstate.setViewTreeSavedStateRegistryOwner 35 import com.android.systemui.animation.DialogTransitionAnimator 36 import com.android.systemui.broadcast.BroadcastDispatcher 37 import com.android.systemui.model.SysUiState 38 39 /** 40 * A [SystemUIDialog] that implements [LifecycleOwner], [SavedStateRegistryOwner] and 41 * [OnBackPressedDispatcherOwner]. 42 * 43 * This class was forked from [androidx.activity.ComponentDialog] and can be used to easily create 44 * SystemUI dialogs without the need to subclass [SystemUIDialog]. You should call 45 * [SystemUIDialogFactory.create] to easily instantiate this class. 46 * 47 * Important: [ComponentSystemUIDialog] should be created and shown on the main thread. 48 * 49 * @see SystemUIDialogFactory.create 50 */ 51 class ComponentSystemUIDialog( 52 context: Context, 53 theme: Int, 54 dismissOnDeviceLock: Boolean, 55 dialogManager: SystemUIDialogManager, 56 sysUiState: SysUiState, 57 broadcastDispatcher: BroadcastDispatcher, 58 dialogTransitionAnimator: DialogTransitionAnimator, 59 delegate: DialogDelegate<SystemUIDialog>, 60 ) : 61 SystemUIDialog( 62 context, 63 theme, 64 dismissOnDeviceLock, 65 dialogManager, 66 sysUiState, 67 broadcastDispatcher, 68 dialogTransitionAnimator, 69 delegate, 70 true, /* shouldAcsdDismissDialog */ 71 ), 72 LifecycleOwner, 73 SavedStateRegistryOwner, 74 OnBackPressedDispatcherOwner { 75 private var _lifecycleRegistry: LifecycleRegistry? = null 76 private val lifecycleRegistry: LifecycleRegistry <lambda>null77 get() = _lifecycleRegistry ?: LifecycleRegistry(this).also { _lifecycleRegistry = it } 78 79 private val savedStateRegistryController: SavedStateRegistryController = 80 SavedStateRegistryController.create(this) 81 override val savedStateRegistry: SavedStateRegistry 82 get() = savedStateRegistryController.savedStateRegistry 83 84 override val lifecycle: Lifecycle 85 get() = lifecycleRegistry 86 onSaveInstanceStatenull87 override fun onSaveInstanceState(): Bundle { 88 val bundle = super.onSaveInstanceState() 89 savedStateRegistryController.performSave(bundle) 90 return bundle 91 } 92 93 @CallSuper onCreatenull94 override fun onCreate(savedInstanceState: Bundle?) { 95 super.onCreate(savedInstanceState) 96 onBackPressedDispatcher.setOnBackInvokedDispatcher(onBackInvokedDispatcher) 97 savedStateRegistryController.performRestore(savedInstanceState) 98 lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_CREATE) 99 } 100 101 @CallSuper startnull102 override fun start() { 103 lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_RESUME) 104 } 105 106 @CallSuper stopnull107 override fun stop() { 108 // This is the closest thing to onDestroy that a Dialog has 109 // TODO(b/296180426): Make SystemUIDialog.onStop() and onStart() open again (annotated with 110 // @CallSuper) and do this *before* calling super.onStop(), like AndroidX ComponentDialog 111 // does. 112 lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_DESTROY) 113 _lifecycleRegistry = null 114 } 115 116 @Suppress("DEPRECATION") <lambda>null117 override val onBackPressedDispatcher = OnBackPressedDispatcher { super.onBackPressed() } 118 119 @CallSuper onBackPressednull120 override fun onBackPressed() { 121 onBackPressedDispatcher.onBackPressed() 122 } 123 setContentViewnull124 override fun setContentView(layoutResID: Int) { 125 initializeViewTreeOwners() 126 super.setContentView(layoutResID) 127 } 128 setContentViewnull129 override fun setContentView(view: View) { 130 initializeViewTreeOwners() 131 super.setContentView(view) 132 } 133 setContentViewnull134 override fun setContentView(view: View, params: ViewGroup.LayoutParams?) { 135 initializeViewTreeOwners() 136 super.setContentView(view, params) 137 } 138 addContentViewnull139 override fun addContentView(view: View, params: ViewGroup.LayoutParams?) { 140 initializeViewTreeOwners() 141 super.addContentView(view, params) 142 } 143 144 /** 145 * Sets the view tree owners before setting the content view so that the inflation process and 146 * attach listeners will see them already present. 147 */ 148 @CallSuper initializeViewTreeOwnersnull149 open fun initializeViewTreeOwners() { 150 window!!.decorView.setViewTreeLifecycleOwner(this) 151 window!!.decorView.setViewTreeOnBackPressedDispatcherOwner(this) 152 window!!.decorView.setViewTreeSavedStateRegistryOwner(this) 153 } 154 } 155