From dbccfe6bf9771e82c21f18de7a24bf2a949b5d38 Mon Sep 17 00:00:00 2001 From: Jonathan Clarke Date: Sun, 25 Sep 2016 21:23:07 +0100 Subject: [PATCH] fixes for #890 Moved java LayoutListener logic out of 'android' package into pygame bootstrap This avoids deadlock due to LayoutListener locking python thread with pyjnius when app onPause/onResume occurs --- .../src/org/renpy/android/PythonActivity.java | 45 +++++++++++++++++++ .../recipes/android/src/android/_android.pyx | 13 +++--- 2 files changed, 53 insertions(+), 5 deletions(-) diff --git a/pythonforandroid/bootstraps/pygame/build/src/org/renpy/android/PythonActivity.java b/pythonforandroid/bootstraps/pygame/build/src/org/renpy/android/PythonActivity.java index aed7e9ed89..e370e5deb6 100644 --- a/pythonforandroid/bootstraps/pygame/build/src/org/renpy/android/PythonActivity.java +++ b/pythonforandroid/bootstraps/pygame/build/src/org/renpy/android/PythonActivity.java @@ -8,10 +8,12 @@ import android.view.KeyEvent; import android.view.Window; import android.view.WindowManager; +import android.view.ViewTreeObserver; import android.widget.Toast; import android.util.Log; import android.content.pm.PackageManager; import android.content.pm.ApplicationInfo; +import android.graphics.Rect; import java.io.InputStream; import java.io.FileInputStream; @@ -150,6 +152,9 @@ protected void onCreate(Bundle savedInstanceState) { getWindow().getDecorView().setBackgroundColor( this.mInfo.metaData.getInt("android.background_color")); } + + this._layout_listener = new PythonLayoutListener(this); + this.mView.getViewTreeObserver().addOnGlobalLayoutListener(this._layout_listener); } /** @@ -623,5 +628,45 @@ static String billingGetPendingMessage() { return mActivity.mBillingQueue.remove(0); } + + //Layout Listener for kivy + //replaces use of pyjnius in "android" package which can result in deadlock + //on app pause + public class PythonLayoutListener implements ViewTreeObserver.OnGlobalLayoutListener { + + private int height = 0; + private PythonActivity mActivity = null; + + public PythonLayoutListener(PythonActivity _mActivity) { + this.mActivity = _mActivity; + } + + @Override + public void onGlobalLayout() { + Rect rctx = new Rect(); + // print('rctx_bottom: {0}, top: {1}'.format(rctx.bottom, rctx.top)) + this.mActivity.getWindow().getDecorView().getWindowVisibleDisplayFrame(rctx); + // print('rctx_bottom: {0}, top: {1}'.format(rctx.bottom, rctx.top)) + // print('activity height: {0}'.format(mActivity.getWindowManager().getDefaultDisplay().getHeight())) + // NOTE top should always be zero + rctx.top = 0; + this.height = this.mActivity.getWindowManager().getDefaultDisplay().getHeight() - (rctx.bottom - rctx.top); + // print('final height: {0}'.format(self.height)) + + } + + public int getHeight() { + return this.height; + } + + } + + private PythonLayoutListener _layout_listener = null; + + public int getLayoutListenerHeight() { + return this._layout_listener.getHeight(); + } + } + diff --git a/pythonforandroid/recipes/android/src/android/_android.pyx b/pythonforandroid/recipes/android/src/android/_android.pyx index d08898e500..a847eb5edd 100644 --- a/pythonforandroid/recipes/android/src/android/_android.pyx +++ b/pythonforandroid/recipes/android/src/android/_android.pyx @@ -196,14 +196,17 @@ if mActivity: self.height = mActivity.getWindowManager().getDefaultDisplay().getHeight() - (rctx.bottom - rctx.top) # print('final height: {0}'.format(self.height)) - ll = LayoutListener() IF BOOTSTRAP == 'sdl2': + ll = LayoutListener() python_act.getLayout().getViewTreeObserver().addOnGlobalLayoutListener(ll) - ELSE: - python_act.mView.getViewTreeObserver().addOnGlobalLayoutListener(ll) - def get_keyboard_height(): - return ll.height + def get_keyboard_height(): + return ll.height + + ELSE: + def get_keyboard_height(): + return python_act.getLayoutListenerHeight() + else: def get_keyboard_height(): return 0