From fb02b19b4c26d61dcaceb3b7e5daa68a4f24b8f6 Mon Sep 17 00:00:00 2001 From: rachytski Date: Sun, 26 Feb 2012 23:35:11 +0400 Subject: [PATCH] supporting smooth progress while copying files from bundle file. --- .../mapswithme/maps/CopyResourcesActivity.cpp | 37 ++++++++++++++++++- .../maps/CopyResourcesActivity.java | 14 +++++-- 2 files changed, 45 insertions(+), 6 deletions(-) diff --git a/android/jni/com/mapswithme/maps/CopyResourcesActivity.cpp b/android/jni/com/mapswithme/maps/CopyResourcesActivity.cpp index db24d64027..42319d0ad8 100644 --- a/android/jni/com/mapswithme/maps/CopyResourcesActivity.cpp +++ b/android/jni/com/mapswithme/maps/CopyResourcesActivity.cpp @@ -13,6 +13,9 @@ #include "../../../../../std/vector.hpp" #include "../../../../../std/string.hpp" +#include "../jni/jni_thread.hpp" +#include "../jni/jni_method.hpp" + // Special error codes to notify GUI about free space //@{ #define ERR_COPIED_SUCCESSFULLY 0 @@ -35,6 +38,12 @@ static jint g_copiedBytesProgress = 0; extern "C" { + JNIEXPORT jint JNICALL + Java_com_mapswithme_maps_CopyResourcesActivity_nativeGetCopiedBytes(JNIEnv * env, jobject thiz) + { + return g_copiedBytesProgress; + } + JNIEXPORT jint JNICALL Java_com_mapswithme_maps_CopyResourcesActivity_nativeGetBytesToCopy(JNIEnv * env, jobject thiz, jstring apkPath, jstring sdcardPath) @@ -90,8 +99,21 @@ extern "C" return totalSizeToCopy; } + jobject g_observer = 0; + jni::Method * g_progressFn = 0; + int g_cycles = 0; + + void CopyFileProgress(int size, int pos) + { + g_cycles++; + /// calling JNI method only once in 5 cycles, as there + /// are an overhead on frequent calls. + if ((g_cycles %=5) == 0) + g_progressFn->CallVoid(g_observer, size, pos); + } + JNIEXPORT jint JNICALL - Java_com_mapswithme_maps_CopyResourcesActivity_nativeCopyNextFile(JNIEnv * env, jobject thiz) + Java_com_mapswithme_maps_CopyResourcesActivity_nativeCopyNextFile(JNIEnv * env, jobject thiz, jobject observer) { if (g_filesToCopy.empty()) return ERR_COPIED_SUCCESSFULLY; @@ -110,16 +132,27 @@ extern "C" return ERR_NOT_ENOUGH_FREE_SPACE; } + jclass k = jni::GetCurrentThreadJNIEnv()->GetObjectClass(observer); + g_progressFn = new jni::Method(k, "onFileProgress", "(II)V"); + g_observer = observer; + g_cycles = 0; + // Perform copying try { - ZipFileReader::UnzipFile(g_apkPath, it->m_pathInZip, it->m_pathInSdcard); + ZipFileReader::UnzipFile(g_apkPath, it->m_pathInZip, it->m_pathInSdcard, &CopyFileProgress); } catch (std::exception const & e) { LOG(LERROR, ("Error while extracting", it->m_pathInZip, "from apk to", it->m_pathInSdcard)); + delete g_progressFn; + g_observer = 0; return ERR_NOT_ENOUGH_FREE_SPACE; } + + g_observer = 0; + delete g_progressFn; + g_copiedBytesProgress += it->m_uncompressedSize; g_filesToCopy.erase(it); return g_copiedBytesProgress; diff --git a/android/src/com/mapswithme/maps/CopyResourcesActivity.java b/android/src/com/mapswithme/maps/CopyResourcesActivity.java index 8df99df20f..0f00191858 100644 --- a/android/src/com/mapswithme/maps/CopyResourcesActivity.java +++ b/android/src/com/mapswithme/maps/CopyResourcesActivity.java @@ -30,7 +30,7 @@ public class CopyResourcesActivity extends Activity { private final String m_apkPath; private final String m_sdcardPath; - + CopyResourcesTask(String apkPath, String sdcardPath) { m_apkPath = apkPath; @@ -79,6 +79,11 @@ public class CopyResourcesActivity extends Activity CopyResourcesActivity.this.onCopyResourcesProgress(copiedBytes[0]); } + protected void onFileProgress(int size, int pos) + { + publishProgress(nativeGetCopiedBytes() + pos); + } + @Override protected Integer doInBackground(Void... p) { @@ -86,7 +91,7 @@ public class CopyResourcesActivity extends Activity int bytesCopied; do { - bytesCopied = nativeCopyNextFile(); + bytesCopied = nativeCopyNextFile(this); if (bytesCopied > 0) publishProgress(new Integer(bytesCopied)); else if (bytesCopied < 0) @@ -152,7 +157,7 @@ public class CopyResourcesActivity extends Activity final String str = getString(R.string.app_name); int len = current * str.length() / total; if (len <= 0) - len = 1; + len = 0; else if (len > str.length()) len = str.length(); return String.format(getString(R.string.loading), str.substring(0, len)); @@ -180,5 +185,6 @@ public class CopyResourcesActivity extends Activity private native void nativeMoveMaps(String fromFolder, String toFolder); private native int nativeGetBytesToCopy(String m_apkPath, String m_sdcardPath); - private native int nativeCopyNextFile(); + private native int nativeGetCopiedBytes(); + private native int nativeCopyNextFile(Object observer); }