[android] Minor code style changes. Force stop downloading by special "safe" function.

This commit is contained in:
vng 2012-05-25 20:47:38 +03:00 committed by Alex Zolotarev
parent 4c60127b39
commit 3dfe083be6
3 changed files with 35 additions and 24 deletions

View file

@ -4,12 +4,6 @@
#include "../../../../../defines.hpp"
#include "../../../../../platform/http_request.hpp"
#include "../../../../../base/logging.hpp"
#include "../../../../../base/string_utils.hpp"
#include "../../../../../coding/zip_reader.hpp"
#include "../../../../../coding/url_encode.hpp"
#include "../../../../../coding/reader_streambuf.hpp"
#include "../../../../../coding/internal/file_data.hpp"
@ -17,6 +11,9 @@
#include "../../../../../platform/platform.hpp"
#include "../../../../../platform/http_request.hpp"
#include "../../../../../base/logging.hpp"
#include "../../../../../base/string_utils.hpp"
#include "../../../../../std/vector.hpp"
#include "../../../../../std/string.hpp"
#include "../../../../../std/bind.hpp"
@ -172,7 +169,7 @@ extern "C"
void DownloadFileProgress(shared_ptr<jobject> obj, downloader::HttpRequest & req)
{
LOG(LDEBUG, (req.Progress().first, "bytes for", g_filesToDownload.back().m_fileName, "was downloaded"));
//LOG(LDEBUG, (req.Progress().first, "bytes for", g_filesToDownload.back().m_fileName, "was downloaded"));
FileToDownload & curFile = g_filesToDownload.back();
@ -190,8 +187,8 @@ extern "C"
}
void DownloadURLListFinished(downloader::HttpRequest & req,
downloader::HttpRequest::CallbackT onFinish,
downloader::HttpRequest::CallbackT onProgress)
downloader::HttpRequest::CallbackT const & onFinish,
downloader::HttpRequest::CallbackT const & onProgress)
{
if (req.Status() == downloader::HttpRequest::EFailed)
onFinish(req);
@ -245,8 +242,8 @@ extern "C"
g_currentRequest.reset(downloader::HttpRequest::PostJson(GetPlatform().MetaServerUrl(),
curFile.m_fileName,
bind(&DownloadURLListFinished, _1,
onFinish,
onProgress)));
onFinish,
onProgress)));
return ERR_FILE_IN_PROGRESS;
}

View file

@ -21,10 +21,10 @@ public:
JNIEnv * env = jni::GetEnv();
jclass klass = env->FindClass("com/mapswithme/maps/downloader/DownloadChunkTask");
ASSERT(klass, ("Can't find java class com/mapswithme/maps/downloader/DownloadChunkTask"));
ASSERT(klass, ());
jmethodID methodId = env->GetMethodID(klass, "<init>", "(JLjava/lang/String;JJJLjava/lang/String;Ljava/lang/String;)V");
ASSERT(methodId, ("Can't find java constructor in com/mapswithme/maps/downloader/DownloadChunkTask"));
ASSERT(methodId, ());
// User id is always the same, so do not waste time on every chunk call
static string uniqueUserId = GetPlatform().UniqueClientId();
@ -33,14 +33,14 @@ public:
methodId,
reinterpret_cast<jlong>(&cb),
env->NewStringUTF(url.c_str()),
beg,
end,
expectedFileSize,
(jlong)beg,
(jlong)end,
(jlong)expectedFileSize,
env->NewStringUTF(pb.c_str()),
env->NewStringUTF(uniqueUserId.c_str())));
methodId = env->GetMethodID(klass, "start", "()V");
ASSERT(methodId, ("Can't find java method 'start' in com/mapswithme/maps/downloader/DownloadChunkTask"));
ASSERT(methodId, ());
env->CallVoidMethod(m_self, methodId);
}
@ -48,10 +48,9 @@ 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, "stop", "(V)V");
env->CallVoidMethod(m_self, methodId);
env->CallBooleanMethod(m_self, methodId, false);
env->DeleteGlobalRef(m_self);
}
};

View file

@ -32,7 +32,7 @@ class DownloadChunkTask extends AsyncTask<Void, byte [], Boolean>
native void onFinish(long httpCallbackID, long httpCode, long beg, long end);
public DownloadChunkTask(long httpCallbackID, String url, long beg, long end, long expectedFileSize,
String postBody, String userAgent)
String postBody, String userAgent)
{
m_httpCallbackID = httpCallbackID;
m_url = url;
@ -52,14 +52,19 @@ class DownloadChunkTask extends AsyncTask<Void, byte [], Boolean>
protected void onPostExecute(Boolean success)
{
if (success)
{
assert(!isCancelled());
assert(m_httpCallbackID != 0);
onFinish(m_httpCallbackID, 200, m_beg, m_end);
}
}
@Override
protected void onCancelled()
{
// Report error in callback only if we're not forcibly canceled
if (m_httpErrorCode != NOT_SET)
// Report error in callback only if we're not forcibly canceled.
if (m_httpCallbackID != 0 && m_httpErrorCode != NOT_SET)
onFinish(m_httpCallbackID, m_httpErrorCode, m_beg, m_end);
}
@ -68,7 +73,9 @@ class DownloadChunkTask extends AsyncTask<Void, byte [], Boolean>
{
if (!isCancelled())
{
// Use progress event to save downloaded bytes
assert(m_httpCallbackID != 0);
// Use progress event to save downloaded bytes.
onWrite(m_httpCallbackID, m_beg + m_downloadedBytes, data[0], data[0].length);
m_downloadedBytes += data[0].length;
}
@ -174,4 +181,12 @@ class DownloadChunkTask extends AsyncTask<Void, byte [], Boolean>
// Download has finished
return true;
}
void stop()
{
// Mark that native http thread is not valid any more.
m_httpCallbackID = 0;
cancel(false);
}
}