From 7fa0cb1b93b1f9bd97996af3f28a7205aaca833e Mon Sep 17 00:00:00 2001 From: Eric House Date: Sat, 10 Feb 2018 09:45:10 -0800 Subject: [PATCH] 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. --- .../org/eehouse/android/xw4/GameUtils.java | 42 +++++++++++-------- 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/GameUtils.java b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/GameUtils.java index 97e6b8b70..bcef8314a 100644 --- a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/GameUtils.java +++ b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/GameUtils.java @@ -25,8 +25,8 @@ import android.content.Context; import android.content.Intent; import android.graphics.Bitmap; import android.net.Uri; -import android.os.AsyncTask; import android.os.Bundle; +import android.os.Handler; import android.text.Html; import android.text.TextUtils; import android.view.Display; @@ -485,12 +485,12 @@ public class GameUtils { } if ( force ) { - new ResendTask( context, filter, proc ).execute(); - System.arraycopy( sendTimes, 0, /* src */ sendTimes, 1, /* dest */ sendTimes.length - 1 ); sendTimes[0] = now; + + new Resender( context, filter, proc ).start(); } } @@ -1255,23 +1255,27 @@ public class GameUtils { return result; } - private static class ResendTask extends AsyncTask { + private static class Resender extends Thread { private Context m_context; private ResendDoneProc m_doneProc; private CommsConnType m_filter; - private int m_nSent = 0; + private Handler m_handler; - public ResendTask( Context context, CommsConnType filter, - ResendDoneProc proc ) + public Resender( Context context, CommsConnType filter, + ResendDoneProc proc ) { m_context = context; m_filter = filter; m_doneProc = proc; + if ( null != proc ) { + m_handler = new Handler(); + } } @Override - protected Void doInBackground( Void... unused ) + public void run() { + int nSentTotal = 0; HashMap games = DBUtils.getGamesWithSendsPending( m_context ); @@ -1296,11 +1300,11 @@ public class GameUtils { int nSent = XwJNI.comms_resendAll( gamePtr, true, m_filter, false ); gamePtr.release(); - Log.d( TAG, "ResendTask.doInBackground(): sent %d " + Log.d( TAG, "Resender.doInBackground(): sent %d " + "messages for rowid %d", nSent, rowid ); - m_nSent += sink.numSent(); + nSentTotal += sink.numSent(); } else { - Log.d( TAG, "ResendTask.doInBackground(): loadMakeGame()" + Log.d( TAG, "Resender.doInBackground(): loadMakeGame()" + " failed for rowid %d", rowid ); } lock.unlock(); @@ -1311,19 +1315,21 @@ public class GameUtils { false, false ); jniThread.release(); } else { - Log.w( TAG, "ResendTask.doInBackground: unable to unlock %d", + Log.w( TAG, "Resender.doInBackground: unable to unlock %d", rowid ); } } } - return null; - } - @Override - protected void onPostExecute( Void unused ) - { 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 ); + } + }); } } }