Home | History | Annotate | Download | only in preference
      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 {
     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;
     37     private boolean mPaddingApplied;
     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 
     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);
     56 
     57         float density = context.getResources().getDisplayMetrics().density;
     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);
     62 
     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,
     68                 defaultBottomPadding);
     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);
     75 
     76         a.recycle();
     77     }
     78 
     79     /**
     80      * {@inheritDoc}
     81      */
     82     @Override
     83     public LayoutParams generateLayoutParams(AttributeSet attrs) {
     84         return new LayoutParams(getContext(), attrs);
     85     }
     86 
     87     @Override
     88     public void addView(View child) {
     89         int borderTop = getPaddingTop();
     90         int borderBottom = getPaddingBottom();
     91         int borderLeft = getPaddingLeft();
     92         int borderRight = getPaddingRight();
     93 
     94         android.view.ViewGroup.LayoutParams params = child.getLayoutParams();
     95         LayoutParams layoutParams = params instanceof PreferenceFrameLayout.LayoutParams
     96             ? (PreferenceFrameLayout.LayoutParams) child.getLayoutParams() : null;
     97         // Check on the id of the child before adding it.
     98         if (layoutParams != null && layoutParams.removeBorders) {
     99             if (mPaddingApplied) {
    100                 borderTop -= mBorderTop;
    101                 borderBottom -= mBorderBottom;
    102                 borderLeft -= mBorderLeft;
    103                 borderRight -= mBorderRight;
    104                 mPaddingApplied = false;
    105             }
    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             }
    116         }
    117 
    118         int previousTop = getPaddingTop();
    119         int previousBottom = getPaddingBottom();
    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);
    125         }
    126 
    127         super.addView(child);
    128     }
    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     }
    153 }