github.com/c-darwin/mobile@v0.0.0-20160313183840-ff625c46f7c9/internal/mobileinit/ctx_android.go (about) 1 // Copyright 2015 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package mobileinit 6 7 /* 8 #include <jni.h> 9 #include <stdlib.h> 10 11 // current_vm is stored to initialize other cgo packages. 12 // 13 // As all the Go packages in a program form a single shared library, 14 // there can only be one JNI_OnLoad function for initialization. In 15 // OpenJDK there is JNI_GetCreatedJavaVMs, but this is not available 16 // on android. 17 JavaVM* current_vm; 18 19 // current_ctx is Android's android.context.Context. May be NULL. 20 jobject current_ctx; 21 22 // Set current_vm and current_ctx. The ctx passed in must be a global 23 // reference instance. 24 void set_vm_ctx(JavaVM* vm, jobject ctx) { 25 current_vm = vm; 26 current_ctx = ctx; 27 // TODO: check leak 28 } 29 */ 30 import "C" 31 32 import "unsafe" 33 34 // SetCurrentContext populates the global Context object with the specified 35 // current JavaVM instance (vm) and android.context.Context object (ctx). 36 // The android.context.Context object must be a global reference. 37 func SetCurrentContext(vm, ctx unsafe.Pointer) { 38 C.set_vm_ctx((*C.JavaVM)(vm), (C.jobject)(ctx)) 39 } 40 41 // TODO(hyangah): should the app package have Context? It may be useful for 42 // external packages that need to access android context and vm. 43 44 // Context holds global OS-specific context. 45 // 46 // Its extra methods are deliberately difficult to access because they must be 47 // used with care. Their use implies the use of cgo, which probably requires 48 // you understand the initialization process in the app package. Also care must 49 // be taken to write both Android, iOS, and desktop-testing versions to 50 // maintain portability. 51 type Context struct{} 52 53 // AndroidContext returns a jobject for the app android.context.Context. 54 func (Context) AndroidContext() unsafe.Pointer { 55 return unsafe.Pointer(C.current_ctx) 56 } 57 58 // JavaVM returns a JNI *JavaVM. 59 func (Context) JavaVM() unsafe.Pointer { 60 return unsafe.Pointer(C.current_vm) 61 }