summaryrefslogtreecommitdiffstats
path: root/src/org/cyanogenmod/theme/chooserv2/ThemeFragment.java
blob: 7be0a0d083ea71e05f9893c05c5af779f0d62f97 (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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
 * 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.chooserv2;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.animation.DecelerateInterpolator;
import android.widget.ScrollView;

import org.cyanogenmod.theme.chooser.R;

import java.util.ArrayList;
import java.util.List;

public class ThemeFragment extends Fragment {
    public static final int ANIMATE_START_DELAY = 75;
    public static final int ANIMATE_DURATION = 500;
    public static final int ANIMATE_INTERPOLATE_FACTOR = 3;
    private ScrollView mScrollView;
    private ViewGroup mScrollContent;

    static ThemeFragment newInstance(String pkgName) {
        ThemeFragment f = new ThemeFragment();
        Bundle args = new Bundle();
        args.putString("pkgName", pkgName);
        f.setArguments(args);
        return f;
    }

    /**
     * When creating, retrieve this instance's number from its arguments.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    /**
     * The Fragment's UI is just a simple text view showing its
     * instance number.
     */
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        ViewPager.LayoutParams params = new ViewPager.LayoutParams();
        params.width = ViewPager.LayoutParams.MATCH_PARENT;
        params.height = ViewPager.LayoutParams.MATCH_PARENT;

        View v = inflater.inflate(R.layout.v2_fragment_pager_list, container, false);
        v.setLayoutParams(params);

        mScrollView = (ScrollView) v.findViewById(android.R.id.list);
        mScrollContent = (ViewGroup) mScrollView.getChildAt(0);

        return v;
    }

    public void expand() {
        for (int i = 0; i < mScrollContent.getChildCount(); i++) {
            View child = mScrollContent.getChildAt(i);
            ViewGroup.LayoutParams layout = child.getLayoutParams();
            layout.height = 400;
            child.setLayoutParams(layout);
        }
        mScrollContent.requestLayout();
        animateChildren();
    }


    public void collapse() {
        for (int i = 0; i < mScrollContent.getChildCount(); i++) {
            View child = mScrollContent.getChildAt(i);
            ViewGroup.LayoutParams layout = child.getLayoutParams();
            layout.height = ViewGroup.LayoutParams.WRAP_CONTENT;
            child.setLayoutParams(layout);
        }
        mScrollContent.requestLayout();

        animateChildren();
    }

    // This will animate the children's vertical value between the existing and
    // new layout changes
    private void animateChildren() {
        final ViewGroup root = (ViewGroup) getActivity().getWindow()
                .getDecorView().findViewById(android.R.id.content);

        // Get the child's current location
        final List<Float> prevYs = new ArrayList<Float>();
        for (int i = 0; i < mScrollContent.getChildCount(); i++) {
            final View v = mScrollContent.getChildAt(i);
            int[] pos = new int[2];
            v.getLocationInWindow(pos);
            prevYs.add((float) pos[1]);
        }

        // Grab the child's new location and animate from prev to current loc.
        final ViewTreeObserver observer = mScrollContent.getViewTreeObserver();
        observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            public boolean onPreDraw() {
                observer.removeOnPreDrawListener(this);

                for (int i = mScrollContent.getChildCount() - 1; i >= 0; i--) {
                    final View v = mScrollContent.getChildAt(i);

                    float prevY;
                    float endY;
                    if (i >= prevYs.size()) {
                        // View is being created
                        prevY = mScrollContent.getTop() + mScrollContent.getHeight();
                        endY = v.getY();
                    } else {
                        prevY = prevYs.get(i);
                        int[] endPos = new int[2];
                        v.getLocationInWindow(endPos);
                        endY = endPos[1];
                    }

                    v.setTranslationY((prevY - endY));
                    root.getOverlay().add(v);

                    v.animate()
                            .setStartDelay(ANIMATE_START_DELAY)
                            .translationY(0)
                            .setDuration(ANIMATE_DURATION)
                            .setInterpolator(
                                    new DecelerateInterpolator(ANIMATE_INTERPOLATE_FACTOR))
                            .withEndAction(new Runnable() {
                                public void run() {
                                    root.getOverlay().remove(v);
                                    mScrollContent.addView(v, 0);
                                }
                            });


                }
                return false;
            }
        });
    }
}