ShadowHelper.java revision 69e74bd8956577d9a3414b81ec661fd5fee42e19
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.ViewGroup;
18import android.view.View;
19
20
21/**
22 * Helper for shadow.
23 */
24final class ShadowHelper {
25
26    final static ShadowHelper sInstance = new ShadowHelper();
27    boolean mSupportsShadow;
28    ShadowHelperVersionImpl mImpl;
29
30    /**
31     * Interface implemented by classes that support Shadow.
32     */
33    static interface ShadowHelperVersionImpl {
34
35        public void prepareParent(ViewGroup parent);
36
37        public Object addShadow(ViewGroup shadowContainer);
38
39        public void setZ(View view, float focusLevel);
40
41        public void setShadowFocusLevel(Object impl, float level);
42
43    }
44
45    /**
46     * Interface used when we do not support Shadow animations.
47     */
48    private static final class ShadowHelperStubImpl implements ShadowHelperVersionImpl {
49
50        @Override
51        public void prepareParent(ViewGroup parent) {
52            // do nothing
53        }
54
55        @Override
56        public Object addShadow(ViewGroup shadowContainer) {
57            // do nothing
58            return null;
59        }
60
61        @Override
62        public void setShadowFocusLevel(Object impl, float level) {
63            // do nothing
64        }
65
66        @Override
67        public void setZ(View view, float focusLevel) {
68            // do nothing
69        }
70
71    }
72
73    /**
74     * Implementation used on JBMR2 (and above).
75     */
76    private static final class ShadowHelperJbmr2Impl implements ShadowHelperVersionImpl {
77
78        @Override
79        public void prepareParent(ViewGroup parent) {
80            ShadowHelperJbmr2.prepareParent(parent);
81        }
82
83        @Override
84        public Object addShadow(ViewGroup shadowContainer) {
85            return ShadowHelperJbmr2.addShadow(shadowContainer);
86        }
87
88        @Override
89        public void setShadowFocusLevel(Object impl, float level) {
90            ShadowHelperJbmr2.setShadowFocusLevel(impl, level);
91        }
92
93        @Override
94        public void setZ(View view, float focusLevel) {
95            // Not supported
96        }
97
98    }
99
100    /**
101     * Implementation used on api 21 (and above).
102     */
103    private static final class ShadowHelperApi21Impl implements ShadowHelperVersionImpl {
104
105        @Override
106        public void prepareParent(ViewGroup parent) {
107            // do nothing
108        }
109
110        @Override
111        public Object addShadow(ViewGroup shadowContainer) {
112            return ShadowHelperApi21.addShadow(shadowContainer);
113        }
114
115        @Override
116        public void setShadowFocusLevel(Object impl, float level) {
117            ShadowHelperApi21.setShadowFocusLevel(impl, level);
118        }
119
120        @Override
121        public void setZ(View view, float focusLevel) {
122            ShadowHelperApi21.setZ(view, focusLevel);
123        }
124
125    }
126
127    /**
128     * Returns the ShadowHelper.
129     */
130    private ShadowHelper() {
131     // TODO: we should use version number once "L" is published
132        if ("L".equals(Build.VERSION.RELEASE)) {
133            mSupportsShadow = true;
134            mImpl = new ShadowHelperApi21Impl();
135        } else if (Build.VERSION.SDK_INT >= 18) {
136            mSupportsShadow = true;
137            mImpl = new ShadowHelperJbmr2Impl();
138        } else {
139            mSupportsShadow = false;
140            mImpl = new ShadowHelperStubImpl();
141        }
142    }
143
144    public static ShadowHelper getInstance() {
145        return sInstance;
146    }
147
148    public boolean supportsShadow() {
149        return mSupportsShadow;
150    }
151
152    public void prepareParent(ViewGroup parent) {
153        mImpl.prepareParent(parent);
154    }
155
156    public Object addShadow(ViewGroup shadowContainer) {
157        return mImpl.addShadow(shadowContainer);
158    }
159
160    public void setShadowFocusLevel(Object impl, float level) {
161        mImpl.setShadowFocusLevel(impl, level);
162    }
163
164    /**
165     * Set the view z coordinate with the given focus level from 0..1.
166     */
167    public void setZ(View view, float focusLevel) {
168        mImpl.setZ(view, focusLevel);
169    }
170}
171