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