res

package
v0.6.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 12, 2025 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package res handles understanding and representing information about Android resources.

Index

Constants

View Source
const (
	// UnknownType needs to be zero value
	UnknownType Type = -2
	ValueType        = -1

	// Anim represents Android Anim resource types.
	Anim = Type(rdpb.Resource_ANIM)
	// Animator represents Android Animator resource types.
	Animator = Type(rdpb.Resource_ANIMATOR)
	// Array represents Android Array resource types.
	Array = Type(rdpb.Resource_ARRAY)
	// Attr represents Android Attr resource types.
	Attr = Type(rdpb.Resource_ATTR)
	// AttrPrivate represents Android AttrPrivate resource types.
	AttrPrivate = Type(rdpb.Resource_ATTR_PRIVATE)
	// Bool represents Android Bool resource types.
	Bool = Type(rdpb.Resource_BOOL)
	// Color represents Android Color resource types.
	Color = Type(rdpb.Resource_COLOR)
	// ConfigVarying represents Android ConfigVarying resource types, not really a type, but it shows up in some CTS tests
	ConfigVarying = Type(rdpb.Resource_CONFIG_VARYING)
	// Dimen represents Android Dimen resource types.
	Dimen = Type(rdpb.Resource_DIMEN)
	// Drawable represents Android Drawable resource types.
	Drawable = Type(rdpb.Resource_DRAWABLE)
	// Font represents Android Font resource types.
	Font = Type(rdpb.Resource_FONT)
	// Fraction represents Android Fraction resource types.
	Fraction = Type(rdpb.Resource_FRACTION)
	// ID represents Android Id resource types.
	ID = Type(rdpb.Resource_ID)
	// Integer represents Android Integer resource types.
	Integer = Type(rdpb.Resource_INTEGER)
	// Interpolator represents Android Interpolator resource types.
	Interpolator = Type(rdpb.Resource_INTERPOLATOR)
	// Layout represents Android Layout resource types.
	Layout = Type(rdpb.Resource_LAYOUT)
	// Menu represents Android Menu resource types.
	Menu = Type(rdpb.Resource_MENU)
	// Mipmap represents Android Mipmap resource types.
	Mipmap = Type(rdpb.Resource_MIPMAP)
	// Navigation represents Android Navigation resource types.
	Navigation = Type(rdpb.Resource_NAVIGATION)
	// Plurals represents Android Plurals resource types.
	Plurals = Type(rdpb.Resource_PLURALS)
	// Raw represents Android Raw resource types.
	Raw = Type(rdpb.Resource_RAW)
	// String represents Android String resource types.
	String = Type(rdpb.Resource_STRING)
	// Style represents Android Style resource types.
	Style = Type(rdpb.Resource_STYLE)
	// Styleable represents Android Styleable resource types.
	Styleable = Type(rdpb.Resource_STYLEABLE)
	// Transition represents Android Transition resource types.
	Transition = Type(rdpb.Resource_TRANSITION)
	// XML represents Android Xml resource types.
	XML = Type(rdpb.Resource_XML)
)

From frameworks/base/tools/aapt2/Resource.h, except UnknownType and ValueType TODO(mauriciogg): use proto definitions and remove ValueType and UnknownType.

View Source
const (
	// GeneratedIDPrefix prefixes an attribute value whose name is IDAttrName, it indicates that
	// this id likely does not exist outside of the current document and a new Id Resource for
	// this value.
	GeneratedIDPrefix = "@+id"
)

Variables

