FragmentTestActivity.java revision a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553
1a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount/*
2a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount * Copyright (C) 2015 The Android Open Source Project
3a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount *
4a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount * Licensed under the Apache License, Version 2.0 (the "License");
5a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount * you may not use this file except in compliance with the License.
6a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount * You may obtain a copy of the License at
7a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount *
8a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount *      http://www.apache.org/licenses/LICENSE-2.0
9a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount *
10a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount * Unless required by applicable law or agreed to in writing, software
11a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount * distributed under the License is distributed on an "AS IS" BASIS,
12a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount * See the License for the specific language governing permissions and
14a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount * limitations under the License.
15a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount */
16a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mountpackage android.support.v4.app.test;
17a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount
18a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mountimport android.app.Activity;
19a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mountimport android.os.Build.VERSION;
20a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mountimport android.os.Build.VERSION_CODES;
21a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mountimport android.os.Bundle;
22a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mountimport android.support.v4.app.Fragment;
23a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mountimport android.support.v4.app.FragmentActivity;
24a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mountimport android.support.v4.test.R;
25a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mountimport android.transition.Transition;
26a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mountimport android.transition.Transition.TransitionListener;
27a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mountimport android.transition.TransitionInflater;
28a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mountimport android.view.LayoutInflater;
29a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mountimport android.view.View;
30a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mountimport android.view.ViewGroup;
31a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount
32a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount/**
33a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount * A simple activity used for Fragment Transitions
34a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount */
35a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mountpublic class FragmentTestActivity extends FragmentActivity {
36a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount    @Override
37a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount    public void onCreate(Bundle icicle) {
38a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount        super.onCreate(icicle);
39a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount        setContentView(R.layout.activity_content);
40a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount    }
41a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount
42a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount    public static class TestFragment extends Fragment {
43a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount        public static final int ENTER = 0;
44a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount        public static final int RETURN = 1;
45a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount        public static final int EXIT = 2;
46a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount        public static final int REENTER = 3;
47a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount        public static final int SHARED_ELEMENT_ENTER = 4;
48a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount        public static final int SHARED_ELEMENT_RETURN = 5;
49a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount        private static final int TRANSITION_COUNT = 6;
50a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount
51a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount        private static final String LAYOUT_ID = "layoutId";
52a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount        private static final String TRANSITION_KEY = "transition_";
53a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount        private int mLayoutId = R.layout.fragment_start;
54a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount        private final int[] mTransitionIds = new int[] {
55a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount                android.R.transition.explode,
56a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount                android.R.transition.explode,
57a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount                android.R.transition.fade,
58a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount                android.R.transition.fade,
59a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount                android.R.transition.move,
60a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount                android.R.transition.move,
61a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount        };
62a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount        private final TransitionCalledListener[] mListeners =
63a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount                new TransitionCalledListener[TRANSITION_COUNT];
64a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount        private OnTransitionListener mOnTransitionListener;
65a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount
66a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount        public TestFragment() {
67a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount            for (int i = 0; i < TRANSITION_COUNT; i++) {
68a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount                mListeners[i] = new TransitionCalledListener();
69a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount            }
70a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount        }
71a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount
72a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount        public static TestFragment create(int layoutId) {
73a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount            TestFragment testFragment = new TestFragment();
74a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount            testFragment.mLayoutId = layoutId;
75a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount            return testFragment;
76a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount        }
77a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount
78a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount        public void clearTransitions() {
79a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount            for (int i = 0; i < TRANSITION_COUNT; i++) {
80a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount                mTransitionIds[i] = 0;
81a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount            }
82a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount        }
83a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount
84a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount        public void clearNotifications() {
85a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount            for (int i = 0; i < TRANSITION_COUNT; i++) {
86a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount                mListeners[i].transitionStarted = false;
87a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount                mListeners[i].transitionEnded = false;
88a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount            }
89a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount        }
90a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount
91a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount        @Override
92a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount        public void onCreate(Bundle savedInstanceState) {
93a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount            super.onCreate(savedInstanceState);
94a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount            if (savedInstanceState != null) {
95a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount                mLayoutId = savedInstanceState.getInt(LAYOUT_ID, mLayoutId);
96a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount                for (int i = 0; i < TRANSITION_COUNT; i++) {
97a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount                    String key = TRANSITION_KEY + i;
98a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount                    mTransitionIds[i] = savedInstanceState.getInt(key, mTransitionIds[i]);
99a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount                }
100a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount            }
101a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount        }
102a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount
103a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount        @Override
104a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount        public void onSaveInstanceState(Bundle outState) {
105a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount            super.onSaveInstanceState(outState);
106a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount            outState.putInt(LAYOUT_ID, mLayoutId);
107a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount            for (int i = 0; i < TRANSITION_COUNT; i++) {
108a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount                String key = TRANSITION_KEY + i;
109a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount                outState.putInt(key, mTransitionIds[i]);
110a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount            }
111a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount        }
112a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount
113a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount        @Override
114a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount        public View onCreateView(LayoutInflater inflater, ViewGroup container,
115a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount                Bundle savedInstanceState) {
116a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount            return inflater.inflate(mLayoutId, container, false);
117a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount        }
118a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount
119a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount        @SuppressWarnings("deprecation")
120a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount        @Override
121a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount        public void onAttach(Activity activity) {
122a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount            super.onAttach(activity);
123a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount            if (VERSION.SDK_INT > VERSION_CODES.KITKAT) {
124a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount                setEnterTransition(loadTransition(ENTER));
125a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount                setReenterTransition(loadTransition(REENTER));
126a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount                setExitTransition(loadTransition(EXIT));
127a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount                setReturnTransition(loadTransition(RETURN));
128a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount                setSharedElementEnterTransition(loadTransition(SHARED_ELEMENT_ENTER));
129a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount                setSharedElementReturnTransition(loadTransition(SHARED_ELEMENT_RETURN));
130a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount            }
131a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount        }
132a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount
133a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount        public void setOnTransitionListener(OnTransitionListener listener) {
134a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount            mOnTransitionListener = listener;
135a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount        }
136a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount
137a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount        public boolean wasStartCalled(int transitionKey) {
138a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount            return mListeners[transitionKey].transitionStarted;
139a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount        }
140a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount
141a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount        public boolean wasEndCalled(int transitionKey) {
142a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount            return mListeners[transitionKey].transitionEnded;
143a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount        }
144a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount
145a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount        private Transition loadTransition(int key) {
146a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount            final int id = mTransitionIds[key];
147a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount            if (id == 0) {
148a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount                return null;
149a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount            }
150a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount            Transition transition = TransitionInflater.from(getActivity()).inflateTransition(id);
151a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount            transition.addListener(mListeners[key]);
152a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount            return transition;
153a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount        }
154a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount
155a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount        private void notifyTransition() {
156a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount            if (mOnTransitionListener != null) {
157a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount                mOnTransitionListener.onTransition(this);
158a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount            }
159a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount        }
160a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount
161a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount        private class TransitionCalledListener implements TransitionListener {
162a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount            public boolean transitionStarted;
163a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount            public boolean transitionEnded;
164a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount
165a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount            public TransitionCalledListener() {
166a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount            }
167a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount
168a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount            @Override
169a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount            public void onTransitionStart(Transition transition) {
170a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount                transitionStarted = true;
171a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount                notifyTransition();
172a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount            }
173a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount
174a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount            @Override
175a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount            public void onTransitionEnd(Transition transition) {
176a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount                transitionEnded = true;
177a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount                notifyTransition();
178a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount            }
179a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount
180a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount            @Override
181a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount            public void onTransitionCancel(Transition transition) {
182a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount            }
183a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount
184a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount            @Override
185a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount            public void onTransitionPause(Transition transition) {
186a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount            }
187a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount
188a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount            @Override
189a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount            public void onTransitionResume(Transition transition) {
190a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount            }
191a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount        }
192a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount    }
193a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount
194a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount    public interface OnTransitionListener {
195a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount        void onTransition(TestFragment fragment);
196a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount    }
197a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount
198a1f95ec10cc7e88dcc1c57c92ac5d0c37c2d9553George Mount}
199