1/* 2 * Copyright (C) 2015 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License 15 */ 16package android.support.v17.leanback.widget; 17 18import android.os.Build; 19import android.view.View; 20import android.view.ViewGroup; 21 22 23/** 24 * Helper for static (nine patch) shadows. 25 */ 26final class StaticShadowHelper { 27 28 final static StaticShadowHelper sInstance = new StaticShadowHelper(); 29 boolean mSupportsShadow; 30 ShadowHelperVersionImpl mImpl; 31 32 /** 33 * Interface implemented by classes that support Shadow. 34 */ 35 static interface ShadowHelperVersionImpl { 36 public void prepareParent(ViewGroup parent); 37 public Object addStaticShadow(ViewGroup shadowContainer, boolean roundedCorners); 38 public void setShadowFocusLevel(Object impl, float level); 39 } 40 41 /** 42 * Interface used when we do not support Shadow animations. 43 */ 44 private static final class ShadowHelperStubImpl implements ShadowHelperVersionImpl { 45 @Override 46 public void prepareParent(ViewGroup parent) { 47 // do nothing 48 } 49 50 @Override 51 public Object addStaticShadow(ViewGroup shadowContainer, boolean roundedCorners) { 52 // do nothing 53 return null; 54 } 55 56 @Override 57 public void setShadowFocusLevel(Object impl, float level) { 58 // do nothing 59 } 60 } 61 62 /** 63 * Implementation used on JBMR2 (and above). 64 */ 65 private static final class ShadowHelperJbmr2Impl implements ShadowHelperVersionImpl { 66 @Override 67 public void prepareParent(ViewGroup parent) { 68 ShadowHelperJbmr2.prepareParent(parent); 69 } 70 71 @Override 72 public Object addStaticShadow(ViewGroup shadowContainer, boolean roundedCorners) { 73 // Static shadows are always rounded 74 return ShadowHelperJbmr2.addShadow(shadowContainer); 75 } 76 77 @Override 78 public void setShadowFocusLevel(Object impl, float level) { 79 ShadowHelperJbmr2.setShadowFocusLevel(impl, level); 80 } 81 } 82 83 /** 84 * Returns the StaticShadowHelper. 85 */ 86 private StaticShadowHelper() { 87 if (Build.VERSION.SDK_INT >= 18) { 88 mSupportsShadow = true; 89 mImpl = new ShadowHelperJbmr2Impl(); 90 } else { 91 mSupportsShadow = false; 92 mImpl = new ShadowHelperStubImpl(); 93 } 94 } 95 96 public static StaticShadowHelper getInstance() { 97 return sInstance; 98 } 99 100 public boolean supportsShadow() { 101 return mSupportsShadow; 102 } 103 104 public void prepareParent(ViewGroup parent) { 105 mImpl.prepareParent(parent); 106 } 107 108 public Object addStaticShadow(ViewGroup shadowContainer, boolean roundedCorners) { 109 return mImpl.addStaticShadow(shadowContainer, roundedCorners); 110 } 111 112 public void setShadowFocusLevel(Object impl, float level) { 113 mImpl.setShadowFocusLevel(impl, level); 114 } 115} 116