From 266e62c45943463e770685208718e017b5c8abb3 Mon Sep 17 00:00:00 2001 From: Victor Shcherb Date: Sun, 18 Oct 2015 17:13:21 +0200 Subject: [PATCH 01/22] 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; From 71eef922ae4c495dbbfe55b0cfcf7e6827188a7a Mon Sep 17 00:00:00 2001 From: Victor Shcherb Date: Sun, 18 Oct 2015 22:14:09 +0200 Subject: [PATCH 02/22] Continue refactoring --- OsmAnd/src/net/osmand/plus/WorldRegion.java | 35 +- .../ActiveDownloadsDialogFragment.java | 8 +- .../plus/download/BaseDownloadActivity.java | 59 +-- .../plus/download/DownloadActivity.java | 99 ++-- .../plus/download/DownloadActivityType.java | 55 +-- .../plus/download/DownloadIndexesThread.java | 394 ++------------- .../download/DownloadOsmandIndexesHelper.java | 82 +++- .../plus/download/DownloadResourceGroup.java | 91 ++++ .../plus/download/DownloadResources.java | 464 ++++++++++++++++++ .../osmand/plus/download/IndexFileList.java | 90 ---- .../plus/download/IndexItemCategory.java | 130 ----- .../plus/download/items/ItemsListBuilder.java | 286 ----------- .../download/items/RegionItemsFragment.java | 2 - 13 files changed, 750 insertions(+), 1045 deletions(-) rename OsmAnd/src/net/osmand/plus/download/{items => }/ActiveDownloadsDialogFragment.java (95%) create mode 100644 OsmAnd/src/net/osmand/plus/download/DownloadResourceGroup.java create mode 100644 OsmAnd/src/net/osmand/plus/download/DownloadResources.java delete mode 100644 OsmAnd/src/net/osmand/plus/download/IndexFileList.java delete mode 100644 OsmAnd/src/net/osmand/plus/download/IndexItemCategory.java delete mode 100644 OsmAnd/src/net/osmand/plus/download/items/ItemsListBuilder.java diff --git a/OsmAnd/src/net/osmand/plus/WorldRegion.java b/OsmAnd/src/net/osmand/plus/WorldRegion.java index 1185169b21..888150d54b 100644 --- a/OsmAnd/src/net/osmand/plus/WorldRegion.java +++ b/OsmAnd/src/net/osmand/plus/WorldRegion.java @@ -39,10 +39,7 @@ public class WorldRegion { private List subregions; private List flattenedSubregions; - private boolean purchased; - private boolean isInPurchasedArea; - private MapState mapState = MapState.NOT_DOWNLOADED; public String getRegionId() { return regionId; @@ -76,32 +73,6 @@ public class WorldRegion { return flattenedSubregions; } - public boolean isPurchased() { - return purchased; - } - - public boolean isInPurchasedArea() { - return isInPurchasedArea; - } - - public MapState getMapState() { - return mapState; - } - - public void processNewMapState(MapState mapState) { - switch (this.mapState) { - case NOT_DOWNLOADED: - this.mapState = mapState; - break; - case DOWNLOADED: - if (mapState == MapState.OUTDATED) - this.mapState = mapState; - break; - case OUTDATED: - break; - } - } - @Override public boolean equals(Object o) { if (this == o) return true; @@ -315,9 +286,5 @@ public class WorldRegion { return null; } - public enum MapState { - NOT_DOWNLOADED, - DOWNLOADED, - OUTDATED - } + } \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/plus/download/items/ActiveDownloadsDialogFragment.java b/OsmAnd/src/net/osmand/plus/download/ActiveDownloadsDialogFragment.java similarity index 95% rename from OsmAnd/src/net/osmand/plus/download/items/ActiveDownloadsDialogFragment.java rename to OsmAnd/src/net/osmand/plus/download/ActiveDownloadsDialogFragment.java index 245d05ba96..46bd280d65 100644 --- a/OsmAnd/src/net/osmand/plus/download/items/ActiveDownloadsDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/download/ActiveDownloadsDialogFragment.java @@ -1,13 +1,9 @@ -package net.osmand.plus.download.items; +package net.osmand.plus.download; import java.util.ArrayList; -import java.util.HashSet; -import java.util.Set; import net.osmand.plus.R; -import net.osmand.plus.download.BaseDownloadActivity; -import net.osmand.plus.download.DownloadActivity; -import net.osmand.plus.download.IndexItem; +import net.osmand.plus.download.items.TwoLineWithImagesViewHolder; import android.app.AlertDialog; import android.app.Dialog; import android.graphics.drawable.Drawable; diff --git a/OsmAnd/src/net/osmand/plus/download/BaseDownloadActivity.java b/OsmAnd/src/net/osmand/plus/download/BaseDownloadActivity.java index a1f7ccef76..e3f52689e7 100644 --- a/OsmAnd/src/net/osmand/plus/download/BaseDownloadActivity.java +++ b/OsmAnd/src/net/osmand/plus/download/BaseDownloadActivity.java @@ -3,8 +3,6 @@ package net.osmand.plus.download; import java.lang.ref.WeakReference; import java.text.MessageFormat; import java.util.HashSet; -import java.util.LinkedList; -import java.util.List; import java.util.Set; import net.osmand.access.AccessibleToast; @@ -12,9 +10,7 @@ import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandSettings; import net.osmand.plus.R; import net.osmand.plus.Version; -import net.osmand.plus.WorldRegion; import net.osmand.plus.activities.ActionBarProgressActivity; -import net.osmand.plus.download.items.ItemsListBuilder; import android.app.AlertDialog; import android.app.Dialog; import android.content.ActivityNotFoundException; @@ -30,7 +26,7 @@ import android.widget.Toast; public class BaseDownloadActivity extends ActionBarProgressActivity { protected OsmandSettings settings; - public static DownloadIndexesThread downloadListIndexThread; + private static DownloadIndexesThread downloadListIndexThread; protected Set> fragSet = new HashSet<>(); public static final int MAXIMUM_AVAILABLE_FREE_DOWNLOADS = 10; @@ -65,62 +61,23 @@ public class BaseDownloadActivity extends ActionBarProgressActivity { } - // FIXME - public void onCategorizationFinished() { + @UiThread + public void downloadInProgress() { } @UiThread - public void updateDownloadList() { + public void downloadHasFinished() { } - + @UiThread - public void updateProgress(boolean updateOnlyProgress) { + public void newDownloadIndexes() { } - - public void downloadedIndexes() { - } - - - public void downloadListUpdated() { - } - /////// FIXME - + public OsmandApplication getMyApplication() { return (OsmandApplication) getApplication(); } - public ItemsListBuilder getItemsBuilder() { - return getItemsBuilder("", false); - } - - public ItemsListBuilder getVoicePromptsBuilder() { - return getItemsBuilder("", true); - } - - public ItemsListBuilder getItemsBuilder(String regionId, boolean voicePromptsOnly) { - if (downloadListIndexThread.getResourcesByRegions().size() > 0) { - ItemsListBuilder builder = new ItemsListBuilder(getMyApplication(), regionId, downloadListIndexThread.getResourcesByRegions(), - downloadListIndexThread.getVoiceRecItems(), downloadListIndexThread.getVoiceTTSItems()); - if (!voicePromptsOnly) { - return builder.build(); - } else { - return builder; - } - } else { - return null; - } - } - - public List getIndexItemsByRegion(WorldRegion region) { - if (downloadListIndexThread.getResourcesByRegions().size() > 0) { - return new LinkedList<>(downloadListIndexThread.getResourcesByRegions().get(region).values()); - } else { - return new LinkedList<>(); - } - } - - public void downloadFilesCheck_3_ValidateSpace(final IndexItem... items) { long szLong = 0; int i = 0; @@ -153,7 +110,7 @@ public class BaseDownloadActivity extends ActionBarProgressActivity { private void downloadFileCheck_Final_Run(IndexItem[] items) { downloadListIndexThread.runDownloadFiles(items); - updateProgress(false); + downloadInProgress(); } diff --git a/OsmAnd/src/net/osmand/plus/download/DownloadActivity.java b/OsmAnd/src/net/osmand/plus/download/DownloadActivity.java index 918f011029..83514c15c0 100644 --- a/OsmAnd/src/net/osmand/plus/download/DownloadActivity.java +++ b/OsmAnd/src/net/osmand/plus/download/DownloadActivity.java @@ -20,7 +20,6 @@ import net.osmand.plus.activities.OsmandBaseExpandableListAdapter; import net.osmand.plus.activities.OsmandExpandableListFragment; import net.osmand.plus.activities.TabActivity; import net.osmand.plus.base.BasicProgressAsyncTask; -import net.osmand.plus.download.items.ActiveDownloadsDialogFragment; import net.osmand.plus.download.items.DialogDismissListener; import net.osmand.plus.download.items.RegionItemsFragment; import net.osmand.plus.download.items.SearchDialogFragment; @@ -36,6 +35,7 @@ import android.net.Uri; import android.os.AsyncTask; import android.os.Bundle; import android.os.StatFs; +import android.support.annotation.UiThread; import android.support.v4.app.DialogFragment; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; @@ -71,8 +71,8 @@ public class DownloadActivity extends BaseDownloadActivity implements DialogDism protected void onCreate(Bundle savedInstanceState) { getMyApplication().applyTheme(this); super.onCreate(savedInstanceState); - if (downloadListIndexThread.getCachedIndexFiles() == null || !downloadListIndexThread.isDownloadedFromInternet()) { - downloadListIndexThread.runReloadIndexFiles(); + if (!getDownloadThread().isDownloadedFromInternet()) { + getDownloadThread().runReloadIndexFiles(); } setContentView(R.layout.download); @@ -111,15 +111,11 @@ public class DownloadActivity extends BaseDownloadActivity implements DialogDism } - public Map getIndexActivatedFileNames() { - return downloadListIndexThread != null ? downloadListIndexThread.getIndexActivatedFileNames() : null; - } - @Override protected void onResume() { super.onResume(); getMyApplication().getAppCustomization().resumeActivity(DownloadActivity.class, this); - updateProgress(false); + downloadInProgress(); } @@ -148,24 +144,39 @@ public class DownloadActivity extends BaseDownloadActivity implements DialogDism super.onPause(); getMyApplication().getAppCustomization().pauseActivity(DownloadActivity.class); } - + @Override - public void updateProgress(boolean updateOnlyProgress) { - BasicProgressAsyncTask basicProgressAsyncTask = - downloadListIndexThread.getCurrentRunningTask(); - if (visibleBanner != null) { - final int countedDownloads = downloadListIndexThread.getCountedDownloads(); - visibleBanner.updateProgress(countedDownloads, basicProgressAsyncTask); - } + @UiThread + public void downloadHasFinished() { + + updateBannerInProgress(); if(activeDownloads != null) { - if(updateOnlyProgress) { - activeDownloads.notifyDataSetChanged(); - } else { - activeDownloads.notifyDataSetInvalidated(); - } + activeDownloads.notifyDataSetInvalidated(); } // FIXME //((DownloadActivity) getActivity()).updateDescriptionTextWithSize(getView()); + for (WeakReference ref : fragSet) { + Fragment f = ref.get(); + notifyUpdateDataSetChanged(f); + if (f instanceof UpdatesIndexFragment) { + if (f.isAdded()) { + ((UpdatesIndexFragment) f).updateItemsList(); + } + } else if(f instanceof RegionItemsFragment) { + Fragment innerFragment = ((RegionItemsFragment)f).getChildFragmentManager().findFragmentById(R.id.fragmentContainer); + notifyUpdateDataSetChanged(innerFragment); + + } + } + } + + @Override + @UiThread + public void downloadInProgress() { + updateBannerInProgress(); + if(activeDownloads != null) { + activeDownloads.notifyDataSetChanged(); + } for (WeakReference ref : fragSet) { Fragment f = ref.get(); notifyUpdateDataSetChanged(f); @@ -176,20 +187,18 @@ public class DownloadActivity extends BaseDownloadActivity implements DialogDism } } - @Override - public void updateDownloadList() { - for (WeakReference ref : fragSet) { - Fragment f = ref.get(); - if (f instanceof UpdatesIndexFragment) { - if (f.isAdded()) { - ((UpdatesIndexFragment) f).updateItemsList(); - } - } + + private void updateBannerInProgress() { + BasicProgressAsyncTask basicProgressAsyncTask = getDownloadThread().getCurrentRunningTask(); + if (visibleBanner != null) { + final int countedDownloads = getDownloadThread().getCountedDownloads(); + visibleBanner.updateProgress(countedDownloads, basicProgressAsyncTask); } } - + @Override - public void onCategorizationFinished() { + @UiThread + public void newDownloadIndexes() { for (WeakReference ref : fragSet) { Fragment f = ref.get(); if (f instanceof WorldItemsFragment) { @@ -200,31 +209,19 @@ public class DownloadActivity extends BaseDownloadActivity implements DialogDism if (f.isAdded()) { ((SearchDialogFragment) f).onCategorizationFinished(); } - } - } - } - - - @Override - public void downloadedIndexes() { - for (WeakReference ref : fragSet) { - Fragment f = ref.get(); - if (f instanceof LocalIndexesFragment) { + } else if (f instanceof LocalIndexesFragment) { if (f.isAdded()) { ((LocalIndexesFragment) f).reloadData(); } } } + downloadHasFinished(); } - - public void setActiveDownloads(ActiveDownloadsDialogFragment activeDownloads) { this.activeDownloads = activeDownloads; } - - private void notifyUpdateDataSetChanged(Fragment f) { if (f != null && f.isAdded()) { if(f instanceof OsmandExpandableListFragment) { @@ -247,18 +244,10 @@ public class DownloadActivity extends BaseDownloadActivity implements DialogDism return ((OsmandApplication) getApplication()).getSettings().isLightActionBar(); } - public Map getIndexFileNames() { - return downloadListIndexThread != null ? downloadListIndexThread.getIndexFileNames() : null; - } - - public List getIndexFiles() { - return downloadListIndexThread != null ? downloadListIndexThread.getCachedIndexFiles() : null; - } - public void registerFreeVersionBanner(View view) { visibleBanner = new BannerAndDownloadFreeVersion(view, this); - updateProgress(true); + updateBannerInProgress(); } diff --git a/OsmAnd/src/net/osmand/plus/download/DownloadActivityType.java b/OsmAnd/src/net/osmand/plus/download/DownloadActivityType.java index 0e22d6ed15..b0f09d08e7 100644 --- a/OsmAnd/src/net/osmand/plus/download/DownloadActivityType.java +++ b/OsmAnd/src/net/osmand/plus/download/DownloadActivityType.java @@ -1,8 +1,15 @@ package net.osmand.plus.download; -import android.content.Context; -import android.os.Parcel; -import android.os.Parcelable; +import static net.osmand.IndexConstants.BINARY_MAP_INDEX_EXT; + +import java.io.File; +import java.io.IOException; +import java.net.URLEncoder; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.HashMap; +import java.util.Map; import net.osmand.AndroidUtils; import net.osmand.IndexConstants; @@ -15,18 +22,9 @@ import net.osmand.util.Algorithms; import org.xmlpull.v1.XmlPullParser; -import java.io.File; -import java.io.IOException; -import java.net.URLEncoder; -import java.text.ParseException; -import java.text.SimpleDateFormat; -import java.util.Date; -import java.util.HashMap; -import java.util.Map; +import android.content.Context; -import static net.osmand.IndexConstants.BINARY_MAP_INDEX_EXT; - -public class DownloadActivityType implements Parcelable { +public class DownloadActivityType { private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd.MM.yyyy"); private static Map byTag = new HashMap<>(); @@ -64,7 +62,6 @@ public class DownloadActivityType implements Parcelable { public DownloadActivityType(int stringResource, String tag, int orderIndex) { this.stringResource = stringResource; this.tag = tag; - this.orderIndex = orderIndex; byTag.put(tag, this); iconResource = R.drawable.ic_map; } @@ -371,32 +368,4 @@ public class DownloadActivityType implements Parcelable { return fileName; } - @Override - public int describeContents() { - return 0; - } - - @Override - public void writeToParcel(Parcel dest, int flags) { - dest.writeInt(this.stringResource); - dest.writeInt(this.iconResource); - dest.writeString(this.tag); - } - - protected DownloadActivityType(Parcel in) { - this.stringResource = in.readInt(); - this.iconResource = in.readInt(); - this.tag = in.readString(); - byTag.put(tag, this); - } - - public static final Parcelable.Creator CREATOR = new Parcelable.Creator() { - public DownloadActivityType createFromParcel(Parcel source) { - return new DownloadActivityType(source); - } - - public DownloadActivityType[] newArray(int size) { - return new DownloadActivityType[size]; - } - }; } \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/plus/download/DownloadIndexesThread.java b/OsmAnd/src/net/osmand/plus/download/DownloadIndexesThread.java index 1dc12ec2c5..d4cc98574d 100644 --- a/OsmAnd/src/net/osmand/plus/download/DownloadIndexesThread.java +++ b/OsmAnd/src/net/osmand/plus/download/DownloadIndexesThread.java @@ -1,36 +1,23 @@ package net.osmand.plus.download; import java.io.File; -import java.io.FilenameFilter; import java.io.IOException; -import java.io.InputStream; import java.util.ArrayList; import java.util.Collections; -import java.util.Comparator; -import java.util.HashMap; import java.util.HashSet; -import java.util.LinkedHashMap; -import java.util.LinkedList; import java.util.List; -import java.util.Map; import java.util.Set; -import java.util.TreeSet; import java.util.concurrent.ConcurrentLinkedQueue; -import net.osmand.Collator; import net.osmand.IndexConstants; -import net.osmand.OsmAndCollator; import net.osmand.PlatformUtil; import net.osmand.access.AccessibleToast; -import net.osmand.map.OsmandRegions; import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandSettings.OsmandPreference; import net.osmand.plus.R; import net.osmand.plus.Version; -import net.osmand.plus.WorldRegion; import net.osmand.plus.base.BasicProgressAsyncTask; import net.osmand.plus.download.DownloadFileHelper.DownloadFileShowWarning; -import net.osmand.plus.download.DownloadOsmandIndexesHelper.AssetIndexItem; import net.osmand.plus.helpers.DatabaseHelper; import net.osmand.plus.resources.ResourceManager; import net.osmand.util.Algorithms; @@ -57,7 +44,7 @@ public class DownloadIndexesThread { private final static Log LOG = PlatformUtil.getLog(DownloadIndexesThread.class); private final Context ctx; private OsmandApplication app; - private java.text.DateFormat dateFormat; + private BaseDownloadActivity uiActivity = null; private DatabaseHelper dbHelper; private DownloadFileHelper downloadFileHelper; @@ -66,25 +53,15 @@ public class DownloadIndexesThread { private IndexItem currentDownloadingItem = null; private int currentDownloadingItemProgress = 0; - - private DownloadIndexes indexes = new DownloadIndexes(); - public static class DownloadIndexes { - Map> resourcesByRegions = new HashMap<>(); - List voiceRecItems = new LinkedList<>(); - List voiceTTSItems = new LinkedList<>(); - IndexFileList indexFiles = null; - } - private Map indexFileNames = new LinkedHashMap<>(); - private Map indexActivatedFileNames = new LinkedHashMap<>(); - private List itemsToUpdate = new ArrayList<>(); - + private DownloadResources indexes; public DownloadIndexesThread(Context ctx) { this.ctx = ctx; app = (OsmandApplication) ctx.getApplicationContext(); + indexes = new DownloadResources(app); + indexes.initAlreadyLoadedFiles(); downloadFileHelper = new DownloadFileHelper(app); - dateFormat = app.getResourceManager().getDateFormat(); dbHelper = new DatabaseHelper(app); } @@ -93,34 +70,26 @@ public class DownloadIndexesThread { public void setUiActivity(BaseDownloadActivity uiActivity) { this.uiActivity = uiActivity; } - - + @UiThread - private void updateProgressUI(boolean onlyProgress) { + protected void downloadInProgress() { if (uiActivity != null) { - uiActivity.updateProgress(onlyProgress); + uiActivity.downloadInProgress(); } } + @UiThread - private void onCategorizationFinished() { + protected void downloadHasFinished() { if (uiActivity != null) { - uiActivity.onCategorizationFinished(); + uiActivity.downloadHasFinished(); } } @UiThread - private void updateDownloadList() { + protected void newDownloadIndexes() { if (uiActivity != null) { - uiActivity.updateDownloadList(); - } - } - - @UiThread - private void notifyFilesToUpdateChanged() { - List filtered = getCachedIndexFiles(); - if (filtered != null) { - updateDownloadList(); + uiActivity.newDownloadIndexes(); } } @@ -161,164 +130,7 @@ 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; - } - - public void updateLoadedFiles() { - Map indexActivatedFileNames = app.getResourceManager().getIndexFileNames(); - listWithAlternatives(dateFormat, app.getAppPath(""), IndexConstants.EXTRA_EXT, - indexActivatedFileNames); - Map indexFileNames = app.getResourceManager().getIndexFileNames(); - listWithAlternatives(dateFormat, app.getAppPath(""), IndexConstants.EXTRA_EXT, - indexFileNames); - app.getAppCustomization().updatedLoadedFiles(indexFileNames, indexActivatedFileNames); - listWithAlternatives(dateFormat, app.getAppPath(IndexConstants.TILES_INDEX_DIR), - IndexConstants.SQLITE_EXT, indexFileNames); - app.getResourceManager().getBackupIndexes(indexFileNames); - this.indexFileNames = indexFileNames; - this.indexActivatedFileNames = indexActivatedFileNames; - //updateFilesToDownload(); - } - - public Map getDownloadedIndexFileNames() { - return indexFileNames; - } - - public boolean isDownloadedFromInternet() { - return indexes.indexFiles != null && indexes.indexFiles.isDownloadedFromInternet(); - } - - public List getItemsToUpdate() { - return itemsToUpdate; - } - - public Map> getResourcesByRegions() { - return indexes.resourcesByRegions; - } - - public List getVoiceRecItems() { - return indexes.voiceRecItems; - } - - public List getVoiceTTSItems() { - return indexes.voiceTTSItems; - } - - private boolean prepareData(List resources, - Map> resourcesByRegions, - List voiceRecItems, List voiceTTSItems) { - List resourcesInRepository; - if (resources != null) { - resourcesInRepository = resources; - } else { - resourcesInRepository = DownloadActivity.downloadListIndexThread.getCachedIndexFiles(); - } - if (resourcesInRepository == null) { - return false; - } - - for (WorldRegion region : app.getWorldRegion().getFlattenedSubregions()) { - processRegion(resourcesInRepository, resourcesByRegions, voiceRecItems, voiceTTSItems, false, region); - } - processRegion(resourcesInRepository, resourcesByRegions, voiceRecItems, voiceTTSItems, true, app.getWorldRegion()); - - final Collator collator = OsmAndCollator.primaryCollator(); - final OsmandRegions osmandRegions = app.getRegions(); - - Collections.sort(voiceRecItems, new Comparator() { - @Override - public int compare(IndexItem lhs, IndexItem rhs) { - return collator.compare(lhs.getVisibleName(app.getApplicationContext(), osmandRegions), - rhs.getVisibleName(app.getApplicationContext(), osmandRegions)); - } - }); - - Collections.sort(voiceTTSItems, new Comparator() { - @Override - public int compare(IndexItem lhs, IndexItem rhs) { - return collator.compare(lhs.getVisibleName(app.getApplicationContext(), osmandRegions), - rhs.getVisibleName(app.getApplicationContext(), osmandRegions)); - } - }); - - return true; - } - - // FIXME argument list - private void processRegion(List resourcesInRepository, Map> resourcesByRegions, - List voiceRecItems, List voiceTTSItems, - boolean processVoiceFiles, WorldRegion region) { - String downloadsIdPrefix = region.getDownloadsIdPrefix(); - Map regionResources = new HashMap<>(); - Set typesSet = new TreeSet<>(new Comparator() { - @Override - public int compare(DownloadActivityType dat1, DownloadActivityType dat2) { - return dat1.getTag().compareTo(dat2.getTag()); - } - }); - for (IndexItem resource : resourcesInRepository) { - if (processVoiceFiles) { - if (resource.getSimplifiedFileName().endsWith(".voice.zip")) { - voiceRecItems.add(resource); - continue; - } else if (resource.getSimplifiedFileName().contains(".ttsvoice.zip")) { - voiceTTSItems.add(resource); - continue; - } - } - if (!resource.getSimplifiedFileName().startsWith(downloadsIdPrefix)) { - continue; - } - - if (resource.type == DownloadActivityType.NORMAL_FILE - || resource.type == DownloadActivityType.ROADS_FILE) { - if (resource.isAlreadyDownloaded(indexFileNames)) { - region.processNewMapState(checkIfItemOutdated(resource) - ? WorldRegion.MapState.OUTDATED : WorldRegion.MapState.DOWNLOADED); - } else { - region.processNewMapState(WorldRegion.MapState.NOT_DOWNLOADED); - } - } - typesSet.add(resource.getType()); - regionResources.put(resource.getSimplifiedFileName(), resource); - } - - if (region.getSuperregion() != null && region.getSuperregion().getSuperregion() != app.getWorldRegion()) { - if (region.getSuperregion().getResourceTypes() == null) { - region.getSuperregion().setResourceTypes(typesSet); - } else { - region.getSuperregion().getResourceTypes().addAll(typesSet); - } - } - - region.setResourceTypes(typesSet); - resourcesByRegions.put(region, regionResources); - } - - - private boolean checkRunning() { - if (getCurrentRunningTask() != null) { - AccessibleToast.makeText(app, R.string.wait_current_task_finished, Toast.LENGTH_SHORT).show(); - return true; - } - return false; - } - + public void runReloadIndexFiles() { if (checkRunning()) { return; @@ -348,76 +160,11 @@ public class DownloadIndexesThread { } } - private

void execute(BasicProgressAsyncTask task, P... indexItems) { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { - task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, indexItems); - } else { - task.execute(indexItems); - } + + public boolean isDownloadedFromInternet() { + return indexes.isDownloadedFromInternet; } - - private void prepareFilesToUpdate() { - List filtered = getCachedIndexFiles(); - if (filtered != null) { - itemsToUpdate.clear(); - for (IndexItem item : filtered) { - boolean outdated = checkIfItemOutdated(item); - //include only activated files here - if (outdated && indexActivatedFileNames.containsKey(item.getTargetFileName())) { - itemsToUpdate.add(item); - } - } - } - } - - - public boolean checkIfItemOutdated(IndexItem item) { - boolean outdated = false; - 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)) { - if ((item.getType() == DownloadActivityType.NORMAL_FILE && !item.extra) || - item.getType() == DownloadActivityType.ROADS_FILE || - item.getType() == DownloadActivityType.WIKIPEDIA_FILE || - item.getType() == DownloadActivityType.SRTM_COUNTRY_FILE) { - outdated = true; - } else { - long itemSize = item.getContentSize(); - long oldItemSize = 0; - if (item.getType() == DownloadActivityType.VOICE_FILE) { - if (item instanceof AssetIndexItem) { - File file = new File(((AssetIndexItem) item).getDestFile()); - oldItemSize = file.length(); - } else { - File fl = new File(item.getType().getDownloadFolder(app, item), sfName + "/_config.p"); - if (fl.exists()) { - oldItemSize = fl.length(); - try { - InputStream is = ctx.getAssets().open("voice/" + sfName + "/config.p"); - if (is != null) { - itemSize = is.available(); - is.close(); - } - } catch (IOException e) { - } - } - } - } else { - oldItemSize = app.getAppPath(item.getTargetFileName()).length(); - } - if (itemSize != oldItemSize) { - outdated = true; - } - } - } - return outdated; - } - + public IndexItem getCurrentDownloadingItem() { return currentDownloadingItem; } @@ -440,6 +187,7 @@ public class DownloadIndexesThread { return null; } + @SuppressWarnings("deprecation") public double getAvailableSpace() { File dir = app.getAppPath("").getParentFile(); double asz = -1; @@ -449,44 +197,29 @@ public class DownloadIndexesThread { } return asz; } + + /// PRIVATE IMPL - - public Map listWithAlternatives(final java.text.DateFormat dateFormat, File file, final String ext, - final Map files) { - if (file.isDirectory()) { - file.list(new FilenameFilter() { - @Override - public boolean accept(File dir, String filename) { - if (filename.endsWith(ext)) { - String date = dateFormat.format(findFileInDir(new File(dir, filename)).lastModified()); - files.put(filename, date); - return true; - } else { - return false; - } - } - }); - + private boolean checkRunning() { + if (getCurrentRunningTask() != null) { + AccessibleToast.makeText(app, R.string.wait_current_task_finished, Toast.LENGTH_SHORT).show(); + return true; } - return files; + return false; } - public File findFileInDir(File file) { - if(file.isDirectory()) { - File[] lf = file.listFiles(); - if(lf != null) { - for(File f : lf) { - if(f.isFile()) { - return f; - } - } - } + @SuppressWarnings("unchecked") + private

void execute(BasicProgressAsyncTask task, P... indexItems) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { + task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, indexItems); + } else { + task.execute(indexItems); } - return file; } - public class ReloadIndexesTask extends BasicProgressAsyncTask { + + private class ReloadIndexesTask extends BasicProgressAsyncTask { public ReloadIndexesTask(Context ctx) { super(ctx); @@ -500,31 +233,26 @@ public class DownloadIndexesThread { } @Override - protected DownloadIndexes doInBackground(Void... params) { - DownloadIndexes result = new DownloadIndexes(); - IndexFileList indexFileList = DownloadOsmandIndexesHelper.getIndexesList(ctx); - result.indexFiles = indexFileList; + protected DownloadResources doInBackground(Void... params) { + DownloadResources result = new DownloadResources(app); + DownloadOsmandIndexesHelper.IndexFileList indexFileList = DownloadOsmandIndexesHelper.getIndexesList(ctx); if (indexFileList != null) { - updateLoadedFiles(); - prepareFilesToUpdate(); - prepareData(indexFileList.getIndexFiles(), result.resourcesByRegions, result.voiceRecItems, result.voiceTTSItems); + result.isDownloadedFromInternet = indexFileList.isDownloadedFromInternet(); + result.mapVersionIsIncreased = indexFileList.isIncreasedMapVersion(); + result.prepareData(indexFileList.getIndexFiles()); } return result; } - protected void onPostExecute(DownloadIndexes result) { + protected void onPostExecute(DownloadResources result) { indexes = result; - if (result.indexFiles != null) { - if (result.indexFiles.isIncreasedMapVersion()) { - showWarnDialog(); - } - } else { + if (result.mapVersionIsIncreased) { + showWarnDialog(); + } else if (!result.isDownloadedFromInternet) { AccessibleToast.makeText(ctx, R.string.list_index_files_was_not_loaded, Toast.LENGTH_LONG).show(); } currentRunningTask.remove(this); - notifyFilesToUpdateChanged(); - updateProgressUI(false); - onCategorizationFinished(); + newDownloadIndexes(); } private void showWarnDialog() { @@ -552,31 +280,12 @@ public class DownloadIndexesThread { @Override protected void updateProgress(boolean updateOnlyProgress, Void tag) { - updateProgressUI(true); + downloadInProgress(); } } - //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 class DownloadIndexesAsyncTask extends BasicProgressAsyncTask implements DownloadFileShowWarning { private OsmandPreference downloads; @@ -598,7 +307,6 @@ public class DownloadIndexesThread { protected void onProgressUpdate(Object... values) { for (Object o : values) { if (o instanceof IndexItem) { - updateProgressUI(false); IndexItem item = (IndexItem) o; String name = item.getBasename(); long count = dbHelper.getCount(name, DatabaseHelper.DOWNLOAD_ENTRY) + 1; @@ -616,9 +324,9 @@ public class DownloadIndexesThread { AccessibleToast.makeText(ctx, message, Toast.LENGTH_LONG).show(); } } - updateProgressUI(false); } } + downloadInProgress(); super.onProgressUpdate(values); } @@ -648,9 +356,8 @@ public class DownloadIndexesThread { } } currentRunningTask.remove(this); - notifyFilesToUpdateChanged(); - updateFilesToUpdate(); - updateProgressUI(false); + downloadHasFinished(); + indexes.updateFilesToUpdate(); } @@ -698,7 +405,6 @@ public class DownloadIndexesThread { currentDownloadingItemProgress = 0; } String warn = reindexFiles(filesToReindex); - updateLoadedFiles(); return warn; } catch (InterruptedException e) { LOG.info("Download Interrupted"); @@ -803,7 +509,7 @@ public class DownloadIndexesThread { @Override protected void updateProgress(boolean updateOnlyProgress, IndexItem tag) { currentDownloadingItemProgress = getProgressPercentage(); - updateProgressUI(true); + downloadInProgress(); } } diff --git a/OsmAnd/src/net/osmand/plus/download/DownloadOsmandIndexesHelper.java b/OsmAnd/src/net/osmand/plus/download/DownloadOsmandIndexesHelper.java index ba1cee8752..513d9da551 100644 --- a/OsmAnd/src/net/osmand/plus/download/DownloadOsmandIndexesHelper.java +++ b/OsmAnd/src/net/osmand/plus/download/DownloadOsmandIndexesHelper.java @@ -1,11 +1,13 @@ package net.osmand.plus.download; import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; +import java.io.Serializable; import java.net.URLConnection; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -32,9 +34,81 @@ import android.content.res.AssetManager; public class DownloadOsmandIndexesHelper { private final static Log log = PlatformUtil.getLog(DownloadOsmandIndexesHelper.class); + public static class IndexFileList implements Serializable { + private static final long serialVersionUID = 1L; + + private boolean downloadedFromInternet = false; + IndexItem basemap; + ArrayList indexFiles = new ArrayList(); + private String mapversion; + + private Comparator comparator = new Comparator(){ + @Override + public int compare(IndexItem o1, IndexItem o2) { + String object1 = o1.getFileName(); + String object2 = o2.getFileName(); + if(object1.endsWith(IndexConstants.ANYVOICE_INDEX_EXT_ZIP)){ + if(object2.endsWith(IndexConstants.ANYVOICE_INDEX_EXT_ZIP)){ + return object1.compareTo(object2); + } else { + return -1; + } + } else if(object2.endsWith(IndexConstants.ANYVOICE_INDEX_EXT_ZIP)){ + return 1; + } + return object1.compareTo(object2); + } + }; + + public void setDownloadedFromInternet(boolean downloadedFromInternet) { + this.downloadedFromInternet = downloadedFromInternet; + } + + public boolean isDownloadedFromInternet() { + return downloadedFromInternet; + } + + public void setMapVersion(String mapversion) { + this.mapversion = mapversion; + } + + public void add(IndexItem indexItem) { + indexFiles.add(indexItem); + if(indexItem.getFileName().toLowerCase().startsWith("world_basemap")) { + basemap = indexItem; + } + } + + public void sort(){ + Collections.sort(indexFiles, comparator); + } + + public boolean isAcceptable() { + return (indexFiles != null && !indexFiles.isEmpty()) || (mapversion != null); + } + + public List getIndexFiles() { + return indexFiles; + } + + public IndexItem getBasemap() { + return basemap; + } + + public boolean isIncreasedMapVersion() { + try { + int mapVersionInList = Integer.parseInt(mapversion); + return IndexConstants.BINARY_MAP_VERSION < mapVersionInList; + } catch (NumberFormatException e) { + //ignore this... + } + return false; + } + + } public static IndexFileList getIndexesList(Context ctx) { - PackageManager pm =ctx.getPackageManager(); + PackageManager pm = ctx.getPackageManager(); AssetManager amanager = ctx.getAssets(); IndexFileList result = downloadIndexesListFromInternet((OsmandApplication) ctx.getApplicationContext()); if (result == null) { @@ -42,7 +116,7 @@ public class DownloadOsmandIndexesHelper { } else { result.setDownloadedFromInternet(true); } - //add all tts files from assets + // add all tts files from assets listVoiceAssets(result, amanager, pm, ((OsmandApplication) ctx.getApplicationContext()).getSettings()); return result; } diff --git a/OsmAnd/src/net/osmand/plus/download/DownloadResourceGroup.java b/OsmAnd/src/net/osmand/plus/download/DownloadResourceGroup.java new file mode 100644 index 0000000000..f7bed5b604 --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/download/DownloadResourceGroup.java @@ -0,0 +1,91 @@ +package net.osmand.plus.download; + +import java.util.ArrayList; +import java.util.List; + +import net.osmand.plus.WorldRegion; + +public class DownloadResourceGroup { + + private final DownloadResourceGroupType type; + private final DownloadResourceGroup parentGroup; + // ASSERT: individualResources are not empty if and only if groups are empty + private final List individualResources; + private final List groups; + protected final String id; + + protected WorldRegion region; + + public enum DownloadResourceGroupType { + WORLD, VOICE, WORLD_MAPS, REGION + + } + + public DownloadResourceGroup(DownloadResourceGroup parentGroup, DownloadResourceGroupType type, String id, + boolean flat) { + if (flat) { + this.individualResources = new ArrayList(); + this.groups = null; + } else { + this.individualResources = null; + this.groups = new ArrayList(); + } + this.id = id; + this.type = type; + this.parentGroup = parentGroup; + } + + public String getGroupId() { + return id; + } + + public boolean flatGroup() { + return individualResources != null; + } + + public DownloadResourceGroup getParentGroup() { + return parentGroup; + } + + public DownloadResources getRoot() { + if (this instanceof DownloadResources) { + return (DownloadResources) this; + } else if (parentGroup != null) { + return parentGroup.getRoot(); + } + return null; + } + + public DownloadResourceGroupType getType() { + return type; + } + + public DownloadResourceGroup getGroupById(String uid) { + String[] lst = uid.split("\\#"); + return getGroupById(lst, 0); + } + + private DownloadResourceGroup getGroupById(String[] lst, int subInd) { + if (lst.length > subInd && lst[subInd].equals(id)) { + if (lst.length == subInd + 1) { + return this; + } else if (groups != null) { + for (DownloadResourceGroup rg : groups) { + DownloadResourceGroup r = rg.getGroupById(lst, subInd + 1); + if (r != null) { + return r; + } + } + } + } + return null; + } + + public String getUniqueId() { + if (parentGroup == null) { + return id; + } + return parentGroup.getUniqueId() + "#" + id; + } + +} \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/plus/download/DownloadResources.java b/OsmAnd/src/net/osmand/plus/download/DownloadResources.java new file mode 100644 index 0000000000..9fb9372724 --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/download/DownloadResources.java @@ -0,0 +1,464 @@ +package net.osmand.plus.download; + +import java.io.File; +import java.io.FilenameFilter; +import java.io.IOException; +import java.io.InputStream; +import java.text.Collator; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.TreeSet; + +import android.content.Context; +import net.osmand.IndexConstants; +import net.osmand.OsmAndCollator; +import net.osmand.PlatformUtil; +import net.osmand.map.OsmandRegions; +import net.osmand.plus.OsmandApplication; +import net.osmand.plus.OsmandPlugin; +import net.osmand.plus.R; +import net.osmand.plus.WorldRegion; +import net.osmand.plus.download.DownloadOsmandIndexesHelper.AssetIndexItem; +import net.osmand.plus.download.items.DownloadIndexes; +import net.osmand.plus.download.items.ItemsListBuilder; +import net.osmand.plus.download.items.ResourceItem; +import net.osmand.plus.download.items.ResourceItemComparator; +import net.osmand.plus.download.items.ItemsListBuilder.VoicePromptsType; +import net.osmand.plus.srtmplugin.SRTMPlugin; +import net.osmand.util.Algorithms; + +public class DownloadResources extends DownloadResourceGroup { + public boolean isDownloadedFromInternet = false; + public boolean mapVersionIsIncreased = false; + public OsmandApplication app; + private Map indexFileNames = new LinkedHashMap<>(); + private Map indexActivatedFileNames = new LinkedHashMap<>(); + private List itemsToUpdate = new ArrayList<>(); + + public DownloadResources(OsmandApplication app) { + super(null, DownloadResourceGroupType.WORLD, "", false); + this.region = app.getWorldRegion(); + this.app = app; + } + + public void initAlreadyLoadedFiles() { + java.text.DateFormat dateFormat = app.getResourceManager().getDateFormat(); + Map indexActivatedFileNames = app.getResourceManager().getIndexFileNames(); + listWithAlternatives(dateFormat, app.getAppPath(""), IndexConstants.EXTRA_EXT, indexActivatedFileNames); + Map indexFileNames = app.getResourceManager().getIndexFileNames(); + listWithAlternatives(dateFormat, app.getAppPath(""), IndexConstants.EXTRA_EXT, indexFileNames); + app.getAppCustomization().updatedLoadedFiles(indexFileNames, indexActivatedFileNames); + listWithAlternatives(dateFormat, app.getAppPath(IndexConstants.TILES_INDEX_DIR), IndexConstants.SQLITE_EXT, + indexFileNames); + app.getResourceManager().getBackupIndexes(indexFileNames); + this.indexFileNames = indexFileNames; + this.indexActivatedFileNames = indexActivatedFileNames; + prepareFilesToUpdate(); + } + + public boolean checkIfItemOutdated(IndexItem item) { + boolean outdated = false; + 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)) { + if ((item.getType() == DownloadActivityType.NORMAL_FILE && !item.extra) + || item.getType() == DownloadActivityType.ROADS_FILE + || item.getType() == DownloadActivityType.WIKIPEDIA_FILE + || item.getType() == DownloadActivityType.SRTM_COUNTRY_FILE) { + outdated = true; + } else { + long itemSize = item.getContentSize(); + long oldItemSize = 0; + if (item.getType() == DownloadActivityType.VOICE_FILE) { + if (item instanceof AssetIndexItem) { + File file = new File(((AssetIndexItem) item).getDestFile()); + oldItemSize = file.length(); + } else { + File fl = new File(item.getType().getDownloadFolder(app, item), sfName + "/_config.p"); + if (fl.exists()) { + oldItemSize = fl.length(); + try { + InputStream is = app.getAssets().open("voice/" + sfName + "/config.p"); + if (is != null) { + itemSize = is.available(); + is.close(); + } + } catch (IOException e) { + } + } + } + } else { + oldItemSize = app.getAppPath(item.getTargetFileName()).length(); + } + if (itemSize != oldItemSize) { + outdated = true; + } + } + } + return outdated; + } + + + + protected 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; + } + + private Map listWithAlternatives(final java.text.DateFormat dateFormat, File file, + final String ext, final Map files) { + if (file.isDirectory()) { + file.list(new FilenameFilter() { + @Override + public boolean accept(File dir, String filename) { + if (filename.endsWith(ext)) { + String date = dateFormat.format(findFileInDir(new File(dir, filename)).lastModified()); + files.put(filename, date); + return true; + } else { + return false; + } + } + }); + + } + return files; + } + + private File findFileInDir(File file) { + if (file.isDirectory()) { + File[] lf = file.listFiles(); + if (lf != null) { + for (File f : lf) { + if (f.isFile()) { + return f; + } + } + } + } + return file; + } + + + + ////////// FIXME //////////// + + private void prepareFilesToUpdate() { + List filtered = getCachedIndexFiles(); + if (filtered != null) { + itemsToUpdate.clear(); + for (IndexItem item : filtered) { + boolean outdated = checkIfItemOutdated(item); + // include only activated files here + if (outdated && indexActivatedFileNames.containsKey(item.getTargetFileName())) { + itemsToUpdate.add(item); + } + } + } + } + + private void processRegion(List resourcesInRepository, DownloadResources di, + boolean processVoiceFiles, WorldRegion region) { + String downloadsIdPrefix = region.getDownloadsIdPrefix(); + Map regionResources = new HashMap<>(); + Set typesSet = new TreeSet<>(new Comparator() { + @Override + public int compare(DownloadActivityType dat1, DownloadActivityType dat2) { + return dat1.getTag().compareTo(dat2.getTag()); + } + }); + for (IndexItem resource : resourcesInRepository) { + if (processVoiceFiles) { + if (resource.getSimplifiedFileName().endsWith(".voice.zip")) { + voiceRecItems.add(resource); + continue; + } else if (resource.getSimplifiedFileName().contains(".ttsvoice.zip")) { + voiceTTSItems.add(resource); + continue; + } + } + if (!resource.getSimplifiedFileName().startsWith(downloadsIdPrefix)) { + continue; + } + + if (resource.type == DownloadActivityType.NORMAL_FILE + || resource.type == DownloadActivityType.ROADS_FILE) { + if (resource.isAlreadyDownloaded(indexFileNames)) { + region.processNewMapState(checkIfItemOutdated(resource) + ? WorldRegion.MapState.OUTDATED : WorldRegion.MapState.DOWNLOADED); + } else { + region.processNewMapState(WorldRegion.MapState.NOT_DOWNLOADED); + } + } + typesSet.add(resource.getType()); + regionResources.put(resource.getSimplifiedFileName(), resource); + } + + if (region.getSuperregion() != null && region.getSuperregion().getSuperregion() != app.getWorldRegion()) { + if (region.getSuperregion().getResourceTypes() == null) { + region.getSuperregion().setResourceTypes(typesSet); + } else { + region.getSuperregion().getResourceTypes().addAll(typesSet); + } + } + + region.setResourceTypes(typesSet); + resourcesByRegions.put(region, regionResources); + } + + protected boolean prepareData(List resources) { + for (WorldRegion region : app.getWorldRegion().getFlattenedSubregions()) { + processRegion(resourcesInRepository, di, false, region); + } + processRegion(resourcesInRepository, di, true, app.getWorldRegion()); + + final net.osmand.Collator collator = OsmAndCollator.primaryCollator(); + final OsmandRegions osmandRegions = app.getRegions(); + + Collections.sort(di.voiceRecItems, new Comparator() { + @Override + public int compare(IndexItem lhs, IndexItem rhs) { + return collator.compare(lhs.getVisibleName(app.getApplicationContext(), osmandRegions), + rhs.getVisibleName(app.getApplicationContext(), osmandRegions)); + } + }); + + Collections.sort(di.voiceTTSItems, new Comparator() { + @Override + public int compare(IndexItem lhs, IndexItem rhs) { + return collator.compare(lhs.getVisibleName(app.getApplicationContext(), osmandRegions), + rhs.getVisibleName(app.getApplicationContext(), osmandRegions)); + } + }); + initAlreadyLoadedFiles(); + return true; + } + + + public class ItemsListBuilder { + + //public static final String WORLD_BASEMAP_KEY = "world_basemap.obf.zip"; + public static final String WORLD_SEAMARKS_KEY = "world_seamarks_basemap.obf.zip"; + private static final org.apache.commons.logging.Log LOG = PlatformUtil.getLog(ItemsListBuilder.class); + + private DownloadIndexes downloadIndexes; + + private List regionMapItems; + private List allResourceItems; + private List allSubregionItems; + + private OsmandApplication app; + private WorldRegion region; + + private boolean srtmDisabled; + private boolean hasSrtm; + private boolean hasHillshade; + + public List getRegionMapItems() { + return regionMapItems; + } + + public List getAllResourceItems() { + return allResourceItems; + } + + public List getRegionsFromAllItems() { + List list = new LinkedList<>(); + for (Object obj : allResourceItems) { + if (obj instanceof WorldRegion) { + list.add((WorldRegion) obj); + } + } + return list; + } + + public static String getVoicePromtName(Context ctx, VoicePromptsType type) { + switch (type) { + case RECORDED: + return ctx.getResources().getString(R.string.index_name_voice); + case TTS: + return ctx.getResources().getString(R.string.index_name_tts_voice); + default: + return ""; + } + } + + public List getVoicePromptsItems(VoicePromptsType type) { + switch (type) { + case RECORDED: + return downloadIndexes.voiceRecItems; + case TTS: + return downloadIndexes.voiceTTSItems; + default: + return new LinkedList<>(); + } + } + + public boolean isVoicePromptsItemsEmpty(VoicePromptsType type) { + switch (type) { + case RECORDED: + return downloadIndexes.voiceRecItems.isEmpty(); + case TTS: + return downloadIndexes.voiceTTSItems.isEmpty(); + default: + return true; + } + } + + // FIXME + public ItemsListBuilder(OsmandApplication app, String regionId, DownloadIndexes di) { + this.app = app; + this.downloadIndexes = di; + + regionMapItems = new LinkedList<>(); + allResourceItems = new LinkedList<>(); + allSubregionItems = new LinkedList<>(); + + region = app.getWorldRegion().getRegionById(regionId); + } + + public ItemsListBuilder build() { + if (obtainDataAndItems()) { + return this; + } else { + return null; + } + } + + private boolean obtainDataAndItems() { + if (downloadIndexes.resourcesByRegions.isEmpty() || region == null) { + return false; + } + + collectSubregionsDataAndItems(); + collectResourcesDataAndItems(); + + return true; + } + + private void collectSubregionsDataAndItems() { + srtmDisabled = OsmandPlugin.getEnabledPlugin(SRTMPlugin.class) == null; + hasSrtm = false; + hasHillshade = false; + + // Collect all regions (and their parents) that have at least one + // resource available in repository or locally. + + allResourceItems.clear(); + allSubregionItems.clear(); + regionMapItems.clear(); + + for (WorldRegion subregion : region.getFlattenedSubregions()) { + if (subregion.getSuperregion() == region) { + if (subregion.getFlattenedSubregions().size() > 0) { + allSubregionItems.add(subregion); + } else { + collectSubregionItems(subregion); + } + } + } + } + + private void collectSubregionItems(WorldRegion region) { + Map regionResources = downloadIndexes.resourcesByRegions.get(region); + + if (regionResources == null) { + return; + } + + List regionMapArray = new LinkedList<>(); + List allResourcesArray = new LinkedList<>(); + + Context context = app.getApplicationContext(); + OsmandRegions osmandRegions = app.getRegions(); + + for (IndexItem indexItem : regionResources.values()) { + + String name = indexItem.getVisibleName(context, osmandRegions, false); + if (Algorithms.isEmpty(name)) { + continue; + } + + ResourceItem resItem = new ResourceItem(indexItem, region); + resItem.setResourceId(indexItem.getSimplifiedFileName()); + resItem.setTitle(name); + + if (region != this.region && srtmDisabled) { + if (indexItem.getType() == DownloadActivityType.SRTM_COUNTRY_FILE) { + if (hasSrtm) { + continue; + } else { + hasSrtm = true; + } + } else if (indexItem.getType() == DownloadActivityType.HILLSHADE_FILE) { + if (hasHillshade) { + continue; + } else { + hasHillshade = true; + } + } + } + + + if (region == this.region) { + regionMapArray.add(resItem); + } else { + allResourcesArray.add(resItem); + } + + } + + regionMapItems.addAll(regionMapArray); + + if (allResourcesArray.size() > 1) { + allSubregionItems.add(region); + } else { + allResourceItems.addAll(allResourcesArray); + } + } + + private void collectResourcesDataAndItems() { + collectSubregionItems(region); + + allResourceItems.addAll(allSubregionItems); + + Collections.sort(allResourceItems, new ResourceItemComparator()); + Collections.sort(regionMapItems, new ResourceItemComparator()); + } + + public enum MapState { + NOT_DOWNLOADED, + DOWNLOADED, + OUTDATED + } + + + public enum VoicePromptsType { + NONE, + RECORDED, + TTS + } + } + +} \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/plus/download/IndexFileList.java b/OsmAnd/src/net/osmand/plus/download/IndexFileList.java deleted file mode 100644 index 059bd5b1fb..0000000000 --- a/OsmAnd/src/net/osmand/plus/download/IndexFileList.java +++ /dev/null @@ -1,90 +0,0 @@ -package net.osmand.plus.download; - -import java.io.Serializable; -import java.util.ArrayList; -import java.util.Collections; -import java.util.Comparator; -import java.util.List; - -import net.osmand.IndexConstants; - -public class IndexFileList implements Serializable { - private static final long serialVersionUID = 1L; - - private boolean downloadedFromInternet = false; - IndexItem basemap; - ArrayList indexFiles = new ArrayList(); - private String mapversion; - - private Comparator comparator = new Comparator(){ - @Override - public int compare(IndexItem o1, IndexItem o2) { - String object1 = o1.getFileName(); - String object2 = o2.getFileName(); - if(object1.endsWith(IndexConstants.ANYVOICE_INDEX_EXT_ZIP)){ - if(object2.endsWith(IndexConstants.ANYVOICE_INDEX_EXT_ZIP)){ - return object1.compareTo(object2); - } else { - return -1; - } - } else if(object2.endsWith(IndexConstants.ANYVOICE_INDEX_EXT_ZIP)){ - return 1; - } - return object1.compareTo(object2); - } - }; - - public void setDownloadedFromInternet(boolean downloadedFromInternet) { - this.downloadedFromInternet = downloadedFromInternet; - } - - public boolean isDownloadedFromInternet() { - return downloadedFromInternet; - } - - public void setMapVersion(String mapversion) { - this.mapversion = mapversion; - } - - public void add(IndexItem indexItem) { - indexFiles.add(indexItem); - if(indexItem.getFileName().toLowerCase().startsWith("world_basemap")) { - basemap = indexItem; - } - } - - public void sort(){ - Collections.sort(indexFiles, comparator); - } - - public boolean isAcceptable() { - return (indexFiles != null && !indexFiles.isEmpty()) || (mapversion != null); - } - - public List getIndexFiles() { - return indexFiles; - } - - public IndexItem getBasemap() { - return basemap; - } - - public boolean isIncreasedMapVersion() { - try { - int mapVersionInList = Integer.parseInt(mapversion); - return IndexConstants.BINARY_MAP_VERSION < mapVersionInList; - } catch (NumberFormatException e) { - //ignore this... - } - return false; - } - - public IndexItem getIndexFilesByName(String key) { - for(IndexItem i : indexFiles) { - if(i.getFileName().equals(key)) { - return i; - } - } - return null; - } -} diff --git a/OsmAnd/src/net/osmand/plus/download/IndexItemCategory.java b/OsmAnd/src/net/osmand/plus/download/IndexItemCategory.java deleted file mode 100644 index c0417eb3a9..0000000000 --- a/OsmAnd/src/net/osmand/plus/download/IndexItemCategory.java +++ /dev/null @@ -1,130 +0,0 @@ -package net.osmand.plus.download; - -import android.support.annotation.NonNull; - -import net.osmand.Collator; -import net.osmand.OsmAndCollator; -import net.osmand.map.OsmandRegions; -import net.osmand.plus.OsmandApplication; -import net.osmand.plus.R; -import net.osmand.plus.Version; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.Comparator; -import java.util.List; -import java.util.Map; -import java.util.TreeMap; - -public class IndexItemCategory implements Comparable { - public final String name; - public final List items = new ArrayList<>(); - private final int order; - - public IndexItemCategory(String name, int order) { - this.name = name; - this.order = order; - } - - @Override - public int compareTo(@NonNull IndexItemCategory another) { - return order < another.order ? -1 : 1; - } - - public static List categorizeIndexItems(final OsmandApplication ctx, - Collection indexItems) { - boolean skipWiki = Version.isFreeVersion(ctx); - final Map cats = new TreeMap<>(); - for (IndexItem i : indexItems) { - int nameId = R.string.index_name_other; - int order = 0; - String lc = i.getFileName().toLowerCase(); - if (lc.endsWith(".voice.zip")) { - nameId = R.string.index_name_voice; - order = 1; - } else if (lc.contains(".ttsvoice.zip")) { - nameId = R.string.index_name_tts_voice; - order = 2; - } else if (lc.contains("_wiki_")) { - if(skipWiki) { - continue; - } - nameId = R.string.index_name_wiki; - order = 10; - } else if (lc.startsWith("us") || - (lc.contains("united states") && lc.startsWith("north-america")) ) { - nameId = R.string.index_name_us; - order = 31; - } else if (lc.startsWith("canada")) { - nameId = R.string.index_name_canada; - order = 32; - } else if (lc.contains("openmaps")) { - nameId = R.string.index_name_openmaps; - order = 90; - } else if (lc.contains("northamerica") || lc.contains("north-america")) { - nameId = R.string.index_name_north_america; - order = 30; - } else if (lc.contains("centralamerica") || lc.contains("central-america") - || lc.contains("caribbean")) { - nameId = R.string.index_name_central_america; - order = 40; - } else if (lc.contains("southamerica") || lc.contains("south-america")) { - nameId = R.string.index_name_south_america; - order = 45; - } else if ( lc.contains("germany")) { - nameId = R.string.index_name_germany; - order = 16; - } else if (lc.startsWith("france_")) { - nameId = R.string.index_name_france; - order = 17; - } else if (lc.startsWith("italy_")) { - nameId = R.string.index_name_italy; - order = 18; - } else if (lc.startsWith("gb_") || lc.startsWith("british")) { - nameId = R.string.index_name_gb; - order = 19; - } else if ( lc.contains("netherlands")) { - nameId = R.string.index_name_netherlands; - order = 20; - } else if (lc.contains("russia")) { - nameId = R.string.index_name_russia; - order = 25; - } else if (lc.contains("europe")) { - nameId = R.string.index_name_europe; - order = 15; - } else if (lc.contains("africa") && !lc.contains("_wiki_")) { - nameId = R.string.index_name_africa; - order = 80; - } else if (lc.contains("_asia")|| lc.startsWith("asia")) { - nameId = R.string.index_name_asia; - order = 50; - } else if (lc.contains("oceania") || lc.contains("australia")) { - nameId = R.string.index_name_oceania; - order = 70; - } else if (lc.contains("tour")) { - nameId = R.string.index_tours; - } - - String name = ctx.getString(nameId); - if (!cats.containsKey(name)) { - cats.put(name, new IndexItemCategory(name, order)); - } - cats.get(name).items.add(i); - } - ArrayList r = new ArrayList<>(cats.values()); - final Collator collator = OsmAndCollator.primaryCollator(); - for(IndexItemCategory ct : r) { - final OsmandRegions osmandRegions = ctx.getResourceManager().getOsmandRegions(); - Collections.sort(ct.items, new Comparator() { - @Override - public int compare(IndexItem lhs, IndexItem rhs) { - return collator.compare(lhs.getVisibleName(ctx, osmandRegions), - rhs.getVisibleName(ctx, osmandRegions)); - } - }); - } - Collections.sort(r); - return r; - } -} diff --git a/OsmAnd/src/net/osmand/plus/download/items/ItemsListBuilder.java b/OsmAnd/src/net/osmand/plus/download/items/ItemsListBuilder.java deleted file mode 100644 index 297b0ebe74..0000000000 --- a/OsmAnd/src/net/osmand/plus/download/items/ItemsListBuilder.java +++ /dev/null @@ -1,286 +0,0 @@ -package net.osmand.plus.download.items; - -import android.content.Context; - -import net.osmand.PlatformUtil; -import net.osmand.map.OsmandRegions; -import net.osmand.plus.OsmandApplication; -import net.osmand.plus.OsmandPlugin; -import net.osmand.plus.R; -import net.osmand.plus.WorldRegion; -import net.osmand.plus.download.DownloadActivityType; -import net.osmand.plus.download.IndexItem; -import net.osmand.plus.srtmplugin.SRTMPlugin; -import net.osmand.util.Algorithms; - -import java.util.Collections; -import java.util.Comparator; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; - -public class ItemsListBuilder { - - //public static final String WORLD_BASEMAP_KEY = "world_basemap.obf.zip"; - public static final String WORLD_SEAMARKS_KEY = "world_seamarks_basemap.obf.zip"; - - private Map> resourcesByRegions; - private List voiceRecItems; - private List voiceTTSItems; - - public static class ResourceItem { - - private String resourceId; - private String title; - - private IndexItem indexItem; - private WorldRegion worldRegion; - - public IndexItem getIndexItem() { - return indexItem; - } - - public WorldRegion getWorldRegion() { - return worldRegion; - } - - public String getResourceId() { - return resourceId; - } - - public void setResourceId(String resourceId) { - this.resourceId = resourceId; - } - - public String getTitle() { - return title; - } - - public void setTitle(String title) { - this.title = title; - } - - public ResourceItem(IndexItem indexItem, WorldRegion worldRegion) { - this.indexItem = indexItem; - this.worldRegion = worldRegion; - } - } - - class ResourceItemComparator implements Comparator { - @Override - public int compare(Object obj1, Object obj2) { - String str1; - String str2; - - if (obj1 instanceof WorldRegion) { - str1 = ((WorldRegion) obj1).getName(); - } else { - ResourceItem item = (ResourceItem) obj1; - str1 = item.title + item.getIndexItem().getType().getOrderIndex(); - } - - if (obj2 instanceof WorldRegion) { - str2 = ((WorldRegion) obj2).getName(); - } else { - ResourceItem item = (ResourceItem) obj2; - str2 = item.title + item.getIndexItem().getType().getOrderIndex(); - } - - return str1.compareTo(str2); - } - } - - public enum VoicePromptsType { - NONE, - RECORDED, - TTS - } - - private static final org.apache.commons.logging.Log LOG = PlatformUtil.getLog(ItemsListBuilder.class); - - private List regionMapItems; - private List allResourceItems; - private List allSubregionItems; - - private OsmandApplication app; - private WorldRegion region; - - private boolean srtmDisabled; - private boolean hasSrtm; - private boolean hasHillshade; - - public List getRegionMapItems() { - return regionMapItems; - } - - public List getAllResourceItems() { - return allResourceItems; - } - - public List getRegionsFromAllItems() { - List list = new LinkedList<>(); - for (Object obj : allResourceItems) { - if (obj instanceof WorldRegion) { - list.add((WorldRegion) obj); - } - } - return list; - } - - public static String getVoicePromtName(Context ctx, VoicePromptsType type) { - switch (type) { - case RECORDED: - return ctx.getResources().getString(R.string.index_name_voice); - case TTS: - return ctx.getResources().getString(R.string.index_name_tts_voice); - default: - return ""; - } - } - - public List getVoicePromptsItems(VoicePromptsType type) { - switch (type) { - case RECORDED: - return voiceRecItems; - case TTS: - return voiceTTSItems; - default: - return new LinkedList<>(); - } - } - - public boolean isVoicePromptsItemsEmpty(VoicePromptsType type) { - switch (type) { - case RECORDED: - return voiceRecItems.isEmpty(); - case TTS: - return voiceTTSItems.isEmpty(); - default: - return true; - } - } - - // FIXME - public ItemsListBuilder(OsmandApplication app, String regionId, Map> resourcesByRegions, - List voiceRecItems, List voiceTTSItems) { - this.app = app; - this.resourcesByRegions = resourcesByRegions; - this.voiceRecItems = voiceRecItems; - this.voiceTTSItems = voiceTTSItems; - - regionMapItems = new LinkedList<>(); - allResourceItems = new LinkedList<>(); - allSubregionItems = new LinkedList<>(); - - region = app.getWorldRegion().getRegionById(regionId); - } - - public ItemsListBuilder build() { - if (obtainDataAndItems()) { - return this; - } else { - return null; - } - } - - private boolean obtainDataAndItems() { - if (resourcesByRegions.isEmpty() || region == null) { - return false; - } - - collectSubregionsDataAndItems(); - collectResourcesDataAndItems(); - - return true; - } - - private void collectSubregionsDataAndItems() { - srtmDisabled = OsmandPlugin.getEnabledPlugin(SRTMPlugin.class) == null; - hasSrtm = false; - hasHillshade = false; - - // Collect all regions (and their parents) that have at least one - // resource available in repository or locally. - - allResourceItems.clear(); - allSubregionItems.clear(); - regionMapItems.clear(); - - for (WorldRegion subregion : region.getFlattenedSubregions()) { - if (subregion.getSuperregion() == region) { - if (subregion.getFlattenedSubregions().size() > 0) { - allSubregionItems.add(subregion); - } else { - collectSubregionItems(subregion); - } - } - } - } - - private void collectSubregionItems(WorldRegion region) { - Map regionResources = resourcesByRegions.get(region); - - if (regionResources == null) { - return; - } - - List regionMapArray = new LinkedList<>(); - List allResourcesArray = new LinkedList<>(); - - Context context = app.getApplicationContext(); - OsmandRegions osmandRegions = app.getRegions(); - - for (IndexItem indexItem : regionResources.values()) { - - String name = indexItem.getVisibleName(context, osmandRegions, false); - if (Algorithms.isEmpty(name)) { - continue; - } - - ResourceItem resItem = new ResourceItem(indexItem, region); - resItem.setResourceId(indexItem.getSimplifiedFileName()); - resItem.setTitle(name); - - if (region != this.region && srtmDisabled) { - if (indexItem.getType() == DownloadActivityType.SRTM_COUNTRY_FILE) { - if (hasSrtm) { - continue; - } else { - hasSrtm = true; - } - } else if (indexItem.getType() == DownloadActivityType.HILLSHADE_FILE) { - if (hasHillshade) { - continue; - } else { - hasHillshade = true; - } - } - } - - - if (region == this.region) { - regionMapArray.add(resItem); - } else { - allResourcesArray.add(resItem); - } - - } - - regionMapItems.addAll(regionMapArray); - - if (allResourcesArray.size() > 1) { - allSubregionItems.add(region); - } else { - allResourceItems.addAll(allResourcesArray); - } - } - - private void collectResourcesDataAndItems() { - collectSubregionItems(region); - - allResourceItems.addAll(allSubregionItems); - - Collections.sort(allResourceItems, new ResourceItemComparator()); - Collections.sort(regionMapItems, new ResourceItemComparator()); - } -} diff --git a/OsmAnd/src/net/osmand/plus/download/items/RegionItemsFragment.java b/OsmAnd/src/net/osmand/plus/download/items/RegionItemsFragment.java index 410a66c15c..b76f7a0b21 100644 --- a/OsmAnd/src/net/osmand/plus/download/items/RegionItemsFragment.java +++ b/OsmAnd/src/net/osmand/plus/download/items/RegionItemsFragment.java @@ -103,7 +103,6 @@ public class RegionItemsFragment extends OsmandExpandableListFragment { } } getMyActivity().startDownload(indexItem); - return true; } } @@ -163,7 +162,6 @@ public class RegionItemsFragment extends OsmandExpandableListFragment { private int groupInProgressPosition = -1; private int childInProgressPosition = -1; private int progress = -1; - private boolean isFinished; public RegionsItemsAdapter() { } From 972371baec4567b4d20b78f6415151a2d641ac57 Mon Sep 17 00:00:00 2001 From: Victor Shcherb Date: Sun, 18 Oct 2015 22:16:49 +0200 Subject: [PATCH 03/22] Rename download-ui package --- OsmAnd/.cproject | 6 ++---- .../net/osmand/plus/activities/LocalIndexHelper.java | 2 +- .../net/osmand/plus/download/DownloadActivity.java | 11 +++++++---- .../net/osmand/plus/download/DownloadResources.java | 10 +++++----- .../{ => ui}/ActiveDownloadsDialogFragment.java | 5 +++-- .../download/{items => ui}/DialogDismissListener.java | 2 +- .../plus/download/{items => ui}/ItemViewHolder.java | 2 +- .../plus/download/{ => ui}/LocalIndexesFragment.java | 6 ++++-- .../download/{items => ui}/RegionDialogFragment.java | 2 +- .../download/{items => ui}/RegionItemsFragment.java | 2 +- .../download/{items => ui}/SearchDialogFragment.java | 2 +- .../download/{items => ui}/SearchItemsFragment.java | 2 +- .../{items => ui}/TwoLineWithImagesViewHolder.java | 2 +- .../plus/download/{ => ui}/UpdatesIndexFragment.java | 6 ++++-- .../download/{items => ui}/VoiceDialogFragment.java | 5 ++--- .../download/{items => ui}/VoiceItemsFragment.java | 5 ++--- .../download/{items => ui}/WorldItemsFragment.java | 4 ++-- .../osmand/plus/myplaces/AvailableGPXFragment.java | 3 +-- 18 files changed, 40 insertions(+), 37 deletions(-) rename OsmAnd/src/net/osmand/plus/download/{ => ui}/ActiveDownloadsDialogFragment.java (97%) rename OsmAnd/src/net/osmand/plus/download/{items => ui}/DialogDismissListener.java (76%) rename OsmAnd/src/net/osmand/plus/download/{items => ui}/ItemViewHolder.java (99%) rename OsmAnd/src/net/osmand/plus/download/{ => ui}/LocalIndexesFragment.java (99%) rename OsmAnd/src/net/osmand/plus/download/{items => ui}/RegionDialogFragment.java (98%) rename OsmAnd/src/net/osmand/plus/download/{items => ui}/RegionItemsFragment.java (99%) rename OsmAnd/src/net/osmand/plus/download/{items => ui}/SearchDialogFragment.java (99%) rename OsmAnd/src/net/osmand/plus/download/{items => ui}/SearchItemsFragment.java (99%) rename OsmAnd/src/net/osmand/plus/download/{items => ui}/TwoLineWithImagesViewHolder.java (96%) rename OsmAnd/src/net/osmand/plus/download/{ => ui}/UpdatesIndexFragment.java (98%) rename OsmAnd/src/net/osmand/plus/download/{items => ui}/VoiceDialogFragment.java (96%) rename OsmAnd/src/net/osmand/plus/download/{items => ui}/VoiceItemsFragment.java (98%) rename OsmAnd/src/net/osmand/plus/download/{items => ui}/WorldItemsFragment.java (99%) diff --git a/OsmAnd/.cproject b/OsmAnd/.cproject index 5fc2b31392..c0b1be7a89 100644 --- a/OsmAnd/.cproject +++ b/OsmAnd/.cproject @@ -1,15 +1,13 @@ - - - + - + diff --git a/OsmAnd/src/net/osmand/plus/activities/LocalIndexHelper.java b/OsmAnd/src/net/osmand/plus/activities/LocalIndexHelper.java index 65a2d144f7..fd47b46c6d 100644 --- a/OsmAnd/src/net/osmand/plus/activities/LocalIndexHelper.java +++ b/OsmAnd/src/net/osmand/plus/activities/LocalIndexHelper.java @@ -16,7 +16,7 @@ import net.osmand.map.TileSourceManager; import net.osmand.plus.OsmandApplication; import net.osmand.plus.R; import net.osmand.plus.SQLiteTileSource; -import net.osmand.plus.download.LocalIndexesFragment.LoadLocalIndexTask; +import net.osmand.plus.download.ui.LocalIndexesFragment.LoadLocalIndexTask; import net.osmand.plus.voice.MediaCommandPlayerImpl; import net.osmand.plus.voice.TTSCommandPlayerImpl; import net.osmand.util.Algorithms; diff --git a/OsmAnd/src/net/osmand/plus/download/DownloadActivity.java b/OsmAnd/src/net/osmand/plus/download/DownloadActivity.java index 83514c15c0..e1fc04662f 100644 --- a/OsmAnd/src/net/osmand/plus/download/DownloadActivity.java +++ b/OsmAnd/src/net/osmand/plus/download/DownloadActivity.java @@ -20,10 +20,13 @@ import net.osmand.plus.activities.OsmandBaseExpandableListAdapter; import net.osmand.plus.activities.OsmandExpandableListFragment; import net.osmand.plus.activities.TabActivity; import net.osmand.plus.base.BasicProgressAsyncTask; -import net.osmand.plus.download.items.DialogDismissListener; -import net.osmand.plus.download.items.RegionItemsFragment; -import net.osmand.plus.download.items.SearchDialogFragment; -import net.osmand.plus.download.items.WorldItemsFragment; +import net.osmand.plus.download.ui.ActiveDownloadsDialogFragment; +import net.osmand.plus.download.ui.DialogDismissListener; +import net.osmand.plus.download.ui.LocalIndexesFragment; +import net.osmand.plus.download.ui.RegionItemsFragment; +import net.osmand.plus.download.ui.SearchDialogFragment; +import net.osmand.plus.download.ui.UpdatesIndexFragment; +import net.osmand.plus.download.ui.WorldItemsFragment; import net.osmand.plus.srtmplugin.SRTMPlugin; import net.osmand.plus.views.controls.PagerSlidingTabStrip; diff --git a/OsmAnd/src/net/osmand/plus/download/DownloadResources.java b/OsmAnd/src/net/osmand/plus/download/DownloadResources.java index 9fb9372724..48211c10e2 100644 --- a/OsmAnd/src/net/osmand/plus/download/DownloadResources.java +++ b/OsmAnd/src/net/osmand/plus/download/DownloadResources.java @@ -26,11 +26,11 @@ import net.osmand.plus.OsmandPlugin; import net.osmand.plus.R; import net.osmand.plus.WorldRegion; import net.osmand.plus.download.DownloadOsmandIndexesHelper.AssetIndexItem; -import net.osmand.plus.download.items.DownloadIndexes; -import net.osmand.plus.download.items.ItemsListBuilder; -import net.osmand.plus.download.items.ResourceItem; -import net.osmand.plus.download.items.ResourceItemComparator; -import net.osmand.plus.download.items.ItemsListBuilder.VoicePromptsType; +import net.osmand.plus.download.ui.DownloadIndexes; +import net.osmand.plus.download.ui.ItemsListBuilder; +import net.osmand.plus.download.ui.ResourceItem; +import net.osmand.plus.download.ui.ResourceItemComparator; +import net.osmand.plus.download.ui.ItemsListBuilder.VoicePromptsType; import net.osmand.plus.srtmplugin.SRTMPlugin; import net.osmand.util.Algorithms; diff --git a/OsmAnd/src/net/osmand/plus/download/ActiveDownloadsDialogFragment.java b/OsmAnd/src/net/osmand/plus/download/ui/ActiveDownloadsDialogFragment.java similarity index 97% rename from OsmAnd/src/net/osmand/plus/download/ActiveDownloadsDialogFragment.java rename to OsmAnd/src/net/osmand/plus/download/ui/ActiveDownloadsDialogFragment.java index 46bd280d65..12bdb5187e 100644 --- a/OsmAnd/src/net/osmand/plus/download/ActiveDownloadsDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/download/ui/ActiveDownloadsDialogFragment.java @@ -1,9 +1,10 @@ -package net.osmand.plus.download; +package net.osmand.plus.download.ui; import java.util.ArrayList; import net.osmand.plus.R; -import net.osmand.plus.download.items.TwoLineWithImagesViewHolder; +import net.osmand.plus.download.DownloadActivity; +import net.osmand.plus.download.IndexItem; import android.app.AlertDialog; import android.app.Dialog; import android.graphics.drawable.Drawable; diff --git a/OsmAnd/src/net/osmand/plus/download/items/DialogDismissListener.java b/OsmAnd/src/net/osmand/plus/download/ui/DialogDismissListener.java similarity index 76% rename from OsmAnd/src/net/osmand/plus/download/items/DialogDismissListener.java rename to OsmAnd/src/net/osmand/plus/download/ui/DialogDismissListener.java index ee42b19414..8a828dfadf 100644 --- a/OsmAnd/src/net/osmand/plus/download/items/DialogDismissListener.java +++ b/OsmAnd/src/net/osmand/plus/download/ui/DialogDismissListener.java @@ -1,4 +1,4 @@ -package net.osmand.plus.download.items; +package net.osmand.plus.download.ui; /** * Used to have smooth transition between dialogs diff --git a/OsmAnd/src/net/osmand/plus/download/items/ItemViewHolder.java b/OsmAnd/src/net/osmand/plus/download/ui/ItemViewHolder.java similarity index 99% rename from OsmAnd/src/net/osmand/plus/download/items/ItemViewHolder.java rename to OsmAnd/src/net/osmand/plus/download/ui/ItemViewHolder.java index 55105998e4..a35100c0c4 100644 --- a/OsmAnd/src/net/osmand/plus/download/items/ItemViewHolder.java +++ b/OsmAnd/src/net/osmand/plus/download/ui/ItemViewHolder.java @@ -1,4 +1,4 @@ -package net.osmand.plus.download.items; +package net.osmand.plus.download.ui; import android.content.Intent; import android.content.res.Resources; diff --git a/OsmAnd/src/net/osmand/plus/download/LocalIndexesFragment.java b/OsmAnd/src/net/osmand/plus/download/ui/LocalIndexesFragment.java similarity index 99% rename from OsmAnd/src/net/osmand/plus/download/LocalIndexesFragment.java rename to OsmAnd/src/net/osmand/plus/download/ui/LocalIndexesFragment.java index 7257759da7..e58d62ad28 100644 --- a/OsmAnd/src/net/osmand/plus/download/LocalIndexesFragment.java +++ b/OsmAnd/src/net/osmand/plus/download/ui/LocalIndexesFragment.java @@ -1,4 +1,4 @@ -package net.osmand.plus.download; +package net.osmand.plus.download.ui; import android.app.Activity; import android.app.AlertDialog; @@ -33,7 +33,6 @@ import android.widget.ImageButton; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; - import net.osmand.IProgress; import net.osmand.IndexConstants; import net.osmand.access.AccessibleToast; @@ -49,6 +48,9 @@ import net.osmand.plus.activities.LocalIndexInfo; import net.osmand.plus.activities.OsmandBaseExpandableListAdapter; import net.osmand.plus.activities.OsmandExpandableListFragment; import net.osmand.plus.dialogs.DirectionsDialogs; +import net.osmand.plus.download.DownloadActivity; +import net.osmand.plus.download.DownloadActivityType; +import net.osmand.plus.download.IndexItem; import net.osmand.plus.helpers.FileNameTranslationHelper; import net.osmand.plus.resources.IncrementalChangesManager; import net.osmand.plus.resources.IncrementalChangesManager.IncrementalUpdate; diff --git a/OsmAnd/src/net/osmand/plus/download/items/RegionDialogFragment.java b/OsmAnd/src/net/osmand/plus/download/ui/RegionDialogFragment.java similarity index 98% rename from OsmAnd/src/net/osmand/plus/download/items/RegionDialogFragment.java rename to OsmAnd/src/net/osmand/plus/download/ui/RegionDialogFragment.java index b399ee9d96..d1b5e19c46 100644 --- a/OsmAnd/src/net/osmand/plus/download/items/RegionDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/download/ui/RegionDialogFragment.java @@ -1,4 +1,4 @@ -package net.osmand.plus.download.items; +package net.osmand.plus.download.ui; import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandSettings; diff --git a/OsmAnd/src/net/osmand/plus/download/items/RegionItemsFragment.java b/OsmAnd/src/net/osmand/plus/download/ui/RegionItemsFragment.java similarity index 99% rename from OsmAnd/src/net/osmand/plus/download/items/RegionItemsFragment.java rename to OsmAnd/src/net/osmand/plus/download/ui/RegionItemsFragment.java index b76f7a0b21..2660f80a75 100644 --- a/OsmAnd/src/net/osmand/plus/download/items/RegionItemsFragment.java +++ b/OsmAnd/src/net/osmand/plus/download/ui/RegionItemsFragment.java @@ -1,4 +1,4 @@ -package net.osmand.plus.download.items; +package net.osmand.plus.download.ui; import java.util.ArrayList; import java.util.LinkedHashMap; diff --git a/OsmAnd/src/net/osmand/plus/download/items/SearchDialogFragment.java b/OsmAnd/src/net/osmand/plus/download/ui/SearchDialogFragment.java similarity index 99% rename from OsmAnd/src/net/osmand/plus/download/items/SearchDialogFragment.java rename to OsmAnd/src/net/osmand/plus/download/ui/SearchDialogFragment.java index 2c7650f734..206d27a4aa 100644 --- a/OsmAnd/src/net/osmand/plus/download/items/SearchDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/download/ui/SearchDialogFragment.java @@ -1,4 +1,4 @@ -package net.osmand.plus.download.items; +package net.osmand.plus.download.ui; import android.os.Bundle; import android.support.v4.app.DialogFragment; diff --git a/OsmAnd/src/net/osmand/plus/download/items/SearchItemsFragment.java b/OsmAnd/src/net/osmand/plus/download/ui/SearchItemsFragment.java similarity index 99% rename from OsmAnd/src/net/osmand/plus/download/items/SearchItemsFragment.java rename to OsmAnd/src/net/osmand/plus/download/ui/SearchItemsFragment.java index 460ca1dc3e..d48eb33ae5 100644 --- a/OsmAnd/src/net/osmand/plus/download/items/SearchItemsFragment.java +++ b/OsmAnd/src/net/osmand/plus/download/ui/SearchItemsFragment.java @@ -1,4 +1,4 @@ -package net.osmand.plus.download.items; +package net.osmand.plus.download.ui; import java.util.ArrayList; import java.util.Collections; diff --git a/OsmAnd/src/net/osmand/plus/download/items/TwoLineWithImagesViewHolder.java b/OsmAnd/src/net/osmand/plus/download/ui/TwoLineWithImagesViewHolder.java similarity index 96% rename from OsmAnd/src/net/osmand/plus/download/items/TwoLineWithImagesViewHolder.java rename to OsmAnd/src/net/osmand/plus/download/ui/TwoLineWithImagesViewHolder.java index 1a3d57c2d8..8ee416b7b5 100644 --- a/OsmAnd/src/net/osmand/plus/download/items/TwoLineWithImagesViewHolder.java +++ b/OsmAnd/src/net/osmand/plus/download/ui/TwoLineWithImagesViewHolder.java @@ -1,4 +1,4 @@ -package net.osmand.plus.download.items; +package net.osmand.plus.download.ui; import net.osmand.plus.R; import net.osmand.plus.download.DownloadActivity; diff --git a/OsmAnd/src/net/osmand/plus/download/UpdatesIndexFragment.java b/OsmAnd/src/net/osmand/plus/download/ui/UpdatesIndexFragment.java similarity index 98% rename from OsmAnd/src/net/osmand/plus/download/UpdatesIndexFragment.java rename to OsmAnd/src/net/osmand/plus/download/ui/UpdatesIndexFragment.java index b98a3b8cbd..87026bb9da 100644 --- a/OsmAnd/src/net/osmand/plus/download/UpdatesIndexFragment.java +++ b/OsmAnd/src/net/osmand/plus/download/ui/UpdatesIndexFragment.java @@ -1,4 +1,4 @@ -package net.osmand.plus.download; +package net.osmand.plus.download.ui; import java.text.ParseException; import java.util.ArrayList; @@ -12,7 +12,9 @@ import net.osmand.map.OsmandRegions; import net.osmand.plus.OsmandApplication; import net.osmand.plus.R; import net.osmand.plus.activities.OsmAndListFragment; -import net.osmand.plus.download.items.TwoLineWithImagesViewHolder; +import net.osmand.plus.download.BaseDownloadActivity; +import net.osmand.plus.download.DownloadActivity; +import net.osmand.plus.download.IndexItem; import org.apache.commons.logging.Log; diff --git a/OsmAnd/src/net/osmand/plus/download/items/VoiceDialogFragment.java b/OsmAnd/src/net/osmand/plus/download/ui/VoiceDialogFragment.java similarity index 96% rename from OsmAnd/src/net/osmand/plus/download/items/VoiceDialogFragment.java rename to OsmAnd/src/net/osmand/plus/download/ui/VoiceDialogFragment.java index e7a20a3642..10ef409e38 100644 --- a/OsmAnd/src/net/osmand/plus/download/items/VoiceDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/download/ui/VoiceDialogFragment.java @@ -1,4 +1,4 @@ -package net.osmand.plus.download.items; +package net.osmand.plus.download.ui; import android.os.Bundle; import android.support.v4.app.DialogFragment; @@ -7,13 +7,12 @@ import android.support.v7.widget.Toolbar; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; - import net.osmand.PlatformUtil; import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandSettings; import net.osmand.plus.R; import net.osmand.plus.download.DownloadActivity; -import net.osmand.plus.download.items.ItemsListBuilder.VoicePromptsType; +import net.osmand.plus.download.ui.ItemsListBuilder.VoicePromptsType; import org.apache.commons.logging.Log; diff --git a/OsmAnd/src/net/osmand/plus/download/items/VoiceItemsFragment.java b/OsmAnd/src/net/osmand/plus/download/ui/VoiceItemsFragment.java similarity index 98% rename from OsmAnd/src/net/osmand/plus/download/items/VoiceItemsFragment.java rename to OsmAnd/src/net/osmand/plus/download/ui/VoiceItemsFragment.java index 999637ca28..89f9fc5d2c 100644 --- a/OsmAnd/src/net/osmand/plus/download/items/VoiceItemsFragment.java +++ b/OsmAnd/src/net/osmand/plus/download/ui/VoiceItemsFragment.java @@ -1,4 +1,4 @@ -package net.osmand.plus.download.items; +package net.osmand.plus.download.ui; import android.content.Context; import android.content.res.Resources; @@ -10,7 +10,6 @@ import android.view.View; import android.view.ViewGroup; import android.widget.ExpandableListView; import android.widget.TextView; - import net.osmand.PlatformUtil; import net.osmand.plus.OsmandApplication; import net.osmand.plus.R; @@ -19,7 +18,7 @@ import net.osmand.plus.activities.OsmandExpandableListFragment; import net.osmand.plus.download.BaseDownloadActivity; import net.osmand.plus.download.DownloadActivity; import net.osmand.plus.download.IndexItem; -import net.osmand.plus.download.items.ItemsListBuilder.VoicePromptsType; +import net.osmand.plus.download.ui.ItemsListBuilder.VoicePromptsType; import org.apache.commons.logging.Log; diff --git a/OsmAnd/src/net/osmand/plus/download/items/WorldItemsFragment.java b/OsmAnd/src/net/osmand/plus/download/ui/WorldItemsFragment.java similarity index 99% rename from OsmAnd/src/net/osmand/plus/download/items/WorldItemsFragment.java rename to OsmAnd/src/net/osmand/plus/download/ui/WorldItemsFragment.java index 2b67857ae4..b5548c87cc 100644 --- a/OsmAnd/src/net/osmand/plus/download/items/WorldItemsFragment.java +++ b/OsmAnd/src/net/osmand/plus/download/ui/WorldItemsFragment.java @@ -1,4 +1,4 @@ -package net.osmand.plus.download.items; +package net.osmand.plus.download.ui; import java.util.ArrayList; import java.util.LinkedHashMap; @@ -14,7 +14,7 @@ import net.osmand.plus.activities.OsmandExpandableListFragment; import net.osmand.plus.download.BaseDownloadActivity; import net.osmand.plus.download.DownloadActivity; import net.osmand.plus.download.IndexItem; -import net.osmand.plus.download.items.ItemsListBuilder.VoicePromptsType; +import net.osmand.plus.download.ui.ItemsListBuilder.VoicePromptsType; import android.content.Context; import android.content.res.Resources; import android.content.res.TypedArray; diff --git a/OsmAnd/src/net/osmand/plus/myplaces/AvailableGPXFragment.java b/OsmAnd/src/net/osmand/plus/myplaces/AvailableGPXFragment.java index c3931b7f77..b05f177d30 100644 --- a/OsmAnd/src/net/osmand/plus/myplaces/AvailableGPXFragment.java +++ b/OsmAnd/src/net/osmand/plus/myplaces/AvailableGPXFragment.java @@ -32,7 +32,6 @@ import android.widget.ImageButton; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; - import net.osmand.IndexConstants; import net.osmand.access.AccessibleToast; import net.osmand.plus.ContextMenuAdapter; @@ -56,7 +55,7 @@ import net.osmand.plus.activities.OsmandExpandableListFragment; import net.osmand.plus.activities.SavingTrackHelper; import net.osmand.plus.activities.TrackActivity; import net.osmand.plus.dialogs.DirectionsDialogs; -import net.osmand.plus.download.LocalIndexesFragment; +import net.osmand.plus.download.ui.LocalIndexesFragment; import net.osmand.plus.helpers.AndroidUiHelper; import net.osmand.plus.monitoring.OsmandMonitoringPlugin; import net.osmand.plus.osmedit.OsmEditingPlugin; From a172c48d08fb3a01cbd431178beeadc71be18ed6 Mon Sep 17 00:00:00 2001 From: Victor Shcherb Date: Sun, 18 Oct 2015 23:39:10 +0200 Subject: [PATCH 04/22] Finalize data package refactoring --- OsmAnd/src/net/osmand/plus/WorldRegion.java | 87 ++-- .../plus/download/DownloadActivity.java | 8 - .../download/DownloadOsmandIndexesHelper.java | 6 +- .../plus/download/DownloadResourceGroup.java | 55 ++- .../plus/download/DownloadResources.java | 376 ++++-------------- .../osmand/plus/download/DownloadTracker.java | 1 - .../net/osmand/plus/download/IndexItem.java | 5 - .../plus/download/ui/ItemViewHolder.java | 2 +- 8 files changed, 159 insertions(+), 381 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/WorldRegion.java b/OsmAnd/src/net/osmand/plus/WorldRegion.java index 888150d54b..048c218e84 100644 --- a/OsmAnd/src/net/osmand/plus/WorldRegion.java +++ b/OsmAnd/src/net/osmand/plus/WorldRegion.java @@ -1,18 +1,16 @@ package net.osmand.plus; -import android.content.res.Resources; - -import net.osmand.PlatformUtil; -import net.osmand.map.OsmandRegions; -import net.osmand.plus.download.DownloadActivityType; - import java.util.HashMap; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Map.Entry; -import java.util.Set; + +import net.osmand.PlatformUtil; +import net.osmand.map.OsmandRegions; +import net.osmand.util.Algorithms; +import android.content.res.Resources; public class WorldRegion { @@ -24,16 +22,15 @@ public class WorldRegion { public static final String NORTH_AMERICA_REGION_ID = "northamerica"; public static final String RUSSIA_REGION_ID = "russia"; public static final String SOUTH_AMERICA_REGION_ID = "southamerica"; + public static final String WORLD = "world"; private static final org.apache.commons.logging.Log LOG = PlatformUtil.getLog(WorldRegion.class); // Region data private String regionId; - private String downloadsIdPrefix; + private String downloadsId; private String name; - private Set resourceTypes; - // Hierarchy private WorldRegion superregion; private List subregions; @@ -45,22 +42,14 @@ public class WorldRegion { return regionId; } - public String getDownloadsIdPrefix() { - return downloadsIdPrefix; + public String getDownloadsId() { + return downloadsId; } public String getName() { return name; } - public Set getResourceTypes() { - return resourceTypes; - } - - public void setResourceTypes(Set resourceTypes) { - this.resourceTypes = resourceTypes; - } - public WorldRegion getSuperregion() { return superregion; } @@ -96,7 +85,7 @@ public class WorldRegion { public void initWorld() { regionId = ""; - downloadsIdPrefix = "world_"; + downloadsId= WORLD; name = ""; superregion = null; } @@ -105,9 +94,9 @@ public class WorldRegion { this.regionId = regionId; String downloadName = osmandRegions.getDownloadName(regionId); if (downloadName != null) { - downloadsIdPrefix = downloadName.toLowerCase() + "."; + downloadsId = downloadName.toLowerCase(); } else { - this.downloadsIdPrefix = regionId.toLowerCase() + "."; + this.downloadsId = regionId.toLowerCase(); } if (name != null) { this.name = name; @@ -120,24 +109,10 @@ public class WorldRegion { return this; } - private WorldRegion init(String regionId, OsmandRegions osmandRegions) { - this.regionId = regionId; - String downloadName = osmandRegions.getDownloadName(regionId); - if (downloadName != null) { - downloadsIdPrefix = downloadName.toLowerCase() + "."; - } else { - this.downloadsIdPrefix = regionId.toLowerCase() + "."; - } - this.name = osmandRegions.getLocaleNameByFullName(regionId, false); - if (this.name == null) { - this.name = capitalize(regionId.replace('_', ' ')); - } - return this; - } private WorldRegion init(String regionId, String name) { this.regionId = regionId; - this.downloadsIdPrefix = regionId.toLowerCase() + "."; + this.downloadsId = regionId.toLowerCase() ; this.name = name; return this; } @@ -149,22 +124,20 @@ public class WorldRegion { } private void propagateSubregionToFlattenedHierarchy(WorldRegion subregion) { - flattenedSubregions.add(subregion); if (superregion != null) { superregion.propagateSubregionToFlattenedHierarchy(subregion); + } else { + flattenedSubregions.add(subregion); } } public void loadWorldRegions(OsmandApplication app) { OsmandRegions osmandRegions = app.getRegions(); - Map loadedItems = osmandRegions.getFullNamesToLowercaseCopy(); if (loadedItems.size() == 0) { return; } - HashMap regionsLookupTable = new HashMap<>(loadedItems.size()); - // Create main regions Resources res = app.getResources(); @@ -208,10 +181,9 @@ public class WorldRegion { addSubregion(southAmericaRegion); regionsLookupTable.put(southAmericaRegion.regionId, southAmericaRegion); - // Process remaining regions + // Process all regions for (; ; ) { int processedRegions = 0; - Iterator> iterator = loadedItems.entrySet().iterator(); while (iterator.hasNext()) { String regionId = iterator.next().getKey(); @@ -226,7 +198,7 @@ public class WorldRegion { continue; } - WorldRegion newRegion = new WorldRegion().init(regionId, osmandRegions); + WorldRegion newRegion = new WorldRegion().init(regionId, osmandRegions, null); parentRegion.addSubregion(newRegion); regionsLookupTable.put(newRegion.regionId, newRegion); @@ -240,21 +212,18 @@ public class WorldRegion { break; } - LOG.warn("Found orphaned regions: " + loadedItems.size()); - for (String regionId : loadedItems.keySet()) { - LOG.warn("FullName = " + regionId + " parent=" + osmandRegions.getParentFullName(regionId)); + if (loadedItems.size() > 0) { + LOG.warn("Found orphaned regions: " + loadedItems.size()); + for (String regionId : loadedItems.keySet()) { + LOG.warn("FullName = " + regionId + " parent=" + osmandRegions.getParentFullName(regionId)); + } } } - private static WorldRegion createRegionAs(String regionId, Map loadedItems, OsmandRegions osmandRegions, String localizedName) { - WorldRegion worldRegion; - boolean hasRegion = loadedItems.containsKey(regionId); - if (hasRegion) { - worldRegion = new WorldRegion().init(regionId, osmandRegions, localizedName); - loadedItems.remove(regionId); - } else { - worldRegion = new WorldRegion().init(regionId, localizedName); - } + private static WorldRegion createRegionAs(String regionId, Map loadedItems, + OsmandRegions osmandRegions, String localizedName) { + WorldRegion worldRegion = new WorldRegion().init(regionId, osmandRegions, localizedName); + loadedItems.remove(regionId); return worldRegion; } @@ -262,10 +231,10 @@ public class WorldRegion { String[] words = s.split(" "); if (words[0].length() > 0) { StringBuilder sb = new StringBuilder(); - sb.append(Character.toUpperCase(words[0].charAt(0))).append(words[0].subSequence(1, words[0].length()).toString().toLowerCase()); + sb.append(Algorithms.capitalizeFirstLetterAndLowercase(words[0])); for (int i = 1; i < words.length; i++) { sb.append(" "); - sb.append(Character.toUpperCase(words[i].charAt(0))).append(words[i].subSequence(1, words[i].length()).toString().toLowerCase()); + sb.append(Algorithms.capitalizeFirstLetterAndLowercase(words[i])); } return sb.toString(); } else { diff --git a/OsmAnd/src/net/osmand/plus/download/DownloadActivity.java b/OsmAnd/src/net/osmand/plus/download/DownloadActivity.java index e1fc04662f..eb7e7b64f3 100644 --- a/OsmAnd/src/net/osmand/plus/download/DownloadActivity.java +++ b/OsmAnd/src/net/osmand/plus/download/DownloadActivity.java @@ -6,17 +6,13 @@ import java.text.MessageFormat; import java.util.ArrayList; import java.util.List; import java.util.Locale; -import java.util.Map; -import net.osmand.PlatformUtil; import net.osmand.plus.OsmandApplication; -import net.osmand.plus.OsmandPlugin; import net.osmand.plus.OsmandSettings; import net.osmand.plus.R; import net.osmand.plus.Version; import net.osmand.plus.activities.LocalIndexInfo; import net.osmand.plus.activities.OsmAndListFragment; -import net.osmand.plus.activities.OsmandBaseExpandableListAdapter; import net.osmand.plus.activities.OsmandExpandableListFragment; import net.osmand.plus.activities.TabActivity; import net.osmand.plus.base.BasicProgressAsyncTask; @@ -27,11 +23,7 @@ import net.osmand.plus.download.ui.RegionItemsFragment; import net.osmand.plus.download.ui.SearchDialogFragment; import net.osmand.plus.download.ui.UpdatesIndexFragment; import net.osmand.plus.download.ui.WorldItemsFragment; -import net.osmand.plus.srtmplugin.SRTMPlugin; import net.osmand.plus.views.controls.PagerSlidingTabStrip; - -import org.apache.commons.logging.Log; - import android.content.ActivityNotFoundException; import android.content.Intent; import android.net.Uri; diff --git a/OsmAnd/src/net/osmand/plus/download/DownloadOsmandIndexesHelper.java b/OsmAnd/src/net/osmand/plus/download/DownloadOsmandIndexesHelper.java index 513d9da551..5f061d2283 100644 --- a/OsmAnd/src/net/osmand/plus/download/DownloadOsmandIndexesHelper.java +++ b/OsmAnd/src/net/osmand/plus/download/DownloadOsmandIndexesHelper.java @@ -12,19 +12,17 @@ import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.zip.GZIPInputStream; - import net.osmand.AndroidUtils; import net.osmand.IndexConstants; import net.osmand.PlatformUtil; import net.osmand.osm.io.NetworkUtils; import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandSettings; - import org.apache.commons.logging.Log; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserException; import org.xmlpull.v1.XmlPullParserFactory; - +import android.annotation.SuppressLint; import android.content.Context; import android.content.pm.ApplicationInfo; import android.content.pm.PackageManager; @@ -72,6 +70,7 @@ public class DownloadOsmandIndexesHelper { this.mapversion = mapversion; } + @SuppressLint("DefaultLocale") public void add(IndexItem indexItem) { indexFiles.add(indexItem); if(indexItem.getFileName().toLowerCase().startsWith("world_basemap")) { @@ -142,7 +141,6 @@ public class DownloadOsmandIndexesHelper { OsmandSettings settings) { try { String ext = DownloadActivityType.addVersionToExt(IndexConstants.TTSVOICE_INDEX_EXT_ZIP, IndexConstants.TTSVOICE_VERSION); - String extvoice = DownloadActivityType.addVersionToExt(IndexConstants.VOICE_INDEX_EXT_ZIP, IndexConstants.VOICE_VERSION); File voicePath = settings.getContext().getAppPath(IndexConstants.VOICE_INDEX_DIR); // list = amanager.list("voice"); String date = ""; diff --git a/OsmAnd/src/net/osmand/plus/download/DownloadResourceGroup.java b/OsmAnd/src/net/osmand/plus/download/DownloadResourceGroup.java index f7bed5b604..80e829b864 100644 --- a/OsmAnd/src/net/osmand/plus/download/DownloadResourceGroup.java +++ b/OsmAnd/src/net/osmand/plus/download/DownloadResourceGroup.java @@ -1,8 +1,14 @@ package net.osmand.plus.download; import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.Iterator; import java.util.List; +import net.osmand.OsmAndCollator; +import net.osmand.map.OsmandRegions; +import net.osmand.plus.OsmandApplication; import net.osmand.plus.WorldRegion; public class DownloadResourceGroup { @@ -15,9 +21,12 @@ public class DownloadResourceGroup { protected final String id; protected WorldRegion region; + public static final String REGION_MAPS_ID = "maps"; public enum DownloadResourceGroupType { - WORLD, VOICE, WORLD_MAPS, REGION +// return ctx.getResources().getString(R.string.index_name_voice); +// return ctx.getResources().getString(R.string.index_name_tts_voice); + WORLD, VOICE_REC, VOICE_TTS, WORLD_MAPS, REGION, REGION_MAPS } @@ -34,6 +43,50 @@ public class DownloadResourceGroup { this.type = type; this.parentGroup = parentGroup; } + + public void trimEmptyGroups() { + if(groups != null) { + for(DownloadResourceGroup gr : groups) { + gr.trimEmptyGroups(); + } + Iterator gr = groups.iterator(); + while(gr.hasNext()) { + DownloadResourceGroup group = gr.next(); + if(group.isEmpty()) { + gr.remove(); + } + } + } + + } + + public void addGroup(DownloadResourceGroup g) { + groups.add(g); + if (g.individualResources != null) { + final net.osmand.Collator collator = OsmAndCollator.primaryCollator(); + final OsmandApplication app = getRoot().app; + final OsmandRegions osmandRegions = app.getRegions(); + Collections.sort(g.individualResources, new Comparator() { + @Override + public int compare(IndexItem lhs, IndexItem rhs) { + return collator.compare(lhs.getVisibleName(app.getApplicationContext(), osmandRegions), + rhs.getVisibleName(app.getApplicationContext(), osmandRegions)); + } + }); + } + } + + public void addItem(IndexItem i) { + individualResources.add(i); + } + + public boolean isEmpty() { + return isEmpty(individualResources) && isEmpty(groups); + } + + private boolean isEmpty(List l) { + return l == null || l.isEmpty(); + } public String getGroupId() { return id; diff --git a/OsmAnd/src/net/osmand/plus/download/DownloadResources.java b/OsmAnd/src/net/osmand/plus/download/DownloadResources.java index 48211c10e2..a00fac3645 100644 --- a/OsmAnd/src/net/osmand/plus/download/DownloadResources.java +++ b/OsmAnd/src/net/osmand/plus/download/DownloadResources.java @@ -4,35 +4,16 @@ import java.io.File; import java.io.FilenameFilter; import java.io.IOException; import java.io.InputStream; -import java.text.Collator; import java.util.ArrayList; -import java.util.Collections; -import java.util.Comparator; -import java.util.HashMap; import java.util.LinkedHashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; -import java.util.Set; -import java.util.TreeSet; -import android.content.Context; import net.osmand.IndexConstants; -import net.osmand.OsmAndCollator; -import net.osmand.PlatformUtil; -import net.osmand.map.OsmandRegions; import net.osmand.plus.OsmandApplication; -import net.osmand.plus.OsmandPlugin; -import net.osmand.plus.R; import net.osmand.plus.WorldRegion; import net.osmand.plus.download.DownloadOsmandIndexesHelper.AssetIndexItem; -import net.osmand.plus.download.ui.DownloadIndexes; -import net.osmand.plus.download.ui.ItemsListBuilder; -import net.osmand.plus.download.ui.ResourceItem; -import net.osmand.plus.download.ui.ResourceItemComparator; -import net.osmand.plus.download.ui.ItemsListBuilder.VoicePromptsType; -import net.osmand.plus.srtmplugin.SRTMPlugin; -import net.osmand.util.Algorithms; public class DownloadResources extends DownloadResourceGroup { public boolean isDownloadedFromInternet = false; @@ -40,8 +21,12 @@ public class DownloadResources extends DownloadResourceGroup { public OsmandApplication app; private Map indexFileNames = new LinkedHashMap<>(); private Map indexActivatedFileNames = new LinkedHashMap<>(); + private List rawResources; private List itemsToUpdate = new ArrayList<>(); - + //public static final String WORLD_BASEMAP_KEY = "world_basemap.obf.zip"; + public static final String WORLD_SEAMARKS_KEY = "world_seamarks_basemap.obf.zip"; + + public DownloadResources(OsmandApplication app) { super(null, DownloadResourceGroupType.WORLD, "", false); this.region = app.getWorldRegion(); @@ -63,13 +48,15 @@ public class DownloadResources extends DownloadResourceGroup { prepareFilesToUpdate(); } - public boolean checkIfItemOutdated(IndexItem item) { + public boolean checkIfItemOutdated(IndexItem item, java.text.DateFormat format) { boolean outdated = false; 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(indexactivateddate == null && indexfilesdate == null) { + return outdated; + } + String date = item.getDate(format); if (date != null && !date.equals(indexactivateddate) && !date.equals(indexfilesdate)) { if ((item.getType() == DownloadActivityType.NORMAL_FILE && !item.extra) || item.getType() == DownloadActivityType.ROADS_FILE @@ -160,16 +147,13 @@ public class DownloadResources extends DownloadResourceGroup { return file; } - - - ////////// FIXME //////////// - private void prepareFilesToUpdate() { - List filtered = getCachedIndexFiles(); + List filtered = rawResources; if (filtered != null) { itemsToUpdate.clear(); + java.text.DateFormat format = app.getResourceManager().getDateFormat(); for (IndexItem item : filtered) { - boolean outdated = checkIfItemOutdated(item); + boolean outdated = checkIfItemOutdated(item, format); // include only activated files here if (outdated && indexActivatedFileNames.containsKey(item.getTargetFileName())) { itemsToUpdate.add(item); @@ -178,287 +162,75 @@ public class DownloadResources extends DownloadResourceGroup { } } - private void processRegion(List resourcesInRepository, DownloadResources di, - boolean processVoiceFiles, WorldRegion region) { - String downloadsIdPrefix = region.getDownloadsIdPrefix(); - Map regionResources = new HashMap<>(); - Set typesSet = new TreeSet<>(new Comparator() { - @Override - public int compare(DownloadActivityType dat1, DownloadActivityType dat2) { - return dat1.getTag().compareTo(dat2.getTag()); - } - }); - for (IndexItem resource : resourcesInRepository) { - if (processVoiceFiles) { - if (resource.getSimplifiedFileName().endsWith(".voice.zip")) { - voiceRecItems.add(resource); - continue; - } else if (resource.getSimplifiedFileName().contains(".ttsvoice.zip")) { - voiceTTSItems.add(resource); - continue; - } - } - if (!resource.getSimplifiedFileName().startsWith(downloadsIdPrefix)) { - continue; - } - - if (resource.type == DownloadActivityType.NORMAL_FILE - || resource.type == DownloadActivityType.ROADS_FILE) { - if (resource.isAlreadyDownloaded(indexFileNames)) { - region.processNewMapState(checkIfItemOutdated(resource) - ? WorldRegion.MapState.OUTDATED : WorldRegion.MapState.DOWNLOADED); - } else { - region.processNewMapState(WorldRegion.MapState.NOT_DOWNLOADED); - } - } - typesSet.add(resource.getType()); - regionResources.put(resource.getSimplifiedFileName(), resource); - } - - if (region.getSuperregion() != null && region.getSuperregion().getSuperregion() != app.getWorldRegion()) { - if (region.getSuperregion().getResourceTypes() == null) { - region.getSuperregion().setResourceTypes(typesSet); - } else { - region.getSuperregion().getResourceTypes().addAll(typesSet); - } - } - - region.setResourceTypes(typesSet); - resourcesByRegions.put(region, regionResources); - } - protected boolean prepareData(List resources) { - for (WorldRegion region : app.getWorldRegion().getFlattenedSubregions()) { - processRegion(resourcesInRepository, di, false, region); + this.rawResources = resources; + DownloadResourceGroup voiceRec = new DownloadResourceGroup(this, DownloadResourceGroupType.VOICE_REC, "voice_rec", true); + DownloadResourceGroup voiceTTS = new DownloadResourceGroup(this, DownloadResourceGroupType.VOICE_TTS, "voice_tts", true); + DownloadResourceGroup worldMaps = new DownloadResourceGroup(this, DownloadResourceGroupType.WORLD_MAPS, "world", true); + Map > groupByRegion = new LinkedHashMap>(); + + Map downloadIdForRegion = new LinkedHashMap(); + for(WorldRegion wg : region.getFlattenedSubregions()) { + downloadIdForRegion.put(wg.getDownloadsId(), wg); } - processRegion(resourcesInRepository, di, true, app.getWorldRegion()); - - final net.osmand.Collator collator = OsmAndCollator.primaryCollator(); - final OsmandRegions osmandRegions = app.getRegions(); - - Collections.sort(di.voiceRecItems, new Comparator() { - @Override - public int compare(IndexItem lhs, IndexItem rhs) { - return collator.compare(lhs.getVisibleName(app.getApplicationContext(), osmandRegions), - rhs.getVisibleName(app.getApplicationContext(), osmandRegions)); + + for (IndexItem ii : resources) { + if (ii.getType() == DownloadActivityType.VOICE_FILE) { + if (ii.getFileName().endsWith(IndexConstants.TTSVOICE_INDEX_EXT_ZIP)) { + voiceTTS.addItem(ii); + } else { + voiceRec.addItem(ii); + } } - }); - - Collections.sort(di.voiceTTSItems, new Comparator() { - @Override - public int compare(IndexItem lhs, IndexItem rhs) { - return collator.compare(lhs.getVisibleName(app.getApplicationContext(), osmandRegions), - rhs.getVisibleName(app.getApplicationContext(), osmandRegions)); + String basename = ii.getBasename().toLowerCase(); + WorldRegion wg = downloadIdForRegion.get(basename); + if (wg != null) { + if (!groupByRegion.containsKey(wg)) { + groupByRegion.put(wg, new ArrayList()); + } + groupByRegion.get(wg).add(ii); + } else { + worldMaps.addItem(ii); } - }); + } + LinkedList queue = new LinkedList(); + LinkedList parent = new LinkedList(); + for(WorldRegion rg : region.getSubregions()) { + queue.add(rg); + parent.add(this); + } + while(!queue.isEmpty()) { + WorldRegion reg = queue.pollFirst(); + DownloadResourceGroup parentGroup = parent.pollFirst(); + List subregions = reg.getSubregions(); + DownloadResourceGroup mainGrp = new DownloadResourceGroup(parentGroup, DownloadResourceGroupType.REGION, reg.getRegionId(), false); + parentGroup.addGroup(mainGrp); + List list = groupByRegion.get(reg); + if(list != null) { + DownloadResourceGroup flatFiles = new DownloadResourceGroup(parentGroup, DownloadResourceGroupType.REGION_MAPS, REGION_MAPS_ID, true); + for(IndexItem ii : list) { + flatFiles.addItem(ii); + } + mainGrp.addGroup(flatFiles); + } + // add to processing queue + for(WorldRegion rg : subregions) { + queue.add(rg); + parent.add(mainGrp); + } + } + // Possible improvements + // 1. if there is no subregions no need to create resource group REGIONS_MAPS - objection raise diversity and there is no value + // 2. if there is no subregions and there only 1 index item it could be merged to the level up - objection there is no such maps + // 3. if hillshade/srtm is disabled, all maps from inner level could be combined into 1 + addGroup(worldMaps); + addGroup(voiceTTS); + addGroup(voiceRec); + trimEmptyGroups(); initAlreadyLoadedFiles(); return true; } - public class ItemsListBuilder { - - //public static final String WORLD_BASEMAP_KEY = "world_basemap.obf.zip"; - public static final String WORLD_SEAMARKS_KEY = "world_seamarks_basemap.obf.zip"; - private static final org.apache.commons.logging.Log LOG = PlatformUtil.getLog(ItemsListBuilder.class); - - private DownloadIndexes downloadIndexes; - - private List regionMapItems; - private List allResourceItems; - private List allSubregionItems; - - private OsmandApplication app; - private WorldRegion region; - - private boolean srtmDisabled; - private boolean hasSrtm; - private boolean hasHillshade; - - public List getRegionMapItems() { - return regionMapItems; - } - - public List getAllResourceItems() { - return allResourceItems; - } - - public List getRegionsFromAllItems() { - List list = new LinkedList<>(); - for (Object obj : allResourceItems) { - if (obj instanceof WorldRegion) { - list.add((WorldRegion) obj); - } - } - return list; - } - - public static String getVoicePromtName(Context ctx, VoicePromptsType type) { - switch (type) { - case RECORDED: - return ctx.getResources().getString(R.string.index_name_voice); - case TTS: - return ctx.getResources().getString(R.string.index_name_tts_voice); - default: - return ""; - } - } - - public List getVoicePromptsItems(VoicePromptsType type) { - switch (type) { - case RECORDED: - return downloadIndexes.voiceRecItems; - case TTS: - return downloadIndexes.voiceTTSItems; - default: - return new LinkedList<>(); - } - } - - public boolean isVoicePromptsItemsEmpty(VoicePromptsType type) { - switch (type) { - case RECORDED: - return downloadIndexes.voiceRecItems.isEmpty(); - case TTS: - return downloadIndexes.voiceTTSItems.isEmpty(); - default: - return true; - } - } - - // FIXME - public ItemsListBuilder(OsmandApplication app, String regionId, DownloadIndexes di) { - this.app = app; - this.downloadIndexes = di; - - regionMapItems = new LinkedList<>(); - allResourceItems = new LinkedList<>(); - allSubregionItems = new LinkedList<>(); - - region = app.getWorldRegion().getRegionById(regionId); - } - - public ItemsListBuilder build() { - if (obtainDataAndItems()) { - return this; - } else { - return null; - } - } - - private boolean obtainDataAndItems() { - if (downloadIndexes.resourcesByRegions.isEmpty() || region == null) { - return false; - } - - collectSubregionsDataAndItems(); - collectResourcesDataAndItems(); - - return true; - } - - private void collectSubregionsDataAndItems() { - srtmDisabled = OsmandPlugin.getEnabledPlugin(SRTMPlugin.class) == null; - hasSrtm = false; - hasHillshade = false; - - // Collect all regions (and their parents) that have at least one - // resource available in repository or locally. - - allResourceItems.clear(); - allSubregionItems.clear(); - regionMapItems.clear(); - - for (WorldRegion subregion : region.getFlattenedSubregions()) { - if (subregion.getSuperregion() == region) { - if (subregion.getFlattenedSubregions().size() > 0) { - allSubregionItems.add(subregion); - } else { - collectSubregionItems(subregion); - } - } - } - } - - private void collectSubregionItems(WorldRegion region) { - Map regionResources = downloadIndexes.resourcesByRegions.get(region); - - if (regionResources == null) { - return; - } - - List regionMapArray = new LinkedList<>(); - List allResourcesArray = new LinkedList<>(); - - Context context = app.getApplicationContext(); - OsmandRegions osmandRegions = app.getRegions(); - - for (IndexItem indexItem : regionResources.values()) { - - String name = indexItem.getVisibleName(context, osmandRegions, false); - if (Algorithms.isEmpty(name)) { - continue; - } - - ResourceItem resItem = new ResourceItem(indexItem, region); - resItem.setResourceId(indexItem.getSimplifiedFileName()); - resItem.setTitle(name); - - if (region != this.region && srtmDisabled) { - if (indexItem.getType() == DownloadActivityType.SRTM_COUNTRY_FILE) { - if (hasSrtm) { - continue; - } else { - hasSrtm = true; - } - } else if (indexItem.getType() == DownloadActivityType.HILLSHADE_FILE) { - if (hasHillshade) { - continue; - } else { - hasHillshade = true; - } - } - } - - - if (region == this.region) { - regionMapArray.add(resItem); - } else { - allResourcesArray.add(resItem); - } - - } - - regionMapItems.addAll(regionMapArray); - - if (allResourcesArray.size() > 1) { - allSubregionItems.add(region); - } else { - allResourceItems.addAll(allResourcesArray); - } - } - - private void collectResourcesDataAndItems() { - collectSubregionItems(region); - - allResourceItems.addAll(allSubregionItems); - - Collections.sort(allResourceItems, new ResourceItemComparator()); - Collections.sort(regionMapItems, new ResourceItemComparator()); - } - - public enum MapState { - NOT_DOWNLOADED, - DOWNLOADED, - OUTDATED - } - - - public enum VoicePromptsType { - NONE, - RECORDED, - TTS - } - } } \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/plus/download/DownloadTracker.java b/OsmAnd/src/net/osmand/plus/download/DownloadTracker.java index 20aa692dad..b612299ebd 100644 --- a/OsmAnd/src/net/osmand/plus/download/DownloadTracker.java +++ b/OsmAnd/src/net/osmand/plus/download/DownloadTracker.java @@ -3,7 +3,6 @@ package net.osmand.plus.download; import java.io.File; import java.io.IOException; import java.net.HttpURLConnection; -import java.net.URL; import java.net.URLEncoder; import java.text.MessageFormat; import java.util.Iterator; diff --git a/OsmAnd/src/net/osmand/plus/download/IndexItem.java b/OsmAnd/src/net/osmand/plus/download/IndexItem.java index a77c35e657..e4d3a3f981 100644 --- a/OsmAnd/src/net/osmand/plus/download/IndexItem.java +++ b/OsmAnd/src/net/osmand/plus/download/IndexItem.java @@ -21,7 +21,6 @@ public class IndexItem implements Comparable/*, Parcelable*/ { String description; String fileName; - String simplifiedFileName; String size; long timestamp; long contentSize; @@ -33,7 +32,6 @@ public class IndexItem implements Comparable/*, Parcelable*/ { public IndexItem(String fileName, String description, long timestamp, String size, long contentSize, long containerSize, DownloadActivityType tp) { this.fileName = fileName; - this.simplifiedFileName = fileName.toLowerCase().replace("_2.", ".").replace("hillshade_", ""); this.description = description; this.timestamp = timestamp; this.size = size; @@ -50,9 +48,6 @@ public class IndexItem implements Comparable/*, Parcelable*/ { return fileName; } - public String getSimplifiedFileName() { - return simplifiedFileName; - } public String getDescription() { return description; diff --git a/OsmAnd/src/net/osmand/plus/download/ui/ItemViewHolder.java b/OsmAnd/src/net/osmand/plus/download/ui/ItemViewHolder.java index a35100c0c4..e90976d9e5 100644 --- a/OsmAnd/src/net/osmand/plus/download/ui/ItemViewHolder.java +++ b/OsmAnd/src/net/osmand/plus/download/ui/ItemViewHolder.java @@ -79,7 +79,7 @@ public class ItemViewHolder extends TwoLineWithImagesViewHolder { nameTextView.setText(indexItem.getVisibleName(context, context.getMyApplication().getRegions(), false)); } else { - if (indexItem.getSimplifiedFileName().equals(ItemsListBuilder.WORLD_SEAMARKS_KEY) + if (indexItem.getSimplifiedFileName().equals(WORLD_SEAMARKS_KEY) && nauticalPluginDisabled) { rightButtonAction = RightButtonAction.ASK_FOR_SEAMARKS_PLUGIN; disabled = true; From 9f44293c19f7b63d3b4fab11959883d5ca42bb4b Mon Sep 17 00:00:00 2001 From: Victor Shcherb Date: Mon, 19 Oct 2015 01:21:54 +0200 Subject: [PATCH 05/22] Continue refactoring --- OsmAnd/res/values/strings.xml | 2 - .../plus/download/DownloadActivity.java | 4 +- .../plus/download/DownloadIndexesThread.java | 5 + .../plus/download/DownloadResourceGroup.java | 101 ++++++- .../plus/download/DownloadResources.java | 33 ++- .../net/osmand/plus/download/IndexItem.java | 73 +---- .../ui/ActiveDownloadsDialogFragment.java | 1 - ...ava => DownloadResourceGroupFragment.java} | 8 +- .../plus/download/ui/ItemViewHolder.java | 6 +- .../plus/download/ui/RegionItemsFragment.java | 253 +++-------------- .../download/ui/SearchDialogFragment.java | 2 +- .../plus/download/ui/VoiceDialogFragment.java | 100 ------- .../plus/download/ui/VoiceItemsFragment.java | 253 ----------------- .../plus/download/ui/WorldItemsFragment.java | 260 ++++++++---------- 14 files changed, 289 insertions(+), 812 deletions(-) rename OsmAnd/src/net/osmand/plus/download/ui/{RegionDialogFragment.java => DownloadResourceGroupFragment.java} (91%) delete mode 100644 OsmAnd/src/net/osmand/plus/download/ui/VoiceDialogFragment.java delete mode 100644 OsmAnd/src/net/osmand/plus/download/ui/VoiceItemsFragment.java diff --git a/OsmAnd/res/values/strings.xml b/OsmAnd/res/values/strings.xml index 30f4b2f7b7..4ef7857661 100644 --- a/OsmAnd/res/values/strings.xml +++ b/OsmAnd/res/values/strings.xml @@ -13,10 +13,8 @@ Category name Add new category Regions - Additional maps Region maps World maps - World regions Hillshade layer disabled Contour lines disabled Add new diff --git a/OsmAnd/src/net/osmand/plus/download/DownloadActivity.java b/OsmAnd/src/net/osmand/plus/download/DownloadActivity.java index eb7e7b64f3..d8dc3c5da6 100644 --- a/OsmAnd/src/net/osmand/plus/download/DownloadActivity.java +++ b/OsmAnd/src/net/osmand/plus/download/DownloadActivity.java @@ -198,11 +198,11 @@ public class DownloadActivity extends BaseDownloadActivity implements DialogDism Fragment f = ref.get(); if (f instanceof WorldItemsFragment) { if (f.isAdded()) { - ((WorldItemsFragment) f).onCategorizationFinished(); + ((WorldItemsFragment) f).newDownloadIndexes(); } } else if (f instanceof SearchDialogFragment) { if (f.isAdded()) { - ((SearchDialogFragment) f).onCategorizationFinished(); + ((SearchDialogFragment) f).newDownloadIndexes(); } } else if (f instanceof LocalIndexesFragment) { if (f.isAdded()) { diff --git a/OsmAnd/src/net/osmand/plus/download/DownloadIndexesThread.java b/OsmAnd/src/net/osmand/plus/download/DownloadIndexesThread.java index d4cc98574d..bd4103b2e8 100644 --- a/OsmAnd/src/net/osmand/plus/download/DownloadIndexesThread.java +++ b/OsmAnd/src/net/osmand/plus/download/DownloadIndexesThread.java @@ -96,6 +96,11 @@ public class DownloadIndexesThread { // PUBLIC API + + public DownloadResources getIndexes() { + return indexes; + } + public List getCurrentDownloadingItems() { List res = new ArrayList(); IndexItem ii = currentDownloadingItem; diff --git a/OsmAnd/src/net/osmand/plus/download/DownloadResourceGroup.java b/OsmAnd/src/net/osmand/plus/download/DownloadResourceGroup.java index 80e829b864..9d38a70ccd 100644 --- a/OsmAnd/src/net/osmand/plus/download/DownloadResourceGroup.java +++ b/OsmAnd/src/net/osmand/plus/download/DownloadResourceGroup.java @@ -1,5 +1,7 @@ package net.osmand.plus.download; +import android.annotation.SuppressLint; + import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; @@ -9,8 +11,10 @@ import java.util.List; import net.osmand.OsmAndCollator; import net.osmand.map.OsmandRegions; import net.osmand.plus.OsmandApplication; +import net.osmand.plus.R; import net.osmand.plus.WorldRegion; +@SuppressLint("DefaultLocale") public class DownloadResourceGroup { private final DownloadResourceGroupType type; @@ -21,17 +25,50 @@ public class DownloadResourceGroup { protected final String id; protected WorldRegion region; - public static final String REGION_MAPS_ID = "maps"; public enum DownloadResourceGroupType { -// return ctx.getResources().getString(R.string.index_name_voice); -// return ctx.getResources().getString(R.string.index_name_tts_voice); - WORLD, VOICE_REC, VOICE_TTS, WORLD_MAPS, REGION, REGION_MAPS + // headers + WORLD_MAPS(R.string.world_maps), REGION_MAPS(R.string.region_maps), VOICE_GROUP(R.string.voices), SUBREGIONS( + R.string.regions), VOICE_HEADER_REC(R.string.index_name_voice), VOICE_HEADER_TTS( + R.string.index_name_tts_voice), + // screen items + VOICE_REC(R.string.index_name_voice), VOICE_TTS(R.string.index_name_tts_voice), WORLD(-1), REGION(-1); + + final int resId; + + private DownloadResourceGroupType(int resId) { + this.resId = resId; + } + + public boolean isScreen() { + return this == WORLD || this == REGION || this == VOICE_TTS || this == VOICE_REC; + } + + public String getDefaultId() { + return name().toLowerCase(); + } + + public int getResourceId() { + return resId; + } + + public boolean containsIndexItem() { + return isHeader() && this != SUBREGIONS; + } + + public boolean isHeader() { + return this == VOICE_HEADER_REC || this == VOICE_HEADER_TTS || this == SUBREGIONS || this == WORLD_MAPS + || this == REGION_MAPS || this == VOICE_GROUP; + } } + + public DownloadResourceGroup(DownloadResourceGroup parentGroup, DownloadResourceGroupType type) { + this(parentGroup, type, type.getDefaultId()); + } - public DownloadResourceGroup(DownloadResourceGroup parentGroup, DownloadResourceGroupType type, String id, - boolean flat) { + public DownloadResourceGroup(DownloadResourceGroup parentGroup, DownloadResourceGroupType type, String id) { + boolean flat = type.containsIndexItem(); if (flat) { this.individualResources = new ArrayList(); this.groups = null; @@ -61,6 +98,16 @@ public class DownloadResourceGroup { } public void addGroup(DownloadResourceGroup g) { + if(type.isScreen()) { + if(!g.type.isHeader()) { + throw new UnsupportedOperationException("Trying to add " + g.getUniqueId() + " to " + getUniqueId()); + } + } + if(type.isHeader()) { + if(!g.type.isScreen()) { + throw new UnsupportedOperationException("Trying to add " + g.getUniqueId() + " to " + getUniqueId()); + } + } groups.add(g); if (g.individualResources != null) { final net.osmand.Collator collator = OsmAndCollator.primaryCollator(); @@ -88,17 +135,31 @@ public class DownloadResourceGroup { return l == null || l.isEmpty(); } - public String getGroupId() { - return id; - } - - public boolean flatGroup() { - return individualResources != null; - } - public DownloadResourceGroup getParentGroup() { return parentGroup; } + + public List getGroups() { + return groups; + } + + public int size() { + return groups != null ? groups.size() : individualResources.size(); + } + + public DownloadResourceGroup getGroupByIndex(int ind) { + if(groups != null && ind < groups.size()) { + return groups.get(ind); + } + return null; + } + + public IndexItem getItemByIndex(int ind) { + if(individualResources != null && ind < individualResources.size()) { + return individualResources.get(ind); + } + return null; + } public DownloadResources getRoot() { if (this instanceof DownloadResources) { @@ -117,6 +178,14 @@ public class DownloadResourceGroup { String[] lst = uid.split("\\#"); return getGroupById(lst, 0); } + + public List getIndividualResources() { + return individualResources; + } + + public WorldRegion getRegion() { + return region; + } private DownloadResourceGroup getGroupById(String[] lst, int subInd) { if (lst.length > subInd && lst[subInd].equals(id)) { @@ -133,6 +202,10 @@ public class DownloadResourceGroup { } return null; } + + public String getName() { + return id; + } public String getUniqueId() { if (parentGroup == null) { diff --git a/OsmAnd/src/net/osmand/plus/download/DownloadResources.java b/OsmAnd/src/net/osmand/plus/download/DownloadResources.java index a00fac3645..cca97fe580 100644 --- a/OsmAnd/src/net/osmand/plus/download/DownloadResources.java +++ b/OsmAnd/src/net/osmand/plus/download/DownloadResources.java @@ -28,7 +28,7 @@ public class DownloadResources extends DownloadResourceGroup { public DownloadResources(OsmandApplication app) { - super(null, DownloadResourceGroupType.WORLD, "", false); + super(null, DownloadResourceGroupType.WORLD, ""); this.region = app.getWorldRegion(); this.app = app; } @@ -164,9 +164,18 @@ public class DownloadResources extends DownloadResourceGroup { protected boolean prepareData(List resources) { this.rawResources = resources; - DownloadResourceGroup voiceRec = new DownloadResourceGroup(this, DownloadResourceGroupType.VOICE_REC, "voice_rec", true); - DownloadResourceGroup voiceTTS = new DownloadResourceGroup(this, DownloadResourceGroupType.VOICE_TTS, "voice_tts", true); - DownloadResourceGroup worldMaps = new DownloadResourceGroup(this, DownloadResourceGroupType.WORLD_MAPS, "world", true); + + DownloadResourceGroup voiceGroup = new DownloadResourceGroup(this, DownloadResourceGroupType.VOICE_GROUP); + DownloadResourceGroup voiceScreenRec = new DownloadResourceGroup(this, DownloadResourceGroupType.VOICE_REC); + DownloadResourceGroup voiceRec = new DownloadResourceGroup(this, DownloadResourceGroupType.VOICE_HEADER_REC); + DownloadResourceGroup voiceTTS = new DownloadResourceGroup(this, DownloadResourceGroupType.VOICE_TTS); + DownloadResourceGroup voiceScreenTTS = new DownloadResourceGroup(this, DownloadResourceGroupType.VOICE_HEADER_TTS); + voiceScreenTTS.addGroup(voiceTTS); + voiceScreenRec.addGroup(voiceRec); + voiceGroup.addGroup(voiceScreenRec); + voiceGroup.addGroup(voiceScreenTTS); + + DownloadResourceGroup worldMaps = new DownloadResourceGroup(this, DownloadResourceGroupType.WORLD_MAPS); Map > groupByRegion = new LinkedHashMap>(); Map downloadIdForRegion = new LinkedHashMap(); @@ -195,19 +204,23 @@ public class DownloadResources extends DownloadResourceGroup { } LinkedList queue = new LinkedList(); LinkedList parent = new LinkedList(); + DownloadResourceGroup worldSubregions = new DownloadResourceGroup(this, DownloadResourceGroupType.SUBREGIONS); + addGroup(worldSubregions); for(WorldRegion rg : region.getSubregions()) { queue.add(rg); - parent.add(this); + parent.add(worldSubregions); } while(!queue.isEmpty()) { WorldRegion reg = queue.pollFirst(); DownloadResourceGroup parentGroup = parent.pollFirst(); List subregions = reg.getSubregions(); - DownloadResourceGroup mainGrp = new DownloadResourceGroup(parentGroup, DownloadResourceGroupType.REGION, reg.getRegionId(), false); + DownloadResourceGroup mainGrp = new DownloadResourceGroup(parentGroup, DownloadResourceGroupType.REGION, reg.getRegionId()); parentGroup.addGroup(mainGrp); + DownloadResourceGroup subRegions = new DownloadResourceGroup(mainGrp, DownloadResourceGroupType.SUBREGIONS); + mainGrp.addGroup(subRegions); List list = groupByRegion.get(reg); if(list != null) { - DownloadResourceGroup flatFiles = new DownloadResourceGroup(parentGroup, DownloadResourceGroupType.REGION_MAPS, REGION_MAPS_ID, true); + DownloadResourceGroup flatFiles = new DownloadResourceGroup(mainGrp, DownloadResourceGroupType.REGION_MAPS); for(IndexItem ii : list) { flatFiles.addItem(ii); } @@ -216,7 +229,7 @@ public class DownloadResources extends DownloadResourceGroup { // add to processing queue for(WorldRegion rg : subregions) { queue.add(rg); - parent.add(mainGrp); + parent.add(subRegions); } } // Possible improvements @@ -224,8 +237,8 @@ public class DownloadResources extends DownloadResourceGroup { // 2. if there is no subregions and there only 1 index item it could be merged to the level up - objection there is no such maps // 3. if hillshade/srtm is disabled, all maps from inner level could be combined into 1 addGroup(worldMaps); - addGroup(voiceTTS); - addGroup(voiceRec); + addGroup(voiceGroup); + trimEmptyGroups(); initAlreadyLoadedFiles(); return true; diff --git a/OsmAnd/src/net/osmand/plus/download/IndexItem.java b/OsmAnd/src/net/osmand/plus/download/IndexItem.java index e4d3a3f981..890dbabfd0 100644 --- a/OsmAnd/src/net/osmand/plus/download/IndexItem.java +++ b/OsmAnd/src/net/osmand/plus/download/IndexItem.java @@ -16,7 +16,7 @@ import org.apache.commons.logging.Log; import android.content.Context; import android.support.annotation.NonNull; -public class IndexItem implements Comparable/*, Parcelable*/ { +public class IndexItem implements Comparable { private static final Log log = PlatformUtil.getLog(IndexItem.class); String description; @@ -127,17 +127,18 @@ public class IndexItem implements Comparable/*, Parcelable*/ { @Override public int compareTo(@NonNull IndexItem another) { -// if(another == null) { -// return -1; -// } return getFileName().compareTo(another.getFileName()); } - public boolean isAlreadyDownloaded(Map listAlreadyDownloaded) { - return listAlreadyDownloaded.containsKey(getTargetFileName()); - } - + public boolean isOutdated() { + FIXME; + } + + + public boolean isDownloaded() { +// return listAlreadyDownloaded.containsKey(getTargetFileName()); + } public String getVisibleName(Context ctx, OsmandRegions osmandRegions) { return type.getVisibleName(this, ctx, osmandRegions, true); @@ -186,60 +187,4 @@ public class IndexItem implements Comparable/*, Parcelable*/ { } -// @Override -// public String toString() { -// return "IndexItem{" + -// "description='" + description + '\'' + -// ", fileName='" + fileName + '\'' + -// ", simplifiedFileName='" + simplifiedFileName + '\'' + -// ", size='" + size + '\'' + -// ", timestamp=" + timestamp + -// ", contentSize=" + contentSize + -// ", containerSize=" + containerSize + -// ", type=" + type.getTag() + -// ", extra=" + extra + -// '}'; -// } - -// @Override -// public int describeContents() { -// return 0; -// } -// -// @Override -// public void writeToParcel(Parcel dest, int flags) { -// dest.writeString(this.description); -// dest.writeString(this.fileName); -// dest.writeString(this.size); -// dest.writeLong(this.timestamp); -// dest.writeLong(this.contentSize); -// dest.writeLong(this.containerSize); -// dest.writeParcelable(this.type, flags); -// dest.writeByte(extra ? (byte) 1 : (byte) 0); -// dest.writeString(this.initializedName); -// dest.writeString(this.simplifiedFileName); -// } -// -// protected IndexItem(Parcel in) { -// this.description = in.readString(); -// this.fileName = in.readString(); -// this.size = in.readString(); -// this.timestamp = in.readLong(); -// this.contentSize = in.readLong(); -// this.containerSize = in.readLong(); -// this.type = in.readParcelable(DownloadActivityType.class.getClassLoader()); -// this.extra = in.readByte() != 0; -// this.initializedName = in.readString(); -// this.simplifiedFileName = in.readString(); -// } -// -// public static final Parcelable.Creator CREATOR = new Parcelable.Creator() { -// public IndexItem createFromParcel(Parcel source) { -// return new IndexItem(source); -// } -// -// public IndexItem[] newArray(int size) { -// return new IndexItem[size]; -// } -// }; } \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/plus/download/ui/ActiveDownloadsDialogFragment.java b/OsmAnd/src/net/osmand/plus/download/ui/ActiveDownloadsDialogFragment.java index 12bdb5187e..5138f34d65 100644 --- a/OsmAnd/src/net/osmand/plus/download/ui/ActiveDownloadsDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/download/ui/ActiveDownloadsDialogFragment.java @@ -52,7 +52,6 @@ public class ActiveDownloadsDialogFragment extends DialogFragment { public static class IndexItemAdapter extends ArrayAdapter { private final Drawable deleteDrawable; private final DownloadActivity context; - private boolean isFinished; public IndexItemAdapter(DownloadActivity context) { super(context, R.layout.two_line_with_images_list_item, new ArrayList()); diff --git a/OsmAnd/src/net/osmand/plus/download/ui/RegionDialogFragment.java b/OsmAnd/src/net/osmand/plus/download/ui/DownloadResourceGroupFragment.java similarity index 91% rename from OsmAnd/src/net/osmand/plus/download/ui/RegionDialogFragment.java rename to OsmAnd/src/net/osmand/plus/download/ui/DownloadResourceGroupFragment.java index d1b5e19c46..a9da0290b0 100644 --- a/OsmAnd/src/net/osmand/plus/download/ui/RegionDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/download/ui/DownloadResourceGroupFragment.java @@ -14,7 +14,7 @@ import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; -public class RegionDialogFragment extends DialogFragment { +public class DownloadResourceGroupFragment extends DialogFragment { public static final String TAG = "RegionDialogFragment"; private static final String REGION_ID_DLG_KEY = "world_region_dialog_key"; private String regionId; @@ -90,15 +90,15 @@ public class RegionDialogFragment extends DialogFragment { } public void onRegionSelected(String regionId) { - final RegionDialogFragment regionDialogFragment = createInstance(regionId); + final DownloadResourceGroupFragment regionDialogFragment = createInstance(regionId); regionDialogFragment.setOnDismissListener(listener); getDownloadActivity().showDialog(getActivity(), regionDialogFragment); } - public static RegionDialogFragment createInstance(String regionId) { + public static DownloadResourceGroupFragment createInstance(String regionId) { Bundle bundle = new Bundle(); bundle.putString(REGION_ID_DLG_KEY, regionId); - RegionDialogFragment fragment = new RegionDialogFragment(); + DownloadResourceGroupFragment fragment = new DownloadResourceGroupFragment(); fragment.setArguments(bundle); return fragment; } diff --git a/OsmAnd/src/net/osmand/plus/download/ui/ItemViewHolder.java b/OsmAnd/src/net/osmand/plus/download/ui/ItemViewHolder.java index e90976d9e5..e9a2825b0b 100644 --- a/OsmAnd/src/net/osmand/plus/download/ui/ItemViewHolder.java +++ b/OsmAnd/src/net/osmand/plus/download/ui/ItemViewHolder.java @@ -42,10 +42,10 @@ public class ItemViewHolder extends TwoLineWithImagesViewHolder { } public ItemViewHolder(View convertView, - DownloadActivity context, - DateFormat dateFormat) { + DownloadActivity context) { super(convertView, context); - this.dateFormat = dateFormat; + + this.dateFormat = context.getMyApplication().getResourceManager().getDateFormat(); TypedValue typedValue = new TypedValue(); Resources.Theme theme = context.getTheme(); diff --git a/OsmAnd/src/net/osmand/plus/download/ui/RegionItemsFragment.java b/OsmAnd/src/net/osmand/plus/download/ui/RegionItemsFragment.java index 2660f80a75..1f0ec6d3ee 100644 --- a/OsmAnd/src/net/osmand/plus/download/ui/RegionItemsFragment.java +++ b/OsmAnd/src/net/osmand/plus/download/ui/RegionItemsFragment.java @@ -1,41 +1,42 @@ package net.osmand.plus.download.ui; -import java.util.ArrayList; -import java.util.LinkedHashMap; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; - 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.download.BaseDownloadActivity; import net.osmand.plus.download.DownloadActivity; import net.osmand.plus.download.DownloadActivityType; +import net.osmand.plus.download.DownloadResourceGroup; +import net.osmand.plus.download.DownloadResources; import net.osmand.plus.download.IndexItem; import android.app.Dialog; import android.content.DialogInterface; -import android.content.res.Resources; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.v4.app.DialogFragment; import android.support.v7.app.AlertDialog; -import android.util.TypedValue; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ExpandableListView; -import android.widget.TextView; public class RegionItemsFragment extends OsmandExpandableListFragment { public static final String TAG = "RegionItemsFragment"; - - private RegionsItemsAdapter listAdapter; - private static final String REGION_ID_KEY = "world_region_id_key"; private String regionId; + private WorldItemsFragment.DownloadResourceGroupAdapter listAdapter; + private DownloadActivity activity; + private DownloadResourceGroup group; + public static RegionItemsFragment createInstance(String regionId) { + Bundle bundle = new Bundle(); + bundle.putString(REGION_ID_KEY, regionId); + RegionItemsFragment fragment = new RegionItemsFragment(); + fragment.setArguments(bundle); + return fragment; + } + @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -52,24 +53,20 @@ public class RegionItemsFragment extends OsmandExpandableListFragment { if (regionId == null) { regionId = getArguments().getString(REGION_ID_KEY); } - if (regionId == null) regionId = ""; ExpandableListView listView = (ExpandableListView) view.findViewById(android.R.id.list); - listAdapter = new RegionsItemsAdapter(); + activity = (DownloadActivity) getActivity(); + DownloadResources indexes = activity.getDownloadThread().getIndexes(); + group = indexes.getGroupById(regionId); + listAdapter = new WorldItemsFragment.DownloadResourceGroupAdapter(activity); listView.setAdapter(listAdapter); setListView(listView); - - if (regionId.length() > 0) { - ItemsListBuilder builder = getDownloadActivity().getItemsBuilder(regionId, false); - if (builder != null) { - fillRegionItemsAdapter(builder); - listAdapter.notifyDataSetChanged(); - expandAllGroups(); - } + if(group != null) { + listAdapter.update(group); } - + expandAllGroups(); return view; } @@ -79,206 +76,46 @@ public class RegionItemsFragment extends OsmandExpandableListFragment { super.onSaveInstanceState(outState); } + @Override - public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, - int childPosition, long id) { - Object obj = listAdapter.getChild(groupPosition, childPosition); - if (obj instanceof WorldRegion) { - WorldRegion region = (WorldRegion) obj; - ((RegionDialogFragment) getParentFragment()) - .onRegionSelected(region.getRegionId()); + public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { + Object child = listAdapter.getChild(groupPosition, childPosition); + if (child instanceof DownloadResourceGroup) { + String uniqueId = ((DownloadResourceGroup) child).getUniqueId(); + final DownloadResourceGroupFragment regionDialogFragment = DownloadResourceGroupFragment.createInstance(); + regionDialogFragment.setOnDismissListener(getDownloadActivity()); + getDownloadActivity().showDialog(getActivity(), regionDialogFragment); + ((DownloadResourceGroupFragment) getParentFragment()).onRegionSelected(uniqueId); return true; - } else if (obj instanceof ItemsListBuilder.ResourceItem) { - if (((ItemViewHolder) v.getTag()).isItemAvailable()) { - IndexItem indexItem = ((ItemsListBuilder.ResourceItem) obj).getIndexItem(); - if (indexItem.getType() == DownloadActivityType.ROADS_FILE) { - IndexItem regularMap = - ((ItemsListBuilder.ResourceItem) listAdapter.getChild(0, 0)) - .getIndexItem(); - if (regularMap.getType() == DownloadActivityType.NORMAL_FILE - && regularMap.isAlreadyDownloaded(getMyActivity().getIndexFileNames())) { - ConfirmDownloadUnneededMapDialogFragment.createInstance(indexItem) - .show(getChildFragmentManager(), "dialog"); - return true; - } - } - getMyActivity().startDownload(indexItem); - return true; + } else if (child instanceof IndexItem) { + IndexItem indexItem = (IndexItem) child; + if (indexItem.getType() == DownloadActivityType.ROADS_FILE) { + // FIXME +// if (regularMap.getType() == DownloadActivityType.NORMAL_FILE +// && regularMap.isAlreadyDownloaded(getMyActivity().getIndexFileNames())) { +// ConfirmDownloadUnneededMapDialogFragment.createInstance(indexItem) +// .show(getChildFragmentManager(), "dialog"); +// return true; +// } } + ((BaseDownloadActivity) getActivity()).startDownload(indexItem); + return true; } return false; } - + private void expandAllGroups() { for (int i = 0; i < listAdapter.getGroupCount(); i++) { getExpandableListView().expandGroup(i); } } - public OsmandApplication getMyApplication() { - return (OsmandApplication) getActivity().getApplication(); - } - - public DownloadActivity getMyActivity() { - return (DownloadActivity) getActivity(); - } - - private void fillRegionItemsAdapter(ItemsListBuilder builder) { - if (listAdapter != null) { - listAdapter.clear(); - if (builder.getRegionMapItems().size() > 0) { - String sectionTitle = getResources().getString(R.string.region_maps); - listAdapter.add(sectionTitle, builder.getRegionMapItems()); - } - if (builder.getAllResourceItems().size() > 0) { - String sectionTitle; - if (builder.getRegionMapItems().size() > 0) { - sectionTitle = getResources().getString(R.string.additional_maps); - } else { - sectionTitle = getResources().getString(R.string.regions); - } - listAdapter.add(sectionTitle, builder.getAllResourceItems()); - } - } - } private DownloadActivity getDownloadActivity() { return (DownloadActivity) getActivity(); } - public static RegionItemsFragment createInstance(String regionId) { - Bundle bundle = new Bundle(); - bundle.putString(REGION_ID_KEY, regionId); - RegionItemsFragment fragment = new RegionItemsFragment(); - fragment.setArguments(bundle); - return fragment; - } - - private class RegionsItemsAdapter extends OsmandBaseExpandableListAdapter { - - private Map> data = new LinkedHashMap<>(); - private List sections = new LinkedList<>(); - - private int groupInProgressPosition = -1; - private int childInProgressPosition = -1; - private int progress = -1; - - public RegionsItemsAdapter() { - } - - public void clear() { - data.clear(); - sections.clear(); - notifyDataSetChanged(); - } - - public void add(String section, List list) { - if (!sections.contains(section)) { - sections.add(section); - } - if (!data.containsKey(section)) { - data.put(section, new ArrayList<>()); - } - data.get(section).addAll(list); - } - - @Override - public Object getChild(int groupPosition, int childPosition) { - String section = sections.get(groupPosition); - return data.get(section).get(childPosition); - } - - @Override - public long getChildId(int groupPosition, int childPosition) { - return groupPosition * 10000 + childPosition; - } - - @Override - public View getChildView(final int groupPosition, final int childPosition, - boolean isLastChild, View convertView, ViewGroup parent) { - - ItemViewHolder viewHolder; - if (convertView == null) { - convertView = LayoutInflater.from(parent.getContext()) - .inflate(R.layout.two_line_with_images_list_item, parent, false); - viewHolder = new ItemViewHolder(convertView, getMyActivity(), - getMyApplication().getResourceManager().getDateFormat()); - convertView.setTag(viewHolder); - } else { - viewHolder = (ItemViewHolder) convertView.getTag(); - } - final Object child = getChild(groupPosition, childPosition); - - if (child instanceof WorldRegion) { - viewHolder.bindRegion((WorldRegion) child); - } else if (child instanceof ItemsListBuilder.ResourceItem) { - final int localProgress = groupPosition == groupInProgressPosition - && childPosition == childInProgressPosition ? progress : -1; - viewHolder.bindIndexItem(((ItemsListBuilder.ResourceItem) child).getIndexItem(), - false, true, localProgress); - } else { - throw new IllegalArgumentException("Item must be of type WorldRegion or " + - "ResourceItem but is of type:" + child.getClass()); - } - - return convertView; - } - - @Override - public View getGroupView(int groupPosition, boolean isExpanded, View convertView, - ViewGroup parent) { - View v = convertView; - String section = getGroup(groupPosition); - - if (v == null) { - v = LayoutInflater.from(getDownloadActivity()) - .inflate(R.layout.download_item_list_section, parent, false); - } - TextView nameView = ((TextView) v.findViewById(R.id.section_name)); - nameView.setText(section); - - v.setOnClickListener(null); - - TypedValue typedValue = new TypedValue(); - Resources.Theme theme = getActivity().getTheme(); - theme.resolveAttribute(R.attr.ctx_menu_info_view_bg, typedValue, true); - v.setBackgroundColor(typedValue.data); - - return v; - } - - @Override - public int getChildrenCount(int groupPosition) { - String section = sections.get(groupPosition); - return data.get(section).size(); - } - - @Override - public String getGroup(int groupPosition) { - return sections.get(groupPosition); - } - - @Override - public int getGroupCount() { - return sections.size(); - } - - @Override - public long getGroupId(int groupPosition) { - return groupPosition; - } - - @Override - public boolean hasStableIds() { - return false; - } - - @Override - public boolean isChildSelectable(int groupPosition, int childPosition) { - return true; - } - - } + public static class ConfirmDownloadUnneededMapDialogFragment extends DialogFragment { private static final String INDEX_ITEM = "index_item"; @@ -311,5 +148,5 @@ public class RegionItemsFragment extends OsmandExpandableListFragment { fragment.setArguments(args); return fragment; } - } + } } diff --git a/OsmAnd/src/net/osmand/plus/download/ui/SearchDialogFragment.java b/OsmAnd/src/net/osmand/plus/download/ui/SearchDialogFragment.java index 206d27a4aa..e365e79acf 100644 --- a/OsmAnd/src/net/osmand/plus/download/ui/SearchDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/download/ui/SearchDialogFragment.java @@ -125,7 +125,7 @@ public class SearchDialogFragment extends DialogFragment { return (DownloadActivity) getActivity(); } - public void onCategorizationFinished() { + public void newDownloadIndexes() { Fragment f = getChildFragmentManager().findFragmentByTag(SearchItemsFragment.TAG); if (f != null) { ((SearchItemsFragment) f).onCategorizationFinished(); diff --git a/OsmAnd/src/net/osmand/plus/download/ui/VoiceDialogFragment.java b/OsmAnd/src/net/osmand/plus/download/ui/VoiceDialogFragment.java deleted file mode 100644 index 10ef409e38..0000000000 --- a/OsmAnd/src/net/osmand/plus/download/ui/VoiceDialogFragment.java +++ /dev/null @@ -1,100 +0,0 @@ -package net.osmand.plus.download.ui; - -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; -import android.view.ViewGroup; -import net.osmand.PlatformUtil; -import net.osmand.plus.OsmandApplication; -import net.osmand.plus.OsmandSettings; -import net.osmand.plus.R; -import net.osmand.plus.download.DownloadActivity; -import net.osmand.plus.download.ui.ItemsListBuilder.VoicePromptsType; - -import org.apache.commons.logging.Log; - -public class VoiceDialogFragment extends DialogFragment { - private static final Log LOG = PlatformUtil.getLog(VoiceDialogFragment.class); - public static final String TAG = "VoiceDialogFragment"; - private static final String VOICE_PROMPT_TYPE_DLG_KEY = "voice_prompt_type_dlg_key"; - private VoicePromptsType voicePromptsType = VoicePromptsType.NONE; - - @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - boolean isLightTheme = ((OsmandApplication) getActivity().getApplication()) - .getSettings().OSMAND_THEME.get() == OsmandSettings.OSMAND_LIGHT_THEME; - int themeId = isLightTheme ? R.style.OsmandLightTheme : R.style.OsmandDarkTheme; - setStyle(STYLE_NO_FRAME, themeId); - } - - @Override - public View onCreateView(LayoutInflater inflater, ViewGroup container, - Bundle savedInstanceState) { - final View view = inflater.inflate(R.layout.maps_in_category_fragment, container, false); - - String value = null; - try { - if (savedInstanceState != null) { - value = savedInstanceState.getString(VOICE_PROMPT_TYPE_DLG_KEY); - if (value != null) { - voicePromptsType = VoicePromptsType.valueOf(value); - } - } - if (voicePromptsType == VoicePromptsType.NONE) { - value = getArguments().getString(VOICE_PROMPT_TYPE_DLG_KEY); - if (value != null) { - voicePromptsType = VoicePromptsType.valueOf(value); - } - } - } catch (IllegalArgumentException e) { - LOG.warn("VOICE_PROMPT_TYPE_DLG_KEY=" + value); - } - - Toolbar toolbar = (Toolbar) view.findViewById(R.id.toolbar); - toolbar.setNavigationIcon(getMyApplication().getIconsCache().getIcon(R.drawable.abc_ic_ab_back_mtrl_am_alpha)); - toolbar.setNavigationOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - dismiss(); - } - }); - - if (voicePromptsType != VoicePromptsType.NONE) { - Fragment fragment = getChildFragmentManager().findFragmentById(R.id.fragmentContainer); - if (fragment == null) { - getChildFragmentManager().beginTransaction().add(R.id.fragmentContainer, - VoiceItemsFragment.createInstance(voicePromptsType)).commit(); - } - - toolbar.setTitle(ItemsListBuilder.getVoicePromtName(getActivity(), voicePromptsType)); - } - ((DownloadActivity)getActivity()).registerFreeVersionBanner(view); - - return view; - } - - @Override - public void onSaveInstanceState(Bundle outState) { - outState.putString(VOICE_PROMPT_TYPE_DLG_KEY, voicePromptsType.name()); - super.onSaveInstanceState(outState); - } - - private OsmandApplication getMyApplication() { - return (OsmandApplication) getActivity().getApplication(); - } - - - public static VoiceDialogFragment createInstance(VoicePromptsType voicePromptsType) { - Bundle bundle = new Bundle(); - bundle.putString(VOICE_PROMPT_TYPE_DLG_KEY, voicePromptsType.name()); - VoiceDialogFragment fragment = new VoiceDialogFragment(); - fragment.setArguments(bundle); - return fragment; - } -} - - diff --git a/OsmAnd/src/net/osmand/plus/download/ui/VoiceItemsFragment.java b/OsmAnd/src/net/osmand/plus/download/ui/VoiceItemsFragment.java deleted file mode 100644 index 89f9fc5d2c..0000000000 --- a/OsmAnd/src/net/osmand/plus/download/ui/VoiceItemsFragment.java +++ /dev/null @@ -1,253 +0,0 @@ -package net.osmand.plus.download.ui; - -import android.content.Context; -import android.content.res.Resources; -import android.content.res.TypedArray; -import android.os.Bundle; -import android.util.TypedValue; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; -import android.widget.ExpandableListView; -import android.widget.TextView; -import net.osmand.PlatformUtil; -import net.osmand.plus.OsmandApplication; -import net.osmand.plus.R; -import net.osmand.plus.activities.OsmandBaseExpandableListAdapter; -import net.osmand.plus.activities.OsmandExpandableListFragment; -import net.osmand.plus.download.BaseDownloadActivity; -import net.osmand.plus.download.DownloadActivity; -import net.osmand.plus.download.IndexItem; -import net.osmand.plus.download.ui.ItemsListBuilder.VoicePromptsType; - -import org.apache.commons.logging.Log; - -import java.util.ArrayList; -import java.util.LinkedHashMap; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; - -public class VoiceItemsFragment extends OsmandExpandableListFragment { - public static final String TAG = "VoiceItemsFragment"; - private static final Log LOG = PlatformUtil.getLog(VoiceItemsFragment.class); - private static final String VOICE_PROMPT_TYPE_KEY = "voice_prompt_type_key"; - private VoicePromptsType voicePromptsType = VoicePromptsType.NONE; - - private VoiceItemsAdapter listAdapter; - - @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setHasOptionsMenu(true); - } - - @Override - public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - View view = inflater.inflate(R.layout.download_items_fragment, container, false); - - String value = null; - try { - if (savedInstanceState != null) { - value = savedInstanceState.getString(VOICE_PROMPT_TYPE_KEY); - if (value != null) { - voicePromptsType = VoicePromptsType.valueOf(value); - } - } - if (voicePromptsType == VoicePromptsType.NONE) { - value = getArguments().getString(VOICE_PROMPT_TYPE_KEY); - if (value != null) { - voicePromptsType = VoicePromptsType.valueOf(value); - } - } - } catch (IllegalArgumentException e) { - LOG.warn("VOICE_PROMPT_TYPE_KEY=" + value); - } - - ExpandableListView listView = (ExpandableListView) view.findViewById(android.R.id.list); - listAdapter = new VoiceItemsAdapter(getActivity()); - listView.setAdapter(listAdapter); - setListView(listView); - - if (voicePromptsType != VoicePromptsType.NONE) { - ItemsListBuilder builder = getDownloadActivity().getVoicePromptsBuilder(); - if (builder != null) { - fillVoiceItemsAdapter(builder); - listAdapter.notifyDataSetChanged(); - expandAllGroups(); - } - } - - return view; - } - - @Override - public void onSaveInstanceState(Bundle outState) { - outState.putString(VOICE_PROMPT_TYPE_KEY, voicePromptsType.name()); - super.onSaveInstanceState(outState); - } - - @Override - public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { - Object obj = listAdapter.getChild(groupPosition, childPosition); - if (((ItemViewHolder) v.getTag()).isItemAvailable()) { - IndexItem indexItem = (IndexItem) obj; - ((BaseDownloadActivity) getActivity()) - .startDownload(indexItem); - - return true; - } else { - return false; - } - } - - private void expandAllGroups() { - for (int i = 0; i < listAdapter.getGroupCount(); i++) { - getExpandableListView().expandGroup(i); - } - } - - public OsmandApplication getMyApplication() { - return (OsmandApplication) getActivity().getApplication(); - } - - public DownloadActivity getMyActivity() { - return (DownloadActivity) getActivity(); - } - - private void fillVoiceItemsAdapter(ItemsListBuilder builder) { - if (listAdapter != null) { - listAdapter.clear(); - if (builder.getVoicePromptsItems(voicePromptsType).size() > 0) { - String sectionTitle = getResources().getString(R.string.voices); - listAdapter.add(sectionTitle, builder.getVoicePromptsItems(voicePromptsType)); - } - } - } - - private DownloadActivity getDownloadActivity() { - return (DownloadActivity) getActivity(); - } - - public static VoiceItemsFragment createInstance(VoicePromptsType voicePromptsType) { - Bundle bundle = new Bundle(); - bundle.putString(VOICE_PROMPT_TYPE_KEY, voicePromptsType.name()); - VoiceItemsFragment fragment = new VoiceItemsFragment(); - fragment.setArguments(bundle); - return fragment; - } - - private class VoiceItemsAdapter extends OsmandBaseExpandableListAdapter { - - private Map> data = new LinkedHashMap<>(); - private List sections = new LinkedList<>(); - - public VoiceItemsAdapter(Context ctx) { - TypedArray ta = ctx.getTheme().obtainStyledAttributes(new int[]{android.R.attr.textColorPrimary}); - ta.recycle(); - } - - public void clear() { - data.clear(); - sections.clear(); - notifyDataSetChanged(); - } - - public void add(String section, List list) { - if (!sections.contains(section)) { - sections.add(section); - } - if (!data.containsKey(section)) { - data.put(section, new ArrayList<>()); - } - data.get(section).addAll(list); - } - - @Override - public Object getChild(int groupPosition, int childPosition) { - String section = sections.get(groupPosition); - return data.get(section).get(childPosition); - } - - @Override - public long getChildId(int groupPosition, int childPosition) { - return groupPosition * 10000 + childPosition; - } - - @Override - public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { - - final Object child = getChild(groupPosition, childPosition); - - ItemViewHolder viewHolder; - if (convertView == null) { - convertView = LayoutInflater.from(parent.getContext()) - .inflate(R.layout.two_line_with_images_list_item, parent, false); - viewHolder = new ItemViewHolder(convertView, - getMyActivity(), - getMyApplication().getResourceManager().getDateFormat()); - convertView.setTag(viewHolder); - } else { - viewHolder = (ItemViewHolder) convertView.getTag(); - } - - IndexItem item = (IndexItem) child; - viewHolder.bindIndexItem(item, true, false); - - return convertView; - } - - @Override - public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { - View v = convertView; - String section = getGroup(groupPosition); - - if (v == null) { - LayoutInflater inflater = (LayoutInflater) getDownloadActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE); - v = inflater.inflate(R.layout.download_item_list_section, parent, false); - } - TextView nameView = ((TextView) v.findViewById(R.id.section_name)); - nameView.setText(section); - - v.setOnClickListener(null); - - TypedValue typedValue = new TypedValue(); - Resources.Theme theme = getActivity().getTheme(); - theme.resolveAttribute(R.attr.ctx_menu_info_view_bg, typedValue, true); - v.setBackgroundColor(typedValue.data); - - return v; - } - - @Override - public int getChildrenCount(int groupPosition) { - String section = sections.get(groupPosition); - return data.get(section).size(); - } - - @Override - public String getGroup(int groupPosition) { - return sections.get(groupPosition); - } - - @Override - public int getGroupCount() { - return sections.size(); - } - - @Override - public long getGroupId(int groupPosition) { - return groupPosition; - } - - @Override - public boolean hasStableIds() { - return false; - } - - @Override - public boolean isChildSelectable(int groupPosition, int childPosition) { - return true; - } - } -} diff --git a/OsmAnd/src/net/osmand/plus/download/ui/WorldItemsFragment.java b/OsmAnd/src/net/osmand/plus/download/ui/WorldItemsFragment.java index b5548c87cc..9cbc24d60a 100644 --- a/OsmAnd/src/net/osmand/plus/download/ui/WorldItemsFragment.java +++ b/OsmAnd/src/net/osmand/plus/download/ui/WorldItemsFragment.java @@ -1,11 +1,9 @@ package net.osmand.plus.download.ui; import java.util.ArrayList; -import java.util.LinkedHashMap; -import java.util.LinkedList; import java.util.List; -import java.util.Map; +import net.osmand.plus.IconsCache; import net.osmand.plus.OsmandApplication; import net.osmand.plus.R; import net.osmand.plus.WorldRegion; @@ -13,8 +11,11 @@ import net.osmand.plus.activities.OsmandBaseExpandableListAdapter; import net.osmand.plus.activities.OsmandExpandableListFragment; import net.osmand.plus.download.BaseDownloadActivity; import net.osmand.plus.download.DownloadActivity; +import net.osmand.plus.download.DownloadActivityType; +import net.osmand.plus.download.DownloadResourceGroup; +import net.osmand.plus.download.DownloadResourceGroup.DownloadResourceGroupType; +import net.osmand.plus.download.DownloadResources; import net.osmand.plus.download.IndexItem; -import net.osmand.plus.download.ui.ItemsListBuilder.VoicePromptsType; import android.content.Context; import android.content.res.Resources; import android.content.res.TypedArray; @@ -36,13 +37,7 @@ public class WorldItemsFragment extends OsmandExpandableListFragment { public static final int RELOAD_ID = 0; public static final int SEARCH_ID = 1; - private WorldItemsAdapter listAdapter; - - protected int worldRegionsIndex = -1; - protected int worldMapsIndex = -1; - protected int voicePromptsIndex = -1; - protected int voicePromptsItemsRecordedSubIndex = -1; - protected int voicePromptsItemsTTSSubIndex = -1; + private DownloadResourceGroupAdapter listAdapter; @Override public void onCreate(Bundle savedInstanceState) { @@ -53,9 +48,8 @@ public class WorldItemsFragment extends OsmandExpandableListFragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.download_index_fragment, container, false); - ExpandableListView listView = (ExpandableListView) view.findViewById(android.R.id.list); - listAdapter = new WorldItemsAdapter(getActivity()); + listAdapter = new DownloadResourceGroupAdapter((DownloadActivity) getActivity()); listView.setAdapter(listAdapter); expandAllGroups(); setListView(listView); @@ -63,19 +57,17 @@ public class WorldItemsFragment extends OsmandExpandableListFragment { View usedSpaceCard = inflater.inflate(R.layout.used_space_card, listView, false); getMyActivity().updateDescriptionTextWithSize(usedSpaceCard); listView.addHeaderView(usedSpaceCard); - - onCategorizationFinished(); - + newDownloadIndexes(); return view; } @Override public void onResume() { super.onResume(); - if (!listAdapter.isEmpty()) { expandAllGroups(); } + listAdapter.notifyDataSetChanged(); } private void expandAllGroups() { @@ -92,62 +84,23 @@ public class WorldItemsFragment extends OsmandExpandableListFragment { return (DownloadActivity) getActivity(); } - private void fillWorldItemsAdapter(ItemsListBuilder builder) { - if (listAdapter != null) { - listAdapter.clear(); - if (builder.getRegionMapItems().size() > 0) { - int unusedIndex = 0; - worldRegionsIndex = unusedIndex++; - listAdapter.add(getResources().getString(R.string.world_regions), builder.getRegionsFromAllItems()); - worldMapsIndex = unusedIndex++; - listAdapter.add(getResources().getString(R.string.world_maps), builder.getRegionMapItems()); - - int unusedSubIndex = 0; - List voicePromptsItems = new LinkedList<>(); - if (!builder.isVoicePromptsItemsEmpty(VoicePromptsType.RECORDED)) { - voicePromptsItems.add(ItemsListBuilder.getVoicePromtName(getActivity(), VoicePromptsType.RECORDED)); - voicePromptsItemsRecordedSubIndex = unusedSubIndex++; - } - if (!builder.isVoicePromptsItemsEmpty(VoicePromptsType.TTS)) { - voicePromptsItems.add(ItemsListBuilder.getVoicePromtName(getActivity(), VoicePromptsType.TTS)); - voicePromptsItemsTTSSubIndex = unusedSubIndex; - } - if (!voicePromptsItems.isEmpty()) { - voicePromptsIndex = unusedIndex; - listAdapter.add(getResources().getString(R.string.voices), voicePromptsItems); - } - } - } - } - @Override public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { - if (groupPosition == worldRegionsIndex) { - WorldRegion region = (WorldRegion)listAdapter.getChild(groupPosition, childPosition); - final RegionDialogFragment regionDialogFragment = RegionDialogFragment.createInstance(region.getRegionId()); + Object child = listAdapter.getChild(groupPosition, childPosition); + if (child instanceof DownloadResourceGroup) { + final DownloadResourceGroupFragment regionDialogFragment = DownloadResourceGroupFragment.createInstance(((DownloadResourceGroup) child).getUniqueId()); regionDialogFragment.setOnDismissListener(getDownloadActivity()); getDownloadActivity().showDialog(getActivity(), regionDialogFragment); return true; - } else if (groupPosition == voicePromptsIndex) { - if (childPosition == voicePromptsItemsRecordedSubIndex) { - getDownloadActivity().showDialog(getActivity(), - VoiceDialogFragment.createInstance(VoicePromptsType.RECORDED)); - } else { - getDownloadActivity().showDialog(getActivity(), - VoiceDialogFragment.createInstance(VoicePromptsType.TTS)); - } - }else if (groupPosition == worldMapsIndex) { - if(((ItemViewHolder) v.getTag()).isItemAvailable()) { - IndexItem indexItem = ((ItemsListBuilder.ResourceItem) - listAdapter.getChild(groupPosition, childPosition)).getIndexItem(); - ((BaseDownloadActivity) getActivity()) - .startDownload(indexItem); - - return true; - } + } else if (child instanceof IndexItem) { + IndexItem indexItem = (IndexItem) child; + ((BaseDownloadActivity) getActivity()).startDownload(indexItem); + return true; } return false; } + + @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { @@ -165,7 +118,7 @@ public class WorldItemsFragment extends OsmandExpandableListFragment { switch (item.getItemId()) { case RELOAD_ID: // re-create the thread - DownloadActivity.downloadListIndexThread.runReloadIndexFiles(); + getDownloadActivity().getDownloadThread().runReloadIndexFiles(); return true; case SEARCH_ID: getDownloadActivity().showDialog(getActivity(), SearchDialogFragment.createInstance("")); @@ -178,50 +131,45 @@ public class WorldItemsFragment extends OsmandExpandableListFragment { private DownloadActivity getDownloadActivity() { return (DownloadActivity) getActivity(); } - - public void onCategorizationFinished() { - ItemsListBuilder builder = getDownloadActivity().getItemsBuilder(); - if (builder != null) { - fillWorldItemsAdapter(builder); - listAdapter.notifyDataSetChanged(); - expandAllGroups(); - } + + public void newDownloadIndexes() { + DownloadResources indexes = getDownloadActivity().getDownloadThread().getIndexes(); + listAdapter.update(indexes); + expandAllGroups(); } - private class WorldItemsAdapter extends OsmandBaseExpandableListAdapter { + public static class DownloadResourceGroupAdapter extends OsmandBaseExpandableListAdapter { - private Map> data = new LinkedHashMap<>(); - private List sections = new LinkedList<>(); + private List data = new ArrayList(); + private DownloadActivity ctx; private class SimpleViewHolder { TextView textView; } - public WorldItemsAdapter(Context ctx) { + public DownloadResourceGroupAdapter(DownloadActivity ctx) { + this.ctx = ctx; TypedArray ta = ctx.getTheme().obtainStyledAttributes(new int[]{android.R.attr.textColorPrimary}); ta.recycle(); } public void clear() { data.clear(); - sections.clear(); notifyDataSetChanged(); } - - public void add(String section, List list) { - if (!sections.contains(section)) { - sections.add(section); - } - if (!data.containsKey(section)) { - data.put(section, new ArrayList<>()); - } - data.get(section).addAll(list); + + public void update(DownloadResourceGroup mainGroup) { + data = mainGroup.getGroups(); + notifyDataSetChanged(); } @Override public Object getChild(int groupPosition, int childPosition) { - String section = sections.get(groupPosition); - return data.get(section).get(childPosition); + DownloadResourceGroup drg = data.get(groupPosition); + if(drg.getType().containsIndexItem()) { + return drg.getItemByIndex(childPosition); + } + return drg.getGroupByIndex(childPosition); } @Override @@ -229,72 +177,81 @@ public class WorldItemsFragment extends OsmandExpandableListFragment { return groupPosition * 10000 + childPosition; } - @Override - public int getChildType(int groupPosition, int childPosition) { - if (groupPosition == worldRegionsIndex || groupPosition == voicePromptsIndex) { - return 0; - } else { - return 1; - } - } - - @Override - public int getChildTypeCount() { - return 2; - } @Override public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { - final Object child = getChild(groupPosition, childPosition); - - if (groupPosition == worldRegionsIndex) { - WorldRegion item = (WorldRegion)child; - SimpleViewHolder viewHolder; + if(child instanceof IndexItem) { + IndexItem item = (IndexItem) child; if (convertView == null) { - convertView = LayoutInflater.from(parent.getContext()) - .inflate(R.layout.simple_list_menu_item, parent, false); - viewHolder = new SimpleViewHolder(); - viewHolder.textView = (TextView) convertView.findViewById(R.id.title); - convertView.setTag(viewHolder); - } else { - viewHolder = (SimpleViewHolder) convertView.getTag(); + convertView = LayoutInflater.from(parent.getContext()) + .inflate(R.layout.two_line_with_images_list_item, parent, false); } - Drawable iconLeft = getMyApplication().getIconsCache() - .getContentIcon(R.drawable.ic_world_globe_dark); - viewHolder.textView.setCompoundDrawablesWithIntrinsicBounds(iconLeft, null, null, null); - viewHolder.textView.setText(item.getName()); - - } else if (groupPosition == worldMapsIndex) { - ItemsListBuilder.ResourceItem item = (ItemsListBuilder.ResourceItem) child; ItemViewHolder viewHolder; - if (convertView == null) { - convertView = LayoutInflater.from(parent.getContext()) - .inflate(R.layout.two_line_with_images_list_item, parent, false); - viewHolder = new ItemViewHolder(convertView, - getMyActivity(), - getMyApplication().getResourceManager().getDateFormat()); - convertView.setTag(viewHolder); - } else { + if(convertView.getTag() instanceof ItemViewHolder) { viewHolder = (ItemViewHolder) convertView.getTag(); + } else { + viewHolder = new ItemViewHolder(convertView,ctx); + convertView.setTag(viewHolder); } - viewHolder.bindIndexItem(item.getIndexItem(), false, false); - } else if (groupPosition == voicePromptsIndex) { - String item = (String)child; + viewHolder.bindIndexItem(item, false, false); + } else { + DownloadResourceGroup group = (DownloadResourceGroup) child; SimpleViewHolder viewHolder; if (convertView == null) { convertView = LayoutInflater.from(parent.getContext()) .inflate(R.layout.simple_list_menu_item, parent, false); - viewHolder = new SimpleViewHolder(); - viewHolder.textView = (TextView) convertView.findViewById(R.id.title); - convertView.setTag(viewHolder); - } else { - viewHolder = (SimpleViewHolder) convertView.getTag(); } - Drawable iconLeft = getMyApplication().getIconsCache() - .getContentIcon(R.drawable.ic_action_volume_up); + if(convertView.getTag() instanceof SimpleViewHolder) { + viewHolder = (SimpleViewHolder) convertView.getTag(); + } else { + viewHolder = new SimpleViewHolder(); + convertView.setTag(viewHolder); + } + Drawable iconLeft; + if(group.getType() == DownloadResourceGroupType.VOICE_REC || + group.getType() == DownloadResourceGroupType.VOICE_TTS) { + iconLeft = ctx.getMyApplication().getIconsCache().getContentIcon(R.drawable.ic_action_volume_up); + } else { + IconsCache cache = ctx.getMyApplication().getIconsCache(); + if (group.getParentGroup() == null + || group.getParentGroup().getType() == DownloadResourceGroupType.WORLD) { + iconLeft = cache.getContentIcon(R.drawable.ic_world_globe_dark); + } else { + DownloadResourceGroup ggr = group.getGroupById(DownloadResourceGroupType.REGION_MAPS + .getDefaultId()); + iconLeft = cache.getContentIcon(R.drawable.ic_map); + if (ggr != null && ggr.getIndividualResources() != null) { + IndexItem item = null; + for (IndexItem ii : ggr.getIndividualResources()) { + if (ii.getType() == DownloadActivityType.NORMAL_FILE + || ii.getType() == DownloadActivityType.ROADS_FILE) { + if (ii.isDownloaded() || ii.isOutdated()) { + item = ii; + break; + } + } + } + if (item != null) { + if (item.isOutdated()) { + iconLeft = cache.getIcon(R.drawable.ic_map, + ctx.getResources().getColor(R.color.color_distance)); + } else { + iconLeft = cache.getIcon(R.drawable.ic_map, + ctx.getResources().getColor(R.color.color_ok)); + } + } + } + } + } viewHolder.textView.setCompoundDrawablesWithIntrinsicBounds(iconLeft, null, null, null); - viewHolder.textView.setText(item); + String name = group.getName(); + WorldRegion region = group.getRegion(); + if(region != null) { + name = region.getName(); + } + viewHolder.textView.setText(name); + } return convertView; @@ -306,17 +263,14 @@ public class WorldItemsFragment extends OsmandExpandableListFragment { View v = convertView; String section = getGroup(groupPosition); if (v == null) { - LayoutInflater inflater = (LayoutInflater) getDownloadActivity() - .getSystemService(Context.LAYOUT_INFLATER_SERVICE); + LayoutInflater inflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE); v = inflater.inflate(R.layout.download_item_list_section, parent, false); } TextView nameView = ((TextView) v.findViewById(R.id.section_name)); nameView.setText(section); - v.setOnClickListener(null); - TypedValue typedValue = new TypedValue(); - Resources.Theme theme = getActivity().getTheme(); + Resources.Theme theme = ctx.getTheme(); theme.resolveAttribute(R.attr.ctx_menu_info_view_bg, typedValue, true); v.setBackgroundColor(typedValue.data); @@ -325,18 +279,22 @@ public class WorldItemsFragment extends OsmandExpandableListFragment { @Override public int getChildrenCount(int groupPosition) { - String section = sections.get(groupPosition); - return data.get(section).size(); + return data.get(groupPosition).size(); } @Override public String getGroup(int groupPosition) { - return sections.get(groupPosition); + DownloadResourceGroup drg = data.get(groupPosition); + int rid = drg.getType().getResourceId(); + if(rid != -1) { + return ctx.getString(rid); + } + return ""; } @Override public int getGroupCount() { - return sections.size(); + return data.size(); } @Override @@ -354,4 +312,6 @@ public class WorldItemsFragment extends OsmandExpandableListFragment { return true; } } + + } From 9b53d737e47dcc57fb1e6a47e1ba57fdc6474dd8 Mon Sep 17 00:00:00 2001 From: Victor Shcherb Date: Mon, 19 Oct 2015 02:09:36 +0200 Subject: [PATCH 06/22] Refactoring --- .../plus/download/BaseDownloadActivity.java | 3 +- .../plus/download/DownloadActivity.java | 124 ++++-------- .../plus/download/DownloadIndexesThread.java | 14 +- .../plus/download/DownloadResources.java | 4 + .../ui/ActiveDownloadsDialogFragment.java | 22 ++- .../download/ui/DialogDismissListener.java | 10 - .../ui/DownloadResourceGroupFragment.java | 185 +++++++++++++++--- .../plus/download/ui/ItemViewHolder.java | 31 ++- .../download/ui/LocalIndexesFragment.java | 3 +- .../plus/download/ui/RegionItemsFragment.java | 152 -------------- .../download/ui/SearchDialogFragment.java | 4 +- .../plus/download/ui/SearchItemsFragment.java | 3 +- .../ui/TwoLineWithImagesViewHolder.java | 33 ---- .../download/ui/UpdatesIndexFragment.java | 83 +++----- .../plus/download/ui/WorldItemsFragment.java | 32 ++- 15 files changed, 308 insertions(+), 395 deletions(-) delete mode 100644 OsmAnd/src/net/osmand/plus/download/ui/DialogDismissListener.java delete mode 100644 OsmAnd/src/net/osmand/plus/download/ui/RegionItemsFragment.java delete mode 100644 OsmAnd/src/net/osmand/plus/download/ui/TwoLineWithImagesViewHolder.java diff --git a/OsmAnd/src/net/osmand/plus/download/BaseDownloadActivity.java b/OsmAnd/src/net/osmand/plus/download/BaseDownloadActivity.java index e3f52689e7..d065c33996 100644 --- a/OsmAnd/src/net/osmand/plus/download/BaseDownloadActivity.java +++ b/OsmAnd/src/net/osmand/plus/download/BaseDownloadActivity.java @@ -11,6 +11,7 @@ import net.osmand.plus.OsmandSettings; import net.osmand.plus.R; import net.osmand.plus.Version; import net.osmand.plus.activities.ActionBarProgressActivity; +import net.osmand.plus.download.DownloadIndexesThread.DownloadEvents; import android.app.AlertDialog; import android.app.Dialog; import android.content.ActivityNotFoundException; @@ -24,7 +25,7 @@ import android.support.v4.app.DialogFragment; import android.support.v4.app.Fragment; import android.widget.Toast; -public class BaseDownloadActivity extends ActionBarProgressActivity { +public class BaseDownloadActivity extends ActionBarProgressActivity implements DownloadEvents { protected OsmandSettings settings; private static DownloadIndexesThread downloadListIndexThread; protected Set> fragSet = new HashSet<>(); diff --git a/OsmAnd/src/net/osmand/plus/download/DownloadActivity.java b/OsmAnd/src/net/osmand/plus/download/DownloadActivity.java index d8dc3c5da6..038dc74b81 100644 --- a/OsmAnd/src/net/osmand/plus/download/DownloadActivity.java +++ b/OsmAnd/src/net/osmand/plus/download/DownloadActivity.java @@ -12,15 +12,11 @@ import net.osmand.plus.OsmandSettings; import net.osmand.plus.R; import net.osmand.plus.Version; import net.osmand.plus.activities.LocalIndexInfo; -import net.osmand.plus.activities.OsmAndListFragment; -import net.osmand.plus.activities.OsmandExpandableListFragment; import net.osmand.plus.activities.TabActivity; import net.osmand.plus.base.BasicProgressAsyncTask; +import net.osmand.plus.download.DownloadIndexesThread.DownloadEvents; import net.osmand.plus.download.ui.ActiveDownloadsDialogFragment; -import net.osmand.plus.download.ui.DialogDismissListener; import net.osmand.plus.download.ui.LocalIndexesFragment; -import net.osmand.plus.download.ui.RegionItemsFragment; -import net.osmand.plus.download.ui.SearchDialogFragment; import net.osmand.plus.download.ui.UpdatesIndexFragment; import net.osmand.plus.download.ui.WorldItemsFragment; import net.osmand.plus.views.controls.PagerSlidingTabStrip; @@ -38,14 +34,11 @@ import android.support.v4.view.ViewPager; import android.text.method.LinkMovementMethod; import android.view.MenuItem; import android.view.View; -import android.widget.ArrayAdapter; -import android.widget.BaseExpandableListAdapter; -import android.widget.ExpandableListAdapter; import android.widget.ProgressBar; import android.widget.TextView; -public class DownloadActivity extends BaseDownloadActivity implements DialogDismissListener { +public class DownloadActivity extends BaseDownloadActivity { private List localIndexInfos = new ArrayList<>(); List mTabs = new ArrayList(); @@ -59,14 +52,13 @@ public class DownloadActivity extends BaseDownloadActivity implements DialogDism public static final MessageFormat formatGb = new MessageFormat("{0, number,#.##} GB", Locale.US); private BannerAndDownloadFreeVersion visibleBanner; - private ActiveDownloadsDialogFragment activeDownloads; - @Override protected void onCreate(Bundle savedInstanceState) { getMyApplication().applyTheme(this); super.onCreate(savedInstanceState); - if (!getDownloadThread().isDownloadedFromInternet()) { + DownloadResources indexes = getDownloadThread().getIndexes(); + if (!indexes.isDownloadedFromInternet) { getDownloadThread().runReloadIndexFiles(); } @@ -80,6 +72,7 @@ public class DownloadActivity extends BaseDownloadActivity implements DialogDism currentTab = 2; } } + visibleBanner = new BannerAndDownloadFreeVersion(findViewById(R.id.mainLayout), this); ViewPager viewPager = (ViewPager) findViewById(R.id.pager); PagerSlidingTabStrip mSlidingTabLayout = (PagerSlidingTabStrip) findViewById(R.id.sliding_tabs); @@ -102,7 +95,6 @@ public class DownloadActivity extends BaseDownloadActivity implements DialogDism final String filter = intent.getExtras().getString(FILTER_KEY); final String filterCat = intent.getExtras().getString(FILTER_CAT); } - registerFreeVersionBanner(findViewById(R.id.mainLayout)); } @@ -143,24 +135,16 @@ public class DownloadActivity extends BaseDownloadActivity implements DialogDism @Override @UiThread public void downloadHasFinished() { - - updateBannerInProgress(); - if(activeDownloads != null) { - activeDownloads.notifyDataSetInvalidated(); - } + visibleBanner.updateBannerInProgress(); // FIXME //((DownloadActivity) getActivity()).updateDescriptionTextWithSize(getView()); + for (WeakReference ref : fragSet) { Fragment f = ref.get(); - notifyUpdateDataSetChanged(f); - if (f instanceof UpdatesIndexFragment) { - if (f.isAdded()) { - ((UpdatesIndexFragment) f).updateItemsList(); + if (f.isAdded()) { + if(f instanceof DownloadEvents) { + ((DownloadEvents) f).downloadHasFinished(); } - } else if(f instanceof RegionItemsFragment) { - Fragment innerFragment = ((RegionItemsFragment)f).getChildFragmentManager().findFragmentById(R.id.fragmentContainer); - notifyUpdateDataSetChanged(innerFragment); - } } } @@ -168,93 +152,47 @@ public class DownloadActivity extends BaseDownloadActivity implements DialogDism @Override @UiThread public void downloadInProgress() { - updateBannerInProgress(); - if(activeDownloads != null) { - activeDownloads.notifyDataSetChanged(); - } + visibleBanner.updateBannerInProgress(); 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); + if (f.isAdded()) { + if(f instanceof DownloadEvents) { + ((DownloadEvents) f).downloadInProgress(); + } } } } - private void updateBannerInProgress() { - BasicProgressAsyncTask basicProgressAsyncTask = getDownloadThread().getCurrentRunningTask(); - if (visibleBanner != null) { - final int countedDownloads = getDownloadThread().getCountedDownloads(); - visibleBanner.updateProgress(countedDownloads, basicProgressAsyncTask); - } - } @Override @UiThread public void newDownloadIndexes() { + visibleBanner.updateBannerInProgress(); + // FIXME + //((DownloadActivity) getActivity()).updateDescriptionTextWithSize(getView()); for (WeakReference ref : fragSet) { Fragment f = ref.get(); - if (f instanceof WorldItemsFragment) { - if (f.isAdded()) { - ((WorldItemsFragment) f).newDownloadIndexes(); - } - } else if (f instanceof SearchDialogFragment) { - if (f.isAdded()) { - ((SearchDialogFragment) f).newDownloadIndexes(); - } - } else if (f instanceof LocalIndexesFragment) { - if (f.isAdded()) { - ((LocalIndexesFragment) f).reloadData(); + if (f.isAdded()) { + if(f instanceof DownloadEvents) { + ((DownloadEvents) f).newDownloadIndexes(); } } } downloadHasFinished(); } - public void setActiveDownloads(ActiveDownloadsDialogFragment activeDownloads) { - this.activeDownloads = activeDownloads; - } - private void notifyUpdateDataSetChanged(Fragment f) { - if (f != null && f.isAdded()) { - if(f instanceof OsmandExpandableListFragment) { - ExpandableListAdapter adapter = ((OsmandExpandableListFragment) f).getAdapter(); - if(adapter instanceof BaseExpandableListAdapter) { - ((BaseExpandableListAdapter)adapter).notifyDataSetChanged(); - } - } - if(f instanceof OsmAndListFragment) { - ArrayAdapter adapter = ((OsmAndListFragment) f).getAdapter(); - if(adapter != null) { - adapter.notifyDataSetChanged(); - } - } - } - } public boolean isLightActionBar() { return ((OsmandApplication) getApplication()).getSettings().isLightActionBar(); } - - public void registerFreeVersionBanner(View view) { - visibleBanner = new BannerAndDownloadFreeVersion(view, this); - updateBannerInProgress(); - } - - public void showDialog(FragmentActivity activity, DialogFragment fragment) { fragment.show(activity.getSupportFragmentManager(), "dialog"); } - @Override - public void onDialogDismissed() { - registerFreeVersionBanner(findViewById(R.id.mainLayout)); - } - private static class ToggleCollapseFreeVersionBanner implements View.OnClickListener { private final View freeVersionDescriptionTextView; private final View buttonsLinearLayout; @@ -292,12 +230,12 @@ public class DownloadActivity extends BaseDownloadActivity implements DialogDism private final TextView downloadsLeftTextView; private final View laterButton; - private final FragmentActivity ctx; + private final DownloadActivity ctx; private final OsmandApplication application; private final boolean shouldShowFreeVersionBanner; private final View freeVersionBannerTitle; - public BannerAndDownloadFreeVersion(View view, final FragmentActivity ctx) { + public BannerAndDownloadFreeVersion(View view, final DownloadActivity ctx) { this.ctx = ctx; application = (OsmandApplication) ctx.getApplicationContext(); freeVersionBanner = view.findViewById(R.id.freeVersionBanner); @@ -318,6 +256,7 @@ public class DownloadActivity extends BaseDownloadActivity implements DialogDism initFreeVersionBanner(); updateFreeVersionBanner(); + updateBannerInProgress(); downloadProgressLayout.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { @@ -325,8 +264,14 @@ public class DownloadActivity extends BaseDownloadActivity implements DialogDism } }); } + + public void updateBannerInProgress() { + BasicProgressAsyncTask basicProgressAsyncTask = ctx.getDownloadThread().getCurrentRunningTask(); + final int countedDownloads = ctx.getDownloadThread().getCountedDownloads(); + updateProgress(countedDownloads, basicProgressAsyncTask); + } - public void updateProgress(int countedDownloads, + private void updateProgress(int countedDownloads, BasicProgressAsyncTask basicProgressAsyncTask) { final boolean isFinished = basicProgressAsyncTask == null || basicProgressAsyncTask.getStatus() == AsyncTask.Status.FINISHED; @@ -379,13 +324,14 @@ public class DownloadActivity extends BaseDownloadActivity implements DialogDism } private void updateFreeVersionBanner() { - if (!shouldShowFreeVersionBanner) return; + if (!shouldShowFreeVersionBanner) { + return; + } setMinimizedFreeVersionBanner(false); OsmandSettings settings = application.getSettings(); final Integer mapsDownloaded = settings.NUMBER_OF_FREE_DOWNLOADS.get(); downloadsLeftProgressBar.setProgress(mapsDownloaded); - int downloadsLeft = BaseDownloadActivity.MAXIMUM_AVAILABLE_FREE_DOWNLOADS - - mapsDownloaded; + int downloadsLeft = BaseDownloadActivity.MAXIMUM_AVAILABLE_FREE_DOWNLOADS - mapsDownloaded; downloadsLeft = Math.max(downloadsLeft, 0); if (downloadsLeft <= 0) { laterButton.setVisibility(View.GONE); diff --git a/OsmAnd/src/net/osmand/plus/download/DownloadIndexesThread.java b/OsmAnd/src/net/osmand/plus/download/DownloadIndexesThread.java index bd4103b2e8..b40cfa2dea 100644 --- a/OsmAnd/src/net/osmand/plus/download/DownloadIndexesThread.java +++ b/OsmAnd/src/net/osmand/plus/download/DownloadIndexesThread.java @@ -54,6 +54,16 @@ public class DownloadIndexesThread { private int currentDownloadingItemProgress = 0; private DownloadResources indexes; + + + public interface DownloadEvents { + + void newDownloadIndexes(); + + void downloadInProgress(); + + void downloadHasFinished(); + } public DownloadIndexesThread(Context ctx) { @@ -166,10 +176,6 @@ public class DownloadIndexesThread { } - public boolean isDownloadedFromInternet() { - return indexes.isDownloadedFromInternet; - } - public IndexItem getCurrentDownloadingItem() { return currentDownloadingItem; } diff --git a/OsmAnd/src/net/osmand/plus/download/DownloadResources.java b/OsmAnd/src/net/osmand/plus/download/DownloadResources.java index cca97fe580..8e5f4fff3b 100644 --- a/OsmAnd/src/net/osmand/plus/download/DownloadResources.java +++ b/OsmAnd/src/net/osmand/plus/download/DownloadResources.java @@ -32,6 +32,10 @@ public class DownloadResources extends DownloadResourceGroup { this.region = app.getWorldRegion(); this.app = app; } + + public List getItemsToUpdate() { + return itemsToUpdate; + } public void initAlreadyLoadedFiles() { java.text.DateFormat dateFormat = app.getResourceManager().getDateFormat(); diff --git a/OsmAnd/src/net/osmand/plus/download/ui/ActiveDownloadsDialogFragment.java b/OsmAnd/src/net/osmand/plus/download/ui/ActiveDownloadsDialogFragment.java index 5138f34d65..91b665743a 100644 --- a/OsmAnd/src/net/osmand/plus/download/ui/ActiveDownloadsDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/download/ui/ActiveDownloadsDialogFragment.java @@ -4,6 +4,7 @@ import java.util.ArrayList; import net.osmand.plus.R; import net.osmand.plus.download.DownloadActivity; +import net.osmand.plus.download.DownloadIndexesThread.DownloadEvents; import net.osmand.plus.download.IndexItem; import android.app.AlertDialog; import android.app.Dialog; @@ -16,7 +17,7 @@ import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; -public class ActiveDownloadsDialogFragment extends DialogFragment { +public class ActiveDownloadsDialogFragment extends DialogFragment implements DownloadEvents { private IndexItemAdapter adapter; @@ -27,21 +28,20 @@ public class ActiveDownloadsDialogFragment extends DialogFragment { builder.setTitle(R.string.downloads).setNegativeButton(R.string.shared_string_dismiss, null); adapter = new IndexItemAdapter(getDownloadActivity()); builder.setAdapter(adapter, null); - getDownloadActivity().setActiveDownloads(this); return builder.create(); } - public void notifyDataSetInvalidated() { + public void newDownloadIndexes() { adapter.refreshAllData(); + }; + + @Override + public void downloadHasFinished() { + adapter.refreshAllData(); } - public void notifyDataSetChanged() { + public void downloadInProgress() { adapter.notifyDataSetChanged(); - } - - public void onDetach() { - super.onDetach(); - getDownloadActivity().setActiveDownloads(null); }; @@ -87,7 +87,9 @@ public class ActiveDownloadsDialogFragment extends DialogFragment { } - private static class DownloadEntryViewHolder extends TwoLineWithImagesViewHolder { + + // FIXME review view holder + private static class DownloadEntryViewHolder extends ItemViewHolder { private final Drawable deleteDrawable; private final IndexItemAdapter adapter; diff --git a/OsmAnd/src/net/osmand/plus/download/ui/DialogDismissListener.java b/OsmAnd/src/net/osmand/plus/download/ui/DialogDismissListener.java deleted file mode 100644 index 8a828dfadf..0000000000 --- a/OsmAnd/src/net/osmand/plus/download/ui/DialogDismissListener.java +++ /dev/null @@ -1,10 +0,0 @@ -package net.osmand.plus.download.ui; - -/** - * Used to have smooth transition between dialogs - */ -public interface DialogDismissListener { - - void onDialogDismissed(); - -} diff --git a/OsmAnd/src/net/osmand/plus/download/ui/DownloadResourceGroupFragment.java b/OsmAnd/src/net/osmand/plus/download/ui/DownloadResourceGroupFragment.java index a9da0290b0..92321921fe 100644 --- a/OsmAnd/src/net/osmand/plus/download/ui/DownloadResourceGroupFragment.java +++ b/OsmAnd/src/net/osmand/plus/download/ui/DownloadResourceGroupFragment.java @@ -4,36 +4,46 @@ import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandSettings; import net.osmand.plus.R; import net.osmand.plus.WorldRegion; +import net.osmand.plus.activities.OsmandExpandableListFragment; +import net.osmand.plus.download.BaseDownloadActivity; import net.osmand.plus.download.DownloadActivity; +import net.osmand.plus.download.DownloadActivityType; +import net.osmand.plus.download.DownloadActivity.BannerAndDownloadFreeVersion; +import net.osmand.plus.download.DownloadIndexesThread.DownloadEvents; +import net.osmand.plus.download.DownloadResourceGroup; +import net.osmand.plus.download.DownloadResources; +import net.osmand.plus.download.IndexItem; +import android.app.Dialog; import android.content.DialogInterface; import android.os.Bundle; +import android.support.annotation.NonNull; import android.support.v4.app.DialogFragment; import android.support.v4.app.Fragment; +import android.support.v7.app.AlertDialog; import android.support.v7.widget.Toolbar; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; +import android.widget.ExpandableListView; -public class DownloadResourceGroupFragment extends DialogFragment { +public class DownloadResourceGroupFragment extends DialogFragment implements DownloadEvents { public static final String TAG = "RegionDialogFragment"; private static final String REGION_ID_DLG_KEY = "world_region_dialog_key"; private String regionId; - private DialogDismissListener dialogDismissListener; - private DialogDismissListener listener; + private View view; + private BannerAndDownloadFreeVersion banner; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - boolean isLightTheme = getMyApplication() - .getSettings().OSMAND_THEME.get() == OsmandSettings.OSMAND_LIGHT_THEME; + boolean isLightTheme = getMyApplication().getSettings().OSMAND_THEME.get() == OsmandSettings.OSMAND_LIGHT_THEME; int themeId = isLightTheme ? R.style.OsmandLightTheme : R.style.OsmandDarkTheme; setStyle(STYLE_NO_FRAME, themeId); } @Override - public View onCreateView(LayoutInflater inflater, ViewGroup container, - Bundle savedInstanceState) { - final View view = inflater.inflate(R.layout.maps_in_category_fragment, container, false); + public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { + view = inflater.inflate(R.layout.maps_in_category_fragment, container, false); if (savedInstanceState != null) { regionId = savedInstanceState.getString(REGION_ID_DLG_KEY); @@ -56,24 +66,37 @@ public class DownloadResourceGroupFragment extends DialogFragment { if (regionId.length() > 0) { Fragment fragment = getChildFragmentManager().findFragmentById(R.id.fragmentContainer); if (fragment == null) { - getChildFragmentManager().beginTransaction().add(R.id.fragmentContainer, - RegionItemsFragment.createInstance(regionId)).commit(); + getChildFragmentManager().beginTransaction() + .add(R.id.fragmentContainer, DownloadResourceGroupListFragment.createInstance(regionId)).commit(); } WorldRegion region = getMyApplication().getWorldRegion().getRegionById(regionId); if (region != null) { toolbar.setTitle(region.getName()); } } - getDownloadActivity().registerFreeVersionBanner(view); - listener = new DialogDismissListener() { - @Override - public void onDialogDismissed() { - if (getDownloadActivity() != null) - getDownloadActivity().registerFreeVersionBanner(view); - } - }; + banner = new BannerAndDownloadFreeVersion(view, (DownloadActivity) getActivity()); return view; } + + @Override + public void newDownloadIndexes() { + banner.updateBannerInProgress(); + // FIXME call inner fragment + } + + @Override + public void downloadHasFinished() { + banner.updateBannerInProgress(); + // FIXME call inner fragment + + } + + @Override + public void downloadInProgress() { + banner.updateBannerInProgress(); + // FIXME call inner fragment + + } @Override public void onSaveInstanceState(Bundle outState) { @@ -91,7 +114,6 @@ public class DownloadResourceGroupFragment extends DialogFragment { public void onRegionSelected(String regionId) { final DownloadResourceGroupFragment regionDialogFragment = createInstance(regionId); - regionDialogFragment.setOnDismissListener(listener); getDownloadActivity().showDialog(getActivity(), regionDialogFragment); } @@ -103,15 +125,124 @@ public class DownloadResourceGroupFragment extends DialogFragment { return fragment; } - @Override - public void onDismiss(DialogInterface dialog) { - super.onDismiss(dialog); - if (dialogDismissListener != null) - dialogDismissListener.onDialogDismissed(); + + // FIXME why do we need fragment in fragment??? + public static class DownloadResourceGroupListFragment extends OsmandExpandableListFragment { + public static final String TAG = "RegionItemsFragment"; + private static final String REGION_ID_KEY = "world_region_id_key"; + private String regionId; + private WorldItemsFragment.DownloadResourceGroupAdapter listAdapter; + private DownloadActivity activity; + private DownloadResourceGroup group; + + public static DownloadResourceGroupListFragment createInstance(String regionId) { + Bundle bundle = new Bundle(); + bundle.putString(REGION_ID_KEY, regionId); + DownloadResourceGroupListFragment fragment = new DownloadResourceGroupListFragment(); + fragment.setArguments(bundle); + return fragment; + } + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setHasOptionsMenu(true); + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { + View view = inflater.inflate(R.layout.download_items_fragment, container, false); + + if (savedInstanceState != null) { + regionId = savedInstanceState.getString(REGION_ID_KEY); + } + if (regionId == null) { + regionId = getArguments().getString(REGION_ID_KEY); + } + if (regionId == null) + regionId = ""; + + ExpandableListView listView = (ExpandableListView) view.findViewById(android.R.id.list); + activity = (DownloadActivity) getActivity(); + DownloadResources indexes = activity.getDownloadThread().getIndexes(); + group = indexes.getGroupById(regionId); + listAdapter = new WorldItemsFragment.DownloadResourceGroupAdapter(activity); + listView.setAdapter(listAdapter); + setListView(listView); + if (group != null) { + listAdapter.update(group); + } + expandAllGroups(); + return view; + } + + @Override + public void onSaveInstanceState(Bundle outState) { + outState.putString(REGION_ID_KEY, regionId); + super.onSaveInstanceState(outState); + } + + @Override + public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { + Object child = listAdapter.getChild(groupPosition, childPosition); + if (child instanceof DownloadResourceGroup) { + String uniqueId = ((DownloadResourceGroup) child).getUniqueId(); + final DownloadResourceGroupFragment regionDialogFragment = DownloadResourceGroupFragment.createInstance(uniqueId); + ((DownloadActivity) getActivity()).showDialog(getActivity(), regionDialogFragment); + return true; + } else if (child instanceof IndexItem) { + IndexItem indexItem = (IndexItem) child; + if (indexItem.getType() == DownloadActivityType.ROADS_FILE) { + // FIXME + // if (regularMap.getType() == DownloadActivityType.NORMAL_FILE + // && regularMap.isAlreadyDownloaded(getMyActivity().getIndexFileNames())) { + // ConfirmDownloadUnneededMapDialogFragment.createInstance(indexItem) + // .show(getChildFragmentManager(), "dialog"); + // return true; + // } + } + ((DownloadActivity) getActivity()).startDownload(indexItem); + return true; + } + return false; + } + + private void expandAllGroups() { + for (int i = 0; i < listAdapter.getGroupCount(); i++) { + getExpandableListView().expandGroup(i); + } + } + } - public void setOnDismissListener(DialogDismissListener listener) { - this.dialogDismissListener = listener; - } + public static class ConfirmDownloadUnneededMapDialogFragment extends DialogFragment { + private static final String INDEX_ITEM = "index_item"; + private static IndexItem item = null; + @NonNull + @Override + public Dialog onCreateDialog(Bundle savedInstanceState) { + final IndexItem indexItem = item; + AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); + builder.setTitle(R.string.are_you_sure); + builder.setMessage(R.string.confirm_download_roadmaps); + builder.setNegativeButton(R.string.shared_string_cancel, null).setPositiveButton( + R.string.shared_string_download, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + if (indexItem != null) { + ((DownloadActivity) getActivity()).startDownload(indexItem); + } + } + }); + return builder.create(); + } + + public static ConfirmDownloadUnneededMapDialogFragment createInstance(@NonNull IndexItem indexItem) { + ConfirmDownloadUnneededMapDialogFragment fragment = new ConfirmDownloadUnneededMapDialogFragment(); + Bundle args = new Bundle(); + fragment.setArguments(args); + return fragment; + } + } } \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/plus/download/ui/ItemViewHolder.java b/OsmAnd/src/net/osmand/plus/download/ui/ItemViewHolder.java index e9a2825b0b..b1d8db5bb4 100644 --- a/OsmAnd/src/net/osmand/plus/download/ui/ItemViewHolder.java +++ b/OsmAnd/src/net/osmand/plus/download/ui/ItemViewHolder.java @@ -6,8 +6,11 @@ import android.graphics.drawable.Drawable; import android.net.Uri; import android.util.TypedValue; import android.view.View; +import android.widget.Button; +import android.widget.ImageView; +import android.widget.ProgressBar; +import android.widget.TextView; import android.widget.Toast; - import net.osmand.access.AccessibleToast; import net.osmand.plus.OsmandPlugin; import net.osmand.plus.R; @@ -22,10 +25,19 @@ import net.osmand.plus.srtmplugin.SRTMPlugin; import java.text.DateFormat; import java.util.Map; -public class ItemViewHolder extends TwoLineWithImagesViewHolder { +public class ItemViewHolder { private final java.text.DateFormat dateFormat; + protected final TextView nameTextView; + protected final TextView descrTextView; + protected final ImageView leftImageView; + protected final ImageView rightImageButton; + protected final Button rightButton; + protected final ProgressBar progressBar; + protected final TextView mapDateTextView; + protected final DownloadActivity context; + private boolean srtmDisabled; private boolean nauticalPluginDisabled; private boolean freeVersion; @@ -40,10 +52,23 @@ public class ItemViewHolder extends TwoLineWithImagesViewHolder { ASK_FOR_SRTM_PLUGIN_ENABLE, ASK_FOR_FULL_VERSION_PURCHASE } + + + public ItemViewHolder(View view, DownloadActivity context) { + this.context = context; + progressBar = (ProgressBar) view.findViewById(R.id.progressBar); + mapDateTextView = (TextView) view.findViewById(R.id.mapDateTextView); + rightButton = (Button) view.findViewById(R.id.rightButton); + leftImageView = (ImageView) view.findViewById(R.id.leftImageView); + descrTextView = (TextView) view.findViewById(R.id.description); + rightImageButton = (ImageView) view.findViewById(R.id.rightImageButton); + nameTextView = (TextView) view.findViewById(R.id.name); + } + public ItemViewHolder(View convertView, DownloadActivity context) { - super(convertView, context); + this(convertView, context); this.dateFormat = context.getMyApplication().getResourceManager().getDateFormat(); diff --git a/OsmAnd/src/net/osmand/plus/download/ui/LocalIndexesFragment.java b/OsmAnd/src/net/osmand/plus/download/ui/LocalIndexesFragment.java index e58d62ad28..d9bfded238 100644 --- a/OsmAnd/src/net/osmand/plus/download/ui/LocalIndexesFragment.java +++ b/OsmAnd/src/net/osmand/plus/download/ui/LocalIndexesFragment.java @@ -50,6 +50,7 @@ import net.osmand.plus.activities.OsmandExpandableListFragment; import net.osmand.plus.dialogs.DirectionsDialogs; import net.osmand.plus.download.DownloadActivity; import net.osmand.plus.download.DownloadActivityType; +import net.osmand.plus.download.DownloadIndexesThread.DownloadEvents; import net.osmand.plus.download.IndexItem; import net.osmand.plus.helpers.FileNameTranslationHelper; import net.osmand.plus.resources.IncrementalChangesManager; @@ -71,7 +72,7 @@ import java.util.Map; import java.util.Set; -public class LocalIndexesFragment extends OsmandExpandableListFragment { +public class LocalIndexesFragment extends OsmandExpandableListFragment implements DownloadEvents { private LoadLocalIndexTask asyncLoader; private LocalIndexesAdapter listAdapter; diff --git a/OsmAnd/src/net/osmand/plus/download/ui/RegionItemsFragment.java b/OsmAnd/src/net/osmand/plus/download/ui/RegionItemsFragment.java deleted file mode 100644 index 1f0ec6d3ee..0000000000 --- a/OsmAnd/src/net/osmand/plus/download/ui/RegionItemsFragment.java +++ /dev/null @@ -1,152 +0,0 @@ -package net.osmand.plus.download.ui; - -import net.osmand.plus.OsmandApplication; -import net.osmand.plus.R; -import net.osmand.plus.WorldRegion; -import net.osmand.plus.activities.OsmandExpandableListFragment; -import net.osmand.plus.download.BaseDownloadActivity; -import net.osmand.plus.download.DownloadActivity; -import net.osmand.plus.download.DownloadActivityType; -import net.osmand.plus.download.DownloadResourceGroup; -import net.osmand.plus.download.DownloadResources; -import net.osmand.plus.download.IndexItem; -import android.app.Dialog; -import android.content.DialogInterface; -import android.os.Bundle; -import android.support.annotation.NonNull; -import android.support.v4.app.DialogFragment; -import android.support.v7.app.AlertDialog; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; -import android.widget.ExpandableListView; - -public class RegionItemsFragment extends OsmandExpandableListFragment { - public static final String TAG = "RegionItemsFragment"; - private static final String REGION_ID_KEY = "world_region_id_key"; - private String regionId; - private WorldItemsFragment.DownloadResourceGroupAdapter listAdapter; - private DownloadActivity activity; - private DownloadResourceGroup group; - - public static RegionItemsFragment createInstance(String regionId) { - Bundle bundle = new Bundle(); - bundle.putString(REGION_ID_KEY, regionId); - RegionItemsFragment fragment = new RegionItemsFragment(); - fragment.setArguments(bundle); - return fragment; - } - - @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setHasOptionsMenu(true); - } - - @Override - public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - View view = inflater.inflate(R.layout.download_items_fragment, container, false); - - if (savedInstanceState != null) { - regionId = savedInstanceState.getString(REGION_ID_KEY); - } - if (regionId == null) { - regionId = getArguments().getString(REGION_ID_KEY); - } - if (regionId == null) - regionId = ""; - - ExpandableListView listView = (ExpandableListView) view.findViewById(android.R.id.list); - activity = (DownloadActivity) getActivity(); - DownloadResources indexes = activity.getDownloadThread().getIndexes(); - group = indexes.getGroupById(regionId); - listAdapter = new WorldItemsFragment.DownloadResourceGroupAdapter(activity); - listView.setAdapter(listAdapter); - setListView(listView); - if(group != null) { - listAdapter.update(group); - } - expandAllGroups(); - return view; - } - - @Override - public void onSaveInstanceState(Bundle outState) { - outState.putString(REGION_ID_KEY, regionId); - super.onSaveInstanceState(outState); - } - - - @Override - public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { - Object child = listAdapter.getChild(groupPosition, childPosition); - if (child instanceof DownloadResourceGroup) { - String uniqueId = ((DownloadResourceGroup) child).getUniqueId(); - final DownloadResourceGroupFragment regionDialogFragment = DownloadResourceGroupFragment.createInstance(); - regionDialogFragment.setOnDismissListener(getDownloadActivity()); - getDownloadActivity().showDialog(getActivity(), regionDialogFragment); - ((DownloadResourceGroupFragment) getParentFragment()).onRegionSelected(uniqueId); - return true; - } else if (child instanceof IndexItem) { - IndexItem indexItem = (IndexItem) child; - if (indexItem.getType() == DownloadActivityType.ROADS_FILE) { - // FIXME -// if (regularMap.getType() == DownloadActivityType.NORMAL_FILE -// && regularMap.isAlreadyDownloaded(getMyActivity().getIndexFileNames())) { -// ConfirmDownloadUnneededMapDialogFragment.createInstance(indexItem) -// .show(getChildFragmentManager(), "dialog"); -// return true; -// } - } - ((BaseDownloadActivity) getActivity()).startDownload(indexItem); - return true; - } - return false; - } - - private void expandAllGroups() { - for (int i = 0; i < listAdapter.getGroupCount(); i++) { - getExpandableListView().expandGroup(i); - } - } - - - private DownloadActivity getDownloadActivity() { - return (DownloadActivity) getActivity(); - } - - - - public static class ConfirmDownloadUnneededMapDialogFragment extends DialogFragment { - private static final String INDEX_ITEM = "index_item"; - private static IndexItem item = null; - - @NonNull - @Override - public Dialog onCreateDialog(Bundle savedInstanceState) { - final IndexItem indexItem = item; - AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); - builder.setTitle(R.string.are_you_sure); - builder.setMessage(R.string.confirm_download_roadmaps); - builder.setNegativeButton(R.string.shared_string_cancel, null) - .setPositiveButton(R.string.shared_string_download, - new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - if(indexItem != null) { - ((DownloadActivity) getActivity()).startDownload(indexItem); - } - } - }); - return builder.create(); - } - - public static ConfirmDownloadUnneededMapDialogFragment createInstance(@NonNull IndexItem indexItem) { - ConfirmDownloadUnneededMapDialogFragment fragment = - new ConfirmDownloadUnneededMapDialogFragment(); - Bundle args = new Bundle(); - fragment.setArguments(args); - return fragment; - } - } -} diff --git a/OsmAnd/src/net/osmand/plus/download/ui/SearchDialogFragment.java b/OsmAnd/src/net/osmand/plus/download/ui/SearchDialogFragment.java index e365e79acf..8df0570dfb 100644 --- a/OsmAnd/src/net/osmand/plus/download/ui/SearchDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/download/ui/SearchDialogFragment.java @@ -10,13 +10,13 @@ import android.view.ViewGroup; import android.widget.RelativeLayout; import android.widget.RelativeLayout.LayoutParams; import android.widget.SearchView; - import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandSettings; import net.osmand.plus.R; import net.osmand.plus.download.DownloadActivity; +import net.osmand.plus.download.DownloadIndexesThread.DownloadEvents; -public class SearchDialogFragment extends DialogFragment { +public class SearchDialogFragment extends DialogFragment implements DownloadEvents { public static final String TAG = "SearchDialogFragment"; private static final String SEARCH_TEXT_DLG_KEY = "search_text_dlg_key"; diff --git a/OsmAnd/src/net/osmand/plus/download/ui/SearchItemsFragment.java b/OsmAnd/src/net/osmand/plus/download/ui/SearchItemsFragment.java index d48eb33ae5..648b2dd175 100644 --- a/OsmAnd/src/net/osmand/plus/download/ui/SearchItemsFragment.java +++ b/OsmAnd/src/net/osmand/plus/download/ui/SearchItemsFragment.java @@ -14,6 +14,7 @@ import net.osmand.plus.R; import net.osmand.plus.WorldRegion; import net.osmand.plus.download.BaseDownloadActivity; import net.osmand.plus.download.DownloadActivity; +import net.osmand.plus.download.DownloadIndexesThread.DownloadEvents; import net.osmand.plus.download.IndexItem; import android.content.Context; import android.content.res.TypedArray; @@ -28,7 +29,7 @@ import android.widget.Filter; import android.widget.Filterable; import android.widget.ListView; -public class SearchItemsFragment extends Fragment { +public class SearchItemsFragment extends Fragment implements DownloadEvents { public static final String TAG = "SearchItemsFragment"; private SearchItemsAdapter listAdapter; diff --git a/OsmAnd/src/net/osmand/plus/download/ui/TwoLineWithImagesViewHolder.java b/OsmAnd/src/net/osmand/plus/download/ui/TwoLineWithImagesViewHolder.java deleted file mode 100644 index 8ee416b7b5..0000000000 --- a/OsmAnd/src/net/osmand/plus/download/ui/TwoLineWithImagesViewHolder.java +++ /dev/null @@ -1,33 +0,0 @@ -package net.osmand.plus.download.ui; - -import net.osmand.plus.R; -import net.osmand.plus.download.DownloadActivity; -import android.view.View; -import android.widget.Button; -import android.widget.ImageView; -import android.widget.ProgressBar; -import android.widget.TextView; - -/** - */ -public class TwoLineWithImagesViewHolder { - protected final TextView nameTextView; - protected final TextView descrTextView; - protected final ImageView leftImageView; - protected final ImageView rightImageButton; - protected final Button rightButton; - protected final ProgressBar progressBar; - protected final TextView mapDateTextView; - protected final DownloadActivity context; - - public TwoLineWithImagesViewHolder(View view, DownloadActivity context) { - this.context = context; - progressBar = (ProgressBar) view.findViewById(R.id.progressBar); - mapDateTextView = (TextView) view.findViewById(R.id.mapDateTextView); - rightButton = (Button) view.findViewById(R.id.rightButton); - leftImageView = (ImageView) view.findViewById(R.id.leftImageView); - descrTextView = (TextView) view.findViewById(R.id.description); - rightImageButton = (ImageView) view.findViewById(R.id.rightImageButton); - nameTextView = (TextView) view.findViewById(R.id.name); - } -} diff --git a/OsmAnd/src/net/osmand/plus/download/ui/UpdatesIndexFragment.java b/OsmAnd/src/net/osmand/plus/download/ui/UpdatesIndexFragment.java index 87026bb9da..aaeaf20b0c 100644 --- a/OsmAnd/src/net/osmand/plus/download/ui/UpdatesIndexFragment.java +++ b/OsmAnd/src/net/osmand/plus/download/ui/UpdatesIndexFragment.java @@ -1,23 +1,19 @@ package net.osmand.plus.download.ui; import java.text.ParseException; -import java.util.ArrayList; import java.util.Comparator; import java.util.Date; import java.util.List; import java.util.Map; -import net.osmand.PlatformUtil; import net.osmand.map.OsmandRegions; import net.osmand.plus.OsmandApplication; import net.osmand.plus.R; import net.osmand.plus.activities.OsmAndListFragment; -import net.osmand.plus.download.BaseDownloadActivity; import net.osmand.plus.download.DownloadActivity; +import net.osmand.plus.download.DownloadIndexesThread.DownloadEvents; +import net.osmand.plus.download.DownloadResources; import net.osmand.plus.download.IndexItem; - -import org.apache.commons.logging.Log; - import android.app.Activity; import android.content.Context; import android.os.Bundle; @@ -33,19 +29,14 @@ import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.TextView; -public class UpdatesIndexFragment extends OsmAndListFragment { - private static final Log LOG = PlatformUtil.getLog(UpdateIndexAdapter.class); +public class UpdatesIndexFragment extends OsmAndListFragment implements DownloadEvents { private static final int RELOAD_ID = 5; private UpdateIndexAdapter listAdapter; - List indexItems = new ArrayList<>(); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - if (BaseDownloadActivity.downloadListIndexThread != null) { - indexItems = new ArrayList<>(DownloadActivity.downloadListIndexThread.getItemsToUpdate()); - } - createListView(); + invalidateListView(); setHasOptionsMenu(true); } @@ -58,11 +49,27 @@ public class UpdatesIndexFragment extends OsmAndListFragment { public ArrayAdapter getAdapter() { return listAdapter; } + + @Override + public void downloadHasFinished() { + invalidateListView(); + } + + @Override + public void downloadInProgress() { + listAdapter.notifyDataSetChanged(); + } + + @Override + public void newDownloadIndexes() { + invalidateListView(); + } - private void createListView() { - updateUpdateAllButton(); + public void invalidateListView() { + DownloadResources indexes = getMyActivity().getDownloadThread().getIndexes(); + List indexItems = indexes.getItemsToUpdate(); if (indexItems.size() == 0) { - if (DownloadActivity.downloadListIndexThread.isDownloadedFromInternet()) { + if (indexes.isDownloadedFromInternet) { indexItems.add(new IndexItem(getString(R.string.everything_up_to_date), "", 0, "", 0, 0, null)); } else { indexItems.add(new IndexItem(getString(R.string.no_index_file_to_download), "", 0, "", 0, 0, null)); @@ -78,10 +85,11 @@ public class UpdatesIndexFragment extends OsmAndListFragment { .compareTo(indexItem2.getVisibleName(getMyApplication(), osmandRegions)); } }); + updateUpdateAllButton(indexes.getItemsToUpdate()); setListAdapter(listAdapter); } - private void updateUpdateAllButton() { + private void updateUpdateAllButton(final List indexItems) { View view = getView(); if (getView() == null) { return; @@ -111,17 +119,6 @@ public class UpdatesIndexFragment extends OsmAndListFragment { super.onResume(); } - public void updateItemsList() { - if (listAdapter == null) { - return; - } - List items = BaseDownloadActivity.downloadListIndexThread.getItemsToUpdate(); - if(items != null) { - indexItems = new ArrayList(items); - createListView(); - } - } - @Override public void onListItemClick(ListView l, View v, int position, long id) { final IndexItem e = (IndexItem) getListAdapter().getItem(position); @@ -132,9 +129,9 @@ public class UpdatesIndexFragment extends OsmAndListFragment { return (DownloadActivity) getActivity(); } + @SuppressWarnings("deprecation") @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { - updateUpdateAllButton(); ActionBar actionBar = getMyActivity().getSupportActionBar(); actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD); @@ -153,7 +150,7 @@ public class UpdatesIndexFragment extends OsmAndListFragment { public boolean onOptionsItemSelected(MenuItem item) { if (item.getItemId() == RELOAD_ID) { // re-create the thread - DownloadActivity.downloadListIndexThread.runReloadIndexFiles(); + getMyActivity().getDownloadThread().runReloadIndexFiles(); return true; } return super.onOptionsItemSelected(item); @@ -167,42 +164,23 @@ public class UpdatesIndexFragment extends OsmAndListFragment { this.items = items; } - public List getIndexFiles() { - return items; - } - @Override public View getView(final int position, final View convertView, final ViewGroup parent) { View v = convertView; if (v == null) { LayoutInflater inflater = LayoutInflater.from(getMyActivity()); - v = inflater.inflate(R.layout.two_line_with_images_list_item, null); + v = inflater.inflate(R.layout.two_line_with_images_list_item, parent, false); v.setTag(new UpdateViewHolder(v, getMyActivity())); } UpdateViewHolder holder = (UpdateViewHolder) v.getTag(); holder.bindUpdatesIndexItem(items.get(position)); return v; } - - public void setIndexFiles(List filtered) { - clear(); - for (IndexItem item : filtered) { - add(item); - } - final OsmandRegions osmandRegions = - getMyApplication().getResourceManager().getOsmandRegions(); - sort(new Comparator() { - @Override - public int compare(IndexItem indexItem, IndexItem indexItem2) { - return indexItem.getVisibleName(getMyApplication(), osmandRegions).compareTo(indexItem2.getVisibleName(getMyApplication(), osmandRegions)); - } - }); - } } - private static class UpdateViewHolder extends TwoLineWithImagesViewHolder { + // FIXME review and delete if duplicate + private static class UpdateViewHolder extends ItemViewHolder { private final java.text.DateFormat format; - private UpdateViewHolder(View convertView, final DownloadActivity context) { super(convertView, context); @@ -245,6 +223,7 @@ public class UpdatesIndexFragment extends OsmAndListFragment { } + // FIXME general method for all maps private String getMapDescription(IndexItem item) { String typeName = getTypeName(item, item.getType().getStringResource()); String date = item.getDate(format); diff --git a/OsmAnd/src/net/osmand/plus/download/ui/WorldItemsFragment.java b/OsmAnd/src/net/osmand/plus/download/ui/WorldItemsFragment.java index 9cbc24d60a..6b688d3718 100644 --- a/OsmAnd/src/net/osmand/plus/download/ui/WorldItemsFragment.java +++ b/OsmAnd/src/net/osmand/plus/download/ui/WorldItemsFragment.java @@ -9,9 +9,9 @@ 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.download.BaseDownloadActivity; import net.osmand.plus.download.DownloadActivity; import net.osmand.plus.download.DownloadActivityType; +import net.osmand.plus.download.DownloadIndexesThread.DownloadEvents; import net.osmand.plus.download.DownloadResourceGroup; import net.osmand.plus.download.DownloadResourceGroup.DownloadResourceGroupType; import net.osmand.plus.download.DownloadResources; @@ -32,7 +32,7 @@ import android.view.ViewGroup; import android.widget.ExpandableListView; import android.widget.TextView; -public class WorldItemsFragment extends OsmandExpandableListFragment { +public class WorldItemsFragment extends OsmandExpandableListFragment implements DownloadEvents { public static final String TAG = "WorldItemsFragment"; public static final int RELOAD_ID = 0; @@ -69,6 +69,23 @@ public class WorldItemsFragment extends OsmandExpandableListFragment { } listAdapter.notifyDataSetChanged(); } + + @Override + public void downloadHasFinished() { + listAdapter.notifyDataSetChanged(); + } + + @Override + public void downloadInProgress() { + listAdapter.notifyDataSetChanged(); + } + + @Override + public void newDownloadIndexes() { + DownloadResources indexes = getDownloadActivity().getDownloadThread().getIndexes(); + listAdapter.update(indexes); + expandAllGroups(); + } private void expandAllGroups() { for (int i = 0; i < listAdapter.getGroupCount(); i++) { @@ -89,12 +106,11 @@ public class WorldItemsFragment extends OsmandExpandableListFragment { Object child = listAdapter.getChild(groupPosition, childPosition); if (child instanceof DownloadResourceGroup) { final DownloadResourceGroupFragment regionDialogFragment = DownloadResourceGroupFragment.createInstance(((DownloadResourceGroup) child).getUniqueId()); - regionDialogFragment.setOnDismissListener(getDownloadActivity()); - getDownloadActivity().showDialog(getActivity(), regionDialogFragment); + ((DownloadActivity) getActivity()).showDialog(getActivity(), regionDialogFragment); return true; } else if (child instanceof IndexItem) { IndexItem indexItem = (IndexItem) child; - ((BaseDownloadActivity) getActivity()).startDownload(indexItem); + ((DownloadActivity) getActivity()).startDownload(indexItem); return true; } return false; @@ -132,11 +148,7 @@ public class WorldItemsFragment extends OsmandExpandableListFragment { return (DownloadActivity) getActivity(); } - public void newDownloadIndexes() { - DownloadResources indexes = getDownloadActivity().getDownloadThread().getIndexes(); - listAdapter.update(indexes); - expandAllGroups(); - } + public static class DownloadResourceGroupAdapter extends OsmandBaseExpandableListAdapter { From 3cc2d941e5062d1197ed0be1c11e804c15e97794 Mon Sep 17 00:00:00 2001 From: Victor Shcherb Date: Mon, 19 Oct 2015 02:33:11 +0200 Subject: [PATCH 07/22] Update fragment --- .../res/layout/maps_in_category_fragment.xml | 15 +- .../ui/DownloadResourceGroupFragment.java | 209 +++++++----------- .../download/ui/LocalIndexesFragment.java | 15 ++ 3 files changed, 111 insertions(+), 128 deletions(-) diff --git a/OsmAnd/res/layout/maps_in_category_fragment.xml b/OsmAnd/res/layout/maps_in_category_fragment.xml index 75c248125b..160b529315 100644 --- a/OsmAnd/res/layout/maps_in_category_fragment.xml +++ b/OsmAnd/res/layout/maps_in_category_fragment.xml @@ -28,8 +28,15 @@ - + + \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/plus/download/ui/DownloadResourceGroupFragment.java b/OsmAnd/src/net/osmand/plus/download/ui/DownloadResourceGroupFragment.java index 92321921fe..41c8d9994b 100644 --- a/OsmAnd/src/net/osmand/plus/download/ui/DownloadResourceGroupFragment.java +++ b/OsmAnd/src/net/osmand/plus/download/ui/DownloadResourceGroupFragment.java @@ -3,12 +3,9 @@ package net.osmand.plus.download.ui; import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandSettings; import net.osmand.plus.R; -import net.osmand.plus.WorldRegion; -import net.osmand.plus.activities.OsmandExpandableListFragment; -import net.osmand.plus.download.BaseDownloadActivity; import net.osmand.plus.download.DownloadActivity; -import net.osmand.plus.download.DownloadActivityType; import net.osmand.plus.download.DownloadActivity.BannerAndDownloadFreeVersion; +import net.osmand.plus.download.DownloadActivityType; import net.osmand.plus.download.DownloadIndexesThread.DownloadEvents; import net.osmand.plus.download.DownloadResourceGroup; import net.osmand.plus.download.DownloadResources; @@ -18,20 +15,25 @@ import android.content.DialogInterface; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.v4.app.DialogFragment; -import android.support.v4.app.Fragment; import android.support.v7.app.AlertDialog; import android.support.v7.widget.Toolbar; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ExpandableListView; +import android.widget.ExpandableListView.OnChildClickListener; -public class DownloadResourceGroupFragment extends DialogFragment implements DownloadEvents { +public class DownloadResourceGroupFragment extends DialogFragment implements DownloadEvents, OnChildClickListener { public static final String TAG = "RegionDialogFragment"; private static final String REGION_ID_DLG_KEY = "world_region_dialog_key"; - private String regionId; + private String groupId; private View view; private BannerAndDownloadFreeVersion banner; + protected ExpandableListView listView; + protected WorldItemsFragment.DownloadResourceGroupAdapter listAdapter; + private DownloadResourceGroup group; + private DownloadActivity activity; + private Toolbar toolbar; @Override public void onCreate(Bundle savedInstanceState) { @@ -39,22 +41,24 @@ public class DownloadResourceGroupFragment extends DialogFragment implements Dow boolean isLightTheme = getMyApplication().getSettings().OSMAND_THEME.get() == OsmandSettings.OSMAND_LIGHT_THEME; int themeId = isLightTheme ? R.style.OsmandLightTheme : R.style.OsmandDarkTheme; setStyle(STYLE_NO_FRAME, themeId); + setHasOptionsMenu(true); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { view = inflater.inflate(R.layout.maps_in_category_fragment, container, false); - if (savedInstanceState != null) { - regionId = savedInstanceState.getString(REGION_ID_DLG_KEY); + groupId = savedInstanceState.getString(REGION_ID_DLG_KEY); } - if (regionId == null) { - regionId = getArguments().getString(REGION_ID_DLG_KEY); + if (groupId == null) { + groupId = getArguments().getString(REGION_ID_DLG_KEY); } - if (regionId == null) - regionId = ""; + if (groupId == null) { + groupId = ""; + } + activity = (DownloadActivity) getActivity(); - Toolbar toolbar = (Toolbar) view.findViewById(R.id.toolbar); + toolbar = (Toolbar) view.findViewById(R.id.toolbar); toolbar.setNavigationIcon(getMyApplication().getIconsCache().getIcon(R.drawable.abc_ic_ab_back_mtrl_am_alpha)); toolbar.setNavigationOnClickListener(new View.OnClickListener() { @Override @@ -63,44 +67,91 @@ public class DownloadResourceGroupFragment extends DialogFragment implements Dow } }); - if (regionId.length() > 0) { - Fragment fragment = getChildFragmentManager().findFragmentById(R.id.fragmentContainer); - if (fragment == null) { - getChildFragmentManager().beginTransaction() - .add(R.id.fragmentContainer, DownloadResourceGroupListFragment.createInstance(regionId)).commit(); - } - WorldRegion region = getMyApplication().getWorldRegion().getRegionById(regionId); - if (region != null) { - toolbar.setTitle(region.getName()); - } - } + setHasOptionsMenu(true); + banner = new BannerAndDownloadFreeVersion(view, (DownloadActivity) getActivity()); + + listView = (ExpandableListView) view.findViewById(android.R.id.list); + listView.setOnChildClickListener(this); + listAdapter = new WorldItemsFragment.DownloadResourceGroupAdapter(activity); + listView.setAdapter(listAdapter); + + reloadData(); return view; } - + + private void reloadData() { + DownloadResources indexes = activity.getDownloadThread().getIndexes(); + group = indexes.getGroupById(groupId); + if (group != null) { + listAdapter.update(group); + if (group.getRegion() != null) { + toolbar.setTitle(group.getRegion().getName()); + } + } + expandAllGroups(); + } + + private void expandAllGroups() { + for (int i = 0; i < listAdapter.getGroupCount(); i++) { + listView.expandGroup(i); + } + } + + @Override + public void onActivityCreated(Bundle savedInstanceState) { + super.onActivityCreated(savedInstanceState); + listView.setBackgroundColor(getResources().getColor( + getMyApplication().getSettings().isLightContent() ? R.color.bg_color_light : R.color.bg_color_dark)); + } + @Override public void newDownloadIndexes() { banner.updateBannerInProgress(); - // FIXME call inner fragment + reloadData(); } - + @Override public void downloadHasFinished() { banner.updateBannerInProgress(); - // FIXME call inner fragment - + listAdapter.notifyDataSetChanged(); } - + @Override public void downloadInProgress() { banner.updateBannerInProgress(); - // FIXME call inner fragment - + listAdapter.notifyDataSetChanged(); + } + + @Override + public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { + Object child = listAdapter.getChild(groupPosition, childPosition); + if (child instanceof DownloadResourceGroup) { + String uniqueId = ((DownloadResourceGroup) child).getUniqueId(); + final DownloadResourceGroupFragment regionDialogFragment = DownloadResourceGroupFragment + .createInstance(uniqueId); + ((DownloadActivity) getActivity()).showDialog(getActivity(), regionDialogFragment); + return true; + } else if (child instanceof IndexItem) { + IndexItem indexItem = (IndexItem) child; + if (indexItem.getType() == DownloadActivityType.ROADS_FILE) { + // FIXME + // if (regularMap.getType() == DownloadActivityType.NORMAL_FILE + // && regularMap.isAlreadyDownloaded(getMyActivity().getIndexFileNames())) { + // ConfirmDownloadUnneededMapDialogFragment.createInstance(indexItem) + // .show(getChildFragmentManager(), "dialog"); + // return true; + // } + } + ((DownloadActivity) getActivity()).startDownload(indexItem); + return true; + } + return false; } @Override public void onSaveInstanceState(Bundle outState) { - outState.putString(REGION_ID_DLG_KEY, regionId); + outState.putString(REGION_ID_DLG_KEY, groupId); super.onSaveInstanceState(outState); } @@ -125,96 +176,6 @@ public class DownloadResourceGroupFragment extends DialogFragment implements Dow return fragment; } - - // FIXME why do we need fragment in fragment??? - public static class DownloadResourceGroupListFragment extends OsmandExpandableListFragment { - public static final String TAG = "RegionItemsFragment"; - private static final String REGION_ID_KEY = "world_region_id_key"; - private String regionId; - private WorldItemsFragment.DownloadResourceGroupAdapter listAdapter; - private DownloadActivity activity; - private DownloadResourceGroup group; - - public static DownloadResourceGroupListFragment createInstance(String regionId) { - Bundle bundle = new Bundle(); - bundle.putString(REGION_ID_KEY, regionId); - DownloadResourceGroupListFragment fragment = new DownloadResourceGroupListFragment(); - fragment.setArguments(bundle); - return fragment; - } - - @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setHasOptionsMenu(true); - } - - @Override - public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - View view = inflater.inflate(R.layout.download_items_fragment, container, false); - - if (savedInstanceState != null) { - regionId = savedInstanceState.getString(REGION_ID_KEY); - } - if (regionId == null) { - regionId = getArguments().getString(REGION_ID_KEY); - } - if (regionId == null) - regionId = ""; - - ExpandableListView listView = (ExpandableListView) view.findViewById(android.R.id.list); - activity = (DownloadActivity) getActivity(); - DownloadResources indexes = activity.getDownloadThread().getIndexes(); - group = indexes.getGroupById(regionId); - listAdapter = new WorldItemsFragment.DownloadResourceGroupAdapter(activity); - listView.setAdapter(listAdapter); - setListView(listView); - if (group != null) { - listAdapter.update(group); - } - expandAllGroups(); - return view; - } - - @Override - public void onSaveInstanceState(Bundle outState) { - outState.putString(REGION_ID_KEY, regionId); - super.onSaveInstanceState(outState); - } - - @Override - public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { - Object child = listAdapter.getChild(groupPosition, childPosition); - if (child instanceof DownloadResourceGroup) { - String uniqueId = ((DownloadResourceGroup) child).getUniqueId(); - final DownloadResourceGroupFragment regionDialogFragment = DownloadResourceGroupFragment.createInstance(uniqueId); - ((DownloadActivity) getActivity()).showDialog(getActivity(), regionDialogFragment); - return true; - } else if (child instanceof IndexItem) { - IndexItem indexItem = (IndexItem) child; - if (indexItem.getType() == DownloadActivityType.ROADS_FILE) { - // FIXME - // if (regularMap.getType() == DownloadActivityType.NORMAL_FILE - // && regularMap.isAlreadyDownloaded(getMyActivity().getIndexFileNames())) { - // ConfirmDownloadUnneededMapDialogFragment.createInstance(indexItem) - // .show(getChildFragmentManager(), "dialog"); - // return true; - // } - } - ((DownloadActivity) getActivity()).startDownload(indexItem); - return true; - } - return false; - } - - private void expandAllGroups() { - for (int i = 0; i < listAdapter.getGroupCount(); i++) { - getExpandableListView().expandGroup(i); - } - } - - } - public static class ConfirmDownloadUnneededMapDialogFragment extends DialogFragment { private static final String INDEX_ITEM = "index_item"; private static IndexItem item = null; diff --git a/OsmAnd/src/net/osmand/plus/download/ui/LocalIndexesFragment.java b/OsmAnd/src/net/osmand/plus/download/ui/LocalIndexesFragment.java index d9bfded238..0fe7907c2a 100644 --- a/OsmAnd/src/net/osmand/plus/download/ui/LocalIndexesFragment.java +++ b/OsmAnd/src/net/osmand/plus/download/ui/LocalIndexesFragment.java @@ -162,6 +162,8 @@ public class LocalIndexesFragment extends OsmandExpandableListFragment implement asyncLoader = new LoadLocalIndexTask(); asyncLoader.execute(getActivity()); } + + private void showContextMenu(final LocalIndexInfo info) { @@ -450,6 +452,19 @@ public class LocalIndexesFragment extends OsmandExpandableListFragment implement } } } + + @Override + public void newDownloadIndexes() { + } + + @Override + public void downloadHasFinished() { + reloadData(); + } + + @Override + public void downloadInProgress() { + } @Override From be38b19e88a1befb47cec8f1324bc02d1def2023 Mon Sep 17 00:00:00 2001 From: Victor Shcherb Date: Mon, 19 Oct 2015 09:18:27 +0200 Subject: [PATCH 08/22] Update fragments --- OsmAnd/res/layout/download_index_fragment.xml | 8 - OsmAnd/res/layout/download_items_fragment.xml | 8 - OsmAnd/res/layout/download_items_list.xml | 11 - .../res/layout/maps_in_category_fragment.xml | 6 +- .../plus/download/DownloadActivity.java | 9 +- .../net/osmand/plus/download/IndexItem.java | 17 + .../ui/DownloadResourceGroupFragment.java | 254 +++++++++++++- .../plus/download/ui/ItemViewHolder.java | 80 +---- .../download/ui/LocalIndexesFragment.java | 1 + .../plus/download/ui/WorldItemsFragment.java | 329 ------------------ 10 files changed, 289 insertions(+), 434 deletions(-) delete mode 100644 OsmAnd/res/layout/download_index_fragment.xml delete mode 100644 OsmAnd/res/layout/download_items_fragment.xml delete mode 100644 OsmAnd/res/layout/download_items_list.xml delete mode 100644 OsmAnd/src/net/osmand/plus/download/ui/WorldItemsFragment.java diff --git a/OsmAnd/res/layout/download_index_fragment.xml b/OsmAnd/res/layout/download_index_fragment.xml deleted file mode 100644 index d6c7653374..0000000000 --- a/OsmAnd/res/layout/download_index_fragment.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - \ No newline at end of file diff --git a/OsmAnd/res/layout/download_items_fragment.xml b/OsmAnd/res/layout/download_items_fragment.xml deleted file mode 100644 index 5cb7be1395..0000000000 --- a/OsmAnd/res/layout/download_items_fragment.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - \ No newline at end of file diff --git a/OsmAnd/res/layout/download_items_list.xml b/OsmAnd/res/layout/download_items_list.xml deleted file mode 100644 index b76ea9e45f..0000000000 --- a/OsmAnd/res/layout/download_items_list.xml +++ /dev/null @@ -1,11 +0,0 @@ - - - diff --git a/OsmAnd/res/layout/maps_in_category_fragment.xml b/OsmAnd/res/layout/maps_in_category_fragment.xml index 160b529315..be090c9781 100644 --- a/OsmAnd/res/layout/maps_in_category_fragment.xml +++ b/OsmAnd/res/layout/maps_in_category_fragment.xml @@ -4,8 +4,7 @@ xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" - android:orientation="vertical" - tools:context="net.osmand.plus.download.newimplementation.NewLocalIndexesFragment"> + android:orientation="vertical"> + app:contentInsetStart="72dp" + /> ref : fragSet) { Fragment f = ref.get(); if (f.isAdded()) { @@ -169,8 +166,6 @@ public class DownloadActivity extends BaseDownloadActivity { @UiThread public void newDownloadIndexes() { visibleBanner.updateBannerInProgress(); - // FIXME - //((DownloadActivity) getActivity()).updateDescriptionTextWithSize(getView()); for (WeakReference ref : fragSet) { Fragment f = ref.get(); if (f.isAdded()) { diff --git a/OsmAnd/src/net/osmand/plus/download/IndexItem.java b/OsmAnd/src/net/osmand/plus/download/IndexItem.java index 890dbabfd0..e1ed4f676c 100644 --- a/OsmAnd/src/net/osmand/plus/download/IndexItem.java +++ b/OsmAnd/src/net/osmand/plus/download/IndexItem.java @@ -135,9 +135,26 @@ public class IndexItem implements Comparable { FIXME; } + public String getLocalDate() { + FIXME; + } + public boolean isDownloaded() { // return listAlreadyDownloaded.containsKey(getTargetFileName()); + Map indexFileNames = context.getIndexFileNames(); + if (indexFileNames != null && indexItem.isAlreadyDownloaded(indexFileNames)) { + boolean outdated = false; + String date; + if (indexItem.getType() == DownloadActivityType.HILLSHADE_FILE) { + date = indexItem.getDate(dateFormat); + } else { + String sfName = indexItem.getTargetFileName(); + Map indexActivatedFileNames = context.getIndexActivatedFileNames(); + final boolean updatableResource = indexActivatedFileNames.containsKey(sfName); + date = updatableResource ? indexActivatedFileNames.get(sfName) : indexFileNames.get(sfName); + outdated = DownloadActivity.downloadListIndexThread.checkIfItemOutdated(indexItem); + } } public String getVisibleName(Context ctx, OsmandRegions osmandRegions) { diff --git a/OsmAnd/src/net/osmand/plus/download/ui/DownloadResourceGroupFragment.java b/OsmAnd/src/net/osmand/plus/download/ui/DownloadResourceGroupFragment.java index 41c8d9994b..e07ce9d88f 100644 --- a/OsmAnd/src/net/osmand/plus/download/ui/DownloadResourceGroupFragment.java +++ b/OsmAnd/src/net/osmand/plus/download/ui/DownloadResourceGroupFragment.java @@ -1,36 +1,56 @@ package net.osmand.plus.download.ui; +import java.util.ArrayList; +import java.util.List; + +import net.osmand.plus.IconsCache; import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandSettings; import net.osmand.plus.R; +import net.osmand.plus.WorldRegion; +import net.osmand.plus.activities.OsmandBaseExpandableListAdapter; import net.osmand.plus.download.DownloadActivity; import net.osmand.plus.download.DownloadActivity.BannerAndDownloadFreeVersion; import net.osmand.plus.download.DownloadActivityType; import net.osmand.plus.download.DownloadIndexesThread.DownloadEvents; import net.osmand.plus.download.DownloadResourceGroup; +import net.osmand.plus.download.DownloadResourceGroup.DownloadResourceGroupType; import net.osmand.plus.download.DownloadResources; import net.osmand.plus.download.IndexItem; +import net.osmand.util.Algorithms; import android.app.Dialog; +import android.content.Context; import android.content.DialogInterface; +import android.content.res.Resources; +import android.content.res.TypedArray; +import android.graphics.drawable.Drawable; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.v4.app.DialogFragment; +import android.support.v4.view.MenuItemCompat; import android.support.v7.app.AlertDialog; import android.support.v7.widget.Toolbar; +import android.util.TypedValue; import android.view.LayoutInflater; +import android.view.Menu; +import android.view.MenuInflater; +import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.ExpandableListView; import android.widget.ExpandableListView.OnChildClickListener; +import android.widget.TextView; public class DownloadResourceGroupFragment extends DialogFragment implements DownloadEvents, OnChildClickListener { + public static final int RELOAD_ID = 0; + public static final int SEARCH_ID = 1; public static final String TAG = "RegionDialogFragment"; private static final String REGION_ID_DLG_KEY = "world_region_dialog_key"; private String groupId; private View view; private BannerAndDownloadFreeVersion banner; protected ExpandableListView listView; - protected WorldItemsFragment.DownloadResourceGroupAdapter listAdapter; + protected DownloadResourceGroupAdapter listAdapter; private DownloadResourceGroup group; private DownloadActivity activity; private Toolbar toolbar; @@ -44,6 +64,10 @@ public class DownloadResourceGroupFragment extends DialogFragment implements Dow setHasOptionsMenu(true); } + public boolean openAsDialog() { + return !Algorithms.isEmpty(groupId); + } + @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { view = inflater.inflate(R.layout.maps_in_category_fragment, container, false); @@ -66,16 +90,19 @@ public class DownloadResourceGroupFragment extends DialogFragment implements Dow dismiss(); } }); + if (!openAsDialog()) { + toolbar.setVisibility(View.GONE); + } setHasOptionsMenu(true); - + banner = new BannerAndDownloadFreeVersion(view, (DownloadActivity) getActivity()); - + listView = (ExpandableListView) view.findViewById(android.R.id.list); listView.setOnChildClickListener(this); - listAdapter = new WorldItemsFragment.DownloadResourceGroupAdapter(activity); + listAdapter = new DownloadResourceGroupAdapter(activity); listView.setAdapter(listAdapter); - + reloadData(); return view; } @@ -101,6 +128,7 @@ public class DownloadResourceGroupFragment extends DialogFragment implements Dow @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); + setShowsDialog(openAsDialog()); listView.setBackgroundColor(getResources().getColor( getMyApplication().getSettings().isLightContent() ? R.color.bg_color_light : R.color.bg_color_dark)); } @@ -163,6 +191,34 @@ public class DownloadResourceGroupFragment extends DialogFragment implements Dow return (DownloadActivity) getActivity(); } + @Override + public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { + if (!openAsDialog()) { + MenuItem itemReload = menu.add(0, RELOAD_ID, 0, R.string.shared_string_refresh); + itemReload.setIcon(R.drawable.ic_action_refresh_dark); + MenuItemCompat.setShowAsAction(itemReload, MenuItemCompat.SHOW_AS_ACTION_ALWAYS); + + MenuItem itemSearch = menu.add(0, SEARCH_ID, 1, R.string.shared_string_search); + itemSearch.setIcon(R.drawable.ic_action_search_dark); + MenuItemCompat.setShowAsAction(itemSearch, MenuItemCompat.SHOW_AS_ACTION_ALWAYS); + } + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + switch (item.getItemId()) { + case RELOAD_ID: + // re-create the thread + getDownloadActivity().getDownloadThread().runReloadIndexFiles(); + return true; + case SEARCH_ID: + getDownloadActivity().showDialog(getActivity(), SearchDialogFragment.createInstance("")); + return true; + default: + return super.onOptionsItemSelected(item); + } + } + public void onRegionSelected(String regionId) { final DownloadResourceGroupFragment regionDialogFragment = createInstance(regionId); getDownloadActivity().showDialog(getActivity(), regionDialogFragment); @@ -206,4 +262,192 @@ public class DownloadResourceGroupFragment extends DialogFragment implements Dow return fragment; } } + + private static class DownloadGroupViewHolder { + TextView textView; + private DownloadActivity ctx; + + public DownloadGroupViewHolder(DownloadActivity ctx, View v) { + this.ctx = ctx; + textView = (TextView) v.findViewById(R.id.title); + } + + private Drawable getIconForGroup(DownloadResourceGroup group) { + Drawable iconLeft; + if (group.getType() == DownloadResourceGroupType.VOICE_REC + || group.getType() == DownloadResourceGroupType.VOICE_TTS) { + iconLeft = ctx.getMyApplication().getIconsCache().getContentIcon(R.drawable.ic_action_volume_up); + } else { + IconsCache cache = ctx.getMyApplication().getIconsCache(); + if (group.getParentGroup() == null + || group.getParentGroup().getType() == DownloadResourceGroupType.WORLD) { + iconLeft = cache.getContentIcon(R.drawable.ic_world_globe_dark); + } else { + DownloadResourceGroup ggr = group + .getGroupById(DownloadResourceGroupType.REGION_MAPS.getDefaultId()); + iconLeft = cache.getContentIcon(R.drawable.ic_map); + if (ggr != null && ggr.getIndividualResources() != null) { + IndexItem item = null; + for (IndexItem ii : ggr.getIndividualResources()) { + if (ii.getType() == DownloadActivityType.NORMAL_FILE + || ii.getType() == DownloadActivityType.ROADS_FILE) { + if (ii.isDownloaded() || ii.isOutdated()) { + item = ii; + break; + } + } + } + if (item != null) { + if (item.isOutdated()) { + iconLeft = cache.getIcon(R.drawable.ic_map, + ctx.getResources().getColor(R.color.color_distance)); + } else { + iconLeft = cache.getIcon(R.drawable.ic_map, + ctx.getResources().getColor(R.color.color_ok)); + } + } + } + } + } + return iconLeft; + } + + public void bindItem(DownloadResourceGroup group) { + Drawable iconLeft = getIconForGroup(group); + textView.setCompoundDrawablesWithIntrinsicBounds(iconLeft, null, null, null); + String name = group.getName(); + WorldRegion region = group.getRegion(); + if (region != null) { + name = region.getName(); + } + textView.setText(name); + } + } + + public static class DownloadResourceGroupAdapter extends OsmandBaseExpandableListAdapter { + + private List data = new ArrayList(); + private DownloadActivity ctx; + + + + public DownloadResourceGroupAdapter(DownloadActivity ctx) { + this.ctx = ctx; + TypedArray ta = ctx.getTheme().obtainStyledAttributes(new int[] { android.R.attr.textColorPrimary }); + ta.recycle(); + } + + public void clear() { + data.clear(); + notifyDataSetChanged(); + } + + public void update(DownloadResourceGroup mainGroup) { + data = mainGroup.getGroups(); + notifyDataSetChanged(); + } + + @Override + public Object getChild(int groupPosition, int childPosition) { + DownloadResourceGroup drg = data.get(groupPosition); + if (drg.getType().containsIndexItem()) { + return drg.getItemByIndex(childPosition); + } + return drg.getGroupByIndex(childPosition); + } + + @Override + public long getChildId(int groupPosition, int childPosition) { + return groupPosition * 10000 + childPosition; + } + + @Override + public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, + View convertView, ViewGroup parent) { + final Object child = getChild(groupPosition, childPosition); + if (child instanceof IndexItem) { + IndexItem item = (IndexItem) child; + ItemViewHolder viewHolder; + if (convertView != null && convertView.getTag() instanceof ItemViewHolder) { + viewHolder = (ItemViewHolder) convertView.getTag(); + } else { + convertView = LayoutInflater.from(parent.getContext()).inflate( + R.layout.two_line_with_images_list_item, parent, false); + viewHolder = new ItemViewHolder(convertView, ctx); + convertView.setTag(viewHolder); + } + viewHolder.bindIndexItem(item, false, false); + } else { + DownloadResourceGroup group = (DownloadResourceGroup) child; + DownloadGroupViewHolder viewHolder; + if (convertView != null && convertView.getTag() instanceof DownloadGroupViewHolder) { + viewHolder = (DownloadGroupViewHolder) convertView.getTag(); + } else { + convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.simple_list_menu_item, + parent, false); + viewHolder = new DownloadGroupViewHolder(ctx, convertView); + convertView.setTag(viewHolder); + } + viewHolder.bindItem(group); + } + + return convertView; + } + + + + @Override + public View getGroupView(int groupPosition, boolean isExpanded, final View convertView, final ViewGroup parent) { + View v = convertView; + String section = getGroup(groupPosition); + if (v == null) { + LayoutInflater inflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE); + v = inflater.inflate(R.layout.download_item_list_section, parent, false); + } + TextView nameView = ((TextView) v.findViewById(R.id.section_name)); + nameView.setText(section); + v.setOnClickListener(null); + TypedValue typedValue = new TypedValue(); + Resources.Theme theme = ctx.getTheme(); + theme.resolveAttribute(R.attr.ctx_menu_info_view_bg, typedValue, true); + v.setBackgroundColor(typedValue.data); + + return v; + } + + @Override + public int getChildrenCount(int groupPosition) { + return data.get(groupPosition).size(); + } + + @Override + public String getGroup(int groupPosition) { + DownloadResourceGroup drg = data.get(groupPosition); + int rid = drg.getType().getResourceId(); + if (rid != -1) { + return ctx.getString(rid); + } + return ""; + } + + @Override + public int getGroupCount() { + return data.size(); + } + + @Override + public long getGroupId(int groupPosition) { + return groupPosition; + } + + @Override + public boolean hasStableIds() { + return false; + } + + @Override + public boolean isChildSelectable(int groupPosition, int childPosition) { + return true; + } + } } \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/plus/download/ui/ItemViewHolder.java b/OsmAnd/src/net/osmand/plus/download/ui/ItemViewHolder.java index b1d8db5bb4..588a0a8645 100644 --- a/OsmAnd/src/net/osmand/plus/download/ui/ItemViewHolder.java +++ b/OsmAnd/src/net/osmand/plus/download/ui/ItemViewHolder.java @@ -25,6 +25,7 @@ import net.osmand.plus.srtmplugin.SRTMPlugin; import java.text.DateFormat; import java.util.Map; +// FIXME public class ItemViewHolder { private final java.text.DateFormat dateFormat; @@ -36,11 +37,13 @@ public class ItemViewHolder { protected final Button rightButton; protected final ProgressBar progressBar; protected final TextView mapDateTextView; - protected final DownloadActivity context; private boolean srtmDisabled; private boolean nauticalPluginDisabled; private boolean freeVersion; + + protected final DownloadActivity context; + private int textColorPrimary; private int textColorSecondary; private RightButtonAction rightButtonAction; @@ -63,12 +66,6 @@ public class ItemViewHolder { descrTextView = (TextView) view.findViewById(R.id.description); rightImageButton = (ImageView) view.findViewById(R.id.rightImageButton); nameTextView = (TextView) view.findViewById(R.id.name); - } - - - public ItemViewHolder(View convertView, - DownloadActivity context) { - this(convertView, context); this.dateFormat = context.getMyApplication().getResourceManager().getDateFormat(); @@ -80,17 +77,25 @@ public class ItemViewHolder { textColorSecondary = typedValue.data; } - public void initAppStatusVariables() { + + // FIXME don't initialize on every row + private void initAppStatusVariables() { srtmDisabled = OsmandPlugin.getEnabledPlugin(SRTMPlugin.class) == null; nauticalPluginDisabled = OsmandPlugin.getEnabledPlugin(NauticalMapsPlugin.class) == null; freeVersion = Version.isFreeVersion(context.getMyApplication()); } + // FIXME public void bindIndexItem(final IndexItem indexItem, - boolean showTypeInTitle, boolean showTypeInDesc, int progress) { + boolean showTypeInTitle, boolean showTypeInDesc) { initAppStatusVariables(); boolean disabled = false; rightButtonAction = RightButtonAction.UNKNOWN; + boolean downloading = context.getDownloadThread().isDownloading(indexItem); + int progress = -1; + if (context.getDownloadThread().getCurrentDownloadingItem() == indexItem) { + progress = context.getDownloadThread().getCurrentDownloadingItemProgress(); + } rightImageButton.setClickable(false); if (progress != -1) { rightImageButton.setClickable(true); @@ -145,18 +150,9 @@ public class ItemViewHolder { progressBar.setVisibility(View.GONE); Map indexFileNames = context.getIndexFileNames(); - if (indexFileNames != null && indexItem.isAlreadyDownloaded(indexFileNames)) { - boolean outdated = false; - String date; - if (indexItem.getType() == DownloadActivityType.HILLSHADE_FILE) { - date = indexItem.getDate(dateFormat); - } else { - String sfName = indexItem.getTargetFileName(); - Map indexActivatedFileNames = context.getIndexActivatedFileNames(); - final boolean updatableResource = indexActivatedFileNames.containsKey(sfName); - date = updatableResource ? indexActivatedFileNames.get(sfName) : indexFileNames.get(sfName); - outdated = DownloadActivity.downloadListIndexThread.checkIfItemOutdated(indexItem); - } + if (indexItem.isDownloaded()) { + String date = indexItem.getLocalDate(); + boolean outdated = indexItem.isOutdated(); String updateDescr = context.getResources().getString(R.string.local_index_installed) + ": " + date; mapDateTextView.setText(updateDescr); @@ -185,9 +181,7 @@ public class ItemViewHolder { if (rightButtonAction != RightButtonAction.UNKNOWN) { rightButton.setText(R.string.get_plugin); rightButton.setVisibility(View.VISIBLE); - rightImageButton.setVisibility(View.GONE); - final RightButtonAction action = rightButtonAction; rightButton.setOnClickListener(new View.OnClickListener() { @@ -228,46 +222,6 @@ public class ItemViewHolder { } } - public void bindIndexItem(final IndexItem indexItem, - boolean showTypeInTitle, boolean showTypeInDesc) { - bindIndexItem(indexItem, showTypeInTitle, showTypeInDesc, -1); - } - - public void bindRegion(WorldRegion region) { - nameTextView.setText(region.getName()); - nameTextView.setTextColor(textColorPrimary); - if (region.getResourceTypes().size() > 0) { - StringBuilder stringBuilder = new StringBuilder(); - for (DownloadActivityType activityType : region.getResourceTypes()) { - if (stringBuilder.length() > 0) { - stringBuilder.append(", "); - } - stringBuilder.append(activityType.getString(context)); - } - } - descrTextView.setVisibility(View.GONE); - mapDateTextView.setVisibility(View.GONE); - - Drawable leftImageDrawable = null; - switch (region.getMapState()) { - case NOT_DOWNLOADED: - leftImageDrawable = getContentIcon(context, R.drawable.ic_map); - break; - case DOWNLOADED: - leftImageDrawable = getContentIcon(context, R.drawable.ic_map, - context.getResources().getColor(R.color.color_ok)); - break; - case OUTDATED: - leftImageDrawable = getContentIcon(context, R.drawable.ic_map, - context.getResources().getColor(R.color.color_distance)); - break; - } - leftImageView.setImageDrawable(leftImageDrawable); - rightButton.setVisibility(View.GONE); - rightImageButton.setVisibility(View.GONE); - progressBar.setVisibility(View.GONE); - } - private Drawable getContentIcon(DownloadActivity context, int resourceId) { return context.getMyApplication().getIconsCache().getContentIcon(resourceId); } diff --git a/OsmAnd/src/net/osmand/plus/download/ui/LocalIndexesFragment.java b/OsmAnd/src/net/osmand/plus/download/ui/LocalIndexesFragment.java index 0fe7907c2a..d197346cbc 100644 --- a/OsmAnd/src/net/osmand/plus/download/ui/LocalIndexesFragment.java +++ b/OsmAnd/src/net/osmand/plus/download/ui/LocalIndexesFragment.java @@ -459,6 +459,7 @@ public class LocalIndexesFragment extends OsmandExpandableListFragment implement @Override public void downloadHasFinished() { + ((DownloadActivity) getActivity()).updateDescriptionTextWithSize(getView()); reloadData(); } diff --git a/OsmAnd/src/net/osmand/plus/download/ui/WorldItemsFragment.java b/OsmAnd/src/net/osmand/plus/download/ui/WorldItemsFragment.java deleted file mode 100644 index 6b688d3718..0000000000 --- a/OsmAnd/src/net/osmand/plus/download/ui/WorldItemsFragment.java +++ /dev/null @@ -1,329 +0,0 @@ -package net.osmand.plus.download.ui; - -import java.util.ArrayList; -import java.util.List; - -import net.osmand.plus.IconsCache; -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.download.DownloadActivity; -import net.osmand.plus.download.DownloadActivityType; -import net.osmand.plus.download.DownloadIndexesThread.DownloadEvents; -import net.osmand.plus.download.DownloadResourceGroup; -import net.osmand.plus.download.DownloadResourceGroup.DownloadResourceGroupType; -import net.osmand.plus.download.DownloadResources; -import net.osmand.plus.download.IndexItem; -import android.content.Context; -import android.content.res.Resources; -import android.content.res.TypedArray; -import android.graphics.drawable.Drawable; -import android.os.Bundle; -import android.support.v4.view.MenuItemCompat; -import android.util.TypedValue; -import android.view.LayoutInflater; -import android.view.Menu; -import android.view.MenuInflater; -import android.view.MenuItem; -import android.view.View; -import android.view.ViewGroup; -import android.widget.ExpandableListView; -import android.widget.TextView; - -public class WorldItemsFragment extends OsmandExpandableListFragment implements DownloadEvents { - public static final String TAG = "WorldItemsFragment"; - - public static final int RELOAD_ID = 0; - public static final int SEARCH_ID = 1; - private DownloadResourceGroupAdapter listAdapter; - - @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setHasOptionsMenu(true); - } - - @Override - public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - View view = inflater.inflate(R.layout.download_index_fragment, container, false); - ExpandableListView listView = (ExpandableListView) view.findViewById(android.R.id.list); - listAdapter = new DownloadResourceGroupAdapter((DownloadActivity) getActivity()); - listView.setAdapter(listAdapter); - expandAllGroups(); - setListView(listView); - - View usedSpaceCard = inflater.inflate(R.layout.used_space_card, listView, false); - getMyActivity().updateDescriptionTextWithSize(usedSpaceCard); - listView.addHeaderView(usedSpaceCard); - newDownloadIndexes(); - return view; - } - - @Override - public void onResume() { - super.onResume(); - if (!listAdapter.isEmpty()) { - expandAllGroups(); - } - listAdapter.notifyDataSetChanged(); - } - - @Override - public void downloadHasFinished() { - listAdapter.notifyDataSetChanged(); - } - - @Override - public void downloadInProgress() { - listAdapter.notifyDataSetChanged(); - } - - @Override - public void newDownloadIndexes() { - DownloadResources indexes = getDownloadActivity().getDownloadThread().getIndexes(); - listAdapter.update(indexes); - expandAllGroups(); - } - - private void expandAllGroups() { - for (int i = 0; i < listAdapter.getGroupCount(); i++) { - getExpandableListView().expandGroup(i); - } - } - - public OsmandApplication getMyApplication() { - return (OsmandApplication) getActivity().getApplication(); - } - - public DownloadActivity getMyActivity() { - return (DownloadActivity) getActivity(); - } - - @Override - public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { - Object child = listAdapter.getChild(groupPosition, childPosition); - if (child instanceof DownloadResourceGroup) { - final DownloadResourceGroupFragment regionDialogFragment = DownloadResourceGroupFragment.createInstance(((DownloadResourceGroup) child).getUniqueId()); - ((DownloadActivity) getActivity()).showDialog(getActivity(), regionDialogFragment); - return true; - } else if (child instanceof IndexItem) { - IndexItem indexItem = (IndexItem) child; - ((DownloadActivity) getActivity()).startDownload(indexItem); - return true; - } - return false; - } - - - - @Override - public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { - MenuItem itemReload = menu.add(0, RELOAD_ID, 0, R.string.shared_string_refresh); - itemReload.setIcon(R.drawable.ic_action_refresh_dark); - MenuItemCompat.setShowAsAction(itemReload, MenuItemCompat.SHOW_AS_ACTION_ALWAYS); - - MenuItem itemSearch = menu.add(0, SEARCH_ID, 1, R.string.shared_string_search); - itemSearch.setIcon(R.drawable.ic_action_search_dark); - MenuItemCompat.setShowAsAction(itemSearch, MenuItemCompat.SHOW_AS_ACTION_ALWAYS); - } - - @Override - public boolean onOptionsItemSelected(MenuItem item) { - switch (item.getItemId()) { - case RELOAD_ID: - // re-create the thread - getDownloadActivity().getDownloadThread().runReloadIndexFiles(); - return true; - case SEARCH_ID: - getDownloadActivity().showDialog(getActivity(), SearchDialogFragment.createInstance("")); - return true; - default: - return super.onOptionsItemSelected(item); - } - } - - private DownloadActivity getDownloadActivity() { - return (DownloadActivity) getActivity(); - } - - - - public static class DownloadResourceGroupAdapter extends OsmandBaseExpandableListAdapter { - - private List data = new ArrayList(); - private DownloadActivity ctx; - - private class SimpleViewHolder { - TextView textView; - } - - public DownloadResourceGroupAdapter(DownloadActivity ctx) { - this.ctx = ctx; - TypedArray ta = ctx.getTheme().obtainStyledAttributes(new int[]{android.R.attr.textColorPrimary}); - ta.recycle(); - } - - public void clear() { - data.clear(); - notifyDataSetChanged(); - } - - public void update(DownloadResourceGroup mainGroup) { - data = mainGroup.getGroups(); - notifyDataSetChanged(); - } - - @Override - public Object getChild(int groupPosition, int childPosition) { - DownloadResourceGroup drg = data.get(groupPosition); - if(drg.getType().containsIndexItem()) { - return drg.getItemByIndex(childPosition); - } - return drg.getGroupByIndex(childPosition); - } - - @Override - public long getChildId(int groupPosition, int childPosition) { - return groupPosition * 10000 + childPosition; - } - - - @Override - public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { - final Object child = getChild(groupPosition, childPosition); - if(child instanceof IndexItem) { - IndexItem item = (IndexItem) child; - if (convertView == null) { - convertView = LayoutInflater.from(parent.getContext()) - .inflate(R.layout.two_line_with_images_list_item, parent, false); - } - ItemViewHolder viewHolder; - if(convertView.getTag() instanceof ItemViewHolder) { - viewHolder = (ItemViewHolder) convertView.getTag(); - } else { - viewHolder = new ItemViewHolder(convertView,ctx); - convertView.setTag(viewHolder); - } - viewHolder.bindIndexItem(item, false, false); - } else { - DownloadResourceGroup group = (DownloadResourceGroup) child; - SimpleViewHolder viewHolder; - if (convertView == null) { - convertView = LayoutInflater.from(parent.getContext()) - .inflate(R.layout.simple_list_menu_item, parent, false); - } - if(convertView.getTag() instanceof SimpleViewHolder) { - viewHolder = (SimpleViewHolder) convertView.getTag(); - } else { - viewHolder = new SimpleViewHolder(); - convertView.setTag(viewHolder); - } - Drawable iconLeft; - if(group.getType() == DownloadResourceGroupType.VOICE_REC || - group.getType() == DownloadResourceGroupType.VOICE_TTS) { - iconLeft = ctx.getMyApplication().getIconsCache().getContentIcon(R.drawable.ic_action_volume_up); - } else { - IconsCache cache = ctx.getMyApplication().getIconsCache(); - if (group.getParentGroup() == null - || group.getParentGroup().getType() == DownloadResourceGroupType.WORLD) { - iconLeft = cache.getContentIcon(R.drawable.ic_world_globe_dark); - } else { - DownloadResourceGroup ggr = group.getGroupById(DownloadResourceGroupType.REGION_MAPS - .getDefaultId()); - iconLeft = cache.getContentIcon(R.drawable.ic_map); - if (ggr != null && ggr.getIndividualResources() != null) { - IndexItem item = null; - for (IndexItem ii : ggr.getIndividualResources()) { - if (ii.getType() == DownloadActivityType.NORMAL_FILE - || ii.getType() == DownloadActivityType.ROADS_FILE) { - if (ii.isDownloaded() || ii.isOutdated()) { - item = ii; - break; - } - } - } - if (item != null) { - if (item.isOutdated()) { - iconLeft = cache.getIcon(R.drawable.ic_map, - ctx.getResources().getColor(R.color.color_distance)); - } else { - iconLeft = cache.getIcon(R.drawable.ic_map, - ctx.getResources().getColor(R.color.color_ok)); - } - } - } - } - } - viewHolder.textView.setCompoundDrawablesWithIntrinsicBounds(iconLeft, null, null, null); - String name = group.getName(); - WorldRegion region = group.getRegion(); - if(region != null) { - name = region.getName(); - } - viewHolder.textView.setText(name); - - } - - return convertView; - } - - @Override - public View getGroupView(int groupPosition, boolean isExpanded, final View convertView, - final ViewGroup parent) { - View v = convertView; - String section = getGroup(groupPosition); - if (v == null) { - LayoutInflater inflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE); - v = inflater.inflate(R.layout.download_item_list_section, parent, false); - } - TextView nameView = ((TextView) v.findViewById(R.id.section_name)); - nameView.setText(section); - v.setOnClickListener(null); - TypedValue typedValue = new TypedValue(); - Resources.Theme theme = ctx.getTheme(); - theme.resolveAttribute(R.attr.ctx_menu_info_view_bg, typedValue, true); - v.setBackgroundColor(typedValue.data); - - return v; - } - - @Override - public int getChildrenCount(int groupPosition) { - return data.get(groupPosition).size(); - } - - @Override - public String getGroup(int groupPosition) { - DownloadResourceGroup drg = data.get(groupPosition); - int rid = drg.getType().getResourceId(); - if(rid != -1) { - return ctx.getString(rid); - } - return ""; - } - - @Override - public int getGroupCount() { - return data.size(); - } - - @Override - public long getGroupId(int groupPosition) { - return groupPosition; - } - - @Override - public boolean hasStableIds() { - return false; - } - - @Override - public boolean isChildSelectable(int groupPosition, int childPosition) { - return true; - } - } - - -} From a8655382ef0279dcc8a4d7479a9fb3b2112f7bc4 Mon Sep 17 00:00:00 2001 From: Alexey Kulish Date: Mon, 19 Oct 2015 17:27:52 +0300 Subject: [PATCH 09/22] Fixes --- OsmAnd/src/net/osmand/plus/WorldRegion.java | 10 +-------- .../plus/download/DownloadResourceGroup.java | 13 ++++++++--- .../plus/download/DownloadResources.java | 10 +++++---- .../net/osmand/plus/download/IndexItem.java | 9 ++++++-- .../ui/DownloadResourceGroupFragment.java | 22 +++++-------------- .../plus/download/ui/ItemViewHolder.java | 17 ++++++-------- .../download/ui/SearchDialogFragment.java | 5 +++-- .../plus/download/ui/SearchItemsFragment.java | 3 ++- 8 files changed, 42 insertions(+), 47 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/WorldRegion.java b/OsmAnd/src/net/osmand/plus/WorldRegion.java index 048c218e84..c867a2287f 100644 --- a/OsmAnd/src/net/osmand/plus/WorldRegion.java +++ b/OsmAnd/src/net/osmand/plus/WorldRegion.java @@ -85,7 +85,7 @@ public class WorldRegion { public void initWorld() { regionId = ""; - downloadsId= WORLD; + downloadsId = WORLD; name = ""; superregion = null; } @@ -109,14 +109,6 @@ public class WorldRegion { return this; } - - private WorldRegion init(String regionId, String name) { - this.regionId = regionId; - this.downloadsId = regionId.toLowerCase() ; - this.name = name; - return this; - } - private void addSubregion(WorldRegion subregion) { subregion.superregion = this; subregions.add(subregion); diff --git a/OsmAnd/src/net/osmand/plus/download/DownloadResourceGroup.java b/OsmAnd/src/net/osmand/plus/download/DownloadResourceGroup.java index 9d38a70ccd..6af9371c4c 100644 --- a/OsmAnd/src/net/osmand/plus/download/DownloadResourceGroup.java +++ b/OsmAnd/src/net/osmand/plus/download/DownloadResourceGroup.java @@ -1,6 +1,7 @@ package net.osmand.plus.download; import android.annotation.SuppressLint; +import android.content.Context; import java.util.ArrayList; import java.util.Collections; @@ -53,7 +54,7 @@ public class DownloadResourceGroup { } public boolean containsIndexItem() { - return isHeader() && this != SUBREGIONS; + return isHeader() && this != SUBREGIONS && this != VOICE_GROUP; } public boolean isHeader() { @@ -203,8 +204,14 @@ public class DownloadResourceGroup { return null; } - public String getName() { - return id; + public String getName(Context ctx) { + if (region != null) { + return region.getName(); + } else if (type != null && type.resId != -1) { + return ctx.getString(type.resId); + } else { + return id; + } } public String getUniqueId() { diff --git a/OsmAnd/src/net/osmand/plus/download/DownloadResources.java b/OsmAnd/src/net/osmand/plus/download/DownloadResources.java index 8e5f4fff3b..56a0601932 100644 --- a/OsmAnd/src/net/osmand/plus/download/DownloadResources.java +++ b/OsmAnd/src/net/osmand/plus/download/DownloadResources.java @@ -170,10 +170,10 @@ public class DownloadResources extends DownloadResourceGroup { this.rawResources = resources; DownloadResourceGroup voiceGroup = new DownloadResourceGroup(this, DownloadResourceGroupType.VOICE_GROUP); - DownloadResourceGroup voiceScreenRec = new DownloadResourceGroup(this, DownloadResourceGroupType.VOICE_REC); - DownloadResourceGroup voiceRec = new DownloadResourceGroup(this, DownloadResourceGroupType.VOICE_HEADER_REC); - DownloadResourceGroup voiceTTS = new DownloadResourceGroup(this, DownloadResourceGroupType.VOICE_TTS); - DownloadResourceGroup voiceScreenTTS = new DownloadResourceGroup(this, DownloadResourceGroupType.VOICE_HEADER_TTS); + DownloadResourceGroup voiceScreenRec = new DownloadResourceGroup(voiceGroup, DownloadResourceGroupType.VOICE_REC); + DownloadResourceGroup voiceScreenTTS = new DownloadResourceGroup(voiceGroup, DownloadResourceGroupType.VOICE_TTS); + DownloadResourceGroup voiceRec = new DownloadResourceGroup(voiceGroup, DownloadResourceGroupType.VOICE_HEADER_REC, DownloadResourceGroupType.VOICE_REC.name().toLowerCase()); + DownloadResourceGroup voiceTTS = new DownloadResourceGroup(voiceGroup, DownloadResourceGroupType.VOICE_HEADER_TTS, DownloadResourceGroupType.VOICE_TTS.name().toLowerCase()); voiceScreenTTS.addGroup(voiceTTS); voiceScreenRec.addGroup(voiceRec); voiceGroup.addGroup(voiceScreenRec); @@ -194,6 +194,7 @@ public class DownloadResources extends DownloadResourceGroup { } else { voiceRec.addItem(ii); } + continue; } String basename = ii.getBasename().toLowerCase(); WorldRegion wg = downloadIdForRegion.get(basename); @@ -219,6 +220,7 @@ public class DownloadResources extends DownloadResourceGroup { DownloadResourceGroup parentGroup = parent.pollFirst(); List subregions = reg.getSubregions(); DownloadResourceGroup mainGrp = new DownloadResourceGroup(parentGroup, DownloadResourceGroupType.REGION, reg.getRegionId()); + mainGrp.region = reg; parentGroup.addGroup(mainGrp); DownloadResourceGroup subRegions = new DownloadResourceGroup(mainGrp, DownloadResourceGroupType.SUBREGIONS); mainGrp.addGroup(subRegions); diff --git a/OsmAnd/src/net/osmand/plus/download/IndexItem.java b/OsmAnd/src/net/osmand/plus/download/IndexItem.java index e1ed4f676c..0e6cb31b4a 100644 --- a/OsmAnd/src/net/osmand/plus/download/IndexItem.java +++ b/OsmAnd/src/net/osmand/plus/download/IndexItem.java @@ -132,16 +132,19 @@ public class IndexItem implements Comparable { public boolean isOutdated() { - FIXME; + //FIXME; + return false; } public String getLocalDate() { - FIXME; + //FIXME; + return "FIXME"; } public boolean isDownloaded() { // return listAlreadyDownloaded.containsKey(getTargetFileName()); + /* Map indexFileNames = context.getIndexFileNames(); if (indexFileNames != null && indexItem.isAlreadyDownloaded(indexFileNames)) { boolean outdated = false; @@ -155,6 +158,8 @@ public class IndexItem implements Comparable { date = updatableResource ? indexActivatedFileNames.get(sfName) : indexFileNames.get(sfName); outdated = DownloadActivity.downloadListIndexThread.checkIfItemOutdated(indexItem); } + */ + return false; // FIXME } public String getVisibleName(Context ctx, OsmandRegions osmandRegions) { diff --git a/OsmAnd/src/net/osmand/plus/download/ui/DownloadResourceGroupFragment.java b/OsmAnd/src/net/osmand/plus/download/ui/DownloadResourceGroupFragment.java index e07ce9d88f..4bdad0b991 100644 --- a/OsmAnd/src/net/osmand/plus/download/ui/DownloadResourceGroupFragment.java +++ b/OsmAnd/src/net/osmand/plus/download/ui/DownloadResourceGroupFragment.java @@ -74,7 +74,7 @@ public class DownloadResourceGroupFragment extends DialogFragment implements Dow if (savedInstanceState != null) { groupId = savedInstanceState.getString(REGION_ID_DLG_KEY); } - if (groupId == null) { + if (groupId == null && getArguments() != null) { groupId = getArguments().getString(REGION_ID_DLG_KEY); } if (groupId == null) { @@ -112,9 +112,7 @@ public class DownloadResourceGroupFragment extends DialogFragment implements Dow group = indexes.getGroupById(groupId); if (group != null) { listAdapter.update(group); - if (group.getRegion() != null) { - toolbar.setTitle(group.getRegion().getName()); - } + toolbar.setTitle(group.getName(activity)); } expandAllGroups(); } @@ -212,18 +210,14 @@ public class DownloadResourceGroupFragment extends DialogFragment implements Dow getDownloadActivity().getDownloadThread().runReloadIndexFiles(); return true; case SEARCH_ID: - getDownloadActivity().showDialog(getActivity(), SearchDialogFragment.createInstance("")); + // FIXME + //getDownloadActivity().showDialog(getActivity(), SearchDialogFragment.createInstance("")); return true; default: return super.onOptionsItemSelected(item); } } - public void onRegionSelected(String regionId) { - final DownloadResourceGroupFragment regionDialogFragment = createInstance(regionId); - getDownloadActivity().showDialog(getActivity(), regionDialogFragment); - } - public static DownloadResourceGroupFragment createInstance(String regionId) { Bundle bundle = new Bundle(); bundle.putString(REGION_ID_DLG_KEY, regionId); @@ -315,12 +309,8 @@ public class DownloadResourceGroupFragment extends DialogFragment implements Dow public void bindItem(DownloadResourceGroup group) { Drawable iconLeft = getIconForGroup(group); textView.setCompoundDrawablesWithIntrinsicBounds(iconLeft, null, null, null); - String name = group.getName(); - WorldRegion region = group.getRegion(); - if (region != null) { - name = region.getName(); - } - textView.setText(name); + String name = group.getName(ctx); + textView.setText(name); } } diff --git a/OsmAnd/src/net/osmand/plus/download/ui/ItemViewHolder.java b/OsmAnd/src/net/osmand/plus/download/ui/ItemViewHolder.java index 588a0a8645..fc75920bee 100644 --- a/OsmAnd/src/net/osmand/plus/download/ui/ItemViewHolder.java +++ b/OsmAnd/src/net/osmand/plus/download/ui/ItemViewHolder.java @@ -15,16 +15,12 @@ import net.osmand.access.AccessibleToast; import net.osmand.plus.OsmandPlugin; import net.osmand.plus.R; import net.osmand.plus.Version; -import net.osmand.plus.WorldRegion; import net.osmand.plus.download.DownloadActivity; import net.osmand.plus.download.DownloadActivityType; import net.osmand.plus.download.IndexItem; import net.osmand.plus.openseamapsplugin.NauticalMapsPlugin; import net.osmand.plus.srtmplugin.SRTMPlugin; -import java.text.DateFormat; -import java.util.Map; - // FIXME public class ItemViewHolder { @@ -49,7 +45,7 @@ public class ItemViewHolder { private RightButtonAction rightButtonAction; private enum RightButtonAction { - UNKNOWN, + NONE, ASK_FOR_SEAMARKS_PLUGIN, ASK_FOR_SRTM_PLUGIN_PURCHASE, ASK_FOR_SRTM_PLUGIN_ENABLE, @@ -90,7 +86,7 @@ public class ItemViewHolder { boolean showTypeInTitle, boolean showTypeInDesc) { initAppStatusVariables(); boolean disabled = false; - rightButtonAction = RightButtonAction.UNKNOWN; + rightButtonAction = RightButtonAction.NONE; boolean downloading = context.getDownloadThread().isDownloading(indexItem); int progress = -1; if (context.getDownloadThread().getCurrentDownloadingItem() == indexItem) { @@ -109,11 +105,13 @@ public class ItemViewHolder { nameTextView.setText(indexItem.getVisibleName(context, context.getMyApplication().getRegions(), false)); } else { +/* FIXME if (indexItem.getSimplifiedFileName().equals(WORLD_SEAMARKS_KEY) && nauticalPluginDisabled) { rightButtonAction = RightButtonAction.ASK_FOR_SEAMARKS_PLUGIN; disabled = true; } +*/ if ((indexItem.getType() == DownloadActivityType.SRTM_COUNTRY_FILE || indexItem.getType() == DownloadActivityType.HILLSHADE_FILE) && srtmDisabled) { OsmandPlugin srtmPlugin = OsmandPlugin.getPlugin(SRTMPlugin.class); @@ -149,7 +147,6 @@ public class ItemViewHolder { rightImageButton.setImageDrawable(getContentIcon(context, R.drawable.ic_action_import)); progressBar.setVisibility(View.GONE); - Map indexFileNames = context.getIndexFileNames(); if (indexItem.isDownloaded()) { String date = indexItem.getLocalDate(); boolean outdated = indexItem.isOutdated(); @@ -178,7 +175,7 @@ public class ItemViewHolder { getContentIcon(context, R.drawable.ic_action_remove_dark)); } - if (rightButtonAction != RightButtonAction.UNKNOWN) { + if (rightButtonAction != RightButtonAction.NONE) { rightButton.setText(R.string.get_plugin); rightButton.setVisibility(View.VISIBLE); rightImageButton.setVisibility(View.GONE); @@ -210,7 +207,7 @@ public class ItemViewHolder { AccessibleToast.makeText(context, context.getString(R.string.activate_srtm_plugin), Toast.LENGTH_SHORT).show(); break; - case UNKNOWN: + case NONE: break; } } @@ -231,6 +228,6 @@ public class ItemViewHolder { } public boolean isItemAvailable() { - return rightButtonAction == RightButtonAction.UNKNOWN; + return rightButtonAction == RightButtonAction.NONE; } } diff --git a/OsmAnd/src/net/osmand/plus/download/ui/SearchDialogFragment.java b/OsmAnd/src/net/osmand/plus/download/ui/SearchDialogFragment.java index 8df0570dfb..01f83740d5 100644 --- a/OsmAnd/src/net/osmand/plus/download/ui/SearchDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/download/ui/SearchDialogFragment.java @@ -16,12 +16,12 @@ import net.osmand.plus.R; import net.osmand.plus.download.DownloadActivity; import net.osmand.plus.download.DownloadIndexesThread.DownloadEvents; -public class SearchDialogFragment extends DialogFragment implements DownloadEvents { +public class SearchDialogFragment { /*extends DialogFragment implements DownloadEvents { public static final String TAG = "SearchDialogFragment"; private static final String SEARCH_TEXT_DLG_KEY = "search_text_dlg_key"; private String searchText; - SearchView search; + private SearchView search; @Override public void onCreate(Bundle savedInstanceState) { @@ -139,4 +139,5 @@ public class SearchDialogFragment extends DialogFragment implements DownloadEven fragment.setArguments(bundle); return fragment; } + */ } diff --git a/OsmAnd/src/net/osmand/plus/download/ui/SearchItemsFragment.java b/OsmAnd/src/net/osmand/plus/download/ui/SearchItemsFragment.java index 648b2dd175..6ef6e9b7c3 100644 --- a/OsmAnd/src/net/osmand/plus/download/ui/SearchItemsFragment.java +++ b/OsmAnd/src/net/osmand/plus/download/ui/SearchItemsFragment.java @@ -29,7 +29,7 @@ import android.widget.Filter; import android.widget.Filterable; import android.widget.ListView; -public class SearchItemsFragment extends Fragment implements DownloadEvents { +public class SearchItemsFragment { /*extends Fragment implements DownloadEvents { public static final String TAG = "SearchItemsFragment"; private SearchItemsAdapter listAdapter; @@ -332,4 +332,5 @@ public class SearchItemsFragment extends Fragment implements DownloadEvents { } } } + */ } From 7b96ba7bb0cf5796cde76a22d835c8c3c1138940 Mon Sep 17 00:00:00 2001 From: Alexey Kulish Date: Mon, 19 Oct 2015 18:06:15 +0300 Subject: [PATCH 10/22] Fix --- OsmAnd/src/net/osmand/plus/download/DownloadResources.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/download/DownloadResources.java b/OsmAnd/src/net/osmand/plus/download/DownloadResources.java index 56a0601932..c8ee9d2994 100644 --- a/OsmAnd/src/net/osmand/plus/download/DownloadResources.java +++ b/OsmAnd/src/net/osmand/plus/download/DownloadResources.java @@ -172,8 +172,8 @@ public class DownloadResources extends DownloadResourceGroup { DownloadResourceGroup voiceGroup = new DownloadResourceGroup(this, DownloadResourceGroupType.VOICE_GROUP); DownloadResourceGroup voiceScreenRec = new DownloadResourceGroup(voiceGroup, DownloadResourceGroupType.VOICE_REC); DownloadResourceGroup voiceScreenTTS = new DownloadResourceGroup(voiceGroup, DownloadResourceGroupType.VOICE_TTS); - DownloadResourceGroup voiceRec = new DownloadResourceGroup(voiceGroup, DownloadResourceGroupType.VOICE_HEADER_REC, DownloadResourceGroupType.VOICE_REC.name().toLowerCase()); - DownloadResourceGroup voiceTTS = new DownloadResourceGroup(voiceGroup, DownloadResourceGroupType.VOICE_HEADER_TTS, DownloadResourceGroupType.VOICE_TTS.name().toLowerCase()); + DownloadResourceGroup voiceRec = new DownloadResourceGroup(voiceGroup, DownloadResourceGroupType.VOICE_HEADER_REC); + DownloadResourceGroup voiceTTS = new DownloadResourceGroup(voiceGroup, DownloadResourceGroupType.VOICE_HEADER_TTS); voiceScreenTTS.addGroup(voiceTTS); voiceScreenRec.addGroup(voiceRec); voiceGroup.addGroup(voiceScreenRec); From 15305bb8463392e8455e63c5f30b08dc0d2e8cfd Mon Sep 17 00:00:00 2001 From: Alexey Kulish Date: Mon, 19 Oct 2015 19:30:11 +0300 Subject: [PATCH 11/22] Flattered subregions only for World region --- OsmAnd/src/net/osmand/plus/WorldRegion.java | 30 ++++++++------------- 1 file changed, 11 insertions(+), 19 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/WorldRegion.java b/OsmAnd/src/net/osmand/plus/WorldRegion.java index c867a2287f..a6f33b5cd8 100644 --- a/OsmAnd/src/net/osmand/plus/WorldRegion.java +++ b/OsmAnd/src/net/osmand/plus/WorldRegion.java @@ -109,18 +109,10 @@ public class WorldRegion { return this; } - private void addSubregion(WorldRegion subregion) { + private void addSubregion(WorldRegion subregion, WorldRegion world) { subregion.superregion = this; subregions.add(subregion); - propagateSubregionToFlattenedHierarchy(subregion); - } - - private void propagateSubregionToFlattenedHierarchy(WorldRegion subregion) { - if (superregion != null) { - superregion.propagateSubregionToFlattenedHierarchy(subregion); - } else { - flattenedSubregions.add(subregion); - } + world.flattenedSubregions.add(subregion); } public void loadWorldRegions(OsmandApplication app) { @@ -135,42 +127,42 @@ public class WorldRegion { WorldRegion africaRegion = createRegionAs(AFRICA_REGION_ID, loadedItems, osmandRegions, res.getString(R.string.index_name_africa)); - addSubregion(africaRegion); + addSubregion(africaRegion, this); regionsLookupTable.put(africaRegion.regionId, africaRegion); WorldRegion asiaRegion = createRegionAs(ASIA_REGION_ID, loadedItems, osmandRegions, res.getString(R.string.index_name_asia)); - addSubregion(asiaRegion); + addSubregion(asiaRegion, this); regionsLookupTable.put(asiaRegion.regionId, asiaRegion); WorldRegion australiaAndOceaniaRegion = createRegionAs(AUSTRALIA_AND_OCEANIA_REGION_ID, loadedItems, osmandRegions, res.getString(R.string.index_name_oceania)); - addSubregion(australiaAndOceaniaRegion); + addSubregion(australiaAndOceaniaRegion, this); regionsLookupTable.put(australiaAndOceaniaRegion.regionId, australiaAndOceaniaRegion); WorldRegion centralAmericaRegion = createRegionAs(CENTRAL_AMERICA_REGION_ID, loadedItems, osmandRegions, res.getString(R.string.index_name_central_america)); - addSubregion(centralAmericaRegion); + addSubregion(centralAmericaRegion, this); regionsLookupTable.put(centralAmericaRegion.regionId, centralAmericaRegion); WorldRegion europeRegion = createRegionAs(EUROPE_REGION_ID, loadedItems, osmandRegions, res.getString(R.string.index_name_europe)); - addSubregion(europeRegion); + addSubregion(europeRegion, this); regionsLookupTable.put(europeRegion.regionId, europeRegion); WorldRegion northAmericaRegion = createRegionAs(NORTH_AMERICA_REGION_ID, loadedItems, osmandRegions, res.getString(R.string.index_name_north_america)); - addSubregion(northAmericaRegion); + addSubregion(northAmericaRegion, this); regionsLookupTable.put(northAmericaRegion.regionId, northAmericaRegion); WorldRegion russiaRegion = createRegionAs(RUSSIA_REGION_ID, loadedItems, osmandRegions, res.getString(R.string.index_name_russia)); - addSubregion(russiaRegion); + addSubregion(russiaRegion, this); regionsLookupTable.put(russiaRegion.regionId, russiaRegion); WorldRegion southAmericaRegion = createRegionAs(SOUTH_AMERICA_REGION_ID, loadedItems, osmandRegions, res.getString(R.string.index_name_south_america)); - addSubregion(southAmericaRegion); + addSubregion(southAmericaRegion, this); regionsLookupTable.put(southAmericaRegion.regionId, southAmericaRegion); // Process all regions @@ -191,7 +183,7 @@ public class WorldRegion { } WorldRegion newRegion = new WorldRegion().init(regionId, osmandRegions, null); - parentRegion.addSubregion(newRegion); + parentRegion.addSubregion(newRegion, this); regionsLookupTable.put(newRegion.regionId, newRegion); // Remove From 48e34264e7e45a16d4d984af2f82e1366ee2d93d Mon Sep 17 00:00:00 2001 From: Alexey Kulish Date: Mon, 19 Oct 2015 20:20:21 +0300 Subject: [PATCH 12/22] Sort world regions --- OsmAnd/src/net/osmand/plus/WorldRegion.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/OsmAnd/src/net/osmand/plus/WorldRegion.java b/OsmAnd/src/net/osmand/plus/WorldRegion.java index a6f33b5cd8..ef567fa1b1 100644 --- a/OsmAnd/src/net/osmand/plus/WorldRegion.java +++ b/OsmAnd/src/net/osmand/plus/WorldRegion.java @@ -1,5 +1,7 @@ package net.osmand.plus; +import java.util.Collections; +import java.util.Comparator; import java.util.HashMap; import java.util.Iterator; import java.util.LinkedList; @@ -196,6 +198,14 @@ public class WorldRegion { break; } + Comparator nameComparator = new Comparator() { + @Override + public int compare(WorldRegion w1, WorldRegion w2) { + return w1.getName().compareTo(w2.getName()); + } + }; + sortSubregions(this, nameComparator); + if (loadedItems.size() > 0) { LOG.warn("Found orphaned regions: " + loadedItems.size()); for (String regionId : loadedItems.keySet()) { @@ -204,6 +214,15 @@ public class WorldRegion { } } + private void sortSubregions(WorldRegion region, Comparator comparator) { + Collections.sort(region.subregions, comparator); + for (WorldRegion r : region.subregions) { + if (r.subregions.size() > 0) { + sortSubregions(r, comparator); + } + } + } + private static WorldRegion createRegionAs(String regionId, Map loadedItems, OsmandRegions osmandRegions, String localizedName) { WorldRegion worldRegion = new WorldRegion().init(regionId, osmandRegions, localizedName); From 560206f476cd2afd480fff45cf19657ae48a8dac Mon Sep 17 00:00:00 2001 From: Victor Shcherb Date: Mon, 19 Oct 2015 19:46:27 +0200 Subject: [PATCH 13/22] Update items --- .../layout/two_line_with_images_list_item.xml | 9 - .../plus/download/DownloadIndexesThread.java | 2 - .../plus/download/DownloadResources.java | 2 +- .../plus/download/ui/ItemViewHolder.java | 157 +++++++++--------- 4 files changed, 83 insertions(+), 87 deletions(-) diff --git a/OsmAnd/res/layout/two_line_with_images_list_item.xml b/OsmAnd/res/layout/two_line_with_images_list_item.xml index fee66d0940..a096f9082e 100644 --- a/OsmAnd/res/layout/two_line_with_images_list_item.xml +++ b/OsmAnd/res/layout/two_line_with_images_list_item.xml @@ -52,15 +52,6 @@ android:textSize="@dimen/default_sub_text_size" tools:text="Contour Lines"/> - - rawResources; private List itemsToUpdate = new ArrayList<>(); //public static final String WORLD_BASEMAP_KEY = "world_basemap.obf.zip"; - public static final String WORLD_SEAMARKS_KEY = "world_seamarks_basemap.obf.zip"; + public static final String WORLD_SEAMARKS_KEY = "world_seamarks_basemap"; public DownloadResources(OsmandApplication app) { diff --git a/OsmAnd/src/net/osmand/plus/download/ui/ItemViewHolder.java b/OsmAnd/src/net/osmand/plus/download/ui/ItemViewHolder.java index 588a0a8645..0d49d4ea9f 100644 --- a/OsmAnd/src/net/osmand/plus/download/ui/ItemViewHolder.java +++ b/OsmAnd/src/net/osmand/plus/download/ui/ItemViewHolder.java @@ -18,6 +18,8 @@ import net.osmand.plus.Version; import net.osmand.plus.WorldRegion; import net.osmand.plus.download.DownloadActivity; import net.osmand.plus.download.DownloadActivityType; +import net.osmand.plus.download.DownloadIndexesThread; +import net.osmand.plus.download.DownloadResources; import net.osmand.plus.download.IndexItem; import net.osmand.plus.openseamapsplugin.NauticalMapsPlugin; import net.osmand.plus.srtmplugin.SRTMPlugin; @@ -36,9 +38,9 @@ public class ItemViewHolder { protected final ImageView rightImageButton; protected final Button rightButton; protected final ProgressBar progressBar; - protected final TextView mapDateTextView; private boolean srtmDisabled; + private boolean srtmNeedsInstallation; private boolean nauticalPluginDisabled; private boolean freeVersion; @@ -46,10 +48,12 @@ public class ItemViewHolder { private int textColorPrimary; private int textColorSecondary; - private RightButtonAction rightButtonAction; + private RightButtonAction clickAction; + + private enum RightButtonAction { - UNKNOWN, + DOWNLOAD, ASK_FOR_SEAMARKS_PLUGIN, ASK_FOR_SRTM_PLUGIN_PURCHASE, ASK_FOR_SRTM_PLUGIN_ENABLE, @@ -60,7 +64,6 @@ public class ItemViewHolder { public ItemViewHolder(View view, DownloadActivity context) { this.context = context; progressBar = (ProgressBar) view.findViewById(R.id.progressBar); - mapDateTextView = (TextView) view.findViewById(R.id.mapDateTextView); rightButton = (Button) view.findViewById(R.id.rightButton); leftImageView = (ImageView) view.findViewById(R.id.leftImageView); descrTextView = (TextView) view.findViewById(R.id.description); @@ -83,59 +86,52 @@ public class ItemViewHolder { srtmDisabled = OsmandPlugin.getEnabledPlugin(SRTMPlugin.class) == null; nauticalPluginDisabled = OsmandPlugin.getEnabledPlugin(NauticalMapsPlugin.class) == null; freeVersion = Version.isFreeVersion(context.getMyApplication()); + OsmandPlugin srtmPlugin = OsmandPlugin.getPlugin(SRTMPlugin.class); + srtmNeedsInstallation = srtmPlugin == null || srtmPlugin.needsInstallation(); } // FIXME public void bindIndexItem(final IndexItem indexItem, boolean showTypeInTitle, boolean showTypeInDesc) { initAppStatusVariables(); - boolean disabled = false; - rightButtonAction = RightButtonAction.UNKNOWN; - boolean downloading = context.getDownloadThread().isDownloading(indexItem); + boolean isDownloading = context.getDownloadThread().isDownloading(indexItem); int progress = -1; if (context.getDownloadThread().getCurrentDownloadingItem() == indexItem) { progress = context.getDownloadThread().getCurrentDownloadingItemProgress(); } - rightImageButton.setClickable(false); - if (progress != -1) { - rightImageButton.setClickable(true); - rightImageButton.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - context.makeSureUserCancelDownload(indexItem); - } - }); - } else if (indexItem.getType() == DownloadActivityType.VOICE_FILE) { - nameTextView.setText(indexItem.getVisibleName(context, - context.getMyApplication().getRegions(), false)); + boolean disabled = checkDisabledAndClickAction(indexItem); + /// name and left item + if (showTypeInTitle) { + nameTextView.setText(indexItem.getType().getString(context)); } else { - if (indexItem.getSimplifiedFileName().equals(WORLD_SEAMARKS_KEY) - && nauticalPluginDisabled) { - rightButtonAction = RightButtonAction.ASK_FOR_SEAMARKS_PLUGIN; - disabled = true; - } - if ((indexItem.getType() == DownloadActivityType.SRTM_COUNTRY_FILE || - indexItem.getType() == DownloadActivityType.HILLSHADE_FILE) && srtmDisabled) { - OsmandPlugin srtmPlugin = OsmandPlugin.getPlugin(SRTMPlugin.class); - if (srtmPlugin == null || srtmPlugin.needsInstallation()) { - rightButtonAction = RightButtonAction.ASK_FOR_SRTM_PLUGIN_PURCHASE; - } else if (!srtmPlugin.isActive()) { - rightButtonAction = RightButtonAction.ASK_FOR_SRTM_PLUGIN_ENABLE; - } - - disabled = true; - } else if (indexItem.getType() == DownloadActivityType.WIKIPEDIA_FILE && freeVersion) { - rightButtonAction = RightButtonAction.ASK_FOR_FULL_VERSION_PURCHASE; - disabled = true; - } - if (showTypeInTitle) { - nameTextView.setText(indexItem.getType().getString(context)); - } else { - nameTextView.setText(indexItem.getVisibleName(context, context.getMyApplication().getRegions(), false)); - } + nameTextView.setText(indexItem.getVisibleName(context, context.getMyApplication().getRegions(), false)); + } + if (indexItem.isDownloaded()) { + String date = indexItem.getLocalDate(); + boolean outdated = indexItem.isOutdated(); + String updateDescr = context.getResources().getString(R.string.local_index_installed) + ": " + + date; + mapDateTextView.setText(updateDescr); + int colorId = outdated ? R.color.color_distance : R.color.color_ok; + final int color = context.getResources().getColor(colorId); + mapDateTextView.setTextColor(color); + leftImageView.setImageDrawable(getContentIcon(context, + indexItem.getType().getIconResource(), color)); + nameTextView.setTextColor(textColorPrimary); + } else if (disabled) { + leftImageView.setImageDrawable(getContentIcon(context, + indexItem.getType().getIconResource(), textColorSecondary)); + nameTextView.setTextColor(textColorSecondary); + } else { + leftImageView.setImageDrawable(getContentIcon(context, + indexItem.getType().getIconResource())); + nameTextView.setTextColor(textColorPrimary); } - if (progress == -1) { + if (isDownloading) { + progressBar.setVisibility(View.GONE); + + // FIXME mapDATETextView descrTextView.setVisibility(View.VISIBLE); if (!showTypeInTitle && (indexItem.getType() == DownloadActivityType.SRTM_COUNTRY_FILE || indexItem.getType() == DownloadActivityType.HILLSHADE_FILE) && srtmDisabled) { @@ -145,44 +141,54 @@ public class ItemViewHolder { } else { descrTextView.setText(indexItem.getSizeDescription(context)); } + rightImageButton.setVisibility(View.VISIBLE); rightImageButton.setImageDrawable(getContentIcon(context, R.drawable.ic_action_import)); - progressBar.setVisibility(View.GONE); - - Map indexFileNames = context.getIndexFileNames(); - if (indexItem.isDownloaded()) { - String date = indexItem.getLocalDate(); - boolean outdated = indexItem.isOutdated(); - String updateDescr = context.getResources().getString(R.string.local_index_installed) + ": " - + date; - mapDateTextView.setText(updateDescr); - int colorId = outdated ? R.color.color_distance : R.color.color_ok; - final int color = context.getResources().getColor(colorId); - mapDateTextView.setTextColor(color); - leftImageView.setImageDrawable(getContentIcon(context, - indexItem.getType().getIconResource(), color)); - nameTextView.setTextColor(textColorPrimary); - } else if (disabled) { - leftImageView.setImageDrawable(getContentIcon(context, - indexItem.getType().getIconResource(), textColorSecondary)); - nameTextView.setTextColor(textColorSecondary); - } else { - leftImageView.setImageDrawable(getContentIcon(context, - indexItem.getType().getIconResource())); - nameTextView.setTextColor(textColorPrimary); - } + } else { progressBar.setVisibility(View.VISIBLE); progressBar.setProgress(progress); - rightImageButton.setImageDrawable( - getContentIcon(context, R.drawable.ic_action_remove_dark)); + + descrTextView.setVisibility(View.GONE); + + rightImageButton.setImageDrawable(getContentIcon(context, R.drawable.ic_action_remove_dark)); + rightImageButton.setClickable(true); + rightImageButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + context.makeSureUserCancelDownload(indexItem); + } + }); } + } - if (rightButtonAction != RightButtonAction.UNKNOWN) { + + private boolean checkDisabledAndClickAction(final IndexItem indexItem) { + boolean disabled = false; + clickAction = RightButtonAction.DOWNLOAD; + if (indexItem.getBasename().toLowerCase().equals(DownloadResources.WORLD_SEAMARKS_KEY) + && nauticalPluginDisabled) { + clickAction = RightButtonAction.ASK_FOR_SEAMARKS_PLUGIN; + disabled = true; + } else if ((indexItem.getType() == DownloadActivityType.SRTM_COUNTRY_FILE || + indexItem.getType() == DownloadActivityType.HILLSHADE_FILE) && srtmDisabled) { + if (srtmNeedsInstallation) { + clickAction = RightButtonAction.ASK_FOR_SRTM_PLUGIN_PURCHASE; + } else { + clickAction = RightButtonAction.ASK_FOR_SRTM_PLUGIN_ENABLE; + } + + disabled = true; + } else if (indexItem.getType() == DownloadActivityType.WIKIPEDIA_FILE && freeVersion) { + clickAction = RightButtonAction.ASK_FOR_FULL_VERSION_PURCHASE; + disabled = true; + } + + if (clickAction != RightButtonAction.DOWNLOAD) { rightButton.setText(R.string.get_plugin); rightButton.setVisibility(View.VISIBLE); rightImageButton.setVisibility(View.GONE); - final RightButtonAction action = rightButtonAction; + final RightButtonAction action = clickAction; rightButton.setOnClickListener(new View.OnClickListener() { @Override @@ -210,16 +216,17 @@ public class ItemViewHolder { AccessibleToast.makeText(context, context.getString(R.string.activate_srtm_plugin), Toast.LENGTH_SHORT).show(); break; - case UNKNOWN: + case DOWNLOAD: break; } } }); - } else { rightButton.setVisibility(View.GONE); rightImageButton.setVisibility(View.VISIBLE); } + + return disabled; } private Drawable getContentIcon(DownloadActivity context, int resourceId) { @@ -231,6 +238,6 @@ public class ItemViewHolder { } public boolean isItemAvailable() { - return rightButtonAction == RightButtonAction.UNKNOWN; + return clickAction == RightButtonAction.DOWNLOAD; } } From 12d7b6c4832f3d2c2641fd2267b627b49ae6a677 Mon Sep 17 00:00:00 2001 From: Victor Shcherb Date: Mon, 19 Oct 2015 20:58:28 +0200 Subject: [PATCH 14/22] Continue refactoring --- .../net/osmand/plus/download/IndexItem.java | 43 +++++++++- .../ui/DownloadResourceGroupFragment.java | 4 +- .../plus/download/ui/ItemViewHolder.java | 81 ++++++++++-------- .../download/ui/UpdatesIndexFragment.java | 85 ++----------------- 4 files changed, 95 insertions(+), 118 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/download/IndexItem.java b/OsmAnd/src/net/osmand/plus/download/IndexItem.java index 0e6cb31b4a..04b61aaf06 100644 --- a/OsmAnd/src/net/osmand/plus/download/IndexItem.java +++ b/OsmAnd/src/net/osmand/plus/download/IndexItem.java @@ -2,6 +2,8 @@ package net.osmand.plus.download; import java.io.File; import java.io.IOException; +import java.text.DateFormat; +import java.text.ParseException; import java.util.Date; import java.util.Map; @@ -131,6 +133,29 @@ public class IndexItem implements Comparable { } + public String getDaysBehind(OsmandApplication app) { + // FIXME + DateFormat format = app.getResourceManager().getDateFormat(); + String sfName = getTargetFileName(); + Map indexActivatedFileNames = app.getResourceManager().getIndexFileNames(); + String dt = indexActivatedFileNames.get(sfName); + if (dt != null) { + try { + Date tm = format.parse(dt); + long days = Math.max(1, (getTimestamp() - tm.getTime()) / (24 * 60 * 60 * 1000) + 1); + return days + " " + app.getString(R.string.days_behind); + } catch (ParseException e1) { + e1.printStackTrace(); + } + } + return ""; + } + + public String getRemoteDate() { + // FIXME; + return "FIXME"; + } + public boolean isOutdated() { //FIXME; return false; @@ -138,11 +163,27 @@ public class IndexItem implements Comparable { public String getLocalDate() { //FIXME; +// DateFormat format = app.getResourceManager().getDateFormat(); +// String sfName = getTargetFileName(); +// Map indexActivatedFileNames = app.getResourceManager().getIndexFileNames(); +// String dt = indexActivatedFileNames.get(sfName); +// if (dt != null) { +// try { +// Date tm = format.parse(dt); +// long days = Math.max(1, (getTimestamp() - tm.getTime()) / (24 * 60 * 60 * 1000) + 1); +// return days + " " + app.getString(R.string.days_behind); +// } catch (ParseException e1) { +// e1.printStackTrace(); +// } +// } +// return ""; + return "FIXME"; } public boolean isDownloaded() { + // FIXME // return listAlreadyDownloaded.containsKey(getTargetFileName()); /* Map indexFileNames = context.getIndexFileNames(); @@ -159,7 +200,7 @@ public class IndexItem implements Comparable { outdated = DownloadActivity.downloadListIndexThread.checkIfItemOutdated(indexItem); } */ - return false; // FIXME + return false; } public String getVisibleName(Context ctx, OsmandRegions osmandRegions) { diff --git a/OsmAnd/src/net/osmand/plus/download/ui/DownloadResourceGroupFragment.java b/OsmAnd/src/net/osmand/plus/download/ui/DownloadResourceGroupFragment.java index 4bdad0b991..6ea8519d6a 100644 --- a/OsmAnd/src/net/osmand/plus/download/ui/DownloadResourceGroupFragment.java +++ b/OsmAnd/src/net/osmand/plus/download/ui/DownloadResourceGroupFragment.java @@ -364,9 +364,11 @@ public class DownloadResourceGroupFragment extends DialogFragment implements Dow convertView = LayoutInflater.from(parent.getContext()).inflate( R.layout.two_line_with_images_list_item, parent, false); viewHolder = new ItemViewHolder(convertView, ctx); + viewHolder.setShowRemoteDate(true); + viewHolder.setShowTypeInDesc(true); convertView.setTag(viewHolder); } - viewHolder.bindIndexItem(item, false, false); + viewHolder.bindIndexItem(item); } else { DownloadResourceGroup group = (DownloadResourceGroup) child; DownloadGroupViewHolder viewHolder; diff --git a/OsmAnd/src/net/osmand/plus/download/ui/ItemViewHolder.java b/OsmAnd/src/net/osmand/plus/download/ui/ItemViewHolder.java index 2945e6af0b..9aed8ddcbb 100644 --- a/OsmAnd/src/net/osmand/plus/download/ui/ItemViewHolder.java +++ b/OsmAnd/src/net/osmand/plus/download/ui/ItemViewHolder.java @@ -1,5 +1,15 @@ package net.osmand.plus.download.ui; +import net.osmand.access.AccessibleToast; +import net.osmand.plus.OsmandPlugin; +import net.osmand.plus.R; +import net.osmand.plus.Version; +import net.osmand.plus.download.DownloadActivity; +import net.osmand.plus.download.DownloadActivityType; +import net.osmand.plus.download.DownloadResources; +import net.osmand.plus.download.IndexItem; +import net.osmand.plus.openseamapsplugin.NauticalMapsPlugin; +import net.osmand.plus.srtmplugin.SRTMPlugin; import android.content.Intent; import android.content.res.Resources; import android.graphics.drawable.Drawable; @@ -11,19 +21,7 @@ import android.widget.ImageView; import android.widget.ProgressBar; import android.widget.TextView; import android.widget.Toast; -import net.osmand.access.AccessibleToast; -import net.osmand.plus.OsmandPlugin; -import net.osmand.plus.R; -import net.osmand.plus.Version; -import net.osmand.plus.download.DownloadActivity; -import net.osmand.plus.download.DownloadActivityType; -import net.osmand.plus.download.DownloadIndexesThread; -import net.osmand.plus.download.DownloadResources; -import net.osmand.plus.download.IndexItem; -import net.osmand.plus.openseamapsplugin.NauticalMapsPlugin; -import net.osmand.plus.srtmplugin.SRTMPlugin; -// FIXME public class ItemViewHolder { private final java.text.DateFormat dateFormat; @@ -45,6 +43,9 @@ public class ItemViewHolder { private int textColorPrimary; private int textColorSecondary; private RightButtonAction clickAction; + + boolean showTypeInDesc; + boolean showRemoteDate; @@ -75,6 +76,14 @@ public class ItemViewHolder { theme.resolveAttribute(android.R.attr.textColorSecondary, typedValue, true); textColorSecondary = typedValue.data; } + + public void setShowRemoteDate(boolean showRemoteDate) { + this.showRemoteDate = showRemoteDate; + } + + public void setShowTypeInDesc(boolean showTypeInDesc) { + this.showTypeInDesc = showTypeInDesc; + } // FIXME don't initialize on every row @@ -86,9 +95,7 @@ public class ItemViewHolder { srtmNeedsInstallation = srtmPlugin == null || srtmPlugin.needsInstallation(); } - // FIXME - public void bindIndexItem(final IndexItem indexItem, - boolean showTypeInTitle, boolean showTypeInDesc) { + public void bindIndexItem(final IndexItem indexItem) { initAppStatusVariables(); boolean isDownloading = context.getDownloadThread().isDownloading(indexItem); int progress = -1; @@ -97,50 +104,51 @@ public class ItemViewHolder { } boolean disabled = checkDisabledAndClickAction(indexItem); /// name and left item - if (showTypeInTitle) { - nameTextView.setText(indexItem.getType().getString(context)); + nameTextView.setText(indexItem.getVisibleName(context, context.getMyApplication().getRegions(), false)); + if(!disabled) { + nameTextView.setTextColor(textColorPrimary); } else { - nameTextView.setText(indexItem.getVisibleName(context, context.getMyApplication().getRegions(), false)); + nameTextView.setTextColor(textColorSecondary); + } + int color = textColorSecondary; + if(indexItem.isDownloaded()) { + int colorId = indexItem.isOutdated() ? R.color.color_distance : R.color.color_ok; + color = context.getResources().getColor(colorId); } if (indexItem.isDownloaded()) { - String date = indexItem.getLocalDate(); - boolean outdated = indexItem.isOutdated(); - String updateDescr = context.getResources().getString(R.string.local_index_installed) + ": " - + date; - mapDateTextView.setText(updateDescr); - int colorId = outdated ? R.color.color_distance : R.color.color_ok; - final int color = context.getResources().getColor(colorId); - mapDateTextView.setTextColor(color); leftImageView.setImageDrawable(getContentIcon(context, indexItem.getType().getIconResource(), color)); - nameTextView.setTextColor(textColorPrimary); } else if (disabled) { leftImageView.setImageDrawable(getContentIcon(context, indexItem.getType().getIconResource(), textColorSecondary)); - nameTextView.setTextColor(textColorSecondary); } else { leftImageView.setImageDrawable(getContentIcon(context, indexItem.getType().getIconResource())); - nameTextView.setTextColor(textColorPrimary); } - if (isDownloading) { + if (!isDownloading) { progressBar.setVisibility(View.GONE); - // FIXME mapDATETextView + descrTextView.setVisibility(color); descrTextView.setVisibility(View.VISIBLE); - if (!showTypeInTitle && (indexItem.getType() == DownloadActivityType.SRTM_COUNTRY_FILE || + if ((indexItem.getType() == DownloadActivityType.SRTM_COUNTRY_FILE || indexItem.getType() == DownloadActivityType.HILLSHADE_FILE) && srtmDisabled) { descrTextView.setText(indexItem.getType().getString(context)); } else if (showTypeInDesc) { - descrTextView.setText(indexItem.getType().getString(context) + " • " + indexItem.getSizeDescription(context)); + descrTextView.setText(indexItem.getType().getString(context) + + " • " + indexItem.getSizeDescription(context) + + " • " + (showRemoteDate ? indexItem.getRemoteDate() : indexItem.getLocalDate())); } else { - descrTextView.setText(indexItem.getSizeDescription(context)); + descrTextView.setText(indexItem.getSizeDescription(context) + " • " + (showRemoteDate ? indexItem.getRemoteDate() : indexItem.getLocalDate())); } - rightImageButton.setVisibility(View.VISIBLE); rightImageButton.setImageDrawable(getContentIcon(context, R.drawable.ic_action_import)); - + rightImageButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + context.startDownload(indexItem); + } + }); } else { progressBar.setVisibility(View.VISIBLE); progressBar.setProgress(progress); @@ -148,7 +156,6 @@ public class ItemViewHolder { descrTextView.setVisibility(View.GONE); rightImageButton.setImageDrawable(getContentIcon(context, R.drawable.ic_action_remove_dark)); - rightImageButton.setClickable(true); rightImageButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { diff --git a/OsmAnd/src/net/osmand/plus/download/ui/UpdatesIndexFragment.java b/OsmAnd/src/net/osmand/plus/download/ui/UpdatesIndexFragment.java index aaeaf20b0c..0daeb47e63 100644 --- a/OsmAnd/src/net/osmand/plus/download/ui/UpdatesIndexFragment.java +++ b/OsmAnd/src/net/osmand/plus/download/ui/UpdatesIndexFragment.java @@ -1,10 +1,7 @@ package net.osmand.plus.download.ui; -import java.text.ParseException; import java.util.Comparator; -import java.util.Date; import java.util.List; -import java.util.Map; import net.osmand.map.OsmandRegions; import net.osmand.plus.OsmandApplication; @@ -14,7 +11,6 @@ import net.osmand.plus.download.DownloadActivity; import net.osmand.plus.download.DownloadIndexesThread.DownloadEvents; import net.osmand.plus.download.DownloadResources; import net.osmand.plus.download.IndexItem; -import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.support.v4.view.MenuItemCompat; @@ -170,84 +166,15 @@ public class UpdatesIndexFragment extends OsmAndListFragment implements Download if (v == null) { LayoutInflater inflater = LayoutInflater.from(getMyActivity()); v = inflater.inflate(R.layout.two_line_with_images_list_item, parent, false); - v.setTag(new UpdateViewHolder(v, getMyActivity())); + v.setTag(new ItemViewHolder(v, getMyActivity())); + } - UpdateViewHolder holder = (UpdateViewHolder) v.getTag(); - holder.bindUpdatesIndexItem(items.get(position)); + ItemViewHolder holder = (ItemViewHolder) v.getTag(); + holder.setShowRemoteDate(true); + holder.setShowTypeInDesc(true); + holder.bindIndexItem(items.get(position)); return v; } } - // FIXME review and delete if duplicate - private static class UpdateViewHolder extends ItemViewHolder { - private final java.text.DateFormat format; - private UpdateViewHolder(View convertView, - final DownloadActivity context) { - super(convertView, context); - format = context.getMyApplication().getResourceManager().getDateFormat(); - } - - public void bindUpdatesIndexItem(IndexItem indexItem) { - if (indexItem.getFileName().equals(context.getString(R.string.everything_up_to_date)) || - indexItem.getFileName().equals(context.getString(R.string.no_index_file_to_download))) { - nameTextView.setText(indexItem.getFileName()); - descrTextView.setText(""); - return; - } - - OsmandRegions osmandRegions = - context.getMyApplication().getResourceManager().getOsmandRegions(); - String eName = indexItem.getVisibleName(context.getMyApplication(), osmandRegions); - - nameTextView.setText(eName.trim().replace('\n', ' ').replace("TTS", "")); //$NON-NLS-1$ - String d = getMapDescription(indexItem); - descrTextView.setText(d); - - String sfName = indexItem.getTargetFileName(); - Map indexActivatedFileNames = context.getMyApplication().getResourceManager().getIndexFileNames(); - String dt = indexActivatedFileNames.get(sfName); - mapDateTextView.setText(""); - if (dt != null) { - try { - Date tm = format.parse(dt); - long days = Math.max(1, (indexItem.getTimestamp() - tm.getTime()) / (24 * 60 * 60 * 1000) + 1); - mapDateTextView.setText(days + " " + context.getString(R.string.days_behind)); - } catch (ParseException e1) { - e1.printStackTrace(); - } - } - rightImageButton.setVisibility(View.VISIBLE); - rightImageButton.setImageDrawable( - context.getMyApplication().getIconsCache() - .getContentIcon(R.drawable.ic_action_import)); - } - - - // FIXME general method for all maps - private String getMapDescription(IndexItem item) { - String typeName = getTypeName(item, item.getType().getStringResource()); - String date = item.getDate(format); - String size = item.getSizeDescription(context); - return typeName + " " + date + " " + size; - } - - private String getTypeName(IndexItem item, int resId) { - Activity activity = context; - if (resId == R.string.download_regular_maps) { - return activity.getString(R.string.shared_string_map); - } else if (resId == R.string.download_wikipedia_maps) { - return activity.getString(R.string.shared_string_wikipedia); - } else if (resId == R.string.voices) { - return item.getTargetFileName().contains("tts") ? activity.getString(R.string.ttsvoice) : activity - .getString(R.string.voice); - } else if (resId == R.string.download_roads_only_maps) { - return activity.getString(R.string.roads_only); - } else if (resId == R.string.download_srtm_maps) { - return activity.getString(R.string.download_srtm_maps); - } else if (resId == R.string.download_hillshade_maps) { - return activity.getString(R.string.download_hillshade_maps); - } - return ""; - } - } } From c47083dc91aedfffff184cc25192552592f1cbcf Mon Sep 17 00:00:00 2001 From: Victor Shcherb Date: Mon, 19 Oct 2015 21:03:35 +0200 Subject: [PATCH 15/22] Fix issue --- .../osmand/plus/download/DownloadActivity.java | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/download/DownloadActivity.java b/OsmAnd/src/net/osmand/plus/download/DownloadActivity.java index c71c4d57b4..293a8748c2 100644 --- a/OsmAnd/src/net/osmand/plus/download/DownloadActivity.java +++ b/OsmAnd/src/net/osmand/plus/download/DownloadActivity.java @@ -138,10 +138,8 @@ public class DownloadActivity extends BaseDownloadActivity { visibleBanner.updateBannerInProgress(); for (WeakReference ref : fragSet) { Fragment f = ref.get(); - if (f.isAdded()) { - if(f instanceof DownloadEvents) { - ((DownloadEvents) f).downloadHasFinished(); - } + if (f instanceof DownloadEvents && f.isAdded()) { + ((DownloadEvents) f).downloadHasFinished(); } } } @@ -152,10 +150,8 @@ public class DownloadActivity extends BaseDownloadActivity { visibleBanner.updateBannerInProgress(); for (WeakReference ref : fragSet) { Fragment f = ref.get(); - if (f.isAdded()) { - if(f instanceof DownloadEvents) { - ((DownloadEvents) f).downloadInProgress(); - } + if (f instanceof DownloadEvents && f.isAdded()) { + ((DownloadEvents) f).downloadInProgress(); } } } @@ -168,10 +164,8 @@ public class DownloadActivity extends BaseDownloadActivity { visibleBanner.updateBannerInProgress(); for (WeakReference ref : fragSet) { Fragment f = ref.get(); - if (f.isAdded()) { - if(f instanceof DownloadEvents) { - ((DownloadEvents) f).newDownloadIndexes(); - } + if (f instanceof DownloadEvents && f.isAdded()) { + ((DownloadEvents) f).newDownloadIndexes(); } } downloadHasFinished(); From 83b0f37a2168a8679bbb2a14f1025b45af5ae83f Mon Sep 17 00:00:00 2001 From: Victor Shcherb Date: Mon, 19 Oct 2015 21:06:08 +0200 Subject: [PATCH 16/22] Fix banners --- .../ui/DownloadResourceGroupFragment.java | 20 ++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/download/ui/DownloadResourceGroupFragment.java b/OsmAnd/src/net/osmand/plus/download/ui/DownloadResourceGroupFragment.java index 6ea8519d6a..040558e2db 100644 --- a/OsmAnd/src/net/osmand/plus/download/ui/DownloadResourceGroupFragment.java +++ b/OsmAnd/src/net/osmand/plus/download/ui/DownloadResourceGroupFragment.java @@ -7,7 +7,6 @@ import net.osmand.plus.IconsCache; import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandSettings; import net.osmand.plus.R; -import net.osmand.plus.WorldRegion; import net.osmand.plus.activities.OsmandBaseExpandableListAdapter; import net.osmand.plus.download.DownloadActivity; import net.osmand.plus.download.DownloadActivity.BannerAndDownloadFreeVersion; @@ -96,7 +95,12 @@ public class DownloadResourceGroupFragment extends DialogFragment implements Dow setHasOptionsMenu(true); - banner = new BannerAndDownloadFreeVersion(view, (DownloadActivity) getActivity()); + if(openAsDialog()) { + banner = new BannerAndDownloadFreeVersion(view, (DownloadActivity) getActivity()); + } else { + banner = null; + view.findViewById(R.id.freeVersionBanner).setVisibility(View.GONE); + } listView = (ExpandableListView) view.findViewById(android.R.id.list); listView.setOnChildClickListener(this); @@ -133,19 +137,25 @@ public class DownloadResourceGroupFragment extends DialogFragment implements Dow @Override public void newDownloadIndexes() { - banner.updateBannerInProgress(); + if(banner != null) { + banner.updateBannerInProgress(); + } reloadData(); } @Override public void downloadHasFinished() { - banner.updateBannerInProgress(); + if(banner != null) { + banner.updateBannerInProgress(); + } listAdapter.notifyDataSetChanged(); } @Override public void downloadInProgress() { - banner.updateBannerInProgress(); + if(banner != null) { + banner.updateBannerInProgress(); + } listAdapter.notifyDataSetChanged(); } From 6f1b683629ac399f7e8605b0c99f7d4a8db8ed6a Mon Sep 17 00:00:00 2001 From: Victor Shcherb Date: Mon, 19 Oct 2015 21:06:41 +0200 Subject: [PATCH 17/22] Fix banners --- OsmAnd/src/net/osmand/plus/download/ui/ItemViewHolder.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/OsmAnd/src/net/osmand/plus/download/ui/ItemViewHolder.java b/OsmAnd/src/net/osmand/plus/download/ui/ItemViewHolder.java index 9aed8ddcbb..29ad64ec75 100644 --- a/OsmAnd/src/net/osmand/plus/download/ui/ItemViewHolder.java +++ b/OsmAnd/src/net/osmand/plus/download/ui/ItemViewHolder.java @@ -129,7 +129,7 @@ public class ItemViewHolder { if (!isDownloading) { progressBar.setVisibility(View.GONE); - descrTextView.setVisibility(color); + descrTextView.setTextColor(color); descrTextView.setVisibility(View.VISIBLE); if ((indexItem.getType() == DownloadActivityType.SRTM_COUNTRY_FILE || indexItem.getType() == DownloadActivityType.HILLSHADE_FILE) && srtmDisabled) { From 6fd9bafa51db4db2cc885d4a9ac32837cd84d659 Mon Sep 17 00:00:00 2001 From: Victor Shcherb Date: Mon, 19 Oct 2015 22:18:03 +0200 Subject: [PATCH 18/22] Refactor --- OsmAnd/src/net/osmand/plus/WorldRegion.java | 8 +- .../ui/ActiveDownloadsDialogFragment.java | 80 ++-------- .../ui/DownloadResourceGroupFragment.java | 36 +++-- .../plus/download/ui/ItemViewHolder.java | 148 +++++++++++------- .../download/ui/SearchDialogFragment.java | 11 +- .../plus/download/ui/SearchItemsFragment.java | 1 + .../download/ui/UpdatesIndexFragment.java | 5 +- 7 files changed, 157 insertions(+), 132 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/WorldRegion.java b/OsmAnd/src/net/osmand/plus/WorldRegion.java index ef567fa1b1..ae12a2e3f8 100644 --- a/OsmAnd/src/net/osmand/plus/WorldRegion.java +++ b/OsmAnd/src/net/osmand/plus/WorldRegion.java @@ -32,6 +32,7 @@ public class WorldRegion { private String regionId; private String downloadsId; private String name; + private String searchText; // Hierarchy private WorldRegion superregion; @@ -95,10 +96,11 @@ public class WorldRegion { private WorldRegion init(String regionId, OsmandRegions osmandRegions, String name) { this.regionId = regionId; String downloadName = osmandRegions.getDownloadName(regionId); + this.searchText = osmandRegions.getDownloadNameIndexLowercase(downloadName); if (downloadName != null) { downloadsId = downloadName.toLowerCase(); } else { - this.downloadsId = regionId.toLowerCase(); + downloadsId = regionId.toLowerCase(); } if (name != null) { this.name = name; @@ -110,6 +112,10 @@ public class WorldRegion { } return this; } + + public String getSearchText() { + return searchText; + } private void addSubregion(WorldRegion subregion, WorldRegion world) { subregion.superregion = this; diff --git a/OsmAnd/src/net/osmand/plus/download/ui/ActiveDownloadsDialogFragment.java b/OsmAnd/src/net/osmand/plus/download/ui/ActiveDownloadsDialogFragment.java index 91b665743a..07631a7170 100644 --- a/OsmAnd/src/net/osmand/plus/download/ui/ActiveDownloadsDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/download/ui/ActiveDownloadsDialogFragment.java @@ -1,6 +1,7 @@ package net.osmand.plus.download.ui; import java.util.ArrayList; +import java.util.List; import net.osmand.plus.R; import net.osmand.plus.download.DownloadActivity; @@ -8,7 +9,6 @@ import net.osmand.plus.download.DownloadIndexesThread.DownloadEvents; import net.osmand.plus.download.IndexItem; import android.app.AlertDialog; import android.app.Dialog; -import android.graphics.drawable.Drawable; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.v4.app.DialogFragment; @@ -26,7 +26,7 @@ public class ActiveDownloadsDialogFragment extends DialogFragment implements Dow public Dialog onCreateDialog(Bundle savedInstanceState) { AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); builder.setTitle(R.string.downloads).setNegativeButton(R.string.shared_string_dismiss, null); - adapter = new IndexItemAdapter(getDownloadActivity()); + adapter = new IndexItemAdapter(this, getDownloadActivity()); builder.setAdapter(adapter, null); return builder.create(); } @@ -50,20 +50,22 @@ public class ActiveDownloadsDialogFragment extends DialogFragment implements Dow } public static class IndexItemAdapter extends ArrayAdapter { - private final Drawable deleteDrawable; private final DownloadActivity context; + private DialogFragment dlgFragment; - public IndexItemAdapter(DownloadActivity context) { + public IndexItemAdapter(DialogFragment dlgFragment, DownloadActivity context) { super(context, R.layout.two_line_with_images_list_item, new ArrayList()); + this.dlgFragment = dlgFragment; this.context = context; - deleteDrawable = context.getMyApplication().getIconsCache() - .getPaintedContentIcon(R.drawable.ic_action_remove_dark, - context.getResources().getColor(R.color.dash_search_icon_dark)); refreshAllData(); } public void refreshAllData() { clear(); + List items = context.getDownloadThread().getCurrentDownloadingItems(); + if(items.isEmpty()) { + dlgFragment.dismissAllowingStateLoss(); + } addAll(context.getDownloadThread().getCurrentDownloadingItems()); } @@ -72,70 +74,20 @@ public class ActiveDownloadsDialogFragment extends DialogFragment implements Dow if (convertView == null) { convertView = LayoutInflater.from(parent.getContext()) .inflate(R.layout.two_line_with_images_list_item, parent, false); - DownloadEntryViewHolder viewHolder = - new DownloadEntryViewHolder(convertView, context, deleteDrawable, this); + ItemViewHolder viewHolder = + new ItemViewHolder(convertView, context); + viewHolder.setSilentCancelDownload(true); + viewHolder.setShowProgressInDescr(true); convertView.setTag(viewHolder); } - DownloadEntryViewHolder viewHolder = (DownloadEntryViewHolder) convertView.getTag(); + ItemViewHolder viewHolder = (ItemViewHolder) convertView.getTag(); IndexItem item = getItem(position); - IndexItem cdi = context.getDownloadThread().getCurrentDownloadingItem(); - viewHolder.bindDownloadEntry(getItem(position), - cdi == item ? context.getDownloadThread().getCurrentDownloadingItemProgress() : -1, - context.getDownloadThread().isDownloading(item)); + viewHolder.bindIndexItem(item); return convertView; } } - // FIXME review view holder - private static class DownloadEntryViewHolder extends ItemViewHolder { - private final Drawable deleteDrawable; - private final IndexItemAdapter adapter; - - private DownloadEntryViewHolder(View convertView, final DownloadActivity context, - Drawable deleteDrawable, IndexItemAdapter adapter) { - super(convertView, context); - this.deleteDrawable = deleteDrawable; - this.adapter = adapter; - progressBar.setVisibility(View.VISIBLE); - rightImageButton.setImageDrawable(deleteDrawable); - } - - public void bindDownloadEntry(final IndexItem item, final int progress, - boolean isDownloaded) { - nameTextView.setText(item.getVisibleName(context, - context.getMyApplication().getRegions())); - rightImageButton.setVisibility(View.VISIBLE); - - int localProgress = progress; - boolean isIndeterminate = true; - if (progress != -1) { - isIndeterminate = false; - double downloaded = item.getContentSizeMB() * progress / 100; - descrTextView.setText(context.getString(R.string.value_downloaded_from_max, downloaded, - item.getContentSizeMB())); - } else if (isDownloaded) { - isIndeterminate = false; - localProgress = progressBar.getMax(); - descrTextView.setText(context.getString(R.string.file_size_in_mb, - item.getContentSizeMB())); - rightImageButton.setVisibility(View.GONE); - } else { - descrTextView.setText(context.getString(R.string.file_size_in_mb, - item.getContentSizeMB())); - } - rightImageButton.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - context.getDownloadThread().cancelDownload(item); - adapter.refreshAllData(); - } - }); - progressBar.setIndeterminate(isIndeterminate); - progressBar.setProgress(localProgress); - - } - - } + } \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/plus/download/ui/DownloadResourceGroupFragment.java b/OsmAnd/src/net/osmand/plus/download/ui/DownloadResourceGroupFragment.java index 040558e2db..4f5e9126d0 100644 --- a/OsmAnd/src/net/osmand/plus/download/ui/DownloadResourceGroupFragment.java +++ b/OsmAnd/src/net/osmand/plus/download/ui/DownloadResourceGroupFragment.java @@ -36,6 +36,7 @@ import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; +import android.view.View.OnClickListener; import android.widget.ExpandableListView; import android.widget.ExpandableListView.OnChildClickListener; import android.widget.TextView; @@ -170,16 +171,27 @@ public class DownloadResourceGroupFragment extends DialogFragment implements Dow return true; } else if (child instanceof IndexItem) { IndexItem indexItem = (IndexItem) child; - if (indexItem.getType() == DownloadActivityType.ROADS_FILE) { - // FIXME - // if (regularMap.getType() == DownloadActivityType.NORMAL_FILE - // && regularMap.isAlreadyDownloaded(getMyActivity().getIndexFileNames())) { - // ConfirmDownloadUnneededMapDialogFragment.createInstance(indexItem) - // .show(getChildFragmentManager(), "dialog"); - // return true; - // } + DownloadResourceGroup groupObj = listAdapter.getGroupObj(groupPosition); + boolean handled = false; + if (indexItem.getType() == DownloadActivityType.ROADS_FILE && groupObj != null + && !activity.getDownloadThread().isDownloading(indexItem)) { + for (IndexItem ii : groupObj.getIndividualResources()) { + if (ii.getType() == DownloadActivityType.NORMAL_FILE) { + if (ii.isDownloaded()) { + handled = true; + ConfirmDownloadUnneededMapDialogFragment.createInstance(indexItem).show( + getChildFragmentManager(), "dialog"); + } + break; + } + } } ((DownloadActivity) getActivity()).startDownload(indexItem); + if (!handled) { + ItemViewHolder vh = (ItemViewHolder) v.getTag(); + OnClickListener ls = vh.getRightButtonAction(indexItem, vh.getClickAction(indexItem)); + ls.onClick(v); + } return true; } return false; @@ -220,8 +232,7 @@ public class DownloadResourceGroupFragment extends DialogFragment implements Dow getDownloadActivity().getDownloadThread().runReloadIndexFiles(); return true; case SEARCH_ID: - // FIXME - //getDownloadActivity().showDialog(getActivity(), SearchDialogFragment.createInstance("")); + getDownloadActivity().showDialog(getActivity(), SearchDialogFragment.createInstance("")); return true; default: return super.onOptionsItemSelected(item); @@ -237,7 +248,6 @@ public class DownloadResourceGroupFragment extends DialogFragment implements Dow } public static class ConfirmDownloadUnneededMapDialogFragment extends DialogFragment { - private static final String INDEX_ITEM = "index_item"; private static IndexItem item = null; @NonNull @@ -421,6 +431,10 @@ public class DownloadResourceGroupFragment extends DialogFragment implements Dow public int getChildrenCount(int groupPosition) { return data.get(groupPosition).size(); } + + public DownloadResourceGroup getGroupObj(int groupPosition) { + return data.get(groupPosition); + } @Override public String getGroup(int groupPosition) { diff --git a/OsmAnd/src/net/osmand/plus/download/ui/ItemViewHolder.java b/OsmAnd/src/net/osmand/plus/download/ui/ItemViewHolder.java index 29ad64ec75..06393476a6 100644 --- a/OsmAnd/src/net/osmand/plus/download/ui/ItemViewHolder.java +++ b/OsmAnd/src/net/osmand/plus/download/ui/ItemViewHolder.java @@ -16,6 +16,7 @@ import android.graphics.drawable.Drawable; import android.net.Uri; import android.util.TypedValue; import android.view.View; +import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ImageView; import android.widget.ProgressBar; @@ -24,7 +25,6 @@ import android.widget.Toast; public class ItemViewHolder { - private final java.text.DateFormat dateFormat; protected final TextView nameTextView; protected final TextView descrTextView; @@ -42,10 +42,11 @@ public class ItemViewHolder { private int textColorPrimary; private int textColorSecondary; - private RightButtonAction clickAction; boolean showTypeInDesc; boolean showRemoteDate; + boolean silentCancelDownload; + boolean showProgressInDesc; @@ -67,7 +68,6 @@ public class ItemViewHolder { rightImageButton = (ImageView) view.findViewById(R.id.rightImageButton); nameTextView = (TextView) view.findViewById(R.id.name); - this.dateFormat = context.getMyApplication().getResourceManager().getDateFormat(); TypedValue typedValue = new TypedValue(); Resources.Theme theme = context.getTheme(); @@ -81,6 +81,14 @@ public class ItemViewHolder { this.showRemoteDate = showRemoteDate; } + public void setShowProgressInDescr(boolean b) { + showProgressInDesc = b; + } + + public void setSilentCancelDownload(boolean silentCancelDownload) { + this.silentCancelDownload = silentCancelDownload; + } + public void setShowTypeInDesc(boolean showTypeInDesc) { this.showTypeInDesc = showTypeInDesc; } @@ -111,7 +119,7 @@ public class ItemViewHolder { nameTextView.setTextColor(textColorSecondary); } int color = textColorSecondary; - if(indexItem.isDownloaded()) { + if(indexItem.isDownloaded() && !isDownloading) { int colorId = indexItem.isOutdated() ? R.color.color_distance : R.color.color_ok; color = context.getResources().getColor(colorId); } @@ -125,11 +133,9 @@ public class ItemViewHolder { leftImageView.setImageDrawable(getContentIcon(context, indexItem.getType().getIconResource())); } - + descrTextView.setTextColor(color); if (!isDownloading) { progressBar.setVisibility(View.GONE); - - descrTextView.setTextColor(color); descrTextView.setVisibility(View.VISIBLE); if ((indexItem.getType() == DownloadActivityType.SRTM_COUNTRY_FILE || indexItem.getType() == DownloadActivityType.HILLSHADE_FILE) && srtmDisabled) { @@ -151,15 +157,31 @@ public class ItemViewHolder { }); } else { progressBar.setVisibility(View.VISIBLE); + progressBar.setIndeterminate(progress == -1); progressBar.setProgress(progress); - descrTextView.setVisibility(View.GONE); + if (showProgressInDesc) { + double mb = indexItem.getContentSizeMB(); + if (progress != -1) { + descrTextView.setText(context.getString(R.string.value_downloaded_from_max, mb * progress / 100, + mb)); + } else { + descrTextView.setText(context.getString(R.string.file_size_in_mb, mb)); + } + descrTextView.setVisibility(View.VISIBLE); + } else { + descrTextView.setVisibility(View.GONE); + } rightImageButton.setImageDrawable(getContentIcon(context, R.drawable.ic_action_remove_dark)); rightImageButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - context.makeSureUserCancelDownload(indexItem); + if(silentCancelDownload) { + context.getDownloadThread().cancelDownload(indexItem); + } else { + context.makeSureUserCancelDownload(indexItem); + } } }); } @@ -167,12 +189,26 @@ public class ItemViewHolder { private boolean checkDisabledAndClickAction(final IndexItem indexItem) { - boolean disabled = false; - clickAction = RightButtonAction.DOWNLOAD; + RightButtonAction clickAction = getClickAction(indexItem); + boolean disabled = clickAction != RightButtonAction.DOWNLOAD; + if (clickAction != RightButtonAction.DOWNLOAD) { + rightButton.setText(R.string.get_plugin); + rightButton.setVisibility(View.VISIBLE); + rightImageButton.setVisibility(View.GONE); + rightButton.setOnClickListener(getRightButtonAction(indexItem, clickAction)); + } else { + rightButton.setVisibility(View.GONE); + rightImageButton.setVisibility(View.VISIBLE); + } + + return disabled; + } + + public RightButtonAction getClickAction(final IndexItem indexItem) { + RightButtonAction clickAction = RightButtonAction.DOWNLOAD; if (indexItem.getBasename().toLowerCase().equals(DownloadResources.WORLD_SEAMARKS_KEY) && nauticalPluginDisabled) { clickAction = RightButtonAction.ASK_FOR_SEAMARKS_PLUGIN; - disabled = true; } else if ((indexItem.getType() == DownloadActivityType.SRTM_COUNTRY_FILE || indexItem.getType() == DownloadActivityType.HILLSHADE_FILE) && srtmDisabled) { if (srtmNeedsInstallation) { @@ -181,55 +217,61 @@ public class ItemViewHolder { clickAction = RightButtonAction.ASK_FOR_SRTM_PLUGIN_ENABLE; } - disabled = true; } else if (indexItem.getType() == DownloadActivityType.WIKIPEDIA_FILE && freeVersion) { clickAction = RightButtonAction.ASK_FOR_FULL_VERSION_PURCHASE; - disabled = true; } - - if (clickAction != RightButtonAction.DOWNLOAD) { - rightButton.setText(R.string.get_plugin); - rightButton.setVisibility(View.VISIBLE); - rightImageButton.setVisibility(View.GONE); - final RightButtonAction action = clickAction; + return clickAction; + } - rightButton.setOnClickListener(new View.OnClickListener() { + public OnClickListener getRightButtonAction(final IndexItem item, final RightButtonAction clickAction) { + if (clickAction != RightButtonAction.DOWNLOAD) { + return new View.OnClickListener() { @Override public void onClick(View v) { - switch (action) { - case ASK_FOR_FULL_VERSION_PURCHASE: - Intent intent = new Intent(Intent.ACTION_VIEW, - Uri.parse(Version.marketPrefix(context.getMyApplication()) - + "net.osmand.plus")); - context.startActivity(intent); - break; - case ASK_FOR_SEAMARKS_PLUGIN: - context.startActivity(new Intent(context, - context.getMyApplication().getAppCustomization().getPluginsActivity())); - AccessibleToast.makeText(context.getApplicationContext(), - context.getString(R.string.activate_seamarks_plugin), Toast.LENGTH_SHORT).show(); - break; - case ASK_FOR_SRTM_PLUGIN_PURCHASE: - OsmandPlugin plugin = OsmandPlugin.getPlugin(SRTMPlugin.class); - context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(plugin.getInstallURL()))); - break; - case ASK_FOR_SRTM_PLUGIN_ENABLE: - context.startActivity(new Intent(context, - context.getMyApplication().getAppCustomization().getPluginsActivity())); - AccessibleToast.makeText(context, - context.getString(R.string.activate_srtm_plugin), Toast.LENGTH_SHORT).show(); - break; - case DOWNLOAD: - break; + switch (clickAction) { + case ASK_FOR_FULL_VERSION_PURCHASE: + Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(Version.marketPrefix(context + .getMyApplication()) + "net.osmand.plus")); + context.startActivity(intent); + break; + case ASK_FOR_SEAMARKS_PLUGIN: + context.startActivity(new Intent(context, context.getMyApplication().getAppCustomization() + .getPluginsActivity())); + AccessibleToast.makeText(context.getApplicationContext(), + context.getString(R.string.activate_seamarks_plugin), Toast.LENGTH_SHORT).show(); + break; + case ASK_FOR_SRTM_PLUGIN_PURCHASE: + OsmandPlugin plugin = OsmandPlugin.getPlugin(SRTMPlugin.class); + context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(plugin.getInstallURL()))); + break; + case ASK_FOR_SRTM_PLUGIN_ENABLE: + context.startActivity(new Intent(context, context.getMyApplication().getAppCustomization() + .getPluginsActivity())); + AccessibleToast.makeText(context, context.getString(R.string.activate_srtm_plugin), + Toast.LENGTH_SHORT).show(); + break; + case DOWNLOAD: + break; } } - }); + }; } else { - rightButton.setVisibility(View.GONE); - rightImageButton.setVisibility(View.VISIBLE); + final boolean isDownloading = context.getDownloadThread().isDownloading(item); + return new View.OnClickListener() { + @Override + public void onClick(View v) { + if(isDownloading) { + if(silentCancelDownload) { + context.getDownloadThread().cancelDownload(item); + } else { + context.makeSureUserCancelDownload(item); + } + } else { + context.startDownload(item); + } + } + }; } - - return disabled; } private Drawable getContentIcon(DownloadActivity context, int resourceId) { @@ -240,7 +282,5 @@ public class ItemViewHolder { return context.getMyApplication().getIconsCache().getPaintedContentIcon(resourceId, color); } - public boolean isItemAvailable() { - return clickAction == RightButtonAction.DOWNLOAD; - } + } diff --git a/OsmAnd/src/net/osmand/plus/download/ui/SearchDialogFragment.java b/OsmAnd/src/net/osmand/plus/download/ui/SearchDialogFragment.java index 01f83740d5..4208fd283c 100644 --- a/OsmAnd/src/net/osmand/plus/download/ui/SearchDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/download/ui/SearchDialogFragment.java @@ -16,7 +16,16 @@ import net.osmand.plus.R; import net.osmand.plus.download.DownloadActivity; import net.osmand.plus.download.DownloadIndexesThread.DownloadEvents; -public class SearchDialogFragment { /*extends DialogFragment implements DownloadEvents { +// FIXME +public class SearchDialogFragment extends DialogFragment { + + public static DialogFragment createInstance(String tg) { + return new SearchDialogFragment(); + } + + + + /*extends DialogFragment implements DownloadEvents { public static final String TAG = "SearchDialogFragment"; private static final String SEARCH_TEXT_DLG_KEY = "search_text_dlg_key"; diff --git a/OsmAnd/src/net/osmand/plus/download/ui/SearchItemsFragment.java b/OsmAnd/src/net/osmand/plus/download/ui/SearchItemsFragment.java index 6ef6e9b7c3..35b6acf0bb 100644 --- a/OsmAnd/src/net/osmand/plus/download/ui/SearchItemsFragment.java +++ b/OsmAnd/src/net/osmand/plus/download/ui/SearchItemsFragment.java @@ -29,6 +29,7 @@ import android.widget.Filter; import android.widget.Filterable; import android.widget.ListView; +//FIXME merge into search dialog fragment public class SearchItemsFragment { /*extends Fragment implements DownloadEvents { public static final String TAG = "SearchItemsFragment"; diff --git a/OsmAnd/src/net/osmand/plus/download/ui/UpdatesIndexFragment.java b/OsmAnd/src/net/osmand/plus/download/ui/UpdatesIndexFragment.java index 0daeb47e63..c41fad496d 100644 --- a/OsmAnd/src/net/osmand/plus/download/ui/UpdatesIndexFragment.java +++ b/OsmAnd/src/net/osmand/plus/download/ui/UpdatesIndexFragment.java @@ -20,6 +20,7 @@ import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; +import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.ListView; @@ -118,7 +119,9 @@ public class UpdatesIndexFragment extends OsmAndListFragment implements Download @Override public void onListItemClick(ListView l, View v, int position, long id) { final IndexItem e = (IndexItem) getListAdapter().getItem(position); - getMyActivity().startDownload(e); + ItemViewHolder vh = (ItemViewHolder) v.getTag(); + OnClickListener ls = vh.getRightButtonAction(e, vh.getClickAction(e)); + ls.onClick(v); } public DownloadActivity getMyActivity() { From d1e802a05fa470c49e970ffdbc06543b0f9e5580 Mon Sep 17 00:00:00 2001 From: Victor Shcherb Date: Mon, 19 Oct 2015 22:39:29 +0200 Subject: [PATCH 19/22] Fix view holder --- .../plus/download/DownloadIndexesThread.java | 7 +- .../plus/download/DownloadResources.java | 22 +++++ .../net/osmand/plus/download/IndexItem.java | 89 +++++++------------ .../plus/download/ui/ItemViewHolder.java | 8 +- 4 files changed, 67 insertions(+), 59 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/download/DownloadIndexesThread.java b/OsmAnd/src/net/osmand/plus/download/DownloadIndexesThread.java index 88a4f0e6bb..5fb60f3b36 100644 --- a/OsmAnd/src/net/osmand/plus/download/DownloadIndexesThread.java +++ b/OsmAnd/src/net/osmand/plus/download/DownloadIndexesThread.java @@ -69,11 +69,15 @@ public class DownloadIndexesThread { this.ctx = ctx; app = (OsmandApplication) ctx.getApplicationContext(); indexes = new DownloadResources(app); - indexes.initAlreadyLoadedFiles(); + updateLoadedFiles(); downloadFileHelper = new DownloadFileHelper(app); dbHelper = new DatabaseHelper(app); } + public void updateLoadedFiles() { + indexes.initAlreadyLoadedFiles(); + } + /// UI notifications methods public void setUiActivity(BaseDownloadActivity uiActivity) { this.uiActivity = uiActivity; @@ -318,6 +322,7 @@ public class DownloadIndexesThread { if (o instanceof IndexItem) { IndexItem item = (IndexItem) o; String name = item.getBasename(); + item.setDownloaded(true); long count = dbHelper.getCount(name, DatabaseHelper.DOWNLOAD_ENTRY) + 1; DatabaseHelper.HistoryDownloadEntry entry = new DatabaseHelper.HistoryDownloadEntry(name, count); if (count == 1) { diff --git a/OsmAnd/src/net/osmand/plus/download/DownloadResources.java b/OsmAnd/src/net/osmand/plus/download/DownloadResources.java index d7dfd0bd45..c1a98e7401 100644 --- a/OsmAnd/src/net/osmand/plus/download/DownloadResources.java +++ b/OsmAnd/src/net/osmand/plus/download/DownloadResources.java @@ -4,6 +4,7 @@ import java.io.File; import java.io.FilenameFilter; import java.io.IOException; import java.io.InputStream; +import java.text.ParseException; import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.LinkedList; @@ -57,10 +58,30 @@ public class DownloadResources extends DownloadResourceGroup { String sfName = item.getTargetFileName(); String indexactivateddate = indexActivatedFileNames.get(sfName); String indexfilesdate = indexFileNames.get(sfName); + item.setDownloaded(false); + item.setOutdated(false); if(indexactivateddate == null && indexfilesdate == null) { return outdated; } + item.setDownloaded(true); String date = item.getDate(format); + boolean parsed = false; + if(indexactivateddate != null) { + try { + item.setLocalTimestamp(format.parse(indexactivateddate).getTime()); + parsed = true; + } catch (ParseException e) { + e.printStackTrace(); + } + } + if (!parsed && indexfilesdate != null) { + try { + item.setLocalTimestamp(format.parse(indexfilesdate).getTime()); + parsed = true; + } catch (ParseException e) { + e.printStackTrace(); + } + } if (date != null && !date.equals(indexactivateddate) && !date.equals(indexfilesdate)) { if ((item.getType() == DownloadActivityType.NORMAL_FILE && !item.extra) || item.getType() == DownloadActivityType.ROADS_FILE @@ -96,6 +117,7 @@ public class DownloadResources extends DownloadResourceGroup { } } } + item.setOutdated(outdated); return outdated; } diff --git a/OsmAnd/src/net/osmand/plus/download/IndexItem.java b/OsmAnd/src/net/osmand/plus/download/IndexItem.java index 04b61aaf06..dc70cb0bfa 100644 --- a/OsmAnd/src/net/osmand/plus/download/IndexItem.java +++ b/OsmAnd/src/net/osmand/plus/download/IndexItem.java @@ -3,9 +3,7 @@ package net.osmand.plus.download; import java.io.File; import java.io.IOException; import java.text.DateFormat; -import java.text.ParseException; import java.util.Date; -import java.util.Map; import net.osmand.IndexConstants; import net.osmand.PlatformUtil; @@ -29,6 +27,11 @@ public class IndexItem implements Comparable { long containerSize; DownloadActivityType type; boolean extra; + + // Update information + boolean outdated; + boolean downloaded; + long localTimestamp; public IndexItem(String fileName, String description, long timestamp, String size, long contentSize, @@ -134,73 +137,47 @@ public class IndexItem implements Comparable { public String getDaysBehind(OsmandApplication app) { - // FIXME - DateFormat format = app.getResourceManager().getDateFormat(); - String sfName = getTargetFileName(); - Map indexActivatedFileNames = app.getResourceManager().getIndexFileNames(); - String dt = indexActivatedFileNames.get(sfName); - if (dt != null) { - try { - Date tm = format.parse(dt); - long days = Math.max(1, (getTimestamp() - tm.getTime()) / (24 * 60 * 60 * 1000) + 1); - return days + " " + app.getString(R.string.days_behind); - } catch (ParseException e1) { - e1.printStackTrace(); - } + if (localTimestamp > 0) { + long days = Math.max(1, (getTimestamp() - localTimestamp) / (24 * 60 * 60 * 1000) + 1); + return days + " " + app.getString(R.string.days_behind); } return ""; } - public String getRemoteDate() { - // FIXME; - return "FIXME"; + public String getRemoteDate(DateFormat dateFormat) { + if(timestamp <= 0) { + return ""; + } + return dateFormat.format(new Date(timestamp)); + } + + + public String getLocalDate(DateFormat dateFormat) { + if(localTimestamp <= 0) { + return ""; + } + return dateFormat.format(new Date(localTimestamp)); } public boolean isOutdated() { - //FIXME; - return false; + return outdated; } - public String getLocalDate() { - //FIXME; -// DateFormat format = app.getResourceManager().getDateFormat(); -// String sfName = getTargetFileName(); -// Map indexActivatedFileNames = app.getResourceManager().getIndexFileNames(); -// String dt = indexActivatedFileNames.get(sfName); -// if (dt != null) { -// try { -// Date tm = format.parse(dt); -// long days = Math.max(1, (getTimestamp() - tm.getTime()) / (24 * 60 * 60 * 1000) + 1); -// return days + " " + app.getString(R.string.days_behind); -// } catch (ParseException e1) { -// e1.printStackTrace(); -// } -// } -// return ""; - - return "FIXME"; + public void setOutdated(boolean outdated) { + this.outdated = outdated; + } + + public void setDownloaded(boolean downloaded) { + this.downloaded = downloaded; + } + + public void setLocalTimestamp(long localTimestamp) { + this.localTimestamp = localTimestamp; } public boolean isDownloaded() { - // FIXME -// return listAlreadyDownloaded.containsKey(getTargetFileName()); - /* - Map indexFileNames = context.getIndexFileNames(); - if (indexFileNames != null && indexItem.isAlreadyDownloaded(indexFileNames)) { - boolean outdated = false; - String date; - if (indexItem.getType() == DownloadActivityType.HILLSHADE_FILE) { - date = indexItem.getDate(dateFormat); - } else { - String sfName = indexItem.getTargetFileName(); - Map indexActivatedFileNames = context.getIndexActivatedFileNames(); - final boolean updatableResource = indexActivatedFileNames.containsKey(sfName); - date = updatableResource ? indexActivatedFileNames.get(sfName) : indexFileNames.get(sfName); - outdated = DownloadActivity.downloadListIndexThread.checkIfItemOutdated(indexItem); - } - */ - return false; + return downloaded; } public String getVisibleName(Context ctx, OsmandRegions osmandRegions) { diff --git a/OsmAnd/src/net/osmand/plus/download/ui/ItemViewHolder.java b/OsmAnd/src/net/osmand/plus/download/ui/ItemViewHolder.java index 06393476a6..d7c64f56d5 100644 --- a/OsmAnd/src/net/osmand/plus/download/ui/ItemViewHolder.java +++ b/OsmAnd/src/net/osmand/plus/download/ui/ItemViewHolder.java @@ -1,5 +1,7 @@ package net.osmand.plus.download.ui; +import java.text.DateFormat; + import net.osmand.access.AccessibleToast; import net.osmand.plus.OsmandPlugin; import net.osmand.plus.R; @@ -47,6 +49,7 @@ public class ItemViewHolder { boolean showRemoteDate; boolean silentCancelDownload; boolean showProgressInDesc; + private DateFormat dateFormat; @@ -61,6 +64,7 @@ public class ItemViewHolder { public ItemViewHolder(View view, DownloadActivity context) { this.context = context; + dateFormat = android.text.format.DateFormat.getMediumDateFormat(context); progressBar = (ProgressBar) view.findViewById(R.id.progressBar); rightButton = (Button) view.findViewById(R.id.rightButton); leftImageView = (ImageView) view.findViewById(R.id.leftImageView); @@ -143,9 +147,9 @@ public class ItemViewHolder { } else if (showTypeInDesc) { descrTextView.setText(indexItem.getType().getString(context) + " • " + indexItem.getSizeDescription(context) + - " • " + (showRemoteDate ? indexItem.getRemoteDate() : indexItem.getLocalDate())); + " • " + (showRemoteDate ? indexItem.getRemoteDate(dateFormat) : indexItem.getLocalDate(dateFormat))); } else { - descrTextView.setText(indexItem.getSizeDescription(context) + " • " + (showRemoteDate ? indexItem.getRemoteDate() : indexItem.getLocalDate())); + descrTextView.setText(indexItem.getSizeDescription(context) + " • " + (showRemoteDate ? indexItem.getRemoteDate(dateFormat) : indexItem.getLocalDate(dateFormat))); } rightImageButton.setImageDrawable(getContentIcon(context, R.drawable.ic_action_import)); From d66031f4d71fbcde7a1c51fa00a052cdf9b36730 Mon Sep 17 00:00:00 2001 From: Victor Shcherb Date: Mon, 19 Oct 2015 22:50:37 +0200 Subject: [PATCH 20/22] Fix small issues --- .../net/osmand/plus/download/IndexItem.java | 4 ++++ .../ui/ActiveDownloadsDialogFragment.java | 1 + .../ui/DownloadResourceGroupFragment.java | 1 - .../plus/download/ui/ItemViewHolder.java | 21 ++++++++++++------- 4 files changed, 19 insertions(+), 8 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/download/IndexItem.java b/OsmAnd/src/net/osmand/plus/download/IndexItem.java index dc70cb0bfa..aeea72e1b9 100644 --- a/OsmAnd/src/net/osmand/plus/download/IndexItem.java +++ b/OsmAnd/src/net/osmand/plus/download/IndexItem.java @@ -73,6 +73,10 @@ public class IndexItem implements Comparable { public double getContentSizeMB() { return ((double)contentSize) / (1 << 20); } + + public double getArchiveSizeMB() { + return ((double)containerSize) / (1 << 20); + } public String getSizeDescription(Context ctx) { return size + " MB"; diff --git a/OsmAnd/src/net/osmand/plus/download/ui/ActiveDownloadsDialogFragment.java b/OsmAnd/src/net/osmand/plus/download/ui/ActiveDownloadsDialogFragment.java index 07631a7170..1137650526 100644 --- a/OsmAnd/src/net/osmand/plus/download/ui/ActiveDownloadsDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/download/ui/ActiveDownloadsDialogFragment.java @@ -77,6 +77,7 @@ public class ActiveDownloadsDialogFragment extends DialogFragment implements Dow ItemViewHolder viewHolder = new ItemViewHolder(convertView, context); viewHolder.setSilentCancelDownload(true); + viewHolder.setShowTypeInDesc(true); viewHolder.setShowProgressInDescr(true); convertView.setTag(viewHolder); } diff --git a/OsmAnd/src/net/osmand/plus/download/ui/DownloadResourceGroupFragment.java b/OsmAnd/src/net/osmand/plus/download/ui/DownloadResourceGroupFragment.java index 4f5e9126d0..974dc67aaa 100644 --- a/OsmAnd/src/net/osmand/plus/download/ui/DownloadResourceGroupFragment.java +++ b/OsmAnd/src/net/osmand/plus/download/ui/DownloadResourceGroupFragment.java @@ -186,7 +186,6 @@ public class DownloadResourceGroupFragment extends DialogFragment implements Dow } } } - ((DownloadActivity) getActivity()).startDownload(indexItem); if (!handled) { ItemViewHolder vh = (ItemViewHolder) v.getTag(); OnClickListener ls = vh.getRightButtonAction(indexItem, vh.getClickAction(indexItem)); diff --git a/OsmAnd/src/net/osmand/plus/download/ui/ItemViewHolder.java b/OsmAnd/src/net/osmand/plus/download/ui/ItemViewHolder.java index d7c64f56d5..58d551d055 100644 --- a/OsmAnd/src/net/osmand/plus/download/ui/ItemViewHolder.java +++ b/OsmAnd/src/net/osmand/plus/download/ui/ItemViewHolder.java @@ -1,7 +1,6 @@ package net.osmand.plus.download.ui; import java.text.DateFormat; - import net.osmand.access.AccessibleToast; import net.osmand.plus.OsmandPlugin; import net.osmand.plus.R; @@ -12,6 +11,7 @@ import net.osmand.plus.download.DownloadResources; import net.osmand.plus.download.IndexItem; import net.osmand.plus.openseamapsplugin.NauticalMapsPlugin; import net.osmand.plus.srtmplugin.SRTMPlugin; +import android.annotation.SuppressLint; import android.content.Intent; import android.content.res.Resources; import android.graphics.drawable.Drawable; @@ -137,7 +137,7 @@ public class ItemViewHolder { leftImageView.setImageDrawable(getContentIcon(context, indexItem.getType().getIconResource())); } - descrTextView.setTextColor(color); + descrTextView.setTextColor(textColorSecondary); if (!isDownloading) { progressBar.setVisibility(View.GONE); descrTextView.setVisibility(View.VISIBLE); @@ -149,7 +149,8 @@ public class ItemViewHolder { " • " + indexItem.getSizeDescription(context) + " • " + (showRemoteDate ? indexItem.getRemoteDate(dateFormat) : indexItem.getLocalDate(dateFormat))); } else { - descrTextView.setText(indexItem.getSizeDescription(context) + " • " + (showRemoteDate ? indexItem.getRemoteDate(dateFormat) : indexItem.getLocalDate(dateFormat))); + descrTextView.setText(indexItem.getSizeDescription(context) + " • " + + (showRemoteDate ? indexItem.getRemoteDate(dateFormat) : indexItem.getLocalDate(dateFormat))); } rightImageButton.setImageDrawable(getContentIcon(context, R.drawable.ic_action_import)); @@ -165,12 +166,17 @@ public class ItemViewHolder { progressBar.setProgress(progress); if (showProgressInDesc) { - double mb = indexItem.getContentSizeMB(); + double mb = indexItem.getArchiveSizeMB(); + String v ; if (progress != -1) { - descrTextView.setText(context.getString(R.string.value_downloaded_from_max, mb * progress / 100, - mb)); + v = context.getString(R.string.value_downloaded_from_max, mb * progress / 100, mb); } else { - descrTextView.setText(context.getString(R.string.file_size_in_mb, mb)); + v = context.getString(R.string.file_size_in_mb, mb); + } + if(showTypeInDesc) { + descrTextView.setText(indexItem.getType().getString(context) + " • " +v); + } else { + descrTextView.setText(v); } descrTextView.setVisibility(View.VISIBLE); } else { @@ -208,6 +214,7 @@ public class ItemViewHolder { return disabled; } + @SuppressLint("DefaultLocale") public RightButtonAction getClickAction(final IndexItem indexItem) { RightButtonAction clickAction = RightButtonAction.DOWNLOAD; if (indexItem.getBasename().toLowerCase().equals(DownloadResources.WORLD_SEAMARKS_KEY) From 654f6c37dfcb42c8bfd50bb34162bee9517aa415 Mon Sep 17 00:00:00 2001 From: Victor Shcherb Date: Mon, 19 Oct 2015 22:54:26 +0200 Subject: [PATCH 21/22] Update indexes --- OsmAnd/res/values/strings.xml | 2 +- .../osmand/plus/download/ui/UpdatesIndexFragment.java | 11 ++++++++--- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/OsmAnd/res/values/strings.xml b/OsmAnd/res/values/strings.xml index 4ef7857661..f3d18725fd 100644 --- a/OsmAnd/res/values/strings.xml +++ b/OsmAnd/res/values/strings.xml @@ -2016,7 +2016,7 @@ Afghanistan, Albania, Algeria, Andorra, Angola, Anguilla, Antigua and Barbuda, A Are you sure you want to download map of roads, even though you have full map? %1$.1f from %2$.1f MB %.1f MB - Update all(%1$s Mb) + Update all (%1$s MB) Free downloads used You can set how many free downloads you have used diff --git a/OsmAnd/src/net/osmand/plus/download/ui/UpdatesIndexFragment.java b/OsmAnd/src/net/osmand/plus/download/ui/UpdatesIndexFragment.java index c41fad496d..811f7a067d 100644 --- a/OsmAnd/src/net/osmand/plus/download/ui/UpdatesIndexFragment.java +++ b/OsmAnd/src/net/osmand/plus/download/ui/UpdatesIndexFragment.java @@ -50,6 +50,7 @@ public class UpdatesIndexFragment extends OsmAndListFragment implements Download @Override public void downloadHasFinished() { invalidateListView(); + updateUpdateAllButton(); } @Override @@ -60,6 +61,7 @@ public class UpdatesIndexFragment extends OsmAndListFragment implements Download @Override public void newDownloadIndexes() { invalidateListView(); + updateUpdateAllButton(); } public void invalidateListView() { @@ -82,15 +84,17 @@ public class UpdatesIndexFragment extends OsmAndListFragment implements Download .compareTo(indexItem2.getVisibleName(getMyApplication(), osmandRegions)); } }); - updateUpdateAllButton(indexes.getItemsToUpdate()); setListAdapter(listAdapter); } - private void updateUpdateAllButton(final List indexItems) { + private void updateUpdateAllButton() { + View view = getView(); - if (getView() == null) { + if (view == null) { return; } + DownloadResources indexes = getMyActivity().getDownloadThread().getIndexes(); + final List indexItems = indexes.getItemsToUpdate(); final TextView updateAllButton = (TextView) view.findViewById(R.id.updateAllButton); if (indexItems.size() == 0 || indexItems.get(0).getType() == null) { updateAllButton.setVisibility(View.GONE); @@ -114,6 +118,7 @@ public class UpdatesIndexFragment extends OsmAndListFragment implements Download @Override public void onResume() { super.onResume(); + updateUpdateAllButton(); } @Override From 27b6b278bd7f4ad72c066a2e9c20ef0cb886cc51 Mon Sep 17 00:00:00 2001 From: Victor Shcherb Date: Mon, 19 Oct 2015 23:14:07 +0200 Subject: [PATCH 22/22] Refactor --- OsmAnd/src/net/osmand/plus/download/ui/ItemViewHolder.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/download/ui/ItemViewHolder.java b/OsmAnd/src/net/osmand/plus/download/ui/ItemViewHolder.java index 58d551d055..885c6f6b05 100644 --- a/OsmAnd/src/net/osmand/plus/download/ui/ItemViewHolder.java +++ b/OsmAnd/src/net/osmand/plus/download/ui/ItemViewHolder.java @@ -173,8 +173,8 @@ public class ItemViewHolder { } else { v = context.getString(R.string.file_size_in_mb, mb); } - if(showTypeInDesc) { - descrTextView.setText(indexItem.getType().getString(context) + " • " +v); + if(showTypeInDesc && indexItem.getType() == DownloadActivityType.ROADS_FILE) { + descrTextView.setText(indexItem.getType().getString(context) + " • " + v); } else { descrTextView.setText(v); }