1/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5 * in compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the License
10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11 * or implied. See the License for the specific language governing permissions and limitations under
12 * the License.
13 */
14package android.support.v17.leanback.widget;
15
16import static android.support.v17.leanback.widget.ItemAlignmentFacet.ITEM_ALIGN_OFFSET_PERCENT_DISABLED;
17import static android.support.v7.widget.RecyclerView.HORIZONTAL;
18
19import android.graphics.Rect;
20import android.support.v17.leanback.widget.GridLayoutManager.LayoutParams;
21import android.view.View;
22import android.view.ViewGroup;
23
24/**
25 * Helper class to handle ItemAlignmentFacet in a grid view.
26 */
27class ItemAlignmentFacetHelper {
28
29    private static Rect sRect = new Rect();
30
31    /**
32     * get alignment position relative to optical left/top of itemView.
33     */
34    static int getAlignmentPosition(View itemView, ItemAlignmentFacet.ItemAlignmentDef facet,
35            int orientation) {
36        LayoutParams p = (LayoutParams) itemView.getLayoutParams();
37        View view = itemView;
38        if (facet.mViewId != 0) {
39            view = itemView.findViewById(facet.mViewId);
40            if (view == null) {
41                view = itemView;
42            }
43        }
44        int alignPos = facet.mOffset;
45        if (orientation == HORIZONTAL) {
46            if (itemView.getLayoutDirection() == View.LAYOUT_DIRECTION_RTL) {
47                alignPos = (view == itemView ? p.getOpticalWidth(view)
48                        : view.getWidth()) - alignPos;
49                if (facet.mOffsetWithPadding) {
50                    if (facet.mOffsetPercent == 0f) {
51                        alignPos -= view.getPaddingRight();
52                    } else if (facet.mOffsetPercent == 100f) {
53                        alignPos += view.getPaddingLeft();
54                    }
55                }
56                if (facet.mOffsetPercent != ITEM_ALIGN_OFFSET_PERCENT_DISABLED) {
57                    alignPos -= (int) (((view == itemView ? p.getOpticalWidth(view)
58                            : view.getWidth()) * facet.mOffsetPercent) / 100f);
59                }
60                if (itemView != view) {
61                    sRect.right = alignPos;
62                    ((ViewGroup) itemView).offsetDescendantRectToMyCoords(view, sRect);
63                    alignPos = sRect.right + p.getOpticalRightInset();
64                }
65            } else  {
66                if (facet.mOffsetWithPadding) {
67                    if (facet.mOffsetPercent == 0f) {
68                        alignPos += view.getPaddingLeft();
69                    } else if (facet.mOffsetPercent == 100f) {
70                        alignPos -= view.getPaddingRight();
71                    }
72                }
73                if (facet.mOffsetPercent != ITEM_ALIGN_OFFSET_PERCENT_DISABLED) {
74                    alignPos += (int) (((view == itemView ? p.getOpticalWidth(view)
75                            : view.getWidth()) * facet.mOffsetPercent) / 100f);
76                }
77                if (itemView != view) {
78                    sRect.left = alignPos;
79                    ((ViewGroup) itemView).offsetDescendantRectToMyCoords(view, sRect);
80                    alignPos = sRect.left - p.getOpticalLeftInset();
81                }
82            }
83        } else {
84            if (facet.mOffsetWithPadding) {
85                if (facet.mOffsetPercent == 0f) {
86                    alignPos += view.getPaddingTop();
87                } else if (facet.mOffsetPercent == 100f) {
88                    alignPos -= view.getPaddingBottom();
89                }
90            }
91            if (facet.mOffsetPercent != ITEM_ALIGN_OFFSET_PERCENT_DISABLED) {
92                alignPos += (int) (((view == itemView ? p.getOpticalHeight(view) : view.getHeight())
93                        * facet.mOffsetPercent) / 100f);
94            }
95            if (itemView != view) {
96                sRect.top = alignPos;
97                ((ViewGroup) itemView).offsetDescendantRectToMyCoords(view, sRect);
98                alignPos = sRect.top - p.getOpticalTopInset();
99            }
100            if (facet.isAlignedToTextViewBaseLine()) {
101                alignPos += view.getBaseline();
102            }
103        }
104        return alignPos;
105    }
106
107}
108