remember theme preference

This commit is contained in:
Hannes Janetzek 2012-09-04 14:24:32 +02:00
parent 1a27f56313
commit b210c5ee73
2 changed files with 41 additions and 29 deletions

View File

@ -14,6 +14,9 @@
*/ */
package org.mapsforge.android; package org.mapsforge.android;
import java.io.FileNotFoundException;
import org.mapsforge.android.rendertheme.InternalRenderTheme;
import org.mapsforge.core.GeoPoint; import org.mapsforge.core.GeoPoint;
import org.mapsforge.core.MapPosition; import org.mapsforge.core.MapPosition;
@ -37,6 +40,7 @@ public abstract class MapActivity extends Activity {
private static final String KEY_MAP_FILE = "mapFile"; private static final String KEY_MAP_FILE = "mapFile";
private static final String KEY_ZOOM_LEVEL = "zoomLevel"; private static final String KEY_ZOOM_LEVEL = "zoomLevel";
private static final String PREFERENCES_FILE = "MapActivity"; private static final String PREFERENCES_FILE = "MapActivity";
private static final String KEY_THEME = "Theme";
private static boolean containsMapViewPosition(SharedPreferences sharedPreferences) { private static boolean containsMapViewPosition(SharedPreferences sharedPreferences) {
return sharedPreferences.contains(KEY_LATITUDE) return sharedPreferences.contains(KEY_LATITUDE)
@ -44,23 +48,15 @@ public abstract class MapActivity extends Activity {
&& sharedPreferences.contains(KEY_ZOOM_LEVEL); && sharedPreferences.contains(KEY_ZOOM_LEVEL);
} }
/**
* Counter to store the last ID given to a MapView.
*/
private int lastMapViewId;
/** /**
* Internal list which contains references to all running MapView objects. * Internal list which contains references to all running MapView objects.
*/ */
private MapView mMapView; private MapView mMapView;
private void destroyMapViews() {
mMapView.destroy();
}
private void restoreMapView(MapView mapView) { private void restoreMapView(MapView mapView) {
SharedPreferences sharedPreferences = getSharedPreferences(PREFERENCES_FILE, SharedPreferences sharedPreferences = getSharedPreferences(PREFERENCES_FILE,
MODE_PRIVATE); MODE_PRIVATE);
if (containsMapViewPosition(sharedPreferences)) { if (containsMapViewPosition(sharedPreferences)) {
if (sharedPreferences.contains(KEY_MAP_FILE)) { if (sharedPreferences.contains(KEY_MAP_FILE)) {
@ -77,12 +73,25 @@ public abstract class MapActivity extends Activity {
MapPosition mapPosition = new MapPosition(geoPoint, (byte) zoomLevel, 1); MapPosition mapPosition = new MapPosition(geoPoint, (byte) zoomLevel, 1);
mapView.setCenterAndZoom(mapPosition); mapView.setCenterAndZoom(mapPosition);
} }
String theme = sharedPreferences.getString(KEY_THEME,
InternalRenderTheme.OSMARENDER.name());
if (theme.startsWith("/")) {
try {
mapView.setRenderTheme(theme);
} catch (FileNotFoundException e) {
mapView.setRenderTheme(InternalRenderTheme.OSMARENDER);
}
} else {
mapView.setRenderTheme(InternalRenderTheme.valueOf(theme));
}
} }
@Override @Override
protected void onDestroy() { protected void onDestroy() {
super.onDestroy(); super.onDestroy();
destroyMapViews(); mMapView.destroy();
} }
@Override @Override
@ -107,6 +116,8 @@ public abstract class MapActivity extends Activity {
editor.putString(KEY_MAP_FILE, mMapView.getMapFile()); editor.putString(KEY_MAP_FILE, mMapView.getMapFile());
} }
editor.putString(KEY_THEME, mMapView.getRenderTheme());
editor.commit(); editor.commit();
} }
@ -116,13 +127,6 @@ public abstract class MapActivity extends Activity {
mMapView.onResume(); mMapView.onResume();
} }
/**
* @return a unique MapView ID on each call.
*/
final int getMapViewId() {
return ++lastMapViewId;
}
/** /**
* This method is called once by each MapView during its setup process. * This method is called once by each MapView during its setup process.
* *

View File

@ -94,8 +94,6 @@ public class MapView extends GLSurfaceView {
private DebugSettings debugSettings; private DebugSettings debugSettings;
private String mMapFile; private String mMapFile;
private File cacheDir;
/** /**
* @param context * @param context
* the enclosing MapActivity instance. * the enclosing MapActivity instance.
@ -138,8 +136,6 @@ public class MapView extends GLSurfaceView {
MapActivity mapActivity = (MapActivity) context; MapActivity mapActivity = (MapActivity) context;
cacheDir = context.getFilesDir();
debugSettings = new DebugSettings(false, false, false, false); debugSettings = new DebugSettings(false, false, false, false);
mMapController = new MapController(this); mMapController = new MapController(this);
@ -181,10 +177,12 @@ public class MapView extends GLSurfaceView {
setMapFile("default"); setMapFile("default");
initMapStartPosition(); initMapStartPosition();
if (!setRenderTheme(DEFAULT_RENDER_THEME)) { mapActivity.registerMapView(this);
Log.d(TAG, "X could not parse theme");
// FIXME show init error dialog // if (!setRenderTheme(DEFAULT_RENDER_THEME)) {
} // Log.d(TAG, "X could not parse theme");
// // FIXME show init error dialog
// }
setEGLConfigChooser(new GlConfigChooser()); setEGLConfigChooser(new GlConfigChooser());
setEGLContextClientVersion(2); setEGLContextClientVersion(2);
@ -193,8 +191,6 @@ public class MapView extends GLSurfaceView {
if (!debugFrameTime) if (!debugFrameTime)
setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY); setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
mapActivity.registerMapView(this);
} }
public final static boolean debugFrameTime = false; public final static boolean debugFrameTime = false;
@ -348,7 +344,7 @@ public class MapView extends GLSurfaceView {
if (mapFile != null) if (mapFile != null)
fileOpenResult = mapDatabase.openFile(new File(mapFile)); fileOpenResult = mapDatabase.openFile(new File(mapFile));
else else
fileOpenResult = mapDatabase.openFile(cacheDir); fileOpenResult = mapDatabase.openFile(null);
if (fileOpenResult != null && fileOpenResult.isSuccess()) { if (fileOpenResult != null && fileOpenResult.isSuccess()) {
mMapFile = mapFile; mMapFile = mapFile;
@ -436,6 +432,12 @@ public class MapView extends GLSurfaceView {
mapWorkersProceed(); mapWorkersProceed();
} }
private String mRenderTheme;
public String getRenderTheme() {
return mRenderTheme;
}
/** /**
* Sets the internal theme which is used for rendering the map. * Sets the internal theme which is used for rendering the map.
* *
@ -451,6 +453,9 @@ public class MapView extends GLSurfaceView {
} }
boolean ret = setRenderTheme((Theme) internalRenderTheme); boolean ret = setRenderTheme((Theme) internalRenderTheme);
if (ret) {
mRenderTheme = internalRenderTheme.name();
}
clearAndRedrawMapView(); clearAndRedrawMapView();
return ret; return ret;
@ -471,7 +476,10 @@ public class MapView extends GLSurfaceView {
throw new IllegalArgumentException("render theme path must not be null"); throw new IllegalArgumentException("render theme path must not be null");
} }
setRenderTheme(new ExternalRenderTheme(renderThemePath)); boolean ret = setRenderTheme(new ExternalRenderTheme(renderThemePath));
if (ret) {
mRenderTheme = renderThemePath;
}
clearAndRedrawMapView(); clearAndRedrawMapView();
} }