110bb1371dca38b5b59f083ee963f7987da6511f2Deepanshu Gupta/* 210bb1371dca38b5b59f083ee963f7987da6511f2Deepanshu Gupta * Copyright (C) 2014 The Android Open Source Project 310bb1371dca38b5b59f083ee963f7987da6511f2Deepanshu Gupta * 410bb1371dca38b5b59f083ee963f7987da6511f2Deepanshu Gupta * Licensed under the Apache License, Version 2.0 (the "License"); 510bb1371dca38b5b59f083ee963f7987da6511f2Deepanshu Gupta * you may not use this file except in compliance with the License. 610bb1371dca38b5b59f083ee963f7987da6511f2Deepanshu Gupta * You may obtain a copy of the License at 710bb1371dca38b5b59f083ee963f7987da6511f2Deepanshu Gupta * 810bb1371dca38b5b59f083ee963f7987da6511f2Deepanshu Gupta * http://www.apache.org/licenses/LICENSE-2.0 910bb1371dca38b5b59f083ee963f7987da6511f2Deepanshu Gupta * 1010bb1371dca38b5b59f083ee963f7987da6511f2Deepanshu Gupta * Unless required by applicable law or agreed to in writing, software 1110bb1371dca38b5b59f083ee963f7987da6511f2Deepanshu Gupta * distributed under the License is distributed on an "AS IS" BASIS, 1210bb1371dca38b5b59f083ee963f7987da6511f2Deepanshu Gupta * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1310bb1371dca38b5b59f083ee963f7987da6511f2Deepanshu Gupta * See the License for the specific language governing permissions and 1410bb1371dca38b5b59f083ee963f7987da6511f2Deepanshu Gupta * limitations under the License. 1510bb1371dca38b5b59f083ee963f7987da6511f2Deepanshu Gupta */ 1610bb1371dca38b5b59f083ee963f7987da6511f2Deepanshu Gupta 1710bb1371dca38b5b59f083ee963f7987da6511f2Deepanshu Guptapackage android.preference; 1810bb1371dca38b5b59f083ee963f7987da6511f2Deepanshu Gupta 1910bb1371dca38b5b59f083ee963f7987da6511f2Deepanshu Guptaimport com.android.internal.R; 2010bb1371dca38b5b59f083ee963f7987da6511f2Deepanshu Guptaimport com.android.layoutlib.bridge.android.BridgeContext; 2110bb1371dca38b5b59f083ee963f7987da6511f2Deepanshu Guptaimport com.android.tools.layoutlib.annotations.LayoutlibDelegate; 2210bb1371dca38b5b59f083ee963f7987da6511f2Deepanshu Gupta 2310bb1371dca38b5b59f083ee963f7987da6511f2Deepanshu Guptaimport org.xmlpull.v1.XmlPullParser; 2410bb1371dca38b5b59f083ee963f7987da6511f2Deepanshu Gupta 2510bb1371dca38b5b59f083ee963f7987da6511f2Deepanshu Guptaimport android.content.Context; 2610bb1371dca38b5b59f083ee963f7987da6511f2Deepanshu Guptaimport android.content.res.TypedArray; 2710bb1371dca38b5b59f083ee963f7987da6511f2Deepanshu Guptaimport android.view.LayoutInflater; 2810bb1371dca38b5b59f083ee963f7987da6511f2Deepanshu Guptaimport android.view.View; 2910bb1371dca38b5b59f083ee963f7987da6511f2Deepanshu Guptaimport android.view.ViewGroup; 3010bb1371dca38b5b59f083ee963f7987da6511f2Deepanshu Guptaimport android.widget.ListView; 3110bb1371dca38b5b59f083ee963f7987da6511f2Deepanshu Gupta 3210bb1371dca38b5b59f083ee963f7987da6511f2Deepanshu Gupta/** 3310bb1371dca38b5b59f083ee963f7987da6511f2Deepanshu Gupta * Delegate that provides implementation for native methods in {@link Preference} 3410bb1371dca38b5b59f083ee963f7987da6511f2Deepanshu Gupta * <p/> 3510bb1371dca38b5b59f083ee963f7987da6511f2Deepanshu Gupta * Through the layoutlib_create tool, selected methods of Preference have been replaced by calls to 3610bb1371dca38b5b59f083ee963f7987da6511f2Deepanshu Gupta * methods of the same name in this delegate class. 3710bb1371dca38b5b59f083ee963f7987da6511f2Deepanshu Gupta */ 3810bb1371dca38b5b59f083ee963f7987da6511f2Deepanshu Guptapublic class Preference_Delegate { 3910bb1371dca38b5b59f083ee963f7987da6511f2Deepanshu Gupta 4010bb1371dca38b5b59f083ee963f7987da6511f2Deepanshu Gupta @LayoutlibDelegate 4110bb1371dca38b5b59f083ee963f7987da6511f2Deepanshu Gupta /*package*/ static View getView(Preference pref, View convertView, ViewGroup parent) { 4210bb1371dca38b5b59f083ee963f7987da6511f2Deepanshu Gupta Context context = pref.getContext(); 4310bb1371dca38b5b59f083ee963f7987da6511f2Deepanshu Gupta BridgeContext bc = context instanceof BridgeContext ? ((BridgeContext) context) : null; 4410bb1371dca38b5b59f083ee963f7987da6511f2Deepanshu Gupta convertView = pref.getView_Original(convertView, parent); 450ff4568ca07c16f2ad4c61872995200115292ecfDeepanshu Gupta if (bc != null) { 460ff4568ca07c16f2ad4c61872995200115292ecfDeepanshu Gupta Object cookie = bc.getCookie(pref); 470ff4568ca07c16f2ad4c61872995200115292ecfDeepanshu Gupta if (cookie != null) { 480ff4568ca07c16f2ad4c61872995200115292ecfDeepanshu Gupta bc.addViewKey(convertView, cookie); 490ff4568ca07c16f2ad4c61872995200115292ecfDeepanshu Gupta } 5010bb1371dca38b5b59f083ee963f7987da6511f2Deepanshu Gupta } 5110bb1371dca38b5b59f083ee963f7987da6511f2Deepanshu Gupta return convertView; 5210bb1371dca38b5b59f083ee963f7987da6511f2Deepanshu Gupta } 5310bb1371dca38b5b59f083ee963f7987da6511f2Deepanshu Gupta 5410bb1371dca38b5b59f083ee963f7987da6511f2Deepanshu Gupta /** 550ff4568ca07c16f2ad4c61872995200115292ecfDeepanshu Gupta * Inflates the parser and returns the ListView containing the Preferences. 5610bb1371dca38b5b59f083ee963f7987da6511f2Deepanshu Gupta */ 5710bb1371dca38b5b59f083ee963f7987da6511f2Deepanshu Gupta public static View inflatePreference(Context context, XmlPullParser parser, ViewGroup root) { 5810bb1371dca38b5b59f083ee963f7987da6511f2Deepanshu Gupta PreferenceManager pm = new PreferenceManager(context); 590ff4568ca07c16f2ad4c61872995200115292ecfDeepanshu Gupta PreferenceInflater inflater = new BridgePreferenceInflater(context, pm); 602f01cf2ea6d8cc07711fccd6157f33226a77da93Deepanshu Gupta PreferenceScreen ps = (PreferenceScreen) inflater.inflate(parser, null, true); 612f01cf2ea6d8cc07711fccd6157f33226a77da93Deepanshu Gupta pm.setPreferences(ps); 6210bb1371dca38b5b59f083ee963f7987da6511f2Deepanshu Gupta ListView preferenceView = createContainerView(context, root); 6310bb1371dca38b5b59f083ee963f7987da6511f2Deepanshu Gupta ps.bind(preferenceView); 6410bb1371dca38b5b59f083ee963f7987da6511f2Deepanshu Gupta return preferenceView; 6510bb1371dca38b5b59f083ee963f7987da6511f2Deepanshu Gupta } 6610bb1371dca38b5b59f083ee963f7987da6511f2Deepanshu Gupta 6710bb1371dca38b5b59f083ee963f7987da6511f2Deepanshu Gupta private static ListView createContainerView(Context context, ViewGroup root) { 6810bb1371dca38b5b59f083ee963f7987da6511f2Deepanshu Gupta TypedArray a = context.obtainStyledAttributes(null, R.styleable.PreferenceFragment, 6910bb1371dca38b5b59f083ee963f7987da6511f2Deepanshu Gupta R.attr.preferenceFragmentStyle, 0); 7010bb1371dca38b5b59f083ee963f7987da6511f2Deepanshu Gupta int mLayoutResId = a.getResourceId(R.styleable.PreferenceFragment_layout, 7110bb1371dca38b5b59f083ee963f7987da6511f2Deepanshu Gupta R.layout.preference_list_fragment); 7210bb1371dca38b5b59f083ee963f7987da6511f2Deepanshu Gupta a.recycle(); 7310bb1371dca38b5b59f083ee963f7987da6511f2Deepanshu Gupta 7410bb1371dca38b5b59f083ee963f7987da6511f2Deepanshu Gupta LayoutInflater inflater = 7510bb1371dca38b5b59f083ee963f7987da6511f2Deepanshu Gupta (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 7610bb1371dca38b5b59f083ee963f7987da6511f2Deepanshu Gupta inflater.inflate(mLayoutResId, root, true); 7710bb1371dca38b5b59f083ee963f7987da6511f2Deepanshu Gupta 7810bb1371dca38b5b59f083ee963f7987da6511f2Deepanshu Gupta return (ListView) root.findViewById(android.R.id.list); 7910bb1371dca38b5b59f083ee963f7987da6511f2Deepanshu Gupta } 8010bb1371dca38b5b59f083ee963f7987da6511f2Deepanshu Gupta} 81