rewrite as Thread instead of AsyncTask

I want to be able to use it in contexts where AsyncTask doesn't make
sense, e.g. from timer in background.
This commit is contained in:
Eric House 2018-02-10 09:45:10 -08:00 committed by Eric House
parent dd298e4549
commit 7fa0cb1b93

View file

@ -25,8 +25,8 @@ import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.net.Uri; import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler;
import android.text.Html; import android.text.Html;
import android.text.TextUtils; import android.text.TextUtils;
import android.view.Display; import android.view.Display;
@ -485,12 +485,12 @@ public class GameUtils {
} }
if ( force ) { if ( force ) {
new ResendTask( context, filter, proc ).execute();
System.arraycopy( sendTimes, 0, /* src */ System.arraycopy( sendTimes, 0, /* src */
sendTimes, 1, /* dest */ sendTimes, 1, /* dest */
sendTimes.length - 1 ); sendTimes.length - 1 );
sendTimes[0] = now; sendTimes[0] = now;
new Resender( context, filter, proc ).start();
} }
} }
@ -1255,23 +1255,27 @@ public class GameUtils {
return result; return result;
} }
private static class ResendTask extends AsyncTask<Void, Void, Void> { private static class Resender extends Thread {
private Context m_context; private Context m_context;
private ResendDoneProc m_doneProc; private ResendDoneProc m_doneProc;
private CommsConnType m_filter; private CommsConnType m_filter;
private int m_nSent = 0; private Handler m_handler;
public ResendTask( Context context, CommsConnType filter, public Resender( Context context, CommsConnType filter,
ResendDoneProc proc ) ResendDoneProc proc )
{ {
m_context = context; m_context = context;
m_filter = filter; m_filter = filter;
m_doneProc = proc; m_doneProc = proc;
if ( null != proc ) {
m_handler = new Handler();
}
} }
@Override @Override
protected Void doInBackground( Void... unused ) public void run()
{ {
int nSentTotal = 0;
HashMap<Long,CommsConnTypeSet> games HashMap<Long,CommsConnTypeSet> games
= DBUtils.getGamesWithSendsPending( m_context ); = DBUtils.getGamesWithSendsPending( m_context );
@ -1296,11 +1300,11 @@ public class GameUtils {
int nSent = XwJNI.comms_resendAll( gamePtr, true, int nSent = XwJNI.comms_resendAll( gamePtr, true,
m_filter, false ); m_filter, false );
gamePtr.release(); gamePtr.release();
Log.d( TAG, "ResendTask.doInBackground(): sent %d " Log.d( TAG, "Resender.doInBackground(): sent %d "
+ "messages for rowid %d", nSent, rowid ); + "messages for rowid %d", nSent, rowid );
m_nSent += sink.numSent(); nSentTotal += sink.numSent();
} else { } else {
Log.d( TAG, "ResendTask.doInBackground(): loadMakeGame()" Log.d( TAG, "Resender.doInBackground(): loadMakeGame()"
+ " failed for rowid %d", rowid ); + " failed for rowid %d", rowid );
} }
lock.unlock(); lock.unlock();
@ -1311,19 +1315,21 @@ public class GameUtils {
false, false ); false, false );
jniThread.release(); jniThread.release();
} else { } else {
Log.w( TAG, "ResendTask.doInBackground: unable to unlock %d", Log.w( TAG, "Resender.doInBackground: unable to unlock %d",
rowid ); rowid );
} }
} }
} }
return null;
}
@Override
protected void onPostExecute( Void unused )
{
if ( null != m_doneProc ) { if ( null != m_doneProc ) {
m_doneProc.onResendDone( m_context, m_nSent ); final int fSentTotal = nSentTotal;
m_handler
.post( new Runnable() {
@Override
public void run() {
m_doneProc.onResendDone( m_context, fSentTotal );
}
});
} }
} }
} }