Use timestamp instead of date

This commit is contained in:
vshcherb 2014-04-26 14:38:27 +02:00
parent 381492698a
commit 740c829fab
9 changed files with 69 additions and 78 deletions

View file

@ -25,6 +25,17 @@ public class Algorithms {
return s == null || s.length() == 0; return s == null || s.length() == 0;
} }
public static long parseLongSilently(String input, long def) {
if(input != null && input.length() > 0) {
try {
return Long.parseLong(input);
} catch (NumberFormatException e) {
return def;
}
}
return def;
}
public static int findFirstNumberEndIndex(String value) { public static int findFirstNumberEndIndex(String value) {
int i = 0; int i = 0;
boolean valid = false; boolean valid = false;

View file

@ -11,14 +11,16 @@ import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import org.xmlpull.v1.XmlPullParser;
import net.osmand.AndroidUtils; import net.osmand.AndroidUtils;
import net.osmand.IndexConstants; import net.osmand.IndexConstants;
import net.osmand.map.OsmandRegions; import net.osmand.map.OsmandRegions;
import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R; import net.osmand.plus.R;
import net.osmand.plus.Version; import net.osmand.plus.Version;
import net.osmand.util.Algorithms;
import org.xmlpull.v1.XmlPullParser;
import android.content.Context; import android.content.Context;
public class DownloadActivityType { public class DownloadActivityType {
@ -28,8 +30,8 @@ public class DownloadActivityType {
public static final DownloadActivityType NORMAL_FILE = new DownloadActivityType(R.string.download_regular_maps, "map"); public static final DownloadActivityType NORMAL_FILE = new DownloadActivityType(R.string.download_regular_maps, "map");
public static final DownloadActivityType VOICE_FILE = new DownloadActivityType(R.string.voice, "voice"); public static final DownloadActivityType VOICE_FILE = new DownloadActivityType(R.string.voice, "voice");
public static final DownloadActivityType ROADS_FILE = new DownloadActivityType(R.string.download_roads_only_maps, "road_map"); public static final DownloadActivityType ROADS_FILE = new DownloadActivityType(R.string.download_roads_only_maps, "road_map");
public static final DownloadActivityType HILLSHADE_FILE = new DownloadActivityType(R.string.download_srtm_maps, "srtm_map"); public static final DownloadActivityType SRTM_COUNTRY_FILE = new DownloadActivityType(R.string.download_srtm_maps, "srtm_map");
public static final DownloadActivityType SRTM_COUNTRY_FILE = new DownloadActivityType(R.string.download_hillshade_maps, "hillshade"); public static final DownloadActivityType HILLSHADE_FILE = new DownloadActivityType(R.string.download_hillshade_maps, "hillshade");
private int resource; private int resource;
private String[] tags; private String[] tags;
@ -151,12 +153,14 @@ public class DownloadActivityType {
return null; return null;
} }
String size = parser.getAttributeValue(null, "size"); //$NON-NLS-1$ String size = parser.getAttributeValue(null, "size"); //$NON-NLS-1$
String date = parser.getAttributeValue(null, "date"); //$NON-NLS-1$
String description = parser.getAttributeValue(null, "description"); //$NON-NLS-1$ String description = parser.getAttributeValue(null, "description"); //$NON-NLS-1$
String parts = parser.getAttributeValue(null, "parts"); //$NON-NLS-1$ long containerSize = Algorithms.parseLongSilently(
date = reparseDate(ctx, date); parser.getAttributeValue(null, "containerSize"), 0);
IndexItem it = new IndexItem(name, description, date, size, parts, this); long contentSize = Algorithms.parseLongSilently(
parser.getAttributeValue(null, "contentSize"), 0);
long timestamp = Algorithms.parseLongSilently(
parser.getAttributeValue(null, "timestamp"), 0);
IndexItem it = new IndexItem(name, description, timestamp, size, contentSize, containerSize, this);
return it; return it;
} }

View file

@ -2,10 +2,8 @@ package net.osmand.plus.download;
import java.io.File; import java.io.File;
import net.osmand.IndexConstants;
public class DownloadEntry { public class DownloadEntry {
public Long dateModified; public long dateModified;
public double sizeMB; public double sizeMB;
public File targetFile; public File targetFile;
@ -16,7 +14,6 @@ public class DownloadEntry {
public String baseName; public String baseName;
public String urlToDownload; public String urlToDownload;
public int parts;
public File existingBackupFile; public File existingBackupFile;
public boolean isAsset; public boolean isAsset;
public String assetName; public String assetName;

View file

@ -201,15 +201,8 @@ public class DownloadFileHelper {
List<File> toReIndex, DownloadFileShowWarning showWarningCallback, boolean forceWifi) throws InterruptedException { List<File> toReIndex, DownloadFileShowWarning showWarningCallback, boolean forceWifi) throws InterruptedException {
try { try {
final List<InputStream> downloadInputStreams = new ArrayList<InputStream>(); final List<InputStream> downloadInputStreams = new ArrayList<InputStream>();
if (de.parts == 1) {
URL url = new URL(de.urlToDownload); //$NON-NLS-1$ URL url = new URL(de.urlToDownload); //$NON-NLS-1$
downloadInputStreams.add(getInputStreamToDownload(url, forceWifi)); downloadInputStreams.add(getInputStreamToDownload(url, forceWifi));
} else {
for (int i = 1; i <= de.parts; i++) {
URL url = new URL(de.urlToDownload + "-" + i); //$NON-NLS-1$
downloadInputStreams.add(getInputStreamToDownload(url, forceWifi));
}
}
de.fileToDownload = de.targetFile; de.fileToDownload = de.targetFile;
if(!de.unzipFolder) { if(!de.unzipFolder) {
de.fileToDownload = new File(de.targetFile.getParentFile(), de.targetFile.getName() +".download"); de.fileToDownload = new File(de.targetFile.getParentFile(), de.targetFile.getName() +".download");
@ -298,11 +291,8 @@ public class DownloadFileHelper {
progress.remaining(remaining / 1024); progress.remaining(remaining / 1024);
} }
out.close(); out.close();
if (de.dateModified != null) {
targetFile.setLastModified(de.dateModified); targetFile.setLastModified(de.dateModified);
} }
}
public void setInterruptDownloading(boolean interruptDownloading) { public void setInterruptDownloading(boolean interruptDownloading) {

View file

@ -14,6 +14,7 @@ import android.content.Context;
import android.content.res.TypedArray; import android.content.res.TypedArray;
import android.graphics.Color; import android.graphics.Color;
import android.graphics.Typeface; import android.graphics.Typeface;
import android.text.format.DateFormat;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
@ -36,6 +37,7 @@ public class DownloadIndexAdapter extends OsmandBaseExpandableListAdapter implem
private int defaultColor; private int defaultColor;
private int updateColor; private int updateColor;
private OsmandRegions osmandRegions; private OsmandRegions osmandRegions;
private java.text.DateFormat format;
public DownloadIndexAdapter(DownloadIndexActivity downloadActivity, List<IndexItem> indexFiles) { public DownloadIndexAdapter(DownloadIndexActivity downloadActivity, List<IndexItem> indexFiles) {
this.downloadActivity = downloadActivity; this.downloadActivity = downloadActivity;
@ -45,6 +47,7 @@ public class DownloadIndexAdapter extends OsmandBaseExpandableListAdapter implem
list.clear(); list.clear();
list.addAll(cats); list.addAll(cats);
} }
format = DateFormat.getDateFormat(downloadActivity);
okColor = downloadActivity.getResources().getColor(R.color.color_ok); okColor = downloadActivity.getResources().getColor(R.color.color_ok);
TypedArray ta = downloadActivity.getTheme().obtainStyledAttributes(new int[]{android.R.attr.textColorPrimary}); TypedArray ta = downloadActivity.getTheme().obtainStyledAttributes(new int[]{android.R.attr.textColorPrimary});
defaultColor = ta.getColor(0, downloadActivity.getResources().getColor(R.color.color_unknown)); defaultColor = ta.getColor(0, downloadActivity.getResources().getColor(R.color.color_unknown));
@ -239,7 +242,7 @@ public class DownloadIndexAdapter extends OsmandBaseExpandableListAdapter implem
OsmandApplication clctx = downloadActivity.getMyApplication(); OsmandApplication clctx = downloadActivity.getMyApplication();
String eName = e.getVisibleDescription(clctx) + "\n" + e.getVisibleName(clctx, osmandRegions); String eName = e.getVisibleDescription(clctx) + "\n" + e.getVisibleName(clctx, osmandRegions);
item.setText(eName.trim()); //$NON-NLS-1$ item.setText(eName.trim()); //$NON-NLS-1$
String d = e.getDate() + "\n" + e.getSizeDescription(clctx); String d = e.getDate(format) + "\n" + e.getSizeDescription(clctx);
description.setText(d.trim()); description.setText(d.trim());
CheckBox ch = (CheckBox) row.findViewById(R.id.check_download_item); CheckBox ch = (CheckBox) row.findViewById(R.id.check_download_item);
@ -271,14 +274,14 @@ public class DownloadIndexAdapter extends OsmandBaseExpandableListAdapter implem
} else { } else {
item.setTypeface(Typeface.DEFAULT, Typeface.ITALIC); item.setTypeface(Typeface.DEFAULT, Typeface.ITALIC);
} }
} else if (e.getDate() != null) { } else if (e.getDate(format) != null) {
String sfName = e.getTargetFileName(); String sfName = e.getTargetFileName();
if (e.getDate().equals(indexActivatedFileNames.get(sfName))) { if (e.getDate(format).equals(indexActivatedFileNames.get(sfName))) {
item.setText(item.getText() + "\n" + downloadActivity.getResources().getString(R.string.local_index_installed) + " : " item.setText(item.getText() + "\n" + downloadActivity.getResources().getString(R.string.local_index_installed) + " : "
+ indexActivatedFileNames.get(sfName)); + indexActivatedFileNames.get(sfName));
item.setTextColor(okColor); // GREEN item.setTextColor(okColor); // GREEN
item.setTypeface(Typeface.DEFAULT, Typeface.NORMAL); item.setTypeface(Typeface.DEFAULT, Typeface.NORMAL);
} else if (e.getDate().equals(indexFileNames.get(sfName))) { } else if (e.getDate(format).equals(indexFileNames.get(sfName))) {
item.setText(item.getText() + "\n" + downloadActivity.getResources().getString(R.string.local_index_installed) + " : " item.setText(item.getText() + "\n" + downloadActivity.getResources().getString(R.string.local_index_installed) + " : "
+ indexFileNames.get(sfName)); + indexFileNames.get(sfName));
item.setTextColor(okColor); item.setTextColor(okColor);

View file

@ -43,6 +43,7 @@ import android.os.AsyncTask;
import android.os.AsyncTask.Status; import android.os.AsyncTask.Status;
import android.os.Build; import android.os.Build;
import android.os.StatFs; import android.os.StatFs;
import android.text.format.DateFormat;
import android.view.View; import android.view.View;
import android.widget.Toast; import android.widget.Toast;
@ -58,11 +59,14 @@ public class DownloadIndexesThread {
private List<BasicProgressAsyncTask<?, ?, ?> > currentRunningTask = Collections.synchronizedList(new ArrayList<BasicProgressAsyncTask<?, ?, ?>>()); private List<BasicProgressAsyncTask<?, ?, ?> > currentRunningTask = Collections.synchronizedList(new ArrayList<BasicProgressAsyncTask<?, ?, ?>>());
private Map<String, String> indexFileNames = new LinkedHashMap<String, String>(); private Map<String, String> indexFileNames = new LinkedHashMap<String, String>();
private Map<String, String> indexActivatedFileNames = new LinkedHashMap<String, String>(); private Map<String, String> indexActivatedFileNames = new LinkedHashMap<String, String>();
private java.text.DateFormat dateFormat;
public DownloadIndexesThread(Context ctx) { public DownloadIndexesThread(Context ctx) {
this.ctx = ctx; this.ctx = ctx;
app = (OsmandApplication) ctx.getApplicationContext(); app = (OsmandApplication) ctx.getApplicationContext();
downloadFileHelper = new DownloadFileHelper(app); downloadFileHelper = new DownloadFileHelper(app);
dateFormat = DateFormat.getDateFormat(app);
} }
public void setUiActivity(DownloadIndexActivity uiActivity) { public void setUiActivity(DownloadIndexActivity uiActivity) {
@ -369,7 +373,7 @@ public class DownloadIndexesThread {
IndexItem basemap = indexFiles.getBasemap(); IndexItem basemap = indexFiles.getBasemap();
if (basemap != null ) { if (basemap != null ) {
String dt = uiActivity.getMyApplication().getResourceManager().getIndexFileNames().get(basemap.getTargetFileName()); String dt = uiActivity.getMyApplication().getResourceManager().getIndexFileNames().get(basemap.getTargetFileName());
if (!basemapExists || !Algorithms.objectEquals(dt, basemap.getDate())) { if (!basemapExists || !Algorithms.objectEquals(dt, basemap.getDate(dateFormat))) {
List<DownloadEntry> downloadEntry = basemap List<DownloadEntry> downloadEntry = basemap
.createDownloadEntry(uiActivity.getMyApplication(), uiActivity.getType(), .createDownloadEntry(uiActivity.getMyApplication(), uiActivity.getType(),
new ArrayList<DownloadEntry>()); new ArrayList<DownloadEntry>());

View file

@ -88,23 +88,19 @@ public class DownloadOsmandIndexesHelper {
if (target.endsWith("-tts/_ttsconfig.p") && target.startsWith("voice/")) { if (target.endsWith("-tts/_ttsconfig.p") && target.startsWith("voice/")) {
String voice = target.substring("voice/".length(), target.length() - "/_ttsconfig.p".length()); String voice = target.substring("voice/".length(), target.length() - "/_ttsconfig.p".length());
File destFile = new File(voicePath, voice + File.separatorChar + "_ttsconfig.p"); File destFile = new File(voicePath, voice + File.separatorChar + "_ttsconfig.p");
result.add(new AssetIndexItem(voice +ext, "voice", date, dateModified, "0.1", "", key, destFile.getPath(), result.add(new AssetIndexItem(voice +ext, "voice", date, dateModified,
DownloadActivityType.VOICE_FILE)); "0.1", 1024*100, key, destFile.getPath(), DownloadActivityType.VOICE_FILE));
} else if (target.endsWith("/_config.p") && target.startsWith("voice/")) { } else if (target.endsWith("/_config.p") && target.startsWith("voice/")) {
String voice = target.substring("voice/".length(), target.length() - "/_config.p".length()); String voice = target.substring("voice/".length(), target.length() - "/_config.p".length());
IndexItem item = result.getIndexFilesByName(key); IndexItem item = result.getIndexFilesByName(key);
if (item != null) { if (item != null) {
File destFile = new File(voicePath, voice + File.separatorChar + "_config.p"); File destFile = new File(voicePath, voice + File.separatorChar + "_config.p");
try { // always use bundled config
Date d = DateFormat.getDateFormat((Context) settings.getContext()).parse(item.getDate()); // if (item.getTimestamp() > dateModified) {
if (d.getTime() > dateModified) { // continue;
continue; // }
} item.timestamp = dateModified;
} catch (Exception es) { item.attachedItem = new AssetIndexItem(voice +extvoice, "voice", date, dateModified, "0.1", 1024*100, key, destFile.getPath(),
log.error("Parse exception", es);
}
item.date = date;
item.attachedItem = new AssetIndexItem(voice +extvoice, "voice", date, dateModified, "0.1", "", key, destFile.getPath(),
DownloadActivityType.VOICE_FILE); DownloadActivityType.VOICE_FILE);
} }
} }
@ -133,7 +129,6 @@ public class DownloadOsmandIndexesHelper {
if (next == XmlPullParser.START_TAG) { if (next == XmlPullParser.START_TAG) {
DownloadActivityType tp = DownloadActivityType.getIndexType(parser.getAttributeValue(null, "type")); DownloadActivityType tp = DownloadActivityType.getIndexType(parser.getAttributeValue(null, "type"));
if (tp != null) { if (tp != null) {
IndexItem it = tp.parseIndexItem(ctx, parser); IndexItem it = tp.parseIndexItem(ctx, parser);
if(it != null) { if(it != null) {
result.add(it); result.add(it);
@ -171,8 +166,8 @@ public class DownloadOsmandIndexesHelper {
private final long dateModified; private final long dateModified;
public AssetIndexItem(String fileName, String description, String date, public AssetIndexItem(String fileName, String description, String date,
long dateModified, String size, String parts, String assetName, String destFile, DownloadActivityType type) { long dateModified, String size, long sizeL, String assetName, String destFile, DownloadActivityType type) {
super(fileName, description, date, size, parts, type); super(fileName, description, dateModified, size, sizeL, sizeL, type);
this.dateModified = dateModified; this.dateModified = dateModified;
this.assetName = assetName; this.assetName = assetName;
this.destFile = destFile; this.destFile = destFile;

View file

@ -2,12 +2,10 @@ package net.osmand.plus.download;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.TimeZone;
import net.osmand.IndexConstants; import net.osmand.IndexConstants;
import net.osmand.PlatformUtil; import net.osmand.PlatformUtil;
@ -24,19 +22,23 @@ public class IndexItem implements Comparable<IndexItem> {
private static final Log log = PlatformUtil.getLog(IndexItem.class); private static final Log log = PlatformUtil.getLog(IndexItem.class);
String description; String description;
String date;
String parts;
String fileName; String fileName;
String size; String size;
long timestamp;
long contentSize;
long containerSize;
IndexItem attachedItem; IndexItem attachedItem;
DownloadActivityType type; DownloadActivityType type;
public IndexItem(String fileName, String description, String date, String size, String parts, DownloadActivityType tp) {
public IndexItem(String fileName, String description, long timestamp, String size, long contentSize,
long containerSize, DownloadActivityType tp) {
this.fileName = fileName; this.fileName = fileName;
this.description = description; this.description = description;
this.date = date; this.timestamp = timestamp;
this.size = size; this.size = size;
this.parts = parts; this.contentSize = contentSize;
this.containerSize = containerSize;
this.type = tp; this.type = tp;
} }
@ -52,17 +54,15 @@ public class IndexItem implements Comparable<IndexItem> {
return description; return description;
} }
public String getDate() { public long getTimestamp() {
return date; return timestamp;
} }
public String getSizeDescription(Context ctx) { public String getSizeDescription(Context ctx) {
return size + " MB"; return size + " MB";
} }
public String getSize() {
return size;
}
public List<DownloadEntry> createDownloadEntry(OsmandApplication ctx, DownloadActivityType type, public List<DownloadEntry> createDownloadEntry(OsmandApplication ctx, DownloadActivityType type,
List<DownloadEntry> downloadEntries) { List<DownloadEntry> downloadEntries) {
@ -91,23 +91,8 @@ public class IndexItem implements Comparable<IndexItem> {
entry.urlToDownload = entry.type.getBaseUrl(ctx, fileName) + entry.type.getUrlSuffix(ctx); entry.urlToDownload = entry.type.getBaseUrl(ctx, fileName) + entry.type.getUrlSuffix(ctx);
entry.zipStream = type.isZipStream(ctx, this); entry.zipStream = type.isZipStream(ctx, this);
entry.unzipFolder = type.isZipFolder(ctx, this); entry.unzipFolder = type.isZipFolder(ctx, this);
try { entry.dateModified = timestamp;
final java.text.DateFormat format = DateFormat.getDateFormat((Context) ctx); entry.sizeMB = contentSize / (1024f*1024f);
format.setTimeZone(TimeZone.getTimeZone("GMT+01:00"));
Date d = format.parse(date);
entry.dateModified = d.getTime();
} catch (ParseException e1) {
log.error("ParseException", e1);
}
try {
entry.sizeMB = Double.parseDouble(size);
} catch (NumberFormatException e1) {
log.error("ParseException", e1);
}
entry.parts = 1;
if (parts != null) {
entry.parts = Integer.parseInt(parts);
}
String extension = type.getUnzipExtension(ctx, this); String extension = type.getUnzipExtension(ctx, this);
entry.targetFile = new File(parent, entry.baseName + extension); entry.targetFile = new File(parent, entry.baseName + extension);
File backup = new File(ctx.getAppPath(IndexConstants.BACKUP_INDEX_DIR), entry.targetFile.getName()); File backup = new File(ctx.getAppPath(IndexConstants.BACKUP_INDEX_DIR), entry.targetFile.getName());
@ -154,4 +139,8 @@ public class IndexItem implements Comparable<IndexItem> {
return type.getTargetFileName(this); return type.getTargetFileName(this);
} }
public String getDate(java.text.DateFormat format) {
return format.format(new Date(timestamp));
}
} }

View file

@ -437,7 +437,6 @@ public class ResourceManager {
List<String> warnings = new ArrayList<String>(); List<String> warnings = new ArrayList<String>();
if (file.exists() && file.canRead()) { if (file.exists() && file.canRead()) {
final java.text.DateFormat format = DateFormat.getDateFormat(context); final java.text.DateFormat format = DateFormat.getDateFormat(context);
format.setTimeZone(TimeZone.getTimeZone("GMT+01:00"));
File[] lf = file.listFiles(); File[] lf = file.listFiles();
if (lf != null) { if (lf != null) {
for (File f : lf) { for (File f : lf) {
@ -616,7 +615,6 @@ public class ResourceManager {
} }
} }
final java.text.DateFormat format = DateFormat.getDateFormat(context); final java.text.DateFormat format = DateFormat.getDateFormat(context);
format.setTimeZone(TimeZone.getTimeZone("GMT+01:00"));
for (File f : files) { for (File f : files) {
progress.startTask(context.getString(R.string.indexing_map) + " " + f.getName(), -1); //$NON-NLS-1$ progress.startTask(context.getString(R.string.indexing_map) + " " + f.getName(), -1); //$NON-NLS-1$
try { try {