1 /* 2 * Copyright (C) 2018 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 com.android.car.settings.common; 18 19 import android.annotation.NonNull; 20 import android.annotation.XmlRes; 21 import android.content.Context; 22 import android.content.res.TypedArray; 23 import android.content.res.XmlResourceParser; 24 import android.os.Bundle; 25 import android.util.AttributeSet; 26 import android.util.Xml; 27 28 import androidx.annotation.IntDef; 29 30 import com.android.car.settings.R; 31 32 import org.xmlpull.v1.XmlPullParser; 33 import org.xmlpull.v1.XmlPullParserException; 34 35 import java.io.IOException; 36 import java.lang.annotation.Retention; 37 import java.lang.annotation.RetentionPolicy; 38 import java.util.ArrayList; 39 import java.util.Arrays; 40 import java.util.List; 41 42 /** 43 * Utility class to parse elements of XML preferences. This is a reduced version of {@code com 44 * .android.settings.core.PreferenceXmlParserUtils}. 45 */ 46 public class PreferenceXmlParser { 47 48 private static final Logger LOG = new Logger(PreferenceXmlParser.class); 49 50 private static final String PREF_TAG_ENDS_WITH = "Preference"; 51 private static final String PREF_GROUP_TAG_ENDS_WITH = "PreferenceGroup"; 52 private static final List<String> SUPPORTED_PREF_TYPES = Arrays.asList("Preference", 53 "PreferenceCategory", "PreferenceScreen"); 54 55 /** 56 * Flag definition to indicate which metadata should be extracted when 57 * {@link #extractMetadata(Context, int, int)} is called. The flags can be combined by using | 58 * (binary or). 59 */ 60 @IntDef(flag = true, value = { 61 MetadataFlag.FLAG_NEED_KEY, 62 MetadataFlag.FLAG_NEED_PREF_CONTROLLER}) 63 @Retention(RetentionPolicy.SOURCE) 64 public @interface MetadataFlag { 65 int FLAG_NEED_KEY = 1; 66 int FLAG_NEED_PREF_CONTROLLER = 1 << 1; 67 } 68 69 static final String METADATA_KEY = "key"; 70 static final String METADATA_CONTROLLER = "controller"; 71 72 /** 73 * Extracts metadata from each preference XML and puts them into a {@link Bundle}. 74 * 75 * @param xmlResId xml res id of a preference screen 76 * @param flags one or more of {@link MetadataFlag} 77 * @return a list of Bundles containing the extracted metadata 78 */ 79 @NonNull extractMetadata(Context context, @XmlRes int xmlResId, int flags)80 public static List<Bundle> extractMetadata(Context context, @XmlRes int xmlResId, int flags) 81 throws IOException, XmlPullParserException { 82 final List<Bundle> metadata = new ArrayList<>(); 83 if (xmlResId <= 0) { 84 LOG.d(xmlResId + " is invalid."); 85 return metadata; 86 } 87 final XmlResourceParser parser = context.getResources().getXml(xmlResId); 88 89 int type; 90 while ((type = parser.next()) != XmlPullParser.END_DOCUMENT 91 && type != XmlPullParser.START_TAG) { 92 // Parse next until start tag is found 93 } 94 final int outerDepth = parser.getDepth(); 95 96 do { 97 if (type != XmlPullParser.START_TAG) { 98 continue; 99 } 100 final String nodeName = parser.getName(); 101 if (!SUPPORTED_PREF_TYPES.contains(nodeName) && !nodeName.endsWith(PREF_TAG_ENDS_WITH) 102 && !nodeName.endsWith(PREF_GROUP_TAG_ENDS_WITH)) { 103 continue; 104 } 105 final Bundle preferenceMetadata = new Bundle(); 106 final AttributeSet attrs = Xml.asAttributeSet(parser); 107 final TypedArray preferenceAttributes = context.obtainStyledAttributes(attrs, 108 R.styleable.Preference); 109 110 if (hasFlag(flags, MetadataFlag.FLAG_NEED_KEY)) { 111 preferenceMetadata.putString(METADATA_KEY, getKey(preferenceAttributes)); 112 } 113 if (hasFlag(flags, MetadataFlag.FLAG_NEED_PREF_CONTROLLER)) { 114 preferenceMetadata.putString(METADATA_CONTROLLER, 115 getController(preferenceAttributes)); 116 } 117 metadata.add(preferenceMetadata); 118 119 preferenceAttributes.recycle(); 120 } while ((type = parser.next()) != XmlPullParser.END_DOCUMENT 121 && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)); 122 parser.close(); 123 124 return metadata; 125 } 126 hasFlag(int flags, @MetadataFlag int flag)127 private static boolean hasFlag(int flags, @MetadataFlag int flag) { 128 return (flags & flag) != 0; 129 } 130 getKey(TypedArray styledAttributes)131 private static String getKey(TypedArray styledAttributes) { 132 return styledAttributes.getString(com.android.internal.R.styleable.Preference_key); 133 } 134 getController(TypedArray styledAttributes)135 private static String getController(TypedArray styledAttributes) { 136 return styledAttributes.getString(R.styleable.Preference_controller); 137 } 138 } 139