1 /*
2  * Copyright (C) 2018 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.documentsui.ui;
18 
19 import android.graphics.Rect;
20 import android.view.MotionEvent;
21 import android.view.View;
22 
23 /**
24  * A utility class for working with Views.
25  */
26 public final class Views {
27 
Views()28     private Views() {}
29 
30     /**
31      * Return whether the event is in the view's region
32      * @param event the motion event
33      * @param view the view to check the selection region
34      * @return True, if the event is in the region. Otherwise, return false.
35      */
isEventOver(MotionEvent event, View view)36     public static boolean isEventOver(MotionEvent event, View view) {
37         if (view == null || event == null || !view.isAttachedToWindow()) {
38             return false;
39         }
40 
41         final int[] coord = new int[2];
42         view.getLocationOnScreen(coord);
43 
44         final Rect viewRect = new Rect(coord[0], coord[1], coord[0] + view.getMeasuredWidth(),
45                 coord[1] + view.getMeasuredHeight());
46 
47         return viewRect.contains((int) event.getRawX(), (int) event.getRawY());
48     }
49 }
50