1/*
2 * Copyright (C) 2016 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
17package android.support.transition;
18
19import android.view.View;
20import android.view.ViewGroup;
21
22import java.lang.reflect.Field;
23import java.lang.reflect.InvocationTargetException;
24import java.lang.reflect.Method;
25
26class SceneKitKat extends SceneWrapper {
27
28    private static Field sEnterAction;
29    private static Method sSetCurrentScene;
30
31    private View mLayout; // alternative to layoutId
32
33    @Override
34    public void init(ViewGroup sceneRoot) {
35        mScene = new android.transition.Scene(sceneRoot);
36    }
37
38    @Override
39    public void init(ViewGroup sceneRoot, View layout) {
40        if (layout instanceof ViewGroup) {
41            mScene = new android.transition.Scene(sceneRoot, (ViewGroup) layout);
42        } else {
43            mScene = new android.transition.Scene(sceneRoot);
44            mLayout = layout;
45        }
46    }
47
48    @Override
49    public void enter() {
50        if (mLayout != null) {
51            // empty out parent container before adding to it
52            final ViewGroup root = getSceneRoot();
53            root.removeAllViews();
54            root.addView(mLayout);
55            invokeEnterAction();
56            updateCurrentScene(root);
57        } else {
58            mScene.enter();
59        }
60    }
61
62    private void invokeEnterAction() {
63        if (sEnterAction == null) {
64            try {
65                sEnterAction = android.transition.Scene.class.getDeclaredField("mEnterAction");
66                sEnterAction.setAccessible(true);
67            } catch (NoSuchFieldException e) {
68                throw new RuntimeException(e);
69            }
70        }
71        try {
72            final Runnable enterAction = (Runnable) sEnterAction.get(mScene);
73            if (enterAction != null) {
74                enterAction.run();
75            }
76        } catch (IllegalAccessException e) {
77            throw new RuntimeException(e);
78        }
79    }
80
81    /** Sets this Scene as the current scene of the View. */
82    private void updateCurrentScene(View view) {
83        if (sSetCurrentScene == null) {
84            try {
85                sSetCurrentScene = android.transition.Scene.class.getDeclaredMethod(
86                        "setCurrentScene", View.class, android.transition.Scene.class);
87                sSetCurrentScene.setAccessible(true);
88            } catch (NoSuchMethodException e) {
89                throw new RuntimeException(e);
90            }
91        }
92        try {
93            sSetCurrentScene.invoke(null, view, mScene);
94        } catch (IllegalAccessException | InvocationTargetException e) {
95            throw new RuntimeException(e);
96        }
97    }
98
99}
100