fix timer-draw NPE due to race condition

use runOnUiThread()....
This commit is contained in:
Eric House 2017-09-19 22:09:57 -07:00
parent a328221b63
commit 39efbaa7f5

View file

@ -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 && if ( m_lastSecsLeft != secondsLeft || m_lastTimerPlayer != player ) {
(m_lastSecsLeft != secondsLeft || m_lastTimerPlayer != player) ) { final Rect rectCopy = new Rect(rect);
m_lastSecsLeft = secondsLeft; final int secondsLeftCopy = secondsLeft;
m_lastTimerPlayer = player; m_activity.runOnUiThread( new Runnable() {
@Override
public void run() {
if ( null != m_jniThread ) {
m_lastSecsLeft = secondsLeftCopy;
m_lastTimerPlayer = player;
String negSign = secondsLeft < 0? "-":""; String negSign = secondsLeftCopy < 0? "-":"";
secondsLeft = Math.abs( secondsLeft ); int secondsLeft = Math.abs( secondsLeftCopy );
String time = String.format( "%s%d:%02d", negSign, secondsLeft/60, String time =
secondsLeft%60 ); String.format( "%s%d:%02d", negSign,
secondsLeft/60, secondsLeft%60 );
fillRectOther( rect, CommonPrefs.COLOR_BACKGRND ); fillRectOther( rectCopy, CommonPrefs.COLOR_BACKGRND );
m_fillPaint.setColor( m_playerColors[player] ); m_fillPaint.setColor( m_playerColors[player] );
Rect shorter = new Rect( rect ); rectCopy.inset( 0, rectCopy.height() / 5 );
shorter.inset( 0, shorter.height() / 5 ); drawCentered( time, rectCopy, null );
drawCentered( time, shorter, null );
m_jniThread.handle( JNIThread.JNICmd.CMD_DRAW ); m_jniThread.handle( JNIThread.JNICmd.CMD_DRAW );
}
}
} );
} }
} }