Adam Cohen | a6d0492 | 2014-10-23 17:28:30 -0700 | [diff] [blame] | 1 | package com.android.launcher3; |
| 2 | |
| 3 | import android.content.Context; |
Adam Cohen | be25822 | 2014-10-24 16:45:59 -0700 | [diff] [blame] | 4 | import android.content.res.TypedArray; |
Adam Cohen | a6d0492 | 2014-10-23 17:28:30 -0700 | [diff] [blame] | 5 | import android.graphics.Rect; |
| 6 | import android.util.AttributeSet; |
| 7 | import android.view.View; |
Sunny Goyal | 4ffec48 | 2016-02-09 11:28:52 -0800 | [diff] [blame] | 8 | import android.view.ViewDebug; |
Adam Cohen | a6d0492 | 2014-10-23 17:28:30 -0700 | [diff] [blame] | 9 | import android.view.ViewGroup; |
| 10 | import android.widget.FrameLayout; |
| 11 | |
Jon Miranda | 758d504 | 2017-11-08 14:38:23 -0800 | [diff] [blame] | 12 | public class InsettableFrameLayout extends FrameLayout implements Insettable { |
Adam Cohen | a6d0492 | 2014-10-23 17:28:30 -0700 | [diff] [blame] | 13 | |
Sunny Goyal | 4ffec48 | 2016-02-09 11:28:52 -0800 | [diff] [blame] | 14 | @ViewDebug.ExportedProperty(category = "launcher") |
Adam Cohen | a6d0492 | 2014-10-23 17:28:30 -0700 | [diff] [blame] | 15 | protected Rect mInsets = new Rect(); |
| 16 | |
Hyunyoung Song | a9a8a42 | 2016-06-15 16:45:48 -0700 | [diff] [blame] | 17 | public Rect getInsets() { |
| 18 | return mInsets; |
| 19 | } |
| 20 | |
Adam Cohen | a6d0492 | 2014-10-23 17:28:30 -0700 | [diff] [blame] | 21 | public InsettableFrameLayout(Context context, AttributeSet attrs) { |
| 22 | super(context, attrs); |
Adam Cohen | a6d0492 | 2014-10-23 17:28:30 -0700 | [diff] [blame] | 23 | } |
| 24 | |
| 25 | public void setFrameLayoutChildInsets(View child, Rect newInsets, Rect oldInsets) { |
Adam Cohen | be25822 | 2014-10-24 16:45:59 -0700 | [diff] [blame] | 26 | final LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
| 27 | |
Adam Cohen | a6d0492 | 2014-10-23 17:28:30 -0700 | [diff] [blame] | 28 | if (child instanceof Insettable) { |
| 29 | ((Insettable) child).setInsets(newInsets); |
Adam Cohen | be25822 | 2014-10-24 16:45:59 -0700 | [diff] [blame] | 30 | } else if (!lp.ignoreInsets) { |
| 31 | lp.topMargin += (newInsets.top - oldInsets.top); |
| 32 | lp.leftMargin += (newInsets.left - oldInsets.left); |
| 33 | lp.rightMargin += (newInsets.right - oldInsets.right); |
| 34 | lp.bottomMargin += (newInsets.bottom - oldInsets.bottom); |
Adam Cohen | a6d0492 | 2014-10-23 17:28:30 -0700 | [diff] [blame] | 35 | } |
Adam Cohen | be25822 | 2014-10-24 16:45:59 -0700 | [diff] [blame] | 36 | child.setLayoutParams(lp); |
Adam Cohen | a6d0492 | 2014-10-23 17:28:30 -0700 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | @Override |
| 40 | public void setInsets(Rect insets) { |
| 41 | final int n = getChildCount(); |
| 42 | for (int i = 0; i < n; i++) { |
| 43 | final View child = getChildAt(i); |
| 44 | setFrameLayoutChildInsets(child, insets, mInsets); |
| 45 | } |
| 46 | mInsets.set(insets); |
| 47 | } |
| 48 | |
| 49 | @Override |
Adam Cohen | be25822 | 2014-10-24 16:45:59 -0700 | [diff] [blame] | 50 | public LayoutParams generateLayoutParams(AttributeSet attrs) { |
| 51 | return new InsettableFrameLayout.LayoutParams(getContext(), attrs); |
| 52 | } |
| 53 | |
| 54 | @Override |
| 55 | protected LayoutParams generateDefaultLayoutParams() { |
| 56 | return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); |
| 57 | } |
| 58 | |
| 59 | // Override to allow type-checking of LayoutParams. |
| 60 | @Override |
| 61 | protected boolean checkLayoutParams(ViewGroup.LayoutParams p) { |
| 62 | return p instanceof InsettableFrameLayout.LayoutParams; |
| 63 | } |
| 64 | |
| 65 | @Override |
| 66 | protected LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) { |
| 67 | return new LayoutParams(p); |
| 68 | } |
| 69 | |
Adam Cohen | bc927f9 | 2014-10-28 16:16:02 -0700 | [diff] [blame] | 70 | public static class LayoutParams extends FrameLayout.LayoutParams { |
Sunny Goyal | 1642f71 | 2018-09-18 11:40:35 -0700 | [diff] [blame] | 71 | public boolean ignoreInsets = false; |
Adam Cohen | be25822 | 2014-10-24 16:45:59 -0700 | [diff] [blame] | 72 | |
| 73 | public LayoutParams(Context c, AttributeSet attrs) { |
| 74 | super(c, attrs); |
| 75 | TypedArray a = c.obtainStyledAttributes(attrs, |
| 76 | R.styleable.InsettableFrameLayout_Layout); |
| 77 | ignoreInsets = a.getBoolean( |
| 78 | R.styleable.InsettableFrameLayout_Layout_layout_ignoreInsets, false); |
| 79 | a.recycle(); |
| 80 | } |
| 81 | |
| 82 | public LayoutParams(int width, int height) { |
| 83 | super(width, height); |
| 84 | } |
| 85 | |
| 86 | public LayoutParams(ViewGroup.LayoutParams lp) { |
| 87 | super(lp); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | @Override |
Jon Miranda | 758d504 | 2017-11-08 14:38:23 -0800 | [diff] [blame] | 92 | public void onViewAdded(View child) { |
| 93 | super.onViewAdded(child); |
Adam Cohen | a6d0492 | 2014-10-23 17:28:30 -0700 | [diff] [blame] | 94 | setFrameLayoutChildInsets(child, mInsets, new Rect()); |
| 95 | } |
Sunny Goyal | 07b6929 | 2018-01-08 14:19:34 -0800 | [diff] [blame] | 96 | |
| 97 | public static void dispatchInsets(ViewGroup parent, Rect insets) { |
| 98 | final int n = parent.getChildCount(); |
| 99 | for (int i = 0; i < n; i++) { |
| 100 | final View child = parent.getChildAt(i); |
| 101 | if (child instanceof Insettable) { |
| 102 | ((Insettable) child).setInsets(insets); |
| 103 | } |
| 104 | } |
| 105 | } |
Adam Cohen | a6d0492 | 2014-10-23 17:28:30 -0700 | [diff] [blame] | 106 | } |