View Source
var (
	// IDAttrName is the android:id attribute xml name.
	// It appears anywhere an xml document wishes to associate a tag to a given android id.
	IDAttrName = xml.Name{Space: "http://schemas.android.com/apk/res/android", Local: "id"}

	// ResourcesTagName <resources> tag wraps all xml documents in res/values directory. These
	// documents are reasonably well structured, and its children _normally_ end up becoming
	// ResourceValues in Android. The exception being <declare-styleable> and <attr> which
	// define how to interpret and store attributes in xml files outside of res/values.
	ResourcesTagName = xml.Name{Local: "resources"}

	// ItemTagName is used in various ways in a <resources> tag. If it is a direct child, it can
	// only denote an id resource. Otherwise, it can be a child of array/*-array and denotes the
	// type and wraps the value of the item of the array.
	ItemTagName = xml.Name{Local: "item"}

	// NameAttrName is an attribute that is expected to be encountered on every tag that is a
	// direct child of <resources>. The value of this tag is the name of the resource that is
	// being generated.
	NameAttrName = xml.Name{Local: "name"}

	// TypeAttrName is the type attribute xml name.
	// It appears in the <item> tag when the item wants to specify its type.
	TypeAttrName = xml.Name{Local: "type"}

	// EnumTagName <enum> appears beneath <attr/> tags to define valid enum values for an attribute.
	EnumTagName = xml.Name{Local: "enum"}

	// FlagTagName <flag> appears beneath <attr/> tags to define valid flag values for an attribute.
	FlagTagName = xml.Name{Local: "flag"}

	// ResourcesTagToType maps the child tag name of resources to the resource type it will generate.
	ResourcesTagToType = map[string]Type{
		"array":             Array,
		"integer-array":     Array,
		"string-array":      Array,
		"attr":              Attr,
		"^attr-private":     AttrPrivate,
		"bool":              Bool,
		"color":             Color,
		"configVarying":     ConfigVarying,
		"dimen":             Dimen,
		"drawable":          Drawable,
		"fraction":          Fraction,
		"id":                ID,
		"integer":           Integer,
		"layout":            Layout,
		"plurals":           Plurals,
		"string":            String,
		"style":             Style,
		"declare-styleable": Styleable,
	}

	// ResourcesChildToSkip a map containing child tags that can be skipped while parsing resources.
	ResourcesChildToSkip = map[xml.Name]bool{
		{Local: "skip"}:        true,
		{Local: "eat-comment"}: true,
		{Local: "public"}:      true,
	}
)
View Source
var (

	// AllTypes is a list of all known resource types.
	AllTypes = make([]Type, 0, len(typeToString))
)
View Source
var ErrNotResPath = errors.New("Not a resource path")

ErrNotResPath the provided path does not seem to point to a resource file

View Source
var ErrSkipResPath = errors.New("resource path that does needs to be skipped")

ErrSkipResPath the provided path does needs to be skipped.

View Source
var (
	// ErrWrongType occurs when a type is used in an operation that it does not support.
	ErrWrongType = errors.New("this type cannot be used in this operation")
)

Functions

func StyleableAttrName

func StyleableAttrName(styleable, attr FullyQualifiedName) (string, error)

StyleableAttrName creates the java identifier for referencing this attribute in the given style.

Types

type Density

type Density uint16

Density represents the dpi value of a resource.

const (
	// UnspecifiedDensity is a default value indicating no dpi has been specified
	UnspecifiedDensity Density = 0

	// LDPI has a dpi of 120
	LDPI Density = 120
	// MDPI has a dpi of 160
	MDPI Density = 160
	// TVDPI has a dpi of 213
	TVDPI Density = 213
	// HDPI has a dpi of 240
	HDPI Density = 240
	// XhDPI has a dpi of 320
	XhDPI Density = 320
	// XxhDPI has a dpi of 480
	XxhDPI Density = 480
	// XxxhDPI has a dpi of 640
	XxxhDPI Density = 640
	// AnyDPI indicates a resource which can be any dpi.
	AnyDPI Density = 0xfffe
	// NoDPI indicates the resources have no dpi constraints
	NoDPI Density = 0xffff
)

From frameworks/base/core/java/Android/content/res/Configuration.java

func ParseDensity

func ParseDensity(s string) (Density, error)

ParseDensity converts a string representation of a density into a Density.

type FullyQualifiedName

type FullyQualifiedName struct {
	Package string
	Type    Type
	Name    string
}

FullyQualifiedName represents the components of a name.

