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