Exemplo n.º 1
0
        /**
         * get resource value by string-format via resourceId.
         */
        public static async Task <string> getResourceById(long resourceId, ResourceTable resourceTable, CultureInfo locale)
        {
            //        An Android Resource id is a 32-bit integer. It comprises
            //        an 8-bit Package id [bits 24-31]
            //        an 8-bit Type id [bits 16-23]
            //        a 16-bit Entry index [bits 0-15]

            // android system styles.
            if (resourceId > AndroidConstants.SYS_STYLE_ID_START && resourceId < AndroidConstants.SYS_STYLE_ID_END)
            {
                return("@android:style/" + ResourceTable.sysStyle[(int)resourceId]); //get((int)resourceId);
            }

            string str = "resourceId:0x" + resourceId.ToString("X");

            if (resourceTable == null)
            {
                return(str);
            }

            short           packageId       = (short)(resourceId >> 24 & 0xff);
            short           typeId          = (short)((resourceId >> 16) & 0xff);
            int             entryIndex      = (int)(resourceId & 0xffff);
            ResourcePackage resourcePackage = resourceTable.getPackage(packageId);

            if (resourcePackage == null)
            {
                return(str);
            }
            TypeSpec     typeSpec = resourcePackage.getTypeSpec(typeId);
            List <RType> types    = resourcePackage.getTypes(typeId);

            if (typeSpec == null || types == null)
            {
                return(str);
            }
            if (!typeSpec.exists(entryIndex))
            {
                return(str);
            }

            // read from type resource
            ResourceEntry resource     = null;
            string        ref_         = null;
            int           currentLevel = -1;

            foreach (RType type in types)
            {
                ResourceEntry curResourceEntry = await type.getResourceEntry(entryIndex);

                if (curResourceEntry == null)
                {
                    continue;
                }

                ref_ = curResourceEntry.getKey();

                ResourceValue currentResourceValue = curResourceEntry.getValue();
                if (currentResourceValue == null)
                {
                    continue;
                }

                // cyclic reference detect
                if (currentResourceValue is ResourceValue.ReferenceResourceValue) //instanceof ResourceValue.ReferenceResourceValue)
                {
                    if (resourceId == ((ResourceValue.ReferenceResourceValue)currentResourceValue).getReferenceResourceId())
                    {
                        continue;
                    }
                }

                int level = Locales.match(locale, type.getLocale());
                if (level == 2)
                {
                    resource = curResourceEntry;
                    break;
                }
                else if (level > currentLevel)
                {
                    resource     = curResourceEntry;
                    currentLevel = level;
                }
            }

            string result;

            if (locale == null || resource == null)
            {
                result = "@" + typeSpec.getName() + "/" + ref_;
            }
            else
            {
                result = resource.toStringValue(resourceTable, locale);
            }
            return(result);
        }