Skip to content

Commit 5e06cdb

Browse files
author
José Pereda
authored
Use JavaFX 20-ea+4 and support long press on Android (#1179)
* Bump JavaFX version to 20-ea+4 * Add support for long press on Android * move handler to inner class * cleanup
1 parent 975ef76 commit 5e06cdb

File tree

6 files changed

+58
-36
lines changed

6 files changed

+58
-36
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,5 @@ bin
3131
.project
3232
.classpath
3333
.vscode
34-
.settings
34+
.settings
35+
local.properties

src/main/java/com/gluonhq/substrate/Constants.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public enum Profile {
108108

109109
public static final String DEFAULT_JAVA_STATIC_SDK_VERSION = "18-ea+prep18-8";
110110
public static final String DEFAULT_JAVA_STATIC_SDK_VERSION11 = "11-ea+10";
111-
public static final String DEFAULT_JAVAFX_STATIC_SDK_VERSION = "20-ea+3";
111+
public static final String DEFAULT_JAVAFX_STATIC_SDK_VERSION = "20-ea+4";
112112
public static final String DEFAULT_JAVAFX_JS_SDK_VERSION = "18-internal+0-2021-09-02-165800";
113113
public static final String DEFAULT_SYSROOT_VERSION = "20210424";
114114
public static final String DEFAULT_CLIBS_VERSION = "27";

src/main/resources/native/android/android_project/app/src/main/java/com/gluonhq/helloandroid/MainActivity.java

+43-19
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,6 @@
3030
import android.app.Activity;
3131
import android.content.Context;
3232
import android.content.Intent;
33-
import android.content.pm.ActivityInfo;
34-
import android.content.pm.PackageManager;
35-
import android.content.pm.PackageManager.NameNotFoundException;
3633
import android.graphics.PixelFormat;
3734
import android.graphics.Rect;
3835
import android.os.Bundle;
@@ -52,13 +49,12 @@
5249
import android.view.Surface;
5350
import android.view.SurfaceHolder;
5451
import android.view.SurfaceView;
52+
import android.view.ViewConfiguration;
5553
import android.view.ViewGroup;
5654
import android.view.Window;
5755
import android.view.WindowManager;
58-
import android.view.WindowManager.LayoutParams;
5956
import android.view.inputmethod.BaseInputConnection;
6057
import android.view.inputmethod.EditorInfo;
61-
import android.view.inputmethod.InputConnection;
6258
import android.view.inputmethod.InputMethodManager;
6359
import android.widget.FrameLayout;
6460

@@ -85,7 +81,6 @@ protected void onCreate(Bundle savedInstanceState) {
8581
super.onCreate(savedInstanceState);
8682
Log.v(TAG, "onCreate start, using Android Logging v1");
8783
System.err.println("onCreate called, writing this to System.err");
88-
super.onCreate(savedInstanceState);
8984

9085
getWindow().requestFeature(Window.FEATURE_NO_TITLE);
9186
getWindow().setSoftInputMode(
@@ -223,10 +218,10 @@ public void run() {
223218
private native void nativeSetSurface(Surface surface);
224219
private native void nativeSurfaceRedrawNeeded();
225220
private native void nativeGotTouchEvent(int pcount, int[] actions, int[] ids, int[] touchXs, int[] touchYs);
226-
private native void nativeGotKeyEvent(int action, int keycode);
227-
private native void nativedispatchKeyEvent(int type, int key, char[] chars, int charCount, int modifiers);
221+
private native void nativeDispatchKeyEvent(int type, int key, char[] chars, int charCount, int modifiers);
228222
private native void nativeDispatchLifecycleEvent(String event);
229223
private native void nativeDispatchActivityResult(int requestCode, int resultCode, Intent intent);
224+
private native void nativeNotifyMenu(int x, int y, int xAbs, int yAbs, boolean isKeyboardTrigger);
230225

231226
class InternalSurfaceView extends SurfaceView {
232227
private static final int ACTION_POINTER_STILL = -1;
@@ -251,7 +246,6 @@ public boolean dispatchTouchEvent(MotionEvent event) {
251246
final int[] ids = new int[pcount];
252247
final int[] touchXs = new int[pcount];
253248
final int[] touchYs = new int[pcount];
254-
Log.v(TAG, "Activity, get touch event, pcount = "+pcount);
255249
if (pcount > 1) {
256250
//multitouch
257251
if (actionCode == MotionEvent.ACTION_POINTER_DOWN
@@ -278,6 +272,16 @@ public boolean dispatchTouchEvent(MotionEvent event) {
278272
ids[0] = event.getPointerId(0);
279273
touchXs[0] = (int) (event.getX()/density);
280274
touchYs[0] = (int) (event.getY()/density);
275+
276+
if (action == MotionEvent.ACTION_DOWN) {
277+
longPress.setX(touchXs[0]);
278+
longPress.setY(touchYs[0]);
279+
handler.postDelayed(longPress, ViewConfiguration.getLongPressTimeout());
280+
}
281+
282+
if (action == MotionEvent.ACTION_UP) {
283+
handler.removeCallbacks(longPress);
284+
}
281285
}
282286
if (!isFocused()) {
283287
Log.v(TAG, "View wasn't focused, requesting focus");
@@ -375,9 +379,31 @@ private void resetText(int length) {
375379
public boolean dispatchKeyEvent(final KeyEvent event) {
376380
Log.v(TAG, "Activity, process get key event, action = "+event);
377381
processAndroidKeyEvent (event);
378-
// nativeGotKeyEvent(event.getAction(), event.getKeyCode());
379382
return true;
380383
}
384+
385+
private final Handler handler = new Handler();
386+
private final LongPress longPress = new LongPress();
387+
388+
private class LongPress implements Runnable {
389+
390+
int x, y;
391+
392+
void setX(int x) {
393+
this.x = x;
394+
}
395+
396+
void setY(int y) {
397+
this.y = y;
398+
}
399+
400+
@Override
401+
public void run() {
402+
Log.d(TAG, "Long press!");
403+
nativeNotifyMenu(x, y, x, y, false);
404+
}
405+
}
406+
381407
}
382408

383409
@Override
@@ -438,18 +464,16 @@ private void notifyLifecycleEvent(String event) {
438464
private int deadKey = 0;
439465

440466
void processAndroidKeyEvent (KeyEvent event) {
441-
System.out.println("KeyEvent: " + event+" with action = "+event.getAction());
442467
int jfxModifiers = mapAndroidModifierToJfx(event.getMetaState());
443468
switch (event.getAction()) {
444469
case KeyEvent.ACTION_DOWN:
445470
KeyCode jfxKeyCode = mapAndroidKeyCodeToJfx(event.getKeyCode());
446-
System.out.println ("[JVDBG] eventkeycode = "+event.getKeyCode()+" and jfxkc = "+jfxKeyCode+" with code "+ jfxKeyCode.impl_getCode());
447-
nativedispatchKeyEvent(PRESS, jfxKeyCode.impl_getCode(), jfxKeyCode.impl_getChar().toCharArray(), jfxKeyCode.impl_getChar().toCharArray().length, jfxModifiers);
471+
nativeDispatchKeyEvent(PRESS, jfxKeyCode.impl_getCode(), jfxKeyCode.impl_getChar().toCharArray(), jfxKeyCode.impl_getChar().toCharArray().length, jfxModifiers);
448472
break;
449473

450474
case KeyEvent.ACTION_UP:
451475
jfxKeyCode = mapAndroidKeyCodeToJfx(event.getKeyCode());
452-
nativedispatchKeyEvent(RELEASE, jfxKeyCode.impl_getCode(), jfxKeyCode.impl_getChar().toCharArray(), jfxKeyCode.impl_getChar().toCharArray().length, jfxModifiers);
476+
nativeDispatchKeyEvent(RELEASE, jfxKeyCode.impl_getCode(), jfxKeyCode.impl_getChar().toCharArray(), jfxKeyCode.impl_getChar().toCharArray().length, jfxModifiers);
453477
int unicodeChar = event.getUnicodeChar();
454478
if ((unicodeChar & KeyCharacterMap.COMBINING_ACCENT) != 0) {
455479
deadKey = unicodeChar & KeyCharacterMap.COMBINING_ACCENT_MASK;
@@ -462,20 +486,20 @@ void processAndroidKeyEvent (KeyEvent event) {
462486
}
463487

464488
if (unicodeChar != 0) {
465-
nativedispatchKeyEvent(TYPED, KeyCode.UNDEFINED.impl_getCode(), Character.toChars(unicodeChar), 1, jfxModifiers);
489+
nativeDispatchKeyEvent(TYPED, KeyCode.UNDEFINED.impl_getCode(), Character.toChars(unicodeChar), 1, jfxModifiers);
466490
}
467491

468492
break;
469493

470494
case KeyEvent.ACTION_MULTIPLE:
471495
if (event.getKeyCode() == KeyEvent.KEYCODE_UNKNOWN) {
472-
nativedispatchKeyEvent(TYPED, KeyCode.UNDEFINED.impl_getCode(), event.getCharacters().toCharArray(), event.getCharacters().toCharArray().length, jfxModifiers);
496+
nativeDispatchKeyEvent(TYPED, KeyCode.UNDEFINED.impl_getCode(), event.getCharacters().toCharArray(), event.getCharacters().toCharArray().length, jfxModifiers);
473497
} else {
474498
jfxKeyCode = mapAndroidKeyCodeToJfx(event.getKeyCode());
475499
for (int i = 0; i < event.getRepeatCount(); i++) {
476-
nativedispatchKeyEvent(PRESS, jfxKeyCode.impl_getCode(), null, 0, jfxModifiers);
477-
nativedispatchKeyEvent(RELEASE, jfxKeyCode.impl_getCode(), null, 0, jfxModifiers);
478-
nativedispatchKeyEvent(TYPED, jfxKeyCode.impl_getCode(), null, 0, jfxModifiers);
500+
nativeDispatchKeyEvent(PRESS, jfxKeyCode.impl_getCode(), null, 0, jfxModifiers);
501+
nativeDispatchKeyEvent(RELEASE, jfxKeyCode.impl_getCode(), null, 0, jfxModifiers);
502+
nativeDispatchKeyEvent(TYPED, jfxKeyCode.impl_getCode(), null, 0, jfxModifiers);
479503
}
480504
}
481505

src/main/resources/native/android/c/grandroid.h

+2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "grandroid_ext.h"
2929

3030
#include <android/native_window_jni.h>
31+
#include <stdbool.h>
3132

3233
#undef com_sun_glass_events_TouchEvent_TOUCH_PRESSED
3334
#define com_sun_glass_events_TouchEvent_TOUCH_PRESSED 811L
@@ -49,6 +50,7 @@ void __attribute__((weak)) androidJfx_setNativeWindow(ANativeWindow *nativeWindo
4950
void __attribute__((weak)) androidJfx_setDensity(float nativeDensity) {}
5051
void __attribute__((weak)) androidJfx_gotTouchEvent(int count, int *actions, int *ids, int *xs, int *ys, int primary) {}
5152
void __attribute__((weak)) androidJfx_gotKeyEvent(int action, int key, jchar *chars, int count, int mods) {}
53+
void __attribute__((weak)) androidJfx_gotMenuEvent(int x, int y, int xAbs, int yAbs, bool isKeyboardTrigger) {}
5254
int __attribute__((weak)) to_jfx_touch_action(int state) { return 0; }
5355

5456
void __attribute__((weak)) androidJfx_startURL(const char *url) {}

src/main/resources/native/android/c/launcher.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@ const char *origArgs[] = {
6565
"-Dcom.sun.javafx.gestures.zoom=true",
6666
"-Dcom.sun.javafx.gestures.rotate=true",
6767
"-Dcom.sun.javafx.gestures.scroll=true",
68-
"-Djavafx.verbose=true",
68+
"-Djavafx.verbose=false",
6969
"-Dmonocle.input.touchRadius=1",
70-
"-Dmonocle.input.traceEvents.verbose=true",
70+
"-Dmonocle.input.traceEvents.verbose=false",
7171
"-Dprism.verbose=true",
7272
"-Xmx4g"};
7373

src/main/resources/native/android/c/touch_events.c

+8-13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2020, Gluon
2+
* Copyright (c) 2020, 2022, Gluon
33
*
44
* This program is free software: you can redistribute it and/or modify
55
* it under the terms of the GNU General Public License as published by
@@ -32,7 +32,7 @@ Java_javafx_scene_control_skin_TextFieldSkinAndroid_showSoftwareKeyboard();
3232

3333
JNIEXPORT void JNICALL Java_com_gluonhq_helloandroid_MainActivity_nativeGotTouchEvent(JNIEnv *env, jobject activity, jint jcount, jintArray jactions, jintArray jids, jintArray jxs, jintArray jys)
3434
{
35-
LOGE(stderr, "Native Dalvik layer got touch event, pass to native Graal layer...");
35+
// LOGE(stderr, "Native Dalvik layer got touch event, pass to native Graal layer...");
3636

3737
jlong jlongids[jcount];
3838

@@ -57,23 +57,18 @@ JNIEXPORT void JNICALL Java_com_gluonhq_helloandroid_MainActivity_nativeGotTouch
5757
(*env)->ReleaseIntArrayElements(env, jxs, xs, 0);
5858
(*env)->ReleaseIntArrayElements(env, jys, ys, 0);
5959

60-
LOGE(stderr, "Native Dalvik layer got touch event, passed to native Graal layer...");
60+
// LOGE(stderr, "Native Dalvik layer got touch event, passed to native Graal layer...");
6161
}
6262

63-
JNIEXPORT void JNICALL Java_com_gluonhq_helloandroid_MainActivity_nativedispatchKeyEvent(JNIEnv *env, jobject activity, jint action, jint keyCode, jcharArray jchars, jint cc, jint modifiers)
63+
JNIEXPORT void JNICALL Java_com_gluonhq_helloandroid_MainActivity_nativeDispatchKeyEvent(JNIEnv *env, jobject activity, jint action, jint keyCode, jcharArray jchars, jint cc, jint modifiers)
6464
{
65-
LOGE(stderr, "Native Dalvik layer has to dispatch key event, pass to native Graal layer with %d chars...", cc);
65+
// LOGE(stderr, "Native Dalvik layer has to dispatch key event, pass to native Graal layer with %d chars...", cc);
6666
jchar *kars = (*env)->GetCharArrayElements(env, jchars, 0);
67-
LOGE(stderr, "passed count = %d and realcount = %d\n", cc, (*env)->GetArrayLength(env, jchars));
68-
LOGE(stderr, "c0 = %c and c1 = %c\n", kars[0], kars[1]);
69-
LOGE(stderr, "c0 = %x and c1 = %x\n", kars[0], kars[1]);
7067
androidJfx_gotKeyEvent(action, keyCode, kars, cc, modifiers);
7168
}
7269

73-
JNIEXPORT void JNICALL Java_com_gluonhq_helloandroid_MainActivity_nativeGotKeyEvent(JNIEnv *env, jobject activity, jint action, jint keyCode)
70+
JNIEXPORT void JNICALL Java_com_gluonhq_helloandroid_MainActivity_nativeNotifyMenu(JNIEnv *env, jobject activity, jint x, jint y, jint xAbs, jint yAbs, jboolean isKeyboardTrigger)
7471
{
75-
LOGE(stderr, "Native Dalvik layer got key event, pass to native Graal layer...");
76-
// androidJfx_gotKeyEvent(action, keyCode);
77-
// Java_com_sun_glass_ui_android_DalvikInput_onKeyEventNative(NULL, NULL, action, keyCode);
78-
LOGE(stderr, "Native Dalvik layer got key event!!!");
72+
// LOGE(stderr, "Native Dalvik layer got notify menu, pass to native Graal layer...");
73+
androidJfx_gotMenuEvent(x, y, xAbs, yAbs, isKeyboardTrigger);
7974
}

0 commit comments

Comments
 (0)