blob: db4d85516fcb824bfe65e4c92cf0357047735c2c [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) {
46 final int n = getChildCount();
47 for (int i = 0; i < n; i++) {
48 final View child = getChildAt(i);
49 setFrameLayoutChildInsets(child, insets, mInsets);
50 }
51 mInsets.set(insets);
52 }
53
54 @Override
Adam Cohenbe258222014-10-24 16:45:59 -070055 public LayoutParams generateLayoutParams(AttributeSet attrs) {
56 return new InsettableFrameLayout.LayoutParams(getContext(), attrs);
57 }
58
59 @Override
60 protected LayoutParams generateDefaultLayoutParams() {
61 return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
62 }
63
64 // Override to allow type-checking of LayoutParams.
65 @Override
66 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
67 return p instanceof InsettableFrameLayout.LayoutParams;
68 }
69
70 @Override
71 protected LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
72 return new LayoutParams(p);
73 }
74
Adam Cohenbc927f92014-10-28 16:16:02 -070075 public static class LayoutParams extends FrameLayout.LayoutParams {
Adam Cohenbe258222014-10-24 16:45:59 -070076 boolean ignoreInsets = false;
77
78 public LayoutParams(Context c, AttributeSet attrs) {
79 super(c, attrs);
80 TypedArray a = c.obtainStyledAttributes(attrs,
81 R.styleable.InsettableFrameLayout_Layout);
82 ignoreInsets = a.getBoolean(
83 R.styleable.InsettableFrameLayout_Layout_layout_ignoreInsets, false);
84 a.recycle();
85 }
86
87 public LayoutParams(int width, int height) {
88 super(width, height);
89 }
90
91 public LayoutParams(ViewGroup.LayoutParams lp) {
92 super(lp);
93 }
94 }
95
96 @Override
Adam Cohena6d04922014-10-23 17:28:30 -070097 public void onChildViewAdded(View parent, View child) {
98 setFrameLayoutChildInsets(child, mInsets, new Rect());
99 }
100
101 @Override
102 public void onChildViewRemoved(View parent, View child) {
103 }
104
105}