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) { |
Adam Cohen | d1e0cec | 2016-07-01 16:55:30 -0700 | [diff] [blame] | 41 | // If the insets haven't changed, this is a no-op. Avoid unnecessary layout caused by |
| 42 | // modifying child layout params. |
| 43 | if (insets.equals(mInsets)) return; |
| 44 | |
Adam Cohen | a6d0492 | 2014-10-23 17:28:30 -0700 | [diff] [blame] | 45 | final int n = getChildCount(); |
| 46 | for (int i = 0; i < n; i++) { |
| 47 | final View child = getChildAt(i); |
| 48 | setFrameLayoutChildInsets(child, insets, mInsets); |
| 49 | } |
| 50 | mInsets.set(insets); |
| 51 | } |
| 52 | |
| 53 | @Override |
Adam Cohen | be25822 | 2014-10-24 16:45:59 -0700 | [diff] [blame] | 54 | public LayoutParams generateLayoutParams(AttributeSet attrs) { |
| 55 | return new InsettableFrameLayout.LayoutParams(getContext(), attrs); |
| 56 | } |
| 57 | |
| 58 | @Override |
| 59 | protected LayoutParams generateDefaultLayoutParams() { |
| 60 | return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); |
| 61 | } |
| 62 | |
| 63 | // Override to allow type-checking of LayoutParams. |
| 64 | @Override |
| 65 | protected boolean checkLayoutParams(ViewGroup.LayoutParams p) { |
| 66 | return p instanceof InsettableFrameLayout.LayoutParams; |
| 67 | } |
| 68 | |
| 69 | @Override |
| 70 | protected LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) { |
| 71 | return new LayoutParams(p); |
| 72 | } |
| 73 | |
Adam Cohen | bc927f9 | 2014-10-28 16:16:02 -0700 | [diff] [blame] | 74 | public static class LayoutParams extends FrameLayout.LayoutParams { |
Adam Cohen | be25822 | 2014-10-24 16:45:59 -0700 | [diff] [blame] | 75 | boolean ignoreInsets = false; |
| 76 | |
| 77 | public LayoutParams(Context c, AttributeSet attrs) { |
| 78 | super(c, attrs); |
| 79 | TypedArray a = c.obtainStyledAttributes(attrs, |
| 80 | R.styleable.InsettableFrameLayout_Layout); |
| 81 | ignoreInsets = a.getBoolean( |
| 82 | R.styleable.InsettableFrameLayout_Layout_layout_ignoreInsets, false); |
| 83 | a.recycle(); |
| 84 | } |
| 85 | |
| 86 | public LayoutParams(int width, int height) { |
| 87 | super(width, height); |
| 88 | } |
| 89 | |
| 90 | public LayoutParams(ViewGroup.LayoutParams lp) { |
| 91 | super(lp); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | @Override |
Jon Miranda | 758d504 | 2017-11-08 14:38:23 -0800 | [diff] [blame] | 96 | public void onViewAdded(View child) { |
| 97 | super.onViewAdded(child); |
Adam Cohen | a6d0492 | 2014-10-23 17:28:30 -0700 | [diff] [blame] | 98 | setFrameLayoutChildInsets(child, mInsets, new Rect()); |
| 99 | } |
Adam Cohen | a6d0492 | 2014-10-23 17:28:30 -0700 | [diff] [blame] | 100 | } |