From 451a3417e97d9d3bb835290a65f9af30b112c789 Mon Sep 17 00:00:00 2001 From: Alan Viverette Date: Tue, 11 Feb 2014 18:08:46 -0800 Subject: [PATCH] Add support for assigning keyed View tags via XML BUG: 12768823 Change-Id: Ic55c88894dc153c0af0805e403ad8e40d13abcd2 --- core/java/android/view/LayoutInflater.java | 33 ++++++++++++++++++++-- core/res/res/values/attrs.xml | 8 ++++++ 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/core/java/android/view/LayoutInflater.java b/core/java/android/view/LayoutInflater.java index aaa8b8c9cc121..c4fac46939334 100644 --- a/core/java/android/view/LayoutInflater.java +++ b/core/java/android/view/LayoutInflater.java @@ -90,6 +90,7 @@ public abstract class LayoutInflater { private static final String TAG_INCLUDE = "include"; private static final String TAG_1995 = "blink"; private static final String TAG_REQUEST_FOCUS = "requestFocus"; + private static final String TAG_TAG = "tag"; private static final int[] ATTRS_THEME = new int[] { com.android.internal.R.attr.theme }; @@ -778,6 +779,8 @@ public abstract class LayoutInflater { if (TAG_REQUEST_FOCUS.equals(name)) { parseRequestFocus(parser, parent); + } else if (TAG_TAG.equals(name)) { + parseViewTag(parser, parent, attrs); } else if (TAG_INCLUDE.equals(name)) { if (parser.getDepth() == 0) { throw new InflateException(" cannot be the root element"); @@ -797,10 +800,36 @@ public abstract class LayoutInflater { if (finishInflate) parent.onFinishInflate(); } - private void parseRequestFocus(XmlPullParser parser, View parent) + /** + * Parses a <request-focus> element and requests focus on + * the containing View. + */ + private void parseRequestFocus(XmlPullParser parser, View view) throws XmlPullParserException, IOException { int type; - parent.requestFocus(); + view.requestFocus(); + final int currentDepth = parser.getDepth(); + while (((type = parser.next()) != XmlPullParser.END_TAG || + parser.getDepth() > currentDepth) && type != XmlPullParser.END_DOCUMENT) { + // Empty + } + } + + /** + * Parses a <tag> element and sets a keyed tag on the + * containing View. + */ + private void parseViewTag(XmlPullParser parser, View view, AttributeSet attrs) + throws XmlPullParserException, IOException { + int type; + + final TypedArray ta = mContext.obtainStyledAttributes( + attrs, com.android.internal.R.styleable.ViewTag); + final int key = ta.getResourceId(com.android.internal.R.styleable.ViewTag_id, 0); + final CharSequence value = ta.getText(com.android.internal.R.styleable.ViewTag_value); + view.setTag(key, value); + ta.recycle(); + final int currentDepth = parser.getDepth(); while (((type = parser.next()) != XmlPullParser.END_TAG || parser.getDepth() > currentDepth) && type != XmlPullParser.END_DOCUMENT) { diff --git a/core/res/res/values/attrs.xml b/core/res/res/values/attrs.xml index 9526e13d5c542..af312cce7555e 100644 --- a/core/res/res/values/attrs.xml +++ b/core/res/res/values/attrs.xml @@ -2257,6 +2257,14 @@ + + + + + + + +