Skip to content

Commit

Permalink
new: menu item to open geo checker
Browse files Browse the repository at this point in the history
  • Loading branch information
Bananeweizen committed Feb 21, 2015
1 parent 5d001f8 commit 0f28169
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 1 deletion.
8 changes: 7 additions & 1 deletion main/res/menu/cache_options.xml
Expand Up @@ -83,5 +83,11 @@
app:actionProviderClass="android.support.v7.widget.ShareActionProvider"
app:showAsAction="ifRoom">
</item>

<item
android:id="@+id/menu_checker"
android:title="@string/cache_menu_checker"
android:visible="false"
app:showAsAction="ifRoom">
</item>

</menu>
1 change: 1 addition & 0 deletions main/res/values/changelog_master.xml
Expand Up @@ -14,6 +14,7 @@
· New: Allow export of waypoints without coords to Locus\n
· New: Confirmation on backup/restore\n
· New: Links in personal notes can be clicked\n
· New: Menu in cache details to open geo checker\n
· Fix: Improve detection pattern for event start time\n
· Fix: Don\'t allow logs in the future\n
· Fix: Allow shortcut creation for All caches list\n
Expand Down
1 change: 1 addition & 0 deletions main/res/values/strings.xml
Expand Up @@ -731,6 +731,7 @@
<string name="cache_menu_pebble">Pebble</string>
<string name="cache_menu_android_wear">Android Wear</string>
<string name="cache_menu_vote">Vote</string>
<string name="cache_menu_checker">Open geo checker</string>
<string name="cache_status">Status</string>
<string name="cache_status_offline_log">Saved Log</string>
<string name="cache_status_found">Found</string>
Expand Down
5 changes: 5 additions & 0 deletions main/src/cgeo/geocaching/CacheDetailActivity.java
Expand Up @@ -44,6 +44,7 @@
import cgeo.geocaching.ui.WeakReferenceHandler;
import cgeo.geocaching.ui.dialog.Dialogs;
import cgeo.geocaching.ui.logs.CacheLogsViewCreator;
import cgeo.geocaching.utils.CheckerUtils;
import cgeo.geocaching.utils.CryptUtils;
import cgeo.geocaching.utils.Formatter;
import cgeo.geocaching.utils.ImageUtils;
Expand Down Expand Up @@ -498,6 +499,7 @@ public boolean onPrepareOptionsMenu(final Menu menu) {
menu.findItem(R.id.menu_delete).setVisible(cache != null && cache.isOffline());
menu.findItem(R.id.menu_refresh).setVisible(cache != null && cache.isOffline());
menu.findItem(R.id.menu_gcvote).setVisible(cache != null && GCVote.isVotingPossible(cache));
menu.findItem(R.id.menu_checker).setVisible(cache != null && StringUtils.isNotEmpty(CheckerUtils.getCheckerUrl(cache)));
return super.onPrepareOptionsMenu(menu);
}

Expand All @@ -522,6 +524,9 @@ public boolean onOptionsItemSelected(final MenuItem item) {
case R.id.menu_gcvote:
showVoteDialog();
return true;
case R.id.menu_checker:
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(CheckerUtils.getCheckerUrl(cache))));
return true;
default:
if (NavigationAppFactory.onMenuItemSelected(item, this, cache)) {
return true;
Expand Down
34 changes: 34 additions & 0 deletions main/src/cgeo/geocaching/utils/CheckerUtils.java
@@ -0,0 +1,34 @@
package cgeo.geocaching.utils;

import cgeo.geocaching.Geocache;

import org.apache.commons.lang3.StringUtils;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;

import android.util.Patterns;

import java.util.regex.Matcher;

public final class CheckerUtils {
private static final String[] CHECKERS = new String[] { "geocheck.org", "geochecker.com", "certitudes.org" };

private CheckerUtils() {
// utility class
}

@Nullable
public static String getCheckerUrl(@NonNull final Geocache cache) {
final String description = cache.getDescription();
final Matcher matcher = Patterns.WEB_URL.matcher(description);
while (matcher.find()) {
final String url = matcher.group();
for (final String checker : CHECKERS) {
if (StringUtils.containsIgnoreCase(url, checker)) {
return url;
}
}
}
return null;
}
}
24 changes: 24 additions & 0 deletions tests/src/cgeo/geocaching/utils/CheckerUtilsTest.java
@@ -0,0 +1,24 @@
package cgeo.geocaching.utils;

import static org.assertj.core.api.Assertions.assertThat;

import cgeo.geocaching.Geocache;

import junit.framework.TestCase;

public class CheckerUtilsTest extends TestCase {

public static void testGetCheckerUrl() throws Exception {
assertUrl("<p style=\"text-align:center;\"><a href=\"http://geocheck.org/geo_inputchkcoord.php?gid=618932716cc7e68-c4bb-4f41-8bb1-3e0a3e374a1f\" target=\"_blank\"><img", "http://geocheck.org/geo_inputchkcoord.php?gid=618932716cc7e68-c4bb-4f41-8bb1-3e0a3e374a1f");
assertUrl("<p style=\"text-align:center;\"><a href=\"http://google.com/geo_inputchkcoord.php?gid=618932716cc7e68-c4bb-4f41-8bb1-3e0a3e374a1f\" target=\"_blank\"><img", null);
assertUrl("http://www.certitudes.org/certitude?wp=GC5MVX7", "http://www.certitudes.org/certitude?wp=GC5MVX7");
assertUrl("http://geochecker.com/index.php?code=e001928e3c2682ec2bae0f24b9d02cfb&action=check&wp=474350573454&name=47656f636865636b6572205465737420666f72204e33382030302e303030205737362030302e303030", "http://geochecker.com/index.php?code=e001928e3c2682ec2bae0f24b9d02cfb&action=check&wp=474350573454&name=47656f636865636b6572205465737420666f72204e33382030302e303030205737362030302e303030");
}

private static void assertUrl(final String description, final String expected) {
final Geocache geocache = new Geocache();
geocache.setDescription(description);
assertThat(CheckerUtils.getCheckerUrl(geocache)).isEqualTo(expected);
}

}

0 comments on commit 0f28169

Please sign in to comment.