Use android time utils

This commit is contained in:
vshcherb 2013-08-17 01:24:08 +02:00
parent b85725d6c1
commit 8fb4fdcb7a
9 changed files with 51 additions and 55 deletions

View file

@ -296,28 +296,4 @@ public class Algorithms {
return defaultValue; return defaultValue;
} }
private static java.text.DateFormat dateFormat;
private static java.text.DateFormat dateTimeFormat;
public static String formatDate(long t) {
return getDateFormat().format(new Date(t));
}
public static DateFormat getDateFormat() {
if(dateFormat == null) {
dateFormat = DateFormat.getDateInstance(DateFormat.SHORT, Locale.getDefault());
}
return dateFormat;
}
public static DateFormat getDateTimeFormat() {
if (dateTimeFormat == null) {
dateTimeFormat = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, Locale.getDefault());
}
return dateTimeFormat;
}
public static String formatDateTime(long t) {
return getDateTimeFormat().format(new Date(t));
}
} }

View file

@ -1,7 +1,11 @@
package net.osmand; package net.osmand;
import java.util.Date;
import android.content.Context; import android.content.Context;
import android.content.res.Configuration; import android.content.res.Configuration;
import android.text.format.DateFormat;
import android.view.View; import android.view.View;
import android.view.inputmethod.InputMethodManager; import android.view.inputmethod.InputMethodManager;
@ -32,4 +36,11 @@ public class AndroidUtils {
}); });
} }
public static String formatDate(Context ctx, long time) {
return DateFormat.getDateFormat(ctx).format(new Date(time));
}
public static String formatTime(Context ctx, long time) {
return DateFormat.getTimeFormat(ctx).format(new Date(time));
}
} }

View file

