12266b92072b2eda16ef71ed6f3d8811159b756deMindy Pereira/*
22266b92072b2eda16ef71ed6f3d8811159b756deMindy Pereira * Copyright (C) 2010 The Android Open Source Project
32266b92072b2eda16ef71ed6f3d8811159b756deMindy Pereira *
42266b92072b2eda16ef71ed6f3d8811159b756deMindy Pereira * Licensed under the Apache License, Version 2.0 (the "License");
52266b92072b2eda16ef71ed6f3d8811159b756deMindy Pereira * you may not use this file except in compliance with the License.
62266b92072b2eda16ef71ed6f3d8811159b756deMindy Pereira * You may obtain a copy of the License at
72266b92072b2eda16ef71ed6f3d8811159b756deMindy Pereira *
82266b92072b2eda16ef71ed6f3d8811159b756deMindy Pereira *      http://www.apache.org/licenses/LICENSE-2.0
92266b92072b2eda16ef71ed6f3d8811159b756deMindy Pereira *
102266b92072b2eda16ef71ed6f3d8811159b756deMindy Pereira * Unless required by applicable law or agreed to in writing, software
112266b92072b2eda16ef71ed6f3d8811159b756deMindy Pereira * distributed under the License is distributed on an "AS IS" BASIS,
122266b92072b2eda16ef71ed6f3d8811159b756deMindy Pereira * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
132266b92072b2eda16ef71ed6f3d8811159b756deMindy Pereira * See the License for the specific language governing permissions and
142266b92072b2eda16ef71ed6f3d8811159b756deMindy Pereira * limitations under the License.
152266b92072b2eda16ef71ed6f3d8811159b756deMindy Pereira */
162266b92072b2eda16ef71ed6f3d8811159b756deMindy Pereira
172266b92072b2eda16ef71ed6f3d8811159b756deMindy Pereirapackage android.preference;
182266b92072b2eda16ef71ed6f3d8811159b756deMindy Pereira
193c9f519f898f8fc809550199ea823c0225c682a8Amith Yamasaniimport android.app.FragmentBreadCrumbs;
202266b92072b2eda16ef71ed6f3d8811159b756deMindy Pereiraimport android.content.Context;
212266b92072b2eda16ef71ed6f3d8811159b756deMindy Pereiraimport android.content.res.TypedArray;
222266b92072b2eda16ef71ed6f3d8811159b756deMindy Pereiraimport android.util.AttributeSet;
232266b92072b2eda16ef71ed6f3d8811159b756deMindy Pereiraimport android.view.View;
242266b92072b2eda16ef71ed6f3d8811159b756deMindy Pereiraimport android.widget.FrameLayout;
252266b92072b2eda16ef71ed6f3d8811159b756deMindy Pereira
262266b92072b2eda16ef71ed6f3d8811159b756deMindy Pereira/**
272266b92072b2eda16ef71ed6f3d8811159b756deMindy Pereira * @hide
282266b92072b2eda16ef71ed6f3d8811159b756deMindy Pereira */
292266b92072b2eda16ef71ed6f3d8811159b756deMindy Pereirapublic class PreferenceFrameLayout extends FrameLayout {
308b2fb60cd49426c77869ddd91cbf4130d337a41cMindy Pereira    private static final int DEFAULT_BORDER_TOP = 0;
318b2fb60cd49426c77869ddd91cbf4130d337a41cMindy Pereira    private static final int DEFAULT_BORDER_BOTTOM = 0;
328b2fb60cd49426c77869ddd91cbf4130d337a41cMindy Pereira    private static final int DEFAULT_BORDER_LEFT = 0;
338b2fb60cd49426c77869ddd91cbf4130d337a41cMindy Pereira    private static final int DEFAULT_BORDER_RIGHT = 0;
348b2fb60cd49426c77869ddd91cbf4130d337a41cMindy Pereira    private final int mBorderTop;
358b2fb60cd49426c77869ddd91cbf4130d337a41cMindy Pereira    private final int mBorderBottom;
368b2fb60cd49426c77869ddd91cbf4130d337a41cMindy Pereira    private final int mBorderLeft;
378b2fb60cd49426c77869ddd91cbf4130d337a41cMindy Pereira    private final int mBorderRight;
383c9f519f898f8fc809550199ea823c0225c682a8Amith Yamasani    private boolean mPaddingApplied;
392266b92072b2eda16ef71ed6f3d8811159b756deMindy Pereira
402266b92072b2eda16ef71ed6f3d8811159b756deMindy Pereira    public PreferenceFrameLayout(Context context) {
412266b92072b2eda16ef71ed6f3d8811159b756deMindy Pereira        this(context, null);
422266b92072b2eda16ef71ed6f3d8811159b756deMindy Pereira    }
432266b92072b2eda16ef71ed6f3d8811159b756deMindy Pereira
442266b92072b2eda16ef71ed6f3d8811159b756deMindy Pereira    public PreferenceFrameLayout(Context context, AttributeSet attrs) {
452266b92072b2eda16ef71ed6f3d8811159b756deMindy Pereira        this(context, attrs, com.android.internal.R.attr.preferenceFrameLayoutStyle);
462266b92072b2eda16ef71ed6f3d8811159b756deMindy Pereira    }
472266b92072b2eda16ef71ed6f3d8811159b756deMindy Pereira
482266b92072b2eda16ef71ed6f3d8811159b756deMindy Pereira    public PreferenceFrameLayout(Context context, AttributeSet attrs, int defStyle) {
492266b92072b2eda16ef71ed6f3d8811159b756deMindy Pereira        super(context, attrs, defStyle);
502266b92072b2eda16ef71ed6f3d8811159b756deMindy Pereira        TypedArray a = context.obtainStyledAttributes(attrs,
512266b92072b2eda16ef71ed6f3d8811159b756deMindy Pereira                com.android.internal.R.styleable.PreferenceFrameLayout, defStyle, 0);
522266b92072b2eda16ef71ed6f3d8811159b756deMindy Pereira
5335c8be065541fc818cc06567acd3c763b5f86056Mindy Pereira        float density = context.getResources().getDisplayMetrics().density;
548b2fb60cd49426c77869ddd91cbf4130d337a41cMindy Pereira        int defaultBorderTop = (int) (density * DEFAULT_BORDER_TOP + 0.5f);
558b2fb60cd49426c77869ddd91cbf4130d337a41cMindy Pereira        int defaultBottomPadding = (int) (density * DEFAULT_BORDER_BOTTOM + 0.5f);
568b2fb60cd49426c77869ddd91cbf4130d337a41cMindy Pereira        int defaultLeftPadding = (int) (density * DEFAULT_BORDER_LEFT + 0.5f);
578b2fb60cd49426c77869ddd91cbf4130d337a41cMindy Pereira        int defaultRightPadding = (int) (density * DEFAULT_BORDER_RIGHT + 0.5f);
5835c8be065541fc818cc06567acd3c763b5f86056Mindy Pereira
598b2fb60cd49426c77869ddd91cbf4130d337a41cMindy Pereira        mBorderTop = a.getDimensionPixelSize(
608b2fb60cd49426c77869ddd91cbf4130d337a41cMindy Pereira                com.android.internal.R.styleable.PreferenceFrameLayout_borderTop,
618b2fb60cd49426c77869ddd91cbf4130d337a41cMindy Pereira                defaultBorderTop);
628b2fb60cd49426c77869ddd91cbf4130d337a41cMindy Pereira        mBorderBottom = a.getDimensionPixelSize(
638b2fb60cd49426c77869ddd91cbf4130d337a41cMindy Pereira                com.android.internal.R.styleable.PreferenceFrameLayout_borderBottom,
648b2fb60cd49426c77869ddd91cbf4130d337a41cMindy Pereira                defaultBottomPadding);
658b2fb60cd49426c77869ddd91cbf4130d337a41cMindy Pereira        mBorderLeft = a.getDimensionPixelSize(
668b2fb60cd49426c77869ddd91cbf4130d337a41cMindy Pereira                com.android.internal.R.styleable.PreferenceFrameLayout_borderLeft,
678b2fb60cd49426c77869ddd91cbf4130d337a41cMindy Pereira                defaultLeftPadding);
688b2fb60cd49426c77869ddd91cbf4130d337a41cMindy Pereira        mBorderRight = a.getDimensionPixelSize(
698b2fb60cd49426c77869ddd91cbf4130d337a41cMindy Pereira                com.android.internal.R.styleable.PreferenceFrameLayout_borderRight,
708b2fb60cd49426c77869ddd91cbf4130d337a41cMindy Pereira                defaultRightPadding);
7135c8be065541fc818cc06567acd3c763b5f86056Mindy Pereira
722266b92072b2eda16ef71ed6f3d8811159b756deMindy Pereira        a.recycle();
732266b92072b2eda16ef71ed6f3d8811159b756deMindy Pereira    }
742266b92072b2eda16ef71ed6f3d8811159b756deMindy Pereira
758b2fb60cd49426c77869ddd91cbf4130d337a41cMindy Pereira    /**
768b2fb60cd49426c77869ddd91cbf4130d337a41cMindy Pereira     * {@inheritDoc}
778b2fb60cd49426c77869ddd91cbf4130d337a41cMindy Pereira     */
788b2fb60cd49426c77869ddd91cbf4130d337a41cMindy Pereira    @Override
798b2fb60cd49426c77869ddd91cbf4130d337a41cMindy Pereira    public LayoutParams generateLayoutParams(AttributeSet attrs) {
808b2fb60cd49426c77869ddd91cbf4130d337a41cMindy Pereira        return new LayoutParams(getContext(), attrs);
818b2fb60cd49426c77869ddd91cbf4130d337a41cMindy Pereira    }
828b2fb60cd49426c77869ddd91cbf4130d337a41cMindy Pereira
832266b92072b2eda16ef71ed6f3d8811159b756deMindy Pereira    @Override
842266b92072b2eda16ef71ed6f3d8811159b756deMindy Pereira    public void addView(View child) {
858b2fb60cd49426c77869ddd91cbf4130d337a41cMindy Pereira        int borderTop = getPaddingTop();
868b2fb60cd49426c77869ddd91cbf4130d337a41cMindy Pereira        int borderBottom = getPaddingBottom();
878b2fb60cd49426c77869ddd91cbf4130d337a41cMindy Pereira        int borderLeft = getPaddingLeft();
888b2fb60cd49426c77869ddd91cbf4130d337a41cMindy Pereira        int borderRight = getPaddingRight();
898b2fb60cd49426c77869ddd91cbf4130d337a41cMindy Pereira
90c57406cdfa1925c3474e87865c51950b76ee0347Jim Miller        android.view.ViewGroup.LayoutParams params = child.getLayoutParams();
91c57406cdfa1925c3474e87865c51950b76ee0347Jim Miller        LayoutParams layoutParams = params instanceof PreferenceFrameLayout.LayoutParams
92c57406cdfa1925c3474e87865c51950b76ee0347Jim Miller            ? (PreferenceFrameLayout.LayoutParams) child.getLayoutParams() : null;
932266b92072b2eda16ef71ed6f3d8811159b756deMindy Pereira        // Check on the id of the child before adding it.
948b2fb60cd49426c77869ddd91cbf4130d337a41cMindy Pereira        if (layoutParams != null && layoutParams.removeBorders) {
952266b92072b2eda16ef71ed6f3d8811159b756deMindy Pereira            if (mPaddingApplied) {
968b2fb60cd49426c77869ddd91cbf4130d337a41cMindy Pereira                borderTop -= mBorderTop;
978b2fb60cd49426c77869ddd91cbf4130d337a41cMindy Pereira                borderBottom -= mBorderBottom;
988b2fb60cd49426c77869ddd91cbf4130d337a41cMindy Pereira                borderLeft -= mBorderLeft;
998b2fb60cd49426c77869ddd91cbf4130d337a41cMindy Pereira                borderRight -= mBorderRight;
1002266b92072b2eda16ef71ed6f3d8811159b756deMindy Pereira                mPaddingApplied = false;
1012266b92072b2eda16ef71ed6f3d8811159b756deMindy Pereira            }
1028b2fb60cd49426c77869ddd91cbf4130d337a41cMindy Pereira        } else {
1038b2fb60cd49426c77869ddd91cbf4130d337a41cMindy Pereira            // Add the padding to the view group after determining if the
1048b2fb60cd49426c77869ddd91cbf4130d337a41cMindy Pereira            // padding already exists.
1058b2fb60cd49426c77869ddd91cbf4130d337a41cMindy Pereira            if (!mPaddingApplied) {
1068b2fb60cd49426c77869ddd91cbf4130d337a41cMindy Pereira                borderTop += mBorderTop;
1078b2fb60cd49426c77869ddd91cbf4130d337a41cMindy Pereira                borderBottom += mBorderBottom;
1088b2fb60cd49426c77869ddd91cbf4130d337a41cMindy Pereira                borderLeft += mBorderLeft;
1098b2fb60cd49426c77869ddd91cbf4130d337a41cMindy Pereira                borderRight += mBorderRight;
1108b2fb60cd49426c77869ddd91cbf4130d337a41cMindy Pereira                mPaddingApplied = true;
1118b2fb60cd49426c77869ddd91cbf4130d337a41cMindy Pereira            }
1122266b92072b2eda16ef71ed6f3d8811159b756deMindy Pereira        }
1138b2fb60cd49426c77869ddd91cbf4130d337a41cMindy Pereira
1142266b92072b2eda16ef71ed6f3d8811159b756deMindy Pereira        int previousTop = getPaddingTop();
1152266b92072b2eda16ef71ed6f3d8811159b756deMindy Pereira        int previousBottom = getPaddingBottom();
1168b2fb60cd49426c77869ddd91cbf4130d337a41cMindy Pereira        int previousLeft = getPaddingLeft();
1178b2fb60cd49426c77869ddd91cbf4130d337a41cMindy Pereira        int previousRight = getPaddingRight();
1188b2fb60cd49426c77869ddd91cbf4130d337a41cMindy Pereira        if (previousTop != borderTop || previousBottom != borderBottom
1198b2fb60cd49426c77869ddd91cbf4130d337a41cMindy Pereira                || previousLeft != borderLeft || previousRight != borderRight) {
1208b2fb60cd49426c77869ddd91cbf4130d337a41cMindy Pereira            setPadding(borderLeft, borderTop, borderRight, borderBottom);
1212266b92072b2eda16ef71ed6f3d8811159b756deMindy Pereira        }
1228b2fb60cd49426c77869ddd91cbf4130d337a41cMindy Pereira
1232266b92072b2eda16ef71ed6f3d8811159b756deMindy Pereira        super.addView(child);
1242266b92072b2eda16ef71ed6f3d8811159b756deMindy Pereira    }
1258b2fb60cd49426c77869ddd91cbf4130d337a41cMindy Pereira
1268b2fb60cd49426c77869ddd91cbf4130d337a41cMindy Pereira    public static class LayoutParams extends FrameLayout.LayoutParams {
1278b2fb60cd49426c77869ddd91cbf4130d337a41cMindy Pereira        public boolean removeBorders = false;
1288b2fb60cd49426c77869ddd91cbf4130d337a41cMindy Pereira        /**
1298b2fb60cd49426c77869ddd91cbf4130d337a41cMindy Pereira         * {@inheritDoc}
1308b2fb60cd49426c77869ddd91cbf4130d337a41cMindy Pereira         */
1318b2fb60cd49426c77869ddd91cbf4130d337a41cMindy Pereira        public LayoutParams(Context c, AttributeSet attrs) {
1328b2fb60cd49426c77869ddd91cbf4130d337a41cMindy Pereira            super(c, attrs);
1338b2fb60cd49426c77869ddd91cbf4130d337a41cMindy Pereira
1348b2fb60cd49426c77869ddd91cbf4130d337a41cMindy Pereira            TypedArray a = c.obtainStyledAttributes(attrs,
1358b2fb60cd49426c77869ddd91cbf4130d337a41cMindy Pereira                    com.android.internal.R.styleable.PreferenceFrameLayout_Layout);
1368b2fb60cd49426c77869ddd91cbf4130d337a41cMindy Pereira            removeBorders = a.getBoolean(
1378b2fb60cd49426c77869ddd91cbf4130d337a41cMindy Pereira                    com.android.internal.R.styleable.PreferenceFrameLayout_Layout_layout_removeBorders,
1388b2fb60cd49426c77869ddd91cbf4130d337a41cMindy Pereira                    false);
1398b2fb60cd49426c77869ddd91cbf4130d337a41cMindy Pereira            a.recycle();
1408b2fb60cd49426c77869ddd91cbf4130d337a41cMindy Pereira        }
1418b2fb60cd49426c77869ddd91cbf4130d337a41cMindy Pereira
1428b2fb60cd49426c77869ddd91cbf4130d337a41cMindy Pereira        /**
1438b2fb60cd49426c77869ddd91cbf4130d337a41cMindy Pereira         * {@inheritDoc}
1448b2fb60cd49426c77869ddd91cbf4130d337a41cMindy Pereira         */
1458b2fb60cd49426c77869ddd91cbf4130d337a41cMindy Pereira        public LayoutParams(int width, int height) {
1468b2fb60cd49426c77869ddd91cbf4130d337a41cMindy Pereira            super(width, height);
1478b2fb60cd49426c77869ddd91cbf4130d337a41cMindy Pereira        }
1488b2fb60cd49426c77869ddd91cbf4130d337a41cMindy Pereira    }
1492266b92072b2eda16ef71ed6f3d8811159b756deMindy Pereira}