mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-11-10 04:57:26 +01:00
internal/uidriver/mobile: Implement Vibrate for Android
This change also adds AndroidManifest.xml for the example. This is used for `gomobile install` command. Updates #1452
This commit is contained in:
parent
d9c362bca9
commit
b4f87f9651
19
examples/vibrate/AndroidManifest.xml
Normal file
19
examples/vibrate/AndroidManifest.xml
Normal file
@ -0,0 +1,19 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<manifest
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
package="org.ebiten.example.vibrate"
|
||||
android:versionCode="1"
|
||||
android:versionName="1.0">
|
||||
<uses-permission android:name="android.permission.VIBRATE"/>
|
||||
<application android:label="Vibrate" android:debuggable="true">
|
||||
<activity android:name="org.golang.app.GoNativeActivity"
|
||||
android:label="vibrate"
|
||||
android:configChanges="orientation|keyboardHidden">
|
||||
<meta-data android:name="android.app.lib_name" android:value="vibrate" />
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN" />
|
||||
<category android:name="android.intent.category.LAUNCHER" />
|
||||
</intent-filter>
|
||||
</activity>
|
||||
</application>
|
||||
</manifest>
|
@ -16,8 +16,68 @@ package mobile
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"golang.org/x/mobile/app"
|
||||
)
|
||||
|
||||
func (u *UserInterface) Vibrate(duration time.Duration) {
|
||||
// TODO: Implement this (#1452)
|
||||
/*
|
||||
|
||||
#include <jni.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
|
||||
// Basically same as:
|
||||
//
|
||||
// Vibrator v = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE);
|
||||
// v.vibrate(millisecond)
|
||||
//
|
||||
// TODO: As of API Level 26, the new API should be Used instead:
|
||||
//
|
||||
// Vibrator v = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE);
|
||||
// v.vibrate(VibrationEffect.createOneShot(millisecond, VibrationEffect.DEFAULT_AMPLITUDE))
|
||||
//
|
||||
// Note that this requires a manifest setting:
|
||||
//
|
||||
// <uses-permission android:name="android.permission.VIBRATE"/>
|
||||
//
|
||||
static void vibrateOneShot(uintptr_t java_vm, uintptr_t jni_env, uintptr_t ctx, int64_t milliseconds) {
|
||||
JavaVM* vm = (JavaVM*)java_vm;
|
||||
JNIEnv* env = (JNIEnv*)jni_env;
|
||||
jobject context = (jobject)ctx;
|
||||
|
||||
const jclass android_content_Context = (*env)->FindClass(env, "android/content/Context");
|
||||
const jclass android_os_Vibrator = (*env)->FindClass(env, "android/os/Vibrator");
|
||||
|
||||
const jobject android_context_Context_VIBRATOR_SERVICE =
|
||||
(*env)->GetStaticObjectField(
|
||||
env, android_content_Context,
|
||||
(*env)->GetStaticFieldID(env, android_content_Context, "VIBRATOR_SERVICE", "Ljava/lang/String;"));
|
||||
|
||||
const jobject vibrator =
|
||||
(*env)->CallObjectMethod(
|
||||
env, context,
|
||||
(*env)->GetMethodID(env, android_content_Context, "getSystemService", "(Ljava/lang/String;)Ljava/lang/Object;"),
|
||||
android_context_Context_VIBRATOR_SERVICE);
|
||||
|
||||
(*env)->CallVoidMethod(
|
||||
env, vibrator,
|
||||
(*env)->GetMethodID(env, android_os_Vibrator, "vibrate", "(J)V"),
|
||||
milliseconds);
|
||||
|
||||
(*env)->DeleteLocalRef(env, android_content_Context);
|
||||
(*env)->DeleteLocalRef(env, android_os_Vibrator);
|
||||
|
||||
(*env)->DeleteLocalRef(env, android_context_Context_VIBRATOR_SERVICE);
|
||||
(*env)->DeleteLocalRef(env, vibrator);
|
||||
}
|
||||
|
||||
*/
|
||||
import "C"
|
||||
|
||||
func (u *UserInterface) Vibrate(duration time.Duration) {
|
||||
_ = app.RunOnJVM(func(vm, env, ctx uintptr) error {
|
||||
// TODO: This might be crash when this is called from init(). How can we detect this?
|
||||
C.vibrateOneShot(C.uintptr_t(vm), C.uintptr_t(env), C.uintptr_t(ctx), C.int64_t(duration/time.Millisecond))
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user