13266e3d7125b0143c5bbc760d2ee24837b982c3fMatthew Fritzepackage com.android.settings.testutils;
23266e3d7125b0143c5bbc760d2ee24837b982c3fMatthew Fritze
33266e3d7125b0143c5bbc760d2ee24837b982c3fMatthew Fritzeimport android.content.res.Resources;
43266e3d7125b0143c5bbc760d2ee24837b982c3fMatthew Fritzeimport android.content.res.XmlResourceParser;
53266e3d7125b0143c5bbc760d2ee24837b982c3fMatthew Fritze
63266e3d7125b0143c5bbc760d2ee24837b982c3fMatthew Fritzeimport android.content.Context;
73266e3d7125b0143c5bbc760d2ee24837b982c3fMatthew Fritzeimport android.text.TextUtils;
83266e3d7125b0143c5bbc760d2ee24837b982c3fMatthew Fritzeimport android.util.AttributeSet;
93266e3d7125b0143c5bbc760d2ee24837b982c3fMatthew Fritzeimport android.util.Xml;
101ac5a012539fda7081fc2646cf330759a456f38bFan Zhangimport com.android.settings.core.PreferenceXmlParserUtils;
113266e3d7125b0143c5bbc760d2ee24837b982c3fMatthew Fritzeimport org.xmlpull.v1.XmlPullParser;
123266e3d7125b0143c5bbc760d2ee24837b982c3fMatthew Fritzeimport org.xmlpull.v1.XmlPullParserException;
133266e3d7125b0143c5bbc760d2ee24837b982c3fMatthew Fritze
143266e3d7125b0143c5bbc760d2ee24837b982c3fMatthew Fritzeimport java.util.ArrayList;
153266e3d7125b0143c5bbc760d2ee24837b982c3fMatthew Fritzeimport java.util.List;
163266e3d7125b0143c5bbc760d2ee24837b982c3fMatthew Fritze
173266e3d7125b0143c5bbc760d2ee24837b982c3fMatthew Fritze/**
183266e3d7125b0143c5bbc760d2ee24837b982c3fMatthew Fritze * Util class for parsing XML
193266e3d7125b0143c5bbc760d2ee24837b982c3fMatthew Fritze */
203266e3d7125b0143c5bbc760d2ee24837b982c3fMatthew Fritzepublic class XmlTestUtils {
213266e3d7125b0143c5bbc760d2ee24837b982c3fMatthew Fritze
223266e3d7125b0143c5bbc760d2ee24837b982c3fMatthew Fritze    /**
233266e3d7125b0143c5bbc760d2ee24837b982c3fMatthew Fritze     * Parses a preference screen's xml, collects and returns all keys used by preferences
243266e3d7125b0143c5bbc760d2ee24837b982c3fMatthew Fritze     * on the screen.
253266e3d7125b0143c5bbc760d2ee24837b982c3fMatthew Fritze     *
263266e3d7125b0143c5bbc760d2ee24837b982c3fMatthew Fritze     * @param context of the preference screen.
273266e3d7125b0143c5bbc760d2ee24837b982c3fMatthew Fritze     * @param xmlId of the Preference Xml to be parsed.
283266e3d7125b0143c5bbc760d2ee24837b982c3fMatthew Fritze     * @return List of all keys in the preference Xml
293266e3d7125b0143c5bbc760d2ee24837b982c3fMatthew Fritze     */
303266e3d7125b0143c5bbc760d2ee24837b982c3fMatthew Fritze    public static List<String> getKeysFromPreferenceXml(Context context, int xmlId) {
313266e3d7125b0143c5bbc760d2ee24837b982c3fMatthew Fritze        final XmlResourceParser parser = context.getResources().getXml(xmlId);
323266e3d7125b0143c5bbc760d2ee24837b982c3fMatthew Fritze        final AttributeSet attrs = Xml.asAttributeSet(parser);
333266e3d7125b0143c5bbc760d2ee24837b982c3fMatthew Fritze        final List<String> keys = new ArrayList<>();
343266e3d7125b0143c5bbc760d2ee24837b982c3fMatthew Fritze        String key;
353266e3d7125b0143c5bbc760d2ee24837b982c3fMatthew Fritze        try {
363266e3d7125b0143c5bbc760d2ee24837b982c3fMatthew Fritze            while (parser.next() != XmlPullParser.END_DOCUMENT) {
373266e3d7125b0143c5bbc760d2ee24837b982c3fMatthew Fritze                try {
381ac5a012539fda7081fc2646cf330759a456f38bFan Zhang                    key = PreferenceXmlParserUtils.getDataKey(context, attrs);
393266e3d7125b0143c5bbc760d2ee24837b982c3fMatthew Fritze                    if (!TextUtils.isEmpty(key)) {
403266e3d7125b0143c5bbc760d2ee24837b982c3fMatthew Fritze                        keys.add(key);
413266e3d7125b0143c5bbc760d2ee24837b982c3fMatthew Fritze                    }
423266e3d7125b0143c5bbc760d2ee24837b982c3fMatthew Fritze                } catch (NullPointerException e) {
433266e3d7125b0143c5bbc760d2ee24837b982c3fMatthew Fritze                    continue;
443266e3d7125b0143c5bbc760d2ee24837b982c3fMatthew Fritze                } catch (Resources.NotFoundException e) {
453266e3d7125b0143c5bbc760d2ee24837b982c3fMatthew Fritze                    continue;
463266e3d7125b0143c5bbc760d2ee24837b982c3fMatthew Fritze                }
473266e3d7125b0143c5bbc760d2ee24837b982c3fMatthew Fritze            }
483266e3d7125b0143c5bbc760d2ee24837b982c3fMatthew Fritze        } catch (java.io.IOException e) {
493266e3d7125b0143c5bbc760d2ee24837b982c3fMatthew Fritze            return null;
503266e3d7125b0143c5bbc760d2ee24837b982c3fMatthew Fritze        } catch (XmlPullParserException e) {
513266e3d7125b0143c5bbc760d2ee24837b982c3fMatthew Fritze            return null;
523266e3d7125b0143c5bbc760d2ee24837b982c3fMatthew Fritze        }
533266e3d7125b0143c5bbc760d2ee24837b982c3fMatthew Fritze
543266e3d7125b0143c5bbc760d2ee24837b982c3fMatthew Fritze        return keys;
553266e3d7125b0143c5bbc760d2ee24837b982c3fMatthew Fritze    }
562caaa7420df26fafc147757d0693d98cf0ed1607Matthew Fritze}