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