From 598bbb3758147af5fd446aac7a487a089d816ca2 Mon Sep 17 00:00:00 2001 From: GaidamakUA Date: Fri, 2 Oct 2015 14:23:07 +0300 Subject: [PATCH] Download progress implemented. --- .../plus/download/DownloadActivity.java | 10 ++++++++-- .../download/items/RegionDialogFragment.java | 19 ++++++++++++++++--- .../download/items/WorldItemsFragment.java | 7 +------ .../newimplementation/DownloadsUiHelper.java | 15 +++++++++++++++ 4 files changed, 40 insertions(+), 11 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/download/DownloadActivity.java b/OsmAnd/src/net/osmand/plus/download/DownloadActivity.java index 887d721155..e54ab24e60 100644 --- a/OsmAnd/src/net/osmand/plus/download/DownloadActivity.java +++ b/OsmAnd/src/net/osmand/plus/download/DownloadActivity.java @@ -627,11 +627,17 @@ public class DownloadActivity extends BaseDownloadActivity { public void setOnProgressUpdateListener(OnProgressUpdateListener onProgressUpdateListener) { this.onProgressUpdateListener = onProgressUpdateListener; - BasicProgressAsyncTask basicProgressAsyncTask = DownloadActivity.downloadListIndexThread.getCurrentRunningTask(); + if (onProgressUpdateListener == null) return; + BasicProgressAsyncTask basicProgressAsyncTask = + DownloadActivity.downloadListIndexThread.getCurrentRunningTask(); final boolean isFinished = basicProgressAsyncTask == null || basicProgressAsyncTask.getStatus() == AsyncTask.Status.FINISHED; if (isFinished) { - this.onProgressUpdateListener.onFinished(); + onProgressUpdateListener.onFinished(); + } else { + onProgressUpdateListener.onProgressUpdate( + basicProgressAsyncTask.getProgressPercentage(), + downloadListIndexThread.getDownloads()); } } diff --git a/OsmAnd/src/net/osmand/plus/download/items/RegionDialogFragment.java b/OsmAnd/src/net/osmand/plus/download/items/RegionDialogFragment.java index 8165bc6805..46d6757edb 100644 --- a/OsmAnd/src/net/osmand/plus/download/items/RegionDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/download/items/RegionDialogFragment.java @@ -2,6 +2,7 @@ package net.osmand.plus.download.items; import android.os.Bundle; import android.support.v4.app.DialogFragment; +import android.support.v4.app.Fragment; import android.support.v7.widget.Toolbar; import android.view.LayoutInflater; import android.view.View; @@ -64,13 +65,24 @@ public class RegionDialogFragment extends DialogFragment { }); if (this.region != null) { - getChildFragmentManager().beginTransaction().add(R.id.fragmentContainer, - RegionItemsFragment.createInstance(region)).commit(); + Fragment fragment = getChildFragmentManager().findFragmentById(R.id.fragmentContainer); + if (fragment == null) { + getChildFragmentManager().beginTransaction().add(R.id.fragmentContainer, + RegionItemsFragment.createInstance(region)).commit(); + } toolbar.setTitle(this.region.getName()); } DownloadsUiHelper.initFreeVersionBanner(view, getMyApplication().getSettings(), getResources()); + + final WorldRegion finalRegion = region; mProgressListener = new DownloadsUiHelper.MapDownloadListener(view, getResources()){ + @Override + public void onProgressUpdate(int progressPercentage, int activeTasks) { + LOG.debug(finalRegion.getName() + " onProgressUpdate()"); + super.onProgressUpdate(progressPercentage, activeTasks); + } + @Override public void onFinished() { super.onFinished(); @@ -85,6 +97,7 @@ public class RegionDialogFragment extends DialogFragment { @Override public void onResume() { super.onResume(); + LOG.debug(region.getName() + " onResume()"); getMyActivity().setOnProgressUpdateListener(mProgressListener); } @@ -109,7 +122,7 @@ public class RegionDialogFragment extends DialogFragment { } public void onRegionSelected(WorldRegion region) { - createInstance(region).show(getChildFragmentManager(), TAG); + DownloadsUiHelper.showDialog(getActivity(), createInstance(region)); } public static RegionDialogFragment createInstance(WorldRegion region) { diff --git a/OsmAnd/src/net/osmand/plus/download/items/WorldItemsFragment.java b/OsmAnd/src/net/osmand/plus/download/items/WorldItemsFragment.java index e10026d3bd..73a9bd28ae 100644 --- a/OsmAnd/src/net/osmand/plus/download/items/WorldItemsFragment.java +++ b/OsmAnd/src/net/osmand/plus/download/items/WorldItemsFragment.java @@ -4,7 +4,6 @@ import android.content.Context; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.support.v4.app.Fragment; -import android.support.v4.app.FragmentTransaction; import android.support.v4.view.MenuItemCompat; import android.view.LayoutInflater; import android.view.Menu; @@ -66,11 +65,7 @@ public class WorldItemsFragment extends Fragment { @Override public void onItemClick(AdapterView parent, View view, int position, long id) { WorldRegion region = worldRegionsAdapter.getItem(position); - - FragmentTransaction fragmentTransaction = getChildFragmentManager().beginTransaction(); - fragmentTransaction.addToBackStack(null); - RegionDialogFragment.createInstance(region) - .show(fragmentTransaction, RegionDialogFragment.TAG); + DownloadsUiHelper.showDialog(getActivity(), RegionDialogFragment.createInstance(region)); } }); diff --git a/OsmAnd/src/net/osmand/plus/download/newimplementation/DownloadsUiHelper.java b/OsmAnd/src/net/osmand/plus/download/newimplementation/DownloadsUiHelper.java index dbb1b3bd5b..2bb9ee6324 100644 --- a/OsmAnd/src/net/osmand/plus/download/newimplementation/DownloadsUiHelper.java +++ b/OsmAnd/src/net/osmand/plus/download/newimplementation/DownloadsUiHelper.java @@ -1,6 +1,10 @@ package net.osmand.plus.download.newimplementation; import android.content.res.Resources; +import android.support.v4.app.DialogFragment; +import android.support.v4.app.Fragment; +import android.support.v4.app.FragmentActivity; +import android.support.v4.app.FragmentTransaction; import android.view.View; import android.widget.ProgressBar; import android.widget.TextView; @@ -46,6 +50,17 @@ public final class DownloadsUiHelper { downloadsLeftProgressBar.setProgress(settings.NUMBER_OF_FREE_DOWNLOADS.get()); } + public static void showDialog(FragmentActivity activity, DialogFragment fragment) { + FragmentTransaction ft = activity.getSupportFragmentManager().beginTransaction(); + Fragment prev = activity.getSupportFragmentManager().findFragmentByTag("dialog"); + if (prev != null) { + ft.remove(prev); + } + ft.addToBackStack(null); + + fragment.show(ft, "dialog"); + } + public static class MapDownloadListener implements DownloadActivity.OnProgressUpdateListener { private final View freeVersionBanner; private final View downloadProgressLayout;