1 package com.android.settings.testutils; 2 3 import static com.android.settings.core.PreferenceXmlParserUtils.METADATA_KEY; 4 import static com.android.settings.core.PreferenceXmlParserUtils.MetadataFlag 5 .FLAG_INCLUDE_PREF_SCREEN; 6 import static com.android.settings.core.PreferenceXmlParserUtils.MetadataFlag.FLAG_NEED_KEY; 7 8 import android.content.Context; 9 import android.os.Bundle; 10 import android.text.TextUtils; 11 12 import com.android.settings.core.PreferenceXmlParserUtils; 13 14 import org.xmlpull.v1.XmlPullParserException; 15 16 import java.util.ArrayList; 17 import java.util.List; 18 19 /** 20 * Util class for parsing XML 21 */ 22 public class XmlTestUtils { 23 24 /** 25 * Parses a preference screen's xml, collects and returns all keys used by preferences 26 * on the screen. 27 * 28 * @param context of the preference screen. 29 * @param xmlId of the Preference Xml to be parsed. 30 * @return List of all keys in the preference Xml 31 */ getKeysFromPreferenceXml(Context context, int xmlId)32 public static List<String> getKeysFromPreferenceXml(Context context, int xmlId) { 33 final List<String> keys = new ArrayList<>(); 34 try { 35 List<Bundle> metadata = PreferenceXmlParserUtils.extractMetadata(context, xmlId, 36 FLAG_NEED_KEY | FLAG_INCLUDE_PREF_SCREEN); 37 for (Bundle bundle : metadata) { 38 final String key = bundle.getString(METADATA_KEY); 39 if (!TextUtils.isEmpty(key)) { 40 keys.add(key); 41 } 42 } 43 } catch (java.io.IOException | XmlPullParserException e) { 44 return null; 45 } 46 47 return keys; 48 } 49 }