Skip to content

Commit 9afdf83

Browse files
authored
Merge pull request #1 from shogo82148/master
update from origin
2 parents a2ff677 + ced17da commit 9afdf83

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+1259
-104
lines changed

.travis.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,6 @@ go:
55
- '1.8.x'
66
- '1.9.x'
77
- '1.10.x'
8+
- '1.11.x'
9+
- '1.12.x'
810
- 'tip'

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ func main() {
2222
defer pkg.Close()
2323

2424
icon, _ := pkg.Icon(nil) // returns the icon of APK as image.Image
25-
pkgName := pkg.PackageName() // returns the pakcage name
25+
pkgName := pkg.PackageName() // returns the package name
2626
}
2727
```
2828

29-
## Low Lebel API
29+
## Low Level API
3030

3131
### Parse XML binary
3232

@@ -65,7 +65,7 @@ import (
6565
func main() {
6666
f, _ := os.Open("resources.arsc")
6767
rsc, _ := androidbinary.NewTableFile(f)
68-
resorce, _ := rsc.GetResource(androidbinary.ResID(0xCAFEBABE), nil)
68+
resource, _ := rsc.GetResource(androidbinary.ResID(0xCAFEBABE), nil)
6969
fmt.Println(resource)
7070
}
7171
```

apk/apk.go

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,9 @@ package apk
33
import (
44
"archive/zip"
55
"bytes"
6-
"encoding/xml"
76
"fmt"
87
"image"
98
"io"
10-
"io/ioutil"
119
"os"
1210
"strconv"
1311

@@ -58,12 +56,12 @@ func OpenZipReader(r io.ReaderAt, size int64) (*Apk, error) {
5856
apk := &Apk{
5957
zipreader: zipreader,
6058
}
61-
if err = apk.parseManifest(); err != nil {
62-
return nil, errors.Wrap(err, "parse-manifest")
63-
}
6459
if err = apk.parseResources(); err != nil {
6560
return nil, err
6661
}
62+
if err = apk.parseManifest(); err != nil {
63+
return nil, errors.Wrap(err, "parse-manifest")
64+
}
6765
return apk, nil
6866
}
6967

@@ -77,7 +75,10 @@ func (k *Apk) Close() error {
7775

7876
// Icon returns the icon image of the APK.
7977
func (k *Apk) Icon(resConfig *androidbinary.ResTableConfig) (image.Image, error) {
80-
iconPath := k.getResource(k.manifest.App.Icon, resConfig)
78+
iconPath, err := k.manifest.App.Icon.WithResTableConfig(resConfig).String()
79+
if err != nil {
80+
return nil, err
81+
}
8182
if androidbinary.IsResID(iconPath) {
8283
return nil, errors.New("unable to convert icon-id to icon path")
8384
}
@@ -91,7 +92,10 @@ func (k *Apk) Icon(resConfig *androidbinary.ResTableConfig) (image.Image, error)
9192

9293
// Label returns the label of the APK.
9394
func (k *Apk) Label(resConfig *androidbinary.ResTableConfig) (s string, err error) {
94-
s = k.getResource(k.manifest.App.Label, resConfig)
95+
s, err = k.manifest.App.Label.WithResTableConfig(resConfig).String()
96+
if err != nil {
97+
return
98+
}
9599
if androidbinary.IsResID(s) {
96100
err = errors.New("unable to convert label-id to string")
97101
}
@@ -105,13 +109,14 @@ func (k *Apk) Manifest() Manifest {
105109

106110
// PackageName returns the package name of the APK.
107111
func (k *Apk) PackageName() string {
108-
return k.manifest.Package
112+
return k.manifest.Package.MustString()
109113
}
110114

111115
func isMainIntentFilter(intent ActivityIntentFilter) bool {
112116
ok := false
113117
for _, action := range intent.Actions {
114-
if action.Name == "android.intent.action.MAIN" {
118+
s, err := action.Name.String()
119+
if err == nil && s == "android.intent.action.MAIN" {
115120
ok = true
116121
break
117122
}
@@ -121,7 +126,8 @@ func isMainIntentFilter(intent ActivityIntentFilter) bool {
121126
}
122127
ok = false
123128
for _, category := range intent.Categories {
124-
if category.Name == "android.intent.category.LAUNCHER" {
129+
s, err := category.Name.String()
130+
if err == nil && s == "android.intent.category.LAUNCHER" {
125131
ok = true
126132
break
127133
}
@@ -134,14 +140,14 @@ func (k *Apk) MainActivity() (activity string, err error) {
134140
for _, act := range k.manifest.App.Activities {
135141
for _, intent := range act.IntentFilters {
136142
if isMainIntentFilter(intent) {
137-
return act.Name, nil
143+
return act.Name.String()
138144
}
139145
}
140146
}
141147
for _, act := range k.manifest.App.ActivityAliases {
142148
for _, intent := range act.IntentFilters {
143149
if isMainIntentFilter(intent) {
144-
return act.TargetActivity, nil
150+
return act.TargetActivity.String()
145151
}
146152
}
147153
}
@@ -158,12 +164,7 @@ func (k *Apk) parseManifest() error {
158164
if err != nil {
159165
return errors.Wrap(err, "parse-xml")
160166
}
161-
reader := xmlfile.Reader()
162-
data, err := ioutil.ReadAll(reader)
163-
if err != nil {
164-
return err
165-
}
166-
return xml.Unmarshal(data, &k.manifest)
167+
return xmlfile.Decode(&k.manifest, k.table, nil)
167168
}
168169

169170
func (k *Apk) parseResources() (err error) {
@@ -175,18 +176,6 @@ func (k *Apk) parseResources() (err error) {
175176
return
176177
}
177178

178-
func (k *Apk) getResource(id string, resConfig *androidbinary.ResTableConfig) string {
179-
resID, err := androidbinary.ParseResID(id)
180-
if err != nil {
181-
return id
182-
}
183-
val, err := k.table.GetResource(resID, resConfig)
184-
if err != nil {
185-
return id
186-
}
187-
return fmt.Sprintf("%s", val)
188-
}
189-
190179
func (k *Apk) readZipFile(name string) (data []byte, err error) {
191180
buf := bytes.NewBuffer(nil)
192181
for _, file := range k.zipreader.File {

apk/apk_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func TestParseAPKFile(t *testing.T) {
2727
assert.Equal(t, "com.example.helloworld", apk.PackageName())
2828

2929
manifest := apk.Manifest()
30-
assert.Equal(t, manifest.SDK.Target, 24)
30+
assert.Equal(t, manifest.SDK.Target.MustInt32(), int32(24))
3131

3232
mainActivity, err := apk.MainActivity()
3333
assert.NoError(t, err)

apk/apkxml.go

Lines changed: 58 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,98 +1,102 @@
11
package apk
22

3+
import (
4+
"github.com/shogo82148/androidbinary"
5+
)
6+
37
// Instrumentation is an application instrumentation code.
48
type Instrumentation struct {
5-
Name string `xml:"name,attr"`
6-
Target string `xml:"targetPackage,attr"`
7-
HandleProfiling bool `xml:"handleProfiling,attr"`
8-
FunctionalTest bool `xml:"functionalTest,attr"`
9+
Name androidbinary.String `xml:"http://schemas.android.com/apk/res/android name,attr"`
10+
Target androidbinary.String `xml:"http://schemas.android.com/apk/res/android targetPackage,attr"`
11+
HandleProfiling androidbinary.Bool `xml:"http://schemas.android.com/apk/res/android handleProfiling,attr"`
12+
FunctionalTest androidbinary.Bool `xml:"http://schemas.android.com/apk/res/android functionalTest,attr"`
913
}
1014

1115
// ActivityAction is an action of an activity.
1216
type ActivityAction struct {
13-
Name string `xml:"name,attr"`
17+
Name androidbinary.String `xml:"http://schemas.android.com/apk/res/android name,attr"`
1418
}
1519

1620
// ActivityCategory is a category of an activity.
1721
type ActivityCategory struct {
18-
Name string `xml:"name,attr"`
22+
Name androidbinary.String `xml:"http://schemas.android.com/apk/res/android name,attr"`
1923
}
2024

21-
// ActivityIntentFilter is an intent filter of an activity.
25+
// ActivityIntentFilter is an androidbinary.Int32ent filter of an activity.
2226
type ActivityIntentFilter struct {
2327
Actions []ActivityAction `xml:"action"`
2428
Categories []ActivityCategory `xml:"category"`
2529
}
2630

2731
// AppActivity is an activity in an application.
2832
type AppActivity struct {
29-
Theme string `xml:"theme,attr"`
30-
Name string `xml:"name,attr"`
31-
Label string `xml:"label,attr"`
33+
Theme androidbinary.String `xml:"http://schemas.android.com/apk/res/android theme,attr"`
34+
Name androidbinary.String `xml:"http://schemas.android.com/apk/res/android name,attr"`
35+
Label androidbinary.String `xml:"http://schemas.android.com/apk/res/android label,attr"`
3236
IntentFilters []ActivityIntentFilter `xml:"intent-filter"`
3337
}
3438

3539
// AppActivityAlias https://developer.android.com/guide/topics/manifest/activity-alias-element
3640
type AppActivityAlias struct {
37-
Name string `xml:"name,attr"`
38-
Label string `xml:"label,attr"`
39-
TargetActivity string `xml:"targetActivity,attr"`
41+
Name androidbinary.String `xml:"http://schemas.android.com/apk/res/android name,attr"`
42+
Label androidbinary.String `xml:"http://schemas.android.com/apk/res/android label,attr"`
43+
TargetActivity androidbinary.String `xml:"http://schemas.android.com/apk/res/android targetActivity,attr"`
4044
IntentFilters []ActivityIntentFilter `xml:"intent-filter"`
4145
}
4246

4347
// MetaData is a metadata in an application.
4448
type MetaData struct {
45-
Name string `xml:"name,attr"`
46-
Value string `xml:"value,attr"`
49+
Name androidbinary.String `xml:"http://schemas.android.com/apk/res/android name,attr"`
50+
Value androidbinary.String `xml:"http://schemas.android.com/apk/res/android value,attr"`
4751
}
4852

4953
// Application is an application in an APK.
5054
type Application struct {
51-
AllowTaskReparenting bool `xml:"allowTaskReparenting,attr"`
52-
AllowBackup bool `xml:"allowBackup,attr"`
53-
BackupAgent string `xml:"backupAgent,attr"`
54-
Debuggable bool `xml:"debuggable,attr"`
55-
Description string `xml:"description,attr"`
56-
Enabled bool `xml:"enabled,attr"`
57-
HasCode bool `xml:"hasCode,attr"`
58-
HardwareAccelerated bool `xml:"hardwareAccelerated,attr"`
59-
Icon string `xml:"icon,attr"`
60-
KillAfterRestore bool `xml:"killAfterRestore,attr"`
61-
LargeHeap bool `xml:"largeHeap,attr"`
62-
Label string `xml:"label,attr"`
63-
Logo string `xml:"logo,attr"`
64-
ManageSpaceActivity string `xml:"manageSpaceActivity,attr"`
65-
Name string `xml:"name,attr"`
66-
Permission string `xml:"permission,attr"`
67-
Persistent bool `xml:"persistent,attr"`
68-
Process string `xml:"process,attr"`
69-
RestoreAnyVersion bool `xml:"restoreAnyVersion,attr"`
70-
RequiredAccountType string `xml:"requiredAccountType,attr"`
71-
RestrictedAccountType string `xml:"restrictedAccountType,attr"`
72-
SupportsRtl bool `xml:"supportsRtl,attr"`
73-
TaskAffinity string `xml:"taskAffinity,attr"`
74-
TestOnly bool `xml:"testOnly,attr"`
75-
Theme string `xml:"theme,attr"`
76-
UIOptions string `xml:"uiOptions,attr"`
77-
VMSafeMode bool `xml:"vmSafeMode,attr"`
78-
Activities []AppActivity `xml:"activity"`
79-
ActivityAliases []AppActivityAlias `xml:"activity-alias"`
80-
MetaData []MetaData `xml:"meta-data"`
55+
AllowTaskReparenting androidbinary.Bool `xml:"http://schemas.android.com/apk/res/android allowTaskReparenting,attr"`
56+
AllowBackup androidbinary.Bool `xml:"http://schemas.android.com/apk/res/android allowBackup,attr"`
57+
BackupAgent androidbinary.String `xml:"http://schemas.android.com/apk/res/android backupAgent,attr"`
58+
Debuggable androidbinary.Bool `xml:"http://schemas.android.com/apk/res/android debuggable,attr"`
59+
Description androidbinary.String `xml:"http://schemas.android.com/apk/res/android description,attr"`
60+
Enabled androidbinary.Bool `xml:"http://schemas.android.com/apk/res/android enabled,attr"`
61+
HasCode androidbinary.Bool `xml:"http://schemas.android.com/apk/res/android hasCode,attr"`
62+
HardwareAccelerated androidbinary.Bool `xml:"http://schemas.android.com/apk/res/android hardwareAccelerated,attr"`
63+
Icon androidbinary.String `xml:"http://schemas.android.com/apk/res/android icon,attr"`
64+
KillAfterRestore androidbinary.Bool `xml:"http://schemas.android.com/apk/res/android killAfterRestore,attr"`
65+
LargeHeap androidbinary.Bool `xml:"http://schemas.android.com/apk/res/android largeHeap,attr"`
66+
Label androidbinary.String `xml:"http://schemas.android.com/apk/res/android label,attr"`
67+
Logo androidbinary.String `xml:"http://schemas.android.com/apk/res/android logo,attr"`
68+
ManageSpaceActivity androidbinary.String `xml:"http://schemas.android.com/apk/res/android manageSpaceActivity,attr"`
69+
Name androidbinary.String `xml:"http://schemas.android.com/apk/res/android name,attr"`
70+
Permission androidbinary.String `xml:"http://schemas.android.com/apk/res/android permission,attr"`
71+
Persistent androidbinary.Bool `xml:"http://schemas.android.com/apk/res/android persistent,attr"`
72+
Process androidbinary.String `xml:"http://schemas.android.com/apk/res/android process,attr"`
73+
RestoreAnyVersion androidbinary.Bool `xml:"http://schemas.android.com/apk/res/android restoreAnyVersion,attr"`
74+
RequiredAccountType androidbinary.String `xml:"http://schemas.android.com/apk/res/android requiredAccountType,attr"`
75+
RestrictedAccountType androidbinary.String `xml:"http://schemas.android.com/apk/res/android restrictedAccountType,attr"`
76+
SupportsRtl androidbinary.Bool `xml:"http://schemas.android.com/apk/res/android supportsRtl,attr"`
77+
TaskAffinity androidbinary.String `xml:"http://schemas.android.com/apk/res/android taskAffinity,attr"`
78+
TestOnly androidbinary.Bool `xml:"http://schemas.android.com/apk/res/android testOnly,attr"`
79+
Theme androidbinary.String `xml:"http://schemas.android.com/apk/res/android theme,attr"`
80+
UIOptions androidbinary.String `xml:"http://schemas.android.com/apk/res/android uiOptions,attr"`
81+
VMSafeMode androidbinary.Bool `xml:"http://schemas.android.com/apk/res/android vmSafeMode,attr"`
82+
Activities []AppActivity `xml:"activity"`
83+
ActivityAliases []AppActivityAlias `xml:"activity-alias"`
84+
MetaData []MetaData `xml:"meta-data"`
8185
}
8286

8387
// UsesSDK is target SDK version.
8488
type UsesSDK struct {
85-
Min int `xml:"minSdkVersion,attr"`
86-
Target int `xml:"targetSdkVersion,attr"`
87-
Max int `xml:"maxSdkVersion,attr"`
89+
Min androidbinary.Int32 `xml:"http://schemas.android.com/apk/res/android minSdkVersion,attr"`
90+
Target androidbinary.Int32 `xml:"http://schemas.android.com/apk/res/android targetSdkVersion,attr"`
91+
Max androidbinary.Int32 `xml:"http://schemas.android.com/apk/res/android maxSdkVersion,attr"`
8892
}
8993

9094
// Manifest is a manifest of an APK.
9195
type Manifest struct {
92-
Package string `xml:"package,attr"`
93-
VersionCode int `xml:"versionCode,attr"`
94-
VersionName string `xml:"versionName,attr"`
95-
App Application `xml:"application"`
96-
Instrument Instrumentation `xml:"instrumentation"`
97-
SDK UsesSDK `xml:"uses-sdk"`
96+
Package androidbinary.String `xml:"package,attr"`
97+
VersionCode androidbinary.Int32 `xml:"http://schemas.android.com/apk/res/android versionCode,attr"`
98+
VersionName androidbinary.String `xml:"http://schemas.android.com/apk/res/android versionName,attr"`
99+
App Application `xml:"application"`
100+
Instrument Instrumentation `xml:"instrumentation"`
101+
SDK UsesSDK `xml:"uses-sdk"`
98102
}

common.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ type ResStringPoolSpan struct {
6868
FirstChar, LastChar uint32
6969
}
7070

71-
// ResStringPool is a string pool resrouce.
71+
// ResStringPool is a string pool resource.
7272
type ResStringPool struct {
7373
Header ResStringPoolHeader
7474
Strings []string
@@ -167,7 +167,7 @@ func readStringPool(sr *io.SectionReader) (*ResStringPool, error) {
167167
}
168168

169169
func readUTF16(sr *io.SectionReader) (string, error) {
170-
// read lenth of string
170+
// read length of string
171171
size, err := readUTF16length(sr)
172172
if err != nil {
173173
return "", err

example_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,20 @@ func ExampleNewXMLFile() {
2525
enc.Encode(v)
2626

2727
// Output:
28-
// <Manifest package="net.sorablue.shogo.FWMeasure" versionCode="1" versionName="テスト版">
29-
// <application allowTaskReparenting="false" allowBackup="false" backupAgent="" debuggable="false" description="" enabled="false" hasCode="false" hardwareAccelerated="false" icon="@0x7F020000" killAfterRestore="false" largeHeap="false" label="@0x7F040000" logo="" manageSpaceActivity="" name="" permission="" persistent="false" process="" restoreAnyVersion="false" requiredAccountType="" restrictedAccountType="" supportsRtl="false" taskAffinity="" testOnly="false" theme="" uiOptions="" vmSafeMode="false">
30-
// <activity theme="" name="FWMeasureActivity" label="">
28+
// <Manifest package="net.sorablue.shogo.FWMeasure" xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="テスト版">
29+
// <application android:allowTaskReparenting="false" android:allowBackup="false" android:backupAgent="" android:debuggable="false" android:description="" android:enabled="false" android:hasCode="false" android:hardwareAccelerated="false" android:icon="@0x7F020000" android:killAfterRestore="false" android:largeHeap="false" android:label="@0x7F040000" android:logo="" android:manageSpaceActivity="" android:name="" android:permission="" android:persistent="false" android:process="" android:restoreAnyVersion="false" android:requiredAccountType="" android:restrictedAccountType="" android:supportsRtl="false" android:taskAffinity="" android:testOnly="false" android:theme="" android:uiOptions="" android:vmSafeMode="false">
30+
// <activity android:theme="" android:name="FWMeasureActivity" android:label="">
3131
// <intent-filter>
32-
// <action name="android.intent.action.MAIN"></action>
33-
// <category name="android.intent.category.LAUNCHER"></category>
32+
// <action android:name="android.intent.action.MAIN"></action>
33+
// <category android:name="android.intent.category.LAUNCHER"></category>
3434
// </intent-filter>
3535
// </activity>
36-
// <activity theme="" name="MapActivity" label=""></activity>
37-
// <activity theme="" name="SettingActivity" label=""></activity>
38-
// <activity theme="" name="PlaceSettingActivity" label=""></activity>
36+
// <activity android:theme="" android:name="MapActivity" android:label=""></activity>
37+
// <activity android:theme="" android:name="SettingActivity" android:label=""></activity>
38+
// <activity android:theme="" android:name="PlaceSettingActivity" android:label=""></activity>
3939
// </application>
40-
// <instrumentation name="" targetPackage="" handleProfiling="false" functionalTest="false"></instrumentation>
41-
// <uses-sdk minSdkVersion="0" targetSdkVersion="0" maxSdkVersion="0"></uses-sdk>
40+
// <instrumentation android:name="" android:targetPackage="" android:handleProfiling="false" android:functionalTest="false"></instrumentation>
41+
// <uses-sdk android:minSdkVersion="0" android:targetSdkVersion="0" android:maxSdkVersion="0"></uses-sdk>
4242
// </Manifest>
4343
}
4444

0 commit comments

Comments
 (0)