aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/cgeo/geocaching/utils
diff options
context:
space:
mode:
Diffstat (limited to 'main/src/cgeo/geocaching/utils')
-rw-r--r--main/src/cgeo/geocaching/utils/AngleUtils.java8
-rw-r--r--main/src/cgeo/geocaching/utils/CryptUtils.java12
-rw-r--r--main/src/cgeo/geocaching/utils/ImageHelper.java11
-rw-r--r--main/src/cgeo/geocaching/utils/LeastRecentlyUsedMap.java2
-rw-r--r--main/src/cgeo/geocaching/utils/LeastRecentlyUsedSet.java2
-rw-r--r--main/src/cgeo/geocaching/utils/LogTemplateProvider.java10
-rw-r--r--main/src/cgeo/geocaching/utils/MatcherWrapper.java91
-rw-r--r--main/src/cgeo/geocaching/utils/PeriodicHandler.java2
-rw-r--r--main/src/cgeo/geocaching/utils/TranslationUtils.java5
9 files changed, 115 insertions, 28 deletions
diff --git a/main/src/cgeo/geocaching/utils/AngleUtils.java b/main/src/cgeo/geocaching/utils/AngleUtils.java
index e2b4a66..6e59a91 100644
--- a/main/src/cgeo/geocaching/utils/AngleUtils.java
+++ b/main/src/cgeo/geocaching/utils/AngleUtils.java
@@ -8,9 +8,11 @@ public class AngleUtils {
/**
* Return the angle to turn of to go from an angle to the other
- *
- * @param from the origin angle in degrees
- * @param to the target angle in degreees
+ *
+ * @param from
+ * the origin angle in degrees
+ * @param to
+ * the target angle in degrees
* @return a value in degrees, in the [-180, 180[ range
*/
public static float difference(final float from, final float to) {
diff --git a/main/src/cgeo/geocaching/utils/CryptUtils.java b/main/src/cgeo/geocaching/utils/CryptUtils.java
index f04327e..7a23156 100644
--- a/main/src/cgeo/geocaching/utils/CryptUtils.java
+++ b/main/src/cgeo/geocaching/utils/CryptUtils.java
@@ -46,16 +46,14 @@ public final class CryptUtils {
boolean plaintext = false;
final int length = text.length();
- int c;
- int capitalized;
for (int index = 0; index < length; index++) {
- c = text.charAt(index);
+ int c = text.charAt(index);
if (c == '[') {
plaintext = true;
} else if (c == ']') {
plaintext = false;
} else if (!plaintext) {
- capitalized = c & 32;
+ int capitalized = c & 32;
c &= ~capitalized;
c = ((c >= 'A') && (c <= 'Z') ? ((c - 'A' + 13) % 26 + 'A') : c)
| capitalized;
@@ -116,16 +114,14 @@ public final class CryptUtils {
boolean plaintext = false;
final int length = span.length();
- int c;
- int capitalized;
for (int index = 0; index < length; index++) {
- c = span.charAt(index);
+ int c = span.charAt(index);
if (c == '[') {
plaintext = true;
} else if (c == ']') {
plaintext = false;
} else if (!plaintext) {
- capitalized = c & 32;
+ int capitalized = c & 32;
c &= ~capitalized;
c = ((c >= 'A') && (c <= 'Z') ? ((c - 'A' + 13) % 26 + 'A') : c)
| capitalized;
diff --git a/main/src/cgeo/geocaching/utils/ImageHelper.java b/main/src/cgeo/geocaching/utils/ImageHelper.java
index 4c18b06..98cad64 100644
--- a/main/src/cgeo/geocaching/utils/ImageHelper.java
+++ b/main/src/cgeo/geocaching/utils/ImageHelper.java
@@ -1,13 +1,12 @@
package cgeo.geocaching.utils;
import cgeo.geocaching.cgeoapplication;
+import cgeo.geocaching.compatibility.Compatibility;
-import android.content.Context;
import android.graphics.Bitmap;
+import android.graphics.Point;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
-import android.view.Display;
-import android.view.WindowManager;
public class ImageHelper {
@@ -24,9 +23,9 @@ public class ImageHelper {
*/
public static BitmapDrawable scaleBitmapToFitDisplay(final Bitmap image) {
final cgeoapplication app = cgeoapplication.getInstance();
- final Display display = ((WindowManager) app.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
- final int maxWidth = display.getWidth() - 25;
- final int maxHeight = display.getHeight() - 25;
+ Point displaySize = Compatibility.getDisplaySize();
+ final int maxWidth = displaySize.x - 25;
+ final int maxHeight = displaySize.y - 25;
Bitmap result = image;
int width = image.getWidth();
diff --git a/main/src/cgeo/geocaching/utils/LeastRecentlyUsedMap.java b/main/src/cgeo/geocaching/utils/LeastRecentlyUsedMap.java
index f0bc4f5..6122532 100644
--- a/main/src/cgeo/geocaching/utils/LeastRecentlyUsedMap.java
+++ b/main/src/cgeo/geocaching/utils/LeastRecentlyUsedMap.java
@@ -16,7 +16,7 @@ import java.util.Map;
*/
public abstract class LeastRecentlyUsedMap<K, V> extends LinkedHashMap<K, V> {
- private static enum OperationModes {
+ private enum OperationModes {
LRU_CACHE, BOUNDED
}
diff --git a/main/src/cgeo/geocaching/utils/LeastRecentlyUsedSet.java b/main/src/cgeo/geocaching/utils/LeastRecentlyUsedSet.java
index b654fd6..aafce4f 100644
--- a/main/src/cgeo/geocaching/utils/LeastRecentlyUsedSet.java
+++ b/main/src/cgeo/geocaching/utils/LeastRecentlyUsedSet.java
@@ -133,7 +133,7 @@ public class LeastRecentlyUsedSet<E> extends AbstractSet<E>
*/
@Override
@SuppressWarnings("unchecked")
- public Object clone() {
+ public Object clone() throws CloneNotSupportedException {
try {
synchronized (this) {
final LeastRecentlyUsedSet<E> newSet = (LeastRecentlyUsedSet<E>) super.clone();
diff --git a/main/src/cgeo/geocaching/utils/LogTemplateProvider.java b/main/src/cgeo/geocaching/utils/LogTemplateProvider.java
index 7cacd9d..7171fde 100644
--- a/main/src/cgeo/geocaching/utils/LogTemplateProvider.java
+++ b/main/src/cgeo/geocaching/utils/LogTemplateProvider.java
@@ -2,8 +2,8 @@ package cgeo.geocaching.utils;
import cgeo.geocaching.R;
import cgeo.geocaching.Settings;
+import cgeo.geocaching.Trackable;
import cgeo.geocaching.cgCache;
-import cgeo.geocaching.cgTrackable;
import cgeo.geocaching.connector.gc.GCConstants;
import cgeo.geocaching.connector.gc.Login;
import cgeo.geocaching.network.Network;
@@ -29,14 +29,14 @@ public class LogTemplateProvider {
*/
public static class LogContext {
private cgCache cache;
- private cgTrackable trackable;
+ private Trackable trackable;
private boolean offline = false;
public LogContext(final cgCache cache) {
this(cache, false);
}
- public LogContext(final cgTrackable trackable) {
+ public LogContext(final Trackable trackable) {
this.trackable = trackable;
}
@@ -53,7 +53,7 @@ public class LogTemplateProvider {
return cache;
}
- public cgTrackable getTrackable() {
+ public Trackable getTrackable() {
return trackable;
}
@@ -148,7 +148,7 @@ public class LogTemplateProvider {
@Override
public String getValue(final LogContext context) {
- cgTrackable trackable = context.getTrackable();
+ Trackable trackable = context.getTrackable();
if (trackable != null) {
return trackable.getOwner();
}
diff --git a/main/src/cgeo/geocaching/utils/MatcherWrapper.java b/main/src/cgeo/geocaching/utils/MatcherWrapper.java
new file mode 100644
index 0000000..c3c1663
--- /dev/null
+++ b/main/src/cgeo/geocaching/utils/MatcherWrapper.java
@@ -0,0 +1,91 @@
+package cgeo.geocaching.utils;
+
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+/**
+ * Wrapper around the regex {@link Matcher} class. This implementation optimizes the memory usage of the matched
+ * Strings.
+ *
+ */
+public class MatcherWrapper {
+ private final Matcher matcher;
+
+ public MatcherWrapper(Pattern pattern, String input) {
+ this.matcher = pattern.matcher(input);
+ }
+
+ /**
+ * see {@link Matcher#find()}
+ */
+ public boolean find() {
+ return matcher.find();
+ }
+
+ /**
+ * see {@link Matcher#group(int)}
+ */
+ public String group(int index) {
+ return newString(matcher.group(index));
+ }
+
+ /**
+ * This method explicitly creates a new String instance from an already existing String. This is necessary to avoid
+ * huge memory leaks in our parser. If we do regular expression matching on large Strings, the returned matches are
+ * otherwise memory mapped substrings of the huge original String, therefore blocking the garbage collector from
+ * removing the huge input String.
+ * <p>
+ * Do not change this method, even if Findbugs and other tools will report a violation for that line!
+ *
+ * @param input
+ * @return
+ */
+ private static String newString(String input) {
+ if (input == null) {
+ return null;
+ }
+ return new String(input); // DON'T REMOVE THE "new String" HERE!
+ }
+
+ /**
+ * see {@link Matcher#groupCount()}
+ */
+ public int groupCount() {
+ return matcher.groupCount();
+ }
+
+ /**
+ * see {@link Matcher#group()}
+ */
+ public String group() {
+ return newString(matcher.group());
+ }
+
+ /**
+ * see {@link Matcher#start()}
+ */
+ public int start() {
+ return matcher.start();
+ }
+
+ /**
+ * see {@link Matcher#replaceAll(String)}
+ */
+ public String replaceAll(String replacement) {
+ return newString(matcher.replaceAll(replacement));
+ }
+
+ /**
+ * see {@link Matcher#matches()}
+ */
+ public boolean matches() {
+ return matcher.matches();
+ }
+
+ /**
+ * see {@link Matcher#replaceFirst(String)}
+ */
+ public String replaceFirst(String replacement) {
+ return newString(matcher.replaceFirst(replacement));
+ }
+}
diff --git a/main/src/cgeo/geocaching/utils/PeriodicHandler.java b/main/src/cgeo/geocaching/utils/PeriodicHandler.java
index 3f6c345..ac6b22a 100644
--- a/main/src/cgeo/geocaching/utils/PeriodicHandler.java
+++ b/main/src/cgeo/geocaching/utils/PeriodicHandler.java
@@ -26,7 +26,7 @@ abstract public class PeriodicHandler extends Handler {
* @param period
* The period in milliseconds.
*/
- public PeriodicHandler(final long period) {
+ protected PeriodicHandler(final long period) {
this.period = period;
}
diff --git a/main/src/cgeo/geocaching/utils/TranslationUtils.java b/main/src/cgeo/geocaching/utils/TranslationUtils.java
index a29c5a7..4d318f0 100644
--- a/main/src/cgeo/geocaching/utils/TranslationUtils.java
+++ b/main/src/cgeo/geocaching/utils/TranslationUtils.java
@@ -1,12 +1,11 @@
package cgeo.geocaching.utils;
import cgeo.geocaching.activity.AbstractActivity;
+import cgeo.geocaching.network.Network;
import android.content.Intent;
import android.net.Uri;
-import java.net.URLEncoder;
-
/**
* Utilities used for translating
*/
@@ -30,7 +29,7 @@ public final class TranslationUtils {
* @return URI ready to be parsed
*/
private static String buildTranslationURI(final String toLang, final String text) {
- String content = URLEncoder.encode(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)) {
content = content.replace("+", "%20");