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