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