blob: f4bfa4549eeb7a988e273509783088c55a698ae7 [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
18 public InsettableFrameLayout(Context context, AttributeSet attrs) {
19 super(context, attrs);
20 setOnHierarchyChangeListener(this);
21 }
22
23 public void setFrameLayoutChildInsets(View child, Rect newInsets, Rect oldInsets) {
Adam Cohenbe258222014-10-24 16:45:59 -070024 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
25
Adam Cohena6d04922014-10-23 17:28:30 -070026 if (child instanceof Insettable) {
27 ((Insettable) child).setInsets(newInsets);
Adam Cohenbe258222014-10-24 16:45:59 -070028 } else if (!lp.ignoreInsets) {
29 lp.topMargin += (newInsets.top - oldInsets.top);
30 lp.leftMargin += (newInsets.left - oldInsets.left);
31 lp.rightMargin += (newInsets.right - oldInsets.right);
32 lp.bottomMargin += (newInsets.bottom - oldInsets.bottom);
Adam Cohena6d04922014-10-23 17:28:30 -070033 }
Adam Cohenbe258222014-10-24 16:45:59 -070034 child.setLayoutParams(lp);
Adam Cohena6d04922014-10-23 17:28:30 -070035 }
36
37 @Override
38 public void setInsets(Rect insets) {
39 final int n = getChildCount();
40 for (int i = 0; i < n; i++) {
41 final View child = getChildAt(i);
42 setFrameLayoutChildInsets(child, insets, mInsets);
43 }
44 mInsets.set(insets);
45 }
46
47 @Override
Adam Cohenbe258222014-10-24 16:45:59 -070048 public LayoutParams generateLayoutParams(AttributeSet attrs) {
49 return new InsettableFrameLayout.LayoutParams(getContext(), attrs);
50 }
51
52 @Override
53 protected LayoutParams generateDefaultLayoutParams() {
54 return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
55 }
56
57 // Override to allow type-checking of LayoutParams.
58 @Override
59 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
60 return p instanceof InsettableFrameLayout.LayoutParams;
61 }
62
63 @Override
64 protected LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
65 return new LayoutParams(p);
66 }
67
Adam Cohenbc927f92014-10-28 16:16:02 -070068 public static class LayoutParams extends FrameLayout.LayoutParams {
Adam Cohenbe258222014-10-24 16:45:59 -070069 boolean ignoreInsets = false;
70
71 public LayoutParams(Context c, AttributeSet attrs) {
72 super(c, attrs);
73 TypedArray a = c.obtainStyledAttributes(attrs,
74 R.styleable.InsettableFrameLayout_Layout);
75 ignoreInsets = a.getBoolean(
76 R.styleable.InsettableFrameLayout_Layout_layout_ignoreInsets, false);
77 a.recycle();
78 }
79
80 public LayoutParams(int width, int height) {
81 super(width, height);
82 }
83
84 public LayoutParams(ViewGroup.LayoutParams lp) {
85 super(lp);
86 }
87 }
88
89 @Override
Adam Cohena6d04922014-10-23 17:28:30 -070090 public void onChildViewAdded(View parent, View child) {
91 setFrameLayoutChildInsets(child, mInsets, new Rect());
92 }
93
94 @Override
95 public void onChildViewRemoved(View parent, View child) {
96 }
97
98}