blob: 75372aa4a3b3e6c6d469d11a9dcae6f2d6784190 [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
Amith Yamasani3c9f5192010-12-08 16:48:31 -080019import android.app.FragmentBreadCrumbs;
Mindy Pereira2266b922010-11-23 13:00:58 -080020import android.content.Context;
21import android.content.res.TypedArray;
22import android.util.AttributeSet;
23import android.view.View;
24import android.widget.FrameLayout;
25
26/**
27 * @hide
28 */
29public class PreferenceFrameLayout extends FrameLayout {
Mindy Pereira8b2fb60c2010-11-24 16:03:40 -080030 private static final int DEFAULT_BORDER_TOP = 0;
31 private static final int DEFAULT_BORDER_BOTTOM = 0;
32 private static final int DEFAULT_BORDER_LEFT = 0;
33 private static final int DEFAULT_BORDER_RIGHT = 0;
34 private final int mBorderTop;
35 private final int mBorderBottom;
36 private final int mBorderLeft;
37 private final int mBorderRight;
Amith Yamasani3c9f5192010-12-08 16:48:31 -080038 private boolean mPaddingApplied;
Mindy Pereira2266b922010-11-23 13:00:58 -080039
40 public PreferenceFrameLayout(Context context) {
41 this(context, null);
42 }
43
44 public PreferenceFrameLayout(Context context, AttributeSet attrs) {
45 this(context, attrs, com.android.internal.R.attr.preferenceFrameLayoutStyle);
46 }
47
48 public PreferenceFrameLayout(Context context, AttributeSet attrs, int defStyle) {
49 super(context, attrs, defStyle);
50 TypedArray a = context.obtainStyledAttributes(attrs,
51 com.android.internal.R.styleable.PreferenceFrameLayout, defStyle, 0);
52
Mindy Pereira35c8be02010-11-24 14:04:04 -080053 float density = context.getResources().getDisplayMetrics().density;
Mindy Pereira8b2fb60c2010-11-24 16:03:40 -080054 int defaultBorderTop = (int) (density * DEFAULT_BORDER_TOP + 0.5f);
55 int defaultBottomPadding = (int) (density * DEFAULT_BORDER_BOTTOM + 0.5f);
56 int defaultLeftPadding = (int) (density * DEFAULT_BORDER_LEFT + 0.5f);
57 int defaultRightPadding = (int) (density * DEFAULT_BORDER_RIGHT + 0.5f);
Mindy Pereira35c8be02010-11-24 14:04:04 -080058
Mindy Pereira8b2fb60c2010-11-24 16:03:40 -080059 mBorderTop = a.getDimensionPixelSize(
60 com.android.internal.R.styleable.PreferenceFrameLayout_borderTop,
61 defaultBorderTop);
62 mBorderBottom = a.getDimensionPixelSize(
63 com.android.internal.R.styleable.PreferenceFrameLayout_borderBottom,
Mindy Pereira35c8be02010-11-24 14:04:04 -080064 defaultBottomPadding);
Mindy Pereira8b2fb60c2010-11-24 16:03:40 -080065 mBorderLeft = a.getDimensionPixelSize(
66 com.android.internal.R.styleable.PreferenceFrameLayout_borderLeft,
67 defaultLeftPadding);
68 mBorderRight = a.getDimensionPixelSize(
69 com.android.internal.R.styleable.PreferenceFrameLayout_borderRight,
70 defaultRightPadding);
Mindy Pereira35c8be02010-11-24 14:04:04 -080071
Mindy Pereira2266b922010-11-23 13:00:58 -080072 a.recycle();
73 }
74
Mindy Pereira8b2fb60c2010-11-24 16:03:40 -080075 /**
76 * {@inheritDoc}
77 */
78 @Override
79 public LayoutParams generateLayoutParams(AttributeSet attrs) {
80 return new LayoutParams(getContext(), attrs);
81 }
82
Mindy Pereira2266b922010-11-23 13:00:58 -080083 @Override
84 public void addView(View child) {
Mindy Pereira8b2fb60c2010-11-24 16:03:40 -080085 int borderTop = getPaddingTop();
86 int borderBottom = getPaddingBottom();
87 int borderLeft = getPaddingLeft();
88 int borderRight = getPaddingRight();
89
Jim Millerc57406c2010-12-08 16:01:05 -080090 android.view.ViewGroup.LayoutParams params = child.getLayoutParams();
91 LayoutParams layoutParams = params instanceof PreferenceFrameLayout.LayoutParams
92 ? (PreferenceFrameLayout.LayoutParams) child.getLayoutParams() : null;
Mindy Pereira2266b922010-11-23 13:00:58 -080093 // Check on the id of the child before adding it.
Mindy Pereira8b2fb60c2010-11-24 16:03:40 -080094 if (layoutParams != null && layoutParams.removeBorders) {
Mindy Pereira2266b922010-11-23 13:00:58 -080095 if (mPaddingApplied) {
Mindy Pereira8b2fb60c2010-11-24 16:03:40 -080096 borderTop -= mBorderTop;
97 borderBottom -= mBorderBottom;
98 borderLeft -= mBorderLeft;
99 borderRight -= mBorderRight;
Mindy Pereira2266b922010-11-23 13:00:58 -0800100 mPaddingApplied = false;
101 }
Mindy Pereira8b2fb60c2010-11-24 16:03:40 -0800102 } else {
103 // Add the padding to the view group after determining if the
104 // padding already exists.
105 if (!mPaddingApplied) {
106 borderTop += mBorderTop;
107 borderBottom += mBorderBottom;
108 borderLeft += mBorderLeft;
109 borderRight += mBorderRight;
110 mPaddingApplied = true;
111 }
Mindy Pereira2266b922010-11-23 13:00:58 -0800112 }
Mindy Pereira8b2fb60c2010-11-24 16:03:40 -0800113
Mindy Pereira2266b922010-11-23 13:00:58 -0800114 int previousTop = getPaddingTop();
115 int previousBottom = getPaddingBottom();
Mindy Pereira8b2fb60c2010-11-24 16:03:40 -0800116 int previousLeft = getPaddingLeft();
117 int previousRight = getPaddingRight();
118 if (previousTop != borderTop || previousBottom != borderBottom
119 || previousLeft != borderLeft || previousRight != borderRight) {
120 setPadding(borderLeft, borderTop, borderRight, borderBottom);
Mindy Pereira2266b922010-11-23 13:00:58 -0800121 }
Mindy Pereira8b2fb60c2010-11-24 16:03:40 -0800122
Mindy Pereira2266b922010-11-23 13:00:58 -0800123 super.addView(child);
124 }
Mindy Pereira8b2fb60c2010-11-24 16:03:40 -0800125
126 public static class LayoutParams extends FrameLayout.LayoutParams {
127 public boolean removeBorders = false;
128 /**
129 * {@inheritDoc}
130 */
131 public LayoutParams(Context c, AttributeSet attrs) {
132 super(c, attrs);
133
134 TypedArray a = c.obtainStyledAttributes(attrs,
135 com.android.internal.R.styleable.PreferenceFrameLayout_Layout);
136 removeBorders = a.getBoolean(
137 com.android.internal.R.styleable.PreferenceFrameLayout_Layout_layout_removeBorders,
138 false);
139 a.recycle();
140 }
141
142 /**
143 * {@inheritDoc}
144 */
145 public LayoutParams(int width, int height) {
146 super(width, height);
147 }
148 }
Mindy Pereira2266b922010-11-23 13:00:58 -0800149}