[android] Fix download progress (#1119)

* [android] Fix download progress

mTrackingIds (Java) and g_completionHandlers (C++) do the same job.

Signed-off-by: Roman Tsisyk <roman@tsisyk.com>
Signed-off-by: Alexander Borsuk <me@alex.bio>
Co-authored-by: Alexander Borsuk <me@alex.bio>
This commit is contained in:
Roman Tsisyk 2021-08-18 18:50:21 +03:00 committed by GitHub
parent 473a8ecd97
commit 697ad142e0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 18 additions and 22 deletions

View file

@ -198,4 +198,11 @@ Java_com_mapswithme_maps_downloader_MapManager_nativeOnDownloadProgress(JNIEnv *
it->second.second(static_cast<int64_t>(bytesDownloaded), static_cast<int64_t>(bytesTotal));
}
JNIEXPORT jboolean JNICALL
Java_com_mapswithme_maps_downloader_MapManager_nativeHasPendingDownloads(JNIEnv *, jclass)
{
return !g_completionHandlers.empty();
}
} // extern "C"

View file

@ -136,7 +136,7 @@ public class MapDownloadManager
requestId = id;
}
mProgressTracker.add(requestId);
startProgressTracking();
return requestId;
}
@ -144,13 +144,11 @@ public class MapDownloadManager
@MainThread
public void remove(long requestId)
{
mProgressTracker.remove(requestId);
mDownloadManager.remove(requestId);
}
public void onDownloadFinished(boolean status, long id)
{
mProgressTracker.remove(id);
MapManager.nativeOnDownloadFinished(status, id);
}

View file

@ -5,14 +5,12 @@ import android.content.Context;
import android.database.Cursor;
import androidx.annotation.NonNull;
import com.mapswithme.util.Utils;
import com.mapswithme.util.concurrency.UiThread;
import com.mapswithme.util.log.Logger;
import com.mapswithme.util.log.LoggerFactory;
import java.util.HashSet;
import java.util.Set;
public class MapDownloadProgressTracker
{
private static final Logger LOGGER = LoggerFactory.INSTANCE.getLogger(LoggerFactory.Type.DOWNLOADER);
@ -22,8 +20,6 @@ public class MapDownloadProgressTracker
@NonNull
private final DownloadManager mDownloadManager;
@NonNull
private final Set<Long> mTrackingIds = new HashSet<>();
private boolean mTrackingEnabled = false;
private final Runnable mTrackingMethod = this::trackProgress;
@ -44,7 +40,8 @@ public class MapDownloadProgressTracker
return;
mTrackingEnabled = true;
trackProgress();
// Delayed start, because g_completionHandlers are empty until enquee() method finishes.
UiThread.runLater(mTrackingMethod, PROGRESS_TRACKING_INTERVAL_MILLISECONDS);
}
public void stop()
@ -53,21 +50,17 @@ public class MapDownloadProgressTracker
UiThread.cancelDelayedTasks(mTrackingMethod);
}
public void add(long id)
{
mTrackingIds.add(id);
}
public void remove(long id)
{
mTrackingIds.remove(id);
}
private void trackProgress()
{
if (!mTrackingEnabled)
return;
if (!MapManager.nativeHasPendingDownloads())
{
mTrackingEnabled = false;
return;
}
DownloadManager.Query query = new DownloadManager.Query();
query.setFilterByStatus(DownloadManager.STATUS_RUNNING);
Cursor cursor = null;
@ -80,9 +73,6 @@ public class MapDownloadProgressTracker
for (int i = 0; i < count; ++i)
{
long id = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_ID));
if (!mTrackingIds.contains(id))
continue;
long bytesDownloaded = cursor.getLong(cursor.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
long bytesTotal = cursor.getLong(cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));

View file

@ -418,4 +418,5 @@ public final class MapManager
public static native void nativeOnDownloadFinished(boolean status, long id);
public static native void nativeOnDownloadProgress(long id, long bytesDownloaded, long bytesTotal);
public static native boolean nativeHasPendingDownloads();
}