1/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5 * in compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the License
10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11 * or implied. See the License for the specific language governing permissions and limitations under
12 * the License.
13 */
14package android.support.v17.leanback.transition;
15
16import static android.support.annotation.RestrictTo.Scope.LIBRARY_GROUP;
17
18import android.content.Context;
19import android.os.Build;
20import android.support.annotation.RestrictTo;
21import android.support.v17.leanback.R;
22
23/**
24 * Helper class to load Leanback specific transition.
25 * @hide
26 */
27@RestrictTo(LIBRARY_GROUP)
28public class LeanbackTransitionHelper {
29
30    interface LeanbackTransitionHelperVersion {
31        Object loadTitleInTransition(Context context);
32        Object loadTitleOutTransition(Context context);
33    }
34
35    /*
36     * Kitkat does not allow load custom transition from resource, calling
37     * LeanbackTransitionHelperKitKat to build custom transition in code.
38     */
39    static class LeanbackTransitionHelperKitKatImpl implements LeanbackTransitionHelperVersion {
40
41        @Override
42        public Object loadTitleInTransition(Context context) {
43            return LeanbackTransitionHelperKitKat.loadTitleInTransition(context);
44        }
45
46        @Override
47        public Object loadTitleOutTransition(Context context) {
48            return LeanbackTransitionHelperKitKat.loadTitleOutTransition(context);
49        }
50    }
51
52    /*
53     * Load transition from resource or just return stub for API17.
54     */
55    static class LeanbackTransitionHelperDefault implements LeanbackTransitionHelperVersion {
56
57        @Override
58        public Object loadTitleInTransition(Context context) {
59            return TransitionHelper.loadTransition(context, R.transition.lb_title_in);
60        }
61
62        @Override
63        public Object loadTitleOutTransition(Context context) {
64            return TransitionHelper.loadTransition(context, R.transition.lb_title_out);
65        }
66    }
67
68    static LeanbackTransitionHelperVersion sImpl;
69
70    static {
71        if (Build.VERSION.SDK_INT >= 21) {
72            sImpl = new LeanbackTransitionHelperDefault();
73        } else if (Build.VERSION.SDK_INT >= 19) {
74            sImpl = new LeanbackTransitionHelperKitKatImpl();
75        } else {
76            // Helper will create a stub object for transition in this case.
77            sImpl = new LeanbackTransitionHelperDefault();
78        }
79    }
80
81    static public Object loadTitleInTransition(Context context) {
82        return sImpl.loadTitleInTransition(context);
83    }
84
85    static public Object loadTitleOutTransition(Context context) {
86        return sImpl.loadTitleOutTransition(context);
87    }
88}
89