Skip to content

Commit 91497c6

Browse files
authored
fix: drag crashing on Android 15 (#453)
1 parent 0361fa5 commit 91497c6

File tree

2 files changed

+31
-24
lines changed

2 files changed

+31
-24
lines changed

super_native_extensions/android/src/main/java/com/superlist/super_native_extensions/DragDropHelper.java

+4-9
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public void onDrawShadow(Canvas canvas) {
5656
}
5757
}
5858

59-
native void updateLastTouchPoint(ViewParent rootView, MotionEvent event);
59+
native void updateLastTouchPoint(ViewParent rootView, int x, int y);
6060

6161
void startDrag(View view, long dragSessionId, ClipData clipData, Bitmap bitmap,
6262
int touchPointX, int touchPointY, int lastTouchEventX, int lastTouchEventY) {
@@ -70,15 +70,10 @@ void startDrag(View view, long dragSessionId, ClipData clipData, Bitmap bitmap,
7070
}
7171
int[] viewLocation = new int[2];
7272
view.getLocationOnScreen(viewLocation);
73-
MotionEvent event = MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(),
74-
MotionEvent.ACTION_MOVE,
75-
lastTouchEventX + viewLocation[0],
76-
lastTouchEventY + viewLocation[1],
77-
0);
78-
event.setSource(InputDevice.SOURCE_CLASS_POINTER);
79-
// Simulate touch event before starting drag which will be the return position
73+
// Override the last touch point which which will be the return position
8074
// on failed drop
81-
updateLastTouchPoint(parent, event);
75+
updateLastTouchPoint(parent, lastTouchEventX + viewLocation[0], lastTouchEventY + viewLocation[1]);
76+
8277
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
8378
view.startDragAndDrop(clipData,
8479
new DragShadowBuilder(bitmap, new Point(touchPointX, touchPointY)), new SessionId(dragSessionId),

super_native_extensions/rust/src/android/drop.rs

+27-15
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@ use std::{
33
collections::HashMap,
44
rc::{Rc, Weak},
55
sync::Arc,
6+
thread,
67
};
78

89
use irondash_engine_context::EngineContext;
910
use irondash_message_channel::{IsolateId, Value};
1011
use irondash_run_loop::RunLoop;
1112
use jni::{
1213
objects::{GlobalRef, JClass, JObject, JString, JValue},
13-
sys::{jlong, jvalue},
14+
sys::{jint, jlong, jvalue},
1415
JNIEnv,
1516
};
1617

@@ -25,6 +26,7 @@ use crate::{
2526
log::OkLog,
2627
reader_manager::RegisteredDataReader,
2728
util::{DropNotifier, NextId},
29+
value_promise::Promise,
2830
};
2931

3032
use super::{
@@ -416,19 +418,28 @@ impl Drop for PlatformDropContext {
416418
fn update_last_touch_point<'a>(
417419
env: &mut JNIEnv<'a>,
418420
view_root: JObject<'a>,
419-
event: &JObject<'a>,
421+
x: i32,
422+
y: i32,
420423
) -> NativeExtensionsResult<()> {
421-
env.call_method(
422-
view_root,
423-
"enqueueInputEvent",
424-
"(Landroid/view/InputEvent;Landroid/view/InputEventReceiver;IZ)V",
425-
&[
426-
(&event).into(),
427-
(&JObject::null()).into(),
428-
1.into(),
429-
true.into(),
430-
],
431-
)?;
424+
let view_root_global = env.new_global_ref(&view_root)?;
425+
let jvm = env.get_java_vm()?;
426+
let p = Arc::new(Promise::new());
427+
let p2 = p.clone();
428+
thread::spawn(move || {
429+
let update = move || -> NativeExtensionsResult<()> {
430+
let mut env = jvm.attach_current_thread()?;
431+
let view_root = view_root_global.as_obj();
432+
let last_touch_point = env
433+
.get_field(view_root, "mLastTouchPoint", "Landroid/graphics/PointF;")?
434+
.l()?;
435+
env.set_field(&last_touch_point, "x", "F", (x as f32).into())?;
436+
env.set_field(&last_touch_point, "y", "F", (y as f32).into())?;
437+
Ok(())
438+
};
439+
p.set(update());
440+
});
441+
p2.wait()?;
442+
432443
Ok(())
433444
}
434445

@@ -440,9 +451,10 @@ pub extern "C" fn Java_com_superlist_super_1native_1extensions_DragDropHelper_up
440451
mut env: JNIEnv<'a>,
441452
_class: JClass,
442453
view_root: JObject<'a>,
443-
event: JObject<'a>,
454+
x: jint,
455+
y: jint,
444456
) {
445-
update_last_touch_point(&mut env, view_root, &event).ok_log();
457+
update_last_touch_point(&mut env, view_root, x, y).ok_log();
446458
}
447459

448460
#[no_mangle]

0 commit comments

Comments
 (0)