Preference_Delegate.java revision 0ff4568ca07c16f2ad4c61872995200115292ecf
1/*
2 * Copyright (C) 2014 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 android.preference;
18
19import com.android.internal.R;
20import com.android.layoutlib.bridge.android.BridgeContext;
21import com.android.tools.layoutlib.annotations.LayoutlibDelegate;
22
23import org.xmlpull.v1.XmlPullParser;
24
25import android.content.Context;
26import android.content.res.TypedArray;
27import android.view.LayoutInflater;
28import android.view.View;
29import android.view.ViewGroup;
30import android.widget.ListView;
31
32import java.util.HashMap;
33import java.util.Map;
34
35/**
36 * Delegate that provides implementation for native methods in {@link Preference}
37 * <p/>
38 * Through the layoutlib_create tool, selected methods of Preference have been replaced by calls to
39 * methods of the same name in this delegate class.
40 */
41public class Preference_Delegate {
42
43    @LayoutlibDelegate
44    /*package*/ static View getView(Preference pref, View convertView, ViewGroup parent) {
45        Context context = pref.getContext();
46        BridgeContext bc = context instanceof BridgeContext ? ((BridgeContext) context) : null;
47        convertView = pref.getView_Original(convertView, parent);
48        if (bc != null) {
49            Object cookie = bc.getCookie(pref);
50            if (cookie != null) {
51                bc.addViewKey(convertView, cookie);
52            }
53        }
54        return convertView;
55    }
56
57    /**
58     * Inflates the parser and returns the ListView containing the Preferences.
59     */
60    public static View inflatePreference(Context context, XmlPullParser parser, ViewGroup root) {
61        PreferenceManager pm = new PreferenceManager(context);
62        PreferenceScreen ps = pm.getPreferenceScreen();
63        PreferenceInflater inflater = new BridgePreferenceInflater(context, pm);
64        ps = (PreferenceScreen) inflater.inflate(parser, ps, true);
65        ListView preferenceView = createContainerView(context, root);
66        ps.bind(preferenceView);
67        return preferenceView;
68    }
69
70    private static ListView createContainerView(Context context, ViewGroup root) {
71        TypedArray a = context.obtainStyledAttributes(null, R.styleable.PreferenceFragment,
72                R.attr.preferenceFragmentStyle, 0);
73        int mLayoutResId = a.getResourceId(R.styleable.PreferenceFragment_layout,
74                        R.layout.preference_list_fragment);
75        a.recycle();
76
77        LayoutInflater inflater =
78                (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
79        inflater.inflate(mLayoutResId, root, true);
80
81        return (ListView) root.findViewById(android.R.id.list);
82    }
83}
84