blob: faa18b805013993febdf98f325dd0aaa562facbf [file] [log] [blame]
Adam Cohena6d04922014-10-23 17:28:30 -07001package com.android.launcher3;
2
3import android.content.Context;
Adam Cohenbe258222014-10-24 16:45:59 -07004import android.content.res.TypedArray;
Adam Cohena6d04922014-10-23 17:28:30 -07005import android.graphics.Rect;
6import android.util.AttributeSet;
7import android.view.View;
Sunny Goyal4ffec482016-02-09 11:28:52 -08008import android.view.ViewDebug;
Adam Cohena6d04922014-10-23 17:28:30 -07009import android.view.ViewGroup;
10import android.widget.FrameLayout;
11
Jon Miranda758d5042017-11-08 14:38:23 -080012public class InsettableFrameLayout extends FrameLayout implements Insettable {
Adam Cohena6d04922014-10-23 17:28:30 -070013
Sunny Goyal4ffec482016-02-09 11:28:52 -080014 @ViewDebug.ExportedProperty(category = "launcher")
Adam Cohena6d04922014-10-23 17:28:30 -070015 protected Rect mInsets = new Rect();
16
Hyunyoung Songa9a8a422016-06-15 16:45:48 -070017 public Rect getInsets() {
18 return mInsets;
19 }
20
Adam Cohena6d04922014-10-23 17:28:30 -070021 public InsettableFrameLayout(Context context, AttributeSet attrs) {
22 super(context, attrs);
Adam Cohena6d04922014-10-23 17:28:30 -070023 }
24
25 public void setFrameLayoutChildInsets(View child, Rect newInsets, Rect oldInsets) {
Adam Cohenbe258222014-10-24 16:45:59 -070026 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
27
Adam Cohena6d04922014-10-23 17:28:30 -070028 if (child instanceof Insettable) {
29 ((Insettable) child).setInsets(newInsets);
Adam Cohenbe258222014-10-24 16:45:59 -070030 } 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 Cohena6d04922014-10-23 17:28:30 -070035 }
Adam Cohenbe258222014-10-24 16:45:59 -070036 child.setLayoutParams(lp);
Adam Cohena6d04922014-10-23 17:28:30 -070037 }
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 Cohenbe258222014-10-24 16:45:59 -070050 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 Cohenbc927f92014-10-28 16:16:02 -070070 public static class LayoutParams extends FrameLayout.LayoutParams {
Sunny Goyal1642f712018-09-18 11:40:35 -070071 public boolean ignoreInsets = false;
Adam Cohenbe258222014-10-24 16:45:59 -070072
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 Miranda758d5042017-11-08 14:38:23 -080092 public void onViewAdded(View child) {
93 super.onViewAdded(child);
Adam Cohena6d04922014-10-23 17:28:30 -070094 setFrameLayoutChildInsets(child, mInsets, new Rect());
95 }
Sunny Goyal07b69292018-01-08 14:19:34 -080096
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 Cohena6d04922014-10-23 17:28:30 -0700106}