internal/uidriver/mobile: make Vibrate async

This commit is contained in:
Hajime Hoshi 2022-01-26 22:23:12 +09:00
parent 8f1f9e3a5f
commit eabd82cda0
2 changed files with 20 additions and 13 deletions

View File

@ -105,9 +105,11 @@ static void vibrateOneShot(uintptr_t java_vm, uintptr_t jni_env, uintptr_t ctx,
import "C" import "C"
func (u *UserInterface) Vibrate(duration time.Duration, magnitude float64) { func (u *UserInterface) Vibrate(duration time.Duration, magnitude float64) {
_ = app.RunOnJVM(func(vm, env, ctx uintptr) error { go func() {
// TODO: This might be crash when this is called from init(). How can we detect this? _ = app.RunOnJVM(func(vm, env, ctx uintptr) error {
C.vibrateOneShot(C.uintptr_t(vm), C.uintptr_t(env), C.uintptr_t(ctx), C.int64_t(duration/time.Millisecond), C.double(magnitude)) // TODO: This might be crash when this is called from init(). How can we detect this?
return nil C.vibrateOneShot(C.uintptr_t(vm), C.uintptr_t(env), C.uintptr_t(ctx), C.int64_t(duration/time.Millisecond), C.double(magnitude))
}) return nil
})
}()
} }

View File

@ -20,6 +20,7 @@ package mobile
// #cgo LDFLAGS: -framework CoreHaptics // #cgo LDFLAGS: -framework CoreHaptics
// //
// #import <CoreHaptics/CoreHaptics.h> // #import <CoreHaptics/CoreHaptics.h>
// #include <dispatch/dispatch.h>
// //
// static id initializeHapticEngine(void) { // static id initializeHapticEngine(void) {
// if (@available(iOS 13.0, *)) { // if (@available(iOS 13.0, *)) {
@ -30,11 +31,13 @@ package mobile
// NSError* error = nil; // NSError* error = nil;
// CHHapticEngine* engine = [[CHHapticEngine alloc] initAndReturnError:&error]; // CHHapticEngine* engine = [[CHHapticEngine alloc] initAndReturnError:&error];
// if (error) { // if (error) {
// NSLog(@"CHHapticEngine::initAndReturnError failed: %@", error);
// return nil; // return nil;
// } // }
// //
// [engine startAndReturnError:&error]; // [engine startAndReturnError:&error];
// if (error) { // if (error) {
// NSLog(@"CHHapticEngine::startAndReturnError failed %@", error);
// return nil; // return nil;
// } // }
// return engine; // return engine;
@ -42,7 +45,7 @@ package mobile
// return nil; // return nil;
// } // }
// //
// static void vibrate(double duration, double intensity) { // static void vibrateOnMainThread(double duration, double intensity) {
// if (@available(iOS 13.0, *)) { // if (@available(iOS 13.0, *)) {
// static BOOL initializeHapticEngineCalled = NO; // static BOOL initializeHapticEngineCalled = NO;
// static CHHapticEngine* engine = nil; // static CHHapticEngine* engine = nil;
@ -93,18 +96,20 @@ package mobile
// } // }
// } // }
// } // }
//
// static void vibrate(double duration, double intensity) {
// dispatch_async(dispatch_get_main_queue(), ^{
// vibrateOnMainThread(duration, intensity);
// });
// }
import "C" import "C"
import ( import (
"sync"
"time" "time"
) )
var vibrationM sync.Mutex
func (u *UserInterface) Vibrate(duration time.Duration, magnitude float64) { func (u *UserInterface) Vibrate(duration time.Duration, magnitude float64) {
vibrationM.Lock() go func() {
defer vibrationM.Unlock() C.vibrate(C.double(float64(duration)/float64(time.Second)), C.double(magnitude))
}()
C.vibrate(C.double(float64(duration)/float64(time.Second)), C.double(magnitude))
} }