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/CryptUtils.java40
-rw-r--r--main/src/cgeo/geocaching/utils/DatabaseBackupUtils.java2
-rw-r--r--main/src/cgeo/geocaching/utils/ImageUtils.java3
-rw-r--r--main/src/cgeo/geocaching/utils/Log.java7
-rw-r--r--main/src/cgeo/geocaching/utils/SimpleCancellableHandler.java1
-rw-r--r--main/src/cgeo/geocaching/utils/SimpleHandler.java37
-rw-r--r--main/src/cgeo/geocaching/utils/UncertainProperty.java5
7 files changed, 41 insertions, 54 deletions
diff --git a/main/src/cgeo/geocaching/utils/CryptUtils.java b/main/src/cgeo/geocaching/utils/CryptUtils.java
index 18a337d..d98585a 100644
--- a/main/src/cgeo/geocaching/utils/CryptUtils.java
+++ b/main/src/cgeo/geocaching/utils/CryptUtils.java
@@ -1,11 +1,17 @@
package cgeo.geocaching.utils;
+import org.apache.commons.lang3.CharEncoding;
+import org.apache.commons.lang3.StringUtils;
+
import android.text.Spannable;
import android.text.SpannableStringBuilder;
+import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
+import java.security.GeneralSecurityException;
import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
@@ -62,7 +68,7 @@ public final class CryptUtils {
public static String rot13(String text) {
if (text == null) {
- return "";
+ return StringUtils.EMPTY;
}
final StringBuilder result = new StringBuilder();
Rot13Encryption rot13 = new Rot13Encryption();
@@ -76,42 +82,44 @@ public final class CryptUtils {
}
public static String md5(String text) {
- String hashed = "";
-
try {
final MessageDigest digest = MessageDigest.getInstance("MD5");
- digest.update(text.getBytes(), 0, text.length());
- hashed = new BigInteger(1, digest.digest()).toString(16);
- } catch (Exception e) {
+ digest.update(text.getBytes(CharEncoding.UTF_8), 0, text.length());
+ return new BigInteger(1, digest.digest()).toString(16);
+ } catch (NoSuchAlgorithmException e) {
+ Log.e("CryptUtils.md5", e);
+ } catch (UnsupportedEncodingException e) {
Log.e("CryptUtils.md5", e);
}
- return hashed;
+ return StringUtils.EMPTY;
}
public static String sha1(String text) {
- String hashed = "";
-
try {
final MessageDigest digest = MessageDigest.getInstance("SHA-1");
- digest.update(text.getBytes(), 0, text.length());
- hashed = new BigInteger(1, digest.digest()).toString(16);
- } catch (Exception e) {
+ digest.update(text.getBytes(CharEncoding.UTF_8), 0, text.length());
+ return new BigInteger(1, digest.digest()).toString(16);
+ } catch (NoSuchAlgorithmException e) {
+ Log.e("CryptUtils.sha1", e);
+ } catch (UnsupportedEncodingException e) {
Log.e("CryptUtils.sha1", e);
}
- return hashed;
+ return StringUtils.EMPTY;
}
public static byte[] hashHmac(String text, String salt) {
byte[] macBytes = {};
try {
- final SecretKeySpec secretKeySpec = new SecretKeySpec(salt.getBytes(), "HmacSHA1");
+ final SecretKeySpec secretKeySpec = new SecretKeySpec(salt.getBytes(CharEncoding.UTF_8), "HmacSHA1");
final Mac mac = Mac.getInstance("HmacSHA1");
mac.init(secretKeySpec);
- macBytes = mac.doFinal(text.getBytes());
- } catch (Exception e) {
+ macBytes = mac.doFinal(text.getBytes(CharEncoding.UTF_8));
+ } catch (GeneralSecurityException e) {
+ Log.e("CryptUtils.hashHmac", e);
+ } catch (UnsupportedEncodingException e) {
Log.e("CryptUtils.hashHmac", e);
}
diff --git a/main/src/cgeo/geocaching/utils/DatabaseBackupUtils.java b/main/src/cgeo/geocaching/utils/DatabaseBackupUtils.java
index 24f375d..8aa605f 100644
--- a/main/src/cgeo/geocaching/utils/DatabaseBackupUtils.java
+++ b/main/src/cgeo/geocaching/utils/DatabaseBackupUtils.java
@@ -97,7 +97,7 @@ public class DatabaseBackupUtils {
public static File getRestoreFile() {
final File fileSourceFile = cgData.getBackupFileInternal();
- return fileSourceFile.exists() ? fileSourceFile : null;
+ return fileSourceFile.exists() && fileSourceFile.length() > 0 ? fileSourceFile : null;
}
public static boolean hasBackup() {
diff --git a/main/src/cgeo/geocaching/utils/ImageUtils.java b/main/src/cgeo/geocaching/utils/ImageUtils.java
index ea7d3ff..dc6333a 100644
--- a/main/src/cgeo/geocaching/utils/ImageUtils.java
+++ b/main/src/cgeo/geocaching/utils/ImageUtils.java
@@ -10,6 +10,7 @@ import android.graphics.drawable.BitmapDrawable;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
+import java.io.IOException;
public final class ImageUtils {
@@ -75,7 +76,7 @@ public final class ImageUtils {
bitmap.compress(format, quality, bos);
bos.flush();
bos.close();
- } catch (Exception e) {
+ } catch (IOException e) {
Log.e("ImageHelper.storeBitmap", e);
}
}
diff --git a/main/src/cgeo/geocaching/utils/Log.java b/main/src/cgeo/geocaching/utils/Log.java
index aa8dbd1..1ade2f9 100644
--- a/main/src/cgeo/geocaching/utils/Log.java
+++ b/main/src/cgeo/geocaching/utils/Log.java
@@ -1,11 +1,14 @@
package cgeo.geocaching.utils;
+import org.apache.commons.lang3.CharEncoding;
+
import android.os.Environment;
import java.io.BufferedWriter;
import java.io.File;
-import java.io.FileWriter;
+import java.io.FileOutputStream;
import java.io.IOException;
+import java.io.OutputStreamWriter;
import java.io.Writer;
public final class Log {
@@ -107,7 +110,7 @@ public final class Log {
}
Writer writer = null;
try {
- writer = new BufferedWriter(new FileWriter(file, true));
+ writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, true), CharEncoding.UTF_8));
writer.write(msg);
} catch (final IOException e) {
Log.e("logToFile: cannot write to " + file, e);
diff --git a/main/src/cgeo/geocaching/utils/SimpleCancellableHandler.java b/main/src/cgeo/geocaching/utils/SimpleCancellableHandler.java
index 9207c74..22cd4d7 100644
--- a/main/src/cgeo/geocaching/utils/SimpleCancellableHandler.java
+++ b/main/src/cgeo/geocaching/utils/SimpleCancellableHandler.java
@@ -26,7 +26,6 @@ public class SimpleCancellableHandler extends CancellableHandler {
activity.showToast(msg.getData().getString(MESSAGE_TEXT));
}
dismissProgress();
- return;
}
@Override
diff --git a/main/src/cgeo/geocaching/utils/SimpleHandler.java b/main/src/cgeo/geocaching/utils/SimpleHandler.java
index b01d0e1..8e0a479 100644
--- a/main/src/cgeo/geocaching/utils/SimpleHandler.java
+++ b/main/src/cgeo/geocaching/utils/SimpleHandler.java
@@ -3,7 +3,6 @@ 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;
@@ -15,48 +14,20 @@ public abstract class SimpleHandler extends Handler {
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);
+ activityRef = new WeakReference<AbstractActivity>(activity);
+ progressDialogRef = new WeakReference<Progress>(progress);
}
@Override
public void handleMessage(final Message msg) {
- AbstractActivity activity = activityRef.get();
+ final AbstractActivity activity = activityRef.get();
if (activity != null && msg.getData() != null && msg.getData().getString(MESSAGE_TEXT) != null) {
activity.showToast(msg.getData().getString(MESSAGE_TEXT));
}
- dismissProgress();
- 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();
+ final 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/UncertainProperty.java b/main/src/cgeo/geocaching/utils/UncertainProperty.java
index 5f86662..e8686e3 100644
--- a/main/src/cgeo/geocaching/utils/UncertainProperty.java
+++ b/main/src/cgeo/geocaching/utils/UncertainProperty.java
@@ -2,6 +2,11 @@ package cgeo.geocaching.utils;
import cgeo.geocaching.connector.gc.Tile;
+/**
+ * Property with certainty. When merging properties, the one with higher certainty wins.
+ *
+ * @param <T>
+ */
public class UncertainProperty<T> {
private final T value;