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 | |
| 12 | public class InsettableFrameLayout extends FrameLayout implements |
| 13 | ViewGroup.OnHierarchyChangeListener, Insettable { |
| 14 | |
Sunny Goyal | 4ffec48 | 2016-02-09 11:28:52 -0800 | [diff] [blame] | 15 | @ViewDebug.ExportedProperty(category = "launcher") |
Adam Cohen | a6d0492 | 2014-10-23 17:28:30 -0700 | [diff] [blame] | 16 | protected Rect mInsets = new Rect(); |
| 17 | |
| 18 | public InsettableFrameLayout(Context context, AttributeSet attrs) { |
| 19 | super(context, attrs); |
| 20 | setOnHierarchyChangeListener(this); |
| 21 | } |
| 22 | |
| 23 | public void setFrameLayoutChildInsets(View child, Rect newInsets, Rect oldInsets) { |
Adam Cohen | be25822 | 2014-10-24 16:45:59 -0700 | [diff] [blame] | 24 | final LayoutParams lp = (LayoutParams) child.getLayoutParams(); |
| 25 | |
Adam Cohen | a6d0492 | 2014-10-23 17:28:30 -0700 | [diff] [blame] | 26 | if (child instanceof Insettable) { |
| 27 | ((Insettable) child).setInsets(newInsets); |
Adam Cohen | be25822 | 2014-10-24 16:45:59 -0700 | [diff] [blame] | 28 | } else if (!lp.ignoreInsets) { |
| 29 | lp.topMargin += (newInsets.top - oldInsets.top); |
| 30 | lp.leftMargin += (newInsets.left - oldInsets.left); |
| 31 | lp.rightMargin += (newInsets.right - oldInsets.right); |
| 32 | lp.bottomMargin += (newInsets.bottom - oldInsets.bottom); |
Adam Cohen | a6d0492 | 2014-10-23 17:28:30 -0700 | [diff] [blame] | 33 | } |
Adam Cohen | be25822 | 2014-10-24 16:45:59 -0700 | [diff] [blame] | 34 | child.setLayoutParams(lp); |
Adam Cohen | a6d0492 | 2014-10-23 17:28:30 -0700 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | @Override |
| 38 | public void setInsets(Rect insets) { |
| 39 | final int n = getChildCount(); |
| 40 | for (int i = 0; i < n; i++) { |
| 41 | final View child = getChildAt(i); |
| 42 | setFrameLayoutChildInsets(child, insets, mInsets); |
| 43 | } |
| 44 | mInsets.set(insets); |
| 45 | } |
| 46 | |
| 47 | @Override |
Adam Cohen | be25822 | 2014-10-24 16:45:59 -0700 | [diff] [blame] | 48 | public LayoutParams generateLayoutParams(AttributeSet attrs) { |
| 49 | return new InsettableFrameLayout.LayoutParams(getContext(), attrs); |
| 50 | } |
| 51 | |
| 52 | @Override |
| 53 | protected LayoutParams generateDefaultLayoutParams() { |
| 54 | return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); |
| 55 | } |
| 56 | |
| 57 | // Override to allow type-checking of LayoutParams. |
| 58 | @Override |
| 59 | protected boolean checkLayoutParams(ViewGroup.LayoutParams p) { |
| 60 | return p instanceof InsettableFrameLayout.LayoutParams; |
| 61 | } |
| 62 | |
| 63 | @Override |
| 64 | protected LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) { |
| 65 | return new LayoutParams(p); |
| 66 | } |
| 67 | |
Adam Cohen | bc927f9 | 2014-10-28 16:16:02 -0700 | [diff] [blame] | 68 | public static class LayoutParams extends FrameLayout.LayoutParams { |
Adam Cohen | be25822 | 2014-10-24 16:45:59 -0700 | [diff] [blame] | 69 | boolean ignoreInsets = false; |
| 70 | |
| 71 | public LayoutParams(Context c, AttributeSet attrs) { |
| 72 | super(c, attrs); |
| 73 | TypedArray a = c.obtainStyledAttributes(attrs, |
| 74 | R.styleable.InsettableFrameLayout_Layout); |
| 75 | ignoreInsets = a.getBoolean( |
| 76 | R.styleable.InsettableFrameLayout_Layout_layout_ignoreInsets, false); |
| 77 | a.recycle(); |
| 78 | } |
| 79 | |
| 80 | public LayoutParams(int width, int height) { |
| 81 | super(width, height); |
| 82 | } |
| 83 | |
| 84 | public LayoutParams(ViewGroup.LayoutParams lp) { |
| 85 | super(lp); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | @Override |
Adam Cohen | a6d0492 | 2014-10-23 17:28:30 -0700 | [diff] [blame] | 90 | public void onChildViewAdded(View parent, View child) { |
| 91 | setFrameLayoutChildInsets(child, mInsets, new Rect()); |
| 92 | } |
| 93 | |
| 94 | @Override |
| 95 | public void onChildViewRemoved(View parent, View child) { |
| 96 | } |
| 97 | |
| 98 | } |