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  */
16 package androidx.leanback.widget;
17 
18 import android.os.Build;
19 import android.view.LayoutInflater;
20 import android.view.View;
21 import android.view.ViewGroup;
22 
23 import androidx.leanback.R;
24 
25 /**
26  * Helper for static (nine patch) shadows.
27  */
28 final class StaticShadowHelper {
StaticShadowHelper()29     private StaticShadowHelper() {
30     }
31 
supportsShadow()32     static boolean supportsShadow() {
33         return Build.VERSION.SDK_INT >= 21;
34     }
35 
prepareParent(ViewGroup parent)36     static void prepareParent(ViewGroup parent) {
37         if (Build.VERSION.SDK_INT >= 21) {
38             parent.setLayoutMode(ViewGroup.LAYOUT_MODE_OPTICAL_BOUNDS);
39         }
40     }
41 
addStaticShadow(ViewGroup shadowContainer)42     static Object addStaticShadow(ViewGroup shadowContainer) {
43         if (Build.VERSION.SDK_INT >= 21) {
44             shadowContainer.setLayoutMode(ViewGroup.LAYOUT_MODE_OPTICAL_BOUNDS);
45             LayoutInflater inflater = LayoutInflater.from(shadowContainer.getContext());
46             inflater.inflate(R.layout.lb_shadow, shadowContainer, true);
47             ShadowImpl impl = new ShadowImpl();
48             impl.mNormalShadow = shadowContainer.findViewById(R.id.lb_shadow_normal);
49             impl.mFocusShadow = shadowContainer.findViewById(R.id.lb_shadow_focused);
50             return impl;
51         }
52         return null;
53     }
54 
setShadowFocusLevel(Object impl, float level)55     static void setShadowFocusLevel(Object impl, float level) {
56         if (Build.VERSION.SDK_INT >= 21) {
57             ShadowImpl shadowImpl = (ShadowImpl) impl;
58             shadowImpl.mNormalShadow.setAlpha(1 - level);
59             shadowImpl.mFocusShadow.setAlpha(level);
60         }
61     }
62 
63     static class ShadowImpl {
64         View mNormalShadow;
65         View mFocusShadow;
66     }
67 }
68