diff options
author | blafoo <github@blafoo.de> | 2012-02-29 20:57:11 +0100 |
---|---|---|
committer | blafoo <github@blafoo.de> | 2012-02-29 20:57:11 +0100 |
commit | ed20ccd76e27b48348f59f1d4650a5e8c65f49af (patch) | |
tree | c705c5c3ed59aa4d806c9c42c523dff4c2a2cd39 | |
parent | daee2e9a847071d1080e42435a41ed3da57c0d29 (diff) | |
download | cgeo-ed20ccd76e27b48348f59f1d4650a5e8c65f49af.zip cgeo-ed20ccd76e27b48348f59f1d4650a5e8c65f49af.tar.gz cgeo-ed20ccd76e27b48348f59f1d4650a5e8c65f49af.tar.bz2 |
Moved methods to better fitting classes
-rw-r--r-- | main/src/cgeo/geocaching/cgBase.java | 152 | ||||
-rw-r--r-- | main/src/cgeo/geocaching/geopoint/Viewport.java | 59 | ||||
-rw-r--r-- | main/src/cgeo/geocaching/maps/CGeoMap.java | 6 | ||||
-rw-r--r-- | main/src/cgeo/geocaching/network/OAuth.java | 2 | ||||
-rw-r--r-- | main/src/cgeo/geocaching/utils/CryptUtils.java | 96 |
5 files changed, 159 insertions, 156 deletions
diff --git a/main/src/cgeo/geocaching/cgBase.java b/main/src/cgeo/geocaching/cgBase.java index b26b8c8..38b404e 100644 --- a/main/src/cgeo/geocaching/cgBase.java +++ b/main/src/cgeo/geocaching/cgBase.java @@ -2595,158 +2595,6 @@ public class cgBase { } } - public static boolean isInViewPort(int centerLat1, int centerLon1, int centerLat2, int centerLon2, int spanLat1, int spanLon1, int spanLat2, int spanLon2) { - try { - // expects coordinates in E6 format - final int left1 = centerLat1 - (spanLat1 / 2); - final int right1 = centerLat1 + (spanLat1 / 2); - final int top1 = centerLon1 + (spanLon1 / 2); - final int bottom1 = centerLon1 - (spanLon1 / 2); - - final int left2 = centerLat2 - (spanLat2 / 2); - final int right2 = centerLat2 + (spanLat2 / 2); - final int top2 = centerLon2 + (spanLon2 / 2); - final int bottom2 = centerLon2 - (spanLon2 / 2); - - if (left2 <= left1) { - return false; - } - if (right2 >= right1) { - return false; - } - if (top2 >= top1) { - return false; - } - if (bottom2 <= bottom1) { - return false; - } - - return true; - } catch (Exception e) { - Log.e(Settings.tag, "cgBase.isInViewPort: " + e.toString()); - return false; - } - } - - // viewport is defined by center, span and some (10%) reserve on every side - /** - * Check if coordinates are located in a viewport (defined by its center and span - * in each direction). The viewport also includes a 10% extension on each side. - * - * @param centerLat - * the viewport center latitude - * @param centerLon - * the viewport center longitude - * @param spanLat - * the latitude span - * @param spanLon - * the longitude span - * @param coords - * the coordinates to check - * @return true if the coordinates are in the viewport - */ - public static boolean isCacheInViewPort(int centerLat, int centerLon, int spanLat, int spanLon, final Geopoint coords) { - return Math.abs(coords.getLatitudeE6() - centerLat) <= Math.abs(spanLat) * 0.6 && - Math.abs(coords.getLongitudeE6() - centerLon) <= Math.abs(spanLon) * 0.6; - } - - private static char[] base64map1 = new char[64]; - - static { - int i = 0; - for (char c = 'A'; c <= 'Z'; c++) { - base64map1[i++] = c; - } - for (char c = 'a'; c <= 'z'; c++) { - base64map1[i++] = c; - } - for (char c = '0'; c <= '9'; c++) { - base64map1[i++] = c; - } - base64map1[i++] = '+'; - base64map1[i++] = '/'; - } - private static byte[] base64map2 = new byte[128]; - - static { - for (int i = 0; i < base64map2.length; i++) { - base64map2[i] = -1; - } - for (int i = 0; i < 64; i++) { - base64map2[base64map1[i]] = (byte) i; - } - } - - public static String base64Encode(byte[] in) { - int iLen = in.length; - int oDataLen = (iLen * 4 + 2) / 3; // output length without padding - int oLen = ((iLen + 2) / 3) * 4; // output length including padding - char[] out = new char[oLen]; - int ip = 0; - int op = 0; - - while (ip < iLen) { - int i0 = in[ip++] & 0xff; - int i1 = ip < iLen ? in[ip++] & 0xff : 0; - int i2 = ip < iLen ? in[ip++] & 0xff : 0; - int o0 = i0 >>> 2; - int o1 = ((i0 & 3) << 4) | (i1 >>> 4); - int o2 = ((i1 & 0xf) << 2) | (i2 >>> 6); - int o3 = i2 & 0x3F; - out[op++] = base64map1[o0]; - out[op++] = base64map1[o1]; - out[op] = op < oDataLen ? base64map1[o2] : '='; - op++; - out[op] = op < oDataLen ? base64map1[o3] : '='; - op++; - } - - return new String(out); - } - - public static byte[] base64Decode(String text) { - char[] in = text.toCharArray(); - - int iLen = in.length; - if (iLen % 4 != 0) { - throw new IllegalArgumentException("Length of Base64 encoded input string is not a multiple of 4."); - } - while (iLen > 0 && in[iLen - 1] == '=') { - iLen--; - } - int oLen = (iLen * 3) / 4; - byte[] out = new byte[oLen]; - int ip = 0; - int op = 0; - while (ip < iLen) { - int i0 = in[ip++]; - int i1 = in[ip++]; - int i2 = ip < iLen ? in[ip++] : 'A'; - int i3 = ip < iLen ? in[ip++] : 'A'; - if (i0 > 127 || i1 > 127 || i2 > 127 || i3 > 127) { - throw new IllegalArgumentException("Illegal character in Base64 encoded data."); - } - int b0 = base64map2[i0]; - int b1 = base64map2[i1]; - int b2 = base64map2[i2]; - int b3 = base64map2[i3]; - if (b0 < 0 || b1 < 0 || b2 < 0 || b3 < 0) { - throw new IllegalArgumentException("Illegal character in Base64 encoded data."); - } - int o0 = (b0 << 2) | (b1 >>> 4); - int o1 = ((b1 & 0xf) << 4) | (b2 >>> 2); - int o2 = ((b2 & 3) << 6) | b3; - out[op++] = (byte) o0; - if (op < oLen) { - out[op++] = (byte) o1; - } - if (op < oLen) { - out[op++] = (byte) o2; - } - } - return out; - } - public static boolean runNavigation(Activity activity, Resources res, Settings settings, final Geopoint coords) { return runNavigation(activity, res, settings, coords, null); } diff --git a/main/src/cgeo/geocaching/geopoint/Viewport.java b/main/src/cgeo/geocaching/geopoint/Viewport.java index 981f0c8..3e92b78 100644 --- a/main/src/cgeo/geocaching/geopoint/Viewport.java +++ b/main/src/cgeo/geocaching/geopoint/Viewport.java @@ -1,5 +1,9 @@ package cgeo.geocaching.geopoint; +import cgeo.geocaching.Settings; + +import android.util.Log; + public class Viewport { public final Geopoint center; @@ -45,4 +49,59 @@ public class Viewport { public String toString() { return "(" + bottomLeft.toString() + "," + topRight.toString() + ")"; } + + // viewport is defined by center, span and some (10%) reserve on every side + /** + * Check if coordinates are located in a viewport (defined by its center and span + * in each direction). The viewport also includes a 10% extension on each side. + * + * @param centerLat + * the viewport center latitude + * @param centerLon + * the viewport center longitude + * @param spanLat + * the latitude span + * @param spanLon + * the longitude span + * @param coords + * the coordinates to check + * @return true if the coordinates are in the viewport + */ + public static boolean isCacheInViewPort(int centerLat, int centerLon, int spanLat, int spanLon, final Geopoint coords) { + return Math.abs(coords.getLatitudeE6() - centerLat) <= Math.abs(spanLat) * 0.6 && + Math.abs(coords.getLongitudeE6() - centerLon) <= Math.abs(spanLon) * 0.6; + } + + public static boolean isInViewPort(int centerLat1, int centerLon1, int centerLat2, int centerLon2, int spanLat1, int spanLon1, int spanLat2, int spanLon2) { + try { + // expects coordinates in E6 format + final int left1 = centerLat1 - (spanLat1 / 2); + final int right1 = centerLat1 + (spanLat1 / 2); + final int top1 = centerLon1 + (spanLon1 / 2); + final int bottom1 = centerLon1 - (spanLon1 / 2); + + final int left2 = centerLat2 - (spanLat2 / 2); + final int right2 = centerLat2 + (spanLat2 / 2); + final int top2 = centerLon2 + (spanLon2 / 2); + final int bottom2 = centerLon2 - (spanLon2 / 2); + + if (left2 <= left1) { + return false; + } + if (right2 >= right1) { + return false; + } + if (top2 >= top1) { + return false; + } + if (bottom2 <= bottom1) { + return false; + } + + return true; + } catch (Exception e) { + Log.e(Settings.tag, "cgBase.isInViewPort: " + e.toString()); + return false; + } + } } diff --git a/main/src/cgeo/geocaching/maps/CGeoMap.java b/main/src/cgeo/geocaching/maps/CGeoMap.java index 372bad0..35a6f9b 100644 --- a/main/src/cgeo/geocaching/maps/CGeoMap.java +++ b/main/src/cgeo/geocaching/maps/CGeoMap.java @@ -619,7 +619,7 @@ public class CGeoMap extends AbstractMap implements OnMapDragListener, ViewFacto for (cgCache oneCache : cachesProtected) { if (oneCache != null && oneCache.getCoords() != null) { - if (cgBase.isCacheInViewPort(mapCenterLat, mapCenterLon, mapSpanLat, mapSpanLon, oneCache.getCoords()) && !app.isOffline(oneCache.getGeocode(), null)) { + if (Viewport.isCacheInViewPort(mapCenterLat, mapCenterLon, mapSpanLat, mapSpanLon, oneCache.getCoords()) && !app.isOffline(oneCache.getGeocode(), null)) { geocodes.add(oneCache.getGeocode()); } } @@ -927,7 +927,7 @@ public class CGeoMap extends AbstractMap implements OnMapDragListener, ViewFacto } else if (((Math.abs(spanLatitudeNow - spanLatitude) > 50) || (Math.abs(spanLongitudeNow - spanLongitude) > 50) || // changed zoom (Math.abs(centerLatitudeNow - centerLatitude) > (spanLatitudeNow / 4)) || (Math.abs(centerLongitudeNow - centerLongitude) > (spanLongitudeNow / 4)) // map moved ) && (cachesCnt <= 0 || CollectionUtils.isEmpty(caches) - || !cgBase.isInViewPort(centerLatitude, centerLongitude, centerLatitudeNow, centerLongitudeNow, spanLatitude, spanLongitude, spanLatitudeNow, spanLongitudeNow))) { + || !Viewport.isInViewPort(centerLatitude, centerLongitude, centerLatitudeNow, centerLongitudeNow, spanLatitude, spanLongitude, spanLatitudeNow, spanLongitudeNow))) { moved = true; } @@ -1095,7 +1095,7 @@ public class CGeoMap extends AbstractMap implements OnMapDragListener, ViewFacto moved = true; } else if (((Math.abs(spanLatitudeNow - spanLatitudeUsers) > 50) || (Math.abs(spanLongitudeNow - spanLongitudeUsers) > 50) || // changed zoom (Math.abs(centerLatitudeNow - centerLatitudeUsers) > (spanLatitudeNow / 4)) || (Math.abs(centerLongitudeNow - centerLongitudeUsers) > (spanLongitudeNow / 4)) // map moved - ) && !cgBase.isInViewPort(centerLatitudeUsers, centerLongitudeUsers, centerLatitudeNow, centerLongitudeNow, spanLatitudeUsers, spanLongitudeUsers, spanLatitudeNow, spanLongitudeNow)) { + ) && !Viewport.isInViewPort(centerLatitudeUsers, centerLongitudeUsers, centerLatitudeNow, centerLongitudeNow, spanLatitudeUsers, spanLongitudeUsers, spanLatitudeNow, spanLongitudeNow)) { moved = true; } diff --git a/main/src/cgeo/geocaching/network/OAuth.java b/main/src/cgeo/geocaching/network/OAuth.java index 6d82bf7..a5393f6 100644 --- a/main/src/cgeo/geocaching/network/OAuth.java +++ b/main/src/cgeo/geocaching/network/OAuth.java @@ -29,6 +29,6 @@ public class OAuth { final String keysPacked = Settings.getKeyConsumerSecret() + "&" + StringUtils.defaultString(tokenSecret); // both even if empty some of them! final String requestPacked = method + "&" + cgBase.urlencode_rfc3986((https ? "https" : "http") + "://" + host + path) + "&" + cgBase.urlencode_rfc3986(StringUtils.join(paramsEncoded.toArray(), '&')); - params.put("oauth_signature", cgBase.base64Encode(CryptUtils.hashHmac(requestPacked, keysPacked))); + params.put("oauth_signature", CryptUtils.base64Encode(CryptUtils.hashHmac(requestPacked, keysPacked))); } } diff --git a/main/src/cgeo/geocaching/utils/CryptUtils.java b/main/src/cgeo/geocaching/utils/CryptUtils.java index 345af88..907fa1f 100644 --- a/main/src/cgeo/geocaching/utils/CryptUtils.java +++ b/main/src/cgeo/geocaching/utils/CryptUtils.java @@ -13,6 +13,32 @@ import javax.crypto.Mac; import javax.crypto.spec.SecretKeySpec; public final class CryptUtils { + + private static char[] base64map1 = new char[64]; + private static byte[] base64map2 = new byte[128]; + + static { + int i = 0; + for (char c = 'A'; c <= 'Z'; c++) { + base64map1[i++] = c; + } + for (char c = 'a'; c <= 'z'; c++) { + base64map1[i++] = c; + } + for (char c = '0'; c <= '9'; c++) { + base64map1[i++] = c; + } + base64map1[i++] = '+'; + base64map1[i++] = '/'; + + for (i = 0; i < base64map2.length; i++) { + base64map2[i] = -1; + } + for (i = 0; i < 64; i++) { + base64map2[base64map1[i]] = (byte) i; + } + } + public static String rot13(String text) { if (text == null) { return ""; @@ -111,4 +137,74 @@ public final class CryptUtils { return buffer; } + public static byte[] base64Decode(String text) { + char[] in = text.toCharArray(); + + int iLen = in.length; + if (iLen % 4 != 0) { + throw new IllegalArgumentException("Length of Base64 encoded input string is not a multiple of 4."); + } + while (iLen > 0 && in[iLen - 1] == '=') { + iLen--; + } + int oLen = (iLen * 3) / 4; + byte[] out = new byte[oLen]; + int ip = 0; + int op = 0; + while (ip < iLen) { + int i0 = in[ip++]; + int i1 = in[ip++]; + int i2 = ip < iLen ? in[ip++] : 'A'; + int i3 = ip < iLen ? in[ip++] : 'A'; + if (i0 > 127 || i1 > 127 || i2 > 127 || i3 > 127) { + throw new IllegalArgumentException("Illegal character in Base64 encoded data."); + } + int b0 = base64map2[i0]; + int b1 = base64map2[i1]; + int b2 = base64map2[i2]; + int b3 = base64map2[i3]; + if (b0 < 0 || b1 < 0 || b2 < 0 || b3 < 0) { + throw new IllegalArgumentException("Illegal character in Base64 encoded data."); + } + int o0 = (b0 << 2) | (b1 >>> 4); + int o1 = ((b1 & 0xf) << 4) | (b2 >>> 2); + int o2 = ((b2 & 3) << 6) | b3; + out[op++] = (byte) o0; + if (op < oLen) { + out[op++] = (byte) o1; + } + if (op < oLen) { + out[op++] = (byte) o2; + } + } + return out; + } + + public static String base64Encode(byte[] in) { + int iLen = in.length; + int oDataLen = (iLen * 4 + 2) / 3; // output length without padding + int oLen = ((iLen + 2) / 3) * 4; // output length including padding + char[] out = new char[oLen]; + int ip = 0; + int op = 0; + + while (ip < iLen) { + int i0 = in[ip++] & 0xff; + int i1 = ip < iLen ? in[ip++] & 0xff : 0; + int i2 = ip < iLen ? in[ip++] & 0xff : 0; + int o0 = i0 >>> 2; + int o1 = ((i0 & 3) << 4) | (i1 >>> 4); + int o2 = ((i1 & 0xf) << 2) | (i2 >>> 6); + int o3 = i2 & 0x3F; + out[op++] = base64map1[o0]; + out[op++] = base64map1[o1]; + out[op] = op < oDataLen ? base64map1[o2] : '='; + op++; + out[op] = op < oDataLen ? base64map1[o3] : '='; + op++; + } + + return new String(out); + } + } |