1 package com.android.cts.verifier.notifications;
2 
3 import static android.view.View.GONE;
4 import static android.view.View.VISIBLE;
5 
6 import android.app.Activity;
7 import android.content.Intent;
8 import android.graphics.Insets;
9 import android.graphics.Rect;
10 import android.os.Bundle;
11 import android.view.View;
12 import android.view.ViewTreeObserver;
13 import android.view.WindowManager;
14 import android.view.WindowMetrics;
15 import android.widget.EditText;
16 import android.widget.TextView;
17 
18 import com.android.cts.verifier.R;
19 
20 /**
21  * Used in BubblesVerifierActivity as the contents of the bubble.
22  */
23 public class BubbleActivity extends Activity {
24     public static final String EXTRA_TEST_NAME = "test_id";
25     public static final String TEST_MIN_HEIGHT = "minHeight";
26     public static final String TEST_MAX_HEIGHT = "maxHeight";
27     public static final String EXTRA_INSETS = "insets";
28 
29     private View mRoot;
30     private TextView mTitle;
31     private TextView mTestMessage;
32     private EditText mEditText;
33 
34     private String mTestName = null;
35     private Insets mInsets;
36     private boolean mIsLargeScreen;
37 
38     private ViewTreeObserver.OnGlobalLayoutListener mListener =
39             new ViewTreeObserver.OnGlobalLayoutListener() {
40                 @Override
41                 public void onGlobalLayout() {
42                     checkHeight();
43                     mRoot.getViewTreeObserver().removeOnGlobalLayoutListener(this);
44                 }
45             };
46 
47     @Override
onCreate(Bundle savedState)48     public void onCreate(Bundle savedState) {
49         super.onCreate(savedState);
50         setContentView(R.layout.bubble_activity);
51         mRoot = findViewById(R.id.layout_root);
52         mTitle = findViewById(R.id.title_text);
53         mTestMessage = findViewById(R.id.test_message);
54         mEditText = findViewById(R.id.edit_text);
55 
56         mIsLargeScreen = getResources().getConfiguration().smallestScreenWidthDp >= 600;
57 
58         getActionBar().hide();
59         setUpTestForExtras();
60     }
61 
setUpTestForExtras()62     private void setUpTestForExtras() {
63         Intent i = getIntent();
64         mTestName = i.getStringExtra(EXTRA_TEST_NAME);
65         mInsets = i.getParcelableExtra(EXTRA_INSETS);
66         if (mTestName == null) {
67             mTestMessage.setVisibility(GONE);
68             return;
69         }
70         if (TEST_MIN_HEIGHT.equals(mTestName)
71                 || TEST_MAX_HEIGHT.equals(mTestName)) {
72             mTestMessage.setVisibility(VISIBLE);
73             mTitle.setVisibility(GONE);
74             mEditText.setVisibility(GONE);
75             ViewTreeObserver observer = mRoot.getViewTreeObserver();
76             observer.addOnGlobalLayoutListener(mListener);
77         }
78     }
79 
checkHeight()80     private void checkHeight() {
81         if (TEST_MIN_HEIGHT.equals(mTestName)) {
82             // Height should be equal or larger than 180dp
83             int minHeight = getResources().getDimensionPixelSize(
84                     R.dimen.bubble_expanded_view_min_height);
85             if (mRoot.getHeight() < minHeight) {
86                 mTestMessage.setText("Test failed -- height too small! bubble expanded view is: "
87                         + mRoot.getHeight() + " vs desired minimum height:" + minHeight);
88             } else {
89                 mTestMessage.setText("Test Passed!");
90             }
91         } else if (TEST_MAX_HEIGHT.equals(mTestName)) {
92             // These are max size which won't include insets.
93             WindowMetrics windowMetricsMax =
94                     getSystemService(WindowManager.class).getMaximumWindowMetrics();
95             // These show the metrics for the bubble activity / the bounds will be that of TaskView
96             WindowMetrics bubbleWindowMetrics =
97                     getSystemService(WindowManager.class).getCurrentWindowMetrics();
98             if (mIsLargeScreen) {
99                 Rect bounds = windowMetricsMax.getBounds();
100                 final float percentOfScreen = Math.min(bounds.height(), bounds.width()) * 0.70f;
101                 if (bubbleWindowMetrics.getBounds().height() <= percentOfScreen) {
102                     mTestMessage.setText("Test failed --"
103                             + " the bubble expanded view is too small, it is: "
104                             + bubbleWindowMetrics.getBounds().height()
105                             + " and minimum is: " + percentOfScreen);
106                 } else {
107                     mTestMessage.setText("Test Passed!");
108                 }
109             } else {
110                 // Bottom of bubble view should be close to the bottom inset. There needs to be some
111                 // padding for the manage button so we allow some flux (200dp).
112                 int maxHeightBuffer = getResources().getDimensionPixelSize(
113                         R.dimen.bubble_expanded_view_max_height_buffer);
114                 int bottomInset = windowMetricsMax.getBounds().bottom - mInsets.bottom;
115                 int diff = bottomInset - bubbleWindowMetrics.getBounds().bottom;
116                 if (diff > maxHeightBuffer) {
117                     mTestMessage.setText("Test failed -- bottom of the bubble expanded view "
118                             + "isn't close enough to the bottom inset: " + diff);
119                 } else {
120                     mTestMessage.setText("Test Passed!");
121                 }
122             }
123         }
124     }
125 }