private void parseRequestFocus(XmlPullParser parser, View view)
        {
            int type;

            view.requestFocus();
            int currentDepth = parser.getDepth();

            while (((type = parser.next()) != XmlPullParser.END_TAG || parser.getDepth() > currentDepth) && type != XmlPullParser.END_DOCUMENT)
            {
                // Empty
            }
        }
        void rInflate(XmlPullParser parser, View parent, AttributeSet attrs, bool finishInflate, bool inheritContext)
        {
            int depth = parser.getDepth();
            int type;

            while (((type = parser.next()) != XmlPullParser.END_TAG || parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT)
            {
                if (type != XmlPullParser.START_TAG)
                {
                    continue;
                }

                string name = parser.getName();

                if (TAG_REQUEST_FOCUS.Equals(name))
                {
                    parseRequestFocus(parser, parent);
                }
                else if (TAG_TAG.Equals(name))
                {
                    parseViewTag(parser, parent, attrs);
                }

                if (TAG_INCLUDE.Equals(name))
                {
                    if (parser.getDepth() == 0)
                    {
                        throw new Exception("<include /> cannot be the root element");
                    }

                    parseInclude(parser, parent, attrs, inheritContext);
                }
                else if (TAG_MERGE.Equals(name))
                {
                    throw new Exception("<merge /> must be the root element");
                }

                else
                {
                    View      view                 = createViewFromTag(parent, name, attrs, inheritContext);
                    ViewGroup viewGroup            = (ViewGroup)parent;
                    ViewGroup.LayoutParams lparams = viewGroup.generateLayoutParams(attrs);
                    rInflate(parser, view, attrs, true, true);
                    viewGroup.addView(view, lparams);
                }
            }

            if (finishInflate)
            {
                parent.onFinishInflate();
            }
        }
Exemple #3
0
        /**
         * Creates or retrieves a ColorStateList that always returns a single color.
         */
        /*public static ColorStateList valueOf(int color)
         * {
         *  // TODO: should we collect these eventually?
         *  lock(sCache)
         *  {
         *      WeakReference<ColorStateList> ref1 = sCache.get(color);
         *
         *      ColorStateList csl1;
         *      ref1.TryGetTarget(out csl1);
         *
         *      ColorStateList csl = ref1 != null ? csl1 : null;
         *      if (csl != null)
         *      {
         *          return csl;
         *      }
         *
         *      csl = new ColorStateList(EMPTY, new int[] { color });
         *      sCache.put(color, new WeakReference<ColorStateList>(csl));
         *      return csl;
         *  }
         * }*/

        /**
         * Create a ColorStateList from an XML document, given a set of {@link Resources}.
         */
        public static ColorStateList createFromXml(Resources r, XmlPullParser parser)
        {
            AttributeSet attrs = Xml.asAttributeSet(parser);

            int type;

            while ((type = parser.next()) != XmlPullParser.START_TAG && type != XmlPullParser.END_DOCUMENT)
            {
            }

            if (type != XmlPullParser.START_TAG)
            {
                throw new Exception("No start tag found");
            }

            return(createFromXmlInner(r, parser, attrs));
        }
        private void parseViewTag(XmlPullParser parser, View view, AttributeSet attrs)
        {
            int type;

            //The below will be activated in the final version of Astoria.
            //TypedArray ta = mContext.obtainStyledAttributes(attrs, com.android.internal.R.styleable.ViewTag);
            //int key = ta.getResourceId(com.android.internal.R.styleable.ViewTag_id, 0);
            //string value = ta.getText(com.android.internal.R.styleable.ViewTag_value);
            //view.setTag(key, value);
            //ta.recycle();

            int currentDepth = parser.getDepth();

            while (((type = parser.next()) != XmlPullParser.END_TAG || parser.getDepth() > currentDepth) && type != XmlPullParser.END_DOCUMENT)
            {
                // Empty
            }
        }
        /**
         * If the type of the object cannot be determined, and thus no Marshal class can handle the object, this
         * method is called. It will build either a SoapPrimitive or a SoapObject
         *
         * @param parser
         * @param typeNamespace
         * @param typeName
         * @return unknownObject wrapped as a SoapPrimitive or SoapObject
         * @throws IOException
         * @throws XmlPullParserException
         */

        protected Object readUnknown(XmlPullParser parser, String typeNamespace, String typeName)
        {
            String name       = parser.getName();
            String namespace_ = parser.getNamespace();

            // cache the attribute info list from the current element before we move on
            List <object> attributeInfoVector = new List <object>();

            for (int attributeCount = 0; attributeCount < parser.getAttributeCount(); attributeCount++)
            {
                AttributeInfo attributeInfo = new AttributeInfo();
                attributeInfo.setName(parser.getAttributeName(attributeCount));
                attributeInfo.setValue(parser.getAttributeValue(attributeCount));
                attributeInfo.setNamespace(parser.getAttributeNamespace(attributeCount));
                attributeInfo.setType(parser.getAttributeType(attributeCount));
                attributeInfoVector.Add(attributeInfo);
            }

            parser.next(); // move to text, inner start tag or end tag
            Object result = null;
            String text   = null;

            if (parser.getEventType() == XmlPullParser.TEXT)
            {
                text = parser.getText();
                SoapPrimitive sp = new SoapPrimitive(typeNamespace, typeName, text);
                result = sp;
                // apply all the cached attribute info list before we add the property and descend further for parsing
                for (int i = 0; i < attributeInfoVector.Count; i++)
                {
                    sp.addAttribute((AttributeInfo)attributeInfoVector[i]);
                }
                parser.next();
            }
            else if (parser.getEventType() == XmlPullParser.END_TAG)
            {
                SoapObject so = new SoapObject(typeNamespace, typeName);
                // apply all the cached attribute info list before we add the property and descend further for parsing
                for (int i = 0; i < attributeInfoVector.Count; i++)
                {
                    so.addAttribute((AttributeInfo)attributeInfoVector[i]);
                }
                result = so;
            }

            if (parser.getEventType() == XmlPullParser.START_TAG)
            {
                if (text != null && text.Trim().Length != 0)
                {
                    throw new Exception("Malformed input: Mixed content");
                }
                SoapObject so = new SoapObject(typeNamespace, typeName);
                // apply all the cached attribute info list before we add the property and descend further for parsing
                for (int i = 0; i < attributeInfoVector.Count; i++)
                {
                    so.addAttribute((AttributeInfo)attributeInfoVector[i]);
                }

                while (parser.getEventType() != XmlPullParser.END_TAG)
                {
                    so.addProperty(parser.getNamespace(), parser.getName(), read(parser, so, so.getPropertyCount(),
                                                                                 null, null, PropertyInfo.OBJECT_TYPE));
                    parser.nextTag();
                }
                result = so;
            }
            parser.require(XmlPullParser.END_TAG, namespace_, name);
            return(result);
        }
        /**
         * Read a FvmSerializable.
         */

        protected void readSerializable(XmlPullParser parser, FvmSerializable obj)
        {
            int tag = 0;

            try
            {
                tag = parser.nextTag();
            }
            catch (XmlPullParserException e)
            {
                if (obj is HasInnerText)
                {
                    ((HasInnerText)obj).setInnerText((parser.getText() != null) ? parser.getText() : "");
                }
                tag = parser.nextTag();
            }
            while (tag != XmlPullParser.END_TAG)
            {
                String name = parser.getName();
                if (!implicitTypes || !(obj is SoapObject))
                {
                    PropertyInfo info          = new PropertyInfo();
                    int          propertyCount = obj.getPropertyCount();
                    bool         propertyFound = false;

                    for (int i = 0; i < propertyCount && !propertyFound; i++)
                    {
                        info.clear();
                        obj.getPropertyInfo(i, properties, info);

                        if ((name.Equals(info.name) && info.namespace_ == null) ||
                            (name.Equals(info.name) && parser.getNamespace().Equals(info.namespace_)))
                        {
                            propertyFound = true;
                            obj.setProperty(i, read(parser, obj, i, null, null, info));
                        }
                    }

                    if (!propertyFound)
                    {
                        if (avoidExceptionForUnknownProperty)
                        {
                            // Dummy loop to read until corresponding END tag
                            while (parser.next() != XmlPullParser.END_TAG || !name.Equals(parser.getName()))
                            {
                            }
                            ;
                        }
                        else
                        {
                            throw new Exception("Unknown Property: " + name);
                        }
                    }
                    else
                    {
                        if (obj is HasAttributes)
                        {
                            HasAttributes soapObject = (HasAttributes)obj;
                            int           cnt        = parser.getAttributeCount();
                            for (int counter = 0; counter < cnt; counter++)
                            {
                                AttributeInfo attributeInfo = new AttributeInfo();
                                attributeInfo.setName(parser.getAttributeName(counter));
                                attributeInfo.setValue(parser.getAttributeValue(counter));
                                attributeInfo.setNamespace(parser.getAttributeNamespace(counter));
                                attributeInfo.setType(parser.getAttributeType(counter));
                                soapObject.setAttribute(attributeInfo);
                            }
                        }
                    }
                }
                else
                {
                    // I can only make this work for SoapObjects - hence the check above
                    // I don't understand namespaces well enough to know whether it is correct in the next line...
                    ((SoapObject)obj).addProperty(parser.getName(), read(parser, obj, obj.getPropertyCount(),
                                                                         ((SoapObject)obj).getNamespace(), name, PropertyInfo.OBJECT_TYPE));
                }
                try
                {
                    tag = parser.nextTag();
                }
                catch (XmlPullParserException e)
                {
                    if (obj is HasInnerText)
                    {
                        ((HasInnerText)obj).setInnerText((parser.getText() != null) ? parser.getText() : "");
                    }
                    tag = parser.nextTag();
                }
            }
            parser.require(XmlPullParser.END_TAG, null, null);
        }
        public virtual View inflate(XmlPullParser parser, ViewGroup root, bool attachToRoot)
        {
            //Trace.traceBegin(Trace.TRACE_TAG_VIEW, "inflate");

            AttributeSet attrs       = Xml.asAttributeSet(parser, mContext);
            Context      lastContext = (Context)mConstructorArgs[0];

            mConstructorArgs[0] = mContext;
            View result = root;

            try
            {
                // Look for the root node.
                int type;
                while ((type = parser.next()) != XmlPullParser.START_TAG && type != XmlPullParser.END_DOCUMENT)
                {
                    // Empty
                    //"WHAT A MISTAKE!" - DJT; but this is how Android does it so we'll do it here :(
                }

                if (type != XmlPullParser.START_TAG)
                {
                    throw new Exception(parser.getPositionDescription() + ": No start tag found!");
                }

                string name = parser.getName();

                if (DEBUG)
                {
                    Debug.WriteLine("**************************");
                    Debug.WriteLine("Creating root view: " + name);
                    Debug.WriteLine("**************************");
                }

                if (TAG_MERGE.Equals(name))
                {
                    if (root == null || !attachToRoot)
                    {
                        throw new Exception("<merge /> can be used only with a valid " + "ViewGroup root and attachToRoot=true");
                    }

                    rInflate(parser, root, attrs, false, false);
                }

                else
                {
                    // Temp is the root view that was found in the xml
                    View temp = createViewFromTag(root, name, attrs, false);

                    ViewGroup.LayoutParams lparams = null;

                    if (root != null)
                    {
                        if (DEBUG)
                        {
                            Debug.WriteLine("Creating params from root: " + root);
                        }

                        // Create layout params that match root, if supplied
                        lparams = root.generateLayoutParams(attrs);

                        if (!attachToRoot)
                        {
                            // Set the layout params for temp if we are not
                            // attaching. (If we are, we use addView, below)
                            temp.setLayoutParams(lparams);
                        }
                    }

                    if (DEBUG)
                    {
                        Debug.WriteLine("-----> start inflating children");
                    }

                    // Inflate all children under temp
                    rInflate(parser, temp, attrs, true, true);

                    if (DEBUG)
                    {
                        Debug.WriteLine("-----> done inflating children");
                    }

                    // We are supposed to attach all the views we found (int temp)
                    // to root. Do that now.
                    if (root != null && attachToRoot)
                    {
                        root.addView(temp, lparams);
                    }

                    // Decide whether to return the root that was passed in or the
                    // top view found in xml.
                    if (root == null || !attachToRoot)
                    {
                        result = temp;
                    }
                }
            }
            catch
            {
                throw;
            }

            finally
            {
                // Don't retain static reference on context.
                mConstructorArgs[0] = lastContext;
                mConstructorArgs[1] = null;
            }

            //Trace.traceEnd(Trace.TRACE_TAG_VIEW);

            return(result);
        }
        private void parseInclude(XmlPullParser parser, View parent, AttributeSet attrs, bool inheritContext)
        {
            int type;

            //if (parent.GetType().Equals(typeof(ViewGroup)))
            if (parent is ViewGroup)
            {
                int layout = attrs.getAttributeResourceValue(null, "layout", 0); //This should return resource value, not name
                if (layout == 0)
                {
                    string value = attrs.getAttributeValue(null, "layout");
                    if (value == null)
                    {
                        throw new Exception("You must specifiy a layout in the" + " include tag: <include layout=\"@layout/layoutID\" />");
                    }
                    else
                    {
                        throw new Exception("You must specifiy a valid layout " + "reference. The layout ID " + value + " is not valid.");
                    }
                }

                else
                {
                    XmlResourceParser childParser = getContext().getResources().getLayout(layout);

                    try
                    {
                        AttributeSet childAttrs = Xml.asAttributeSet(childParser, mContext);

                        while ((type = childParser.next()) != XmlPullParser.START_TAG && type != XmlPullParser.END_DOCUMENT)
                        {
                            // Empty.
                        }

                        if (type != XmlPullParser.START_TAG)
                        {
                            throw new Exception(childParser.getPositionDescription() + ": No start tag found!");
                        }

                        string childName = childParser.getName();

                        if (TAG_MERGE.Equals(childName))
                        {
                            // Inflate all children.
                            rInflate(childParser, parent, childAttrs, false, inheritContext);
                        }
                        else
                        {
                            View      view  = createViewFromTag(parent, childName, childAttrs, inheritContext);
                            ViewGroup group = (ViewGroup)parent;

                            // We try to load the layout params set in the <include /> tag. If
                            // they don't exist, we will rely on the layout params set in the
                            // included XML file.
                            // During a layoutparams generation, a runtime exception is thrown
                            // if either layout_width or layout_height is missing. We catch
                            // this exception and set localParams accordingly: true means we
                            // successfully loaded layout params from the <include /> tag,
                            // false means we need to rely on the included layout params.
                            ViewGroup.LayoutParams lparams = null;
                            try
                            {
                                lparams = group.generateLayoutParams(attrs);
                            }
                            catch
                            {
                                lparams = group.generateLayoutParams(childAttrs);
                            }
                            finally
                            {
                                if (lparams != null)
                                {
                                    view.setLayoutParams(lparams);
                                }
                            }

                            // Inflate all children.
                            rInflate(childParser, view, childAttrs, true, true);

                            /*
                             * // Attempt to override the included layout's android:id with the
                             * // one set on the <include /> tag itself.
                             * TypedArray a = mContext.obtainStyledAttributes(attrs, com.android.inner.R.styleable.View, 0, 0);
                             * int id = a.getResourceId(com.android.inner.R.styleable.View_id, View.NO_ID);
                             * // While we're at it, let's try to override android:visibility.
                             * int visibility = a.getInt(com.android.inner.R.styleable.View_visibility, -1);
                             * a.recycle();
                             *
                             * if (id != View.NO_ID)
                             * {
                             *  view.setId(id);
                             * }
                             *
                             * switch (visibility)
                             * {
                             *  case 0:
                             *      view.setVisibility(View.VISIBLE);
                             *      break;
                             *  case 1:
                             *      view.setVisibility(View.INVISIBLE);
                             *      break;
                             *  case 2:
                             *      view.setVisibility(View.GONE);
                             *      break;
                             * }
                             */

                            group.addView(view);
                        }
                    }

                    finally { }
                }
            }
            else
            {
                throw new Exception("<include /> can only be used inside of a ViewGroup");
            }

            int currentDepth = parser.getDepth();

            while (((type = parser.next()) != XmlPullParser.END_TAG || parser.getDepth() > currentDepth) && type != XmlPullParser.END_DOCUMENT)
            {
                // Empty
            }
        }