blob: 154641cabe2ded936fbf09d3a8e6a2880e7314c9 [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
Hyunyoung Song645764e2016-06-06 14:19:02 -070012import com.android.launcher3.allapps.AllAppsContainerView;
13import com.android.launcher3.config.FeatureFlags;
14
Adam Cohena6d04922014-10-23 17:28:30 -070015public class InsettableFrameLayout extends FrameLayout implements
16 ViewGroup.OnHierarchyChangeListener, Insettable {
17
Sunny Goyal4ffec482016-02-09 11:28:52 -080018 @ViewDebug.ExportedProperty(category = "launcher")
Adam Cohena6d04922014-10-23 17:28:30 -070019 protected Rect mInsets = new Rect();
20
Hyunyoung Songa9a8a422016-06-15 16:45:48 -070021 public Rect getInsets() {
22 return mInsets;
23 }
24
Adam Cohena6d04922014-10-23 17:28:30 -070025 public InsettableFrameLayout(Context context, AttributeSet attrs) {
26 super(context, attrs);
27 setOnHierarchyChangeListener(this);
28 }
29
30 public void setFrameLayoutChildInsets(View child, Rect newInsets, Rect oldInsets) {
Adam Cohenbe258222014-10-24 16:45:59 -070031 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
32
Adam Cohena6d04922014-10-23 17:28:30 -070033 if (child instanceof Insettable) {
34 ((Insettable) child).setInsets(newInsets);
Adam Cohenbe258222014-10-24 16:45:59 -070035 } else if (!lp.ignoreInsets) {
36 lp.topMargin += (newInsets.top - oldInsets.top);
37 lp.leftMargin += (newInsets.left - oldInsets.left);
38 lp.rightMargin += (newInsets.right - oldInsets.right);
39 lp.bottomMargin += (newInsets.bottom - oldInsets.bottom);
Adam Cohena6d04922014-10-23 17:28:30 -070040 }
Adam Cohenbe258222014-10-24 16:45:59 -070041 child.setLayoutParams(lp);
Adam Cohena6d04922014-10-23 17:28:30 -070042 }
43
44 @Override
45 public void setInsets(Rect insets) {
Adam Cohend1e0cec2016-07-01 16:55:30 -070046 // If the insets haven't changed, this is a no-op. Avoid unnecessary layout caused by
47 // modifying child layout params.
48 if (insets.equals(mInsets)) return;
49
Adam Cohena6d04922014-10-23 17:28:30 -070050 final int n = getChildCount();
51 for (int i = 0; i < n; i++) {
52 final View child = getChildAt(i);
53 setFrameLayoutChildInsets(child, insets, mInsets);
54 }
55 mInsets.set(insets);
56 }
57
58 @Override
Adam Cohenbe258222014-10-24 16:45:59 -070059 public LayoutParams generateLayoutParams(AttributeSet attrs) {
60 return new InsettableFrameLayout.LayoutParams(getContext(), attrs);
61 }
62
63 @Override
64 protected LayoutParams generateDefaultLayoutParams() {
65 return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
66 }
67
68 // Override to allow type-checking of LayoutParams.
69 @Override
70 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
71 return p instanceof InsettableFrameLayout.LayoutParams;
72 }
73
74 @Override
75 protected LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
76 return new LayoutParams(p);
77 }
78
Adam Cohenbc927f92014-10-28 16:16:02 -070079 public static class LayoutParams extends FrameLayout.LayoutParams {
Adam Cohenbe258222014-10-24 16:45:59 -070080 boolean ignoreInsets = false;
81
82 public LayoutParams(Context c, AttributeSet attrs) {
83 super(c, attrs);
84 TypedArray a = c.obtainStyledAttributes(attrs,
85 R.styleable.InsettableFrameLayout_Layout);
86 ignoreInsets = a.getBoolean(
87 R.styleable.InsettableFrameLayout_Layout_layout_ignoreInsets, false);
88 a.recycle();
89 }
90
91 public LayoutParams(int width, int height) {
92 super(width, height);
93 }
94
95 public LayoutParams(ViewGroup.LayoutParams lp) {
96 super(lp);
97 }
98 }
99
100 @Override
Adam Cohena6d04922014-10-23 17:28:30 -0700101 public void onChildViewAdded(View parent, View child) {
102 setFrameLayoutChildInsets(child, mInsets, new Rect());
103 }
104
105 @Override
106 public void onChildViewRemoved(View parent, View child) {
107 }
108
109}