1/*
2 * Copyright (C) 2014 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 android.graphics.Rect;
17import android.support.v4.view.ViewCompat;
18import android.view.View;
19import android.view.View.MeasureSpec;
20import android.view.ViewGroup;
21import android.view.ViewGroup.MarginLayoutParams;
22
23/**
24 * A helper class for showing a hover card view below a {@link HorizontalGridView}.  The hover card
25 * is aligned to the starting edge of the selected child view.  If there is no space when scrolling
26 * to the end, the ending edge of the hover card will be aligned to the ending edge of the parent
27 * view, excluding padding.
28 */
29public final class HorizontalHoverCardSwitcher extends PresenterSwitcher {
30    // left and right of selected card view
31    int mCardLeft, mCardRight;
32
33    private int[] mTmpOffsets = new int[2];
34    private Rect mTmpRect = new Rect();
35
36    @Override
37    protected void insertView(View view) {
38        // append hovercard to the end of container
39        getParentViewGroup().addView(view);
40    }
41
42    @Override
43    protected void onViewSelected(View view) {
44        int rightLimit = getParentViewGroup().getWidth() - getParentViewGroup().getPaddingRight();
45        int leftLimit = getParentViewGroup().getPaddingLeft();
46        // measure the hover card width; if it's too large, align hover card
47        // end edge with row view's end edge, otherwise align start edges.
48        view.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
49        MarginLayoutParams params = (MarginLayoutParams) view.getLayoutParams();
50        boolean isRtl = ViewCompat.getLayoutDirection(view) == View.LAYOUT_DIRECTION_RTL;
51        if (!isRtl && mCardLeft + view.getMeasuredWidth() > rightLimit) {
52            params.leftMargin = rightLimit  - view.getMeasuredWidth();
53        } else if (isRtl && mCardLeft < leftLimit) {
54            params.leftMargin = leftLimit;
55        } else if (isRtl) {
56            params.leftMargin = mCardRight - view.getMeasuredWidth();
57        } else {
58            params.leftMargin = mCardLeft;
59        }
60        view.requestLayout();
61    }
62
63    /**
64     * Select a childView inside a grid view and create/bind a corresponding hover card view
65     * for the object.
66     */
67    public void select(HorizontalGridView gridView, View childView, Object object) {
68        ViewGroup parent = getParentViewGroup();
69        gridView.getViewSelectedOffsets(childView, mTmpOffsets);
70        mTmpRect.set(0, 0, childView.getWidth(), childView.getHeight());
71        parent.offsetDescendantRectToMyCoords(childView, mTmpRect);
72        mCardLeft = mTmpRect.left - mTmpOffsets[0];
73        mCardRight = mTmpRect.right - mTmpOffsets[0];
74        select(object);
75    }
76
77}
78