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 Guptaimport java.util.HashMap;
3310bb1371dca38b5b59f083ee963f7987da6511f2Deepanshu Guptaimport java.util.Map;
3410bb1371dca38b5b59f083ee963f7987da6511f2Deepanshu Gupta
3510bb1371dca38b5b59f083ee963f7987da6511f2Deepanshu Gupta/**
3610bb1371dca38b5b59f083ee963f7987da6511f2Deepanshu Gupta * Delegate that provides implementation for native methods in {@link Preference}
3710bb1371dca38b5b59f083ee963f7987da6511f2Deepanshu Gupta * <p/>
3810bb1371dca38b5b59f083ee963f7987da6511f2Deepanshu Gupta * Through the layoutlib_create tool, selected methods of Preference have been replaced by calls to
3910bb1371dca38b5b59f083ee963f7987da6511f2Deepanshu Gupta * methods of the same name in this delegate class.
4010bb1371dca38b5b59f083ee963f7987da6511f2Deepanshu Gupta */
4110bb1371dca38b5b59f083ee963f7987da6511f2Deepanshu Guptapublic class Preference_Delegate {
4210bb1371dca38b5b59f083ee963f7987da6511f2Deepanshu Gupta
4310bb1371dca38b5b59f083ee963f7987da6511f2Deepanshu Gupta    @LayoutlibDelegate
4410bb1371dca38b5b59f083ee963f7987da6511f2Deepanshu Gupta    /*package*/ static View getView(Preference pref, View convertView, ViewGroup parent) {
4510bb1371dca38b5b59f083ee963f7987da6511f2Deepanshu Gupta        Context context = pref.getContext();
4610bb1371dca38b5b59f083ee963f7987da6511f2Deepanshu Gupta        BridgeContext bc = context instanceof BridgeContext ? ((BridgeContext) context) : null;
4710bb1371dca38b5b59f083ee963f7987da6511f2Deepanshu Gupta        convertView = pref.getView_Original(convertView, parent);
480ff4568ca07c16f2ad4c61872995200115292ecfDeepanshu Gupta        if (bc != null) {
490ff4568ca07c16f2ad4c61872995200115292ecfDeepanshu Gupta            Object cookie = bc.getCookie(pref);
500ff4568ca07c16f2ad4c61872995200115292ecfDeepanshu Gupta            if (cookie != null) {
510ff4568ca07c16f2ad4c61872995200115292ecfDeepanshu Gupta                bc.addViewKey(convertView, cookie);
520ff4568ca07c16f2ad4c61872995200115292ecfDeepanshu Gupta            }
5310bb1371dca38b5b59f083ee963f7987da6511f2Deepanshu Gupta        }
5410bb1371dca38b5b59f083ee963f7987da6511f2Deepanshu Gupta        return convertView;
5510bb1371dca38b5b59f083ee963f7987da6511f2Deepanshu Gupta    }
5610bb1371dca38b5b59f083ee963f7987da6511f2Deepanshu Gupta
5710bb1371dca38b5b59f083ee963f7987da6511f2Deepanshu Gupta    /**
580ff4568ca07c16f2ad4c61872995200115292ecfDeepanshu Gupta     * Inflates the parser and returns the ListView containing the Preferences.
5910bb1371dca38b5b59f083ee963f7987da6511f2Deepanshu Gupta     */
6010bb1371dca38b5b59f083ee963f7987da6511f2Deepanshu Gupta    public static View inflatePreference(Context context, XmlPullParser parser, ViewGroup root) {
6110bb1371dca38b5b59f083ee963f7987da6511f2Deepanshu Gupta        PreferenceManager pm = new PreferenceManager(context);
6210bb1371dca38b5b59f083ee963f7987da6511f2Deepanshu Gupta        PreferenceScreen ps = pm.getPreferenceScreen();
630ff4568ca07c16f2ad4c61872995200115292ecfDeepanshu Gupta        PreferenceInflater inflater = new BridgePreferenceInflater(context, pm);
6410bb1371dca38b5b59f083ee963f7987da6511f2Deepanshu Gupta        ps = (PreferenceScreen) inflater.inflate(parser, ps, true);
6510bb1371dca38b5b59f083ee963f7987da6511f2Deepanshu Gupta        ListView preferenceView = createContainerView(context, root);
6610bb1371dca38b5b59f083ee963f7987da6511f2Deepanshu Gupta        ps.bind(preferenceView);
6710bb1371dca38b5b59f083ee963f7987da6511f2Deepanshu Gupta        return preferenceView;
6810bb1371dca38b5b59f083ee963f7987da6511f2Deepanshu Gupta    }
6910bb1371dca38b5b59f083ee963f7987da6511f2Deepanshu Gupta
7010bb1371dca38b5b59f083ee963f7987da6511f2Deepanshu Gupta    private static ListView createContainerView(Context context, ViewGroup root) {
7110bb1371dca38b5b59f083ee963f7987da6511f2Deepanshu Gupta        TypedArray a = context.obtainStyledAttributes(null, R.styleable.PreferenceFragment,
7210bb1371dca38b5b59f083ee963f7987da6511f2Deepanshu Gupta                R.attr.preferenceFragmentStyle, 0);
7310bb1371dca38b5b59f083ee963f7987da6511f2Deepanshu Gupta        int mLayoutResId = a.getResourceId(R.styleable.PreferenceFragment_layout,
7410bb1371dca38b5b59f083ee963f7987da6511f2Deepanshu Gupta                        R.layout.preference_list_fragment);
7510bb1371dca38b5b59f083ee963f7987da6511f2Deepanshu Gupta        a.recycle();
7610bb1371dca38b5b59f083ee963f7987da6511f2Deepanshu Gupta
7710bb1371dca38b5b59f083ee963f7987da6511f2Deepanshu Gupta        LayoutInflater inflater =
7810bb1371dca38b5b59f083ee963f7987da6511f2Deepanshu Gupta                (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
7910bb1371dca38b5b59f083ee963f7987da6511f2Deepanshu Gupta        inflater.inflate(mLayoutResId, root, true);
8010bb1371dca38b5b59f083ee963f7987da6511f2Deepanshu Gupta
8110bb1371dca38b5b59f083ee963f7987da6511f2Deepanshu Gupta        return (ListView) root.findViewById(android.R.id.list);
8210bb1371dca38b5b59f083ee963f7987da6511f2Deepanshu Gupta    }
8310bb1371dca38b5b59f083ee963f7987da6511f2Deepanshu Gupta}
84