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 
17 package com.android.wm.shell.compatui;
18 
19 import android.annotation.Nullable;
20 import android.content.Context;
21 import android.graphics.drawable.Drawable;
22 import android.util.AttributeSet;
23 import android.view.View;
24 import android.widget.CheckBox;
25 import android.widget.TextView;
26 
27 import androidx.constraintlayout.widget.ConstraintLayout;
28 
29 import com.android.wm.shell.R;
30 
31 import java.util.function.Consumer;
32 
33 /**
34  * Container for a SCM restart confirmation dialog and background dim.
35  */
36 public class RestartDialogLayout extends ConstraintLayout implements DialogContainerSupplier {
37 
38     private View mDialogContainer;
39     private TextView mDialogTitle;
40     private Drawable mBackgroundDim;
41 
RestartDialogLayout(Context context)42     public RestartDialogLayout(Context context) {
43         this(context, null);
44     }
45 
RestartDialogLayout(Context context, AttributeSet attrs)46     public RestartDialogLayout(Context context, AttributeSet attrs) {
47         this(context, attrs, 0);
48     }
49 
RestartDialogLayout(Context context, AttributeSet attrs, int defStyleAttr)50     public RestartDialogLayout(Context context, AttributeSet attrs, int defStyleAttr) {
51         this(context, attrs, defStyleAttr, 0);
52     }
53 
RestartDialogLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)54     public RestartDialogLayout(Context context, AttributeSet attrs, int defStyleAttr,
55             int defStyleRes) {
56         super(context, attrs, defStyleAttr, defStyleRes);
57     }
58 
59     @Override
getDialogContainerView()60     public View getDialogContainerView() {
61         return mDialogContainer;
62     }
63 
getDialogTitle()64     TextView getDialogTitle() {
65         return mDialogTitle;
66     }
67 
68     @Override
getBackgroundDimDrawable()69     public Drawable getBackgroundDimDrawable() {
70         return mBackgroundDim;
71     }
72 
73     /**
74      * Register a callback for the dismiss button and background dim.
75      *
76      * @param callback The callback to register or null if all on click listeners should be removed.
77      */
setDismissOnClickListener(@ullable Runnable callback)78     void setDismissOnClickListener(@Nullable Runnable callback) {
79         final OnClickListener listener = callback == null ? null : view -> callback.run();
80         findViewById(R.id.letterbox_restart_dialog_dismiss_button).setOnClickListener(listener);
81     }
82 
83     /**
84      * Register a callback for the restart button
85      *
86      * @param callback The callback to register or null if all on click listeners should be removed.
87      */
setRestartOnClickListener(@ullable Consumer<Boolean> callback)88     void setRestartOnClickListener(@Nullable Consumer<Boolean> callback) {
89         final CheckBox dontShowAgainCheckbox = findViewById(R.id.letterbox_restart_dialog_checkbox);
90         final OnClickListener listener = callback == null ? null : view -> callback.accept(
91                 dontShowAgainCheckbox.isChecked());
92         findViewById(R.id.letterbox_restart_dialog_restart_button).setOnClickListener(listener);
93     }
94 
95     @Override
onFinishInflate()96     protected void onFinishInflate() {
97         super.onFinishInflate();
98         final View checkboxContainer = findViewById(
99                 R.id.letterbox_restart_dialog_checkbox_container);
100         final CheckBox checkbox = findViewById(R.id.letterbox_restart_dialog_checkbox);
101         mDialogContainer = findViewById(R.id.letterbox_restart_dialog_container);
102         mDialogTitle = findViewById(R.id.letterbox_restart_dialog_title);
103         mBackgroundDim = getBackground().mutate();
104         // Set the alpha of the background dim to 0 for enter animation.
105         mBackgroundDim.setAlpha(0);
106         // We add a no-op on-click listener to the dialog container so that clicks on it won't
107         // propagate to the listener of the layout (which represents the background dim).
108         mDialogContainer.setOnClickListener(view -> {});
109         checkboxContainer.setOnClickListener(view -> checkbox.performClick());
110     }
111 }
112