Skip to content

Commit

Permalink
OSMNavigator: MapsForge tile source even when restarting the app (or …
Browse files Browse the repository at this point in the history
…rotating the device); Full cache clean option.
  • Loading branch information
MKergall committed Mar 6, 2018
1 parent 0e77635 commit 06dcfe2
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 32 deletions.
4 changes: 2 additions & 2 deletions OSMNavigator/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ android {
applicationId "com.osmnavigator"
minSdkVersion 10
targetSdkVersion 23
versionCode 19
versionName "1.9"
versionCode 20
versionName "2.0"
}
buildTypes {
release {
Expand Down
73 changes: 45 additions & 28 deletions OSMNavigator/src/main/java/com/osmnavigator/MapActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@
import org.osmdroid.tileprovider.MapTileProviderBase;
import org.osmdroid.tileprovider.MapTileProviderBasic;
import org.osmdroid.tileprovider.cachemanager.CacheManager;
import org.osmdroid.tileprovider.modules.IFilesystemCache;
import org.osmdroid.tileprovider.modules.SqlTileWriter;
import org.osmdroid.tileprovider.tilesource.ITileSource;
import org.osmdroid.tileprovider.tilesource.MapBoxTileSource;
import org.osmdroid.tileprovider.tilesource.OnlineTileSourceBase;
Expand Down Expand Up @@ -209,11 +211,15 @@ public class MapActivity extends Activity implements MapEventsReceiver, Location

String tileProviderName = prefs.getString("TILE_PROVIDER", "Mapnik");
mNightMode = prefs.getBoolean("NIGHT_MODE", false);
try {
ITileSource tileSource = TileSourceFactory.getTileSource(tileProviderName);
map.setTileSource(tileSource);
} catch (IllegalArgumentException e) {
map.setTileSource(TileSourceFactory.MAPNIK);
if ("rendertheme-v4".equals(tileProviderName)) {
setMapsForgeTileProvider();
} else {
try {
ITileSource tileSource = TileSourceFactory.getTileSource(tileProviderName);
map.setTileSource(tileSource);
} catch (IllegalArgumentException e) {
map.setTileSource(TileSourceFactory.MAPNIK);
}
}
if (mNightMode)
map.getOverlayManager().getTilesOverlay().setColorFilter(TilesOverlay.INVERT_COLORS);
Expand Down Expand Up @@ -742,11 +748,10 @@ public void updateUIWithPolygon(ArrayList<GeoPoint> polygon, String name){
}

//Async task to reverse-geocode the marker position in a separate thread:
private class ReverseGeocodingTask extends AsyncTask<Object, Void, String> {
private class ReverseGeocodingTask extends AsyncTask<Marker, Void, String> {
Marker marker;
protected String doInBackground(Object... params) {
marker = (Marker)params[0];
return getAddress(marker.getPosition());
protected String doInBackground(Marker... params) {
return getAddress(params[0].getPosition());
}
protected void onPostExecute(String result) {
marker.setSnippet(result);
Expand Down Expand Up @@ -947,17 +952,16 @@ else if (roads[0].mStatus > Road.STATUS_TECHNICAL_ISSUE) //functional issues
/**
* Async task to get the road in a separate thread.
*/
private class UpdateRoadTask extends AsyncTask<Object, Void, Road[]> {
private class UpdateRoadTask extends AsyncTask<ArrayList<GeoPoint>, Void, Road[]> {

private final Context mContext;

public UpdateRoadTask(Context context) {
this.mContext = context;
}

protected Road[] doInBackground(Object... params) {
@SuppressWarnings("unchecked")
ArrayList<GeoPoint> waypoints = (ArrayList<GeoPoint>)params[0];
protected Road[] doInBackground(ArrayList<GeoPoint>... params) {
ArrayList<GeoPoint> waypoints = params[0];
RoadManager roadManager;
Locale locale = Locale.getDefault();
switch (mWhichRouteProvider){
Expand Down Expand Up @@ -1106,11 +1110,11 @@ String getOSMTag(String humanReadableFeature){
return map.get(humanReadableFeature.toLowerCase(Locale.getDefault()));
}

private class POILoadingTask extends AsyncTask<Object, Void, ArrayList<POI>> {
private class POILoadingTask extends AsyncTask<String, Void, ArrayList<POI>> {
String mFeatureTag;
String message;
protected ArrayList<POI> doInBackground(Object... params) {
mFeatureTag = (String)params[0];
protected ArrayList<POI> doInBackground(String... params) {
mFeatureTag = params[0];
BoundingBox bb = map.getBoundingBox();
if (mFeatureTag == null || mFeatureTag.equals("")){
return null;
Expand Down Expand Up @@ -1383,10 +1387,10 @@ void insertOverlaysInKml(){
}

//Async task to reverse-geocode the KML point in a separate thread:
private class KMLGeocodingTask extends AsyncTask<Object, Void, String> {
private class KMLGeocodingTask extends AsyncTask<KmlPlacemark, Void, String> {
KmlPlacemark kmlPoint;
protected String doInBackground(Object... params) {
kmlPoint = (KmlPlacemark)params[0];
protected String doInBackground(KmlPlacemark... params) {
kmlPoint = params[0];
return getAddress(((KmlPoint) kmlPoint.mGeometry).getPosition());
}
protected void onPostExecute(String result) {
Expand All @@ -1399,7 +1403,7 @@ protected void onPostExecute(String result) {
void addKmlPoint(GeoPoint position){
KmlFeature kmlPoint = new KmlPlacemark(position);
mKmlDocument.mKmlRoot.add(kmlPoint);
new KMLGeocodingTask().execute(kmlPoint);
new KMLGeocodingTask().execute((KmlPlacemark)kmlPoint);
updateUIWithKml();
}

Expand Down Expand Up @@ -1567,6 +1571,22 @@ boolean setMapsForgeTileProvider(){
return true;
}

private class CacheClearer extends AsyncTask<Void, Void, Boolean> {
protected Boolean doInBackground(Void... params) {
IFilesystemCache tileWriter = map.getTileProvider().getTileWriter();
if (tileWriter instanceof SqlTileWriter) {
return ((SqlTileWriter) tileWriter).purgeCache();
} else
return false;
}
protected void onPostExecute(Boolean result) {
if (result)
Toast.makeText(map.getContext(), "Cache Purge successful", Toast.LENGTH_SHORT).show();
else
Toast.makeText(map.getContext(), "Cache Purge failed", Toast.LENGTH_SHORT).show();
}
}

@Override public boolean onOptionsItemSelected(MenuItem item) {
Intent myIntent;
switch (item.getItemId()) {
Expand Down Expand Up @@ -1654,11 +1674,11 @@ boolean setMapsForgeTileProvider(){
mNightMode = false;
item.setChecked(true);
return true;
case R.id.menu_tile_mapnik_by_night:
case R.id.menu_tile_mapnik_by_night:
setStdTileProvider();
map.setTileSource(TileSourceFactory.MAPNIK);
map.getOverlayManager().getTilesOverlay().setColorFilter(TilesOverlay.INVERT_COLORS);
mNightMode = true;
map.setTileSource(TileSourceFactory.MAPNIK);
map.getOverlayManager().getTilesOverlay().setColorFilter(TilesOverlay.INVERT_COLORS);
mNightMode = true;
item.setChecked(true);
return true;
case R.id.menu_tile_mapbox_satellite:
Expand All @@ -1682,10 +1702,7 @@ boolean setMapsForgeTileProvider(){
return true;
}
case R.id.menu_clear_view_area:{
CacheManager cacheManager = new CacheManager(map);
int zoomMin = map.getZoomLevel();
int zoomMax = map.getZoomLevel()+7;
cacheManager.cleanAreaAsync(this, map.getBoundingBox(), zoomMin, zoomMax);
new CacheClearer().execute();
return true;
}
case R.id.menu_cache_usage:{
Expand Down
2 changes: 1 addition & 1 deletion OSMNavigator/src/main/res/values-fr/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
<string name="menu_pois">Points d\'intérêt</string>
<string name="menu_cache">Gestion du Cache</string>
<string name="menu_download_view_area">Télécharger la vue</string>
<string name="menu_clear_view_area">Vider la vue du cache</string>
<string name="menu_clear_view_area">Vider le cache</string>
<string name="menu_cache_usage">Utilisation du cache</string>
<string name="menu_tile">Choix de la carte</string>
<string name="menu_route">Options sur les routes</string>
Expand Down
2 changes: 1 addition & 1 deletion OSMNavigator/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
<string name="menu_pois">Features list</string>
<string name="menu_cache">Cache Manager</string>
<string name="menu_download_view_area">Download view area</string>
<string name="menu_clear_view_area">Clear view area</string>
<string name="menu_clear_view_area">Clear cache</string>
<string name="menu_cache_usage">Cache usage</string>
<string name="menu_tile">Tile Provider</string>
<string name="menu_route">Route Provider</string>
Expand Down

0 comments on commit 06dcfe2

Please sign in to comment.