From 39efbaa7f5d93a1eca764077bc7f85a5e8b2eeb3 Mon Sep 17 00:00:00 2001 From: Eric House Date: Tue, 19 Sep 2017 22:09:57 -0700 Subject: [PATCH] fix timer-draw NPE due to race condition use runOnUiThread().... --- .../org/eehouse/android/xw4/BoardCanvas.java | 39 ++++++++++++------- 1 file changed, 24 insertions(+), 15 deletions(-) diff --git a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/BoardCanvas.java b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/BoardCanvas.java index 176f6d7cc..5b0dcd7a3 100644 --- a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/BoardCanvas.java +++ b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/BoardCanvas.java @@ -322,26 +322,35 @@ public class BoardCanvas extends Canvas implements DrawCtx { } } - public void drawTimer( Rect rect, int player, int secondsLeft ) + public void drawTimer( Rect rect, final int player, + int secondsLeft ) { - if ( null != m_jniThread && - (m_lastSecsLeft != secondsLeft || m_lastTimerPlayer != player) ) { - m_lastSecsLeft = secondsLeft; - m_lastTimerPlayer = player; + if ( m_lastSecsLeft != secondsLeft || m_lastTimerPlayer != player ) { + final Rect rectCopy = new Rect(rect); + final int secondsLeftCopy = secondsLeft; + m_activity.runOnUiThread( new Runnable() { + @Override + public void run() { + if ( null != m_jniThread ) { + m_lastSecsLeft = secondsLeftCopy; + m_lastTimerPlayer = player; - String negSign = secondsLeft < 0? "-":""; - secondsLeft = Math.abs( secondsLeft ); - String time = String.format( "%s%d:%02d", negSign, secondsLeft/60, - secondsLeft%60 ); + String negSign = secondsLeftCopy < 0? "-":""; + int secondsLeft = Math.abs( secondsLeftCopy ); + String time = + String.format( "%s%d:%02d", negSign, + secondsLeft/60, secondsLeft%60 ); - fillRectOther( rect, CommonPrefs.COLOR_BACKGRND ); - m_fillPaint.setColor( m_playerColors[player] ); + fillRectOther( rectCopy, CommonPrefs.COLOR_BACKGRND ); + m_fillPaint.setColor( m_playerColors[player] ); - Rect shorter = new Rect( rect ); - shorter.inset( 0, shorter.height() / 5 ); - drawCentered( time, shorter, null ); + rectCopy.inset( 0, rectCopy.height() / 5 ); + drawCentered( time, rectCopy, null ); - m_jniThread.handle( JNIThread.JNICmd.CMD_DRAW ); + m_jniThread.handle( JNIThread.JNICmd.CMD_DRAW ); + } + } + } ); } }