1 /*
2  * Copyright (C) 2015 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 android.support.design.widget;
18 
19 import android.graphics.Rect;
20 import android.os.Build;
21 import android.view.View;
22 import android.view.ViewGroup;
23 
24 class ViewGroupUtils {
25 
26     private interface ViewGroupUtilsImpl {
offsetDescendantRect(ViewGroup parent, View child, Rect rect)27         void offsetDescendantRect(ViewGroup parent, View child, Rect rect);
28     }
29 
30     private static class ViewGroupUtilsImplBase implements ViewGroupUtilsImpl {
31         @Override
offsetDescendantRect(ViewGroup parent, View child, Rect rect)32         public void offsetDescendantRect(ViewGroup parent, View child, Rect rect) {
33             parent.offsetDescendantRectToMyCoords(child, rect);
34             // View#offsetDescendantRectToMyCoords includes scroll offsets of the last child.
35             // We need to reverse it here so that we get the rect of the view itself rather
36             // than its content.
37             rect.offset(child.getScrollX(), child.getScrollY());
38         }
39     }
40 
41     private static class ViewGroupUtilsImplHoneycomb implements ViewGroupUtilsImpl {
42         @Override
offsetDescendantRect(ViewGroup parent, View child, Rect rect)43         public void offsetDescendantRect(ViewGroup parent, View child, Rect rect) {
44             ViewGroupUtilsHoneycomb.offsetDescendantRect(parent, child, rect);
45         }
46     }
47 
48     private static final ViewGroupUtilsImpl IMPL;
49 
50     static {
51         final int version = Build.VERSION.SDK_INT;
52         if (version >= 11) {
53             IMPL = new ViewGroupUtilsImplHoneycomb();
54         } else {
55             IMPL = new ViewGroupUtilsImplBase();
56         }
57     }
58 
59     /**
60      * This is a port of the common
61      * {@link ViewGroup#offsetDescendantRectToMyCoords(android.view.View, android.graphics.Rect)}
62      * from the framework, but adapted to take transformations into account. The result
63      * will be the bounding rect of the real transformed rect.
64      *
65      * @param descendant view defining the original coordinate system of rect
66      * @param rect (in/out) the rect to offset from descendant to this view's coordinate system
67      */
offsetDescendantRect(ViewGroup parent, View descendant, Rect rect)68     static void offsetDescendantRect(ViewGroup parent, View descendant, Rect rect) {
69         IMPL.offsetDescendantRect(parent, descendant, rect);
70     }
71 
72     /**
73      * Retrieve the transformed bounding rect of an arbitrary descendant view.
74      * This does not need to be a direct child.
75      *
76      * @param descendant descendant view to reference
77      * @param out rect to set to the bounds of the descendant view
78      */
getDescendantRect(ViewGroup parent, View descendant, Rect out)79     static void getDescendantRect(ViewGroup parent, View descendant, Rect out) {
80         out.set(0, 0, descendant.getWidth(), descendant.getHeight());
81         offsetDescendantRect(parent, descendant, out);
82     }
83 
84 }
85