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 
17 package androidx.leanback.preference.internal;
18 
19 import static androidx.annotation.RestrictTo.Scope.LIBRARY_GROUP_PREFIX;
20 
21 import android.content.Context;
22 import android.graphics.Outline;
23 import android.util.AttributeSet;
24 import android.view.View;
25 import android.view.ViewOutlineProvider;
26 import android.widget.FrameLayout;
27 
28 import androidx.annotation.RestrictTo;
29 
30 /**
31  * {@link FrameLayout} subclass that provides an outline only when it has children, so that it does
32  * not cast a shadow when empty.
33  *
34  */
35 @RestrictTo(LIBRARY_GROUP_PREFIX)
36 public class OutlineOnlyWithChildrenFrameLayout extends FrameLayout {
37 
38     private ViewOutlineProvider mMagicalOutlineProvider;
39     ViewOutlineProvider mInnerOutlineProvider;
40 
OutlineOnlyWithChildrenFrameLayout(Context context)41     public OutlineOnlyWithChildrenFrameLayout(Context context) {
42         super(context);
43     }
44 
OutlineOnlyWithChildrenFrameLayout(Context context, AttributeSet attrs)45     public OutlineOnlyWithChildrenFrameLayout(Context context, AttributeSet attrs) {
46         super(context, attrs);
47     }
48 
OutlineOnlyWithChildrenFrameLayout(Context context, AttributeSet attrs, int defStyleAttr)49     public OutlineOnlyWithChildrenFrameLayout(Context context, AttributeSet attrs,
50             int defStyleAttr) {
51         super(context, attrs, defStyleAttr);
52     }
53 
OutlineOnlyWithChildrenFrameLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)54     public OutlineOnlyWithChildrenFrameLayout(Context context, AttributeSet attrs,
55             int defStyleAttr, int defStyleRes) {
56         super(context, attrs, defStyleAttr, defStyleRes);
57     }
58 
59     @Override
onLayout(boolean changed, int left, int top, int right, int bottom)60     protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
61         super.onLayout(changed, left, top, right, bottom);
62         invalidateOutline();
63     }
64 
65     @Override
setOutlineProvider(ViewOutlineProvider provider)66     public void setOutlineProvider(ViewOutlineProvider provider) {
67         mInnerOutlineProvider = provider;
68         if (mMagicalOutlineProvider == null) {
69             // Can't initialize this directly because this method is called from the superclass's
70             // constructor.
71             mMagicalOutlineProvider = new ViewOutlineProvider() {
72                 @Override
73                 public void getOutline(View view, Outline outline) {
74                     if (getChildCount() > 0) {
75                         mInnerOutlineProvider.getOutline(view, outline);
76                     } else {
77                         ViewOutlineProvider.BACKGROUND.getOutline(view, outline);
78                     }
79                 }
80             };
81         }
82         super.setOutlineProvider(mMagicalOutlineProvider);
83     }
84 }
85