func ParseName

func ParseName(unparsed string, resType Type) (FullyQualifiedName, error)

ParseName is given a name string and optional context about the name (what type the name may be) and attempts to extract the local name, Type, and package from the unparsed input. The format of unparsed names is flexible and not well specified. A FullyQualifiedName's String method will emit pkg:type/name which every tool understands, but ParseName will encounter input like ?type:pkg/name - an undocumented, but legal way to specify a reference to a style. If unparsed is so mangled that a legal name cannot possibly be determined, it will return an error.

func (FullyQualifiedName) JavaName

func (f FullyQualifiedName) JavaName() (string, error)

JavaName returns a version of the FullyQualifiedName that should be used for resource identifier fields.

func (FullyQualifiedName) SetMetaData

func (f FullyQualifiedName) SetMetaData(md *rmpb.StyleableMetaData) error

SetMetaData sets all name related fields for this style on a StyleableMetaData proto

func (FullyQualifiedName) SetResource

func (f FullyQualifiedName) SetResource(r *rdpb.Resource) error

SetResource sets all the name related fields on the top level resource proto.

func (FullyQualifiedName) String

func (f FullyQualifiedName) String() string

type Kind

type Kind uint8

Kind indicates what type of resource file emits this resource. A resource can be found in res/values folder (and therefore is a Value - which can be represented as a ResourceValue in Android) or in folders outside of res/values (such as res/layout) and thus are not ResourceValues but rather some external resource (such as an image or parsed xml file).

const (
	// Unknown should not be encountered.
	Unknown Kind = iota
	// Value can only be encountered in res/values folders.
	Value
	// NonValue can not be encountered in res/values folders.
	NonValue
	// Both  is a Kind of Type which may be inside a res/values folder or in another res/ folder.
	Both
)

type PathInfo

type PathInfo struct {
	Path      string
	ResDir    string
	TypeDir   string
	Type      Type
	Qualifier string
	Density   Density
}

PathInfo contains all information about a resource that can be derived from its location on the filesystem.

func MakePathInfo

func MakePathInfo(p string) (*PathInfo, error)

MakePathInfo converts a path string into a PathInfo object.

func MakePathInfos

func MakePathInfos(paths []string) ([]*PathInfo, error)

MakePathInfos converts a list of path strings into a list of PathInfo objects.

func ParsePath

func ParsePath(p string) (PathInfo, error)

ParsePath converts a path string into a PathInfo object if the string points to a resource file.

type Type

type Type rdpb.Resource_Type

Type of resource (eg: string, layout, drawable)

func ParseType

func ParseType(s string) (Type, error)

ParseType converts a string into a well known type

func ParseValueOrType

func ParseValueOrType(s string) (Type, error)

ParseValueOrType converts a string into a value type or well known type

func (Type) Enum

func (t Type) Enum() (rdpb.Resource_Type, error)

Enum converts a Type into a enum proto value

func (Type) IsReal

func (t Type) IsReal() bool

IsReal indicates that the type is known to the android framework.

func (Type) IsSerializable

func (t Type) IsSerializable() bool

IsSerializable indicates that the Type can be converted to a proto (some types are only for in memory operations).

func (Type) Kind

func (t Type) Kind() Kind

Kind indicates the resource kind of this type.

func (Type) NestedClassName

func (t Type) NestedClassName() (string, error)

NestedClassName is the R.java nested class name for this type (if the type is understood by android).

func (Type) String

func (t Type) String() string

String for Type structs corresponds to the string format known to Android.

type ValuesResource

type ValuesResource struct {
	Src     *PathInfo
	N       FullyQualifiedName
	Payload []byte
}

ValuesResource represents a resource element.

Directories

Path Synopsis
Package respipe contains utilities for running pipelines on android resources.
Package respipe contains utilities for running pipelines on android resources.
Package resxml contains common functions to extract information from xml files and feed that information into the resource processing pipeline.
Package resxml contains common functions to extract information from xml files and feed that information into the resource processing pipeline.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL