x/mobile/app: Open function for basic asset management
LGTM=hyangah R=hyangah CC=golang-codereviews https://golang.org/cl/187740044
This commit is contained in:
parent
5875cc8cf7
commit
2d6073265a
@ -12,11 +12,15 @@ 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 <pthread.h>
|
||||
|
||||
#include <jni.h>
|
||||
#include <pthread.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
pthread_cond_t go_started_cond;
|
||||
pthread_mutex_t go_started_mu;
|
||||
@ -32,15 +36,22 @@ JavaVM* current_vm;
|
||||
*/
|
||||
import "C"
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
"runtime"
|
||||
"unsafe"
|
||||
|
||||
"golang.org/x/mobile/geom"
|
||||
)
|
||||
|
||||
var assetManager *C.AAssetManager
|
||||
|
||||
//export onCreate
|
||||
func onCreate(activity *C.ANativeActivity) {
|
||||
assetManager = activity.assetManager
|
||||
|
||||
config := C.AConfiguration_new()
|
||||
C.AConfiguration_fromAssetManager(config, activity.assetManager)
|
||||
density := C.AConfiguration_getDensity(config)
|
||||
@ -150,6 +161,57 @@ var (
|
||||
windowCreated = 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(assetManager, 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 run(cb Callbacks) {
|
||||
// We want to keep the event loop on a consistent OS thread.
|
||||
runtime.LockOSThread()
|
||||
|
34
app/app.go
34
app/app.go
@ -4,7 +4,11 @@
|
||||
|
||||
package app
|
||||
|
||||
import "golang.org/x/mobile/event"
|
||||
import (
|
||||
"io"
|
||||
|
||||
"golang.org/x/mobile/event"
|
||||
)
|
||||
|
||||
// Run starts the app.
|
||||
//
|
||||
@ -26,10 +30,24 @@ type Callbacks struct {
|
||||
Touch func(event.Touch)
|
||||
}
|
||||
|
||||
/*
|
||||
TODO(crawshaw): Implement.
|
||||
var Start func()
|
||||
var Stop func()
|
||||
var Resume func()
|
||||
var Pause func()
|
||||
*/
|
||||
// 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
|
||||
}
|
||||
|
25
app/desktop.go
Normal file
25
app/desktop.go
Normal file
@ -0,0 +1,25 @@
|
||||
// Copyright 2014 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 !android
|
||||
|
||||
package app
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
// Logic shared by desktop debugging implementations.
|
||||
|
||||
func openAsset(name string) (ReadSeekCloser, error) {
|
||||
if !filepath.IsAbs(name) {
|
||||
name = filepath.Join("assets", name)
|
||||
}
|
||||
f, err := os.Open(name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return f, nil
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user