blob: 60f5ca29253799d7148b9d227d0cf2c3cadd58b7 [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) {
Adam Cohend1e0cec2016-07-01 16:55:30 -070041 // 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 Cohena6d04922014-10-23 17:28:30 -070045 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 Cohenbe258222014-10-24 16:45:59 -070054 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 Cohenbc927f92014-10-28 16:16:02 -070074 public static class LayoutParams extends FrameLayout.LayoutParams {
Adam Cohenbe258222014-10-24 16:45:59 -070075 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 Miranda758d5042017-11-08 14:38:23 -080096 public void onViewAdded(View child) {
97 super.onViewAdded(child);
Adam Cohena6d04922014-10-23 17:28:30 -070098 setFrameLayoutChildInsets(child, mInsets, new Rect());
99 }
Adam Cohena6d04922014-10-23 17:28:30 -0700100}