Continue refactoring

This commit is contained in:
Victor Shcherb 2015-10-18 22:14:09 +02:00
parent 266e62c459
commit 71eef922ae
13 changed files with 750 additions and 1045 deletions

View file

@ -39,10 +39,7 @@ public class WorldRegion {
private List<WorldRegion> subregions;
private List<WorldRegion> 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
}
}

View file

@ -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;

View file

@ -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<WeakReference<Fragment>> 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<IndexItem> 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();
}

View file

@ -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<String, String> 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<Fragment> 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<Fragment> 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<Fragment> 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<Fragment> 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<Fragment> 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<String, String> getIndexFileNames() {
return downloadListIndexThread != null ? downloadListIndexThread.getIndexFileNames() : null;
}
public List<IndexItem> getIndexFiles() {
return downloadListIndexThread != null ? downloadListIndexThread.getCachedIndexFiles() : null;
}
public void registerFreeVersionBanner(View view) {
visibleBanner = new BannerAndDownloadFreeVersion(view, this);
updateProgress(true);
updateBannerInProgress();
}

View file

@ -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<String, DownloadActivityType> 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<DownloadActivityType> CREATOR = new Parcelable.Creator<DownloadActivityType>() {
public DownloadActivityType createFromParcel(Parcel source) {
return new DownloadActivityType(source);
}
public DownloadActivityType[] newArray(int size) {
return new DownloadActivityType[size];
}
};
}

View file

