Workaround[mouse]: Improve the workaround

This commit is contained in:
artdeell
2024-06-19 17:45:51 +03:00
committed by Maksim Belov
parent ea5b6013f6
commit 348fc84cb4
2 changed files with 20 additions and 20 deletions

View File

@@ -25,6 +25,7 @@ import android.os.Bundle;
import android.os.IBinder;
import android.provider.DocumentsContract;
import android.util.Log;
import android.view.InputDevice;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
@@ -38,6 +39,7 @@ import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.core.content.ContextCompat;
import androidx.drawerlayout.widget.DrawerLayout;
@@ -630,10 +632,25 @@ public class MainActivity extends BaseActivity implements ControlButtonMenuListe
}
/*
* Android 14 (or some devices, at least) seems to dispatch the the captured mouse events as trackball events
* due to a bug(?) somewhere(????)
*/
@RequiresApi(api = Build.VERSION_CODES.O)
private boolean checkCaptureDispatchConditions(MotionEvent event) {
int eventSource = event.getSource();
// On my device, the mouse sends events as a relative mouse device.
// Not comparing with == here because apparently `eventSource` is a mask that can
// sometimes indicate multiple sources, like in the case of InputDevice.SOURCE_TOUCHPAD
// (which is *also* an InputDevice.SOURCE_MOUSE when controlling a cursor)
return (eventSource & InputDevice.SOURCE_MOUSE_RELATIVE) != 0 ||
(eventSource & InputDevice.SOURCE_MOUSE) != 0;
}
@Override
public boolean dispatchTrackballEvent(MotionEvent ev) {
// Android 14: route trackball events directly to the MinecraftGLSurface
if(minecraftGLView != null) return minecraftGLView.dispatchTrackballEvent(ev);
return super.dispatchTrackballEvent(ev);
if(MainActivity.isAndroid8OrHigher() && checkCaptureDispatchConditions(ev))
return minecraftGLView.dispatchCapturedPointerEvent(ev);
else return super.dispatchTrackballEvent(ev);
}
}

View File

@@ -79,7 +79,6 @@ public class MinecraftGLSurface extends View implements GrabListener {
private final InGUIEventProcessor mInGUIProcessor = new InGUIEventProcessor(mScaleFactor);
private TouchEventProcessor mCurrentTouchProcessor = mInGUIProcessor;
private AndroidPointerCapture mPointerCapture;
private boolean mCaptureEngaged;
private boolean mLastGrabState = false;
public MinecraftGLSurface(Context context) {
@@ -408,20 +407,4 @@ public class MinecraftGLSurface extends View implements GrabListener {
mSurfaceReadyListenerLock.notifyAll();
}
}
@Override
public void onPointerCaptureChange(boolean hasCapture) {
mCaptureEngaged = hasCapture;
super.onPointerCaptureChange(hasCapture);
}
@Override
public boolean dispatchTrackballEvent(MotionEvent event) {
// Android 14 has broke the pointer capture, which causes the OS to route the captured pointer events to trackball-related methods.
// Correct this by sending trackball events as captured pointer events when the pointer is captured.
if(mCaptureEngaged && mPointerCapture != null && MainActivity.isAndroid8OrHigher()) {
return mPointerCapture.onCapturedPointer(this, event);
}
return super.dispatchTrackballEvent(event);
}
}