From 122b270025ae2c1291ef0e0be62130c36cd2e56b Mon Sep 17 00:00:00 2001 From: Eric House Date: Tue, 5 Feb 2019 07:44:16 -0800 Subject: [PATCH] fix NPE and let GameSummary stay until replaced GameListItem was clearing its summary when kicking off a replacement load. Instead keep the old around in case somebody wants it (e.g. to figure out what menus to enable) until the reload finishes. Also add logging of how long loading takes. I think a cache might be called for. --- .../java/org/eehouse/android/xw4/DBUtils.java | 10 ++++++++-- .../org/eehouse/android/xw4/GameListItem.java | 3 +-- .../eehouse/android/xw4/GamesListDelegate.java | 15 +++++++++++---- 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/DBUtils.java b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/DBUtils.java index 397a0d2f4..31ed2ccdf 100644 --- a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/DBUtils.java +++ b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/DBUtils.java @@ -131,6 +131,7 @@ public class DBUtils { public static GameSummary getSummary( Context context, GameLock lock ) { + long startMS = System.currentTimeMillis(); initDB( context ); GameSummary summary = null; String[] columns = { ROW_ID, @@ -154,8 +155,8 @@ public class DBUtils { selection, null, null, null, null ); if ( 1 == cursor.getCount() && cursor.moveToFirst() ) { summary = new GameSummary(); - summary.nMoves = cursor.getInt(cursor. - getColumnIndex(DBHelper.NUM_MOVES)); + summary.nMoves = cursor + .getInt( cursor.getColumnIndex(DBHelper.NUM_MOVES) ); summary.nPlayers = cursor.getInt(cursor. getColumnIndex(DBHelper.NUM_PLAYERS)); @@ -264,6 +265,11 @@ public class DBUtils { if ( null == summary && lock.canWrite() ) { summary = GameUtils.summarize( context, lock ); } + long endMS = System.currentTimeMillis(); + + // Might want to be cacheing this... + Log.d( TAG, "getSummary(rowid=%d) => %s (took %dms)", + lock.getRowid(), summary, endMS - startMS ); return summary; } // getSummary diff --git a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/GameListItem.java b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/GameListItem.java index cb3203c48..b4cd40d4a 100644 --- a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/GameListItem.java +++ b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/GameListItem.java @@ -91,9 +91,9 @@ public class GameListItem extends LinearLayout m_dsdel = new DrawSelDelegate( this ); } + // Might return null!! public GameSummary getSummary() { - Assert.assertNotNull( m_summary ); return m_summary; } @@ -111,7 +111,6 @@ public class GameListItem extends LinearLayout public void forceReload() { // DbgUtils.logf( "GameListItem.forceReload: rowid=%d", m_rowid ); - m_summary = null; setLoaded( false ); // Apparently it's impossible to reliably cancel an existing // AsyncTask, so let it complete, but drop the results as soon diff --git a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/GamesListDelegate.java b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/GamesListDelegate.java index b90aef833..cdec67cce 100644 --- a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/GamesListDelegate.java +++ b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/GamesListDelegate.java @@ -1624,6 +1624,7 @@ public class GamesListDelegate extends ListDelegateBase return handled;// || super.onOptionsItemSelected( item ); } + @Override public void onCreateContextMenu( ContextMenu menu, View view, ContextMenuInfo menuInfo ) { @@ -1664,9 +1665,15 @@ public class GamesListDelegate extends ListDelegateBase enable = BoardDelegate.rematchSupported( m_activity, rowID ); Utils.setItemVisible( menu, R.id.games_game_rematch, enable ); - boolean isMultiGame = item.getSummary().isMultiGame(); - enable = isMultiGame - && (BuildConfig.DEBUG || XWPrefs.getDebugEnabled( m_activity )); + // Deal with possibility summary's temporarily null.... + GameSummary summary = item.getSummary(); + enable = false; + boolean isMultiGame = false; + if ( null != summary ) { + isMultiGame = summary.isMultiGame(); + enable = isMultiGame + && (BuildConfig.DEBUG || XWPrefs.getDebugEnabled( m_activity )); + } Utils.setItemVisible( menu, R.id.games_game_invites, enable ); Utils.setItemVisible( menu, R.id.games_game_netstats, isMultiGame ); @@ -1675,7 +1682,7 @@ public class GamesListDelegate extends ListDelegateBase Utils.setItemVisible( menu, R.id.games_game_reset, enable ); } } - } + } // onCreateContextMenu public boolean onContextItemSelected( MenuItem item ) {