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.widget;
15
16import android.os.Build;
17import android.view.View;
18
19
20/**
21 * Helper for shadow.
22 */
23final class ShadowHelper {
24
25    final static ShadowHelper sInstance = new ShadowHelper();
26    boolean mSupportsDynamicShadow;
27    ShadowHelperVersionImpl mImpl;
28
29    /**
30     * Interface implemented by classes that support Shadow.
31     */
32    static interface ShadowHelperVersionImpl {
33        public Object addDynamicShadow(
34                View shadowContainer, float unfocusedZ, float focusedZ, int roundedCornerRadius);
35        public void setZ(View view, float z);
36        public void setShadowFocusLevel(Object impl, float level);
37    }
38
39    /**
40     * Interface used when we do not support Shadow animations.
41     */
42    private static final class ShadowHelperStubImpl implements ShadowHelperVersionImpl {
43        ShadowHelperStubImpl() {
44        }
45
46        @Override
47        public Object addDynamicShadow(
48                View shadowContainer, float focusedZ, float unfocusedZ, int roundedCornerRadius) {
49            // do nothing
50            return null;
51        }
52
53        @Override
54        public void setShadowFocusLevel(Object impl, float level) {
55            // do nothing
56        }
57
58        @Override
59        public void setZ(View view, float z) {
60            // do nothing
61        }
62    }
63
64    /**
65     * Implementation used on api 21 (and above).
66     */
67    private static final class ShadowHelperApi21Impl implements ShadowHelperVersionImpl {
68        ShadowHelperApi21Impl() {
69        }
70
71        @Override
72        public Object addDynamicShadow(
73                View shadowContainer, float unfocusedZ, float focusedZ, int roundedCornerRadius) {
74            return ShadowHelperApi21.addDynamicShadow(
75                    shadowContainer, unfocusedZ, focusedZ, roundedCornerRadius);
76        }
77
78        @Override
79        public void setShadowFocusLevel(Object impl, float level) {
80            ShadowHelperApi21.setShadowFocusLevel(impl, level);
81        }
82
83        @Override
84        public void setZ(View view, float z) {
85            ShadowHelperApi21.setZ(view, z);
86        }
87    }
88
89    /**
90     * Returns the ShadowHelper.
91     */
92    private ShadowHelper() {
93        if (Build.VERSION.SDK_INT >= 21) {
94            mSupportsDynamicShadow = true;
95            mImpl = new ShadowHelperApi21Impl();
96        } else {
97            mImpl = new ShadowHelperStubImpl();
98        }
99    }
100
101    public static ShadowHelper getInstance() {
102        return sInstance;
103    }
104
105    public boolean supportsDynamicShadow() {
106        return mSupportsDynamicShadow;
107    }
108
109    public Object addDynamicShadow(
110            View shadowContainer, float unfocusedZ, float focusedZ, int roundedCornerRadius) {
111        return mImpl.addDynamicShadow(shadowContainer, unfocusedZ, focusedZ, roundedCornerRadius);
112    }
113
114    public void setShadowFocusLevel(Object impl, float level) {
115        mImpl.setShadowFocusLevel(impl, level);
116    }
117
118    /**
119     * Set the view z coordinate.
120     */
121    public void setZ(View view, float z) {
122        mImpl.setZ(view, z);
123    }
124
125}
126