1 /*
2  * Copyright (C) 2021 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.server.wm.flicker.testapp;
18 
19 import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
20 import static android.view.WindowInsets.Type.ime;
21 import static android.view.WindowInsets.Type.navigationBars;
22 import static android.view.WindowInsets.Type.statusBars;
23 import static android.view.WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM;
24 import static android.view.WindowManager.LayoutParams.FLAG_DIM_BEHIND;
25 import static android.view.WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
26 
27 import android.app.Activity;
28 import android.app.AlertDialog;
29 import android.app.Dialog;
30 import android.graphics.Color;
31 import android.os.Bundle;
32 import android.view.WindowManager;
33 import android.widget.LinearLayout;
34 import android.widget.TextView;
35 
36 public class DialogThemedActivity extends Activity {
37     @Override
onCreate(Bundle savedInstanceState)38     public void onCreate(Bundle savedInstanceState) {
39         super.onCreate(savedInstanceState);
40         setContentView(R.layout.activity_simple);
41         getWindow().addFlags(FLAG_NOT_FOCUSABLE);
42         getWindow().getDecorView().setBackgroundColor(Color.TRANSPARENT);
43         TextView textView = new TextView(this);
44         // Print SystemBars' insets visibility on this window for demonstrating during the test.
45         textView.setId(android.R.id.text1);
46         textView.setText("Insets visibility\n\n");
47         textView.setTextColor(Color.BLACK);
48         LinearLayout layout = new LinearLayout(this);
49         layout.setBackgroundColor(Color.GREEN);
50         layout.addView(textView);
51 
52         // Create a dialog with dialog-themed activity
53         AlertDialog dialog = new AlertDialog.Builder(this)
54                 .setView(layout)
55                 .setTitle("Dialog for test")
56                 .create();
57         final WindowManager.LayoutParams attrs = new WindowManager.LayoutParams(MATCH_PARENT,
58                 MATCH_PARENT);
59         attrs.flags = FLAG_DIM_BEHIND | FLAG_ALT_FOCUSABLE_IM;
60         dialog.getWindow().getDecorView().setLayoutParams(attrs);
61         dialog.setCanceledOnTouchOutside(true);
62         dialog.setOnShowListener(d -> textView.setText(textView.getText()
63                 + "IME: " + isInsetsVisible(dialog, ime()) + "\n"
64                 + "StatusBar: " + isInsetsVisible(dialog, statusBars()) + "\n"
65                 + "NavBar: " + isInsetsVisible(dialog, navigationBars()) + "\n")
66         );
67         dialog.show();
68         dialog.setOnDismissListener((d) -> finish());
69     }
70 
isInsetsVisible(Dialog d, int type)71     private String isInsetsVisible(Dialog d, int type) {
72         return d.getWindow().getDecorView().getRootWindowInsets().isVisible(type) ? "VISIBLE"
73                 : "INVISIBLE";
74     }
75 }
76