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 | |
| 19 | import android.content.Context; |
| 20 | import android.content.res.TypedArray; |
| 21 | import android.util.AttributeSet; |
| 22 | import android.view.View; |
| 23 | import android.widget.FrameLayout; |
| 24 | |
| 25 | /** |
| 26 | * @hide |
| 27 | */ |
| 28 | public class PreferenceFrameLayout extends FrameLayout { |
Mindy Pereira | 8b2fb60c | 2010-11-24 16:03:40 -0800 | [diff] [blame] | 29 | 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 Yamasani | 3c9f519 | 2010-12-08 16:48:31 -0800 | [diff] [blame] | 37 | private boolean mPaddingApplied; |
Mindy Pereira | 2266b92 | 2010-11-23 13:00:58 -0800 | [diff] [blame] | 38 | |
| 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 Viverette | 617feb9 | 2013-09-09 18:09:13 -0700 | [diff] [blame] | 47 | 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 Pereira | 2266b92 | 2010-11-23 13:00:58 -0800 | [diff] [blame] | 56 | |
Mindy Pereira | 35c8be0 | 2010-11-24 14:04:04 -0800 | [diff] [blame] | 57 | float density = context.getResources().getDisplayMetrics().density; |
Mindy Pereira | 8b2fb60c | 2010-11-24 16:03:40 -0800 | [diff] [blame] | 58 | 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 Pereira | 35c8be0 | 2010-11-24 14:04:04 -0800 | [diff] [blame] | 62 | |
Mindy Pereira | 8b2fb60c | 2010-11-24 16:03:40 -0800 | [diff] [blame] | 63 | 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 Pereira | 35c8be0 | 2010-11-24 14:04:04 -0800 | [diff] [blame] | 68 | defaultBottomPadding); |
Mindy Pereira | 8b2fb60c | 2010-11-24 16:03:40 -0800 | [diff] [blame] | 69 | 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 Pereira | 35c8be0 | 2010-11-24 14:04:04 -0800 | [diff] [blame] | 75 | |
Mindy Pereira | 2266b92 | 2010-11-23 13:00:58 -0800 | [diff] [blame] | 76 | a.recycle(); |
| 77 | } |
| 78 | |
Mindy Pereira | 8b2fb60c | 2010-11-24 16:03:40 -0800 | [diff] [blame] | 79 | /** |
| 80 | * {@inheritDoc} |
| 81 | */ |
| 82 | @Override |
| 83 | public LayoutParams generateLayoutParams(AttributeSet attrs) { |
| 84 | return new LayoutParams(getContext(), attrs); |
| 85 | } |
| 86 | |
Mindy Pereira | 2266b92 | 2010-11-23 13:00:58 -0800 | [diff] [blame] | 87 | @Override |
| 88 | public void addView(View child) { |
Mindy Pereira | 8b2fb60c | 2010-11-24 16:03:40 -0800 | [diff] [blame] | 89 | int borderTop = getPaddingTop(); |
| 90 | int borderBottom = getPaddingBottom(); |
| 91 | int borderLeft = getPaddingLeft(); |
| 92 | int borderRight = getPaddingRight(); |
| 93 | |
Jim Miller | c57406c | 2010-12-08 16:01:05 -0800 | [diff] [blame] | 94 | android.view.ViewGroup.LayoutParams params = child.getLayoutParams(); |
| 95 | LayoutParams layoutParams = params instanceof PreferenceFrameLayout.LayoutParams |
| 96 | ? (PreferenceFrameLayout.LayoutParams) child.getLayoutParams() : null; |
Mindy Pereira | 2266b92 | 2010-11-23 13:00:58 -0800 | [diff] [blame] | 97 | // Check on the id of the child before adding it. |
Mindy Pereira | 8b2fb60c | 2010-11-24 16:03:40 -0800 | [diff] [blame] | 98 | if (layoutParams != null && layoutParams.removeBorders) { |
Mindy Pereira | 2266b92 | 2010-11-23 13:00:58 -0800 | [diff] [blame] | 99 | if (mPaddingApplied) { |
Mindy Pereira | 8b2fb60c | 2010-11-24 16:03:40 -0800 | [diff] [blame] | 100 | borderTop -= mBorderTop; |
| 101 | borderBottom -= mBorderBottom; |
| 102 | borderLeft -= mBorderLeft; |
| 103 | borderRight -= mBorderRight; |
Mindy Pereira | 2266b92 | 2010-11-23 13:00:58 -0800 | [diff] [blame] | 104 | mPaddingApplied = false; |
| 105 | } |
Mindy Pereira | 8b2fb60c | 2010-11-24 16:03:40 -0800 | [diff] [blame] | 106 | } 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 Pereira | 2266b92 | 2010-11-23 13:00:58 -0800 | [diff] [blame] | 116 | } |
Mindy Pereira | 8b2fb60c | 2010-11-24 16:03:40 -0800 | [diff] [blame] | 117 | |
Mindy Pereira | 2266b92 | 2010-11-23 13:00:58 -0800 | [diff] [blame] | 118 | int previousTop = getPaddingTop(); |
| 119 | int previousBottom = getPaddingBottom(); |
Mindy Pereira | 8b2fb60c | 2010-11-24 16:03:40 -0800 | [diff] [blame] | 120 | 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 Pereira | 2266b92 | 2010-11-23 13:00:58 -0800 | [diff] [blame] | 125 | } |
Mindy Pereira | 8b2fb60c | 2010-11-24 16:03:40 -0800 | [diff] [blame] | 126 | |
Mindy Pereira | 2266b92 | 2010-11-23 13:00:58 -0800 | [diff] [blame] | 127 | super.addView(child); |
| 128 | } |
Mindy Pereira | 8b2fb60c | 2010-11-24 16:03:40 -0800 | [diff] [blame] | 129 | |
| 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 Pereira | 2266b92 | 2010-11-23 13:00:58 -0800 | [diff] [blame] | 153 | } |