blob: be7649013ca23f3428905fde595e6bdd7a39d3f3 [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
12public class InsettableFrameLayout extends FrameLayout implements
13 ViewGroup.OnHierarchyChangeListener, Insettable {
14
Sunny Goyal4ffec482016-02-09 11:28:52 -080015 @ViewDebug.ExportedProperty(category = "launcher")
Adam Cohena6d04922014-10-23 17:28:30 -070016 protected Rect mInsets = new Rect();
17
Hyunyoung Songa9a8a422016-06-15 16:45:48 -070018 public Rect getInsets() {
19 return mInsets;
20 }
21
Adam Cohena6d04922014-10-23 17:28:30 -070022 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 Cohenbe258222014-10-24 16:45:59 -070028 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
29
Adam Cohena6d04922014-10-23 17:28:30 -070030 if (child instanceof Insettable) {
31 ((Insettable) child).setInsets(newInsets);
Adam Cohenbe258222014-10-24 16:45:59 -070032 } 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 Cohena6d04922014-10-23 17:28:30 -070037 }
Adam Cohenbe258222014-10-24 16:45:59 -070038 child.setLayoutParams(lp);
Adam Cohena6d04922014-10-23 17:28:30 -070039 }
40
41 @Override
42 public void setInsets(Rect insets) {
Adam Cohend1e0cec2016-07-01 16:55:30 -070043 // 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 Cohena6d04922014-10-23 17:28:30 -070047 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 Cohenbe258222014-10-24 16:45:59 -070056 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 Cohenbc927f92014-10-28 16:16:02 -070076 public static class LayoutParams extends FrameLayout.LayoutParams {
Adam Cohenbe258222014-10-24 16:45:59 -070077 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 Cohena6d04922014-10-23 17:28:30 -070098 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}