diff options
Diffstat (limited to 'main/src/cgeo/geocaching/utils')
8 files changed, 264 insertions, 54 deletions
diff --git a/main/src/cgeo/geocaching/utils/GeoDirHandler.java b/main/src/cgeo/geocaching/utils/GeoDirHandler.java index 78455c4..14e6426 100644 --- a/main/src/cgeo/geocaching/utils/GeoDirHandler.java +++ b/main/src/cgeo/geocaching/utils/GeoDirHandler.java @@ -1,8 +1,8 @@ package cgeo.geocaching.utils; import cgeo.geocaching.IGeoData; -import cgeo.geocaching.Settings; import cgeo.geocaching.cgeoapplication; +import cgeo.geocaching.settings.Settings; import android.os.Handler; import android.os.Message; @@ -63,6 +63,11 @@ public abstract class GeoDirHandler extends Handler implements IObserver<Object> obtainMessage(OBSERVABLE, o).sendToTarget(); } + public void updateAll() { + update(app.currentGeo()); + update(app.currentDirection()); + } + /** * Update method called when new IGeoData is available. * diff --git a/main/src/cgeo/geocaching/utils/Log.java b/main/src/cgeo/geocaching/utils/Log.java index f912ddd..f7f33d9 100644 --- a/main/src/cgeo/geocaching/utils/Log.java +++ b/main/src/cgeo/geocaching/utils/Log.java @@ -23,11 +23,11 @@ final public class Log { } /** - * make a non persisted copy of the debug flag from the settings for performance reasons - * + * save a copy of the debug flag from the settings for performance reasons + * * @param isDebug */ - public static void setDebugUnsaved(boolean isDebug) { + public static void setDebug(boolean isDebug) { Log.isDebug = isDebug; } diff --git a/main/src/cgeo/geocaching/utils/LogTemplateProvider.java b/main/src/cgeo/geocaching/utils/LogTemplateProvider.java index 2576e64..98201b5 100644 --- a/main/src/cgeo/geocaching/utils/LogTemplateProvider.java +++ b/main/src/cgeo/geocaching/utils/LogTemplateProvider.java @@ -1,12 +1,12 @@ package cgeo.geocaching.utils; +import cgeo.geocaching.Geocache; import cgeo.geocaching.R; -import cgeo.geocaching.Settings; import cgeo.geocaching.Trackable; -import cgeo.geocaching.Geocache; -import cgeo.geocaching.connector.gc.GCConstants; -import cgeo.geocaching.connector.gc.Login; -import cgeo.geocaching.network.Network; +import cgeo.geocaching.connector.ConnectorFactory; +import cgeo.geocaching.connector.IConnector; +import cgeo.geocaching.connector.capability.ILogin; +import cgeo.geocaching.settings.Settings; import cgeo.geocaching.ui.Formatter; import org.apache.commons.lang3.StringUtils; @@ -14,10 +14,14 @@ import org.apache.commons.lang3.StringUtils; import java.util.ArrayList; /** - * provides all the available templates for logging + * Provides all the available templates for logging. * */ -public class LogTemplateProvider { +public final class LogTemplateProvider { + + private LogTemplateProvider() { + // utility class + } /** * Context aware data container for log templates. @@ -40,54 +44,57 @@ public class LogTemplateProvider { this.trackable = trackable; } - public LogContext(boolean offline) { + public LogContext(final boolean offline) { this(null, offline); } - public LogContext(final Geocache cache, boolean offline) { + public LogContext(final Geocache cache, final boolean offline) { this.cache = cache; this.offline = offline; } - public Geocache getCache() { + public final Geocache getCache() { return cache; } - public Trackable getTrackable() { + public final Trackable getTrackable() { return trackable; } - public boolean isOffline() { + public final boolean isOffline() { return offline; } } - public static abstract class LogTemplate { + public abstract static class LogTemplate { private final String template; private final int resourceId; - protected LogTemplate(String template, int resourceId) { + protected LogTemplate(final String template, final int resourceId) { this.template = template; this.resourceId = resourceId; } - abstract public String getValue(LogContext context); + public abstract String getValue(LogContext context); - public int getResourceId() { + public final int getResourceId() { return resourceId; } - public int getItemId() { + public final int getItemId() { return template.hashCode(); } - public String getTemplateString() { + public final String getTemplateString() { return template; } - protected String apply(String input, LogContext context) { - if (input.contains("[" + template + "]")) { - return StringUtils.replace(input, "[" + template + "]", getValue(context)); + protected final String apply(final String input, final LogContext context) { + final String bracketedTemplate = "[" + template + "]"; + + // check containment first to not unconditionally call the getValue(...) method + if (input.contains(bracketedTemplate)) { + return StringUtils.replace(input, bracketedTemplate, getValue(context)); } return input; } @@ -128,20 +135,32 @@ public class LogTemplateProvider { @Override public String getValue(final LogContext context) { - int current = Login.getActualCachesFound(); + final Geocache cache = context.getCache(); + if (cache == null) { + return StringUtils.EMPTY; + } + + int current = 0; + final IConnector connector = ConnectorFactory.getConnector(cache); + if (connector instanceof ILogin) { + current = ((ILogin) connector).getCachesFound(); + } + + // try updating the login information, if the counter is zero if (current == 0) { if (context.isOffline()) { - return ""; + return StringUtils.EMPTY; + } + if (connector instanceof ILogin) { + ((ILogin) connector).login(null, null); + current = ((ILogin) connector).getCachesFound(); } - final String page = Network.getResponseData(Network.getRequest("http://www.geocaching.com/email/")); - current = parseFindCount(page); } - String findCount = ""; if (current >= 0) { - findCount = String.valueOf(current + 1); + return String.valueOf(current + 1); } - return findCount; + return StringUtils.EMPTY; } }); templates.add(new LogTemplate("OWNER", R.string.init_signature_template_owner) { @@ -156,13 +175,13 @@ public class LogTemplateProvider { if (cache != null) { return cache.getOwnerDisplayName(); } - return ""; + return StringUtils.EMPTY; } }); return templates; } - public static LogTemplate getTemplate(int itemId) { + public static LogTemplate getTemplate(final int itemId) { for (LogTemplate template : getTemplates()) { if (template.getItemId() == itemId) { return template; @@ -171,9 +190,9 @@ public class LogTemplateProvider { return null; } - public static String applyTemplates(String signature, LogContext context) { + public static String applyTemplates(final String signature, final LogContext context) { if (signature == null) { - return ""; + return StringUtils.EMPTY; } String result = signature; for (LogTemplate template : getTemplates()) { @@ -181,17 +200,4 @@ public class LogTemplateProvider { } return result; } - - private static int parseFindCount(String page) { - if (StringUtils.isBlank(page)) { - return -1; - } - - try { - return Integer.parseInt(TextUtils.getMatch(page, GCConstants.PATTERN_CACHES_FOUND, true, "-1").replaceAll("[,.]", "")); - } catch (NumberFormatException e) { - Log.e("parseFindCount", e); - return -1; - } - } } diff --git a/main/src/cgeo/geocaching/utils/ProcessUtils.java b/main/src/cgeo/geocaching/utils/ProcessUtils.java index b566b79..3345ff1 100644 --- a/main/src/cgeo/geocaching/utils/ProcessUtils.java +++ b/main/src/cgeo/geocaching/utils/ProcessUtils.java @@ -2,8 +2,14 @@ package cgeo.geocaching.utils; import cgeo.geocaching.cgeoapplication; +import org.apache.commons.collections.CollectionUtils; + import android.content.Intent; +import android.content.pm.PackageInfo; import android.content.pm.PackageManager; +import android.content.pm.ResolveInfo; + +import java.util.List; public final class ProcessUtils { @@ -11,10 +17,44 @@ public final class ProcessUtils { // utility class } - public static boolean isInstalled(final String packageName) { + /** + * Preferred method to detect the availability of an external app + * + * @param packageName + * @return + */ + public static boolean isLaunchable(final String packageName) { return getLaunchIntent(packageName) != null; } + /** + * Checks whether a launch intent is available or if the package is just installed + * This function is relatively costly, so if you know that the package in question has + * a launch intent, use isLaunchable() instead. + * + * @param packageName + * @return + */ + public static boolean isInstalled(final String packageName) { + return isLaunchable(packageName) || hasPackageInstalled(packageName); + } + + /** + * This will find installed applications even without launch intent (e.g. the streetview plugin). + */ + private static boolean hasPackageInstalled(final String packageName) { + final List<PackageInfo> packs = cgeoapplication.getInstance().getPackageManager().getInstalledPackages(0); + for (final PackageInfo packageInfo : packs) { + if (packageName.equals(packageInfo.packageName)) { + return true; + } + } + return false; + } + + /** + * This will find applications, which can be launched. + */ public static Intent getLaunchIntent(final String packageName) { if (packageName == null) { return null; @@ -24,8 +64,16 @@ public final class ProcessUtils { // This can throw an exception where the exception type is only defined on API Level > 3 // therefore surround with try-catch return packageManager.getLaunchIntentForPackage(packageName); - } catch (Exception e) { + } catch (final Exception e) { return null; } } + + public static boolean isIntentAvailable(final String intent) { + final PackageManager packageManager = cgeoapplication.getInstance().getPackageManager(); + final List<ResolveInfo> list = packageManager.queryIntentActivities( + new Intent(intent), PackageManager.MATCH_DEFAULT_ONLY); + + return CollectionUtils.isNotEmpty(list); + } } diff --git a/main/src/cgeo/geocaching/utils/SimpleCancellableHandler.java b/main/src/cgeo/geocaching/utils/SimpleCancellableHandler.java new file mode 100644 index 0000000..94246e0 --- /dev/null +++ b/main/src/cgeo/geocaching/utils/SimpleCancellableHandler.java @@ -0,0 +1,86 @@ +package cgeo.geocaching.utils; + +import cgeo.geocaching.CacheDetailActivity; +import cgeo.geocaching.activity.AbstractActivity; +import cgeo.geocaching.activity.Progress; + +import android.content.res.Resources; +import android.os.Message; + +import java.lang.ref.WeakReference; + +public class SimpleCancellableHandler extends CancellableHandler { + public static final String SUCCESS_TEXT = "success_message"; + protected final WeakReference<AbstractActivity> activityRef; + protected final WeakReference<Progress> progressDialogRef; + + public SimpleCancellableHandler(final AbstractActivity activity, final Progress progress) { + this.activityRef = new WeakReference<AbstractActivity>(activity); + this.progressDialogRef = new WeakReference<Progress>(progress); + } + + @Override + public void handleRegularMessage(final Message msg) { + AbstractActivity activity = activityRef.get(); + if (activity != null && msg.getData() != null && msg.getData().getString(SUCCESS_TEXT) != null) { + activity.showToast(msg.getData().getString(SUCCESS_TEXT)); + } + Progress progressDialog = progressDialogRef.get(); + if (progressDialog != null) { + progressDialog.dismiss(); + } + return; + } + + @Override + public void handleCancel(final Object extra) { + AbstractActivity activity = activityRef.get(); + if (activity != null) { + activity.showToast((String) extra); + } + Progress progressDialog = progressDialogRef.get(); + if (progressDialog != null) { + progressDialog.dismiss(); + } + } + + public final void showToast(int resId) { + AbstractActivity activity = activityRef.get(); + if (activity != null) { + Resources res = activity.getResources(); + activity.showToast(res.getText(resId).toString()); + } + } + + public final void dismissProgress() { + Progress progressDialog = progressDialogRef.get(); + if (progressDialog != null) { + progressDialog.dismiss(); + } + } + + protected final void setProgressMessage(final String txt) { + Progress progressDialog = progressDialogRef.get(); + if (progressDialog != null) { + progressDialog.setMessage(txt); + } + } + + protected final void finishActivity() { + AbstractActivity activity = activityRef.get(); + if (activity != null) { + activity.finish(); + } + + } + + protected void updateStatusMsg(final int resId, final String msg) { + CacheDetailActivity activity = ((CacheDetailActivity) activityRef.get()); + if (activity != null) { + setProgressMessage(activity.getResources().getString(resId) + + "\n\n" + + msg); + } + } + +} diff --git a/main/src/cgeo/geocaching/utils/SimpleHandler.java b/main/src/cgeo/geocaching/utils/SimpleHandler.java new file mode 100644 index 0000000..554ded6 --- /dev/null +++ b/main/src/cgeo/geocaching/utils/SimpleHandler.java @@ -0,0 +1,65 @@ +package cgeo.geocaching.utils; + +import cgeo.geocaching.activity.AbstractActivity; +import cgeo.geocaching.activity.Progress; + +import android.content.res.Resources; +import android.os.Handler; +import android.os.Message; + +import java.lang.ref.WeakReference; + +public abstract class SimpleHandler extends Handler { + public static final String SUCCESS_TEXT = "success_message"; + protected final WeakReference<AbstractActivity> activityRef; + protected final WeakReference<Progress> progressDialogRef; + + public SimpleHandler(final AbstractActivity activity, final Progress progress) { + this.activityRef = new WeakReference<AbstractActivity>(activity); + this.progressDialogRef = new WeakReference<Progress>(progress); + } + + @Override + public void handleMessage(final Message msg) { + AbstractActivity activity = activityRef.get(); + if (activity != null && msg.getData() != null && msg.getData().getString(SUCCESS_TEXT) != null) { + activity.showToast(msg.getData().getString(SUCCESS_TEXT)); + } + Progress progressDialog = progressDialogRef.get(); + if (progressDialog != null) { + progressDialog.dismiss(); + } + return; + } + + protected final void showToast(final int resId) { + AbstractActivity activity = activityRef.get(); + if (activity != null) { + Resources res = activity.getResources(); + activity.showToast(res.getText(resId).toString()); + } + } + + protected final void dismissProgress() { + Progress progressDialog = progressDialogRef.get(); + if (progressDialog != null) { + progressDialog.dismiss(); + } + } + + protected final void setProgressMessage(final String txt) { + Progress progressDialog = progressDialogRef.get(); + if (progressDialog != null) { + progressDialog.setMessage(txt); + } + } + + protected final void finishActivity() { + AbstractActivity activity = activityRef.get(); + if (activity != null) { + activity.finish(); + } + + } + +} diff --git a/main/src/cgeo/geocaching/utils/TextUtils.java b/main/src/cgeo/geocaching/utils/TextUtils.java index 68ac595..c9d4958 100644 --- a/main/src/cgeo/geocaching/utils/TextUtils.java +++ b/main/src/cgeo/geocaching/utils/TextUtils.java @@ -108,13 +108,13 @@ public final class TextUtils { } /** - * Replaces every \n, \r and \t with a single space. Afterwards multiples spaces + * Replaces every \n, \r and \t with a single space. Afterwards multiple spaces * are merged into a single space. Finally leading spaces are deleted. * * This method must be fast, but may not lead to the shortest replacement String. * * You are only allowed to change this code if you can prove it became faster on a device. - * see cgeo.geocaching.test.WhiteSpaceTest#replaceWhitespaceManually in the test project + * see cgeo.geocaching.test.WhiteSpaceTest#replaceWhitespaceManually in the test project. * * @param data * complete HTML page diff --git a/main/src/cgeo/geocaching/utils/TranslationUtils.java b/main/src/cgeo/geocaching/utils/TranslationUtils.java index 05045ee..1224f7e 100644 --- a/main/src/cgeo/geocaching/utils/TranslationUtils.java +++ b/main/src/cgeo/geocaching/utils/TranslationUtils.java @@ -35,7 +35,7 @@ public final class TranslationUtils { private static String buildTranslationURI(final String toLang, final String text) { String content = Network.encode(text); // the app works better without the "+", the website works better with "+", therefore assume using the app if installed - if (ProcessUtils.isInstalled(TRANSLATION_APP)) { + if (ProcessUtils.isLaunchable(TRANSLATION_APP)) { content = content.replace("+", "%20"); } return translationWebsite + translationForceClassicMode + translationAutoSelect + translationFieldSeparator + toLang + translationFieldSeparator + content; |
