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  * @deprecated Use the <a href="{@docRoot}jetpack/androidx.html">AndroidX</a>
     29  *      <a href="{@docRoot}reference/androidx/preference/package-summary.html">
     30  *      Preference Library</a> for consistent behavior across all devices. For more information on
     31  *      using the AndroidX Preference Library see
     32  *      <a href="{@docRoot}guide/topics/ui/settings.html">Settings</a>.
     33  */
     34 @Deprecated
     35 public class PreferenceFrameLayout extends FrameLayout {
     36     private static final int DEFAULT_BORDER_TOP = 0;
     37     private static final int DEFAULT_BORDER_BOTTOM = 0;
     38     private static final int DEFAULT_BORDER_LEFT = 0;
     39     private static final int DEFAULT_BORDER_RIGHT = 0;
     40     private final int mBorderTop;
     41     private final int mBorderBottom;
     42     private final int mBorderLeft;
     43     private final int mBorderRight;
     44     private boolean mPaddingApplied;
     45 
     46     public PreferenceFrameLayout(Context context) {
     47         this(context, null);
     48     }
     49 
     50     public PreferenceFrameLayout(Context context, AttributeSet attrs) {
     51         this(context, attrs, com.android.internal.R.attr.preferenceFrameLayoutStyle);
     52     }
     53 
     54     public PreferenceFrameLayout(Context context, AttributeSet attrs, int defStyleAttr) {
     55         this(context, attrs, defStyleAttr, 0);
     56     }
     57 
     58     public PreferenceFrameLayout(
     59             Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
     60         super(context, attrs, defStyleAttr, defStyleRes);
     61         final TypedArray a = context.obtainStyledAttributes(attrs,
     62                 com.android.internal.R.styleable.PreferenceFrameLayout, defStyleAttr, defStyleRes);
     63 
     64         float density = context.getResources().getDisplayMetrics().density;
     65         int defaultBorderTop = (int) (density * DEFAULT_BORDER_TOP + 0.5f);
     66         int defaultBottomPadding = (int) (density * DEFAULT_BORDER_BOTTOM + 0.5f);
     67         int defaultLeftPadding = (int) (density * DEFAULT_BORDER_LEFT + 0.5f);
     68         int defaultRightPadding = (int) (density * DEFAULT_BORDER_RIGHT + 0.5f);
     69 
     70         mBorderTop = a.getDimensionPixelSize(
     71                 com.android.internal.R.styleable.PreferenceFrameLayout_borderTop,
     72                 defaultBorderTop);
     73         mBorderBottom = a.getDimensionPixelSize(
     74                 com.android.internal.R.styleable.PreferenceFrameLayout_borderBottom,
     75                 defaultBottomPadding);
     76         mBorderLeft = a.getDimensionPixelSize(
     77                 com.android.internal.R.styleable.PreferenceFrameLayout_borderLeft,
     78                 defaultLeftPadding);
     79         mBorderRight = a.getDimensionPixelSize(
     80                 com.android.internal.R.styleable.PreferenceFrameLayout_borderRight,
     81                 defaultRightPadding);
     82 
     83         a.recycle();
     84     }
     85 
     86     /**
     87      * {@inheritDoc}
     88      */
     89     @Override
     90     public LayoutParams generateLayoutParams(AttributeSet attrs) {
     91         return new LayoutParams(getContext(), attrs);
     92     }
     93 
     94     @Override
     95     public void addView(View child) {
     96         int borderTop = getPaddingTop();
     97         int borderBottom = getPaddingBottom();
     98         int borderLeft = getPaddingLeft();
     99         int borderRight = getPaddingRight();
    100 
    101         android.view.ViewGroup.LayoutParams params = child.getLayoutParams();
    102         LayoutParams layoutParams = params instanceof PreferenceFrameLayout.LayoutParams
    103             ? (PreferenceFrameLayout.LayoutParams) child.getLayoutParams() : null;
    104         // Check on the id of the child before adding it.
    105         if (layoutParams != null && layoutParams.removeBorders) {
    106             if (mPaddingApplied) {
    107                 borderTop -= mBorderTop;
    108                 borderBottom -= mBorderBottom;
    109                 borderLeft -= mBorderLeft;
    110                 borderRight -= mBorderRight;
    111                 mPaddingApplied = false;
    112             }
    113         } else {
    114             // Add the padding to the view group after determining if the
    115             // padding already exists.
    116             if (!mPaddingApplied) {
    117                 borderTop += mBorderTop;
    118                 borderBottom += mBorderBottom;
    119                 borderLeft += mBorderLeft;
    120                 borderRight += mBorderRight;
    121                 mPaddingApplied = true;
    122             }
    123         }
    124 
    125         int previousTop = getPaddingTop();
    126         int previousBottom = getPaddingBottom();
    127         int previousLeft = getPaddingLeft();
    128         int previousRight = getPaddingRight();
    129         if (previousTop != borderTop || previousBottom != borderBottom
    130                 || previousLeft != borderLeft || previousRight != borderRight) {
    131             setPadding(borderLeft, borderTop, borderRight, borderBottom);
    132         }
    133 
    134         super.addView(child);
    135     }
    136 
    137     public static class LayoutParams extends FrameLayout.LayoutParams {
    138         public boolean removeBorders = false;
    139         /**
    140          * {@inheritDoc}
    141          */
    142         public LayoutParams(Context c, AttributeSet attrs) {
    143             super(c, attrs);
    144 
    145             TypedArray a = c.obtainStyledAttributes(attrs,
    146                     com.android.internal.R.styleable.PreferenceFrameLayout_Layout);
    147             removeBorders = a.getBoolean(
    148                     com.android.internal.R.styleable.PreferenceFrameLayout_Layout_layout_removeBorders,
    149                     false);
    150             a.recycle();
    151         }
    152 
    153         /**
    154          * {@inheritDoc}
    155          */
    156         public LayoutParams(int width, int height) {
    157             super(width, height);
    158         }
    159     }
    160 }