diff options
| author | Samuel Tardieu <sam@rfc1149.net> | 2012-04-20 20:16:40 +0200 |
|---|---|---|
| committer | Samuel Tardieu <sam@rfc1149.net> | 2012-04-20 22:14:19 +0200 |
| commit | b9b297a38accdd54641b325ece04f488bbf90fb8 (patch) | |
| tree | 5ab47eb0f713148a86a03d7c14ee2223acbfb396 | |
| parent | c91a817d13a8f68cc5b7399ba4cbe7cd16a4645b (diff) | |
| download | cgeo-b9b297a38accdd54641b325ece04f488bbf90fb8.zip cgeo-b9b297a38accdd54641b325ece04f488bbf90fb8.tar.gz cgeo-b9b297a38accdd54641b325ece04f488bbf90fb8.tar.bz2 | |
Refactoring: cleanup control flow
19 files changed, 241 insertions, 336 deletions
diff --git a/main/src/cgeo/geocaching/AdressListActivity.java b/main/src/cgeo/geocaching/AdressListActivity.java index 57d3cdd..a67f4a2 100644 --- a/main/src/cgeo/geocaching/AdressListActivity.java +++ b/main/src/cgeo/geocaching/AdressListActivity.java @@ -57,18 +57,13 @@ public class AdressListActivity extends AbstractListActivity { @Override protected void onPostExecute(final List<Address> addresses) { waitDialog.dismiss(); - try { - if (CollectionUtils.isEmpty(addresses)) { - showToast(res.getString(R.string.err_search_address_no_match)); - finish(); - return; - } else { - for (Address address : addresses) { - adapter.add(address); // don't use addAll, it's only available with API >= 11 - } + if (CollectionUtils.isNotEmpty(addresses)) { + for (final Address address : addresses) { + adapter.add(address); // don't use addAll, it's only available with API >= 11 } - } catch (Exception e) { - Log.e("AdressListActivity.onPostExecute", e); + } else { + showToast(res.getString(R.string.err_search_address_no_match)); + finish(); } } diff --git a/main/src/cgeo/geocaching/Settings.java b/main/src/cgeo/geocaching/Settings.java index 03427c5..1b9aac4 100644 --- a/main/src/cgeo/geocaching/Settings.java +++ b/main/src/cgeo/geocaching/Settings.java @@ -104,11 +104,10 @@ public final class Settings { static coordInputFormatEnum fromInt(int id) { final coordInputFormatEnum[] values = coordInputFormatEnum.values(); - if (id >= 0 && id < values.length) { - return values[id]; - } else { + if (id < 0 || id >= values.length) { return Min; } + return values[id]; } } @@ -234,11 +233,7 @@ public final class Settings { } public static String getUsername() { - if (null == username) { - return sharedPrefs.getString(KEY_USERNAME, null); - } else { - return username; - } + return username != null ? username : sharedPrefs.getString(KEY_USERNAME, null); } public static boolean setLogin(final String username, final String password) { @@ -928,7 +923,7 @@ public final class Settings { /** * The Threshold for the showing of child waypoints - * + * * @return */ diff --git a/main/src/cgeo/geocaching/StaticMapsActivity.java b/main/src/cgeo/geocaching/StaticMapsActivity.java index fdb6d67..2e5f423 100644 --- a/main/src/cgeo/geocaching/StaticMapsActivity.java +++ b/main/src/cgeo/geocaching/StaticMapsActivity.java @@ -37,26 +37,20 @@ public class StaticMapsActivity extends AbstractActivity { @Override public void handleMessage(Message msg) { + if (waitDialog != null) { + waitDialog.dismiss(); + } try { if (CollectionUtils.isEmpty(maps)) { - if (waitDialog != null) { - waitDialog.dismiss(); - } - if ((waypoint_id != null && Settings.isStoreOfflineWpMaps()) || (waypoint_id == null && Settings.isStoreOfflineMaps())) { - AlertDialog.Builder builder = new AlertDialog.Builder(StaticMapsActivity.this); + final AlertDialog.Builder builder = new AlertDialog.Builder(StaticMapsActivity.this); builder.setMessage(R.string.err_detail_ask_store_map_static).setPositiveButton(android.R.string.yes, dialogClickListener) .setNegativeButton(android.R.string.no, dialogClickListener).show(); } else { showToast(res.getString(R.string.err_detail_not_load_map_static)); finish(); } - return; } else { - if (waitDialog != null) { - waitDialog.dismiss(); - } - if (inflater == null) { inflater = getLayoutInflater(); } @@ -66,7 +60,7 @@ public class StaticMapsActivity extends AbstractActivity { } smapsView.removeAllViews(); - for (Bitmap image : maps) { + for (final Bitmap image : maps) { if (image != null) { final ImageView map = (ImageView) inflater.inflate(R.layout.map_static_item, null); map.setImageBitmap(image); @@ -75,9 +69,6 @@ public class StaticMapsActivity extends AbstractActivity { } } } catch (Exception e) { - if (waitDialog != null) { - waitDialog.dismiss(); - } Log.e("StaticMapsActivity.loadMapsHandler: " + e.toString()); } } diff --git a/main/src/cgeo/geocaching/apps/cache/navi/StaticMapApp.java b/main/src/cgeo/geocaching/apps/cache/navi/StaticMapApp.java index a0eb5a1..db316bd 100644 --- a/main/src/cgeo/geocaching/apps/cache/navi/StaticMapApp.java +++ b/main/src/cgeo/geocaching/apps/cache/navi/StaticMapApp.java @@ -1,5 +1,6 @@ package cgeo.geocaching.apps.cache.navi; +import cgeo.geocaching.ILogable; import cgeo.geocaching.R; import cgeo.geocaching.StaticMapsActivity; import cgeo.geocaching.cgCache; @@ -25,25 +26,20 @@ class StaticMapApp extends AbstractNavigationApp { @Override public boolean invoke(cgGeo geo, Activity activity, cgCache cache, cgWaypoint waypoint, final Geopoint coords) { - - String geocode = null; - if (cache != null && cache.getListId() != 0) { - geocode = cache.getGeocode().toUpperCase(); - } - if (waypoint != null) { - geocode = waypoint.getGeocode().toUpperCase(); - } + final ILogable logable = cache != null && cache.getListId() != 0 ? cache : waypoint; + final String geocode = logable.getGeocode().toUpperCase(); if (geocode == null) { ActivityMixin.showToast(activity, getString(R.string.err_detail_no_map_static)); return true; - } else { - final Intent intent = new Intent(activity, StaticMapsActivity.class); - intent.putExtra("geocode", geocode); - if (waypoint != null) { - intent.putExtra("waypoint", waypoint.getId()); - } - activity.startActivity(intent); - return true; } + + final Intent intent = new Intent(activity, StaticMapsActivity.class); + intent.putExtra("geocode", geocode); + if (waypoint != null) { + intent.putExtra("waypoint", waypoint.getId()); + } + activity.startActivity(intent); + + return true; } } diff --git a/main/src/cgeo/geocaching/cgCache.java b/main/src/cgeo/geocaching/cgCache.java index f95fb2c..9dfaa24 100644 --- a/main/src/cgeo/geocaching/cgCache.java +++ b/main/src/cgeo/geocaching/cgCache.java @@ -394,15 +394,9 @@ public class cgCache implements ICache, IWaypoint { if (StringUtils.isBlank(guid)) { return false; } - Pattern patternOk = Pattern.compile(guid, Pattern.CASE_INSENSITIVE); - Matcher matcherOk = patternOk.matcher(page); - if (matcherOk.find()) { - Log.i("cgCache.isGuidContainedInPage: guid '" + guid + "' found"); - return true; - } else { - Log.i("cgCache.isGuidContainedInPage: guid '" + guid + "' not found"); - return false; - } + final Boolean found = Pattern.compile(guid, Pattern.CASE_INSENSITIVE).matcher(page).find(); + Log.i("cgCache.isGuidContainedInPage: guid '" + guid + "' " + (found ? "" : "not ") + "found"); + return found; } public boolean isEventCache() { diff --git a/main/src/cgeo/geocaching/cgData.java b/main/src/cgeo/geocaching/cgData.java index 8045279..bb8afc9 100644 --- a/main/src/cgeo/geocaching/cgData.java +++ b/main/src/cgeo/geocaching/cgData.java @@ -372,22 +372,18 @@ public class cgData { final boolean backupDone = LocalStorage.copy(new File(path), target); init(); - if (backupDone) { - Log.i("Database was copied to " + target); - return target.getPath(); - } else { + if (!backupDone) { Log.e("Database could not be copied to " + target); return null; } + + Log.i("Database was copied to " + target); + return target.getPath(); } public static File isRestoreFile() { final File fileSourceFile = backupFile(); - if (fileSourceFile.exists()) { - return fileSourceFile; - } else { - return null; - } + return fileSourceFile.exists() ? fileSourceFile : null; } public boolean restoreDatabase() { @@ -1594,68 +1590,68 @@ public class cgData { null); try { - if (cursor.moveToFirst()) { - final Set<cgCache> caches = new HashSet<cgCache>(); - do { - //Extracted Method = LOADDBMINIMAL - cgCache cache = cgData.createCacheFromDatabaseContent(cursor); + if (!cursor.moveToFirst()) { + return Collections.emptySet(); + } - if (loadFlags.contains(LoadFlag.LOAD_ATTRIBUTES)) { - cache.setAttributes(loadAttributes(cache.getGeocode())); - } + final Set<cgCache> caches = new HashSet<cgCache>(); + do { + //Extracted Method = LOADDBMINIMAL + cgCache cache = cgData.createCacheFromDatabaseContent(cursor); - if (loadFlags.contains(LoadFlag.LOAD_WAYPOINTS)) { - final List<cgWaypoint> waypoints = loadWaypoints(cache.getGeocode()); - if (CollectionUtils.isNotEmpty(waypoints)) { - cache.setWaypoints(waypoints, false); - } + if (loadFlags.contains(LoadFlag.LOAD_ATTRIBUTES)) { + cache.setAttributes(loadAttributes(cache.getGeocode())); + } + + if (loadFlags.contains(LoadFlag.LOAD_WAYPOINTS)) { + final List<cgWaypoint> waypoints = loadWaypoints(cache.getGeocode()); + if (CollectionUtils.isNotEmpty(waypoints)) { + cache.setWaypoints(waypoints, false); } + } - if (loadFlags.contains(LoadFlag.LOAD_SPOILERS)) { - final List<cgImage> spoilers = loadSpoilers(cache.getGeocode()); - if (CollectionUtils.isNotEmpty(spoilers)) { - if (cache.getSpoilers() == null) { - cache.setSpoilers(new ArrayList<cgImage>()); - } else { - cache.getSpoilers().clear(); - } - cache.getSpoilers().addAll(spoilers); + if (loadFlags.contains(LoadFlag.LOAD_SPOILERS)) { + final List<cgImage> spoilers = loadSpoilers(cache.getGeocode()); + if (CollectionUtils.isNotEmpty(spoilers)) { + if (cache.getSpoilers() == null) { + cache.setSpoilers(new ArrayList<cgImage>()); + } else { + cache.getSpoilers().clear(); } + cache.getSpoilers().addAll(spoilers); } + } - if (loadFlags.contains(LoadFlag.LOAD_LOGS)) { - cache.setLogs(loadLogs(cache.getGeocode())); - final Map<LogType, Integer> logCounts = loadLogCounts(cache.getGeocode()); - if (MapUtils.isNotEmpty(logCounts)) { - cache.getLogCounts().clear(); - cache.getLogCounts().putAll(logCounts); - } + if (loadFlags.contains(LoadFlag.LOAD_LOGS)) { + cache.setLogs(loadLogs(cache.getGeocode())); + final Map<LogType, Integer> logCounts = loadLogCounts(cache.getGeocode()); + if (MapUtils.isNotEmpty(logCounts)) { + cache.getLogCounts().clear(); + cache.getLogCounts().putAll(logCounts); } + } - if (loadFlags.contains(LoadFlag.LOAD_INVENTORY)) { - final List<cgTrackable> inventory = loadInventory(cache.getGeocode()); - if (CollectionUtils.isNotEmpty(inventory)) { - if (cache.getInventory() == null) { - cache.setInventory(new ArrayList<cgTrackable>()); - } else { - cache.getInventory().clear(); - } - cache.getInventory().addAll(inventory); + if (loadFlags.contains(LoadFlag.LOAD_INVENTORY)) { + final List<cgTrackable> inventory = loadInventory(cache.getGeocode()); + if (CollectionUtils.isNotEmpty(inventory)) { + if (cache.getInventory() == null) { + cache.setInventory(new ArrayList<cgTrackable>()); + } else { + cache.getInventory().clear(); } + cache.getInventory().addAll(inventory); } + } - if (loadFlags.contains(LoadFlag.LOAD_OFFLINE_LOG)) { - cache.setLogOffline(hasLogOffline(cache.getGeocode())); - } - cache.addStorageLocation(StorageLocation.DATABASE); - cacheCache.putCacheInCache(cache); + if (loadFlags.contains(LoadFlag.LOAD_OFFLINE_LOG)) { + cache.setLogOffline(hasLogOffline(cache.getGeocode())); + } + cache.addStorageLocation(StorageLocation.DATABASE); + cacheCache.putCacheInCache(cache); - caches.add(cache); - } while (cursor.moveToNext()); - return caches; - } else { - return Collections.emptySet(); - } + caches.add(cache); + } while (cursor.moveToNext()); + return caches; } finally { cursor.close(); } @@ -2884,11 +2880,7 @@ public class cgData { databaseRW.endTransaction(); } - if (id < 0) { - return -1; - } else { - return (id + customListIdOffset); - } + return id >= 0 ? id + customListIdOffset : -1; } /** diff --git a/main/src/cgeo/geocaching/cgeoadvsearch.java b/main/src/cgeo/geocaching/cgeoadvsearch.java index 194fdbb..2c1d5f8 100644 --- a/main/src/cgeo/geocaching/cgeoadvsearch.java +++ b/main/src/cgeo/geocaching/cgeoadvsearch.java @@ -121,32 +121,25 @@ public class cgeoadvsearch extends AbstractActivity { * @return true if a search was performed, else false */ private boolean instantSearch(final String query, final boolean keywordSearch) { - try { - String result = BaseUtils.getMatch(query, GCConstants.PATTERN_GC_CODE, true, 0, "", false); - if (StringUtils.isNotBlank(result)) { - final Intent cachesIntent = new Intent(this, CacheDetailActivity.class); - cachesIntent.putExtra("geocode", result.toUpperCase()); - startActivity(cachesIntent); - - return true; - } else { - result = BaseUtils.getMatch(query, GCConstants.PATTERN_TB_CODE, true, 0, "", false); - if (StringUtils.isNotBlank(result)) { - final Intent trackablesIntent = new Intent(this, cgeotrackable.class); - trackablesIntent.putExtra("geocode", result.toUpperCase()); - startActivity(trackablesIntent); - - return true; - } else if (keywordSearch) { // keyword fallback, if desired by caller - cgeocaches.startActivityKeyword(this, query.trim()); - return true; - } else { - return false; - } + final String geocode = BaseUtils.getMatch(query, GCConstants.PATTERN_GC_CODE, true, 0, "", false); + if (StringUtils.isNotBlank(geocode)) { + final Intent cachesIntent = new Intent(this, CacheDetailActivity.class); + cachesIntent.putExtra("geocode", geocode.toUpperCase()); + startActivity(cachesIntent); + return true; + } - } - } catch (Exception e) { - Log.w("cgeoadvsearch.instantSearch: " + e.toString()); + final String trackable = BaseUtils.getMatch(query, GCConstants.PATTERN_TB_CODE, true, 0, "", false); + if (StringUtils.isNotBlank(trackable)) { + final Intent trackablesIntent = new Intent(this, cgeotrackable.class); + trackablesIntent.putExtra("geocode", trackable.toUpperCase()); + startActivity(trackablesIntent); + return true; + } + + if (keywordSearch) { // keyword fallback, if desired by caller + cgeocaches.startActivityKeyword(this, query.trim()); + return true; } return false; diff --git a/main/src/cgeo/geocaching/cgeonavigate.java b/main/src/cgeo/geocaching/cgeonavigate.java index 869b9dd..5f4a7f4 100644 --- a/main/src/cgeo/geocaching/cgeonavigate.java +++ b/main/src/cgeo/geocaching/cgeonavigate.java @@ -197,42 +197,27 @@ public class cgeonavigate extends AbstractActivity { } @Override - public boolean onCreateOptionsMenu(Menu menu) { - if (Settings.isUseCompass()) { - menu.add(0, MENU_SWITCH_COMPASS_GPS, 0, res.getString(R.string.use_gps)).setIcon(android.R.drawable.ic_menu_compass); - } else { - menu.add(0, MENU_SWITCH_COMPASS_GPS, 0, res.getString(R.string.use_compass)).setIcon(android.R.drawable.ic_menu_compass); - } + public boolean onCreateOptionsMenu(final Menu menu) { + menu.add(0, MENU_SWITCH_COMPASS_GPS, 0, res.getString(Settings.isUseCompass() ? R.string.use_gps : R.string.use_compass)).setIcon(android.R.drawable.ic_menu_compass); menu.add(0, MENU_MAP, 0, res.getString(R.string.caches_on_map)).setIcon(android.R.drawable.ic_menu_mapmode); menu.add(0, 2, 0, res.getString(R.string.destination_set)).setIcon(android.R.drawable.ic_menu_edit); if (coordinates.size() > 1) { - SubMenu subMenu = menu.addSubMenu(0, 3, 0, res.getString(R.string.destination_select)).setIcon(android.R.drawable.ic_menu_myplaces); - + final SubMenu subMenu = menu.addSubMenu(0, 3, 0, res.getString(R.string.destination_select)).setIcon(android.R.drawable.ic_menu_myplaces); int cnt = 4; - for (IWaypoint coordinate : coordinates) { + for (final IWaypoint coordinate : coordinates) { subMenu.add(0, cnt, 0, coordinate.getName() + " (" + coordinate.getCoordType() + ")"); cnt++; } - - return true; } else { menu.add(0, 3, 0, res.getString(R.string.destination_select)).setIcon(android.R.drawable.ic_menu_myplaces).setEnabled(false); - - return true; } + return true; } @Override public boolean onPrepareOptionsMenu(Menu menu) { super.onPrepareOptionsMenu(menu); - - MenuItem item = menu.findItem(MENU_SWITCH_COMPASS_GPS); - if (Settings.isUseCompass()) { - item.setTitle(res.getString(R.string.use_gps)); - } else { - item.setTitle(res.getString(R.string.use_compass)); - } - + menu.findItem(MENU_SWITCH_COMPASS_GPS).setTitle(res.getString(Settings.isUseCompass() ? R.string.use_gps : R.string.use_compass)); return true; } diff --git a/main/src/cgeo/geocaching/cgeowaypoint.java b/main/src/cgeo/geocaching/cgeowaypoint.java index 2c14601..a6a4363 100644 --- a/main/src/cgeo/geocaching/cgeowaypoint.java +++ b/main/src/cgeo/geocaching/cgeowaypoint.java @@ -42,77 +42,63 @@ public class cgeowaypoint extends AbstractActivity { @Override public void handleMessage(Message msg) { - try { - if (waypoint == null) { - if (waitDialog != null) { - waitDialog.dismiss(); - waitDialog = null; - } + if (waitDialog != null) { + waitDialog.dismiss(); + waitDialog = null; + } - showToast(res.getString(R.string.err_waypoint_load_failed)); + if (waypoint == null) { + showToast(res.getString(R.string.err_waypoint_load_failed)); + finish(); + return; + } - finish(); - return; - } else { - final TextView identification = (TextView) findViewById(R.id.identification); - final TextView coords = (TextView) findViewById(R.id.coordinates); - final ImageView defaultNavigation = (ImageView) findViewById(R.id.defaultNavigation); - final View separator = findViewById(R.id.separator); + final TextView identification = (TextView) findViewById(R.id.identification); + final TextView coords = (TextView) findViewById(R.id.coordinates); + final ImageView defaultNavigation = (ImageView) findViewById(R.id.defaultNavigation); + final View separator = findViewById(R.id.separator); - final View headline = findViewById(R.id.headline); - registerNavigationMenu(headline); + final View headline = findViewById(R.id.headline); + registerNavigationMenu(headline); - if (StringUtils.isNotBlank(waypoint.getName())) { - setTitle(Html.fromHtml(waypoint.getName()).toString()); - } else { - setTitle(res.getString(R.string.waypoint_title)); - } + if (StringUtils.isNotBlank(waypoint.getName())) { + setTitle(Html.fromHtml(waypoint.getName()).toString()); + } else { + setTitle(res.getString(R.string.waypoint_title)); + } - if (!waypoint.getPrefix().equalsIgnoreCase("OWN")) { - identification.setText(waypoint.getPrefix() + "/" + waypoint.getLookup()); - } else { - identification.setText(res.getString(R.string.waypoint_custom)); - } - registerNavigationMenu(identification); - waypoint.setIcon(res, identification); - - if (waypoint.getCoords() != null) { - coords.setText(Html.fromHtml(waypoint.getCoords().toString()), TextView.BufferType.SPANNABLE); - defaultNavigation.setVisibility(View.VISIBLE); - separator.setVisibility(View.VISIBLE); - } else { - coords.setText(res.getString(R.string.waypoint_unknown_coordinates)); - defaultNavigation.setVisibility(View.GONE); - separator.setVisibility(View.GONE); - } - registerNavigationMenu(coords); + if (!waypoint.getPrefix().equalsIgnoreCase("OWN")) { + identification.setText(waypoint.getPrefix() + "/" + waypoint.getLookup()); + } else { + identification.setText(res.getString(R.string.waypoint_custom)); + } + registerNavigationMenu(identification); + waypoint.setIcon(res, identification); - if (StringUtils.isNotBlank(waypoint.getNote())) { - final TextView note = (TextView) findViewById(R.id.note); - note.setText(Html.fromHtml(waypoint.getNote()), TextView.BufferType.SPANNABLE); - registerNavigationMenu(note); - } + if (waypoint.getCoords() != null) { + coords.setText(Html.fromHtml(waypoint.getCoords().toString()), TextView.BufferType.SPANNABLE); + defaultNavigation.setVisibility(View.VISIBLE); + separator.setVisibility(View.VISIBLE); + } else { + coords.setText(res.getString(R.string.waypoint_unknown_coordinates)); + defaultNavigation.setVisibility(View.GONE); + separator.setVisibility(View.GONE); + } + registerNavigationMenu(coords); - Button buttonEdit = (Button) findViewById(R.id.edit); - buttonEdit.setOnClickListener(new editWaypointListener()); + if (StringUtils.isNotBlank(waypoint.getNote())) { + final TextView note = (TextView) findViewById(R.id.note); + note.setText(Html.fromHtml(waypoint.getNote()), TextView.BufferType.SPANNABLE); + registerNavigationMenu(note); + } - Button buttonDelete = (Button) findViewById(R.id.delete); - if (waypoint.isUserDefined()) { - buttonDelete.setOnClickListener(new deleteWaypointListener()); - buttonDelete.setVisibility(View.VISIBLE); - } + final Button buttonEdit = (Button) findViewById(R.id.edit); + buttonEdit.setOnClickListener(new editWaypointListener()); - if (waitDialog != null) { - waitDialog.dismiss(); - waitDialog = null; - } - } - } catch (Exception e) { - if (waitDialog != null) { - waitDialog.dismiss(); - waitDialog = null; - } - Log.e("cgeowaypoint.loadWaypointHandler: " + e.toString()); + if (waypoint.isUserDefined()) { + final Button buttonDelete = (Button) findViewById(R.id.delete); + buttonDelete.setOnClickListener(new deleteWaypointListener()); + buttonDelete.setVisibility(View.VISIBLE); } } @@ -329,7 +315,6 @@ public class cgeowaypoint extends AbstractActivity { StaticMapsProvider.removeWpStaticMaps(id, geocode); finish(); - return; } else { showToast(res.getString(R.string.err_waypoint_delete_failed)); } diff --git a/main/src/cgeo/geocaching/cgeowaypointadd.java b/main/src/cgeo/geocaching/cgeowaypointadd.java index 196a8b3..47cd64a 100644 --- a/main/src/cgeo/geocaching/cgeowaypointadd.java +++ b/main/src/cgeo/geocaching/cgeowaypointadd.java @@ -438,7 +438,6 @@ public class cgeowaypointadd extends AbstractActivity { StaticMapsProvider.storeWaypointStaticMap(cache, cgeowaypointadd.this, waypoint, false); } finish(); - return; } else { showToast(res.getString(R.string.err_waypoint_add_failed)); } diff --git a/main/src/cgeo/geocaching/connector/gc/GCParser.java b/main/src/cgeo/geocaching/connector/gc/GCParser.java index cf7c762..7143058 100644 --- a/main/src/cgeo/geocaching/connector/gc/GCParser.java +++ b/main/src/cgeo/geocaching/connector/gc/GCParser.java @@ -186,15 +186,11 @@ public abstract class GCParser { if (StringUtils.isNotBlank(inventoryPre)) { final Matcher matcherTbsInside = GCConstants.PATTERN_SEARCH_TRACKABLESINSIDE.matcher(inventoryPre); while (matcherTbsInside.find()) { - if (matcherTbsInside.groupCount() == 2 && matcherTbsInside.group(2) != null) { - final String inventoryItem = matcherTbsInside.group(2).toLowerCase(); - if (inventoryItem.equals("premium member only cache")) { - continue; - } else { - if (cache.getInventoryItems() <= 0) { - cache.setInventoryItems(1); - } - } + if (matcherTbsInside.groupCount() == 2 && + matcherTbsInside.group(2) != null && + !matcherTbsInside.group(2).equalsIgnoreCase("premium member only cache") && + cache.getInventoryItems() <= 0) { + cache.setInventoryItems(1); } } } diff --git a/main/src/cgeo/geocaching/connector/gc/Login.java b/main/src/cgeo/geocaching/connector/gc/Login.java index 1e614df..24f916a 100644 --- a/main/src/cgeo/geocaching/connector/gc/Login.java +++ b/main/src/cgeo/geocaching/connector/gc/Login.java @@ -106,28 +106,28 @@ public abstract class Login { loginResponse = Network.postRequest("https://www.geocaching.com/login/default.aspx", params); loginData = Network.getResponseData(loginResponse); - if (StringUtils.isNotBlank(loginData)) { - if (Login.getLoginStatus(loginData)) { - Log.i("Successfully logged in Geocaching.com as " + login.left); - - Login.switchToEnglish(loginData); - Settings.setCookieStore(Cookies.dumpCookieStore()); - - return StatusCode.NO_ERROR; // logged in - } else { - if (loginData.contains("Your username/password combination does not match.")) { - Log.i("Failed to log in Geocaching.com as " + login.left + " because of wrong username/password"); - return StatusCode.WRONG_LOGIN_DATA; // wrong login - } else { - Log.i("Failed to log in Geocaching.com as " + login.left + " for some unknown reason"); - return StatusCode.UNKNOWN_ERROR; // can't login - } - } - } else { + if (StringUtils.isBlank(loginData)) { Log.e("cgeoBase.login: Failed to retrieve login page (2nd)"); // FIXME: should it be CONNECTION_FAILED to match the first attempt? return StatusCode.COMMUNICATION_ERROR; // no login page } + + if (Login.getLoginStatus(loginData)) { + Log.i("Successfully logged in Geocaching.com as " + login.left); + + Login.switchToEnglish(loginData); + Settings.setCookieStore(Cookies.dumpCookieStore()); + + return StatusCode.NO_ERROR; // logged in + } + + if (loginData.contains("Your username/password combination does not match.")) { + Log.i("Failed to log in Geocaching.com as " + login.left + " because of wrong username/password"); + return StatusCode.WRONG_LOGIN_DATA; // wrong login + } + + Log.i("Failed to log in Geocaching.com as " + login.left + " for some unknown reason"); + return StatusCode.UNKNOWN_ERROR; // can't login } public static StatusCode logout() { @@ -421,13 +421,15 @@ public abstract class Login { public static String getRequestLogged(final String uri, final Parameters params) { final String data = Network.getResponseData(Network.getRequest(uri, params)); - if (!getLoginStatus(data)) { - if (login() == StatusCode.NO_ERROR) { - return Network.getResponseData(Network.getRequest(uri, params)); - } else { - Log.i("Working as guest."); - } + if (getLoginStatus(data)) { + return data; } + + if (login() == StatusCode.NO_ERROR) { + return Network.getResponseData(Network.getRequest(uri, params)); + } + + Log.w("Working as guest."); return data; } diff --git a/main/src/cgeo/geocaching/files/FileList.java b/main/src/cgeo/geocaching/files/FileList.java index 576aa2c..48a98a7 100644 --- a/main/src/cgeo/geocaching/files/FileList.java +++ b/main/src/cgeo/geocaching/files/FileList.java @@ -63,30 +63,14 @@ public abstract class FileList<T extends ArrayAdapter<File>> extends AbstractLis @Override public void handleMessage(Message msg) { - try { - if (CollectionUtils.isEmpty(files)) { - if (waitDialog != null) { - waitDialog.dismiss(); - } - - showToast(res.getString(R.string.file_list_no_files)); - - finish(); - return; - } else { - if (adapter != null) { - adapter.notifyDataSetChanged(); - } - } - - if (waitDialog != null) { - waitDialog.dismiss(); - } - } catch (Exception e) { - if (waitDialog != null) { - waitDialog.dismiss(); - } - Log.e("cgFileList.loadFilesHandler: " + e.toString()); + if (waitDialog != null) { + waitDialog.dismiss(); + } + if (CollectionUtils.isEmpty(files)) { + showToast(res.getString(R.string.file_list_no_files)); + finish(); + } else if (adapter != null) { + adapter.notifyDataSetChanged(); } } }; diff --git a/main/src/cgeo/geocaching/geopoint/GeopointParser.java b/main/src/cgeo/geocaching/geopoint/GeopointParser.java index ad5d6ba..3c72f1e 100644 --- a/main/src/cgeo/geocaching/geopoint/GeopointParser.java +++ b/main/src/cgeo/geocaching/geopoint/GeopointParser.java @@ -129,19 +129,18 @@ public class GeopointParser return new ResultWrapper(sign * (degree + minutes / 60.0 + seconds / 3600.0), matcher.start(), matcher.group().length()); - } else { - - // Nothing found with "N 52...", try to match string as decimaldegree - try { - final String[] items = StringUtils.split(text.trim()); - if (items.length > 0) { - final int index = (latlon == LatLon.LON ? items.length - 1 : 0); - final int pos = (latlon == LatLon.LON ? text.lastIndexOf(items[index]) : text.indexOf(items[index])); - return new ResultWrapper(Double.parseDouble(items[index]), pos, items[index].length()); - } - } catch (NumberFormatException e) { - // The right exception will be raised below. + } + + // Nothing found with "N 52...", try to match string as decimaldegree + try { + final String[] items = StringUtils.split(text.trim()); + if (items.length > 0) { + final int index = (latlon == LatLon.LON ? items.length - 1 : 0); + final int pos = (latlon == LatLon.LON ? text.lastIndexOf(items[index]) : text.indexOf(items[index])); + return new ResultWrapper(Double.parseDouble(items[index]), pos, items[index].length()); } + } catch (NumberFormatException e) { + // The right exception will be raised below. } throw new ParseException("Could not parse coordinates as " + latlon + ": \"" + text + "\"", latlon); diff --git a/main/src/cgeo/geocaching/geopoint/Viewport.java b/main/src/cgeo/geocaching/geopoint/Viewport.java index 4f70291..1d082fc 100644 --- a/main/src/cgeo/geocaching/geopoint/Viewport.java +++ b/main/src/cgeo/geocaching/geopoint/Viewport.java @@ -128,16 +128,16 @@ public class Viewport { public Viewport expand(final ICoordinates point) { if (contains(point)) { return this; - } else { - final Geopoint coords = point.getCoords(); - final double latitude = coords.getLatitude(); - final double longitude = coords.getLongitude(); - final double latMin = Math.min(getLatitudeMin(), latitude); - final double latMax = Math.max(getLatitudeMax(), latitude); - final double lonMin = Math.min(getLongitudeMin(), longitude); - final double lonMax = Math.max(getLongitudeMax(), longitude); - return new Viewport(new Geopoint(latMin, lonMin), new Geopoint(latMax, lonMax)); } + + final Geopoint coords = point.getCoords(); + final double latitude = coords.getLatitude(); + final double longitude = coords.getLongitude(); + final double latMin = Math.min(getLatitudeMin(), latitude); + final double latMax = Math.max(getLatitudeMax(), latitude); + final double lonMin = Math.min(getLongitudeMin(), longitude); + final double lonMax = Math.max(getLongitudeMax(), longitude); + return new Viewport(new Geopoint(latMin, lonMin), new Geopoint(latMax, lonMax)); } /** diff --git a/main/src/cgeo/geocaching/maps/CGeoMap.java b/main/src/cgeo/geocaching/maps/CGeoMap.java index 7c70266..42f12a9 100644 --- a/main/src/cgeo/geocaching/maps/CGeoMap.java +++ b/main/src/cgeo/geocaching/maps/CGeoMap.java @@ -876,17 +876,17 @@ public class CGeoMap extends AbstractMap implements OnMapDragListener, ViewFacto * @return the current map state as an array of int, or null if no map state is available */ private int[] currentMapState() { - if (mapView != null) { - int[] mapState = new int[4]; - GeoPointImpl mapCenter = mapView.getMapViewCenter(); - mapState[0] = mapCenter.getLatitudeE6(); - mapState[1] = mapCenter.getLongitudeE6(); - mapState[2] = mapView.getMapZoomLevel(); - mapState[3] = followMyLocation ? 1 : 0; - return mapState; - } else { + if (mapView == null) { return mapStateIntent; } + + final GeoPointImpl mapCenter = mapView.getMapViewCenter(); + return new int[] { + mapCenter.getLatitudeE6(), + mapCenter.getLongitudeE6(), + mapView.getMapZoomLevel(), + followMyLocation ? 1 : 0 + }; } private void savePrefs() { diff --git a/main/src/cgeo/geocaching/network/Network.java b/main/src/cgeo/geocaching/network/Network.java index bb5a949..8d79aed 100644 --- a/main/src/cgeo/geocaching/network/Network.java +++ b/main/src/cgeo/geocaching/network/Network.java @@ -203,17 +203,20 @@ public abstract class Network { } private static Parameters cacheHeaders(final File cacheFile) { - if (cacheFile != null && cacheFile.exists()) { - final String etag = LocalStorage.getSavedHeader(cacheFile, "etag"); - if (etag != null) { - return new Parameters("If-None-Match", etag); - } else { - final String lastModified = LocalStorage.getSavedHeader(cacheFile, "last-modified"); - if (lastModified != null) { - return new Parameters("If-Modified-Since", lastModified); - } - } + if (cacheFile == null || !cacheFile.exists()) { + return null; + } + + final String etag = LocalStorage.getSavedHeader(cacheFile, "etag"); + if (etag != null) { + return new Parameters("If-None-Match", etag); + } + + final String lastModified = LocalStorage.getSavedHeader(cacheFile, "last-modified"); + if (lastModified != null) { + return new Parameters("If-Modified-Since", lastModified); } + return null; } diff --git a/main/src/cgeo/geocaching/network/Parameters.java b/main/src/cgeo/geocaching/network/Parameters.java index 5c0328a..9cf51d2 100644 --- a/main/src/cgeo/geocaching/network/Parameters.java +++ b/main/src/cgeo/geocaching/network/Parameters.java @@ -82,11 +82,7 @@ public class Parameters extends ArrayList<NameValuePair> { * @return the object itself if it is non-null, a new one otherwise */ public static Parameters extend(final Parameters params, final String... keyValues) { - if (params == null) { - return new Parameters(keyValues); - } else { - return params.put(keyValues); - } + return params == null ? new Parameters(keyValues) : params.put(keyValues); } /** diff --git a/main/src/cgeo/geocaching/utils/LeastRecentlyUsedMap.java b/main/src/cgeo/geocaching/utils/LeastRecentlyUsedMap.java index 5628a4b..953fc47 100644 --- a/main/src/cgeo/geocaching/utils/LeastRecentlyUsedMap.java +++ b/main/src/cgeo/geocaching/utils/LeastRecentlyUsedMap.java @@ -54,9 +54,9 @@ public abstract class LeastRecentlyUsedMap<K, V> extends LinkedHashMap<K, V> { final V oldVal = super.remove(key); put(key, value); return oldVal; - } else { - return super.put(key, value); } + + return super.put(key, value); } @Override |
