Mindy Pereira | 2266b92 | 2010-11-23 13:00:58 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.preference; |
| 18 | |
Amith Yamasani | 3c9f519 | 2010-12-08 16:48:31 -0800 | [diff] [blame] | 19 | import android.app.FragmentBreadCrumbs; |
Mindy Pereira | 2266b92 | 2010-11-23 13:00:58 -0800 | [diff] [blame] | 20 | import android.content.Context; |
| 21 | import android.content.res.TypedArray; |
| 22 | import android.util.AttributeSet; |
| 23 | import android.view.View; |
| 24 | import android.widget.FrameLayout; |
| 25 | |
| 26 | /** |
| 27 | * @hide |
| 28 | */ |
| 29 | public class PreferenceFrameLayout extends FrameLayout { |
Mindy Pereira | 8b2fb60c | 2010-11-24 16:03:40 -0800 | [diff] [blame] | 30 | 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 Yamasani | 3c9f519 | 2010-12-08 16:48:31 -0800 | [diff] [blame] | 38 | private boolean mPaddingApplied; |
Mindy Pereira | 2266b92 | 2010-11-23 13:00:58 -0800 | [diff] [blame] | 39 | |
| 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 Pereira | 35c8be0 | 2010-11-24 14:04:04 -0800 | [diff] [blame] | 53 | float density = context.getResources().getDisplayMetrics().density; |
Mindy Pereira | 8b2fb60c | 2010-11-24 16:03:40 -0800 | [diff] [blame] | 54 | 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 Pereira | 35c8be0 | 2010-11-24 14:04:04 -0800 | [diff] [blame] | 58 | |
Mindy Pereira | 8b2fb60c | 2010-11-24 16:03:40 -0800 | [diff] [blame] | 59 | 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 Pereira | 35c8be0 | 2010-11-24 14:04:04 -0800 | [diff] [blame] | 64 | defaultBottomPadding); |
Mindy Pereira | 8b2fb60c | 2010-11-24 16:03:40 -0800 | [diff] [blame] | 65 | 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 Pereira | 35c8be0 | 2010-11-24 14:04:04 -0800 | [diff] [blame] | 71 | |
Mindy Pereira | 2266b92 | 2010-11-23 13:00:58 -0800 | [diff] [blame] | 72 | a.recycle(); |
| 73 | } |
| 74 | |
Mindy Pereira | 8b2fb60c | 2010-11-24 16:03:40 -0800 | [diff] [blame] | 75 | /** |
| 76 | * {@inheritDoc} |
| 77 | */ |
| 78 | @Override |
| 79 | public LayoutParams generateLayoutParams(AttributeSet attrs) { |
| 80 | return new LayoutParams(getContext(), attrs); |
| 81 | } |
| 82 | |
Mindy Pereira | 2266b92 | 2010-11-23 13:00:58 -0800 | [diff] [blame] | 83 | @Override |
| 84 | public void addView(View child) { |
Mindy Pereira | 8b2fb60c | 2010-11-24 16:03:40 -0800 | [diff] [blame] | 85 | int borderTop = getPaddingTop(); |
| 86 | int borderBottom = getPaddingBottom(); |
| 87 | int borderLeft = getPaddingLeft(); |
| 88 | int borderRight = getPaddingRight(); |
| 89 | |
Jim Miller | c57406c | 2010-12-08 16:01:05 -0800 | [diff] [blame] | 90 | android.view.ViewGroup.LayoutParams params = child.getLayoutParams(); |
| 91 | LayoutParams layoutParams = params instanceof PreferenceFrameLayout.LayoutParams |
| 92 | ? (PreferenceFrameLayout.LayoutParams) child.getLayoutParams() : null; |
Mindy Pereira | 2266b92 | 2010-11-23 13:00:58 -0800 | [diff] [blame] | 93 | // Check on the id of the child before adding it. |
Mindy Pereira | 8b2fb60c | 2010-11-24 16:03:40 -0800 | [diff] [blame] | 94 | if (layoutParams != null && layoutParams.removeBorders) { |
Mindy Pereira | 2266b92 | 2010-11-23 13:00:58 -0800 | [diff] [blame] | 95 | if (mPaddingApplied) { |
Mindy Pereira | 8b2fb60c | 2010-11-24 16:03:40 -0800 | [diff] [blame] | 96 | borderTop -= mBorderTop; |
| 97 | borderBottom -= mBorderBottom; |
| 98 | borderLeft -= mBorderLeft; |
| 99 | borderRight -= mBorderRight; |
Mindy Pereira | 2266b92 | 2010-11-23 13:00:58 -0800 | [diff] [blame] | 100 | mPaddingApplied = false; |
| 101 | } |
Mindy Pereira | 8b2fb60c | 2010-11-24 16:03:40 -0800 | [diff] [blame] | 102 | } 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 Pereira | 2266b92 | 2010-11-23 13:00:58 -0800 | [diff] [blame] | 112 | } |
Mindy Pereira | 8b2fb60c | 2010-11-24 16:03:40 -0800 | [diff] [blame] | 113 | |
Mindy Pereira | 2266b92 | 2010-11-23 13:00:58 -0800 | [diff] [blame] | 114 | int previousTop = getPaddingTop(); |
| 115 | int previousBottom = getPaddingBottom(); |
Mindy Pereira | 8b2fb60c | 2010-11-24 16:03:40 -0800 | [diff] [blame] | 116 | 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 Pereira | 2266b92 | 2010-11-23 13:00:58 -0800 | [diff] [blame] | 121 | } |
Mindy Pereira | 8b2fb60c | 2010-11-24 16:03:40 -0800 | [diff] [blame] | 122 | |
Mindy Pereira | 2266b92 | 2010-11-23 13:00:58 -0800 | [diff] [blame] | 123 | super.addView(child); |
| 124 | } |
Mindy Pereira | 8b2fb60c | 2010-11-24 16:03:40 -0800 | [diff] [blame] | 125 | |
| 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 Pereira | 2266b92 | 2010-11-23 13:00:58 -0800 | [diff] [blame] | 149 | } |