From 3e6a7418b19af8b093a25e38d975fdd129807977 Mon Sep 17 00:00:00 2001 From: Eric House Date: Sat, 9 Nov 2013 06:59:33 -0800 Subject: [PATCH] move snapshoting code into GameUtils, and use it whenever loading a GameListItem that should have a snapshot but doesn't. This lets games have snapshots immediately rather than only after they've been opened once. --- .../eehouse/android/xw4/BoardActivity.java | 55 +------------ .../org/eehouse/android/xw4/GameListItem.java | 12 +++ .../org/eehouse/android/xw4/GameUtils.java | 79 +++++++++++++++++++ 3 files changed, 93 insertions(+), 53 deletions(-) diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/BoardActivity.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/BoardActivity.java index c5e9d91e0..28c9d1e27 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/BoardActivity.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/BoardActivity.java @@ -39,7 +39,6 @@ import android.os.Handler; import android.os.Message; import android.text.TextUtils; -import android.view.Display; import android.view.KeyEvent; import android.view.Menu; import android.view.MenuInflater; @@ -2042,7 +2041,8 @@ public class BoardActivity extends XWActivity // Before we dispose, and after JNIThread has relinquished // interest, redraw on smaller scale. - takeSnapshot(); + Bitmap thumb = GameUtils.takeSnapshot( this, m_jniGamePtr, m_gi ); + DBUtils.saveThumbnail( this, m_gameLock, thumb ); XwJNI.game_dispose( m_jniGamePtr ); m_jniGamePtr = 0; @@ -2053,57 +2053,6 @@ public class BoardActivity extends XWActivity } } - private void takeSnapshot() - { - if ( GitVersion.THUMBNAIL_SUPPORTED ) { - Bitmap thumb = null; - if ( XWPrefs.getThumbEnabled( this ) ) { - int nCols = m_gi.boardSize; - int scale = XWPrefs.getThumbScale( this ); - Assert.assertTrue( 0 < scale ); - - Display display = getWindowManager().getDefaultDisplay(); - int width = display.getWidth(); - int height = display.getHeight(); - int dim = Math.min( width, height ) / scale; - int size = dim - (dim % nCols); - - // If user wants active rect, we try to make it as - // large as possible while still not exceeding the - // scale. Since we're only using a fraction of the - // board, the board we draw before clipping may be - // huge. - int[] dims = new int[2]; - Rect activeRect = - XWPrefs.getUseActiveRect( this ) ? new Rect() : null; - if ( null != activeRect ) { - dims = new int[2]; - XwJNI.board_getActiveRect( m_jniGamePtr, activeRect, dims ); - int numCells = Math.max( dims[0], dims[1] ); - size = size * nCols / numCells; - } - thumb = Bitmap.createBitmap( size, size, Bitmap.Config.ARGB_8888 ); - - XwJNI.board_figureLayout( m_jniGamePtr, m_gi, 0, 0, size, size, - 0, 0, 0, 20, 20, false, null ); - - ThumbCanvas canvas = new ThumbCanvas( this, thumb ); - XwJNI.board_setDraw( m_jniGamePtr, canvas ); - XwJNI.board_invalAll( m_jniGamePtr ); - XwJNI.board_draw( m_jniGamePtr ); - - if ( null != activeRect ) { - XwJNI.board_getActiveRect( m_jniGamePtr, activeRect, null ); - thumb = Bitmap.createBitmap( thumb, activeRect.left, - activeRect.top, - activeRect.width(), - activeRect.height() ); - } - } - DBUtils.saveThumbnail( this, m_gameLock, thumb ); - } - } // takeSnapshot - private void warnIfNoTransport() { switch( m_connType ) { diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/GameListItem.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/GameListItem.java index 8233c4435..4cb8df5b9 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/GameListItem.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/GameListItem.java @@ -20,6 +20,7 @@ package org.eehouse.android.xw4; +import android.app.Activity; import android.content.Context; import android.graphics.Bitmap; import android.graphics.Canvas; @@ -48,6 +49,7 @@ public class GameListItem extends LinearLayout private static HashSet s_invalRows = new HashSet(); + private Activity m_activity; private Context m_context; private boolean m_loaded; private long m_rowid; @@ -70,6 +72,9 @@ public class GameListItem extends LinearLayout { super( cx, as ); m_context = cx; + if ( cx instanceof Activity ) { + m_activity = (Activity)cx; + } m_loaded = false; m_rowid = DBUtils.ROWID_NOTFOUND; m_lastMoveTime = 0; @@ -343,6 +348,13 @@ public class GameListItem extends LinearLayout { if ( 0 == --m_loadingCount ) { m_summary = summary; + + if ( null != m_activity && null == summary.getThumbnail() + && XWPrefs.getThumbEnabled( m_context ) ) { + summary.setThumbnail( GameUtils.loadMakeBitmap( m_activity, + m_rowid ) ); + } + setData( summary ); setLoaded( null != m_summary ); synchronized( s_invalRows ) { diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/GameUtils.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/GameUtils.java index ed8fc4793..8a5f8820b 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/GameUtils.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/GameUtils.java @@ -23,9 +23,12 @@ package org.eehouse.android.xw4; import android.app.Activity; import android.content.Context; import android.content.Intent; +import android.graphics.Bitmap; +import android.graphics.Rect; import android.net.Uri; import android.text.Html; import android.text.TextUtils; +import android.view.Display; import java.io.File; import java.io.FileOutputStream; import java.util.Arrays; @@ -47,6 +50,7 @@ public class GameUtils { public static final String INTENT_FORRESULT_ROWID = "forresult"; private static final long GROUPID_UNSPEC = -1; + private static Integer s_minScreen; public static class NoSuchGameException extends RuntimeException { public NoSuchGameException() { @@ -292,6 +296,81 @@ public class GameUtils { return gamePtr; } + public static Bitmap loadMakeBitmap( Activity activity, long rowid ) + { + DbgUtils.logf( "loadMakeBitmap(): taking snapshot..." ); + Bitmap thumb = null; + GameLock lock = new GameLock( rowid, false ); + if ( lock.tryLock() ) { + CurGameInfo gi = new CurGameInfo( activity ); + int gamePtr = loadMakeGame( activity, gi, lock ); + if ( 0 != gamePtr ) { + thumb = takeSnapshot( activity, gamePtr, gi ); + XwJNI.game_dispose( gamePtr ); + } + + lock.unlock(); + } + DbgUtils.logf( "loadMakeBitmap(): done with snapshot" ); + return thumb; + } + + public static Bitmap takeSnapshot( Activity activity, int gamePtr, + CurGameInfo gi ) + { + Bitmap thumb = null; + if ( GitVersion.THUMBNAIL_SUPPORTED ) { + if ( XWPrefs.getThumbEnabled( activity ) ) { + int nCols = gi.boardSize; + int scale = XWPrefs.getThumbScale( activity ); + Assert.assertTrue( 0 < scale ); + + if ( null == s_minScreen ) { + Display display = + activity.getWindowManager().getDefaultDisplay(); + int width = display.getWidth(); + int height = display.getHeight(); + s_minScreen = new Integer( Math.min( width, height ) ); + } + int dim = s_minScreen / scale; + int size = dim - (dim % nCols); + + // If user wants active rect, we try to make it as + // large as possible while still not exceeding the + // scale. Since we're only using a fraction of the + // board, the board we draw before clipping may be + // huge. + int[] dims = new int[2]; + Rect activeRect = + XWPrefs.getUseActiveRect( activity ) ? new Rect() : null; + if ( null != activeRect ) { + dims = new int[2]; + XwJNI.board_getActiveRect( gamePtr, activeRect, dims ); + int numCells = Math.max( dims[0], dims[1] ); + size = size * nCols / numCells; + } + thumb = Bitmap.createBitmap( size, size, Bitmap.Config.ARGB_8888 ); + + XwJNI.board_figureLayout( gamePtr, gi, 0, 0, size, size, + 0, 0, 0, 20, 20, false, null ); + + ThumbCanvas canvas = new ThumbCanvas( activity, thumb ); + XwJNI.board_setDraw( gamePtr, canvas ); + XwJNI.board_invalAll( gamePtr ); + XwJNI.board_draw( gamePtr ); + + if ( null != activeRect ) { + XwJNI.board_getActiveRect( gamePtr, activeRect, null ); + thumb = Bitmap.createBitmap( thumb, activeRect.left, + activeRect.top, + activeRect.width(), + activeRect.height() ); + } + } + } + return thumb; + } + public static long saveGame( Context context, int gamePtr, CurGameInfo gi, GameLock lock, boolean setCreate )