diff --git a/OsmAnd/src/net/osmand/plus/download/BaseDownloadActivity.java b/OsmAnd/src/net/osmand/plus/download/BaseDownloadActivity.java index c1e49861a2..5017abd669 100644 --- a/OsmAnd/src/net/osmand/plus/download/BaseDownloadActivity.java +++ b/OsmAnd/src/net/osmand/plus/download/BaseDownloadActivity.java @@ -57,8 +57,11 @@ public class BaseDownloadActivity extends ActionBarProgressActivity { public void cancelDownload(IndexItem item) { - // TODO Auto-generated method stub - FIXME; + downloadListIndexThread.cancelDownload(item); + } + + public void startDownload(IndexItem... items) { + downloadFilesWithAllChecks(items); } @@ -71,7 +74,7 @@ public class BaseDownloadActivity extends ActionBarProgressActivity { } @UiThread - public void updateProgress(boolean updateOnlyProgress, Object tag) { + public void updateProgress(boolean updateOnlyProgress) { } public void downloadedIndexes() { @@ -119,66 +122,63 @@ public class BaseDownloadActivity extends ActionBarProgressActivity { } } - public boolean startDownload(IndexItem... items) { - for(IndexItem i : items) { - downloadListIndexThread.addToDownload(i); - } - // FIXME ??? commented line -// if (downloadListIndexThread.getCurrentRunningTask() != null && getEntriesToDownload().get(item) == null) { -// return false; -// } - downloadFilesWithAllChecks(); - updateFragments(); - return true; - } - - - public void downloadFilesPreCheckSpace() { - double sz = 0; - List list = downloadListIndexThread.flattenDownloadEntries(); - for (DownloadEntry es : list) { - sz += es.sizeMB; + public void downloadFilesCheck_3_ValidateSpace(final IndexItem... items) { + long szLong = 0; + int i = 0; + for (IndexItem es : downloadListIndexThread.getCurrentDownloadingItems()) { + szLong += es.contentSize; + i++; } + for (IndexItem es : items) { + szLong += es.contentSize; + i++; + } + double sz = ((double) szLong) / (1 << 20); // get availabile space double asz = downloadListIndexThread.getAvailableSpace(); if (asz != -1 && asz > 0 && sz / asz > 0.4) { AlertDialog.Builder builder = new AlertDialog.Builder(this); - builder.setMessage(MessageFormat.format(getString(R.string.download_files_question_space), list.size(), sz, asz)); + builder.setMessage(MessageFormat.format(getString(R.string.download_files_question_space), i, sz, asz)); builder.setPositiveButton(R.string.shared_string_yes, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { - downloadListIndexThread.runDownloadFiles(); + downloadFileCheck_Final_Run(items); } }); builder.setNegativeButton(R.string.shared_string_no, null); builder.show(); } else { - downloadListIndexThread.runDownloadFiles(); + downloadFileCheck_Final_Run(items); } } + private void downloadFileCheck_Final_Run(IndexItem[] items) { + downloadListIndexThread.runDownloadFiles(items); + updateFragments(); + } - protected void downloadFilesWithAllChecks() { - downloadFilesCheckFreeVersion(); + + protected void downloadFilesWithAllChecks(IndexItem[] items) { + downloadFilesCheck_1_FreeVersion(items); } - protected void downloadFilesCheckFreeVersion() { + protected void downloadFilesCheck_1_FreeVersion(IndexItem[] items) { if (Version.isFreeVersion(getMyApplication())) { int total = settings.NUMBER_OF_FREE_DOWNLOADS.get(); if (total > MAXIMUM_AVAILABLE_FREE_DOWNLOADS) { new InstallPaidVersionDialogFragment() .show(getSupportFragmentManager(), InstallPaidVersionDialogFragment.TAG); } else { - downloadFilesCheckInternet(); + downloadFilesCheck_2_Internet(items); } } else { - downloadFilesCheckInternet(); + downloadFilesCheck_2_Internet(items); } } - protected void downloadFilesCheckInternet() { + protected void downloadFilesCheck_2_Internet(IndexItem[] items) { if (!getMyApplication().getSettings().isWifiConnected()) { if (getMyApplication().getSettings().isInternetConnectionAvailable()) { new ConfirmDownloadDialogFragment().show(getSupportFragmentManager(), @@ -187,7 +187,7 @@ public class BaseDownloadActivity extends ActionBarProgressActivity { AccessibleToast.makeText(this, R.string.no_index_file_to_download, Toast.LENGTH_LONG).show(); } } else { - downloadFilesPreCheckSpace(); + downloadFilesCheck_3_ValidateSpace(items); } } @@ -196,8 +196,7 @@ public class BaseDownloadActivity extends ActionBarProgressActivity { fragSet.add(new WeakReference(fragment)); } - public void makeSureUserCancelDownload(IndexItem item) { - TODO; + public void makeSureUserCancelDownload(final IndexItem item) { AlertDialog.Builder bld = new AlertDialog.Builder(this); bld.setTitle(getString(R.string.shared_string_cancel)); bld.setMessage(R.string.confirm_interrupt_download); @@ -205,14 +204,13 @@ public class BaseDownloadActivity extends ActionBarProgressActivity { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); - cancelDownload.run(); + cancelDownload(item); } }); bld.setNegativeButton(R.string.shared_string_no, null); bld.show(); } - - + public static class InstallPaidVersionDialogFragment extends DialogFragment { public static final String TAG = "InstallPaidVersionDialogFragment"; @NonNull @@ -258,7 +256,7 @@ public class BaseDownloadActivity extends ActionBarProgressActivity { builder.setPositiveButton(R.string.shared_string_yes, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { - ((BaseDownloadActivity) getActivity()).downloadFilesPreCheckSpace(); + ((BaseDownloadActivity) getActivity()).downloadFilesCheck_3_ValidateSpace(); } }); builder.setNegativeButton(R.string.shared_string_no, null); diff --git a/OsmAnd/src/net/osmand/plus/download/DownloadActivity.java b/OsmAnd/src/net/osmand/plus/download/DownloadActivity.java index 3499e83700..fef7af4753 100644 --- a/OsmAnd/src/net/osmand/plus/download/DownloadActivity.java +++ b/OsmAnd/src/net/osmand/plus/download/DownloadActivity.java @@ -150,7 +150,7 @@ public class DownloadActivity extends BaseDownloadActivity implements DialogDism } @Override - public void updateProgress(boolean updateOnlyProgress, Object tag) { + public void updateProgress(boolean updateOnlyProgress) { BasicProgressAsyncTask basicProgressAsyncTask = downloadListIndexThread.getCurrentRunningTask(); if (visibleBanner != null) { @@ -214,7 +214,8 @@ public class DownloadActivity extends BaseDownloadActivity implements DialogDism if(activeDownloads != null) { activeDownloads.refresh(); } - ((DownloadActivity) getActivity()).updateDescriptionTextWithSize(getView()); + // FIXME + //((DownloadActivity) getActivity()).updateDescriptionTextWithSize(getView()); for (WeakReference ref : fragSet) { Fragment f = ref.get(); notifyUpdateDataSetChanged(f); @@ -259,7 +260,7 @@ public class DownloadActivity extends BaseDownloadActivity implements DialogDism public void registerFreeVersionBanner(View view) { visibleBanner = new BannerAndDownloadFreeVersion(view, this); - updateProgress(true, null); + updateProgress(true); } @@ -354,7 +355,6 @@ public class DownloadActivity extends BaseDownloadActivity implements DialogDism boolean indeterminate = basicProgressAsyncTask.isIndeterminate(); String message = basicProgressAsyncTask.getDescription(); int percent = basicProgressAsyncTask.getProgressPercentage(); - setMinimizedFreeVersionBanner(true); updateAvailableDownloads(countedDownloads); downloadProgressLayout.setVisibility(View.VISIBLE); diff --git a/OsmAnd/src/net/osmand/plus/download/DownloadEntry.java b/OsmAnd/src/net/osmand/plus/download/DownloadEntry.java index 1ceb0eb258..23c68c66c3 100644 --- a/OsmAnd/src/net/osmand/plus/download/DownloadEntry.java +++ b/OsmAnd/src/net/osmand/plus/download/DownloadEntry.java @@ -14,20 +14,16 @@ public class DownloadEntry { public String baseName; public String urlToDownload; - public File existingBackupFile; public boolean isAsset; public String assetName; public DownloadActivityType type; - public IndexItem item; - public DownloadEntry(IndexItem item) { - this.item = item; + public DownloadEntry() { } - - public DownloadEntry(IndexItem pr, String assetName, String fileName, long dateModified) { + + public DownloadEntry(String assetName, String fileName, long dateModified) { this.dateModified = dateModified; - this.item = pr; targetFile = new File(fileName); this.assetName = assetName; isAsset = true; diff --git a/OsmAnd/src/net/osmand/plus/download/DownloadIndexesThread.java b/OsmAnd/src/net/osmand/plus/download/DownloadIndexesThread.java index 4774d714c7..cbd26bd0ee 100644 --- a/OsmAnd/src/net/osmand/plus/download/DownloadIndexesThread.java +++ b/OsmAnd/src/net/osmand/plus/download/DownloadIndexesThread.java @@ -1,19 +1,22 @@ package net.osmand.plus.download; -import android.annotation.SuppressLint; -import android.app.AlertDialog; -import android.content.ActivityNotFoundException; -import android.content.Context; -import android.content.DialogInterface; -import android.content.Intent; -import android.net.Uri; -import android.os.AsyncTask; -import android.os.AsyncTask.Status; -import android.os.Build; -import android.os.StatFs; -import android.support.annotation.UiThread; -import android.view.View; -import android.widget.Toast; +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; @@ -34,25 +37,20 @@ import net.osmand.util.Algorithms; import org.apache.commons.logging.Log; -import java.io.File; -import java.io.FilenameFilter; -import java.io.IOException; -import java.io.InputStream; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.Comparator; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Iterator; -import java.util.LinkedHashMap; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; -import java.util.Set; -import java.util.TreeSet; -import java.util.concurrent.ConcurrentHashMap; +import android.annotation.SuppressLint; +import android.app.AlertDialog; +import android.content.ActivityNotFoundException; +import android.content.Context; +import android.content.DialogInterface; +import android.content.Intent; +import android.net.Uri; +import android.os.AsyncTask; +import android.os.AsyncTask.Status; +import android.os.Build; +import android.os.StatFs; +import android.support.annotation.UiThread; +import android.view.View; +import android.widget.Toast; @SuppressLint("NewApi") public class DownloadIndexesThread { @@ -64,6 +62,9 @@ public class DownloadIndexesThread { private DatabaseHelper dbHelper; private DownloadFileHelper downloadFileHelper; private List> currentRunningTask = Collections.synchronizedList(new ArrayList>()); + private ConcurrentLinkedQueue indexItemDownloading = new ConcurrentLinkedQueue(); + private IndexItem currentDownloadingItem = null; + private int currentDownloadingItemProgress = 0; private DownloadIndexes indexes = new DownloadIndexes(); @@ -76,7 +77,6 @@ public class DownloadIndexesThread { private Map indexFileNames = new LinkedHashMap<>(); private Map indexActivatedFileNames = new LinkedHashMap<>(); private List itemsToUpdate = new ArrayList<>(); - private Map> entriesToDownload = new LinkedHashMap>(); @@ -92,22 +92,17 @@ public class DownloadIndexesThread { public void setUiActivity(BaseDownloadActivity uiActivity) { this.uiActivity = uiActivity; } - - public List flattenDownloadEntries() { - List res = new ArrayList(); - for (List ens : getEntriesToDownload().values()) { - if (ens != null) { - res.addAll(ens); - } + + public List getCurrentDownloadingItems() { + List res = new ArrayList(); + IndexItem ii = currentDownloadingItem; + if(ii != null) { + res.add(ii); } + res.addAll(indexItemDownloading); return res; } - protected void addToDownload(IndexItem item) { - List download = item.createDownloadEntry(getMyApplication(), item.getType(), new ArrayList()); - getEntriesToDownload().put(item, download); - } - public List getCachedIndexFiles() { return indexes.indexFiles != null ? indexes.indexFiles.getIndexFiles() : null; } @@ -268,11 +263,26 @@ public class DownloadIndexesThread { execute(new ReloadIndexesTask(ctx)); } - public void runDownloadFiles() { - if (checkRunning()) { - return; + public void runDownloadFiles(IndexItem... items) { + if (getCurrentRunningTask() instanceof ReloadIndexesTask) { + if(checkRunning()) { + return; + } } - execute(new DownloadIndexesAsyncTask(ctx)); + for(IndexItem i : items) { + indexItemDownloading.add(i); + } + if (currentDownloadingItem == null) { + execute(new DownloadIndexesAsyncTask(ctx)); + } + } + + public void cancelDownload(IndexItem item) { + if(currentDownloadingItem == item) { + downloadFileHelper.setInterruptDownloading(true);; + } + // TODO Auto-generated method stub + } private

void execute(BasicProgressAsyncTask task, P... indexItems) { @@ -387,7 +397,16 @@ public class DownloadIndexesThread { } 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) { @@ -451,12 +470,12 @@ public class DownloadIndexesThread { public int getCountedDownloads() { int i = 0; - Collection> vs = getEntriesToDownload().values(); - for (List v : vs) { - for (DownloadEntry e : v) { - if (DownloadActivityType.isCountedInDownloads(e.item)) { - i++; - } + if(currentDownloadingItem != null && DownloadActivityType.isCountedInDownloads(currentDownloadingItem)) { + i++; + } + for(IndexItem ii : indexItemDownloading) { + if (DownloadActivityType.isCountedInDownloads(ii)) { + i++; } } return i; @@ -501,7 +520,7 @@ public class DownloadIndexesThread { } currentRunningTask.remove(this); if (uiActivity != null) { - uiActivity.updateProgress(false, tag); + uiActivity.updateProgress(false); uiActivity.onCategorizationFinished(); } } @@ -532,7 +551,7 @@ public class DownloadIndexesThread { @Override protected void updateProgress(boolean updateOnlyProgress, Void tag) { if (uiActivity != null) { - uiActivity.updateProgress(updateOnlyProgress, null); + uiActivity.updateProgress(updateOnlyProgress); } } } @@ -542,6 +561,7 @@ public class DownloadIndexesThread { public class DownloadIndexesAsyncTask extends BasicProgressAsyncTask implements DownloadFileShowWarning { private OsmandPreference downloads; + public DownloadIndexesAsyncTask(Context ctx) { super(ctx); @@ -559,11 +579,11 @@ public class DownloadIndexesThread { @Override protected void onProgressUpdate(Object... values) { for (Object o : values) { - if (o instanceof DownloadEntry) { + if (o instanceof IndexItem) { if (uiActivity != null) { uiActivity.updateFragments(); - DownloadEntry item = (DownloadEntry) o; - String name = item.item.getBasename(); + 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) { @@ -572,15 +592,6 @@ public class DownloadIndexesThread { dbHelper.update(entry, DatabaseHelper.DOWNLOAD_ENTRY); } } - } else if (o instanceof IndexItem) { - entriesToDownload.remove(o); - if (uiActivity != null) { - uiActivity.updateFragments(); - IndexItem item = (IndexItem) o; - - long count = dbHelper.getCount(item.getBasename(), DatabaseHelper.DOWNLOAD_ENTRY) + 1; - dbHelper.add(new DatabaseHelper.HistoryDownloadEntry(item.getBasename(), count), DatabaseHelper.DOWNLOAD_ENTRY); - } } else if (o instanceof String) { String message = (String) o; if (!message.equals("I/O error occurred : Interrupted")) { @@ -590,7 +601,7 @@ public class DownloadIndexesThread { } } else { if (uiActivity != null) { - uiActivity.updateProgress(true, null); + uiActivity.updateProgress(true); } } } @@ -637,74 +648,43 @@ public class DownloadIndexesThread { try { List filesToReindex = new ArrayList(); boolean forceWifi = downloadFileHelper.isWifiConnected(); - Set currentDownloads = new HashSet(); - String breakDownloadMessage = null; - downloadCycle: - while (!entriesToDownload.isEmpty()) { - Iterator>> it = entriesToDownload.entrySet().iterator(); - IndexItem file = null; - List list = null; - while (it.hasNext()) { - Entry> n = it.next(); - if (!currentDownloads.containsAll(n.getValue())) { - file = n.getKey(); - list = n.getValue(); - break; + Set currentDownloads = new HashSet(); + try { + downloadCycle: while (!indexItemDownloading.isEmpty()) { + IndexItem item = indexItemDownloading.poll(); + currentDownloadingItem = item; + currentDownloadingItemProgress = 0; + if (currentDownloads.contains(item)) { + continue; } - } - if (file == null) { - break downloadCycle; - } - if (list != null) { + currentDownloads.add(item); boolean success = false; - for (DownloadEntry entry : list) { - if (currentDownloads.contains(entry)) { - continue; - } - currentDownloads.add(entry); - double asz = getAvailableSpace(); - // validate interrupted - if (downloadFileHelper.isInterruptDownloading()) { - break downloadCycle; - } - // validate enough space - if (asz != -1 && entry.sizeMB > asz) { - breakDownloadMessage = app.getString(R.string.download_files_not_enough_space, entry.sizeMB, asz); - break downloadCycle; - } - if (exceedsFreelimit(entry)) { - breakDownloadMessage = app.getString(R.string.free_version_message, DownloadActivity.MAXIMUM_AVAILABLE_FREE_DOWNLOADS - + ""); - break downloadCycle; - } - setTag(entry); - boolean result = downloadFile(entry, filesToReindex, forceWifi); - success = result || success; - if (result) { - if (DownloadActivityType.isCountedInDownloads(entry.item)) { - downloads.set(downloads.get() + 1); - } - if (entry.existingBackupFile != null) { - Algorithms.removeAllFiles(entry.existingBackupFile); - } -// trackEvent(entry); - publishProgress(entry); - } + if(!validateEnoughSpace(item)) { + break downloadCycle; } - if (success) { - entriesToDownload.remove(file); + if(!validateNotExceedsFreeLimit(item)) { + break downloadCycle; + } + setTag(item); + boolean result = downloadFile(item, filesToReindex, forceWifi); + success = result || success; + if (result) { + if (DownloadActivityType.isCountedInDownloads(item)) { + downloads.set(downloads.get() + 1); + } + File bf = item.getBackupFile(app); + if (bf.exists()) { + Algorithms.removeAllFiles(bf); + } + // trackEvent(entry); + publishProgress(item); } } - + } finally { + currentDownloadingItem = null; + currentDownloadingItemProgress = 0; } String warn = reindexFiles(filesToReindex); - if (breakDownloadMessage != null) { - if (warn != null) { - warn = breakDownloadMessage + "\n" + warn; - } else { - warn = breakDownloadMessage; - } - } updateLoadedFiles(); return warn; } catch (InterruptedException e) { @@ -714,10 +694,30 @@ public class DownloadIndexesThread { return null; } - private boolean exceedsFreelimit(DownloadEntry entry) { - return Version.isFreeVersion(app) && - DownloadActivityType.isCountedInDownloads(entry.item) && downloads.get() >= DownloadActivity.MAXIMUM_AVAILABLE_FREE_DOWNLOADS; + private boolean validateEnoughSpace(IndexItem item) { + double asz = getAvailableSpace(); + double cs =(item.contentSize / (1 << 20)); + // validate enough space + if (asz != -1 && cs > asz) { + String breakDownloadMessage = app.getString(R.string.download_files_not_enough_space, + cs, asz); + publishProgress(breakDownloadMessage); + return false; + } + return true; } + + private boolean validateNotExceedsFreeLimit(IndexItem item) { + boolean exceed = Version.isFreeVersion(app) && + DownloadActivityType.isCountedInDownloads(item) && downloads.get() >= DownloadActivity.MAXIMUM_AVAILABLE_FREE_DOWNLOADS; + if(exceed) { + String breakDownloadMessage = app.getString(R.string.free_version_message, + DownloadActivity.MAXIMUM_AVAILABLE_FREE_DOWNLOADS + ""); + publishProgress(breakDownloadMessage); + } + return !exceed; + } + private String reindexFiles(List filesToReindex) { boolean vectorMapsToReindex = false; @@ -760,9 +760,14 @@ public class DownloadIndexesThread { publishProgress(warning); } - public boolean downloadFile(DownloadEntry de, List filesToReindex, boolean forceWifi) + public boolean downloadFile(IndexItem item, List filesToReindex, boolean forceWifi) throws InterruptedException { + downloadFileHelper.setInterruptDownloading(false); + DownloadEntry de = item.createDownloadEntry(app); boolean res = false; + if(de == null) { + return res; + } if (de.isAsset) { try { if (ctx != null) { @@ -783,10 +788,11 @@ public class DownloadIndexesThread { } @Override - protected void updateProgress(boolean updateOnlyProgress, Object tag) { - if (uiActivity != null) { - uiActivity.updateProgress(updateOnlyProgress, tag); - } + protected void updateProgress(boolean updateOnlyProgress, IndexItem tag) { + currentDownloadingItemProgress = getProgressPercentage(); + uiActivity.updateProgress(true); } } + + } \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/plus/download/DownloadOsmandIndexesHelper.java b/OsmAnd/src/net/osmand/plus/download/DownloadOsmandIndexesHelper.java index be3f168e5d..ba1cee8752 100644 --- a/OsmAnd/src/net/osmand/plus/download/DownloadOsmandIndexesHelper.java +++ b/OsmAnd/src/net/osmand/plus/download/DownloadOsmandIndexesHelper.java @@ -169,9 +169,8 @@ public class DownloadOsmandIndexesHelper { } @Override - public List createDownloadEntry(OsmandApplication ctx, DownloadActivityType type, List res) { - res.add(new DownloadEntry(this, assetName, destFile, dateModified)); - return res; + public DownloadEntry createDownloadEntry(OsmandApplication ctx) { + return new DownloadEntry(assetName, destFile, dateModified); } public String getDestFile(){ diff --git a/OsmAnd/src/net/osmand/plus/download/IndexItem.java b/OsmAnd/src/net/osmand/plus/download/IndexItem.java index 0045a8ba2d..1a0d4a4efa 100644 --- a/OsmAnd/src/net/osmand/plus/download/IndexItem.java +++ b/OsmAnd/src/net/osmand/plus/download/IndexItem.java @@ -1,26 +1,22 @@ package net.osmand.plus.download; -import android.content.Context; -import android.os.Parcel; -import android.os.Parcelable; -import android.support.annotation.NonNull; +import java.io.File; +import java.io.IOException; +import java.util.Date; +import java.util.Map; import net.osmand.IndexConstants; import net.osmand.PlatformUtil; import net.osmand.map.OsmandRegions; import net.osmand.plus.OsmandApplication; import net.osmand.plus.R; -import net.osmand.plus.helpers.HasName; import org.apache.commons.logging.Log; -import java.io.File; -import java.io.IOException; -import java.util.Date; -import java.util.List; -import java.util.Map; +import android.content.Context; +import android.support.annotation.NonNull; -public class IndexItem implements Comparable, HasName, Parcelable { +public class IndexItem implements Comparable/*, Parcelable*/ { private static final Log log = PlatformUtil.getLog(IndexItem.class); String description; @@ -33,7 +29,6 @@ public class IndexItem implements Comparable, HasName, Parcelable { DownloadActivityType type; boolean extra; - private String initializedName; public IndexItem(String fileName, String description, long timestamp, String size, long contentSize, long containerSize, DownloadActivityType tp) { @@ -74,14 +69,17 @@ public class IndexItem implements Comparable, HasName, Parcelable { public long getContentSize() { return contentSize; } + + public double getContentSizeMB() { + return ((double)contentSize) / (1 << 20); + } public String getSizeDescription(Context ctx) { return size + " MB"; } - public List createDownloadEntry(OsmandApplication ctx, DownloadActivityType type, - List downloadEntries) { + public DownloadEntry createDownloadEntry(OsmandApplication ctx) { String fileName = this.fileName; File parent = type.getDownloadFolder(ctx, this); boolean preventMediaIndexing = type.preventMediaIndexing(ctx, this); @@ -97,11 +95,12 @@ public class IndexItem implements Comparable, HasName, Parcelable { } } } - final DownloadEntry entry; + DownloadEntry entry; if (parent == null || !parent.exists()) { ctx.showToastMessage(R.string.sd_dir_not_accessible); + entry = null; } else { - entry = new DownloadEntry(this); + entry = new DownloadEntry(); entry.type = type; entry.baseName = getBasename(); entry.urlToDownload = entry.type.getBaseUrl(ctx, fileName) + entry.type.getUrlSuffix(ctx); @@ -109,15 +108,26 @@ public class IndexItem implements Comparable, HasName, Parcelable { entry.unzipFolder = type.isZipFolder(ctx, this); entry.dateModified = timestamp; entry.sizeMB = contentSize / (1024f*1024f); - String extension = type.getUnzipExtension(ctx, this); - entry.targetFile = new File(parent, entry.baseName + extension); - File backup = new File(ctx.getAppPath(IndexConstants.BACKUP_INDEX_DIR), entry.targetFile.getName()); - if (backup.exists()) { - entry.existingBackupFile = backup; - } - downloadEntries.add(entry); + entry.targetFile = getTargetFile(ctx); } - return downloadEntries; + return entry; + } + + public String getTargetFileName() { + return type.getTargetFileName(this); + } + + public String getBasename() { + return type.getBasename(this); + } + + private File getTargetFile(OsmandApplication ctx) { + return new File(type.getDownloadFolder(ctx, this), getBasename() + type.getUnzipExtension(ctx, this)); + } + + public File getBackupFile(OsmandApplication ctx) { + File backup = new File(ctx.getAppPath(IndexConstants.BACKUP_INDEX_DIR), getTargetFile(ctx).getName()); + return backup; } @Override @@ -132,9 +142,7 @@ public class IndexItem implements Comparable, HasName, Parcelable { return listAlreadyDownloaded.containsKey(getTargetFileName()); } - public String getBasename() { - return type.getBasename(this); - } + public String getVisibleName(Context ctx, OsmandRegions osmandRegions) { return type.getVisibleName(this, ctx, osmandRegions, true); @@ -148,22 +156,12 @@ public class IndexItem implements Comparable, HasName, Parcelable { return type.getVisibleDescription(this, clctx); } - public String getTargetFileName() { - return type.getTargetFileName(this); - } + public String getDate(java.text.DateFormat format) { return format.format(new Date(timestamp)); } - @Override - public String getName() { - return initializedName + " must be fixed"; - } - - public void setName(String initializedName) { - this.initializedName = initializedName; - } // @Override // public String toString() { @@ -180,45 +178,45 @@ public class IndexItem implements Comparable, HasName, Parcelable { // '}'; // } - @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]; - } - }; +// @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/items/ActiveDownloadsDialogFragment.java b/OsmAnd/src/net/osmand/plus/download/items/ActiveDownloadsDialogFragment.java index 10210bdbea..1eafae97c7 100644 --- a/OsmAnd/src/net/osmand/plus/download/items/ActiveDownloadsDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/download/items/ActiveDownloadsDialogFragment.java @@ -7,7 +7,7 @@ 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.DownloadEntry; +import net.osmand.plus.download.IndexItem; import android.app.AlertDialog; import android.app.Dialog; import android.graphics.drawable.Drawable; @@ -21,14 +21,14 @@ import android.widget.ArrayAdapter; public class ActiveDownloadsDialogFragment extends DialogFragment { - private DownloadEntryAdapter adapter; + private IndexItemAdapter adapter; @NonNull @Override 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 DownloadEntryAdapter(getDownloadActivity()); + adapter = new IndexItemAdapter(getDownloadActivity()); builder.setAdapter(adapter, null); getDownloadActivity().setActiveDownloads(this); return builder.create(); @@ -48,7 +48,7 @@ public class ActiveDownloadsDialogFragment extends DialogFragment { return (DownloadActivity) getActivity(); } - public static class DownloadEntryAdapter extends ArrayAdapter { + public static class IndexItemAdapter extends ArrayAdapter { private final Drawable deleteDrawable; private final DownloadActivity context; private int itemInProgressPosition = -1; @@ -56,8 +56,8 @@ public class ActiveDownloadsDialogFragment extends DialogFragment { private final Set downloadedItems = new HashSet<>(); private boolean isFinished; - public DownloadEntryAdapter(DownloadActivity context) { - super(context, R.layout.two_line_with_images_list_item, new ArrayList()); + public IndexItemAdapter(DownloadActivity context) { + super(context, R.layout.two_line_with_images_list_item, new ArrayList()); this.context = context; deleteDrawable = context.getMyApplication().getIconsCache() .getPaintedContentIcon(R.drawable.ic_action_remove_dark, @@ -67,7 +67,7 @@ public class ActiveDownloadsDialogFragment extends DialogFragment { public void updateData() { clear(); - addAll(BaseDownloadActivity.downloadListIndexThread.flattenDownloadEntries()); + addAll(BaseDownloadActivity.downloadListIndexThread.getCurrentDownloadingItems()); } @Override @@ -90,10 +90,10 @@ public class ActiveDownloadsDialogFragment extends DialogFragment { private static class DownloadEntryViewHolder extends TwoLineWithImagesViewHolder { private final Drawable deleteDrawable; - private final DownloadEntryAdapter adapter; + private final IndexItemAdapter adapter; private DownloadEntryViewHolder(View convertView, final DownloadActivity context, - Drawable deleteDrawable, DownloadEntryAdapter adapter) { + Drawable deleteDrawable, IndexItemAdapter adapter) { super(convertView, context); this.deleteDrawable = deleteDrawable; this.adapter = adapter; @@ -101,9 +101,9 @@ public class ActiveDownloadsDialogFragment extends DialogFragment { rightImageButton.setImageDrawable(deleteDrawable); } - public void bindDownloadEntry(final DownloadEntry downloadEntry, final int progress, + public void bindDownloadEntry(final IndexItem item, final int progress, boolean isDownloaded) { - nameTextView.setText(downloadEntry.item.getVisibleName(context, + nameTextView.setText(item.getVisibleName(context, context.getMyApplication().getRegions())); rightImageButton.setVisibility(View.VISIBLE); @@ -111,23 +111,23 @@ public class ActiveDownloadsDialogFragment extends DialogFragment { boolean isIndeterminate = true; if (progress != -1) { isIndeterminate = false; - double downloaded = downloadEntry.sizeMB * progress / 100; + double downloaded = item.getContentSizeMB() * progress / 100; descrTextView.setText(context.getString(R.string.value_downloaded_from_max, downloaded, - downloadEntry.sizeMB)); + item.getContentSizeMB())); } else if (isDownloaded) { isIndeterminate = false; localProgress = progressBar.getMax(); descrTextView.setText(context.getString(R.string.file_size_in_mb, - downloadEntry.sizeMB)); + item.getContentSizeMB())); rightImageButton.setVisibility(View.GONE); } else { descrTextView.setText(context.getString(R.string.file_size_in_mb, - downloadEntry.sizeMB)); + item.getContentSizeMB())); } rightImageButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - context.cancelDownload(downloadEntry.item); + context.cancelDownload(item); adapter.updateData(); } }); diff --git a/OsmAnd/src/net/osmand/plus/download/items/RegionItemsFragment.java b/OsmAnd/src/net/osmand/plus/download/items/RegionItemsFragment.java index d6873e2345..91a904d0de 100644 --- a/OsmAnd/src/net/osmand/plus/download/items/RegionItemsFragment.java +++ b/OsmAnd/src/net/osmand/plus/download/items/RegionItemsFragment.java @@ -291,11 +291,12 @@ public class RegionItemsFragment extends OsmandExpandableListFragment { 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 = getArguments().getParcelable(INDEX_ITEM); + 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); @@ -304,7 +305,9 @@ public class RegionItemsFragment extends OsmandExpandableListFragment { new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { - ((DownloadActivity) getActivity()).startDownload(indexItem); + if(indexItem != null) { + ((DownloadActivity) getActivity()).startDownload(indexItem); + } } }); return builder.create(); @@ -314,7 +317,6 @@ public class RegionItemsFragment extends OsmandExpandableListFragment { ConfirmDownloadUnneededMapDialogFragment fragment = new ConfirmDownloadUnneededMapDialogFragment(); Bundle args = new Bundle(); - args.putParcelable(INDEX_ITEM, indexItem); fragment.setArguments(args); return fragment; }