summaryrefslogtreecommitdiffstats
path: root/tests/TransitionTests/src/com/android/transitiontests/ContactsExpansion.java
blob: 55a96a5a228f6caa52ddd161a35166ed48c46ec5 (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
/*
 * Copyright (C) 2013 The Android Open Source 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 com.android.transitiontests;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.transition.Fade;
import android.view.transition.Scene;
import android.view.transition.Transition;
import android.widget.ImageView;
import android.widget.TextView;
import android.view.transition.Crossfade;
import android.view.transition.Move;
import android.view.transition.Rotate;
import android.view.transition.TransitionGroup;
import android.view.transition.TransitionManager;
import com.android.transitiontest.R;

public class ContactsExpansion extends Activity {

    String contactsData[] = {
            "Alan Green", "56 Bob Street", "Boston, MA 02134", "617-555-5555", "blatt@blatt.com",
            "Bob Foonman", "92 The Avenue", "Chico, CA 78456", "510-555-5556", "bob@jerk.com",
            "Tracey Sue", "95 Houses Street", "San Jose, CA 96504", "415-555-5557", "ts@thing.com",
    };

    View currentItem = null;

    TransitionGroup mMyAutoTransition = new TransitionGroup(TransitionGroup.SEQUENTIALLY);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.contacts_list);
        ViewGroup contactsContainer = (ViewGroup) findViewById(R.id.contactsContainer);

        int contactsIndex = 0;
        addContact(contactsContainer, contactsIndex, R.drawable.self_portrait_square_100);
        contactsIndex += 5;
        addContact(contactsContainer, contactsIndex, R.drawable.self_portrait_square_100);
        contactsIndex += 5;
        addContact(contactsContainer, contactsIndex, R.drawable.self_portrait_square_100);

    }

    private void addContact(ViewGroup container, int dataIndex, int thumbnailID) {
        LayoutInflater inflater = (LayoutInflater)
                getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View contactItem = inflater.inflate(R.layout.contact_collapsed, container, false);
        ImageView thumbnailView = (ImageView) contactItem.findViewById(R.id.contact_picture);
        thumbnailView.setImageResource(thumbnailID);
        ((TextView)contactItem.findViewById(R.id.contact_name)).setText(contactsData[dataIndex++]);
        ((TextView)contactItem.findViewById(R.id.contact_street)).
                setText(contactsData[dataIndex++]);
        ((TextView)contactItem.findViewById(R.id.contact_city)).setText(contactsData[dataIndex++]);
        ((TextView)contactItem.findViewById(R.id.contact_phone)).setText(contactsData[dataIndex++]);
        ((TextView)contactItem.findViewById(R.id.contact_email)).setText(contactsData[dataIndex++]);
        container.addView(contactItem);

        final TransitionGroup myTransition = new TransitionGroup();
        myTransition.addTransitions(new Fade(Fade.IN),
                new Rotate().setTargetIds(R.id.contact_arrow),
                new Move(), new Fade(Fade.OUT),
                new Crossfade().setTargetIds(R.id.contact_picture));
        final ToggleScene toggleScene = new ToggleScene(container, myTransition);
        contactItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                currentItem = v;
                toggleScene.changeToScene();
            }
        });
    }

    class ToggleScene {
        boolean expanded = false;
        Scene mScene;
        Transition mTransition;

        ToggleScene(ViewGroup rootView, Transition transition) {
            mScene = new Scene(rootView);
            mTransition = transition;
            mScene.setEnterAction(new Runnable() {
                @Override
                public void run() {
                    if (currentItem != null) {
                        System.out.println("onsceneChanged: currentItem = " + currentItem);
                        View expandedContainer = currentItem.findViewById(R.id.expanded_info);
                        expandedContainer.setVisibility(expanded ? View.GONE : View.VISIBLE);
                        ImageView thumbnailView =
                                (ImageView) currentItem.findViewById(R.id.contact_picture);
                        thumbnailView.setImageResource(expanded ? R.drawable.self_portrait_square_100 :
                                R.drawable.self_portrait_square_200);
                        ImageView arrow = (ImageView) currentItem.findViewById(R.id.contact_arrow);
                        arrow.setRotation(expanded ? 0 : 90);
                        expanded = !expanded;
                    }
                }
            });
        }

        void changeToScene() {
            TransitionManager.go(mScene, mTransition);
        }
    };
}