Skip to content

Commit 727b529

Browse files
committed
add comments
1 parent 40e6c18 commit 727b529

5 files changed

Lines changed: 56 additions & 5 deletions

File tree

apk/apk.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/shogo82148/androidbinary"
1616
)
1717

18+
// Apk is an application package file for android.
1819
type Apk struct {
1920
f *os.File
2021
zipreader *zip.Reader
@@ -71,7 +72,7 @@ func (k *Apk) Close() error {
7172
return k.f.Close()
7273
}
7374

74-
// Icon return icon image
75+
// Icon returns the icon image of the APK.
7576
func (k *Apk) Icon(resConfig *androidbinary.ResTableConfig) (image.Image, error) {
7677
iconPath := k.getResource(k.manifest.App.Icon, resConfig)
7778
if androidbinary.IsResID(iconPath) {
@@ -85,6 +86,7 @@ func (k *Apk) Icon(resConfig *androidbinary.ResTableConfig) (image.Image, error)
8586
return m, err
8687
}
8788

89+
// Label returns the label of the APK.
8890
func (k *Apk) Label(resConfig *androidbinary.ResTableConfig) (s string, err error) {
8991
s = k.getResource(k.manifest.App.Label, resConfig)
9092
if androidbinary.IsResID(s) {
@@ -93,14 +95,17 @@ func (k *Apk) Label(resConfig *androidbinary.ResTableConfig) (s string, err erro
9395
return
9496
}
9597

98+
// Manifest returns the manifest of the APK.
9699
func (k *Apk) Manifest() Manifest {
97100
return k.manifest
98101
}
99102

103+
// PackageName returns the package name of the APK.
100104
func (k *Apk) PackageName() string {
101105
return k.manifest.Package
102106
}
103107

108+
// MainAcitivty returns the name of the main activity.
104109
func (k *Apk) MainAcitivty() (activity string, err error) {
105110
for _, act := range k.manifest.App.Activity {
106111
for _, intent := range act.IntentFilter {

apk/apkxml.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,38 @@
11
package apk
22

3+
// Instrumentation is an application instrumentation code.
34
type Instrumentation struct {
45
Name string `xml:"name,attr"`
56
Target string `xml:"targetPackage,attr"`
67
HandleProfiling bool `xml:"handleProfiling,attr"`
78
FunctionalTest bool `xml:"functionalTest,attr"`
89
}
910

11+
// ActivityAction is an action of an activity.
1012
type ActivityAction struct {
1113
Name string `xml:"name,attr"`
1214
}
1315

16+
// ActivityCategory is a category of an activity.
1417
type ActivityCategory struct {
1518
Name string `xml:"name,attr"`
1619
}
1720

21+
// ActivityIntentFilter is an intent filter of an activity.
1822
type ActivityIntentFilter struct {
1923
Action ActivityAction `xml:"action"`
2024
Category ActivityCategory `xml:"category"`
2125
}
2226

27+
// AppActivity is an activity in an application.
2328
type AppActivity struct {
2429
Theme string `xml:"theme,attr"`
2530
Name string `xml:"name,attr"`
2631
Label string `xml:"label,attr"`
2732
IntentFilter []ActivityIntentFilter `xml:"intent-filter"`
2833
}
2934

35+
// Application is an application in an APK.
3036
type Application struct {
3137
AllowTaskReparenting bool `xml:"allowTaskReparenting,attr"`
3238
AllowBackup bool `xml:"allowBackup,attr"`
@@ -53,22 +59,24 @@ type Application struct {
5359
TaskAffinity string `xml:"taskAffinity,attr"`
5460
TestOnly bool `xml:"testOnly,attr"`
5561
Theme string `xml:"theme,attr"`
56-
UiOptions string `xml:"uiOptions,attr"`
57-
VmSafeMode bool `xml:"vmSafeMode,attr"`
62+
UIOptions string `xml:"uiOptions,attr"`
63+
VMSafeMode bool `xml:"vmSafeMode,attr"`
5864
Activity []AppActivity `xml:"activity"`
5965
}
6066

61-
type UsesSdk struct {
67+
// UsesSDK is target SDK version.
68+
type UsesSDK struct {
6269
Min int `xml:"minSdkVersion,attr"`
6370
Target int `xml:"targetSdkVersion,attr"`
6471
Max int `xml:"maxSdkVersion,attr"`
6572
}
6673

74+
// Manifest is a manifest of an APK.
6775
type Manifest struct {
6876
Package string `xml:"package,attr"`
6977
VersionCode int `xml:"versionCode,attr"`
7078
VersionName string `xml:"versionName,attr"`
7179
App Application `xml:"application"`
7280
Instrument Instrumentation `xml:"instrumentation"`
73-
Sdk UsesSdk `xml:"uses-sdk"`
81+
SDK UsesSDK `xml:"uses-sdk"`
7482
}

common.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ const (
3232
RES_TABLE_TYPE_SPEC_TYPE = 0x0202
3333
)
3434

35+
// ResChunkHeader is a header of a resource chunk.
3536
type ResChunkHeader struct {
3637
Type uint16
3738
HeaderSize uint16
@@ -62,6 +63,7 @@ type ResStringPool struct {
6263

6364
const NilResStringPoolRef = ResStringPoolRef(0xFFFFFFFF)
6465

66+
// ResStringPoolRef is a type representing a reference to a string.
6567
type ResStringPoolRef uint32
6668

6769
const (
@@ -85,13 +87,15 @@ const (
8587
TYPE_LAST_INT = 0x1f
8688
)
8789

90+
// ResValue is a representation of a value in a resource
8891
type ResValue struct {
8992
Size uint16
9093
Res0 uint8
9194
DataType uint8
9295
Data uint32
9396
}
9497

98+
// GetString returns a string referenced by ref.
9599
func (pool *ResStringPool) GetString(ref ResStringPoolRef) string {
96100
return pool.Strings[int(ref)]
97101
}

table.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,22 @@ import (
99
"unsafe"
1010
)
1111

12+
// ResID is ID for resources.
1213
type ResID uint32
1314

15+
// TableFile is a resrouce table file.
1416
type TableFile struct {
1517
stringPool *ResStringPool
1618
tablePackages map[uint32]*TablePackage
1719
}
1820

21+
// ResTableHeader is a header of TableFile.
1922
type ResTableHeader struct {
2023
Header ResChunkHeader
2124
PackageCount uint32
2225
}
2326

27+
// ResTablePackage is a header of table packages.
2428
type ResTablePackage struct {
2529
Header ResChunkHeader
2630
ID uint32
@@ -31,13 +35,15 @@ type ResTablePackage struct {
3135
LastPublicKey uint32
3236
}
3337

38+
// TablePackage is a table package.
3439
type TablePackage struct {
3540
Header ResTablePackage
3641
TypeStrings *ResStringPool
3742
KeyStrings *ResStringPool
3843
TableTypes []*TableType
3944
}
4045

46+
// ResTableType is a type of a table.
4147
type ResTableType struct {
4248
Header ResChunkHeader
4349
ID uint8
@@ -99,6 +105,7 @@ const (
99105
NAVHIDDEN_YES = 0x08
100106
)
101107

108+
// ResTableConfig is a configuration of a table.
102109
type ResTableConfig struct {
103110
Size uint32
104111
// imsi
@@ -138,23 +145,27 @@ type ResTableConfig struct {
138145
ScreenHeightDp uint16
139146
}
140147

148+
// TableType is a collection of resource entries for a particular resource data type.
141149
type TableType struct {
142150
Header *ResTableType
143151
Entries []TableEntry
144152
}
145153

154+
// ResTableEntry is the beginning of information about an entry in the resource table.
146155
type ResTableEntry struct {
147156
Size uint16
148157
Flags uint16
149158
Key ResStringPoolRef
150159
}
151160

161+
// TableEntry is a entry in a recource table.
152162
type TableEntry struct {
153163
Key *ResTableEntry
154164
Value *ResValue
155165
Flags uint32
156166
}
157167

168+
// ResTableTypeSpec is specification of the resources defined by a particular type.
158169
type ResTableTypeSpec struct {
159170
Header ResChunkHeader
160171
ID uint8
@@ -184,18 +195,22 @@ func (id ResID) String() string {
184195
return fmt.Sprintf("@0x%08X", uint32(id))
185196
}
186197

198+
// Package returns the package index of id.
187199
func (id ResID) Package() int {
188200
return int(id) >> 24
189201
}
190202

203+
// Type returns the type index of id.
191204
func (id ResID) Type() int {
192205
return (int(id) >> 16) & 0xFF
193206
}
194207

208+
// Entry returns the entry index of id.
195209
func (id ResID) Entry() int {
196210
return int(id) & 0xFFFF
197211
}
198212

213+
// NewTableFile returns new TableFile.
199214
func NewTableFile(r io.ReaderAt) (*TableFile, error) {
200215
f := new(TableFile)
201216
sr := io.NewSectionReader(r, 0, 1<<63-1)
@@ -241,6 +256,7 @@ func (p *TablePackage) findEntry(typeIndex, entryIndex int, config *ResTableConf
241256
return best.Entries[entryIndex]
242257
}
243258

259+
// GetResource returns a resrouce referenced by id.
244260
func (f *TableFile) GetResource(id ResID, config *ResTableConfig) (interface{}, error) {
245261
p := f.findPackage(id.Package())
246262
if p == nil {
@@ -266,6 +282,7 @@ func (f *TableFile) GetResource(id ResID, config *ResTableConfig) (interface{},
266282
return v.Data, nil
267283
}
268284

285+
// GetString returns a string referenced by ref.
269286
func (f *TableFile) GetString(ref ResStringPoolRef) string {
270287
return f.stringPool.GetString(ref)
271288
}
@@ -413,6 +430,7 @@ func readTableTypeSpec(sr *io.SectionReader) ([]uint32, error) {
413430
return flags, nil
414431
}
415432

433+
// IsMoreSpecificThan returns true if c is more specific than o.
416434
func (c *ResTableConfig) IsMoreSpecificThan(o *ResTableConfig) bool {
417435
// nil ResTableConfig is never more specific than any ResTableConfig
418436
if c == nil {
@@ -637,6 +655,7 @@ func (c *ResTableConfig) IsMoreSpecificThan(o *ResTableConfig) bool {
637655
return false
638656
}
639657

658+
// IsBetterThan returns true if c is better than o for the r configuration.
640659
func (c *ResTableConfig) IsBetterThan(o *ResTableConfig, r *ResTableConfig) bool {
641660
if r == nil {
642661
return c.IsMoreSpecificThan(o)
@@ -842,6 +861,9 @@ func (c *ResTableConfig) IsBetterThan(o *ResTableConfig, r *ResTableConfig) bool
842861
return false
843862
}
844863

864+
// IsLocaleMoreSpecificThan a positive integer if this config is more specific than o,
865+
// a negative integer if |o| is more specific
866+
// and 0 if they're equally specific.
845867
func (c *ResTableConfig) IsLocaleMoreSpecificThan(o *ResTableConfig) int {
846868
if (c.Language != [2]uint8{} || c.Country != [2]uint8{}) || (o.Language != [2]uint8{} || o.Country != [2]uint8{}) {
847869
if c.Language != o.Language {
@@ -865,6 +887,7 @@ func (c *ResTableConfig) IsLocaleMoreSpecificThan(o *ResTableConfig) int {
865887
return 0
866888
}
867889

890+
// IsLocaleBetterThan returns true if c is a better locale match than o for the r configuration.
868891
func (c *ResTableConfig) IsLocaleBetterThan(o *ResTableConfig, r *ResTableConfig) bool {
869892
if r.Language == [2]uint8{} && r.Country == [2]uint8{} {
870893
// The request doesn't have a locale, so no resource is better
@@ -900,6 +923,7 @@ func (c *ResTableConfig) IsLocaleBetterThan(o *ResTableConfig, r *ResTableConfig
900923
return false
901924
}
902925

926+
// Match returns true if c can be considered a match for the parameters in settings.
903927
func (c *ResTableConfig) Match(settings *ResTableConfig) bool {
904928
// nil ResTableConfig always matches.
905929
if settings == nil {
@@ -1045,6 +1069,7 @@ func (c *ResTableConfig) Match(settings *ResTableConfig) bool {
10451069
return true
10461070
}
10471071

1072+
// Locale returns the locale of the configuration.
10481073
func (c *ResTableConfig) Locale() string {
10491074
if c.Language[0] == 0 {
10501075
return ""

xml.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"io"
99
)
1010

11+
// XMLFile is an XML file expressed in binary format.
1112
type XMLFile struct {
1213
stringPool *ResStringPool
1314
resourceMap []uint32
@@ -16,17 +17,20 @@ type XMLFile struct {
1617
xmlBuffer bytes.Buffer
1718
}
1819

20+
// ResXMLTreeNode is basic XML tree node.
1921
type ResXMLTreeNode struct {
2022
Header ResChunkHeader
2123
LineNumber uint32
2224
Comment ResStringPoolRef
2325
}
2426

27+
// ResXMLTreeNamespaceExt is extended XML tree node for namespace start/end nodes.
2528
type ResXMLTreeNamespaceExt struct {
2629
Prefix ResStringPoolRef
2730
URI ResStringPoolRef
2831
}
2932

33+
// ResXMLTreeAttrExt is extended XML tree node for start tags -- includes attribute.
3034
type ResXMLTreeAttrExt struct {
3135
NS ResStringPoolRef
3236
Name ResStringPoolRef
@@ -38,18 +42,21 @@ type ResXMLTreeAttrExt struct {
3842
StyleIndex uint16
3943
}
4044

45+
// ResXMLTreeAttribute is an attribute of start tags.
4146
type ResXMLTreeAttribute struct {
4247
NS ResStringPoolRef
4348
Name ResStringPoolRef
4449
RawValue ResStringPoolRef
4550
TypedValue ResValue
4651
}
4752

53+
// ResXMLTreeEndElementExt is extended XML tree node for element start/end nodes.
4854
type ResXMLTreeEndElementExt struct {
4955
NS ResStringPoolRef
5056
Name ResStringPoolRef
5157
}
5258

59+
// NewXMLFile returns a new XMLFile.
5360
func NewXMLFile(r io.ReaderAt) (*XMLFile, error) {
5461
f := new(XMLFile)
5562
sr := io.NewSectionReader(r, 0, 1<<63-1)
@@ -71,6 +78,7 @@ func NewXMLFile(r io.ReaderAt) (*XMLFile, error) {
7178
return f, nil
7279
}
7380

81+
// Reader returns a reader of XML file expressed in text format.
7482
func (f *XMLFile) Reader() *bytes.Reader {
7583
return bytes.NewReader(f.xmlBuffer.Bytes())
7684
}
@@ -108,6 +116,7 @@ func (f *XMLFile) readChunk(r io.ReaderAt, offset int64) (*ResChunkHeader, error
108116
return chunkHeader, nil
109117
}
110118

119+
// GetString returns a string referenced by ref.
111120
func (f *XMLFile) GetString(ref ResStringPoolRef) string {
112121
return f.stringPool.GetString(ref)
113122
}

0 commit comments

Comments
 (0)