@ -12,6 +12,7 @@ import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import net.osmand.AndroidUtils;
import net.osmand.access.AccessibleToast; import net.osmand.access.AccessibleToast;
import net.osmand.plus.R; import net.osmand.plus.R;
import net.osmand.util.Algorithms; import net.osmand.util.Algorithms;
@ -26,6 +27,7 @@ import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.net.Uri; import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
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;
@ -131,7 +133,7 @@ public class ContributionVersionActivity extends OsmandListActivity {
AccessibleToast.makeText( AccessibleToast.makeText(
this, this,
MessageFormat.format(getString(R.string.build_installed), currentSelectedBuild.tag, MessageFormat.format(getString(R.string.build_installed), currentSelectedBuild.tag,
Algorithms.formatDate(currentSelectedBuild.date.getTime())), Toast.LENGTH_LONG).show(); AndroidUtils.formatDate(getMyApplication(), currentSelectedBuild.date.getTime())), Toast.LENGTH_LONG).show();
} }
getMyApplication().getSettings().CONTRIBUTION_INSTALL_APP_DATE.set(dateFormat.format(d)); getMyApplication().getSettings().CONTRIBUTION_INSTALL_APP_DATE.set(dateFormat.format(d));
} }
@ -195,7 +197,7 @@ public class ContributionVersionActivity extends OsmandListActivity {
final OsmAndBuild item = (OsmAndBuild) getListAdapter().getItem(position); final OsmAndBuild item = (OsmAndBuild) getListAdapter().getItem(position);
Builder builder = new AlertDialog.Builder(this); Builder builder = new AlertDialog.Builder(this);
builder.setMessage(MessageFormat.format(getString(R.string.install_selected_build), item.tag, builder.setMessage(MessageFormat.format(getString(R.string.install_selected_build), item.tag,
Algorithms.formatDate(item.date.getTime()), item.size)); AndroidUtils.formatDate(getMyApplication(), item.date.getTime()), item.size));
builder.setPositiveButton(R.string.default_buttons_yes, new DialogInterface.OnClickListener() { builder.setPositiveButton(R.string.default_buttons_yes, new DialogInterface.OnClickListener() {
@Override @Override
@ -248,7 +250,7 @@ public class ContributionVersionActivity extends OsmandListActivity {
TextView description = (TextView) row.findViewById(R.id.download_descr); TextView description = (TextView) row.findViewById(R.id.download_descr);
StringBuilder format = new StringBuilder(); StringBuilder format = new StringBuilder();
format.append(Algorithms.formatDate(build.date.getTime()))/*.append(" : ").append(build.size).append(" MB")*/; format.append(AndroidUtils.formatDate(getMyApplication(), build.date.getTime()))/*.append(" : ").append(build.size).append(" MB")*/;
description.setText(format.toString()); description.setText(format.toString());
int color = getResources().getColor(R.color.color_unknown); int color = getResources().getColor(R.color.color_unknown);

View file

@ -11,6 +11,7 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.TreeMap; import java.util.TreeMap;
import net.osmand.AndroidUtils;
import net.osmand.IndexConstants; import net.osmand.IndexConstants;
import net.osmand.access.AccessibleToast; import net.osmand.access.AccessibleToast;
import net.osmand.plus.ClientContext; import net.osmand.plus.ClientContext;
@ -28,10 +29,10 @@ import net.osmand.plus.download.DownloadIndexesThread;
import net.osmand.plus.download.IndexItem; import net.osmand.plus.download.IndexItem;
import net.osmand.plus.download.IndexItemCategory; import net.osmand.plus.download.IndexItemCategory;
import net.osmand.plus.srtmplugin.SRTMPlugin; import net.osmand.plus.srtmplugin.SRTMPlugin;
import net.osmand.util.Algorithms;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.app.AlertDialog.Builder; import android.app.AlertDialog.Builder;
import android.content.ActivityNotFoundException; import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener; import android.content.DialogInterface.OnClickListener;
import android.content.Intent; import android.content.Intent;
@ -456,24 +457,24 @@ public class DownloadIndexActivity extends OsmandExpandableListActivity {
private Map<String, String> listAlreadyDownloadedWithAlternatives() { private Map<String, String> listAlreadyDownloadedWithAlternatives() {
Map<String, String> files = new TreeMap<String, String>(); Map<String, String> files = new TreeMap<String, String>();
listWithAlternatives(getMyApplication().getAppPath(IndexConstants.BACKUP_INDEX_DIR),IndexConstants.BINARY_MAP_INDEX_EXT, files); listWithAlternatives(getMyApplication(), getMyApplication().getAppPath(IndexConstants.BACKUP_INDEX_DIR),IndexConstants.BINARY_MAP_INDEX_EXT, files);
listWithAlternatives(getMyApplication().getAppPath(IndexConstants.MAPS_PATH),IndexConstants.BINARY_MAP_INDEX_EXT, files); listWithAlternatives(getMyApplication(), getMyApplication().getAppPath(IndexConstants.MAPS_PATH),IndexConstants.BINARY_MAP_INDEX_EXT, files);
listWithAlternatives(getMyApplication().getAppPath(IndexConstants.MAPS_PATH),IndexConstants.EXTRA_EXT, files); listWithAlternatives(getMyApplication(), getMyApplication().getAppPath(IndexConstants.MAPS_PATH),IndexConstants.EXTRA_EXT, files);
if(OsmandPlugin.getEnabledPlugin(SRTMPlugin.class) != null) { if(OsmandPlugin.getEnabledPlugin(SRTMPlugin.class) != null) {
listWithAlternatives(getMyApplication().getAppPath(IndexConstants.SRTM_INDEX_DIR),IndexConstants.BINARY_MAP_INDEX_EXT, files); listWithAlternatives(getMyApplication(), getMyApplication().getAppPath(IndexConstants.SRTM_INDEX_DIR),IndexConstants.BINARY_MAP_INDEX_EXT, files);
} }
listWithAlternatives(getMyApplication().getAppPath(IndexConstants.VOICE_INDEX_DIR),"", files); listWithAlternatives(getMyApplication(), getMyApplication().getAppPath(IndexConstants.VOICE_INDEX_DIR),"", files);
return files; return files;
} }
public static Map<String, String> listWithAlternatives(File file, final String ext, public static Map<String, String> listWithAlternatives(final Context ctx, File file, final String ext,
final Map<String, String> files) { final Map<String, String> files) {
if (file.isDirectory()) { if (file.isDirectory()) {
file.list(new FilenameFilter() { file.list(new FilenameFilter() {
@Override @Override
public boolean accept(File dir, String filename) { public boolean accept(File dir, String filename) {
if (filename.endsWith(ext)) { if (filename.endsWith(ext)) {
String date = Algorithms.formatDate(new File(dir, filename).lastModified()); String date = AndroidUtils.formatDate(ctx, new File(dir, filename).lastModified());
files.put(filename, date); files.put(filename, date);
return true; return true;
} else { } else {

View file

@ -15,6 +15,7 @@ import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import net.osmand.AndroidUtils;
import net.osmand.IProgress; import net.osmand.IProgress;
import net.osmand.IndexConstants; import net.osmand.IndexConstants;
import net.osmand.Location; import net.osmand.Location;
@ -275,19 +276,19 @@ public class AudioVideoNotesPlugin extends OsmandPlugin {
public String getDescription(Context ctx) { public String getDescription(Context ctx) {
String nm = name == null ? "" : name; String nm = name == null ? "" : name;
if (isPhoto()) { if (isPhoto()) {
return ctx.getString(R.string.recording_photo_description, nm, Algorithms.formatDateTime(file.lastModified())).trim(); return ctx.getString(R.string.recording_photo_description, nm, AndroidUtils.formatTime(ctx, file.lastModified())).trim();
} }
updateInternalDescription(); updateInternalDescription();
return ctx.getString(R.string.recording_description, nm, getDuration(ctx), Algorithms.formatDateTime(file.lastModified())) return ctx.getString(R.string.recording_description, nm, getDuration(ctx), AndroidUtils.formatTime(ctx, file.lastModified()))
.trim(); .trim();
} }
public String getSmallDescription(Context ctx) { public String getSmallDescription(Context ctx) {
String nm = name == null ? "" : name; String nm = name == null ? "" : name;
if (isPhoto()) { if (isPhoto()) {
return ctx.getString(R.string.recording_photo_description, nm, Algorithms.formatDateTime(file.lastModified())).trim(); return ctx.getString(R.string.recording_photo_description, nm, AndroidUtils.formatTime(ctx,file.lastModified())).trim();
} }
return ctx.getString(R.string.recording_description, nm, "", Algorithms.formatDateTime(file.lastModified())).trim(); return ctx.getString(R.string.recording_description, nm, "", AndroidUtils.formatTime(ctx, file.lastModified())).trim();
} }
private String getDuration(Context ctx) { private String getDuration(Context ctx) {

View file

@ -89,12 +89,12 @@ public class DownloadIndexesThread {
public void updateLoadedFiles() { public void updateLoadedFiles() {
Map<String, String> indexActivatedFileNames = app.getResourceManager().getIndexFileNames(); Map<String, String> indexActivatedFileNames = app.getResourceManager().getIndexFileNames();
DownloadIndexActivity.listWithAlternatives(app.getAppPath(""), DownloadIndexActivity.listWithAlternatives(app, app.getAppPath(""),
IndexConstants.EXTRA_EXT, indexActivatedFileNames); IndexConstants.EXTRA_EXT, indexActivatedFileNames);
Map<String, String> indexFileNames = app.getResourceManager().getIndexFileNames(); Map<String, String> indexFileNames = app.getResourceManager().getIndexFileNames();
DownloadIndexActivity.listWithAlternatives(app.getAppPath(""), DownloadIndexActivity.listWithAlternatives(app, app.getAppPath(""),
IndexConstants.EXTRA_EXT, indexFileNames); IndexConstants.EXTRA_EXT, indexFileNames);
DownloadIndexActivity.listWithAlternatives(app.getAppPath(IndexConstants.TILES_INDEX_DIR), DownloadIndexActivity.listWithAlternatives(app,app.getAppPath(IndexConstants.TILES_INDEX_DIR),
IndexConstants.SQLITE_EXT, indexFileNames); IndexConstants.SQLITE_EXT, indexFileNames);
app.getResourceManager().getBackupIndexes(indexFileNames); app.getResourceManager().getBackupIndexes(indexFileNames);
this.indexFileNames = indexFileNames; this.indexFileNames = indexFileNames;

View file

@ -6,19 +6,18 @@ import java.io.InputStream;
import java.net.URL; import java.net.URL;
import java.text.ParseException; import java.text.ParseException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import net.osmand.AndroidUtils;
import net.osmand.IndexConstants; import net.osmand.IndexConstants;
import net.osmand.PlatformUtil; import net.osmand.PlatformUtil;
import net.osmand.plus.ClientContext; import net.osmand.plus.ClientContext;
import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandApplication;
import net.osmand.plus.OsmandSettings; import net.osmand.plus.OsmandSettings;
import net.osmand.plus.Version; import net.osmand.plus.Version;
import net.osmand.util.Algorithms;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParser;
@ -30,6 +29,7 @@ import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException; import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.AssetManager; import android.content.res.AssetManager;
import android.text.format.DateFormat;
public class DownloadOsmandIndexesHelper { public class DownloadOsmandIndexesHelper {
private final static Log log = PlatformUtil.getLog(DownloadOsmandIndexesHelper.class); private final static Log log = PlatformUtil.getLog(DownloadOsmandIndexesHelper.class);
@ -40,7 +40,7 @@ public class DownloadOsmandIndexesHelper {
PackageManager pm =ctx.getPackageManager(); PackageManager pm =ctx.getPackageManager();
AssetManager amanager = ctx.getAssets(); AssetManager amanager = ctx.getAssets();
String versionUrlParam = Version.getVersionAsURLParam(((OsmandApplication) ctx.getApplicationContext())); String versionUrlParam = Version.getVersionAsURLParam(((OsmandApplication) ctx.getApplicationContext()));
IndexFileList result = downloadIndexesListFromInternet(versionUrlParam); IndexFileList result = downloadIndexesListFromInternet(ctx, versionUrlParam);
if (result == null) { if (result == null) {
result = new IndexFileList(); result = new IndexFileList();
} else { } else {
@ -81,7 +81,7 @@ public class DownloadOsmandIndexesHelper {
try { try {
ApplicationInfo appInfo = pm.getApplicationInfo(OsmandApplication.class.getPackage().getName(), 0); ApplicationInfo appInfo = pm.getApplicationInfo(OsmandApplication.class.getPackage().getName(), 0);
dateModified = new File(appInfo.sourceDir).lastModified(); dateModified = new File(appInfo.sourceDir).lastModified();
date = Algorithms.formatDate(dateModified); date = AndroidUtils.formatDate((Context) settings.getContext(), dateModified);
} catch (NameNotFoundException e) { } catch (NameNotFoundException e) {
//do nothing... //do nothing...
} }
@ -98,7 +98,7 @@ public class DownloadOsmandIndexesHelper {
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 { try {
Date d = Algorithms.getDateFormat().parse(item.getDate()); Date d = DateFormat.getDateFormat((Context) settings.getContext()).parse(item.getDate());
if (d.getTime() > dateModified) { if (d.getTime() > dateModified) {
continue; continue;
} }
@ -130,7 +130,7 @@ public class DownloadOsmandIndexesHelper {
return null; return null;
} }
private static IndexFileList downloadIndexesListFromInternet(String versionAsUrl){ private static IndexFileList downloadIndexesListFromInternet(Context ctx, String versionAsUrl){
try { try {
IndexFileList result = new IndexFileList(); IndexFileList result = new IndexFileList();
log.debug("Start loading list of index files"); //$NON-NLS-1$ log.debug("Start loading list of index files"); //$NON-NLS-1$
@ -150,7 +150,7 @@ public class DownloadOsmandIndexesHelper {
String date = parser.getAttributeValue(null, "date"); //$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$ String parts = parser.getAttributeValue(null, "parts"); //$NON-NLS-1$
date = reparseDate(date); date = reparseDate(ctx, date);
IndexItem it = new IndexItem(name, description, date, size, parts); IndexItem it = new IndexItem(name, description, date, size, parts);
it.setType(tp); it.setType(tp);
result.add(it); result.add(it);
@ -180,10 +180,10 @@ public class DownloadOsmandIndexesHelper {
} }
} }
private static String reparseDate(String date) { private static String reparseDate(Context ctx, String date) {
try { try {
Date d = simpleDateFormat.parse(date); Date d = simpleDateFormat.parse(date);
return Algorithms.formatDate(d.getTime()); return AndroidUtils.formatDate(ctx, d.getTime());
} catch (ParseException e) { } catch (ParseException e) {
return date; return date;
} }

View file

@ -10,6 +10,7 @@ import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import net.osmand.AndroidUtils;
import net.osmand.IndexConstants; import net.osmand.IndexConstants;
import net.osmand.PlatformUtil; import net.osmand.PlatformUtil;
import net.osmand.plus.ClientContext; import net.osmand.plus.ClientContext;
@ -19,6 +20,9 @@ import net.osmand.util.Algorithms;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import android.content.Context;
import android.text.format.DateFormat;
public class IndexItem implements Comparable<IndexItem> { public class IndexItem implements Comparable<IndexItem> {
private static final Log log = PlatformUtil.getLog(IndexItem.class); private static final Log log = PlatformUtil.getLog(IndexItem.class);
@ -193,7 +197,7 @@ public class IndexItem implements Comparable<IndexItem> {
entry.zipStream = zipStream; entry.zipStream = zipStream;
entry.unzipFolder = unzipDir; entry.unzipFolder = unzipDir;
try { try {
Date d = Algorithms.getDateFormat().parse(date); Date d = DateFormat.getDateFormat((Context) ctx).parse(date);
entry.dateModified = d.getTime(); entry.dateModified = d.getTime();
} catch (ParseException e1) { } catch (ParseException e1) {
log.error("ParseException", e1); log.error("ParseException", e1);

View file

@ -18,6 +18,7 @@ import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.TreeMap; import java.util.TreeMap;
import net.osmand.AndroidUtils;
import net.osmand.GeoidAltitudeCorrection; import net.osmand.GeoidAltitudeCorrection;
import net.osmand.IProgress; import net.osmand.IProgress;
import net.osmand.IndexConstants; import net.osmand.IndexConstants;
@ -427,7 +428,7 @@ public class ResourceManager {
conf = new File(f, "_ttsconfig.p"); conf = new File(f, "_ttsconfig.p");
} }
if (conf.exists()) { if (conf.exists()) {
indexFileNames.put(f.getName(), Algorithms.formatDate(conf.lastModified())); //$NON-NLS-1$ indexFileNames.put(f.getName(), AndroidUtils.formatDate(context, conf.lastModified())); //$NON-NLS-1$
} }
} }
} }
@ -619,7 +620,7 @@ public class ResourceManager {
if (dateCreated == 0) { if (dateCreated == 0) {
dateCreated = f.lastModified(); dateCreated = f.lastModified();
} }
indexFileNames.put(f.getName(), Algorithms.formatDate(dateCreated)); //$NON-NLS-1$ indexFileNames.put(f.getName(), AndroidUtils.formatDate(context, dateCreated)); //$NON-NLS-1$
for (String rName : index.getRegionNames()) { for (String rName : index.getRegionNames()) {
// skip duplicate names (don't make collision between getName() and name in the map) // skip duplicate names (don't make collision between getName() and name in the map)
// it can be dangerous to use one file to different indexes if it is multithreaded // it can be dangerous to use one file to different indexes if it is multithreaded
@ -907,7 +908,7 @@ public class ResourceManager {
if (lf != null) { if (lf != null) {
for (File f : lf) { for (File f : lf) {
if (f != null && f.getName().endsWith(IndexConstants.BINARY_MAP_INDEX_EXT)) { if (f != null && f.getName().endsWith(IndexConstants.BINARY_MAP_INDEX_EXT)) {
map.put(f.getName(), Algorithms.formatDate(f.lastModified())); //$NON-NLS-1$ map.put(f.getName(), AndroidUtils.formatDate(context, f.lastModified())); //$NON-NLS-1$
} }
} }
} }