From ee65717d7b538f2368c299cbc297d4879ef4f91c Mon Sep 17 00:00:00 2001 From: GaidamakUA Date: Mon, 12 Oct 2015 10:05:53 +0300 Subject: [PATCH] Download dialog --- OsmAnd/src/net/osmand/plus/WorldRegion.java | 2 - .../plus/base/BasicProgressAsyncTask.java | 8 +++- .../plus/download/BaseDownloadActivity.java | 2 +- .../plus/download/DownloadActivity.java | 41 +++++++++++++++---- .../plus/download/DownloadIndexFragment.java | 4 +- .../plus/download/DownloadIndexesThread.java | 35 ++++++++-------- 6 files changed, 61 insertions(+), 31 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/WorldRegion.java b/OsmAnd/src/net/osmand/plus/WorldRegion.java index cb228cb831..be9ed9139e 100644 --- a/OsmAnd/src/net/osmand/plus/WorldRegion.java +++ b/OsmAnd/src/net/osmand/plus/WorldRegion.java @@ -89,7 +89,6 @@ public class WorldRegion { } public void processNewMapState(MapState mapState) { - LOG.debug("old state=" + this.mapState); switch (this.mapState) { case NOT_DOWNLOADED: this.mapState = mapState; @@ -101,7 +100,6 @@ public class WorldRegion { case OUTDATED: break; } - LOG.debug("new state=" + this.mapState); } @Override diff --git a/OsmAnd/src/net/osmand/plus/base/BasicProgressAsyncTask.java b/OsmAnd/src/net/osmand/plus/base/BasicProgressAsyncTask.java index c284a62d61..ccf8ad2faf 100644 --- a/OsmAnd/src/net/osmand/plus/base/BasicProgressAsyncTask.java +++ b/OsmAnd/src/net/osmand/plus/base/BasicProgressAsyncTask.java @@ -16,6 +16,7 @@ public abstract class BasicProgressAsyncTask extends A protected String message = ""; //$NON-NLS-1$ protected Context ctx; protected boolean interrupted = false; + protected Object tag; private Handler uiHandler; public BasicProgressAsyncTask(Context ctx) { @@ -45,7 +46,7 @@ public abstract class BasicProgressAsyncTask extends A updProgress(false); } - protected abstract void updateProgress(boolean updateOnlyProgress); + protected abstract void updateProgress(boolean updateOnlyProgress, Object tag); @Override public void startWork(int work) { @@ -75,7 +76,7 @@ public abstract class BasicProgressAsyncTask extends A Message msg = Message.obtain(uiHandler, new Runnable() { @Override public void run() { - updateProgress(updateOnlyProgress); + updateProgress(updateOnlyProgress, tag); } }); msg.what = OsmAndConstants.UI_HANDLER_PROGRESS + 2; @@ -122,4 +123,7 @@ public abstract class BasicProgressAsyncTask extends A return interrupted; } + protected void setTag(Object tag) { + this.tag = tag; + } } \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/plus/download/BaseDownloadActivity.java b/OsmAnd/src/net/osmand/plus/download/BaseDownloadActivity.java index 6b6e466eb8..acd153fd14 100644 --- a/OsmAnd/src/net/osmand/plus/download/BaseDownloadActivity.java +++ b/OsmAnd/src/net/osmand/plus/download/BaseDownloadActivity.java @@ -79,7 +79,7 @@ public class BaseDownloadActivity extends ActionBarProgressActivity { } - public void updateProgress(boolean updateOnlyProgress) { + public void updateProgress(boolean updateOnlyProgress, Object tag) { } diff --git a/OsmAnd/src/net/osmand/plus/download/DownloadActivity.java b/OsmAnd/src/net/osmand/plus/download/DownloadActivity.java index 1fa52e964d..0f6ba5c679 100644 --- a/OsmAnd/src/net/osmand/plus/download/DownloadActivity.java +++ b/OsmAnd/src/net/osmand/plus/download/DownloadActivity.java @@ -244,7 +244,7 @@ public class DownloadActivity extends BaseDownloadActivity implements RegionDial } @Override - public void updateProgress(boolean updateOnlyProgress) { + public void updateProgress(boolean updateOnlyProgress, Object tag) { BasicProgressAsyncTask basicProgressAsyncTask = DownloadActivity.downloadListIndexThread.getCurrentRunningTask(); if (visibleBanner != null) { @@ -252,7 +252,7 @@ public class DownloadActivity extends BaseDownloadActivity implements RegionDial visibleBanner.updateProgress(countedDownloads, basicProgressAsyncTask); } if (progressAdapter != null) { - progressAdapter.setProgress(basicProgressAsyncTask.getProgressPercentage()); + progressAdapter.setProgress(basicProgressAsyncTask, tag); } if (!updateOnlyProgress) { updateDownloadButton(); @@ -581,12 +581,12 @@ public class DownloadActivity extends BaseDownloadActivity implements RegionDial public void registerFreeVersionBanner(View view) { visibleBanner = new BannerAndDownloadFreeVersion(view, this); - updateProgress(true); + updateProgress(true, null); } public void registerUpdateListener(ActiveDownloadsDialogFragment.DownloadEntryAdapter adapter) { progressAdapter = adapter; - updateProgress(true); + updateProgress(true, null); } public void showDialog(FragmentActivity activity, DialogFragment fragment) { @@ -773,12 +773,18 @@ public class DownloadActivity extends BaseDownloadActivity implements RegionDial for (List list : vs) { downloadEntries.addAll(list); } - builder.setAdapter(new DownloadEntryAdapter(getActivity(), downloadEntries), null); + final DownloadEntryAdapter adapter = new DownloadEntryAdapter(getActivity(), downloadEntries); + builder.setAdapter(adapter, null); + ((DownloadActivity) getActivity()).registerUpdateListener(adapter); return builder.create(); } private static class DownloadEntryAdapter extends ArrayAdapter { private final Drawable deleteDrawable; + private int itemInProgressPosition = -1; + private int progress = -1; + private final Set downloadedItems = new HashSet<>(); + private boolean isFinished; public DownloadEntryAdapter(Context context, List objects) { super(context, R.layout.two_line_with_images_list_item, objects); @@ -798,11 +804,32 @@ public class DownloadActivity extends BaseDownloadActivity implements RegionDial } DownloadEntryViewHolder viewHolder = (DownloadEntryViewHolder) convertView.getTag(); viewHolder.bindDownloadEntry(getItem(position)); + if (position == itemInProgressPosition) { + viewHolder.progressBar.setIndeterminate(false); + viewHolder.progressBar.setProgress(progress); + } else if (isFinished || downloadedItems.contains(position)) { + viewHolder.progressBar.setIndeterminate(false); + viewHolder.progressBar.setProgress(viewHolder.progressBar.getMax()); + } return convertView; } - public void setProgress(int percent) { - // TODO: 10/9/15 implement + public void setProgress(BasicProgressAsyncTask task, Object tag) { + isFinished = task == null + || task.getStatus() == AsyncTask.Status.FINISHED; + itemInProgressPosition = -1; + progress = -1; + if (isFinished) return; + if (tag instanceof DownloadEntry) { + progress = task.getProgressPercentage(); + for (int i = 0; i < getCount(); i++) { + if (getItem(i).equals(tag)) { + itemInProgressPosition = i; + downloadedItems.add(i); + } + } + } + notifyDataSetChanged(); } } diff --git a/OsmAnd/src/net/osmand/plus/download/DownloadIndexFragment.java b/OsmAnd/src/net/osmand/plus/download/DownloadIndexFragment.java index dec73d6306..d8273293b9 100644 --- a/OsmAnd/src/net/osmand/plus/download/DownloadIndexFragment.java +++ b/OsmAnd/src/net/osmand/plus/download/DownloadIndexFragment.java @@ -101,7 +101,7 @@ public class DownloadIndexFragment extends OsmandExpandableListFragment { @Override public void onResume() { super.onResume(); - getDownloadActivity().updateProgress(false); + getDownloadActivity().updateProgress(false, null); BasicProgressAsyncTask t = DownloadActivity.downloadListIndexThread.getCurrentRunningTask(); if(t instanceof DownloadIndexesThread.DownloadIndexesAsyncTask) { View mainView = getView().findViewById(R.id.MainLayout); @@ -300,7 +300,7 @@ public class DownloadIndexFragment extends OsmandExpandableListFragment { public View findViewById(int id){ return getView().findViewById(id);} public void updateProgress(boolean b) { - getDownloadActivity().updateProgress(b); + getDownloadActivity().updateProgress(b, null); } public void categorizationFinished(List filtered, List cats) { diff --git a/OsmAnd/src/net/osmand/plus/download/DownloadIndexesThread.java b/OsmAnd/src/net/osmand/plus/download/DownloadIndexesThread.java index 0a4729fc10..6cbdf2f177 100644 --- a/OsmAnd/src/net/osmand/plus/download/DownloadIndexesThread.java +++ b/OsmAnd/src/net/osmand/plus/download/DownloadIndexesThread.java @@ -61,13 +61,13 @@ public class DownloadIndexesThread { private Set currentDownloads = new HashSet(); private final Context ctx; private OsmandApplication app; - private final static Log log = PlatformUtil.getLog(DownloadIndexesThread.class); + private final static Log LOG = PlatformUtil.getLog(DownloadIndexesThread.class); private DownloadFileHelper downloadFileHelper; private List> currentRunningTask = Collections.synchronizedList(new ArrayList>()); - private Map indexFileNames = new LinkedHashMap(); - private Map indexActivatedFileNames = new LinkedHashMap(); + private Map indexFileNames = new LinkedHashMap<>(); + private Map indexActivatedFileNames = new LinkedHashMap<>(); private java.text.DateFormat dateFormat; - private List itemsToUpdate = new ArrayList(); + private List itemsToUpdate = new ArrayList<>(); private Map> resourcesByRegions = new HashMap<>(); private List voiceRecItems = new LinkedList<>(); @@ -349,7 +349,7 @@ public class DownloadIndexesThread { } currentRunningTask.remove(this); if (uiActivity != null) { - uiActivity.updateProgress(false); + uiActivity.updateProgress(false, tag); } updateFilesToUpdate(); } @@ -401,6 +401,7 @@ public class DownloadIndexesThread { + ""); break downloadCycle; } + setTag(entry); boolean result = downloadFile(entry, filesToReindex, forceWifi); success = result || success; if (result) { @@ -431,7 +432,7 @@ public class DownloadIndexesThread { updateLoadedFiles(); return warn; } catch (InterruptedException e) { - log.info("Download Interrupted"); + LOG.info("Download Interrupted"); // do not dismiss dialog } return null; @@ -492,12 +493,12 @@ public class DownloadIndexesThread { ResourceManager.copyAssets(ctx.getAssets(), de.assetName, de.targetFile); boolean changedDate = de.targetFile.setLastModified(de.dateModified); if (!changedDate) { - log.error("Set last timestamp is not supported"); + LOG.error("Set last timestamp is not supported"); } res = true; } } catch (IOException e) { - log.error("Copy exception", e); + LOG.error("Copy exception", e); } } else { res = downloadFileHelper.downloadFile(de, this, filesToReindex, this, forceWifi); @@ -506,9 +507,9 @@ public class DownloadIndexesThread { } @Override - protected void updateProgress(boolean updateOnlyProgress) { + protected void updateProgress(boolean updateOnlyProgress, Object tag) { if (uiActivity != null) { - uiActivity.updateProgress(updateOnlyProgress); + uiActivity.updateProgress(updateOnlyProgress, tag); } } } @@ -573,7 +574,7 @@ public class DownloadIndexesThread { } currentRunningTask.remove(this); if (uiActivity != null) { - uiActivity.updateProgress(false); + uiActivity.updateProgress(false, tag); runCategorization(uiActivity.getDownloadType()); uiActivity.onCategorizationFinished(); } @@ -603,9 +604,9 @@ public class DownloadIndexesThread { } @Override - protected void updateProgress(boolean updateOnlyProgress) { + protected void updateProgress(boolean updateOnlyProgress, Object tag) { if (uiActivity != null) { - uiActivity.updateProgress(updateOnlyProgress); + uiActivity.updateProgress(updateOnlyProgress, tag); } } @@ -644,7 +645,7 @@ public class DownloadIndexesThread { currentRunningTask.add(this); this.message = ctx.getString(R.string.downloading_list_indexes); if (uiActivity != null) { - uiActivity.updateProgress(false); + uiActivity.updateProgress(false, tag); } } @@ -676,14 +677,14 @@ public class DownloadIndexesThread { currentRunningTask.remove(this); if (uiActivity != null) { uiActivity.categorizationFinished(filtered, cats); - uiActivity.updateProgress(false); + uiActivity.updateProgress(false, tag); } } @Override - protected void updateProgress(boolean updateOnlyProgress) { + protected void updateProgress(boolean updateOnlyProgress, Object tag) { if (uiActivity != null) { - uiActivity.updateProgress(updateOnlyProgress); + uiActivity.updateProgress(updateOnlyProgress, tag); } }