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.annotation.VisibleForTesting; 22 import android.support.v4.content.res.TypedArrayUtils; 23 import android.support.v7.preference.Preference; 24 import android.support.v7.preference.PreferenceViewHolder; 25 import android.util.AttributeSet; 26 import android.view.LayoutInflater; 27 import android.view.View; 28 import android.view.ViewGroup; 29 import android.widget.FrameLayout; 30 31 import com.android.settings.R; 32 import com.android.settings.Utils; 33 34 public class LayoutPreference extends Preference { 35 36 private final View.OnClickListener mClickListener = v -> performClick(v); 37 private boolean mAllowDividerAbove; 38 private boolean mAllowDividerBelow; 39 40 @VisibleForTesting 41 View mRootView; 42 43 public LayoutPreference(Context context, AttributeSet attrs) { 44 super(context, attrs); 45 init(context, attrs, 0 /* defStyleAttr */); 46 } 47 48 public LayoutPreference(Context context, AttributeSet attrs, int defStyleAttr) { 49 super(context, attrs, defStyleAttr); 50 init(context, attrs, defStyleAttr); 51 } 52 53 public LayoutPreference(Context context, int resource) { 54 this(context, LayoutInflater.from(context).inflate(resource, null, false)); 55 } 56 57 public LayoutPreference(Context context, View view) { 58 super(context); 59 setView(view); 60 } 61 62 private void init(Context context, AttributeSet attrs, int defStyleAttr) { 63 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.Preference); 64 mAllowDividerAbove = TypedArrayUtils.getBoolean(a, R.styleable.Preference_allowDividerAbove, 65 R.styleable.Preference_allowDividerAbove, false); 66 mAllowDividerBelow = TypedArrayUtils.getBoolean(a, R.styleable.Preference_allowDividerBelow, 67 R.styleable.Preference_allowDividerBelow, false); 68 a.recycle(); 69 70 a = context.obtainStyledAttributes( 71 attrs, com.android.internal.R.styleable.Preference, defStyleAttr, 0); 72 int layoutResource = a.getResourceId(com.android.internal.R.styleable.Preference_layout, 73 0); 74 if (layoutResource == 0) { 75 throw new IllegalArgumentException("LayoutPreference requires a layout to be defined"); 76 } 77 a.recycle(); 78 79 // Need to create view now so that findViewById can be called immediately. 80 final View view = LayoutInflater.from(getContext()) 81 .inflate(layoutResource, null, false); 82 setView(view); 83 } 84 85 private void setView(View view) { 86 setLayoutResource(R.layout.layout_preference_frame); 87 final ViewGroup allDetails = view.findViewById(R.id.all_details); 88 if (allDetails != null) { 89 Utils.forceCustomPadding(allDetails, true /* additive padding */); 90 } 91 mRootView = view; 92 setShouldDisableView(false); 93 } 94 95 @Override 96 public void onBindViewHolder(PreferenceViewHolder holder) { 97 holder.itemView.setOnClickListener(mClickListener); 98 99 final boolean selectable = isSelectable(); 100 holder.itemView.setFocusable(selectable); 101 holder.itemView.setClickable(selectable); 102 holder.setDividerAllowedAbove(mAllowDividerAbove); 103 holder.setDividerAllowedBelow(mAllowDividerBelow); 104 105 FrameLayout layout = (FrameLayout) holder.itemView; 106 layout.removeAllViews(); 107 ViewGroup parent = (ViewGroup) mRootView.getParent(); 108 if (parent != null) { 109 parent.removeView(mRootView); 110 } 111 layout.addView(mRootView); 112 } 113 114 public <T extends View> T findViewById(int id) { 115 return mRootView.findViewById(id); 116 } 117 118 }