blob: 886338f04c4f40f6651a3cd389105b0d5c6573aa [file] [log] [blame]
Mindy Pereira2266b922010-11-23 13:00:58 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.preference;
18
19import android.content.Context;
20import android.content.res.TypedArray;
21import android.util.AttributeSet;
22import android.view.View;
23import android.widget.FrameLayout;
24
25/**
26 * @hide
27 */
28public class PreferenceFrameLayout extends FrameLayout {
Mindy Pereira8b2fb60c2010-11-24 16:03:40 -080029 private static final int DEFAULT_BORDER_TOP = 0;
30 private static final int DEFAULT_BORDER_BOTTOM = 0;
31 private static final int DEFAULT_BORDER_LEFT = 0;
32 private static final int DEFAULT_BORDER_RIGHT = 0;
33 private final int mBorderTop;
34 private final int mBorderBottom;
35 private final int mBorderLeft;
36 private final int mBorderRight;
Amith Yamasani3c9f5192010-12-08 16:48:31 -080037 private boolean mPaddingApplied;
Mindy Pereira2266b922010-11-23 13:00:58 -080038
39 public PreferenceFrameLayout(Context context) {
40 this(context, null);
41 }
42
43 public PreferenceFrameLayout(Context context, AttributeSet attrs) {
44 this(context, attrs, com.android.internal.R.attr.preferenceFrameLayoutStyle);
45 }
46
Alan Viverette617feb92013-09-09 18:09:13 -070047 public PreferenceFrameLayout(Context context, AttributeSet attrs, int defStyleAttr) {
48 this(context, attrs, defStyleAttr, 0);
49 }
50
51 public PreferenceFrameLayout(
52 Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
53 super(context, attrs, defStyleAttr, defStyleRes);
54 final TypedArray a = context.obtainStyledAttributes(attrs,
55 com.android.internal.R.styleable.PreferenceFrameLayout, defStyleAttr, defStyleRes);
Mindy Pereira2266b922010-11-23 13:00:58 -080056
Mindy Pereira35c8be02010-11-24 14:04:04 -080057 float density = context.getResources().getDisplayMetrics().density;
Mindy Pereira8b2fb60c2010-11-24 16:03:40 -080058 int defaultBorderTop = (int) (density * DEFAULT_BORDER_TOP + 0.5f);
59 int defaultBottomPadding = (int) (density * DEFAULT_BORDER_BOTTOM + 0.5f);
60 int defaultLeftPadding = (int) (density * DEFAULT_BORDER_LEFT + 0.5f);
61 int defaultRightPadding = (int) (density * DEFAULT_BORDER_RIGHT + 0.5f);
Mindy Pereira35c8be02010-11-24 14:04:04 -080062
Mindy Pereira8b2fb60c2010-11-24 16:03:40 -080063 mBorderTop = a.getDimensionPixelSize(
64 com.android.internal.R.styleable.PreferenceFrameLayout_borderTop,
65 defaultBorderTop);
66 mBorderBottom = a.getDimensionPixelSize(
67 com.android.internal.R.styleable.PreferenceFrameLayout_borderBottom,
Mindy Pereira35c8be02010-11-24 14:04:04 -080068 defaultBottomPadding);
Mindy Pereira8b2fb60c2010-11-24 16:03:40 -080069 mBorderLeft = a.getDimensionPixelSize(
70 com.android.internal.R.styleable.PreferenceFrameLayout_borderLeft,
71 defaultLeftPadding);
72 mBorderRight = a.getDimensionPixelSize(
73 com.android.internal.R.styleable.PreferenceFrameLayout_borderRight,
74 defaultRightPadding);
Mindy Pereira35c8be02010-11-24 14:04:04 -080075
Mindy Pereira2266b922010-11-23 13:00:58 -080076 a.recycle();
77 }
78
Mindy Pereira8b2fb60c2010-11-24 16:03:40 -080079 /**
80 * {@inheritDoc}
81 */
82 @Override
83 public LayoutParams generateLayoutParams(AttributeSet attrs) {
84 return new LayoutParams(getContext(), attrs);
85 }
86
Mindy Pereira2266b922010-11-23 13:00:58 -080087 @Override
88 public void addView(View child) {
Mindy Pereira8b2fb60c2010-11-24 16:03:40 -080089 int borderTop = getPaddingTop();
90 int borderBottom = getPaddingBottom();
91 int borderLeft = getPaddingLeft();
92 int borderRight = getPaddingRight();
93
Jim Millerc57406c2010-12-08 16:01:05 -080094 android.view.ViewGroup.LayoutParams params = child.getLayoutParams();
95 LayoutParams layoutParams = params instanceof PreferenceFrameLayout.LayoutParams
96 ? (PreferenceFrameLayout.LayoutParams) child.getLayoutParams() : null;
Mindy Pereira2266b922010-11-23 13:00:58 -080097 // Check on the id of the child before adding it.
Mindy Pereira8b2fb60c2010-11-24 16:03:40 -080098 if (layoutParams != null && layoutParams.removeBorders) {
Mindy Pereira2266b922010-11-23 13:00:58 -080099 if (mPaddingApplied) {
Mindy Pereira8b2fb60c2010-11-24 16:03:40 -0800100 borderTop -= mBorderTop;
101 borderBottom -= mBorderBottom;
102 borderLeft -= mBorderLeft;
103 borderRight -= mBorderRight;
Mindy Pereira2266b922010-11-23 13:00:58 -0800104 mPaddingApplied = false;
105 }
Mindy Pereira8b2fb60c2010-11-24 16:03:40 -0800106 } else {
107 // Add the padding to the view group after determining if the
108 // padding already exists.
109 if (!mPaddingApplied) {
110 borderTop += mBorderTop;
111 borderBottom += mBorderBottom;
112 borderLeft += mBorderLeft;
113 borderRight += mBorderRight;
114 mPaddingApplied = true;
115 }
Mindy Pereira2266b922010-11-23 13:00:58 -0800116 }
Mindy Pereira8b2fb60c2010-11-24 16:03:40 -0800117
Mindy Pereira2266b922010-11-23 13:00:58 -0800118 int previousTop = getPaddingTop();
119 int previousBottom = getPaddingBottom();
Mindy Pereira8b2fb60c2010-11-24 16:03:40 -0800120 int previousLeft = getPaddingLeft();
121 int previousRight = getPaddingRight();
122 if (previousTop != borderTop || previousBottom != borderBottom
123 || previousLeft != borderLeft || previousRight != borderRight) {
124 setPadding(borderLeft, borderTop, borderRight, borderBottom);
Mindy Pereira2266b922010-11-23 13:00:58 -0800125 }
Mindy Pereira8b2fb60c2010-11-24 16:03:40 -0800126
Mindy Pereira2266b922010-11-23 13:00:58 -0800127 super.addView(child);
128 }
Mindy Pereira8b2fb60c2010-11-24 16:03:40 -0800129
130 public static class LayoutParams extends FrameLayout.LayoutParams {
131 public boolean removeBorders = false;
132 /**
133 * {@inheritDoc}
134 */
135 public LayoutParams(Context c, AttributeSet attrs) {
136 super(c, attrs);
137
138 TypedArray a = c.obtainStyledAttributes(attrs,
139 com.android.internal.R.styleable.PreferenceFrameLayout_Layout);
140 removeBorders = a.getBoolean(
141 com.android.internal.R.styleable.PreferenceFrameLayout_Layout_layout_removeBorders,
142 false);
143 a.recycle();
144 }
145
146 /**
147 * {@inheritDoc}
148 */
149 public LayoutParams(int width, int height) {
150 super(width, height);
151 }
152 }
Mindy Pereira2266b922010-11-23 13:00:58 -0800153}