8000 fixes for #890 · hottwaj/python-for-android@dbccfe6 · GitHub
[go: up one dir, main page]

Skip to content

Commit dbccfe6

Browse files
committed
fixes for kivy#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
1 parent 916d60e commit dbccfe6

File tree

2 files changed

+53
-5
lines changed

2 files changed

+53
-5
lines changed

pythonforandroid/bootstraps/pygame/build/src/org/renpy/android/PythonActivity.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@
88
import android.view.KeyEvent;
99
import android.view.Window;
1010
import android.view.WindowManager;
11+
import android.view.ViewTreeObserver;
1112
import android.widget.Toast;
1213
import android.util.Log;
1314
import android.content.pm.PackageManager;
1415
import android.content.pm.ApplicationInfo;
16+
import android.graphics.Rect;
1517

1618
import java.io.InputStream;
1719
import java.io.FileInputStream;
@@ -150,6 +152,9 @@ protected void onCreate(Bundle savedInstanceState) {
150152
getWindow().getDecorView().setBackgroundColor(
151153
this.mInfo.metaData.getInt("android.background_color"));
152154
}
155+
156+
this._layout_listener = new PythonLayoutListener(this);
157+
this.mView.getViewTreeObserver().addOnGlobalLayoutListener(this._layout_listener);
153158
}
154159

155160
/**
@@ -623,5 +628,45 @@ static String billingGetPendingMessage() {
623628
return mActivity.mBillingQueue.remove(0);
624629
}
625630

631+
632+
//Layout Listener for kivy
633+
//replaces use of pyjnius in "android" package which can result in deadlock
634+
//on app pause
635+
public class PythonLayoutListener implements ViewTreeObserver.OnGlobalLayoutListener {
636+
637+
private int height = 0;
638+
private PythonActivity mActivity = null;
639+
640+
public PythonLayoutListener(PythonActivity _mActivity) {
641+
this.mActivity = _mActivity;
642+
}
643+
644+
@Override
645+
public void onGlobalLayout() {
646+
Rect rctx = new Rect();
647+
// print('rctx_bottom: {0}, top: {1}'.format(rctx.bottom, rctx.top))
648+
this.mActivity.getWindow().getDecorView().getWindowVisibleDisplayFrame(rctx);
649+
// print('rctx_bottom: {0}, top: {1}'.format(rctx.bottom, rctx.top))
650+
// print('activity height: {0}'.format(mActivity.getWindowManager().getDefaultDisplay().getHeight()))
651+
// NOTE top should always be zero
652+
rctx.top = 0;
653+
this.height = this.mActivity.getWindowManager().getDefaultDisplay().getHeight() - (rctx.bottom - rctx.top);
654+
// print('final height: {0}'.format(self.height))
655+
656+
}
657+
658+
public int getHeight() {
659+
return this.height;
660+
}
661+
662+
}
663+
664+
private PythonLayoutListener _layout_listener = null;
665+
666+
public int getLayoutListenerHeight() {
667+
return this._layout_listener.getHeight();
668+
}
669+
626670
}
627671

672+

pythonforandroid/recipes/android/src/android/_android.pyx

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -196,14 +196,17 @@ if mActivity:
196196
self.height = mActivity.getWindowManager().getDefaultDisplay().getHeight() - (rctx.bottom - rctx.top)
197197
# print('final height: {0}'.format(self.height))
198198

199-
ll = LayoutListener()
200199
IF BOOTSTRAP == 'sdl2':
200+
ll = LayoutListener()
201201
python_act.getLayout().getViewTreeObserver().addOnGlobalLayoutListener(ll)
202-
ELSE:
203-
python_act.mView.getViewTreeObserver().addOnGlobalLayoutListener(ll)
204202

205-
def get_keyboard_height():
206-
return ll.height
203+
def get_keyboard_height():
204+
return ll.height
205+
206+
ELSE:
207+
def get_keyboard_height():
208+
return python_act.getLayoutListenerHeight()
209+
207210
else:
208211
def get_keyboard_height():
209212
return 0

0 commit comments

Comments
 (0)
0