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.app.FragmentBreadCrumbs;
     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 {
     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;
     38     private boolean mPaddingApplied;
     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 
     53         float density = context.getResources().getDisplayMetrics().density;
     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);
     58 
     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,
     64                 defaultBottomPadding);
     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);
     71 
     72         a.recycle();
     73     }
     74 
     75     /**
     76      * {@inheritDoc}
     77      */
     78     @Override
     79     public LayoutParams generateLayoutParams(AttributeSet attrs) {
     80         return new LayoutParams(getContext(), attrs);
     81     }
     82 
     83     @Override
     84     public void addView(View child) {
     85         int borderTop = getPaddingTop();
     86         int borderBottom = getPaddingBottom();
     87         int borderLeft = getPaddingLeft();
     88         int borderRight = getPaddingRight();
     89 
     90         android.view.ViewGroup.LayoutParams params = child.getLayoutParams();
     91         LayoutParams layoutParams = params instanceof PreferenceFrameLayout.LayoutParams
     92             ? (PreferenceFrameLayout.LayoutParams) child.getLayoutParams() : null;
     93         // Check on the id of the child before adding it.
     94         if (layoutParams != null && layoutParams.removeBorders) {
     95             if (mPaddingApplied) {
     96                 borderTop -= mBorderTop;
     97                 borderBottom -= mBorderBottom;
     98                 borderLeft -= mBorderLeft;
     99                 borderRight -= mBorderRight;
    100                 mPaddingApplied = false;
    101             }
    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             }
    112         }
    113 
    114         int previousTop = getPaddingTop();
    115         int previousBottom = getPaddingBottom();
    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);
    121         }
    122 
    123         super.addView(child);
    124     }
    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     }
    149 }