asset: move app.Open to its own package
This does not break the dependency on the app package's AndroidContext for loading assets on android. A potential answer for gobind-based apps: add a SetAndroidContext method to app.Context. But I'll explore that separately after the long weekend. Change-Id: I812f899740e288c379eee7900f42d9d53926d4ce Reviewed-on: https://go-review.googlesource.com/11675 Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
This commit is contained in:
parent
743b9bfd9a
commit
8bb4ca139b
@ -4,7 +4,6 @@
|
||||
|
||||
// +build android
|
||||
|
||||
#include <android/asset_manager_jni.h>
|
||||
#include <android/log.h>
|
||||
#include <dlfcn.h>
|
||||
#include <errno.h>
|
||||
|
@ -25,7 +25,6 @@ package app
|
||||
#cgo LDFLAGS: -llog -landroid
|
||||
|
||||
#include <android/log.h>
|
||||
#include <android/asset_manager.h>
|
||||
#include <android/configuration.h>
|
||||
#include <android/native_activity.h>
|
||||
#include <time.h>
|
||||
@ -46,16 +45,9 @@ JavaVM* current_vm;
|
||||
jobject current_ctx;
|
||||
|
||||
jclass app_find_class(JNIEnv* env, const char* name);
|
||||
|
||||
// asset_manager is the asset manager of the app.
|
||||
// For all-Go app, this is initialized in onCreate.
|
||||
// For go library app, this is set from the context passed to Go.run.
|
||||
AAssetManager* asset_manager;
|
||||
*/
|
||||
import "C"
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
"runtime"
|
||||
@ -92,8 +84,6 @@ func callMain(mainPC uintptr) {
|
||||
|
||||
//export onCreate
|
||||
func onCreate(activity *C.ANativeActivity) {
|
||||
C.asset_manager = activity.assetManager
|
||||
|
||||
config := C.AConfiguration_new()
|
||||
C.AConfiguration_fromAssetManager(config, activity.assetManager)
|
||||
density := C.AConfiguration_getDensity(config)
|
||||
@ -219,57 +209,6 @@ var (
|
||||
windowRedrawNeeded = make(chan *C.ANativeWindow)
|
||||
)
|
||||
|
||||
func openAsset(name string) (ReadSeekCloser, error) {
|
||||
cname := C.CString(name)
|
||||
defer C.free(unsafe.Pointer(cname))
|
||||
a := &asset{
|
||||
ptr: C.AAssetManager_open(C.asset_manager, cname, C.AASSET_MODE_UNKNOWN),
|
||||
name: name,
|
||||
}
|
||||
if a.ptr == nil {
|
||||
return nil, a.errorf("open", "bad asset")
|
||||
}
|
||||
return a, nil
|
||||
}
|
||||
|
||||
type asset struct {
|
||||
ptr *C.AAsset
|
||||
name string
|
||||
}
|
||||
|
||||
func (a *asset) errorf(op string, format string, v ...interface{}) error {
|
||||
return &os.PathError{
|
||||
Op: op,
|
||||
Path: a.name,
|
||||
Err: fmt.Errorf(format, v...),
|
||||
}
|
||||
}
|
||||
|
||||
func (a *asset) Read(p []byte) (n int, err error) {
|
||||
n = int(C.AAsset_read(a.ptr, unsafe.Pointer(&p[0]), C.size_t(len(p))))
|
||||
if n == 0 && len(p) > 0 {
|
||||
return 0, io.EOF
|
||||
}
|
||||
if n < 0 {
|
||||
return 0, a.errorf("read", "negative bytes: %d", n)
|
||||
}
|
||||
return n, nil
|
||||
}
|
||||
|
||||
func (a *asset) Seek(offset int64, whence int) (int64, error) {
|
||||
// TODO(crawshaw): use AAsset_seek64 if it is available.
|
||||
off := C.AAsset_seek(a.ptr, C.off_t(offset), C.int(whence))
|
||||
if off == -1 {
|
||||
return 0, a.errorf("seek", "bad result for offset=%d, whence=%d", offset, whence)
|
||||
}
|
||||
return int64(off), nil
|
||||
}
|
||||
|
||||
func (a *asset) Close() error {
|
||||
C.AAsset_close(a.ptr)
|
||||
return nil
|
||||
}
|
||||
|
||||
func main(f func(App)) {
|
||||
// Preserve this OS thread for the GL context created in windowDraw.
|
||||
runtime.LockOSThread()
|
||||
|
28
app/app.go
28
app/app.go
@ -6,11 +6,7 @@
|
||||
|
||||
package app
|
||||
|
||||
import (
|
||||
"io"
|
||||
|
||||
"golang.org/x/mobile/event"
|
||||
)
|
||||
import "golang.org/x/mobile/event"
|
||||
|
||||
// Main is called by the main.main function to run the mobile application.
|
||||
//
|
||||
@ -230,25 +226,3 @@ type Callbacks struct {
|
||||
// Config is called by the app when configuration has changed.
|
||||
Config func(new, old event.Config)
|
||||
}
|
||||
|
||||
// Open opens a named asset.
|
||||
//
|
||||
// On Android, assets are accessed via android.content.res.AssetManager.
|
||||
// These files are stored in the assets/ directory of the app. Any raw asset
|
||||
// can be accessed by its direct relative name. For example assets/img.png
|
||||
// can be opened with Open("img.png").
|
||||
//
|
||||
// On iOS an asset is a resource stored in the application bundle.
|
||||
// Resources can be loaded using the same relative paths.
|
||||
//
|
||||
// For consistency when debugging on a desktop, assets are read from a
|
||||
// directoy named assets under the current working directory.
|
||||
func Open(name string) (ReadSeekCloser, error) {
|
||||
return openAsset(name)
|
||||
}
|
||||
|
||||
// ReadSeekCloser is an io.ReadSeeker and io.Closer.
|
||||
type ReadSeekCloser interface {
|
||||
io.ReadSeeker
|
||||
io.Closer
|
||||
}
|
||||
|
28
asset/asset.go
Normal file
28
asset/asset.go
Normal file
@ -0,0 +1,28 @@
|
||||
// Package asset provides access to application-bundled assets.
|
||||
//
|
||||
// On Android, assets are accessed via android.content.res.AssetManager.
|
||||
// These files are stored in the assets/ directory of the app. Any raw asset
|
||||
// can be accessed by its direct relative name. For example assets/img.png
|
||||
// can be opened with Open("img.png").
|
||||
//
|
||||
// On iOS an asset is a resource stored in the application bundle.
|
||||
// Resources can be loaded using the same relative paths.
|
||||
//
|
||||
// For consistency when debugging on a desktop, assets are read from a
|
||||
// directoy named assets under the current working directory.
|
||||
package asset
|
||||
|
||||
import "io"
|
||||
|
||||
// Open opens a named asset.
|
||||
//
|
||||
// Errors are of type *os.PathError.
|
||||
func Open(name string) (File, error) {
|
||||
return openAsset(name)
|
||||
}
|
||||
|
||||
// File is an open asset.
|
||||
type File interface {
|
||||
io.ReadSeeker
|
||||
io.Closer
|
||||
}
|
120
asset/asset_android.go
Normal file
120
asset/asset_android.go
Normal file
@ -0,0 +1,120 @@
|
||||
package asset
|
||||
|
||||
/*
|
||||
#cgo LDFLAGS: -llog -landroid
|
||||
#include <android/log.h>
|
||||
#include <android/asset_manager.h>
|
||||
#include <android/asset_manager_jni.h>
|
||||
#include <jni.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#define LOG_FATAL(...) __android_log_print(ANDROID_LOG_FATAL, "Go/asset", __VA_ARGS__)
|
||||
|
||||
// asset_manager is the asset manager of the app.
|
||||
AAssetManager* asset_manager;
|
||||
|
||||
void asset_manager_init(void* java_vm, void* ctx) {
|
||||
JavaVM* vm = (JavaVM*)(java_vm);
|
||||
JNIEnv* env;
|
||||
int err;
|
||||
int attached = 0;
|
||||
|
||||
err = (*vm)->GetEnv(vm, (void**)&env, JNI_VERSION_1_6);
|
||||
if (err != JNI_OK) {
|
||||
if (err == JNI_EDETACHED) {
|
||||
if ((*vm)->AttachCurrentThread(vm, &env, 0) != 0) {
|
||||
LOG_FATAL("cannot attach JVM");
|
||||
}
|
||||
attached = 1;
|
||||
} else {
|
||||
LOG_FATAL("GetEnv unexpected error: %d", err);
|
||||
}
|
||||
}
|
||||
|
||||
// Equivalent to:
|
||||
// assetManager = ctx.getResources().getAssets();
|
||||
jclass ctx_clazz = (*env)->FindClass(env, "android/content/Context");
|
||||
jmethodID getres_id = (*env)->GetMethodID(env, ctx_clazz, "getResources", "()Landroid/content/res/Resources;");
|
||||
jobject res = (*env)->CallObjectMethod(env, ctx, getres_id);
|
||||
jclass res_clazz = (*env)->FindClass(env, "android/content/res/Resources");
|
||||
jmethodID getam_id = (*env)->GetMethodID(env, res_clazz, "getAssets", "()Landroid/content/res/AssetManager;");
|
||||
jobject am = (*env)->CallObjectMethod(env, res, getam_id);
|
||||
|
||||
// Pin the AssetManager and load an AAssetManager from it.
|
||||
am = (*env)->NewGlobalRef(env, am);
|
||||
asset_manager = AAssetManager_fromJava(env, am);
|
||||
|
||||
if (attached) {
|
||||
(*vm)->DetachCurrentThread(vm);
|
||||
}
|
||||
}
|
||||
*/
|
||||
import "C"
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"sync"
|
||||
"unsafe"
|
||||
|
||||
"golang.org/x/mobile/app"
|
||||
)
|
||||
|
||||
var assetOnce sync.Once
|
||||
|
||||
func assetInit() {
|
||||
ctx := app.Context{}
|
||||
C.asset_manager_init(ctx.JavaVM(), ctx.AndroidContext())
|
||||
}
|
||||
|
||||
func openAsset(name string) (File, error) {
|
||||
assetOnce.Do(assetInit)
|
||||
cname := C.CString(name)
|
||||
defer C.free(unsafe.Pointer(cname))
|
||||
a := &asset{
|
||||
ptr: C.AAssetManager_open(C.asset_manager, cname, C.AASSET_MODE_UNKNOWN),
|
||||
name: name,
|
||||
}
|
||||
if a.ptr == nil {
|
||||
return nil, a.errorf("open", "bad asset")
|
||||
}
|
||||
return a, nil
|
||||
}
|
||||
|
||||
type asset struct {
|
||||
ptr *C.AAsset
|
||||
name string
|
||||
}
|
||||
|
||||
func (a *asset) errorf(op string, format string, v ...interface{}) error {
|
||||
return &os.PathError{
|
||||
Op: op,
|
||||
Path: a.name,
|
||||
Err: fmt.Errorf(format, v...),
|
||||
}
|
||||
}
|
||||
|
||||
func (a *asset) Read(p []byte) (n int, err error) {
|
||||
n = int(C.AAsset_read(a.ptr, unsafe.Pointer(&p[0]), C.size_t(len(p))))
|
||||
if n == 0 && len(p) > 0 {
|
||||
return 0, io.EOF
|
||||
}
|
||||
if n < 0 {
|
||||
return 0, a.errorf("read", "negative bytes: %d", n)
|
||||
}
|
||||
return n, nil
|
||||
}
|
||||
|
||||
func (a *asset) Seek(offset int64, whence int) (int64, error) {
|
||||
// TODO(crawshaw): use AAsset_seek64 if it is available.
|
||||
off := C.AAsset_seek(a.ptr, C.off_t(offset), C.int(whence))
|
||||
if off == -1 {
|
||||
return 0, a.errorf("seek", "bad result for offset=%d, whence=%d", offset, whence)
|
||||
}
|
||||
return int64(off), nil
|
||||
}
|
||||
|
||||
func (a *asset) Close() error {
|
||||
C.AAsset_close(a.ptr)
|
||||
return nil
|
||||
}
|
25
asset/asset_darwin_armx.go
Normal file
25
asset/asset_darwin_armx.go
Normal file
@ -0,0 +1,25 @@
|
||||
// Copyright 2015 The Go Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
// +build darwin
|
||||
// +build arm arm64
|
||||
|
||||
package app
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
func openAsset(name string) (File, error) {
|
||||
if !filepath.IsAbs(name) {
|
||||
name = filepath.Join("assets", name)
|
||||
}
|
||||
f, err := os.Open(name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return f, nil
|
||||
|
||||
}
|
@ -2,18 +2,16 @@
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
// +build linux,!android darwin
|
||||
// +build linux,!android darwin,!arm,!arm64
|
||||
|
||||
package app
|
||||
package asset
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
// Logic shared by desktop debugging implementations.
|
||||
|
||||
func openAsset(name string) (ReadSeekCloser, error) {
|
||||
func openAsset(name string) (File, error) {
|
||||
if !filepath.IsAbs(name) {
|
||||
name = filepath.Join("assets", name)
|
||||
}
|
@ -39,6 +39,7 @@ import (
|
||||
|
||||
"golang.org/x/mobile/app"
|
||||
"golang.org/x/mobile/app/debug"
|
||||
"golang.org/x/mobile/asset"
|
||||
"golang.org/x/mobile/event"
|
||||
"golang.org/x/mobile/exp/audio"
|
||||
"golang.org/x/mobile/exp/sprite"
|
||||
@ -71,7 +72,7 @@ func main() {
|
||||
}
|
||||
|
||||
func start() {
|
||||
rc, err := app.Open("boing.wav")
|
||||
rc, err := asset.Open("boing.wav")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
@ -152,7 +153,7 @@ func boing() {
|
||||
}
|
||||
|
||||
func loadGopher() sprite.SubTex {
|
||||
a, err := app.Open("gopher.jpeg")
|
||||
a, err := asset.Open("gopher.jpeg")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
@ -36,6 +36,7 @@ import (
|
||||
|
||||
"golang.org/x/mobile/app"
|
||||
"golang.org/x/mobile/app/debug"
|
||||
"golang.org/x/mobile/asset"
|
||||
"golang.org/x/mobile/event"
|
||||
"golang.org/x/mobile/exp/sprite"
|
||||
"golang.org/x/mobile/exp/sprite/clock"
|
||||
@ -131,7 +132,7 @@ const (
|
||||
)
|
||||
|
||||
func loadTextures() []sprite.SubTex {
|
||||
a, err := app.Open("waza-gophers.jpeg")
|
||||
a, err := asset.Open("waza-gophers.jpeg")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user