Fix mouse capture after returning focus to the window on Android Q

This commit is contained in:
Cameron Gutman 2019-06-05 22:43:16 -07:00
parent 832e7197c5
commit 97702b8861

View File

@ -517,8 +517,17 @@ public class Game extends Activity implements SurfaceHolder.Callback,
// Capture is lost when focus is lost, so it must be requested again // Capture is lost when focus is lost, so it must be requested again
// when focus is regained. // when focus is regained.
if (inputCaptureProvider.isCapturingEnabled() && hasFocus) { if (inputCaptureProvider.isCapturingEnabled() && hasFocus) {
// Recapture the pointer if focus was regained // Recapture the pointer if focus was regained. On Android Q,
streamView.requestPointerCapture(); // we have to delay a bit before requesting capture because otherwise
// we'll hit the "requestPointerCapture called for a window that has no focus"
// error and it will not actually capture the cursor.
Handler h = new Handler();
h.postDelayed(new Runnable() {
@Override
public void run() {
streamView.requestPointerCapture();
}
}, 500);
} }
} }
} }
@ -1405,7 +1414,7 @@ public class Game extends Activity implements SurfaceHolder.Callback,
public void run() { public void run() {
inputCaptureProvider.enableCapture(); inputCaptureProvider.enableCapture();
} }
}, 1000); }, 500);
// Keep the display on // Keep the display on
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);