1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

os: clean up usage of ANativeActivity, allow access to fields (#14948)

This commit is contained in:
Larpon 2022-07-05 15:30:10 +02:00 committed by GitHub
parent 56d62a6e6f
commit 0dd5050b29
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 25 deletions

View File

@ -1,36 +1,19 @@
module gg module gg
import os
import sokol.sapp import sokol.sapp
#include <android/configuration.h> #include <android/configuration.h>
#include <android/native_activity.h>
fn C.AConfiguration_new() voidptr fn C.AConfiguration_new() voidptr
fn C.AConfiguration_fromAssetManager(voidptr, voidptr) fn C.AConfiguration_fromAssetManager(voidptr, voidptr)
fn C.AConfiguration_getDensity(voidptr) u32 fn C.AConfiguration_getDensity(voidptr) u32
fn C.AConfiguration_delete(voidptr) fn C.AConfiguration_delete(voidptr)
struct C.AAssetManager {}
// See https://developer.android.com/ndk/reference/struct/a-native-activity for more info.
struct C.ANativeActivity {
pub:
assetManager voidptr // Pointer to the Asset Manager instance for the application. (AAssetManager *)
callbacks voidptr // Pointer to the callback function table of the native application. (struct ANativeActivityCallbacks *)
clazz voidptr // The NativeActivity object handle.
env voidptr // JNI context for the main thread of the app.
externalDataPath &char // Path to this application's external (removable/mountable) data directory.
instance voidptr // This is the native instance of the application.
internalDataPath &char // Path to this application's internal data directory.
obbPath &char // Available starting with Honeycomb: path to the directory containing the application's OBB files (if any).
sdkVersion int // The platform's SDK version code.
vm voidptr // The global handle on the process's Java VM
}
// android_dpi_scale returns the scale factor of the device. // android_dpi_scale returns the scale factor of the device.
pub fn android_dpi_scale() f32 { pub fn android_dpi_scale() f32 {
config := C.AConfiguration_new() config := C.AConfiguration_new()
activity := &C.ANativeActivity(sapp.android_get_native_activity()) activity := &os.NativeActivity(sapp.android_get_native_activity())
C.AConfiguration_fromAssetManager(config, activity.assetManager) C.AConfiguration_fromAssetManager(config, activity.assetManager)
density := C.AConfiguration_getDensity(config) density := C.AConfiguration_getDensity(config)
C.AConfiguration_delete(config) C.AConfiguration_delete(config)

View File

@ -1,7 +1,8 @@
module os module os
#include <asset_manager.h> #include <android/asset_manager.h>
#include <asset_manager_jni.h> #include <android/asset_manager_jni.h>
#include <android/native_activity.h>
pub enum AssetMode { pub enum AssetMode {
buffer = C.AASSET_MODE_BUFFER // Caller plans to ask for a read-only buffer with all data. buffer = C.AASSET_MODE_BUFFER // Caller plans to ask for a read-only buffer with all data.
@ -10,7 +11,9 @@ pub enum AssetMode {
unknown = C.AASSET_MODE_UNKNOWN // No specific information about how data will be accessed. unknown = C.AASSET_MODE_UNKNOWN // No specific information about how data will be accessed.
} }
// See https://developer.android.com/ndk/reference/struct/a-native-activity for more info.
struct C.ANativeActivity { struct C.ANativeActivity {
pub:
assetManager &AssetManager // Pointer to the Asset Manager instance for the application. assetManager &AssetManager // Pointer to the Asset Manager instance for the application.
clazz voidptr // (jobject) The NativeActivity object handle. clazz voidptr // (jobject) The NativeActivity object handle.
env voidptr // (JNIEnv *) JNI context for the main thread of the app. env voidptr // (JNIEnv *) JNI context for the main thread of the app.
@ -63,8 +66,8 @@ pub fn (a &Asset) get_length() int {
fn C.AAsset_getLength64(&C.AAsset) i64 fn C.AAsset_getLength64(&C.AAsset) i64
// get_length_64 returns the total size of the asset data. // get_length_64 returns the total size of the asset data using
// get_length_64 returns the size using a 64-bit number insted of 32-bit as `get_length`. // a 64-bit number insted of 32-bit as `get_length`.
pub fn (a &Asset) get_length_64() i64 { pub fn (a &Asset) get_length_64() i64 {
return C.AAsset_getLength64(a) return C.AAsset_getLength64(a)
} }
@ -85,14 +88,14 @@ pub fn (a &Asset) close() {
} }
// read_apk_asset returns all the data located at `path`. // read_apk_asset returns all the data located at `path`.
// `path` is expected to be relative to the `assets` // `path` is expected to be relative to the APK/AAB `assets` directory.
pub fn read_apk_asset(path string) ![]u8 { pub fn read_apk_asset(path string) ![]u8 {
$if apk { $if apk {
act := &NativeActivity(C.sapp_android_get_native_activity()) act := &NativeActivity(C.sapp_android_get_native_activity())
if isnil(act) { if isnil(act) {
return error('could not get reference to Android activity') return error('could not get reference to Android activity')
} }
asset_manager := &AssetManager(act.assetManager) asset_manager := act.assetManager
asset := asset_manager.open(path, .streaming)! asset := asset_manager.open(path, .streaming)!
len := asset.get_length() len := asset.get_length()
buf := []u8{len: len} buf := []u8{len: len}