@ -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<WorldRegion, Map<String, IndexItem>> resourcesByRegions = new HashMap<>();
List<IndexItem> voiceRecItems = new LinkedList<>();
List<IndexItem> voiceTTSItems = new LinkedList<>();
IndexFileList indexFiles = null;
}
private Map<String, String> indexFileNames = new LinkedHashMap<>();
private Map<String, String> indexActivatedFileNames = new LinkedHashMap<>();
private List<IndexItem> 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<IndexItem> filtered = getCachedIndexFiles();
if (filtered != null) {
updateDownloadList();
uiActivity.newDownloadIndexes();
}
}
@ -161,164 +130,7 @@ public class DownloadIndexesThread {
}
return i;
}
// FIXME
public List<IndexItem> getCachedIndexFiles() {
return indexes.indexFiles != null ? indexes.indexFiles.getIndexFiles() : null;
}
// FIXME
public Map<String, String> getIndexFileNames() {
return indexFileNames;
}
// FIXME
public Map<String, String> getIndexActivatedFileNames() {
return indexActivatedFileNames;
}
public void updateLoadedFiles() {
Map<String, String> indexActivatedFileNames = app.getResourceManager().getIndexFileNames();
listWithAlternatives(dateFormat, app.getAppPath(""), IndexConstants.EXTRA_EXT,
indexActivatedFileNames);
Map<String, String> 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<String, String> getDownloadedIndexFileNames() {
return indexFileNames;
}
public boolean isDownloadedFromInternet() {
return indexes.indexFiles != null && indexes.indexFiles.isDownloadedFromInternet();
}
public List<IndexItem> getItemsToUpdate() {
return itemsToUpdate;
}
public Map<WorldRegion, Map<String, IndexItem>> getResourcesByRegions() {
return indexes.resourcesByRegions;
}
public List<IndexItem> getVoiceRecItems() {
return indexes.voiceRecItems;
}
public List<IndexItem> getVoiceTTSItems() {
return indexes.voiceTTSItems;
}
private boolean prepareData(List<IndexItem> resources,
Map<WorldRegion, Map<String, IndexItem>> resourcesByRegions,
List<IndexItem> voiceRecItems, List<IndexItem> voiceTTSItems) {
List<IndexItem> 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<IndexItem>() {
@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<IndexItem>() {
@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<IndexItem> resourcesInRepository, Map<WorldRegion,
Map<String, IndexItem>> resourcesByRegions,
List<IndexItem> voiceRecItems, List<IndexItem> voiceTTSItems,
boolean processVoiceFiles, WorldRegion region) {
String downloadsIdPrefix = region.getDownloadsIdPrefix();
Map<String, IndexItem> regionResources = new HashMap<>();
Set<DownloadActivityType> typesSet = new TreeSet<>(new Comparator<DownloadActivityType>() {
@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 <P> void execute(BasicProgressAsyncTask<?, P, ?, ?> 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<IndexItem> 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<String, String> listWithAlternatives(final java.text.DateFormat dateFormat, File file, final String ext,
final Map<String, String> 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 <P> void execute(BasicProgressAsyncTask<?, P, ?, ?> 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<Void, Void, Void, DownloadIndexes> {
private class ReloadIndexesTask extends BasicProgressAsyncTask<Void, Void, Void, DownloadResources> {
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<IndexItem> stillUpdate = new ArrayList<IndexItem>();
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<IndexItem, IndexItem, Object, String> implements DownloadFileShowWarning {
private class DownloadIndexesAsyncTask extends BasicProgressAsyncTask<IndexItem, IndexItem, Object, String> implements DownloadFileShowWarning {
private OsmandPreference<Integer> 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();
}
}

View file

@ -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<IndexItem> indexFiles = new ArrayList<IndexItem>();
private String mapversion;
private Comparator<IndexItem> comparator = new Comparator<IndexItem>(){
@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<IndexItem> 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;
}

View file

@ -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<IndexItem> individualResources;
private final List<DownloadResourceGroup> 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<IndexItem>();
this.groups = null;
} else {
this.individualResources = null;
this.groups = new ArrayList<DownloadResourceGroup>();
}
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;
}
}

View file

@ -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<String, String> indexFileNames = new LinkedHashMap<>();
private Map<String, String> indexActivatedFileNames = new LinkedHashMap<>();
private List<IndexItem> 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<String, String> indexActivatedFileNames = app.getResourceManager().getIndexFileNames();
listWithAlternatives(dateFormat, app.getAppPath(""), IndexConstants.EXTRA_EXT, indexActivatedFileNames);
Map<String, String> 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<IndexItem> stillUpdate = new ArrayList<IndexItem>();
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<String, String> listWithAlternatives(final java.text.DateFormat dateFormat, File file,
final String ext, final Map<String, String> 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<IndexItem> 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<IndexItem> resourcesInRepository, DownloadResources di,
boolean processVoiceFiles, WorldRegion region) {
String downloadsIdPrefix = region.getDownloadsIdPrefix();
Map<String, IndexItem> regionResources = new HashMap<>();
Set<DownloadActivityType> typesSet = new TreeSet<>(new Comparator<DownloadActivityType>() {
@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<IndexItem> 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<IndexItem>() {
@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<IndexItem>() {
@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<ResourceItem> regionMapItems;
private List<Object> allResourceItems;
private List<WorldRegion> allSubregionItems;
private OsmandApplication app;
private WorldRegion region;
private boolean srtmDisabled;
private boolean hasSrtm;
private boolean hasHillshade;
public List<ResourceItem> getRegionMapItems() {
return regionMapItems;
}
public List<Object> getAllResourceItems() {
return allResourceItems;
}
public List<WorldRegion> getRegionsFromAllItems() {
List<WorldRegion> 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<IndexItem> 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<String, IndexItem> regionResources = downloadIndexes.resourcesByRegions.get(region);
if (regionResources == null) {
return;
}
List<ResourceItem> regionMapArray = new LinkedList<>();
List<Object> 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
}
}
}

View file

@ -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<IndexItem> indexFiles = new ArrayList<IndexItem>();
private String mapversion;
private Comparator<IndexItem> comparator = new Comparator<IndexItem>(){
@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<IndexItem> 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;
}
}

View file

@ -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<IndexItemCategory> {
public final String name;
public final List<IndexItem> 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<IndexItemCategory> categorizeIndexItems(final OsmandApplication ctx,
Collection<IndexItem> indexItems) {
boolean skipWiki = Version.isFreeVersion(ctx);
final Map<String, IndexItemCategory> 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<IndexItemCategory> 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<IndexItem>() {
@Override
public int compare(IndexItem lhs, IndexItem rhs) {
return collator.compare(lhs.getVisibleName(ctx, osmandRegions),
rhs.getVisibleName(ctx, osmandRegions));
}
});
}
Collections.sort(r);
return r;
}
}

View file

@ -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<WorldRegion, Map<String, IndexItem>> resourcesByRegions;
private List<IndexItem> voiceRecItems;
private List<IndexItem> 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<Object> {
@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<ResourceItem> regionMapItems;
private List<Object> allResourceItems;
private List<WorldRegion> allSubregionItems;
private OsmandApplication app;
private WorldRegion region;
private boolean srtmDisabled;
private boolean hasSrtm;
private boolean hasHillshade;
public List<ResourceItem> getRegionMapItems() {
return regionMapItems;
}
public List<Object> getAllResourceItems() {
return allResourceItems;
}
public List<WorldRegion> getRegionsFromAllItems() {
List<WorldRegion> 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<IndexItem> 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<WorldRegion, Map<String, IndexItem>> resourcesByRegions,
List<IndexItem> voiceRecItems, List<IndexItem> 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<String, IndexItem> regionResources = resourcesByRegions.get(region);
if (regionResources == null) {
return;
}
List<ResourceItem> regionMapArray = new LinkedList<>();
List<Object> 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());
}
}

View file

@ -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() {
}