fixed weird problem with AsyncTask canceling.

This commit is contained in:
rachytski 2012-05-23 15:24:01 +04:00 committed by Alex Zolotarev
parent 27447517e3
commit d5d446cc2d
2 changed files with 16 additions and 7 deletions

View file

@ -42,8 +42,8 @@ public:
~HttpThread()
{
JNIEnv * env = jni::GetEnv();
jmethodID methodId = jni::GetJavaMethodID(env, m_self, "cancel", "(Z)Z");
ASSERT(methodId, ("Can't find java method 'cancel' in com/mapswithme/maps/downloader/DownloadChunkTask"));
jmethodID methodId = jni::GetJavaMethodID(env, m_self, "safeCancel", "()V");
ASSERT(methodId, ("Can't find java method 'safeCancel' in com/mapswithme/maps/downloader/DownloadChunkTask"));
env->CallBooleanMethod(m_self, methodId, false);
env->DeleteGlobalRef(m_self);

View file

@ -51,7 +51,8 @@ class DownloadChunkTask extends AsyncTask<Void, byte [], Void>
@Override
protected void onPostExecute(Void resCode)
{
onFinish(m_httpCallbackID, 200, m_beg, m_end);
if (!mIsCancelled)
onFinish(m_httpCallbackID, 200, m_beg, m_end);
}
@Override
@ -65,7 +66,7 @@ class DownloadChunkTask extends AsyncTask<Void, byte [], Void>
@Override
protected void onProgressUpdate(byte []... data)
{
if (!isCancelled())
if (!mIsCancelled)
{
// Use progress event to save downloaded bytes
onWrite(m_httpCallbackID, m_beg + m_downloadedBytes, data[0], data[0].length);
@ -88,7 +89,7 @@ class DownloadChunkTask extends AsyncTask<Void, byte [], Void>
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
if (isCancelled())
if (mIsCancelled)
{
urlConnection.disconnect();
return null;
@ -120,7 +121,7 @@ class DownloadChunkTask extends AsyncTask<Void, byte [], Void>
os.flush();
}
if (isCancelled())
if (mIsCancelled)
{
urlConnection.disconnect();
return null;
@ -142,7 +143,7 @@ class DownloadChunkTask extends AsyncTask<Void, byte [], Void>
long readBytes;
while ((readBytes = is.read(tempBuf)) != -1)
{
if (isCancelled())
if (mIsCancelled)
{
urlConnection.disconnect();
return null;
@ -170,4 +171,12 @@ class DownloadChunkTask extends AsyncTask<Void, byte [], Void>
return null;
}
private boolean mIsCancelled = false;
void safeCancel()
{
mIsCancelled = true;
cancel(false);
}
}