Home | History | Annotate | Download | only in applications
      1 /*
      2  * Copyright (C) 2015 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 com.android.settings.applications;
     18 
     19 import android.content.Context;
     20 import android.content.res.TypedArray;
     21 import android.support.v7.preference.Preference;
     22 import android.support.v7.preference.PreferenceViewHolder;
     23 import android.util.AttributeSet;
     24 import android.view.LayoutInflater;
     25 import android.view.View;
     26 import android.view.ViewGroup;
     27 import android.widget.FrameLayout;
     28 
     29 import com.android.settings.R;
     30 import com.android.settings.Utils;
     31 
     32 public class LayoutPreference extends Preference {
     33 
     34     private View mRootView;
     35 
     36     public LayoutPreference(Context context, AttributeSet attrs) {
     37         super(context, attrs);
     38         final TypedArray a = context.obtainStyledAttributes(
     39                 attrs, com.android.internal.R.styleable.Preference, 0, 0);
     40         int layoutResource = a.getResourceId(com.android.internal.R.styleable.Preference_layout,
     41                 0);
     42         if (layoutResource == 0) {
     43             throw new IllegalArgumentException("LayoutPreference requires a layout to be defined");
     44         }
     45         // Need to create view now so that findViewById can be called immediately.
     46         final View view = LayoutInflater.from(getContext())
     47                 .inflate(layoutResource, null, false);
     48         setView(view);
     49     }
     50 
     51     public LayoutPreference(Context context, int resource) {
     52         this(context, LayoutInflater.from(context).inflate(resource, null, false));
     53     }
     54 
     55     public LayoutPreference(Context context, View view) {
     56         super(context);
     57         setView(view);
     58     }
     59 
     60     private void setView(View view) {
     61         setLayoutResource(R.layout.layout_preference_frame);
     62         setSelectable(false);
     63         final ViewGroup allDetails = (ViewGroup) view.findViewById(R.id.all_details);
     64         if (allDetails != null) {
     65             Utils.forceCustomPadding(allDetails, true /* additive padding */);
     66         }
     67         mRootView = view;
     68         setShouldDisableView(false);
     69     }
     70 
     71     @Override
     72     public void onBindViewHolder(PreferenceViewHolder view) {
     73         FrameLayout layout = (FrameLayout) view.itemView;
     74         layout.removeAllViews();
     75         ViewGroup parent = (ViewGroup) mRootView.getParent();
     76         if (parent != null) {
     77             parent.removeView(mRootView);
     78         }
     79         layout.addView(mRootView);
     80     }
     81 
     82     public View findViewById(int id) {
     83         return mRootView.findViewById(id);
     84     }
     85 
     86 }
     87