From 266e62c45943463e770685208718e017b5c8abb3 Mon Sep 17 00:00:00 2001 From: Victor Shcherb Date: Sun, 18 Oct 2015 17:13:21 +0200 Subject: [PATCH] Continue refactoring --- .../plus/download/BaseDownloadActivity.java | 4 +- .../plus/download/DownloadActivity.java | 36 ++-- .../osmand/plus/download/DownloadEntry.java | 31 --- .../plus/download/DownloadFileHelper.java | 8 +- .../plus/download/DownloadIndexesThread.java | 191 +++++++++--------- .../net/osmand/plus/download/IndexItem.java | 28 +++ .../items/ActiveDownloadsDialogFragment.java | 14 +- .../download/items/RegionItemsFragment.java | 7 - 8 files changed, 154 insertions(+), 165 deletions(-) delete mode 100644 OsmAnd/src/net/osmand/plus/download/DownloadEntry.java diff --git a/OsmAnd/src/net/osmand/plus/download/BaseDownloadActivity.java b/OsmAnd/src/net/osmand/plus/download/BaseDownloadActivity.java index e7aa7d3e43..a1f7ccef76 100644 --- a/OsmAnd/src/net/osmand/plus/download/BaseDownloadActivity.java +++ b/OsmAnd/src/net/osmand/plus/download/BaseDownloadActivity.java @@ -80,8 +80,6 @@ public class BaseDownloadActivity extends ActionBarProgressActivity { public void downloadedIndexes() { } - public void updateFragments() { - } public void downloadListUpdated() { } @@ -155,7 +153,7 @@ public class BaseDownloadActivity extends ActionBarProgressActivity { private void downloadFileCheck_Final_Run(IndexItem[] items) { downloadListIndexThread.runDownloadFiles(items); - updateFragments(); + updateProgress(false); } diff --git a/OsmAnd/src/net/osmand/plus/download/DownloadActivity.java b/OsmAnd/src/net/osmand/plus/download/DownloadActivity.java index fef7af4753..918f011029 100644 --- a/OsmAnd/src/net/osmand/plus/download/DownloadActivity.java +++ b/OsmAnd/src/net/osmand/plus/download/DownloadActivity.java @@ -119,7 +119,7 @@ public class DownloadActivity extends BaseDownloadActivity implements DialogDism protected void onResume() { super.onResume(); getMyApplication().getAppCustomization().resumeActivity(DownloadActivity.class, this); - updateFragments(); + updateProgress(false); } @@ -157,8 +157,22 @@ public class DownloadActivity extends BaseDownloadActivity implements DialogDism final int countedDownloads = downloadListIndexThread.getCountedDownloads(); visibleBanner.updateProgress(countedDownloads, basicProgressAsyncTask); } - if (!updateOnlyProgress) { - updateFragments(); + if(activeDownloads != null) { + if(updateOnlyProgress) { + activeDownloads.notifyDataSetChanged(); + } else { + activeDownloads.notifyDataSetInvalidated(); + } + } + // FIXME + //((DownloadActivity) getActivity()).updateDescriptionTextWithSize(getView()); + for (WeakReference ref : fragSet) { + Fragment f = ref.get(); + notifyUpdateDataSetChanged(f); + if(f instanceof RegionItemsFragment) { + Fragment innerFragment = ((RegionItemsFragment)f).getChildFragmentManager().findFragmentById(R.id.fragmentContainer); + notifyUpdateDataSetChanged(innerFragment); + } } } @@ -209,22 +223,6 @@ public class DownloadActivity extends BaseDownloadActivity implements DialogDism this.activeDownloads = activeDownloads; } - @Override - public void updateFragments() { - if(activeDownloads != null) { - activeDownloads.refresh(); - } - // FIXME - //((DownloadActivity) getActivity()).updateDescriptionTextWithSize(getView()); - for (WeakReference ref : fragSet) { - Fragment f = ref.get(); - notifyUpdateDataSetChanged(f); - if(f instanceof RegionItemsFragment) { - Fragment innerFragment = ((RegionItemsFragment)f).getChildFragmentManager().findFragmentById(R.id.fragmentContainer); - notifyUpdateDataSetChanged(innerFragment); - } - } - } private void notifyUpdateDataSetChanged(Fragment f) { diff --git a/OsmAnd/src/net/osmand/plus/download/DownloadEntry.java b/OsmAnd/src/net/osmand/plus/download/DownloadEntry.java deleted file mode 100644 index 23c68c66c3..0000000000 --- a/OsmAnd/src/net/osmand/plus/download/DownloadEntry.java +++ /dev/null @@ -1,31 +0,0 @@ -package net.osmand.plus.download; - -import java.io.File; - -public class DownloadEntry { - public long dateModified; - public double sizeMB; - - public File targetFile; - public boolean zipStream; - public boolean unzipFolder; - - public File fileToDownload; - - public String baseName; - public String urlToDownload; - public boolean isAsset; - public String assetName; - public DownloadActivityType type; - - - public DownloadEntry() { - } - - public DownloadEntry(String assetName, String fileName, long dateModified) { - this.dateModified = dateModified; - targetFile = new File(fileName); - this.assetName = assetName; - isAsset = true; - } -} \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/plus/download/DownloadFileHelper.java b/OsmAnd/src/net/osmand/plus/download/DownloadFileHelper.java index 5e75f77ff2..525ad45592 100644 --- a/OsmAnd/src/net/osmand/plus/download/DownloadFileHelper.java +++ b/OsmAnd/src/net/osmand/plus/download/DownloadFileHelper.java @@ -200,7 +200,7 @@ public class DownloadFileHelper { return ctx.getSettings().isWifiConnected(); } - public boolean downloadFile(DownloadEntry de, IProgress progress, + public boolean downloadFile(IndexItem.DownloadEntry de, IProgress progress, List toReIndex, DownloadFileShowWarning showWarningCallback, boolean forceWifi) throws InterruptedException { try { final List downloadInputStreams = new ArrayList(); @@ -235,7 +235,7 @@ public class DownloadFileHelper { } } - private void copyVoiceConfig(DownloadEntry de) { + private void copyVoiceConfig(IndexItem.DownloadEntry de) { File f = ctx.getAppPath("/voice/" + de.baseName + "/_config.p"); if (f.exists()) try { InputStream is = ctx.getAssets().open("voice/" + de.baseName + "/config.p"); @@ -252,7 +252,7 @@ public class DownloadFileHelper { } } - private void unzipFile(DownloadEntry de, IProgress progress, List is) throws IOException { + private void unzipFile(IndexItem.DownloadEntry de, IProgress progress, List is) throws IOException { CountingMultiInputStream fin = new CountingMultiInputStream(is); int len = (int) fin.available(); int mb = (int) (len / (1024f*1024f)); @@ -308,7 +308,7 @@ public class DownloadFileHelper { fin.close(); } - private void copyFile(DownloadEntry de, IProgress progress, + private void copyFile(IndexItem.DownloadEntry de, IProgress progress, CountingMultiInputStream countIS, int length, InputStream toRead, File targetFile) throws IOException { targetFile.getParentFile().mkdirs(); diff --git a/OsmAnd/src/net/osmand/plus/download/DownloadIndexesThread.java b/OsmAnd/src/net/osmand/plus/download/DownloadIndexesThread.java index c33510875c..1dc12ec2c5 100644 --- a/OsmAnd/src/net/osmand/plus/download/DownloadIndexesThread.java +++ b/OsmAnd/src/net/osmand/plus/download/DownloadIndexesThread.java @@ -65,8 +65,8 @@ public class DownloadIndexesThread { private ConcurrentLinkedQueue indexItemDownloading = new ConcurrentLinkedQueue(); private IndexItem currentDownloadingItem = null; private int currentDownloadingItemProgress = 0; - - + + private DownloadIndexes indexes = new DownloadIndexes(); public static class DownloadIndexes { Map> resourcesByRegions = new HashMap<>(); @@ -77,8 +77,8 @@ public class DownloadIndexesThread { private Map indexFileNames = new LinkedHashMap<>(); private Map indexActivatedFileNames = new LinkedHashMap<>(); private List itemsToUpdate = new ArrayList<>(); - - + + public DownloadIndexesThread(Context ctx) { this.ctx = ctx; @@ -89,10 +89,44 @@ public class DownloadIndexesThread { } + /// UI notifications methods public void setUiActivity(BaseDownloadActivity uiActivity) { this.uiActivity = uiActivity; } + + + @UiThread + private void updateProgressUI(boolean onlyProgress) { + if (uiActivity != null) { + uiActivity.updateProgress(onlyProgress); + } + } + + @UiThread + private void onCategorizationFinished() { + if (uiActivity != null) { + uiActivity.onCategorizationFinished(); + } + } + @UiThread + private void updateDownloadList() { + if (uiActivity != null) { + uiActivity.updateDownloadList(); + } + } + + @UiThread + private void notifyFilesToUpdateChanged() { + List filtered = getCachedIndexFiles(); + if (filtered != null) { + updateDownloadList(); + } + } + + + // PUBLIC API + public List getCurrentDownloadingItems() { List res = new ArrayList(); IndexItem ii = currentDownloadingItem; @@ -102,7 +136,7 @@ public class DownloadIndexesThread { res.addAll(indexItemDownloading); return res; } - + public boolean isDownloading(IndexItem item) { if(item == currentDownloadingItem) { return true; @@ -114,7 +148,7 @@ public class DownloadIndexesThread { } return false; } - + public int getCountedDownloads() { int i = 0; if(currentDownloadingItem != null && DownloadActivityType.isCountedInDownloads(currentDownloadingItem)) { @@ -127,18 +161,18 @@ public class DownloadIndexesThread { } return i; } - - + + // FIXME public List getCachedIndexFiles() { return indexes.indexFiles != null ? indexes.indexFiles.getIndexFiles() : null; } - + // FIXME public Map getIndexFileNames() { return indexFileNames; } - + // FIXME public Map getIndexActivatedFileNames() { return indexActivatedFileNames; @@ -185,8 +219,8 @@ public class DownloadIndexesThread { } private boolean prepareData(List resources, - Map> resourcesByRegions, - List voiceRecItems, List voiceTTSItems) { + Map> resourcesByRegions, + List voiceRecItems, List voiceTTSItems) { List resourcesInRepository; if (resources != null) { resourcesInRepository = resources; @@ -227,8 +261,8 @@ public class DownloadIndexesThread { // FIXME argument list private void processRegion(List resourcesInRepository, Map> resourcesByRegions, - List voiceRecItems, List voiceTTSItems, - boolean processVoiceFiles, WorldRegion region) { + List voiceRecItems, List voiceTTSItems, + boolean processVoiceFiles, WorldRegion region) { String downloadsIdPrefix = region.getDownloadsIdPrefix(); Map regionResources = new HashMap<>(); Set typesSet = new TreeSet<>(new Comparator() { @@ -275,7 +309,7 @@ public class DownloadIndexesThread { region.setResourceTypes(typesSet); resourcesByRegions.put(region, regionResources); } - + private boolean checkRunning() { if (getCurrentRunningTask() != null) { @@ -305,13 +339,13 @@ public class DownloadIndexesThread { execute(new DownloadIndexesAsyncTask(ctx)); } } - + public void cancelDownload(IndexItem item) { if(currentDownloadingItem == item) { downloadFileHelper.setInterruptDownloading(true);; + } else { + indexItemDownloading.remove(item); } - // TODO Auto-generated method stub - } private

void execute(BasicProgressAsyncTask task, P... indexItems) { @@ -336,15 +370,6 @@ public class DownloadIndexesThread { } } - @UiThread - private void notifyFilesToUpdateChanged() { - List filtered = getCachedIndexFiles(); - if (filtered != null) { - if (uiActivity != null) { - uiActivity.updateDownloadList(); - } - } - } public boolean checkIfItemOutdated(IndexItem item) { boolean outdated = false; @@ -385,8 +410,6 @@ public class DownloadIndexesThread { } else { oldItemSize = app.getAppPath(item.getTargetFileName()).length(); } - - if (itemSize != oldItemSize) { outdated = true; } @@ -395,47 +418,14 @@ public class DownloadIndexesThread { return outdated; } - private void updateFilesToUpdate() { - List stillUpdate = new ArrayList(); - for (IndexItem item : itemsToUpdate) { - String sfName = item.getTargetFileName(); - java.text.DateFormat format = app.getResourceManager().getDateFormat(); - String date = item.getDate(format); - String indexactivateddate = indexActivatedFileNames.get(sfName); - String indexfilesdate = indexFileNames.get(sfName); - if (date != null && - !date.equals(indexactivateddate) && - !date.equals(indexfilesdate) && - indexActivatedFileNames.containsKey(sfName)) { - stillUpdate.add(item); - } - } - itemsToUpdate = stillUpdate; - if (uiActivity != null) { - uiActivity.updateDownloadList(); - } - } - - - public boolean isDownloadRunning() { - for (int i = 0; i < currentRunningTask.size(); i++) { - if (currentRunningTask.get(i) instanceof DownloadIndexesAsyncTask) { - return true; - - } - } - return false; - } - public IndexItem getCurrentDownloadingItem() { return currentDownloadingItem; } - + public int getCurrentDownloadingItemProgress() { return currentDownloadingItemProgress; } - // FIXME deprecated public BasicProgressAsyncTask getCurrentRunningTask() { for (int i = 0; i < currentRunningTask.size(); ) { if (currentRunningTask.get(i).getStatus() == Status.FINISHED) { @@ -459,7 +449,7 @@ public class DownloadIndexesThread { } return asz; } - + public Map listWithAlternatives(final java.text.DateFormat dateFormat, File file, final String ext, final Map files) { @@ -524,7 +514,6 @@ public class DownloadIndexesThread { protected void onPostExecute(DownloadIndexes result) { indexes = result; - notifyFilesToUpdateChanged(); if (result.indexFiles != null) { if (result.indexFiles.isIncreasedMapVersion()) { showWarnDialog(); @@ -533,10 +522,9 @@ public class DownloadIndexesThread { AccessibleToast.makeText(ctx, R.string.list_index_files_was_not_loaded, Toast.LENGTH_LONG).show(); } currentRunningTask.remove(this); - if (uiActivity != null) { - uiActivity.updateProgress(false); - uiActivity.onCategorizationFinished(); - } + notifyFilesToUpdateChanged(); + updateProgressUI(false); + onCategorizationFinished(); } private void showWarnDialog() { @@ -564,18 +552,34 @@ public class DownloadIndexesThread { @Override protected void updateProgress(boolean updateOnlyProgress, Void tag) { - if (uiActivity != null) { - uiActivity.updateProgress(updateOnlyProgress); - } + updateProgressUI(true); } } - - + + + //FIXME + private void updateFilesToUpdate() { + List stillUpdate = new ArrayList(); + for (IndexItem item : itemsToUpdate) { + String sfName = item.getTargetFileName(); + java.text.DateFormat format = app.getResourceManager().getDateFormat(); + String date = item.getDate(format); + String indexactivateddate = indexActivatedFileNames.get(sfName); + String indexfilesdate = indexFileNames.get(sfName); + if (date != null && + !date.equals(indexactivateddate) && + !date.equals(indexfilesdate) && + indexActivatedFileNames.containsKey(sfName)) { + stillUpdate.add(item); + } + } + itemsToUpdate = stillUpdate; + } public class DownloadIndexesAsyncTask extends BasicProgressAsyncTask implements DownloadFileShowWarning { private OsmandPreference downloads; - + public DownloadIndexesAsyncTask(Context ctx) { super(ctx); @@ -594,17 +598,15 @@ public class DownloadIndexesThread { protected void onProgressUpdate(Object... values) { for (Object o : values) { if (o instanceof IndexItem) { - if (uiActivity != null) { - uiActivity.updateFragments(); - IndexItem item = (IndexItem) o; - String name = item.getBasename(); - long count = dbHelper.getCount(name, DatabaseHelper.DOWNLOAD_ENTRY) + 1; - DatabaseHelper.HistoryDownloadEntry entry = new DatabaseHelper.HistoryDownloadEntry(name, count); - if (count == 1) { - dbHelper.add(entry, DatabaseHelper.DOWNLOAD_ENTRY); - } else { - dbHelper.update(entry, DatabaseHelper.DOWNLOAD_ENTRY); - } + updateProgressUI(false); + IndexItem item = (IndexItem) o; + String name = item.getBasename(); + long count = dbHelper.getCount(name, DatabaseHelper.DOWNLOAD_ENTRY) + 1; + DatabaseHelper.HistoryDownloadEntry entry = new DatabaseHelper.HistoryDownloadEntry(name, count); + if (count == 1) { + dbHelper.add(entry, DatabaseHelper.DOWNLOAD_ENTRY); + } else { + dbHelper.update(entry, DatabaseHelper.DOWNLOAD_ENTRY); } } else if (o instanceof String) { String message = (String) o; @@ -613,11 +615,8 @@ public class DownloadIndexesThread { !message.equals(uiActivity.getString(R.string.shared_string_download_successful))) { AccessibleToast.makeText(ctx, message, Toast.LENGTH_LONG).show(); } - } else { - if (uiActivity != null) { - uiActivity.updateProgress(true); - } } + updateProgressUI(false); } } super.onProgressUpdate(values); @@ -647,15 +646,15 @@ public class DownloadIndexesThread { if (mainView != null) { mainView.setKeepScreenOn(false); } - uiActivity.downloadedIndexes(); } currentRunningTask.remove(this); - if (uiActivity != null) { - uiActivity.updateProgress(false); - } + notifyFilesToUpdateChanged(); updateFilesToUpdate(); + updateProgressUI(false); } + + @Override protected String doInBackground(IndexItem... filesToDownload) { @@ -777,7 +776,7 @@ public class DownloadIndexesThread { public boolean downloadFile(IndexItem item, List filesToReindex, boolean forceWifi) throws InterruptedException { downloadFileHelper.setInterruptDownloading(false); - DownloadEntry de = item.createDownloadEntry(app); + IndexItem.DownloadEntry de = item.createDownloadEntry(app); boolean res = false; if(de == null) { return res; @@ -804,7 +803,7 @@ public class DownloadIndexesThread { @Override protected void updateProgress(boolean updateOnlyProgress, IndexItem tag) { currentDownloadingItemProgress = getProgressPercentage(); - uiActivity.updateProgress(true); + updateProgressUI(true); } } diff --git a/OsmAnd/src/net/osmand/plus/download/IndexItem.java b/OsmAnd/src/net/osmand/plus/download/IndexItem.java index 1a0d4a4efa..a77c35e657 100644 --- a/OsmAnd/src/net/osmand/plus/download/IndexItem.java +++ b/OsmAnd/src/net/osmand/plus/download/IndexItem.java @@ -161,6 +161,34 @@ public class IndexItem implements Comparable/*, Parcelable*/ { public String getDate(java.text.DateFormat format) { return format.format(new Date(timestamp)); } + + public static class DownloadEntry { + public long dateModified; + public double sizeMB; + + public File targetFile; + public boolean zipStream; + public boolean unzipFolder; + + public File fileToDownload; + + public String baseName; + public String urlToDownload; + public boolean isAsset; + public String assetName; + public DownloadActivityType type; + + + public DownloadEntry() { + } + + public DownloadEntry(String assetName, String fileName, long dateModified) { + this.dateModified = dateModified; + targetFile = new File(fileName); + this.assetName = assetName; + isAsset = true; + } + } // @Override diff --git a/OsmAnd/src/net/osmand/plus/download/items/ActiveDownloadsDialogFragment.java b/OsmAnd/src/net/osmand/plus/download/items/ActiveDownloadsDialogFragment.java index 35319f7dec..245d05ba96 100644 --- a/OsmAnd/src/net/osmand/plus/download/items/ActiveDownloadsDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/download/items/ActiveDownloadsDialogFragment.java @@ -34,8 +34,12 @@ public class ActiveDownloadsDialogFragment extends DialogFragment { return builder.create(); } - public void refresh() { - adapter.updateData(); + public void notifyDataSetInvalidated() { + adapter.refreshAllData(); + } + + public void notifyDataSetChanged() { + adapter.notifyDataSetChanged(); } public void onDetach() { @@ -59,10 +63,10 @@ public class ActiveDownloadsDialogFragment extends DialogFragment { deleteDrawable = context.getMyApplication().getIconsCache() .getPaintedContentIcon(R.drawable.ic_action_remove_dark, context.getResources().getColor(R.color.dash_search_icon_dark)); - updateData(); + refreshAllData(); } - public void updateData() { + public void refreshAllData() { clear(); addAll(context.getDownloadThread().getCurrentDownloadingItems()); } @@ -127,7 +131,7 @@ public class ActiveDownloadsDialogFragment extends DialogFragment { @Override public void onClick(View v) { context.getDownloadThread().cancelDownload(item); - adapter.updateData(); + adapter.refreshAllData(); } }); progressBar.setIndeterminate(isIndeterminate); diff --git a/OsmAnd/src/net/osmand/plus/download/items/RegionItemsFragment.java b/OsmAnd/src/net/osmand/plus/download/items/RegionItemsFragment.java index 91a904d0de..410a66c15c 100644 --- a/OsmAnd/src/net/osmand/plus/download/items/RegionItemsFragment.java +++ b/OsmAnd/src/net/osmand/plus/download/items/RegionItemsFragment.java @@ -6,24 +6,17 @@ import java.util.LinkedList; import java.util.List; import java.util.Map; -import net.osmand.PlatformUtil; import net.osmand.plus.OsmandApplication; import net.osmand.plus.R; import net.osmand.plus.WorldRegion; import net.osmand.plus.activities.OsmandBaseExpandableListAdapter; import net.osmand.plus.activities.OsmandExpandableListFragment; -import net.osmand.plus.base.BasicProgressAsyncTask; import net.osmand.plus.download.DownloadActivity; import net.osmand.plus.download.DownloadActivityType; -import net.osmand.plus.download.DownloadEntry; import net.osmand.plus.download.IndexItem; - -import org.apache.commons.logging.Log; - import android.app.Dialog; import android.content.DialogInterface; import android.content.res.Resources; -import android.os.AsyncTask; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.v4.app.DialogFragment;