blob: 61edc0f7f6f6961f6a49b928482fe67d085261c9 [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
21 public InsettableFrameLayout(Context context, AttributeSet attrs) {
22 super(context, attrs);
23 setOnHierarchyChangeListener(this);
24 }
25
26 public void setFrameLayoutChildInsets(View child, Rect newInsets, Rect oldInsets) {
Adam Cohenbe258222014-10-24 16:45:59 -070027 final LayoutParams lp = (LayoutParams) child.getLayoutParams();
28
Adam Cohena6d04922014-10-23 17:28:30 -070029 if (child instanceof Insettable) {
30 ((Insettable) child).setInsets(newInsets);
Adam Cohenbe258222014-10-24 16:45:59 -070031 } else if (!lp.ignoreInsets) {
32 lp.topMargin += (newInsets.top - oldInsets.top);
33 lp.leftMargin += (newInsets.left - oldInsets.left);
34 lp.rightMargin += (newInsets.right - oldInsets.right);
35 lp.bottomMargin += (newInsets.bottom - oldInsets.bottom);
Adam Cohena6d04922014-10-23 17:28:30 -070036 }
Hyunyoung Song645764e2016-06-06 14:19:02 -070037 if (FeatureFlags.LAUNCHER3_ALL_APPS_PULL_UP && child instanceof AllAppsContainerView) {
38 lp.setMargins(0, 0, 0, lp.bottomMargin);
39 }
Adam Cohenbe258222014-10-24 16:45:59 -070040 child.setLayoutParams(lp);
Adam Cohena6d04922014-10-23 17:28:30 -070041 }
42
43 @Override
44 public void setInsets(Rect insets) {
45 final int n = getChildCount();
46 for (int i = 0; i < n; i++) {
47 final View child = getChildAt(i);
48 setFrameLayoutChildInsets(child, insets, mInsets);
49 }
50 mInsets.set(insets);
51 }
52
53 @Override
Adam Cohenbe258222014-10-24 16:45:59 -070054 public LayoutParams generateLayoutParams(AttributeSet attrs) {
55 return new InsettableFrameLayout.LayoutParams(getContext(), attrs);
56 }
57
58 @Override
59 protected LayoutParams generateDefaultLayoutParams() {
60 return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
61 }
62
63 // Override to allow type-checking of LayoutParams.
64 @Override
65 protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
66 return p instanceof InsettableFrameLayout.LayoutParams;
67 }
68
69 @Override
70 protected LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
71 return new LayoutParams(p);
72 }
73
Adam Cohenbc927f92014-10-28 16:16:02 -070074 public static class LayoutParams extends FrameLayout.LayoutParams {
Adam Cohenbe258222014-10-24 16:45:59 -070075 boolean ignoreInsets = false;
76
77 public LayoutParams(Context c, AttributeSet attrs) {
78 super(c, attrs);
79 TypedArray a = c.obtainStyledAttributes(attrs,
80 R.styleable.InsettableFrameLayout_Layout);
81 ignoreInsets = a.getBoolean(
82 R.styleable.InsettableFrameLayout_Layout_layout_ignoreInsets, false);
83 a.recycle();
84 }
85
86 public LayoutParams(int width, int height) {
87 super(width, height);
88 }
89
90 public LayoutParams(ViewGroup.LayoutParams lp) {
91 super(lp);
92 }
93 }
94
95 @Override
Adam Cohena6d04922014-10-23 17:28:30 -070096 public void onChildViewAdded(View parent, View child) {
97 setFrameLayoutChildInsets(child, mInsets, new Rect());
98 }
99
100 @Override
101 public void onChildViewRemoved(View parent, View child) {
102 }
103
104}