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 androidx.test.uiautomator.testapp;
18 
19 import android.app.Activity;
20 import android.graphics.Point;
21 import android.graphics.Rect;
22 import android.os.Bundle;
23 import android.view.View;
24 import android.widget.TextView;
25 
26 import org.jspecify.annotations.Nullable;
27 
28 public class VisibleBoundsTestActivity extends Activity {
29 
30     @Override
onCreate(@ullable Bundle savedInstanceState)31     public void onCreate(@Nullable Bundle savedInstanceState) {
32         super.onCreate(savedInstanceState);
33 
34         setContentView(R.layout.visible_bounds_test_activity);
35 
36         TextView partlyInvisibleRegion = findViewById(R.id.partly_invisible_region);
37         TextView regionInsideScrollable = findViewById(R.id.region_inside_scrollable);
38 
39         partlyInvisibleRegion.setOnClickListener(new OnRegionClick());
40         partlyInvisibleRegion.setOnLongClickListener(new OnRegionLongClick());
41         regionInsideScrollable.setOnClickListener(new OnRegionClick());
42     }
43 
44     static class OnRegionClick implements View.OnClickListener {
45         @Override
onClick(View view)46         public void onClick(View view) {
47             Rect visibleRegion = new Rect();
48             view.getGlobalVisibleRect(visibleRegion);
49             ((TextView) view).setText(visibleRegion.toString());
50         }
51     }
52 
53     static class OnRegionLongClick implements View.OnLongClickListener {
54         @Override
onLongClick(View view)55         public boolean onLongClick(View view) {
56             Rect visibleRegion = new Rect();
57             view.getGlobalVisibleRect(visibleRegion);
58             Point visibleRegionCenter = new Point(visibleRegion.centerX(), visibleRegion.centerY());
59             ((TextView) view).setText(visibleRegionCenter.toString());
60             return true;
61         }
62     }
63 }
64