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