use of android.content.res.XmlResourceParser in project android_frameworks_base by ParanoidAndroid.
the class PreferenceActivity method loadHeadersFromResource.
/**
* Parse the given XML file as a header description, adding each
* parsed Header into the target list.
*
* @param resid The XML resource to load and parse.
* @param target The list in which the parsed headers should be placed.
*/
public void loadHeadersFromResource(int resid, List<Header> target) {
XmlResourceParser parser = null;
try {
parser = getResources().getXml(resid);
AttributeSet attrs = Xml.asAttributeSet(parser);
int type;
while ((type = parser.next()) != XmlPullParser.END_DOCUMENT && type != XmlPullParser.START_TAG) {
// Parse next until start tag is found
}
String nodeName = parser.getName();
if (!"preference-headers".equals(nodeName)) {
throw new RuntimeException("XML document must start with <preference-headers> tag; found" + nodeName + " at " + parser.getPositionDescription());
}
Bundle curBundle = null;
final int outerDepth = parser.getDepth();
while ((type = parser.next()) != XmlPullParser.END_DOCUMENT && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
continue;
}
nodeName = parser.getName();
if ("header".equals(nodeName)) {
Header header = new Header();
TypedArray sa = getResources().obtainAttributes(attrs, com.android.internal.R.styleable.PreferenceHeader);
header.id = sa.getResourceId(com.android.internal.R.styleable.PreferenceHeader_id, (int) HEADER_ID_UNDEFINED);
TypedValue tv = sa.peekValue(com.android.internal.R.styleable.PreferenceHeader_title);
if (tv != null && tv.type == TypedValue.TYPE_STRING) {
if (tv.resourceId != 0) {
header.titleRes = tv.resourceId;
} else {
header.title = tv.string;
}
}
tv = sa.peekValue(com.android.internal.R.styleable.PreferenceHeader_summary);
if (tv != null && tv.type == TypedValue.TYPE_STRING) {
if (tv.resourceId != 0) {
header.summaryRes = tv.resourceId;
} else {
header.summary = tv.string;
}
}
tv = sa.peekValue(com.android.internal.R.styleable.PreferenceHeader_breadCrumbTitle);
if (tv != null && tv.type == TypedValue.TYPE_STRING) {
if (tv.resourceId != 0) {
header.breadCrumbTitleRes = tv.resourceId;
} else {
header.breadCrumbTitle = tv.string;
}
}
tv = sa.peekValue(com.android.internal.R.styleable.PreferenceHeader_breadCrumbShortTitle);
if (tv != null && tv.type == TypedValue.TYPE_STRING) {
if (tv.resourceId != 0) {
header.breadCrumbShortTitleRes = tv.resourceId;
} else {
header.breadCrumbShortTitle = tv.string;
}
}
header.iconRes = sa.getResourceId(com.android.internal.R.styleable.PreferenceHeader_icon, 0);
header.fragment = sa.getString(com.android.internal.R.styleable.PreferenceHeader_fragment);
sa.recycle();
if (curBundle == null) {
curBundle = new Bundle();
}
final int innerDepth = parser.getDepth();
while ((type = parser.next()) != XmlPullParser.END_DOCUMENT && (type != XmlPullParser.END_TAG || parser.getDepth() > innerDepth)) {
if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
continue;
}
String innerNodeName = parser.getName();
if (innerNodeName.equals("extra")) {
getResources().parseBundleExtra("extra", attrs, curBundle);
XmlUtils.skipCurrentTag(parser);
} else if (innerNodeName.equals("intent")) {
header.intent = Intent.parseIntent(getResources(), parser, attrs);
} else {
XmlUtils.skipCurrentTag(parser);
}
}
if (curBundle.size() > 0) {
header.fragmentArguments = curBundle;
curBundle = null;
}
target.add(header);
} else {
XmlUtils.skipCurrentTag(parser);
}
}
} catch (XmlPullParserException e) {
throw new RuntimeException("Error parsing headers", e);
} catch (IOException e) {
throw new RuntimeException("Error parsing headers", e);
} finally {
if (parser != null)
parser.close();
}
}
use of android.content.res.XmlResourceParser in project android_frameworks_base by ParanoidAndroid.
the class AutoText method init.
private void init(Resources r) {
XmlResourceParser parser = r.getXml(com.android.internal.R.xml.autotext);
StringBuilder right = new StringBuilder(RIGHT);
mTrie = new char[DEFAULT];
mTrie[TRIE_ROOT] = TRIE_NULL;
mTrieUsed = TRIE_ROOT + 1;
try {
XmlUtils.beginDocument(parser, "words");
String odest = "";
char ooff = 0;
while (true) {
XmlUtils.nextElement(parser);
String element = parser.getName();
if (element == null || !(element.equals("word"))) {
break;
}
String src = parser.getAttributeValue(null, "src");
if (parser.next() == XmlPullParser.TEXT) {
String dest = parser.getText();
char off;
if (dest.equals(odest)) {
off = ooff;
} else {
off = (char) right.length();
right.append((char) dest.length());
right.append(dest);
}
add(src, off);
}
}
// Don't let Resources cache a copy of all these strings.
r.flushLayoutCache();
} catch (XmlPullParserException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
parser.close();
}
mText = right.toString();
}
use of android.content.res.XmlResourceParser in project android_frameworks_base by ParanoidAndroid.
the class TimeUtils method getTimeZones.
/**
* Returns the time zones for the country, which is the code
* attribute of the timezone element in time_zones_by_country.xml. Do not modify.
*
* @param country is a two character country code.
* @return TimeZone list, maybe empty but never null. Do not modify.
* @hide
*/
public static ArrayList<TimeZone> getTimeZones(String country) {
synchronized (sLastLockObj) {
if ((country != null) && country.equals(sLastCountry)) {
if (DBG)
Log.d(TAG, "getTimeZones(" + country + "): return cached version");
return sLastZones;
}
}
ArrayList<TimeZone> tzs = new ArrayList<TimeZone>();
if (country == null) {
if (DBG)
Log.d(TAG, "getTimeZones(null): return empty list");
return tzs;
}
Resources r = Resources.getSystem();
XmlResourceParser parser = r.getXml(com.android.internal.R.xml.time_zones_by_country);
try {
XmlUtils.beginDocument(parser, "timezones");
while (true) {
XmlUtils.nextElement(parser);
String element = parser.getName();
if (element == null || !(element.equals("timezone"))) {
break;
}
String code = parser.getAttributeValue(null, "code");
if (country.equals(code)) {
if (parser.next() == XmlPullParser.TEXT) {
String zoneIdString = parser.getText();
TimeZone tz = TimeZone.getTimeZone(zoneIdString);
if (tz.getID().startsWith("GMT") == false) {
// tz.getID doesn't start not "GMT" so its valid
tzs.add(tz);
if (DBG) {
Log.d(TAG, "getTimeZone('" + country + "'): found tz.getID==" + ((tz != null) ? tz.getID() : "<no tz>"));
}
}
}
}
}
} catch (XmlPullParserException e) {
Log.e(TAG, "Got xml parser exception getTimeZone('" + country + "'): e=", e);
} catch (IOException e) {
Log.e(TAG, "Got IO exception getTimeZone('" + country + "'): e=", e);
} finally {
parser.close();
}
synchronized (sLastLockObj) {
// Cache the last result;
sLastZones = tzs;
sLastCountry = country;
return sLastZones;
}
}
use of android.content.res.XmlResourceParser in project XobotOS by xamarin.
the class PreferenceActivity method loadHeadersFromResource.
/**
* Parse the given XML file as a header description, adding each
* parsed Header into the target list.
*
* @param resid The XML resource to load and parse.
* @param target The list in which the parsed headers should be placed.
*/
public void loadHeadersFromResource(int resid, List<Header> target) {
XmlResourceParser parser = null;
try {
parser = getResources().getXml(resid);
AttributeSet attrs = Xml.asAttributeSet(parser);
int type;
while ((type = parser.next()) != XmlPullParser.END_DOCUMENT && type != XmlPullParser.START_TAG) {
// Parse next until start tag is found
}
String nodeName = parser.getName();
if (!"preference-headers".equals(nodeName)) {
throw new RuntimeException("XML document must start with <preference-headers> tag; found" + nodeName + " at " + parser.getPositionDescription());
}
Bundle curBundle = null;
final int outerDepth = parser.getDepth();
while ((type = parser.next()) != XmlPullParser.END_DOCUMENT && (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
continue;
}
nodeName = parser.getName();
if ("header".equals(nodeName)) {
Header header = new Header();
TypedArray sa = getResources().obtainAttributes(attrs, com.android.internal.R.styleable.PreferenceHeader);
header.id = sa.getResourceId(com.android.internal.R.styleable.PreferenceHeader_id, (int) HEADER_ID_UNDEFINED);
TypedValue tv = sa.peekValue(com.android.internal.R.styleable.PreferenceHeader_title);
if (tv != null && tv.type == TypedValue.TYPE_STRING) {
if (tv.resourceId != 0) {
header.titleRes = tv.resourceId;
} else {
header.title = tv.string;
}
}
tv = sa.peekValue(com.android.internal.R.styleable.PreferenceHeader_summary);
if (tv != null && tv.type == TypedValue.TYPE_STRING) {
if (tv.resourceId != 0) {
header.summaryRes = tv.resourceId;
} else {
header.summary = tv.string;
}
}
tv = sa.peekValue(com.android.internal.R.styleable.PreferenceHeader_breadCrumbTitle);
if (tv != null && tv.type == TypedValue.TYPE_STRING) {
if (tv.resourceId != 0) {
header.breadCrumbTitleRes = tv.resourceId;
} else {
header.breadCrumbTitle = tv.string;
}
}
tv = sa.peekValue(com.android.internal.R.styleable.PreferenceHeader_breadCrumbShortTitle);
if (tv != null && tv.type == TypedValue.TYPE_STRING) {
if (tv.resourceId != 0) {
header.breadCrumbShortTitleRes = tv.resourceId;
} else {
header.breadCrumbShortTitle = tv.string;
}
}
header.iconRes = sa.getResourceId(com.android.internal.R.styleable.PreferenceHeader_icon, 0);
header.fragment = sa.getString(com.android.internal.R.styleable.PreferenceHeader_fragment);
sa.recycle();
if (curBundle == null) {
curBundle = new Bundle();
}
final int innerDepth = parser.getDepth();
while ((type = parser.next()) != XmlPullParser.END_DOCUMENT && (type != XmlPullParser.END_TAG || parser.getDepth() > innerDepth)) {
if (type == XmlPullParser.END_TAG || type == XmlPullParser.TEXT) {
continue;
}
String innerNodeName = parser.getName();
if (innerNodeName.equals("extra")) {
getResources().parseBundleExtra("extra", attrs, curBundle);
XmlUtils.skipCurrentTag(parser);
} else if (innerNodeName.equals("intent")) {
header.intent = Intent.parseIntent(getResources(), parser, attrs);
} else {
XmlUtils.skipCurrentTag(parser);
}
}
if (curBundle.size() > 0) {
header.fragmentArguments = curBundle;
curBundle = null;
}
target.add(header);
} else {
XmlUtils.skipCurrentTag(parser);
}
}
} catch (XmlPullParserException e) {
throw new RuntimeException("Error parsing headers", e);
} catch (IOException e) {
throw new RuntimeException("Error parsing headers", e);
} finally {
if (parser != null)
parser.close();
}
}
use of android.content.res.XmlResourceParser in project XobotOS by xamarin.
the class PreferenceManager method inflateFromIntent.
/**
* Inflates a preference hierarchy from the preference hierarchies of
* {@link Activity Activities} that match the given {@link Intent}. An
* {@link Activity} defines its preference hierarchy with meta-data using
* the {@link #METADATA_KEY_PREFERENCES} key.
* <p>
* If a preference hierarchy is given, the new preference hierarchies will
* be merged in.
*
* @param queryIntent The intent to match activities.
* @param rootPreferences Optional existing hierarchy to merge the new
* hierarchies into.
* @return The root hierarchy (if one was not provided, the new hierarchy's
* root).
*/
PreferenceScreen inflateFromIntent(Intent queryIntent, PreferenceScreen rootPreferences) {
final List<ResolveInfo> activities = queryIntentActivities(queryIntent);
final HashSet<String> inflatedRes = new HashSet<String>();
for (int i = activities.size() - 1; i >= 0; i--) {
final ActivityInfo activityInfo = activities.get(i).activityInfo;
final Bundle metaData = activityInfo.metaData;
if ((metaData == null) || !metaData.containsKey(METADATA_KEY_PREFERENCES)) {
continue;
}
// Need to concat the package with res ID since the same res ID
// can be re-used across contexts
final String uniqueResId = activityInfo.packageName + ":" + activityInfo.metaData.getInt(METADATA_KEY_PREFERENCES);
if (!inflatedRes.contains(uniqueResId)) {
inflatedRes.add(uniqueResId);
final Context context;
try {
context = mContext.createPackageContext(activityInfo.packageName, 0);
} catch (NameNotFoundException e) {
Log.w(TAG, "Could not create context for " + activityInfo.packageName + ": " + Log.getStackTraceString(e));
continue;
}
final PreferenceInflater inflater = new PreferenceInflater(context, this);
final XmlResourceParser parser = activityInfo.loadXmlMetaData(context.getPackageManager(), METADATA_KEY_PREFERENCES);
rootPreferences = (PreferenceScreen) inflater.inflate(parser, rootPreferences, true);
parser.close();
}
}
rootPreferences.onAttachedToHierarchy(this);
return rootPreferences;
}
Aggregations