summaryrefslogtreecommitdiffstats
path: root/src/org/cyanogenmod/theme/util/ChooserDetailLinearLayout.java
blob: b178d03978ffca02b095791acd451d9d0a4fd275 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
 * Copyright (C) 2014 The CyanogenMod Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.cyanogenmod.theme.util;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;


/**
 * This class will layout its children in a very specific way. It assumes it is a child of a
 * scroll view.
 *
 * The first two children are assumed to be the "theme preview" and the "drawer handle" which will
 * be visible. These children will be restricted in their size since they must be entirely visible.
 * The third child will be shown as the user scrolls down and can take as much space as it needs.
 */
public class ChooserDetailLinearLayout extends LinearLayout {

    // The smaller this value the greater the effect
    public final int PARALLAX_CONSTANT = 2;

    // Child indices
    public final int THEME_PREVIEW_INDEX = 0;
    public final int DRAWER_HANDLE_INDEX = 1;
    public final int DRAWER_CONTENT_INDEX = 2;
    public final int PAGE_INDICATOR_INDEX = 3;

    private ViewPager mPager;

    public ChooserDetailLinearLayout(Context context) {
        super(context);
    }

    public ChooserDetailLinearLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ChooserDetailLinearLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    /**
     * Assumes the heightMeasureSpec will be exact. This exact data will be used
     * to determine the visible page size.
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);

        // Drawer handle is just author/title. Should get whatever space it needs
        View drawerHandle = getChildAt(DRAWER_HANDLE_INDEX);
        measureChildWithMargins(drawerHandle, widthMeasureSpec, 0, heightMeasureSpec, 0);

        // Give the theme preview the remainder
        View themePreview = getChildAt(THEME_PREVIEW_INDEX);
        int usedHeight = drawerHandle.getMeasuredHeight();
        measureChildWithMargins(themePreview, widthMeasureSpec, 0, heightMeasureSpec, usedHeight);

        // Give the drawer content as much as space as it needs.
        View drawerContent = getChildAt(DRAWER_CONTENT_INDEX);
        final MarginLayoutParams lp = (MarginLayoutParams) drawerContent.getLayoutParams();
        int childWidthSpec  = getChildMeasureSpec(widthMeasureSpec, 0, lp.width);
        int childHeightSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
        drawerContent.measure(childWidthSpec, childHeightSpec);

        //Give the page indicator as much space as it needs
        View pageIndicator = getChildAt(PAGE_INDICATOR_INDEX);
        pageIndicator.measure(childWidthSpec, childHeightSpec);

        // Measure ourself
        int width = drawerHandle.getMeasuredWidth();
        int height = themePreview.getMeasuredHeight() +
                        drawerHandle.getMeasuredHeight() + drawerContent.getMeasuredHeight();
        setMeasuredDimension(width, height);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);

        // Place the page indicator above the drawer handle
        View pageIndicator = getChildAt(PAGE_INDICATOR_INDEX);
        int top = getChildAt(DRAWER_HANDLE_INDEX).getTop();
        int height = pageIndicator.getMeasuredHeight();
        pageIndicator.layout(l, top-height, r, top);
    }

    public void onScrollChanged(int l, int t, int oldl, int oldt) {
        // Give a parallax effect to the theme preview by scrolling in the
        // opposite direction of the scrollview
        int yScroll = (int) (-t / PARALLAX_CONSTANT);
        getChildAt(THEME_PREVIEW_INDEX).scrollTo(0, yScroll);
    }

    View getHandle() {
        return getChildAt(DRAWER_HANDLE_INDEX);
    }
}