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