save thumbnail in separate step, creating new square board-only

drawctxt to render it without scaling.  Draw whole board rather than
just the active rect.
This commit is contained in:
Eric House 2013-11-07 05:44:16 -08:00
parent baba78535f
commit 16923eb27e
4 changed files with 102 additions and 20 deletions

View file

@ -32,12 +32,13 @@ import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
@ -57,8 +58,8 @@ import junit.framework.Assert;
import org.eehouse.android.xw4.jni.*;
import org.eehouse.android.xw4.jni.CommsAddrRec.CommsConnType;
import org.eehouse.android.xw4.jni.JNIThread.*;
import org.eehouse.android.xw4.jni.CurGameInfo.DeviceRole;
import org.eehouse.android.xw4.jni.JNIThread.*;
public class BoardActivity extends XWActivity
@ -2035,13 +2036,16 @@ public class BoardActivity extends XWActivity
interruptBlockingThread();
if ( null != m_jniThread ) {
Bitmap thumb = m_view.getScaledBoard();
m_jniThread.waitToStop( save, thumb );
m_jniThread.waitToStop( save );
m_jniThread = null;
}
clearThis();
// Before we dispose, and after JNIThread has relinquished
// interest, redraw on smaller scale.
takeSnapshot();
XwJNI.game_dispose( m_jniGamePtr );
m_jniGamePtr = 0;
m_gi = null;
@ -2051,6 +2055,26 @@ public class BoardActivity extends XWActivity
}
}
private void takeSnapshot()
{
if ( GitVersion.THUMBNAIL_SUPPORTED ) {
final int size = 150;
Bitmap thumb =
Bitmap.createBitmap( size, size, Bitmap.Config.ARGB_8888 );
Rect bounds = new Rect( 0, 0, size, size );
XwJNI.board_figureLayout( m_jniGamePtr, m_gi, 0, 0, 5, 5, false,
bounds, null );
ThumbCanvas canvas = new ThumbCanvas( this, thumb );
XwJNI.board_setDraw( m_jniGamePtr, canvas );
XwJNI.board_invalAll( m_jniGamePtr );
XwJNI.board_draw( m_jniGamePtr );
DBUtils.saveThumbnail( this, m_gameLock, thumb );
}
}
private void warnIfNoTransport()
{
switch( m_connType ) {

View file

@ -326,18 +326,6 @@ public class DBUtils {
}
}
if ( GitVersion.THUMBNAIL_SUPPORTED ) {
Bitmap thumb = summary.getThumbnail();
if ( null == thumb ) {
values.putNull( DBHelper.THUMBNAIL );
} else {
ByteArrayOutputStream bas = new ByteArrayOutputStream();
thumb.compress( CompressFormat.PNG, 0 , bas );
byte[] data = bas.toByteArray();
values.put( DBHelper.THUMBNAIL, data );
}
}
values.put( DBHelper.SERVERROLE, summary.serverRole.ordinal() );
long result = db.update( DBHelper.TABLE_NAME_SUM,
@ -447,6 +435,29 @@ public class DBUtils {
return 0 != getInt( context, rowid, DBHelper.GAME_OVER, 0 );
}
public static void saveThumbnail( Context context, GameLock lock,
Bitmap thumb )
{
if ( GitVersion.THUMBNAIL_SUPPORTED ) {
initDB( context );
synchronized( s_dbHelper ) {
SQLiteDatabase db = s_dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
String selection = String.format( ROW_ID_FMT, lock.getRowid() );
ByteArrayOutputStream bas = new ByteArrayOutputStream();
thumb.compress( CompressFormat.PNG, 0, bas );
values.put( DBHelper.THUMBNAIL, bas.toByteArray() );
long result = db.update( DBHelper.TABLE_NAME_SUM,
values, selection, null );
Assert.assertTrue( result >= 0 );
db.close();
}
}
}
public static String getRelayID( Context context, long rowid )
{
String result = null;

View file

@ -0,0 +1,50 @@
/* -*- compile-command: "cd ../../../../../; ant debug install"; -*- */
/*
* Copyright 2013 by Eric House (xwords@eehouse.org). All
* rights reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
package org.eehouse.android.xw4;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Rect;
public class ThumbCanvas extends BoardCanvas {
public ThumbCanvas( Activity activity, Bitmap bitmap )
{
super( activity, bitmap, null );
DbgUtils.logf( "creating new ThumbCanvas" );
}
// These should not be needed if common code gets fixed! So the
// whole class should go away. PENDING
@Override
public boolean scoreBegin( Rect rect, int numPlayers, int[] scores,
int remCount )
{
return false;
}
@Override
public boolean trayBegin( Rect rect, int owner, int score )
{
return false;
}
}

View file

@ -137,7 +137,6 @@ public class JNIThread extends Thread {
private static final int kMinDivWidth = 10;
private int m_connsIconID = 0;
private String m_newDict = null;
private Bitmap m_thumbnail;
LinkedBlockingQueue<QueueElem> m_queue;
@ -166,12 +165,11 @@ public class JNIThread extends Thread {
m_queue = new LinkedBlockingQueue<QueueElem>();
}
public void waitToStop( boolean save, Bitmap thumb )
public void waitToStop( boolean save )
{
synchronized ( this ) {
m_stopped = true;
m_saveOnStop = save;
m_thumbnail = thumb;
}
handle( JNICmd.CMD_NONE ); // tickle it
try {
@ -297,7 +295,6 @@ public class JNIThread extends Thread {
// DbgUtils.logf( "no change in game; can skip saving" );
} else {
GameSummary summary = new GameSummary( m_context, m_gi );
summary.setThumbnail( m_thumbnail );
XwJNI.game_summarize( m_jniGamePtr, summary );
DBUtils.saveGame( m_context, m_lock, state, false );
DBUtils.saveSummary( m_context, m_lock, summary );