diff options
author | Samuel Tardieu <sam@rfc1149.net> | 2014-10-22 20:44:11 +0200 |
---|---|---|
committer | Samuel Tardieu <sam@rfc1149.net> | 2014-10-22 20:44:11 +0200 |
commit | b41ca6e3195dcf60a842632b37b90059b7cd9960 (patch) | |
tree | b14bae3aaf84664d9453753abb4a259bedc8797d /main/src/cgeo/geocaching/connector/gc/GCLogin.java | |
parent | ee7f4ea1ee068383572131f98c61571ffd304aa4 (diff) | |
parent | 1da80caa52fbb6a66db92c93298cf34390ac3676 (diff) | |
download | cgeo-b41ca6e3195dcf60a842632b37b90059b7cd9960.zip cgeo-b41ca6e3195dcf60a842632b37b90059b7cd9960.tar.gz cgeo-b41ca6e3195dcf60a842632b37b90059b7cd9960.tar.bz2 |
Merge branch 'issue-4416' into upstream
Conflicts:
main/src/cgeo/geocaching/connector/gc/GCLogin.java
Diffstat (limited to 'main/src/cgeo/geocaching/connector/gc/GCLogin.java')
-rw-r--r-- | main/src/cgeo/geocaching/connector/gc/GCLogin.java | 80 |
1 files changed, 17 insertions, 63 deletions
diff --git a/main/src/cgeo/geocaching/connector/gc/GCLogin.java b/main/src/cgeo/geocaching/connector/gc/GCLogin.java index b2122d4..9eb54ad 100644 --- a/main/src/cgeo/geocaching/connector/gc/GCLogin.java +++ b/main/src/cgeo/geocaching/connector/gc/GCLogin.java @@ -27,43 +27,15 @@ import android.graphics.drawable.Drawable; import java.text.ParseException; import java.text.SimpleDateFormat; -import java.util.Collections; import java.util.Date; -import java.util.HashMap; -import java.util.Locale; -import java.util.Map; +import java.util.GregorianCalendar; public class GCLogin extends AbstractLogin { - private static final String DEFAULT_CUSTOM_DATE_FORMAT = "MM/dd/yyyy"; - private final static String ENGLISH = "<a href=\"#\">English ▼</a>"; - private final static Map<String, SimpleDateFormat> GC_CUSTOM_DATE_FORMATS; public static final String LANGUAGE_CHANGE_URI = "http://www.geocaching.com/my/souvenirs.aspx"; - static { - final String[] formats = new String[] { - DEFAULT_CUSTOM_DATE_FORMAT, - "yyyy-MM-dd", - "yyyy/MM/dd", - "dd.MM.yyyy", - "dd/MMM/yyyy", - "dd.MMM.yyyy", - "MMM/dd/yyyy", - "dd MMM yy", - "dd/MM/yyyy" - }; - - final Map<String, SimpleDateFormat> map = new HashMap<>(); - - for (final String format : formats) { - map.put(format, new SimpleDateFormat(format, Locale.ENGLISH)); - } - - GC_CUSTOM_DATE_FORMATS = Collections.unmodifiableMap(map); - } - private GCLogin() { // singleton } @@ -76,6 +48,11 @@ public class GCLogin extends AbstractLogin { private static final GCLogin INSTANCE = new GCLogin(); } + private static StatusCode resetGcCustomDate(final StatusCode statusCode) { + Settings.setGcCustomDate("MM/dd/yyyy"); + return statusCode; + } + @Override protected StatusCode login(boolean retry) { final ImmutablePair<String, String> credentials = Settings.getGcCredentials(); @@ -85,7 +62,7 @@ public class GCLogin extends AbstractLogin { if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) { clearLoginInfo(); Log.e("Login.login: No login information stored"); - return StatusCode.NO_LOGIN_INFO_STORED; + return resetGcCustomDate(StatusCode.NO_LOGIN_INFO_STORED); } setActualStatus(CgeoApplication.getInstance().getString(R.string.init_login_popup_working)); @@ -105,6 +82,7 @@ public class GCLogin extends AbstractLogin { if (switchToEnglish(loginData) && retry) { return login(false); } + detectGcCustomDate(); return StatusCode.NO_ERROR; // logged in } @@ -141,17 +119,18 @@ public class GCLogin extends AbstractLogin { } Log.i("Successfully logged in Geocaching.com as " + username + " (" + Settings.getGCMemberStatus() + ')'); Settings.setCookieStore(Cookies.dumpCookieStore()); + detectGcCustomDate(); return StatusCode.NO_ERROR; // logged in } if (loginData.contains("Your username/password combination does not match.")) { Log.i("Failed to log in Geocaching.com as " + username + " because of wrong username/password"); - return StatusCode.WRONG_LOGIN_DATA; // wrong login + return resetGcCustomDate(StatusCode.WRONG_LOGIN_DATA); // wrong login } if (loginData.contains("You must validate your account before you can log in.")) { Log.i("Failed to log in Geocaching.com as " + username + " because account needs to be validated first"); - return StatusCode.UNVALIDATED_ACCOUNT; + return resetGcCustomDate(StatusCode.UNVALIDATED_ACCOUNT); } Log.i("Failed to log in Geocaching.com as " + username + " for some unknown reason"); @@ -160,7 +139,7 @@ public class GCLogin extends AbstractLogin { return login(false); } - return StatusCode.UNKNOWN_ERROR; // can't login + return resetGcCustomDate(StatusCode.UNKNOWN_ERROR); // can't login } public StatusCode logout() { @@ -287,9 +266,9 @@ public class GCLogin extends AbstractLogin { /** * Detect user date settings on geocaching.com */ - public static void detectGcCustomDate() { + private static void detectGcCustomDate() { - final String result = Network.getResponseData(Network.getRequest("http://www.geocaching.com/account/ManagePreferences.aspx")); + final String result = Network.getResponseData(Network.getRequest("https://www.geocaching.com/myaccount/settings/preferences")); if (null == result) { Log.w("Login.detectGcCustomDate: result is null"); @@ -303,40 +282,15 @@ public class GCLogin extends AbstractLogin { } public static Date parseGcCustomDate(final String input, final String format) throws ParseException { - if (StringUtils.isBlank(input)) { - throw new ParseException("Input is null", 0); - } - - final String trimmed = input.trim(); - - if (GC_CUSTOM_DATE_FORMATS.containsKey(format)) { - try { - return GC_CUSTOM_DATE_FORMATS.get(format).parse(trimmed); - } catch (final ParseException ignored) { - } - } - - for (final SimpleDateFormat sdf : GC_CUSTOM_DATE_FORMATS.values()) { - try { - return sdf.parse(trimmed); - } catch (final ParseException ignored) { - } - } - - throw new ParseException("No matching pattern", 0); + return new SimpleDateFormat(format).parse(input.trim()); } public static Date parseGcCustomDate(final String input) throws ParseException { return parseGcCustomDate(input, Settings.getGcCustomDate()); } - public static SimpleDateFormat getCustomGcDateFormat() { - final String format = Settings.getGcCustomDate(); - if (GC_CUSTOM_DATE_FORMATS.containsKey(format)) { - return GC_CUSTOM_DATE_FORMATS.get(format); - } - - return GC_CUSTOM_DATE_FORMATS.get(DEFAULT_CUSTOM_DATE_FORMAT); + public static String formatGcCustomDate(int year, int month, int day) { + return new SimpleDateFormat(Settings.getGcCustomDate()).format(new GregorianCalendar(year, month - 1, day).getTime()); } /** |