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.app.AlertDialog
20 import android.content.Context
21 import android.content.res.Configuration
22 import android.os.Bundle
23 import android.view.ViewRootImpl
24 import android.view.ViewRootImpl.ConfigChangedCallback
25 import androidx.annotation.StyleRes
26 
27 /**
28  * Implementation of [AlertDialog] that takes as parameter a [DialogDelegate].
29  *
30  * Can be used when composition is preferred over inheritance.
31  */
32 class AlertDialogWithDelegate(
33     context: Context,
34     @StyleRes theme: Int,
35     private val delegate: DialogDelegate<AlertDialog>
36 ) : AlertDialog(context, theme), ConfigChangedCallback {
37 
onCreatenull38     override fun onCreate(savedInstanceState: Bundle?) {
39         delegate.beforeCreate(dialog = this, savedInstanceState)
40         super.onCreate(savedInstanceState)
41         delegate.onCreate(dialog = this, savedInstanceState)
42     }
43 
onConfigurationChangednull44     override fun onConfigurationChanged(configuration: Configuration) {
45         delegate.onConfigurationChanged(dialog = this, configuration)
46     }
47 
onStartnull48     override fun onStart() {
49         super.onStart()
50         ViewRootImpl.addConfigCallback(this)
51         delegate.onStart(dialog = this)
52     }
53 
onStopnull54     override fun onStop() {
55         super.onStop()
56         ViewRootImpl.removeConfigCallback(this)
57         delegate.onStop(dialog = this)
58     }
59 
onWindowFocusChangednull60     override fun onWindowFocusChanged(hasFocus: Boolean) {
61         super.onWindowFocusChanged(hasFocus)
62         delegate.onWindowFocusChanged(dialog = this, hasFocus)
63     }
64 }
65