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.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.TextView;
25 
26 import androidx.constraintlayout.widget.ConstraintLayout;
27 
28 import com.android.wm.shell.R;
29 
30 /**
31  * Container for Letterbox Education Dialog and background dim.
32  *
33  * <p>This layout should fill the entire task and the background around the dialog acts as the
34  * background dim which dismisses the dialog when clicked.
35  */
36 class LetterboxEduDialogLayout extends ConstraintLayout implements DialogContainerSupplier {
37 
38     private View mDialogContainer;
39     private TextView mDialogTitle;
40     private Drawable mBackgroundDim;
41 
LetterboxEduDialogLayout(Context context)42     public LetterboxEduDialogLayout(Context context) {
43         this(context, null);
44     }
45 
LetterboxEduDialogLayout(Context context, AttributeSet attrs)46     public LetterboxEduDialogLayout(Context context, AttributeSet attrs) {
47         this(context, attrs, 0);
48     }
49 
LetterboxEduDialogLayout(Context context, AttributeSet attrs, int defStyleAttr)50     public LetterboxEduDialogLayout(Context context, AttributeSet attrs, int defStyleAttr) {
51         this(context, attrs, defStyleAttr, 0);
52     }
53 
LetterboxEduDialogLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)54     public LetterboxEduDialogLayout(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 
64     @Override
getBackgroundDimDrawable()65     public Drawable getBackgroundDimDrawable() {
66         return mBackgroundDim;
67     }
68 
getDialogTitle()69     TextView getDialogTitle() {
70         return mDialogTitle;
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_education_dialog_dismiss_button).setOnClickListener(listener);
81         // Clicks on the background dim should also dismiss the dialog.
82         setOnClickListener(listener);
83         // We add a no-op on-click listener to the dialog container so that clicks on it won't
84         // propagate to the listener of the layout (which represents the background dim).
85         mDialogContainer.setOnClickListener(callback == null ? null : view -> {});
86     }
87 
88     @Override
onFinishInflate()89     protected void onFinishInflate() {
90         super.onFinishInflate();
91         mDialogContainer = findViewById(R.id.letterbox_education_dialog_container);
92         mDialogTitle = findViewById(R.id.letterbox_education_dialog_title);
93         mBackgroundDim = getBackground().mutate();
94         // Set the alpha of the background dim to 0 for enter animation.
95         mBackgroundDim.setAlpha(0);
96     }
97 }
98