From f436090c6fb492529a4531b253eab1ee5e745c2e Mon Sep 17 00:00:00 2001 From: Eric House Date: Thu, 17 Jan 2019 12:28:01 -0800 Subject: [PATCH] install listeners immediately after setting them Fix a race condition introduced by making initing jnithread be asynchronous: all the layout that was supposed to happen after listeners were added instead happened before, so that they weren't there to be installed as part of layout. So now, after adding them, get them hooked up to the UI. (The complexity of this is all historical: at some point the listeners were getting added BEFORE there were views to attach them to, so they were cached and added later. Probably now they could simply be installed as part of adding them. But I'm not doing that now.) --- .../java/org/eehouse/android/xw4/BoardContainer.java | 4 ++-- .../java/org/eehouse/android/xw4/BoardDelegate.java | 8 +++++--- .../src/main/java/org/eehouse/android/xw4/Toolbar.java | 10 ++++++++-- 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/BoardContainer.java b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/BoardContainer.java index e2b092f4b..0f26d5a2c 100644 --- a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/BoardContainer.java +++ b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/BoardContainer.java @@ -70,7 +70,7 @@ public class BoardContainer extends ViewGroup { } @Override - protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) + protected void onMeasure( int widthMeasureSpec, int heightMeasureSpec ) { int width = MeasureSpec.getSize( widthMeasureSpec ); int height = MeasureSpec.getSize( heightMeasureSpec ); @@ -111,7 +111,7 @@ public class BoardContainer extends ViewGroup { // gets it all IFF the trade buttons aren't visible. @Override protected void onLayout( boolean changed, int left, int top, - int right, int bottom) + int right, int bottom ) { // If this isn't true, need to refigure the rects // Assert.assertTrue( 0 == left && 0 == top ); diff --git a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/BoardDelegate.java b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/BoardDelegate.java index 9c3297def..0298232c5 100644 --- a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/BoardDelegate.java +++ b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/BoardDelegate.java @@ -738,6 +738,7 @@ public class BoardDelegate extends DelegateBase if ( null != findViewById( R.id.tbar_parent_hor ) ) { if ( null == m_toolbar ) { m_toolbar = new Toolbar( m_activity, this ); + populateToolbar(); } } } @@ -2150,7 +2151,6 @@ public class BoardDelegate extends DelegateBase } initToolbar(); - populateToolbar(); adjustTradeVisibility(); int flags = DBUtils.getMsgFlags( m_activity, m_rowid ); @@ -2245,7 +2245,7 @@ public class BoardDelegate extends DelegateBase } // Why this m_relayMissing thing? if ( 0 == nMissing /* || !m_relayMissing*/ ) { - Log.d( TAG, "dismissing invite alert %H", m_inviteAlert ); + // Log.d( TAG, "dismissing invite alert %H", m_inviteAlert ); dismissInviteAlert(); } } @@ -2285,6 +2285,7 @@ public class BoardDelegate extends DelegateBase private void populateToolbar() { + Assert.assertTrue( null != m_toolbar || !BuildConfig.DEBUG ); if ( null != m_toolbar ) { m_toolbar.setListener( Buttons.BUTTON_BROWSE_DICT, R.string.not_again_browseall, @@ -2321,7 +2322,8 @@ public class BoardDelegate extends DelegateBase .setListener( Buttons.BUTTON_CHAT, R.string.not_again_chat, R.string.key_notagain_chat, - Action.CHAT_ACTION ); + Action.CHAT_ACTION ) + .installListeners(); } else { Log.e( TAG, "not initing toolbar; still null" ); } diff --git a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/Toolbar.java b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/Toolbar.java index dc86f8aa8..a25bc2503 100644 --- a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/Toolbar.java +++ b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/Toolbar.java @@ -134,11 +134,17 @@ public class Toolbar implements BoardContainer.SizeChangeListener { protected int enabledCount() { return m_enabled.size(); } // SizeChangeListener + @Override public void sizeChanged( int width, int height, boolean isPortrait ) + { + installListeners(); + doShowHide(); + } + + public void installListeners() { tryAddListeners( m_onClickListeners ); tryAddListeners( m_onLongClickListeners ); - doShowHide(); } private void tryAddListeners( Map map ) @@ -163,7 +169,7 @@ public class Toolbar implements BoardContainer.SizeChangeListener { } else if ( listener instanceof View.OnLongClickListener ) { button.setOnLongClickListener( (View.OnLongClickListener)listener ); } else { - Assert.fail(); + Assert.assertFalse( BuildConfig.DEBUG ); } } return success;