From 6d75254f7c0072930812c514343bb90771530a2c Mon Sep 17 00:00:00 2001 From: GaidamakUA Date: Thu, 1 Oct 2015 18:10:46 +0300 Subject: [PATCH] Downloads progress implementation in progress. --- .../src/net/osmand/util/Algorithms.java | 24 ++++-- OsmAnd/res/layout/download_index_fragment.xml | 1 + .../layout/local_index_fragment_header.xml | 12 +-- .../res/layout/maps_in_category_fragment.xml | 47 ++++++++++++ OsmAnd/res/values/strings.xml | 1 + .../plus/download/DownloadActivity.java | 31 +++++++- .../plus/download/DownloadIndexFragment.java | 2 - .../download/items/DownloadItemsFragment.java | 4 + .../MapsInCategoryFragment.java | 75 +++++++++++++++++-- .../NewLocalIndexesFragment.java | 6 +- .../SubcategoriesFragment.java | 10 +-- .../plus/osmedit/BasicDataFragment.java | 4 +- 12 files changed, 181 insertions(+), 36 deletions(-) diff --git a/OsmAnd-java/src/net/osmand/util/Algorithms.java b/OsmAnd-java/src/net/osmand/util/Algorithms.java index 7da3e44d21..e6fa8347bd 100644 --- a/OsmAnd-java/src/net/osmand/util/Algorithms.java +++ b/OsmAnd-java/src/net/osmand/util/Algorithms.java @@ -1,6 +1,10 @@ package net.osmand.util; +import net.osmand.PlatformUtil; + +import org.apache.commons.logging.Log; + import java.io.BufferedReader; import java.io.Closeable; import java.io.EOFException; @@ -11,7 +15,6 @@ import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; -import java.io.UnsupportedEncodingException; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; @@ -20,10 +23,6 @@ import java.util.Iterator; import java.util.Map; import java.util.Map.Entry; -import net.osmand.PlatformUtil; - -import org.apache.commons.logging.Log; - /** * Basic algorithms that are not in jdk @@ -504,6 +503,21 @@ public class Algorithms { return hours + ":" + min + ":" + sec; } } + + public static String formatMinutesDuration(int minutes) { + if (minutes < 60) { + return String.valueOf(minutes); + } else { + String min; + if (minutes % 60 < 10) { + min = "0" + (minutes % 60); + } else { + min = (minutes % 60) + ""; + } + int hours = minutes / 60; + return hours + ":" + min; + } + } public static > T parseEnumValue(T[] cl, String val, T defaultValue){ for(int i = 0; i< cl.length; i++) { diff --git a/OsmAnd/res/layout/download_index_fragment.xml b/OsmAnd/res/layout/download_index_fragment.xml index 673114158f..b366869679 100644 --- a/OsmAnd/res/layout/download_index_fragment.xml +++ b/OsmAnd/res/layout/download_index_fragment.xml @@ -3,6 +3,7 @@ android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> + + tools:text="@string/device_memory"/> + + + + + + + + + + + + + + + + + Any unsaved changes will be lost. Continue? %1$s downloads left Roads + Downloding - %1$d file diff --git a/OsmAnd/src/net/osmand/plus/download/DownloadActivity.java b/OsmAnd/src/net/osmand/plus/download/DownloadActivity.java index 252cc0ef90..d67d859ac0 100644 --- a/OsmAnd/src/net/osmand/plus/download/DownloadActivity.java +++ b/OsmAnd/src/net/osmand/plus/download/DownloadActivity.java @@ -78,6 +78,8 @@ public class DownloadActivity extends BaseDownloadActivity { private List downloadTypes = new ArrayList(); private List cats; + private OnProgressUpdateListener onProgressUpdateListener; + @Override protected void onCreate(Bundle savedInstanceState) { getMyApplication().applyTheme(this); @@ -255,6 +257,17 @@ public class DownloadActivity extends BaseDownloadActivity { @Override public void updateProgress(boolean updateOnlyProgress) { BasicProgressAsyncTask basicProgressAsyncTask = DownloadActivity.downloadListIndexThread.getCurrentRunningTask(); + final boolean isFinished = basicProgressAsyncTask == null + || basicProgressAsyncTask.getStatus() == AsyncTask.Status.FINISHED; + if (onProgressUpdateListener != null) { + if (isFinished) { + onProgressUpdateListener.onFinished(); + } else { + onProgressUpdateListener.onProgressUpdate( + basicProgressAsyncTask.getProgressPercentage(), + downloadListIndexThread.getDownloads()); + } + } //needed when rotation is performed and progress can be null if (progressView == null) { return; @@ -265,9 +278,8 @@ public class DownloadActivity extends BaseDownloadActivity { determinateProgressBar.setProgress(basicProgressAsyncTask.getProgressPercentage()); } } else { - boolean visible = basicProgressAsyncTask != null && basicProgressAsyncTask.getStatus() != AsyncTask.Status.FINISHED; - progressView.setVisibility(visible ? View.VISIBLE : View.GONE); - if (visible) { + progressView.setVisibility(!isFinished ? View.VISIBLE : View.GONE); + if (!isFinished) { boolean indeterminate = basicProgressAsyncTask.isIndeterminate(); indeterminateProgressBar.setVisibility(!indeterminate ? View.GONE : View.VISIBLE); determinateProgressBar.setVisibility(indeterminate ? View.GONE : View.VISIBLE); @@ -613,5 +625,18 @@ public class DownloadActivity extends BaseDownloadActivity { } } + public void setOnProgressUpdateListener(OnProgressUpdateListener onProgressUpdateListener) { + this.onProgressUpdateListener = onProgressUpdateListener; + BasicProgressAsyncTask basicProgressAsyncTask = DownloadActivity.downloadListIndexThread.getCurrentRunningTask(); + final boolean isFinished = basicProgressAsyncTask == null + || basicProgressAsyncTask.getStatus() == AsyncTask.Status.FINISHED; + if (isFinished) { + this.onProgressUpdateListener.onFinished(); + } + } + public interface OnProgressUpdateListener { + void onProgressUpdate(int progressPercentage, int activeTasks); + void onFinished(); + } } diff --git a/OsmAnd/src/net/osmand/plus/download/DownloadIndexFragment.java b/OsmAnd/src/net/osmand/plus/download/DownloadIndexFragment.java index 0802692936..dec73d6306 100644 --- a/OsmAnd/src/net/osmand/plus/download/DownloadIndexFragment.java +++ b/OsmAnd/src/net/osmand/plus/download/DownloadIndexFragment.java @@ -214,8 +214,6 @@ public class DownloadIndexFragment extends OsmandExpandableListFragment { } } - - @Override public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { final IndexItem e = listAdapter.getChild(groupPosition, childPosition); diff --git a/OsmAnd/src/net/osmand/plus/download/items/DownloadItemsFragment.java b/OsmAnd/src/net/osmand/plus/download/items/DownloadItemsFragment.java index 04a5f61ed3..2539b13c15 100644 --- a/OsmAnd/src/net/osmand/plus/download/items/DownloadItemsFragment.java +++ b/OsmAnd/src/net/osmand/plus/download/items/DownloadItemsFragment.java @@ -21,6 +21,7 @@ import net.osmand.plus.OsmandApplication; import net.osmand.plus.R; import net.osmand.plus.WorldRegion; import net.osmand.plus.download.DownloadActivity; +import net.osmand.plus.download.newimplementation.DownloadsUiInitHelper; import org.apache.commons.logging.Log; @@ -76,6 +77,9 @@ public class DownloadItemsFragment extends Fragment { fillVoicePromtsAdapter(); } + DownloadsUiInitHelper.initFreeVersionBanner(view, + getMyApplication().getSettings(), getResources()); + return view; } diff --git a/OsmAnd/src/net/osmand/plus/download/newimplementation/MapsInCategoryFragment.java b/OsmAnd/src/net/osmand/plus/download/newimplementation/MapsInCategoryFragment.java index 4640b42505..2b193eaf2d 100644 --- a/OsmAnd/src/net/osmand/plus/download/newimplementation/MapsInCategoryFragment.java +++ b/OsmAnd/src/net/osmand/plus/download/newimplementation/MapsInCategoryFragment.java @@ -1,5 +1,6 @@ package net.osmand.plus.download.newimplementation; +import android.content.res.Resources; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.v4.app.DialogFragment; @@ -7,12 +8,14 @@ import android.support.v7.widget.Toolbar; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; +import android.widget.ProgressBar; +import android.widget.TextView; import net.osmand.PlatformUtil; import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandSettings; import net.osmand.plus.R; -import net.osmand.plus.helpers.HasName; +import net.osmand.plus.download.DownloadActivity; import org.apache.commons.logging.Log; @@ -20,6 +23,7 @@ public class MapsInCategoryFragment extends DialogFragment { private static final Log LOG = PlatformUtil.getLog(IndexItemCategoryWithSubcat.class); public static final String TAG = "MapsInCategoryFragment"; private static final String CATEGORY = "category"; + private MapDownloadListener mProgressListener; @Override public void onCreate(Bundle savedInstanceState) { @@ -33,7 +37,7 @@ public class MapsInCategoryFragment extends DialogFragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - View view = inflater.inflate(R.layout.maps_in_category_fragment, container, false); + final View view = inflater.inflate(R.layout.maps_in_category_fragment, container, false); IndexItemCategoryWithSubcat category = getArguments().getParcelable(CATEGORY); assert category != null; @@ -51,9 +55,41 @@ public class MapsInCategoryFragment extends DialogFragment { } }); + DownloadsUiInitHelper.initFreeVersionBanner(view, + getMyActivity().getMyApplication().getSettings(), getResources()); + mProgressListener = new MapDownloadListener(view, getResources()){ + @Override + public void onFinished() { + super.onFinished(); + DownloadsUiInitHelper.initFreeVersionBanner(view, + getMyActivity().getMyApplication().getSettings(), getResources()); + } + }; + view.findViewById(R.id.downloadProgressLayout).setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + + } + }); return view; } + @Override + public void onResume() { + super.onResume(); + getMyActivity().setOnProgressUpdateListener(mProgressListener); + } + + @Override + public void onPause() { + super.onPause(); + getMyActivity().setOnProgressUpdateListener(null); + } + + private DownloadActivity getMyActivity() { + return (DownloadActivity) getActivity(); + } + public void onCategorySelected(@NonNull IndexItemCategoryWithSubcat category) { LOG.debug("onCategorySelected()"); createInstance(category).show(getChildFragmentManager(), TAG); @@ -68,16 +104,39 @@ public class MapsInCategoryFragment extends DialogFragment { return fragment; } - public static class Divider implements HasName { - private final String text; + private static class MapDownloadListener implements DownloadActivity.OnProgressUpdateListener { + private final View freeVersionBanner; + private final View downloadProgressLayout; + private final ProgressBar progressBar; + private final TextView leftTextView; + private final TextView rightTextView; + private final Resources resources; - public Divider(String text) { - this.text = text; + MapDownloadListener(View view, Resources resources) { + this.resources = resources; + freeVersionBanner = view.findViewById(R.id.freeVersionBanner); + downloadProgressLayout = view.findViewById(R.id.downloadProgressLayout); + progressBar = (ProgressBar) view.findViewById(R.id.progressBar); + leftTextView = (TextView) view.findViewById(R.id.leftTextView); + rightTextView = (TextView) view.findViewById(R.id.rightTextView); + } + @Override + public void onProgressUpdate(int progressPercentage, int activeTasks) { + if (freeVersionBanner.getVisibility() == View.VISIBLE) { + freeVersionBanner.setVisibility(View.GONE); + downloadProgressLayout.setVisibility(View.VISIBLE); + } + progressBar.setProgress(progressPercentage); + final String format = resources.getString(R.string.downloading_number_of_fiels); + String numberOfTasks = String.format(format, activeTasks); + leftTextView.setText(numberOfTasks); + rightTextView.setText(progressPercentage + "%"); } @Override - public String getName() { - return text; + public void onFinished() { + freeVersionBanner.setVisibility(View.VISIBLE); + downloadProgressLayout.setVisibility(View.GONE); } } } diff --git a/OsmAnd/src/net/osmand/plus/download/newimplementation/NewLocalIndexesFragment.java b/OsmAnd/src/net/osmand/plus/download/newimplementation/NewLocalIndexesFragment.java index 3ffdf5ae58..40f099481c 100644 --- a/OsmAnd/src/net/osmand/plus/download/newimplementation/NewLocalIndexesFragment.java +++ b/OsmAnd/src/net/osmand/plus/download/newimplementation/NewLocalIndexesFragment.java @@ -78,7 +78,7 @@ public class NewLocalIndexesFragment extends OsmAndListFragment { } private void initMemoryConsumedCard(View header) { - ProgressBar sizeProgress = (ProgressBar) header.findViewById(R.id.memory_progress); + ProgressBar sizeProgress = (ProgressBar) header.findViewById(R.id.progressBar); File dir = getMyApplication().getAppPath("").getParentFile(); String size = formatGb.format(new Object[]{0}); int percent = 0; @@ -92,9 +92,11 @@ public class NewLocalIndexesFragment extends OsmAndListFragment { sizeProgress.setProgress(percent); String text = getString(R.string.free, size); - TextView descriptionText = (TextView) header.findViewById(R.id.memory_size); + TextView descriptionText = (TextView) header.findViewById(R.id.rightTextView); descriptionText.setText(Html.fromHtml(text)); descriptionText.setMovementMethod(LinkMovementMethod.getInstance()); + + ((TextView) header.findViewById(R.id.leftTextView)).setText(R.string.device_memory); } @Override diff --git a/OsmAnd/src/net/osmand/plus/download/newimplementation/SubcategoriesFragment.java b/OsmAnd/src/net/osmand/plus/download/newimplementation/SubcategoriesFragment.java index cb36323689..fcdd1ca970 100644 --- a/OsmAnd/src/net/osmand/plus/download/newimplementation/SubcategoriesFragment.java +++ b/OsmAnd/src/net/osmand/plus/download/newimplementation/SubcategoriesFragment.java @@ -18,7 +18,6 @@ import android.widget.ProgressBar; import android.widget.TextView; import net.osmand.plus.OsmandApplication; -import net.osmand.plus.OsmandSettings; import net.osmand.plus.R; import net.osmand.plus.download.BaseDownloadActivity; import net.osmand.plus.download.DownloadActivity; @@ -43,14 +42,10 @@ public class SubcategoriesFragment extends Fragment { mAdapter.addAll(category.items); mAdapter.addAll(category.subcats); - View freeVersionBanner = inflater.inflate(R.layout.free_version_banner, listView, false); - final OsmandSettings settings = application.getSettings(); - DownloadsUiInitHelper.initFreeVersionBanner(freeVersionBanner, settings, getResources()); - listView.addHeaderView(freeVersionBanner); listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView parent, View view, int position, long id) { - final HasName item = mAdapter.getItem(position - 1); + final HasName item = mAdapter.getItem(position); if (item instanceof IndexItemCategoryWithSubcat) { ((MapsInCategoryFragment) getParentFragment()) .onCategorySelected((IndexItemCategoryWithSubcat) item); @@ -139,9 +134,6 @@ public class SubcategoriesFragment extends Fragment { public void onClick(View v) { ((BaseDownloadActivity) v.getContext()) .startDownload((IndexItem) v.getTag(R.id.index_item)); - progressBar.setVisibility(View.VISIBLE); - rightImageButton.setImageDrawable(getContextIcon(context, - R.drawable.ic_action_remove_dark)); } }); progressBar.setVisibility(View.GONE); diff --git a/OsmAnd/src/net/osmand/plus/osmedit/BasicDataFragment.java b/OsmAnd/src/net/osmand/plus/osmedit/BasicDataFragment.java index 6e1cf43af3..affa4f162c 100644 --- a/OsmAnd/src/net/osmand/plus/osmedit/BasicDataFragment.java +++ b/OsmAnd/src/net/osmand/plus/osmedit/BasicDataFragment.java @@ -255,10 +255,10 @@ public class BasicDataFragment extends Fragment daysTextView.setText(stringBuilder.toString()); TextView openingTextView = (TextView) view.findViewById(R.id.openingTextView); - openingTextView.setText(Algorithms.formatDuration(rule.getStartTime())); + openingTextView.setText(Algorithms.formatMinutesDuration(rule.getStartTime())); TextView closingTextView = (TextView) view.findViewById(R.id.closingTextView); - closingTextView.setText(Algorithms.formatDuration(rule.getEndTime())); + closingTextView.setText(Algorithms.formatMinutesDuration(rule.getEndTime())); timeContainer.setVisibility(View.VISIBLE); daysTextView.setOnClickListener(new View.OnClickListener() {