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