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