Skip to content

Commit

Permalink
fix: drag crashing on Android 15 (#453)
Browse files Browse the repository at this point in the history
  • Loading branch information
knopp authored Oct 13, 2024
1 parent 0361fa5 commit 91497c6
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public void onDrawShadow(Canvas canvas) {
}
}

native void updateLastTouchPoint(ViewParent rootView, MotionEvent event);
native void updateLastTouchPoint(ViewParent rootView, int x, int y);

void startDrag(View view, long dragSessionId, ClipData clipData, Bitmap bitmap,
int touchPointX, int touchPointY, int lastTouchEventX, int lastTouchEventY) {
Expand All @@ -70,15 +70,10 @@ void startDrag(View view, long dragSessionId, ClipData clipData, Bitmap bitmap,
}
int[] viewLocation = new int[2];
view.getLocationOnScreen(viewLocation);
MotionEvent event = MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(),
MotionEvent.ACTION_MOVE,
lastTouchEventX + viewLocation[0],
lastTouchEventY + viewLocation[1],
0);
event.setSource(InputDevice.SOURCE_CLASS_POINTER);
// Simulate touch event before starting drag which will be the return position
// Override the last touch point which which will be the return position
// on failed drop
updateLastTouchPoint(parent, event);
updateLastTouchPoint(parent, lastTouchEventX + viewLocation[0], lastTouchEventY + viewLocation[1]);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
view.startDragAndDrop(clipData,
new DragShadowBuilder(bitmap, new Point(touchPointX, touchPointY)), new SessionId(dragSessionId),
Expand Down
42 changes: 27 additions & 15 deletions super_native_extensions/rust/src/android/drop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ use std::{
collections::HashMap,
rc::{Rc, Weak},
sync::Arc,
thread,
};

use irondash_engine_context::EngineContext;
use irondash_message_channel::{IsolateId, Value};
use irondash_run_loop::RunLoop;
use jni::{
objects::{GlobalRef, JClass, JObject, JString, JValue},
sys::{jlong, jvalue},
sys::{jint, jlong, jvalue},
JNIEnv,
};

Expand All @@ -25,6 +26,7 @@ use crate::{
log::OkLog,
reader_manager::RegisteredDataReader,
util::{DropNotifier, NextId},
value_promise::Promise,
};

use super::{
Expand Down Expand Up @@ -416,19 +418,28 @@ impl Drop for PlatformDropContext {
fn update_last_touch_point<'a>(
env: &mut JNIEnv<'a>,
view_root: JObject<'a>,
event: &JObject<'a>,
x: i32,
y: i32,
) -> NativeExtensionsResult<()> {
env.call_method(
view_root,
"enqueueInputEvent",
"(Landroid/view/InputEvent;Landroid/view/InputEventReceiver;IZ)V",
&[
(&event).into(),
(&JObject::null()).into(),
1.into(),
true.into(),
],
)?;
let view_root_global = env.new_global_ref(&view_root)?;
let jvm = env.get_java_vm()?;
let p = Arc::new(Promise::new());
let p2 = p.clone();
thread::spawn(move || {
let update = move || -> NativeExtensionsResult<()> {
let mut env = jvm.attach_current_thread()?;
let view_root = view_root_global.as_obj();
let last_touch_point = env
.get_field(view_root, "mLastTouchPoint", "Landroid/graphics/PointF;")?
.l()?;
env.set_field(&last_touch_point, "x", "F", (x as f32).into())?;
env.set_field(&last_touch_point, "y", "F", (y as f32).into())?;
Ok(())
};
p.set(update());
});
p2.wait()?;

Ok(())
}

Expand All @@ -440,9 +451,10 @@ pub extern "C" fn Java_com_superlist_super_1native_1extensions_DragDropHelper_up
mut env: JNIEnv<'a>,
_class: JClass,
view_root: JObject<'a>,
event: JObject<'a>,
x: jint,
y: jint,
) {
update_last_touch_point(&mut env, view_root, &event).ok_log();
update_last_touch_point(&mut env, view_root, x, y).ok_log();
}

#[no_mangle]
Expand Down

0 comments on commit 91497c6

Please sign in to comment.