Centralise int and date parsing and formatting in Utils
This commit is contained in:
parent
c92970a3ed
commit
b751594bfe
@ -29,7 +29,6 @@ import org.xml.sax.Attributes;
|
||||
import org.xml.sax.SAXException;
|
||||
import org.xml.sax.helpers.DefaultHandler;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@ -129,18 +128,10 @@ public class RepoXMLHandler extends DefaultHandler {
|
||||
curapk.version = str;
|
||||
break;
|
||||
case "versioncode":
|
||||
try {
|
||||
curapk.vercode = Integer.parseInt(str);
|
||||
} catch (NumberFormatException ex) {
|
||||
curapk.vercode = -1;
|
||||
}
|
||||
curapk.vercode = Utils.parseInt(str, -1);
|
||||
break;
|
||||
case "size":
|
||||
try {
|
||||
curapk.size = Integer.parseInt(str);
|
||||
} catch (NumberFormatException ex) {
|
||||
curapk.size = 0;
|
||||
}
|
||||
curapk.size = Utils.parseInt(str, 0);
|
||||
break;
|
||||
case "hash":
|
||||
if (hashType == null || hashType.equals("md5")) {
|
||||
@ -163,26 +154,13 @@ public class RepoXMLHandler extends DefaultHandler {
|
||||
curapk.apkName = str;
|
||||
break;
|
||||
case "sdkver":
|
||||
try {
|
||||
curapk.minSdkVersion = Integer.parseInt(str);
|
||||
} catch (NumberFormatException ex) {
|
||||
curapk.minSdkVersion = 0;
|
||||
}
|
||||
curapk.minSdkVersion = Utils.parseInt(str, 0);
|
||||
break;
|
||||
case "maxsdkver":
|
||||
try {
|
||||
curapk.maxSdkVersion = Integer.parseInt(str);
|
||||
} catch (NumberFormatException ex) {
|
||||
curapk.maxSdkVersion = 0;
|
||||
}
|
||||
curapk.maxSdkVersion = Utils.parseInt(str, 0);
|
||||
break;
|
||||
case "added":
|
||||
try {
|
||||
curapk.added = str.length() == 0 ? null : Utils.DATE_FORMAT
|
||||
.parse(str);
|
||||
} catch (ParseException e) {
|
||||
curapk.added = null;
|
||||
}
|
||||
curapk.added = Utils.parseDate(str, null);
|
||||
break;
|
||||
case "permissions":
|
||||
curapk.permissions = Utils.CommaSeparatedList.make(str);
|
||||
@ -243,30 +221,16 @@ public class RepoXMLHandler extends DefaultHandler {
|
||||
curapp.trackerURL = str;
|
||||
break;
|
||||
case "added":
|
||||
try {
|
||||
curapp.added = str.length() == 0 ? null : Utils.DATE_FORMAT
|
||||
.parse(str);
|
||||
} catch (ParseException e) {
|
||||
curapp.added = null;
|
||||
}
|
||||
curapp.added = Utils.parseDate(str, null);
|
||||
break;
|
||||
case "lastupdated":
|
||||
try {
|
||||
curapp.lastUpdated = str.length() == 0 ? null
|
||||
: Utils.DATE_FORMAT.parse(str);
|
||||
} catch (ParseException e) {
|
||||
curapp.lastUpdated = null;
|
||||
}
|
||||
curapp.lastUpdated = Utils.parseDate(str, null);
|
||||
break;
|
||||
case "marketversion":
|
||||
curapp.upstreamVersion = str;
|
||||
break;
|
||||
case "marketvercode":
|
||||
try {
|
||||
curapp.upstreamVercode = Integer.parseInt(str);
|
||||
} catch (NumberFormatException ex) {
|
||||
curapp.upstreamVercode = -1;
|
||||
}
|
||||
curapp.upstreamVercode = Utils.parseInt(str, -1);
|
||||
break;
|
||||
case "categories":
|
||||
curapp.categories = Utils.CommaSeparatedList.make(str);
|
||||
@ -293,19 +257,8 @@ public class RepoXMLHandler extends DefaultHandler {
|
||||
if (pk != null)
|
||||
pubkey = pk;
|
||||
|
||||
final String maxAgeAttr = attributes.getValue("", "maxage");
|
||||
if (maxAgeAttr != null) {
|
||||
try {
|
||||
maxage = Integer.parseInt(maxAgeAttr);
|
||||
} catch (NumberFormatException nfe) {}
|
||||
}
|
||||
|
||||
final String versionAttr = attributes.getValue("", "version");
|
||||
if (versionAttr != null) {
|
||||
try {
|
||||
version = Integer.parseInt(versionAttr);
|
||||
} catch (NumberFormatException nfe) {}
|
||||
}
|
||||
maxage = Utils.parseInt(attributes.getValue("", "maxage"), -1);
|
||||
version = Utils.parseInt(attributes.getValue("", "version"), -1);
|
||||
|
||||
final String nm = attributes.getValue("", "name");
|
||||
if (nm != null)
|
||||
|
@ -54,7 +54,9 @@ import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.security.cert.Certificate;
|
||||
import java.security.cert.CertificateEncodingException;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.Formatter;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
@ -69,12 +71,12 @@ public final class Utils {
|
||||
|
||||
// The date format used for storing dates (e.g. lastupdated, added) in the
|
||||
// database.
|
||||
public static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
|
||||
private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
|
||||
|
||||
private static final String[] FRIENDLY_SIZE_FORMAT = {
|
||||
"%.0f B", "%.0f KiB", "%.1f MiB", "%.2f GiB" };
|
||||
|
||||
public static final SimpleDateFormat LOG_DATE_FORMAT =
|
||||
private static final SimpleDateFormat LOG_DATE_FORMAT =
|
||||
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH);
|
||||
|
||||
public static String getIconsDir(Context context) {
|
||||
@ -470,6 +472,45 @@ public final class Utils {
|
||||
return String.format("%0" + (bytes.length << 1) + "X", bi);
|
||||
}
|
||||
|
||||
public static int parseInt(String str, int fallback) {
|
||||
if (str == null || str.length() == 0) {
|
||||
return fallback;
|
||||
}
|
||||
int result;
|
||||
try {
|
||||
result = Integer.parseInt(str);
|
||||
} catch (NumberFormatException e) {
|
||||
result = fallback;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public static Date parseDate(String str, Date fallback) {
|
||||
if (str == null || str.length() == 0) {
|
||||
return fallback;
|
||||
}
|
||||
Date result;
|
||||
try {
|
||||
result = DATE_FORMAT.parse(str);
|
||||
} catch (ParseException e) {
|
||||
result = fallback;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public static String formatDate(Date date, String fallback) {
|
||||
if (date == null) {
|
||||
return fallback;
|
||||
}
|
||||
return DATE_FORMAT.format(date);
|
||||
}
|
||||
|
||||
public static String formatLogDate(Date date) {
|
||||
if (date == null) {
|
||||
return "(unknown)";
|
||||
}
|
||||
return LOG_DATE_FORMAT.format(date);
|
||||
}
|
||||
|
||||
// Need this to add the unimplemented support for ordered and unordered
|
||||
// lists to Html.fromHtml().
|
||||
|
@ -71,7 +71,7 @@ public class Apk extends ValueObject implements Comparable<Apk> {
|
||||
hashType = cursor.getString(i);
|
||||
break;
|
||||
case ApkProvider.DataColumns.ADDED_DATE:
|
||||
added = ValueObject.toDate(cursor.getString(i));
|
||||
added = Utils.parseDate(cursor.getString(i), null);
|
||||
break;
|
||||
case ApkProvider.DataColumns.FEATURES:
|
||||
features = Utils.CommaSeparatedList.make(cursor.getString(i));
|
||||
@ -147,7 +147,7 @@ public class Apk extends ValueObject implements Comparable<Apk> {
|
||||
values.put(ApkProvider.DataColumns.NAME, apkName);
|
||||
values.put(ApkProvider.DataColumns.MIN_SDK_VERSION, minSdkVersion);
|
||||
values.put(ApkProvider.DataColumns.MAX_SDK_VERSION, maxSdkVersion);
|
||||
values.put(ApkProvider.DataColumns.ADDED_DATE, added == null ? "" : Utils.DATE_FORMAT.format(added));
|
||||
values.put(ApkProvider.DataColumns.ADDED_DATE, Utils.formatDate(added, ""));
|
||||
values.put(ApkProvider.DataColumns.PERMISSIONS, Utils.CommaSeparatedList.str(permissions));
|
||||
values.put(ApkProvider.DataColumns.FEATURES, Utils.CommaSeparatedList.str(features));
|
||||
values.put(ApkProvider.DataColumns.NATIVE_CODE, Utils.CommaSeparatedList.str(nativecode));
|
||||
|
@ -176,10 +176,10 @@ public class App extends ValueObject implements Comparable<App> {
|
||||
upstreamVersion = cursor.getString(i);
|
||||
break;
|
||||
case AppProvider.DataColumns.ADDED:
|
||||
added = ValueObject.toDate(cursor.getString(i));
|
||||
added = Utils.parseDate(cursor.getString(i), null);
|
||||
break;
|
||||
case AppProvider.DataColumns.LAST_UPDATED:
|
||||
lastUpdated = ValueObject.toDate(cursor.getString(i));
|
||||
lastUpdated = Utils.parseDate(cursor.getString(i), null);
|
||||
break;
|
||||
case AppProvider.DataColumns.CATEGORIES:
|
||||
categories = Utils.CommaSeparatedList.make(cursor.getString(i));
|
||||
@ -383,8 +383,8 @@ public class App extends ValueObject implements Comparable<App> {
|
||||
values.put(AppProvider.DataColumns.LITECOIN_ADDR, litecoinAddr);
|
||||
values.put(AppProvider.DataColumns.DOGECOIN_ADDR, dogecoinAddr);
|
||||
values.put(AppProvider.DataColumns.FLATTR_ID, flattrID);
|
||||
values.put(AppProvider.DataColumns.ADDED, added == null ? "" : Utils.DATE_FORMAT.format(added));
|
||||
values.put(AppProvider.DataColumns.LAST_UPDATED, added == null ? "" : Utils.DATE_FORMAT.format(lastUpdated));
|
||||
values.put(AppProvider.DataColumns.ADDED, Utils.formatDate(added, ""));
|
||||
values.put(AppProvider.DataColumns.LAST_UPDATED, Utils.formatDate(lastUpdated, ""));
|
||||
values.put(AppProvider.DataColumns.SUGGESTED_VERSION_CODE, suggestedVercode);
|
||||
values.put(AppProvider.DataColumns.UPSTREAM_VERSION, upstreamVersion);
|
||||
values.put(AppProvider.DataColumns.UPSTREAM_VERSION_CODE, upstreamVercode);
|
||||
|
@ -612,13 +612,13 @@ public class AppProvider extends FDroidProvider {
|
||||
|
||||
private AppQuerySelection queryNewlyAdded() {
|
||||
final String selection = "fdroid_app.added > ?";
|
||||
final String[] args = { Utils.DATE_FORMAT.format(Preferences.get().calcMaxHistory()) };
|
||||
final String[] args = { Utils.formatDate(Preferences.get().calcMaxHistory(), "") };
|
||||
return new AppQuerySelection(selection, args);
|
||||
}
|
||||
|
||||
private AppQuerySelection queryRecentlyUpdated() {
|
||||
final String selection = "fdroid_app.added != fdroid_app.lastUpdated AND fdroid_app.lastUpdated > ?";
|
||||
final String[] args = { Utils.DATE_FORMAT.format(Preferences.get().calcMaxHistory()) };
|
||||
final String[] args = { Utils.formatDate(Preferences.get().calcMaxHistory(), "") };
|
||||
return new AppQuerySelection(selection, args);
|
||||
}
|
||||
|
||||
|
@ -9,7 +9,6 @@ import org.fdroid.fdroid.Utils;
|
||||
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.text.ParseException;
|
||||
import java.util.Date;
|
||||
|
||||
public class Repo extends ValueObject {
|
||||
@ -34,7 +33,6 @@ public class Repo extends ValueObject {
|
||||
public boolean isSwap;
|
||||
|
||||
public Repo() {
|
||||
|
||||
}
|
||||
|
||||
public Repo(Cursor cursor) {
|
||||
@ -62,7 +60,7 @@ public class Repo extends ValueObject {
|
||||
inuse = cursor.getInt(i) == 1;
|
||||
break;
|
||||
case RepoProvider.DataColumns.LAST_UPDATED:
|
||||
lastUpdated = toDate(cursor.getString(i));
|
||||
lastUpdated = Utils.parseDate(cursor.getString(i), null);
|
||||
break;
|
||||
case RepoProvider.DataColumns.MAX_AGE:
|
||||
maxage = cursor.getInt(i);
|
||||
@ -129,9 +127,8 @@ public class Repo extends ValueObject {
|
||||
private static int toInt(Integer value) {
|
||||
if (value == null) {
|
||||
return 0;
|
||||
} else {
|
||||
return value;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
public void setValues(ContentValues values) {
|
||||
@ -161,14 +158,8 @@ public class Repo extends ValueObject {
|
||||
}
|
||||
|
||||
if (values.containsKey(RepoProvider.DataColumns.LAST_UPDATED)) {
|
||||
String dateString = values.getAsString(RepoProvider.DataColumns.LAST_UPDATED);
|
||||
if (dateString != null) {
|
||||
try {
|
||||
lastUpdated = Utils.DATE_FORMAT.parse(dateString);
|
||||
} catch (ParseException e) {
|
||||
Log.e(TAG, "Error parsing date " + dateString);
|
||||
}
|
||||
}
|
||||
final String dateString = values.getAsString(RepoProvider.DataColumns.LAST_UPDATED);
|
||||
lastUpdated = Utils.parseDate(dateString, null);
|
||||
}
|
||||
|
||||
if (values.containsKey(RepoProvider.DataColumns.MAX_AGE)) {
|
||||
|
@ -1,13 +1,9 @@
|
||||
package org.fdroid.fdroid.data;
|
||||
|
||||
import android.database.Cursor;
|
||||
import android.util.Log;
|
||||
|
||||
import org.fdroid.fdroid.Utils;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.util.Date;
|
||||
|
||||
abstract class ValueObject {
|
||||
|
||||
private static final String TAG = "fdroid.ValueObject";
|
||||
@ -20,16 +16,4 @@ abstract class ValueObject {
|
||||
}
|
||||
}
|
||||
|
||||
static Date toDate(String string) {
|
||||
Date date = null;
|
||||
if (string != null) {
|
||||
try {
|
||||
date = Utils.DATE_FORMAT.parse(string);
|
||||
} catch (ParseException e) {
|
||||
Log.e(TAG, "Error parsing date " + string);
|
||||
}
|
||||
}
|
||||
return date;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -191,7 +191,7 @@ abstract public class RepoUpdater {
|
||||
|
||||
ContentValues values = new ContentValues();
|
||||
|
||||
values.put(RepoProvider.DataColumns.LAST_UPDATED, Utils.DATE_FORMAT.format(new Date()));
|
||||
values.put(RepoProvider.DataColumns.LAST_UPDATED, Utils.formatDate(new Date(), ""));
|
||||
|
||||
if (repo.lastetag == null || !repo.lastetag.equals(etag)) {
|
||||
values.put(RepoProvider.DataColumns.LAST_ETAG, etag);
|
||||
|
@ -121,7 +121,7 @@ public class SignedRepoUpdater extends RepoUpdater {
|
||||
UpdateException {
|
||||
Date updateTime = new Date(System.currentTimeMillis());
|
||||
Log.d(TAG, "Getting signed index from " + repo.address + " at " +
|
||||
Utils.LOG_DATE_FORMAT.format(updateTime));
|
||||
Utils.formatLogDate(updateTime));
|
||||
|
||||
File indexJar = downloadedFile;
|
||||
File indexXml = null;
|
||||
|
Loading…
x
Reference in New Issue
Block a user