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 com.android.systemui.util.view
18 
19 import android.graphics.Rect
20 import android.view.View
21 import com.android.systemui.dagger.SysUISingleton
22 import javax.inject.Inject
23 
24 /**
25  * A class with generic view utility methods.
26  *
27  * Doesn't use static methods so that it can be easily mocked out in tests.
28  */
29 @SysUISingleton
30 class ViewUtil @Inject constructor() {
31     /**
32      * Returns true if the given (x, y) point (in screen coordinates) is within the status bar
33      * view's range and false otherwise.
34      */
touchIsWithinViewnull35     fun touchIsWithinView(view: View, x: Float, y: Float): Boolean {
36         val left = view.locationOnScreen[0]
37         val top = view.locationOnScreen[1]
38         return left <= x &&
39                 x <= left + view.width &&
40                 top <= y &&
41                 y <= top + view.height
42     }
43 
44     /**
45      * Sets [outRect] to be the view's location within its window.
46      */
setRectToViewWindowLocationnull47     fun setRectToViewWindowLocation(view: View, outRect: Rect) {
48         val locInWindow = IntArray(2)
49         view.getLocationInWindow(locInWindow)
50 
51         val x = locInWindow[0]
52         val y = locInWindow[1]
53 
54         outRect.set(
55             x,
56             y,
57             x + view.width,
58             y + view.height,
59         )
60     }
61 }
62