diff --git a/android/jni/com/mapswithme/platform/HttpThread.cpp b/android/jni/com/mapswithme/platform/HttpThread.cpp index 586ef828e1..8160ed06e7 100644 --- a/android/jni/com/mapswithme/platform/HttpThread.cpp +++ b/android/jni/com/mapswithme/platform/HttpThread.cpp @@ -42,6 +42,14 @@ HttpThread::HttpThread(string const & url, HttpThread::~HttpThread() { + jclass k = jni::GetCurrentThreadJNIEnv()->FindClass("com/mapswithme/maps/downloader/DownloadChunkTask"); + + jni::Method markAsCancelledFn(k, "markAsCancelled", "()V"); + markAsCancelledFn.CallVoid(m_self); + + jni::Method cancelFn(k, "cancel", "(Z)Z"); + cancelFn.CallVoid(m_self, false); + LOG(LINFO, ("destroying http_thread")); } diff --git a/android/src/com/mapswithme/maps/downloader/DownloadChunkTask.java b/android/src/com/mapswithme/maps/downloader/DownloadChunkTask.java index 5271445a55..e2e98a49a0 100644 --- a/android/src/com/mapswithme/maps/downloader/DownloadChunkTask.java +++ b/android/src/com/mapswithme/maps/downloader/DownloadChunkTask.java @@ -35,6 +35,7 @@ class DownloadChunkTask extends AsyncTask private List m_chunks; private boolean m_hasError; + private boolean m_isCancelled; private long m_response; private long m_downloadedSize; @@ -52,6 +53,12 @@ class DownloadChunkTask extends AsyncTask m_requestedSize = end - beg + 1; m_postBody = postBody; m_chunks = new ArrayList(); + m_isCancelled = false; + } + + protected void markAsCancelled() + { + m_isCancelled = true; } @Override @@ -62,6 +69,12 @@ class DownloadChunkTask extends AsyncTask @Override protected void onPostExecute(Void resCode) { + if (m_isCancelled) + { + Log.d(TAG, String.format("downloading was cancelled, chunk %1$d, %2$d", m_beg, m_end)); + return; + } + if (!m_hasError) { byte [] buf = new byte[(int) m_downloadedSize]; @@ -115,6 +128,12 @@ class DownloadChunkTask extends AsyncTask HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); + if (isCancelled()) + { + urlConnection.disconnect(); + return null; + } + Log.d(TAG, String.format("configuring connection parameter for range %1$d : %2$d", m_beg, m_end)); urlConnection.setDoOutput(true); @@ -141,6 +160,12 @@ class DownloadChunkTask extends AsyncTask os.writeChars(m_postBody); } + if (isCancelled()) + { + urlConnection.disconnect(); + return null; + } + Log.d(TAG, ("getting response")); int response = urlConnection.getResponseCode(); @@ -155,6 +180,12 @@ class DownloadChunkTask extends AsyncTask { while (true) { + if (isCancelled()) + { + urlConnection.disconnect(); + return null; + } + long readBytes = is.read(tempBuf); if (readBytes == -1) @@ -200,7 +231,7 @@ class DownloadChunkTask extends AsyncTask m_hasError = true; m_response = -1; } - + return null; } }