1 /*
2  * Copyright 2018 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.cardview.widget;
17 
18 import android.content.Context;
19 import android.content.res.ColorStateList;
20 import android.view.View;
21 
22 import androidx.annotation.Nullable;
23 import androidx.annotation.RequiresApi;
24 
25 @RequiresApi(21)
26 class CardViewApi21Impl implements CardViewImpl {
27 
28     @Override
initialize(CardViewDelegate cardView, Context context, ColorStateList backgroundColor, float radius, float elevation, float maxElevation)29     public void initialize(CardViewDelegate cardView, Context context,
30                 ColorStateList backgroundColor, float radius, float elevation, float maxElevation) {
31         final RoundRectDrawable background = new RoundRectDrawable(backgroundColor, radius);
32         cardView.setCardBackground(background);
33 
34         View view = cardView.getCardView();
35         view.setClipToOutline(true);
36         view.setElevation(elevation);
37         setMaxElevation(cardView, maxElevation);
38     }
39 
40     @Override
setRadius(CardViewDelegate cardView, float radius)41     public void setRadius(CardViewDelegate cardView, float radius) {
42         getCardBackground(cardView).setRadius(radius);
43     }
44 
45     @Override
initStatic()46     public void initStatic() {
47     }
48 
49     @Override
setMaxElevation(CardViewDelegate cardView, float maxElevation)50     public void setMaxElevation(CardViewDelegate cardView, float maxElevation) {
51         getCardBackground(cardView).setPadding(maxElevation,
52                 cardView.getUseCompatPadding(), cardView.getPreventCornerOverlap());
53         updatePadding(cardView);
54     }
55 
56     @Override
getMaxElevation(CardViewDelegate cardView)57     public float getMaxElevation(CardViewDelegate cardView) {
58         return getCardBackground(cardView).getPadding();
59     }
60 
61     @Override
getMinWidth(CardViewDelegate cardView)62     public float getMinWidth(CardViewDelegate cardView) {
63         return getRadius(cardView) * 2;
64     }
65 
66     @Override
getMinHeight(CardViewDelegate cardView)67     public float getMinHeight(CardViewDelegate cardView) {
68         return getRadius(cardView) * 2;
69     }
70 
71     @Override
getRadius(CardViewDelegate cardView)72     public float getRadius(CardViewDelegate cardView) {
73         return getCardBackground(cardView).getRadius();
74     }
75 
76     @Override
setElevation(CardViewDelegate cardView, float elevation)77     public void setElevation(CardViewDelegate cardView, float elevation) {
78         cardView.getCardView().setElevation(elevation);
79     }
80 
81     @Override
getElevation(CardViewDelegate cardView)82     public float getElevation(CardViewDelegate cardView) {
83         return cardView.getCardView().getElevation();
84     }
85 
86     @Override
updatePadding(CardViewDelegate cardView)87     public void updatePadding(CardViewDelegate cardView) {
88         if (!cardView.getUseCompatPadding()) {
89             cardView.setShadowPadding(0, 0, 0, 0);
90             return;
91         }
92         float elevation = getMaxElevation(cardView);
93         final float radius = getRadius(cardView);
94         int hPadding = (int) Math.ceil(RoundRectDrawableWithShadow
95                 .calculateHorizontalPadding(elevation, radius, cardView.getPreventCornerOverlap()));
96         int vPadding = (int) Math.ceil(RoundRectDrawableWithShadow
97                 .calculateVerticalPadding(elevation, radius, cardView.getPreventCornerOverlap()));
98         cardView.setShadowPadding(hPadding, vPadding, hPadding, vPadding);
99     }
100 
101     @Override
onCompatPaddingChanged(CardViewDelegate cardView)102     public void onCompatPaddingChanged(CardViewDelegate cardView) {
103         setMaxElevation(cardView, getMaxElevation(cardView));
104     }
105 
106     @Override
onPreventCornerOverlapChanged(CardViewDelegate cardView)107     public void onPreventCornerOverlapChanged(CardViewDelegate cardView) {
108         setMaxElevation(cardView, getMaxElevation(cardView));
109     }
110 
111     @Override
setBackgroundColor(CardViewDelegate cardView, @Nullable ColorStateList color)112     public void setBackgroundColor(CardViewDelegate cardView, @Nullable ColorStateList color) {
113         getCardBackground(cardView).setColor(color);
114     }
115 
116     @Override
getBackgroundColor(CardViewDelegate cardView)117     public ColorStateList getBackgroundColor(CardViewDelegate cardView) {
118         return getCardBackground(cardView).getColor();
119     }
120 
getCardBackground(CardViewDelegate cardView)121     private RoundRectDrawable getCardBackground(CardViewDelegate cardView) {
122         return ((RoundRectDrawable) cardView.getCardBackground());
123     }
124 }
125