aboutsummaryrefslogtreecommitdiffstats
path: root/main/src
diff options
context:
space:
mode:
authorSamuel Tardieu <sam@rfc1149.net>2013-08-25 14:18:19 +0200
committerSamuel Tardieu <sam@rfc1149.net>2013-08-25 14:18:19 +0200
commit8c5a5569f8bb8dcebc2e12528aa4574ccf9e26d4 (patch)
tree733790dcc1c7517a5bcdc7a61a5e49d7343a51d1 /main/src
parent55844edaf8351d0ca3134213e997428402174513 (diff)
downloadcgeo-8c5a5569f8bb8dcebc2e12528aa4574ccf9e26d4.zip
cgeo-8c5a5569f8bb8dcebc2e12528aa4574ccf9e26d4.tar.gz
cgeo-8c5a5569f8bb8dcebc2e12528aa4574ccf9e26d4.tar.bz2
fix #3186: do not try to install a null map source
If the wanted map source can no longer be found (because it has been made unavaiable on the device), try to install a default one, or fail silently with an error message in the log.
Diffstat (limited to 'main/src')
-rw-r--r--main/src/cgeo/geocaching/maps/MapProviderFactory.java15
-rw-r--r--main/src/cgeo/geocaching/settings/SettingsActivity.java14
2 files changed, 28 insertions, 1 deletions
diff --git a/main/src/cgeo/geocaching/maps/MapProviderFactory.java b/main/src/cgeo/geocaching/maps/MapProviderFactory.java
index 84c9329..a03fc47 100644
--- a/main/src/cgeo/geocaching/maps/MapProviderFactory.java
+++ b/main/src/cgeo/geocaching/maps/MapProviderFactory.java
@@ -69,6 +69,12 @@ public class MapProviderFactory {
parentMenu.setGroupCheckable(R.id.menu_group_map_sources, true, true);
}
+ /**
+ * Return a map source by id.
+ *
+ * @param id the map source id
+ * @return the map source, or <tt>null</tt> if <tt>id</tt> does not correspond to a registered map source
+ */
public static MapSource getMapSource(int id) {
for (MapSource mapSource : mapSources) {
if (mapSource.getNumericalId() == id) {
@@ -78,6 +84,15 @@ public class MapProviderFactory {
return null;
}
+ /**
+ * Return a map source if there is at least one.
+ *
+ * @return the first map source in the collection, or <tt>null</tt> if there are none registered
+ */
+ public static MapSource getAnyMapSource() {
+ return mapSources.isEmpty() ? null : mapSources.get(0);
+ }
+
public static void registerMapSource(final MapSource mapSource) {
mapSources.add(mapSource);
}
diff --git a/main/src/cgeo/geocaching/settings/SettingsActivity.java b/main/src/cgeo/geocaching/settings/SettingsActivity.java
index 42f6074..ece2acb 100644
--- a/main/src/cgeo/geocaching/settings/SettingsActivity.java
+++ b/main/src/cgeo/geocaching/settings/SettingsActivity.java
@@ -40,6 +40,7 @@ import android.view.View;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListAdapter;
+import android.widget.Toast;
import java.io.File;
import java.util.ArrayList;
@@ -482,7 +483,18 @@ public class SettingsActivity extends PreferenceActivity {
} else if (isPreference(preference, R.string.pref_mapsource)) {
// reset the cached map source
int mapSourceId = Integer.valueOf(stringValue);
- final MapSource mapSource = MapProviderFactory.getMapSource(mapSourceId);
+ MapSource mapSource = MapProviderFactory.getMapSource(mapSourceId);
+ // If there is no corresponding map source (because some map sources were
+ // removed from the device since) then use the first one available.
+ if (mapSource == null) {
+ mapSource = MapProviderFactory.getAnyMapSource();
+ if (mapSource == null) {
+ // There are no map source. There is little we can do here, except log an error and
+ // return to avoid triggering a null pointer exception.
+ Log.e("SettingsActivity.onPreferenceChange: no map source available");
+ return true;
+ }
+ }
Settings.setMapSource(mapSource);
preference.setSummary(mapSource.getName());
} else if (isPreference(preference, R.string.pref_connectorOCActive) || isPreference(preference, R.string.pref_connectorOCPLActive) || isPreference(preference, R.string.pref_connectorGCActive)) {