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