Fix raster map menu crash

This commit is contained in:
max-klaus 2020-06-11 15:14:56 +03:00
parent 5b97a0b4aa
commit 19e5db7b0a

View file

@ -1,6 +1,7 @@
package net.osmand.plus.rastermaps; package net.osmand.plus.rastermaps;
import android.app.Activity; import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.os.AsyncTask; import android.os.AsyncTask;
@ -52,6 +53,7 @@ import net.osmand.plus.views.OsmandMapTileView;
import net.osmand.util.Algorithms; import net.osmand.util.Algorithms;
import java.io.File; import java.io.File;
import java.lang.ref.WeakReference;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -128,8 +130,6 @@ public class OsmandRasterMapsPlugin extends OsmandPlugin {
}; };
// mapView.addLayer(overlayLayer, 0.7f); // mapView.addLayer(overlayLayer, 0.7f);
settings.MAP_OVERLAY_TRANSPARENCY.addListener(overlayLayerListener); settings.MAP_OVERLAY_TRANSPARENCY.addListener(overlayLayerListener);
} }
@Override @Override
@ -138,8 +138,9 @@ public class OsmandRasterMapsPlugin extends OsmandPlugin {
} }
public void updateMapLayers(OsmandMapTileView mapView, CommonPreference<String> settingsToWarnAboutMap, public void updateMapLayers(OsmandMapTileView mapView,
final MapActivityLayers layers) { CommonPreference<String> settingsToWarnAboutMap,
MapActivityLayers layers) {
if (overlayLayer == null) { if (overlayLayer == null) {
createLayers(); createLayers();
} }
@ -169,7 +170,8 @@ public class OsmandRasterMapsPlugin extends OsmandPlugin {
} }
public void updateLayer(OsmandMapTileView mapView, OsmandSettings settings, public void updateLayer(OsmandMapTileView mapView, OsmandSettings settings,
MapTileLayer layer, CommonPreference<String> preference, float layerOrder, boolean warnWhenSelected) { MapTileLayer layer, CommonPreference<String> preference,
float layerOrder, boolean warnWhenSelected) {
ITileSource overlay = settings.getTileSourceByName(preference.get(), warnWhenSelected); ITileSource overlay = settings.getTileSourceByName(preference.get(), warnWhenSelected);
if (!Algorithms.objectEquals(overlay, layer.getMap())) { if (!Algorithms.objectEquals(overlay, layer.getMap())) {
if (overlay == null) { if (overlay == null) {
@ -186,21 +188,21 @@ public class OsmandRasterMapsPlugin extends OsmandPlugin {
@NonNull final CommonPreference<String> mapPref, @NonNull final CommonPreference<String> mapPref,
@NonNull final CommonPreference<String> exMapPref, @NonNull final CommonPreference<String> exMapPref,
boolean force, boolean force,
@NonNull final MapActivity activity, @NonNull final MapActivity mapActivity,
@Nullable final OnMapSelectedCallback callback) { @Nullable final OnMapSelectedCallback callback) {
final MapActivityLayers layers = activity.getMapLayers(); final WeakReference<MapActivity> mapActivityRef = new WeakReference<>(mapActivity);
if (!force && exMapPref.get() != null) { if (!force && exMapPref.get() != null) {
mapPref.set(exMapPref.get()); mapPref.set(exMapPref.get());
if (callback != null) { if (callback != null) {
callback.onMapSelected(false); callback.onMapSelected(false);
} }
updateMapLayers(mapView, mapPref, layers); updateMapLayers(mapView, mapPref, mapActivity.getMapLayers());
return; return;
} }
final OsmandSettings settings = app.getSettings(); OsmandSettings settings = app.getSettings();
Map<String, String> entriesMap = settings.getTileSourceEntries(); Map<String, String> entriesMap = settings.getTileSourceEntries();
final ArrayList<String> keys = new ArrayList<>(entriesMap.keySet()); final ArrayList<String> keys = new ArrayList<>(entriesMap.keySet());
AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(activity, getThemeRes(activity, app))); AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(mapActivity, getThemeRes(mapActivity)));
final String[] items = new String[entriesMap.size() + 1]; final String[] items = new String[entriesMap.size() + 1];
int i = 0; int i = 0;
for (String it : entriesMap.values()) { for (String it : entriesMap.values()) {
@ -211,13 +213,24 @@ public class OsmandRasterMapsPlugin extends OsmandPlugin {
builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() { builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
@Override @Override
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
MapActivity activity = mapActivityRef.get();
if (activity == null || activity.isFinishing()) {
return;
}
if (which == items.length - 1) { if (which == items.length - 1) {
installMapLayers(activity, new ResultMatcher<TileSourceTemplate>() { installMapLayers(activity, new ResultMatcher<TileSourceTemplate>() {
TileSourceTemplate template = null; TileSourceTemplate template = null;
int count = 0; int count = 0;
boolean cancel = false;
@Override @Override
public boolean publish(TileSourceTemplate object) { public boolean publish(TileSourceTemplate object) {
MapActivity mapActivity = mapActivityRef.get();
if (mapActivity == null || mapActivity.isFinishing()) {
cancel = true;
return false;
}
OsmandMapTileView mapView = mapActivity.getMapView();
if (object == null) { if (object == null) {
if (count == 1) { if (count == 1) {
mapPref.set(template.getName()); mapPref.set(template.getName());
@ -225,9 +238,9 @@ public class OsmandRasterMapsPlugin extends OsmandPlugin {
if (callback != null) { if (callback != null) {
callback.onMapSelected(false); callback.onMapSelected(false);
} }
updateMapLayers(mapView, mapPref, layers); updateMapLayers(mapView, mapPref, mapActivity.getMapLayers());
} else { } else {
selectMapOverlayLayer(mapView, mapPref, exMapPref, false, activity, null); selectMapOverlayLayer(mapView, mapPref, exMapPref, false, mapActivity, null);
} }
} else { } else {
count++; count++;
@ -238,7 +251,7 @@ public class OsmandRasterMapsPlugin extends OsmandPlugin {
@Override @Override
public boolean isCancelled() { public boolean isCancelled() {
return false; return cancel;
} }
}); });
} else { } else {
@ -247,7 +260,7 @@ public class OsmandRasterMapsPlugin extends OsmandPlugin {
if (callback != null) { if (callback != null) {
callback.onMapSelected(false); callback.onMapSelected(false);
} }
updateMapLayers(mapView, mapPref, layers); updateMapLayers(mapActivity.getMapView(), mapPref, activity.getMapLayers());
} }
dialog.dismiss(); dialog.dismiss();
} }
@ -266,43 +279,52 @@ public class OsmandRasterMapsPlugin extends OsmandPlugin {
} }
@Override @Override
public void registerLayerContextMenuActions(final OsmandMapTileView mapView, public void registerLayerContextMenuActions(OsmandMapTileView mapView,
ContextMenuAdapter adapter, ContextMenuAdapter adapter,
final MapActivity mapActivity) { MapActivity mapActivity) {
final WeakReference<MapActivity> mapActivityRef = new WeakReference<>(mapActivity);
ContextMenuAdapter.ItemClickListener listener = new ContextMenuAdapter.OnRowItemClick() { ContextMenuAdapter.ItemClickListener listener = new ContextMenuAdapter.OnRowItemClick() {
@Override @Override
public boolean onRowItemClick(ArrayAdapter<ContextMenuItem> adapter, View view, int itemId, int position) { public boolean onRowItemClick(ArrayAdapter<ContextMenuItem> adapter, View view, int itemId, int position) {
int[] viewCoordinates = AndroidUtils.getCenterViewCoordinates(view); MapActivity mapActivity = mapActivityRef.get();
if (itemId == R.string.layer_overlay) { if (mapActivity != null && !mapActivity.isFinishing()) {
mapActivity.getDashboard().setDashboardVisibility(true, DashboardType.OVERLAY_MAP, viewCoordinates); int[] viewCoordinates = AndroidUtils.getCenterViewCoordinates(view);
return false; if (itemId == R.string.layer_overlay) {
} else if (itemId == R.string.layer_underlay) { mapActivity.getDashboard().setDashboardVisibility(true, DashboardType.OVERLAY_MAP, viewCoordinates);
mapActivity.getDashboard().setDashboardVisibility(true, DashboardType.UNDERLAY_MAP, viewCoordinates); return false;
return false; } else if (itemId == R.string.layer_underlay) {
mapActivity.getDashboard().setDashboardVisibility(true, DashboardType.UNDERLAY_MAP, viewCoordinates);
return false;
}
} }
return true; return true;
} }
@Override @Override
public boolean onContextMenuClick(final ArrayAdapter<ContextMenuItem> adapter, int itemId, final int pos, boolean isChecked, int[] viewCoordinates) { public boolean onContextMenuClick(final ArrayAdapter<ContextMenuItem> adapter, int itemId, final int pos, boolean isChecked, int[] viewCoordinates) {
final OsmandSettings settings = mapActivity.getMyApplication().getSettings(); MapActivity mapActivity = mapActivityRef.get();
if (mapActivity == null || mapActivity.isFinishing()) {
return false;
}
if (itemId == R.string.layer_overlay) { if (itemId == R.string.layer_overlay) {
toggleUnderlayState(mapActivity, RasterMapType.OVERLAY, toggleUnderlayState(mapActivity, RasterMapType.OVERLAY,
new OnMapSelectedCallback() { new OnMapSelectedCallback() {
@Override @Override
public void onMapSelected(boolean canceled) { public void onMapSelected(boolean canceled) {
ContextMenuItem item = adapter.getItem(pos); MapActivity mapActivity = mapActivityRef.get();
if (mapActivity != null && !mapActivity.isFinishing()) {
String overlayMapDescr = settings.MAP_OVERLAY.get(); ContextMenuItem item = adapter.getItem(pos);
if (item != null) {
boolean hasOverlayDescription = overlayMapDescr != null; String overlayMapDescr = mapActivity.getMyApplication().getSettings().MAP_OVERLAY.get();
overlayMapDescr = hasOverlayDescription ? overlayMapDescr boolean hasOverlayDescription = overlayMapDescr != null;
: mapActivity.getString(R.string.shared_string_none); overlayMapDescr = hasOverlayDescription ? overlayMapDescr
: mapActivity.getString(R.string.shared_string_none);
item.setDescription(overlayMapDescr); item.setDescription(overlayMapDescr);
item.setSelected(hasOverlayDescription); item.setSelected(hasOverlayDescription);
item.setColorRes(hasOverlayDescription ? R.color.osmand_orange : ContextMenuItem.INVALID_ID); item.setColorRes(hasOverlayDescription ? R.color.osmand_orange : ContextMenuItem.INVALID_ID);
adapter.notifyDataSetChanged(); adapter.notifyDataSetChanged();
}
}
} }
}); });
return false; return false;
@ -311,24 +333,29 @@ public class OsmandRasterMapsPlugin extends OsmandPlugin {
new OnMapSelectedCallback() { new OnMapSelectedCallback() {
@Override @Override
public void onMapSelected(boolean canceled) { public void onMapSelected(boolean canceled) {
ContextMenuItem item = adapter.getItem(pos); MapActivity mapActivity = mapActivityRef.get();
if (mapActivity != null && !mapActivity.isFinishing()) {
ContextMenuItem item = adapter.getItem(pos);
if (item != null) {
String underlayMapDescr = settings.MAP_UNDERLAY.get();
String underlayMapDescr = settings.MAP_UNDERLAY.get(); boolean hasUnderlayDescription = underlayMapDescr != null;
underlayMapDescr = hasUnderlayDescription
? underlayMapDescr
: mapActivity.getString(R.string.shared_string_none);
boolean hasUnderlayDescription = underlayMapDescr != null; item.setDescription(underlayMapDescr);
underlayMapDescr = hasUnderlayDescription ? underlayMapDescr item.setSelected(hasUnderlayDescription);
: mapActivity.getString(R.string.shared_string_none); item.setColorRes(hasUnderlayDescription ? R.color.osmand_orange : ContextMenuItem.INVALID_ID);
item.setDescription(underlayMapDescr); adapter.notifyDataSetChanged();
item.setSelected(hasUnderlayDescription);
item.setColorRes(hasUnderlayDescription ? R.color.osmand_orange : ContextMenuItem.INVALID_ID);
adapter.notifyDataSetChanged(); final CommonPreference<Boolean> hidePolygonsPref =
mapActivity.getMyApplication().getSettings().getCustomRenderBooleanProperty("noPolygons");
final CommonPreference<Boolean> hidePolygonsPref = hidePolygonsPref.set(hasUnderlayDescription);
mapActivity.getMyApplication().getSettings().getCustomRenderBooleanProperty("noPolygons"); RasterMapMenu.refreshMapComplete(mapActivity);
hidePolygonsPref.set(hasUnderlayDescription); }
RasterMapMenu.refreshMapComplete(mapActivity); }
} }
}); });
return false; return false;
@ -381,18 +408,23 @@ public class OsmandRasterMapsPlugin extends OsmandPlugin {
@Override @Override
public void registerMapContextMenuActions(final MapActivity mapActivity, final double latitude, final double longitude, ContextMenuAdapter adapter, public void registerMapContextMenuActions(MapActivity mapActivity,
Object selectedObj) { final double latitude, final double longitude,
final OsmandMapTileView mapView = mapActivity.getMapView(); ContextMenuAdapter adapter, Object selectedObj) {
if (mapView.getMainLayer() instanceof MapTileLayer) { final WeakReference<MapActivity> mapActivityRef = new WeakReference<>(mapActivity);
if (mapActivity.getMapView().getMainLayer() instanceof MapTileLayer) {
ItemClickListener listener = new ContextMenuAdapter.ItemClickListener() { ItemClickListener listener = new ContextMenuAdapter.ItemClickListener() {
@Override @Override
public boolean onContextMenuClick(ArrayAdapter<ContextMenuItem> adapter, int resId, int pos, boolean isChecked, int[] viewCoordinates) { public boolean onContextMenuClick(ArrayAdapter<ContextMenuItem> adapter, int resId, int pos, boolean isChecked, int[] viewCoordinates) {
if (resId == R.string.context_menu_item_update_map) { MapActivity mapActivity = mapActivityRef.get();
mapActivity.getMapActions().reloadTile(mapView.getZoom(), latitude, longitude); if (mapActivity != null && !mapActivity.isFinishing()) {
} else if (resId == R.string.shared_string_download_map) { OsmandMapTileView mapView = mapActivity.getMapView();
DownloadTilesDialog dlg = new DownloadTilesDialog(mapActivity, (OsmandApplication) mapActivity.getApplication(), mapView); if (resId == R.string.context_menu_item_update_map) {
dlg.openDialog(); mapActivity.getMapActions().reloadTile(mapView.getZoom(), latitude, longitude);
} else if (resId == R.string.shared_string_download_map) {
DownloadTilesDialog dlg = new DownloadTilesDialog(mapActivity, (OsmandApplication) mapActivity.getApplication(), mapView);
dlg.openDialog();
}
} }
return true; return true;
} }
@ -412,10 +444,10 @@ public class OsmandRasterMapsPlugin extends OsmandPlugin {
} }
} }
public static void installMapLayers(final Activity activity, final ResultMatcher<TileSourceTemplate> result) { public static void installMapLayers(Activity activity, final ResultMatcher<TileSourceTemplate> result) {
final WeakReference<Activity> activityRef = new WeakReference<>(activity);
final OsmandApplication app = (OsmandApplication) activity.getApplication(); final OsmandApplication app = (OsmandApplication) activity.getApplication();
final OsmandSettings settings = app.getSettings(); OsmandSettings settings = app.getSettings();
final Map<String, String> entriesMap = settings.getTileSourceEntries();
if (!settings.isInternetConnectionAvailable(true)) { if (!settings.isInternetConnectionAvailable(true)) {
Toast.makeText(activity, R.string.internet_not_available, Toast.LENGTH_LONG).show(); Toast.makeText(activity, R.string.internet_not_available, Toast.LENGTH_LONG).show();
return; return;
@ -427,27 +459,36 @@ public class OsmandRasterMapsPlugin extends OsmandPlugin {
} }
protected void onPostExecute(final java.util.List<TileSourceTemplate> downloaded) { protected void onPostExecute(final java.util.List<TileSourceTemplate> downloaded) {
Activity activity = activityRef.get();
if (activity == null || activity.isFinishing()) {
return;
}
OsmandApplication app = (OsmandApplication) activity.getApplication();
if (downloaded == null || downloaded.isEmpty()) { if (downloaded == null || downloaded.isEmpty()) {
Toast.makeText(activity, R.string.shared_string_io_error, Toast.LENGTH_SHORT).show(); Toast.makeText(activity, R.string.shared_string_io_error, Toast.LENGTH_SHORT).show();
return; return;
} }
AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(activity, getThemeRes(activity, app))); AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(activity, getThemeRes(activity)));
String[] names = new String[downloaded.size()]; String[] names = new String[downloaded.size()];
for (int i = 0; i < names.length; i++) { for (int i = 0; i < names.length; i++) {
names[i] = downloaded.get(i).getName(); names[i] = downloaded.get(i).getName();
} }
final boolean[] selected = new boolean[downloaded.size()]; final boolean[] selected = new boolean[downloaded.size()];
boolean nightMode = isNightMode(activity, app); boolean nightMode = isNightMode(activity);
int themeResId = getThemeRes(activity, app); int themeResId = getThemeRes(activity);
int selectedProfileColor = ContextCompat.getColor(app, app.getSettings().getApplicationMode().getIconColorInfo().getColor(nightMode)); int selectedProfileColor = ContextCompat.getColor(app, app.getSettings().getApplicationMode().getIconColorInfo().getColor(nightMode));
DialogListItemAdapter dialogAdapter = DialogListItemAdapter.createMultiChoiceAdapter(names, nightMode, selected, app, DialogListItemAdapter dialogAdapter = DialogListItemAdapter.createMultiChoiceAdapter(names, nightMode, selected, app,
selectedProfileColor, themeResId, new View.OnClickListener() { selectedProfileColor, themeResId, new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
int which = (int) v.getTag(); Activity activity = activityRef.get();
selected[which] = !selected[which]; if (activity != null && !activity.isFinishing()) {
if (entriesMap.containsKey(downloaded.get(which).getName()) && selected[which]) { Map<String, String> entriesMap = ((OsmandApplication)activity.getApplication()).getSettings().getTileSourceEntries();
Toast.makeText(activity, R.string.tile_source_already_installed, Toast.LENGTH_SHORT).show(); int which = (int) v.getTag();
selected[which] = !selected[which];
if (entriesMap.containsKey(downloaded.get(which).getName()) && selected[which]) {
Toast.makeText(activity, R.string.tile_source_already_installed, Toast.LENGTH_SHORT).show();
}
} }
} }
} }
@ -458,22 +499,26 @@ public class OsmandRasterMapsPlugin extends OsmandPlugin {
builder.setPositiveButton(R.string.shared_string_apply, new DialogInterface.OnClickListener() { builder.setPositiveButton(R.string.shared_string_apply, new DialogInterface.OnClickListener() {
@Override @Override
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
List<TileSourceTemplate> toInstall = new ArrayList<>(); Activity activity = activityRef.get();
for (int i = 0; i < selected.length; i++) { if (activity != null && !activity.isFinishing()) {
if (selected[i]) { OsmandSettings settings = ((OsmandApplication) activity.getApplication()).getSettings();
toInstall.add(downloaded.get(i)); List<TileSourceTemplate> toInstall = new ArrayList<>();
} for (int i = 0; i < selected.length; i++) {
} if (selected[i]) {
for (TileSourceTemplate ts : toInstall) { toInstall.add(downloaded.get(i));
if (settings.installTileSource(ts)) {
if (result != null) {
result.publish(ts);
} }
} }
} for (TileSourceTemplate ts : toInstall) {
// at the end publish null to show end of process if (settings.installTileSource(ts)) {
if (!toInstall.isEmpty() && result != null) { if (result != null) {
result.publish(null); result.publish(ts);
}
}
}
// at the end publish null to show end of process
if (!toInstall.isEmpty() && result != null) {
result.publish(null);
}
} }
} }
}); });
@ -483,19 +528,20 @@ public class OsmandRasterMapsPlugin extends OsmandPlugin {
t.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); t.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} }
public static void defineNewEditLayer(final Activity activity, final ResultMatcher<TileSourceTemplate> resultMatcher, final String editedLayerName) { public static void defineNewEditLayer(Activity activity, final ResultMatcher<TileSourceTemplate> resultMatcher, final String editedLayerName) {
final OsmandApplication app = (OsmandApplication) activity.getApplication(); final WeakReference<Activity> activityRef = new WeakReference<>(activity);
final OsmandSettings settings = app.getSettings(); OsmandApplication app = (OsmandApplication) activity.getApplication();
final Map<String, String> entriesMap = settings.getTileSourceEntries(true); OsmandSettings settings = app.getSettings();
Map<String, String> entriesMap = settings.getTileSourceEntries(true);
final SQLiteTileSource[] sqLiteTileSource = new SQLiteTileSource[1]; final SQLiteTileSource[] sqLiteTileSource = new SQLiteTileSource[1];
boolean nightMode = isNightMode(activity, app); boolean nightMode = isNightMode(activity);
final int dp8 = AndroidUtils.dpToPx(app, 8f); final int dp8 = AndroidUtils.dpToPx(app, 8f);
int textColorPrimary = ContextCompat.getColor(app, nightMode ? R.color.text_color_primary_dark : R.color.text_color_primary_light); int textColorPrimary = ContextCompat.getColor(app, nightMode ? R.color.text_color_primary_dark : R.color.text_color_primary_light);
TileSourceTemplate ts = new TileSourceTemplate("NewMapnik", "http://mapnik.osmand.net/{0}/{1}/{2}.png", TileSourceTemplate ts = new TileSourceTemplate("NewMapnik", "http://mapnik.osmand.net/{0}/{1}/{2}.png",
"png", 17, 5, 256, 16, 32000); "png", 17, 5, 256, 16, 32000);
final TileSourceTemplate[] result = new TileSourceTemplate[]{ts}; final TileSourceTemplate[] result = new TileSourceTemplate[]{ts};
AlertDialog.Builder bld = new AlertDialog.Builder(new ContextThemeWrapper(activity, getThemeRes(activity, app))); AlertDialog.Builder bld = new AlertDialog.Builder(new ContextThemeWrapper(activity, getThemeRes(activity)));
View view = UiUtilities.getInflater(activity, isNightMode(activity, app)).inflate(R.layout.editing_tile_source, null); View view = UiUtilities.getInflater(activity, isNightMode(activity)).inflate(R.layout.editing_tile_source, null);
final EditText name = (EditText) view.findViewById(R.id.Name); final EditText name = (EditText) view.findViewById(R.id.Name);
final Spinner existing = (Spinner) view.findViewById(R.id.TileSourceSpinner); final Spinner existing = (Spinner) view.findViewById(R.id.TileSourceSpinner);
final TextView existingHint = (TextView) view.findViewById(R.id.TileSourceHint); final TextView existingHint = (TextView) view.findViewById(R.id.TileSourceHint);
@ -539,10 +585,8 @@ public class OsmandRasterMapsPlugin extends OsmandPlugin {
template.setExpirationTimeMinutes(sqLiteTileSource[0].getExpirationTimeMinutes()); template.setExpirationTimeMinutes(sqLiteTileSource[0].getExpirationTimeMinutes());
template.setEllipticYTile(sqLiteTileSource[0].isEllipticYTile()); template.setEllipticYTile(sqLiteTileSource[0].isEllipticYTile());
} }
if (template != null) { result[0] = template.copy();
result[0] = template.copy(); updateTileSourceEditView(result[0], name, urlToLoad, minZoom, maxZoom, expire, elliptic);
updateTileSourceEditView(result[0], name, urlToLoad, minZoom, maxZoom, expire, elliptic);
}
existingHint.setVisibility(View.GONE); existingHint.setVisibility(View.GONE);
existing.setVisibility(View.GONE); existing.setVisibility(View.GONE);
} }
@ -551,13 +595,12 @@ public class OsmandRasterMapsPlugin extends OsmandPlugin {
@Override @Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (position > 0) { Activity activity = activityRef.get();
if (activity != null && !activity.isFinishing() && position > 0) {
File f = ((OsmandApplication) activity.getApplication()).getAppPath(IndexConstants.TILES_INDEX_DIR + templates.get(position)); File f = ((OsmandApplication) activity.getApplication()).getAppPath(IndexConstants.TILES_INDEX_DIR + templates.get(position));
TileSourceTemplate template = TileSourceManager.createTileSourceTemplate(f); TileSourceTemplate template = TileSourceManager.createTileSourceTemplate(f);
if (template != null) { result[0] = template.copy();
result[0] = template.copy(); updateTileSourceEditView(result[0], name, urlToLoad, minZoom, maxZoom, expire, elliptic);
updateTileSourceEditView(result[0], name, urlToLoad, minZoom, maxZoom, expire, elliptic);
}
} }
} }
@ -571,29 +614,33 @@ public class OsmandRasterMapsPlugin extends OsmandPlugin {
@Override @Override
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
TileSourceTemplate r = result[0]; Activity activity = activityRef.get();
try { if (activity != null && !activity.isFinishing()) {
r.setName(name.getText().toString()); OsmandSettings settings = ((OsmandApplication) activity.getApplication()).getSettings();
r.setExpirationTimeMinutes(expire.getText().length() == 0 ? -1 : TileSourceTemplate r = result[0];
Integer.parseInt(expire.getText().toString())); try {
r.setMinZoom(Integer.parseInt(minZoom.getText().toString())); r.setName(name.getText().toString());
r.setMaxZoom(Integer.parseInt(maxZoom.getText().toString())); r.setExpirationTimeMinutes(expire.getText().length() == 0 ? -1 :
r.setEllipticYTile(elliptic.isChecked()); Integer.parseInt(expire.getText().toString()));
r.setUrlToLoad(urlToLoad.getText().toString().equals("") ? null : urlToLoad.getText().toString().replace("{$x}", "{1}") r.setMinZoom(Integer.parseInt(minZoom.getText().toString()));
.replace("{$y}", "{2}").replace("{$z}", "{0}")); r.setMaxZoom(Integer.parseInt(maxZoom.getText().toString()));
if (sqLiteTileSource[0] != null) { r.setEllipticYTile(elliptic.isChecked());
sqLiteTileSource[0].updateFromTileSourceTemplate(r); r.setUrlToLoad(urlToLoad.getText().toString().equals("") ? null : urlToLoad.getText().toString().replace("{$x}", "{1}")
} else { .replace("{$y}", "{2}").replace("{$z}", "{0}"));
if (r.getName().length() > 0) { if (sqLiteTileSource[0] != null) {
if (settings.installTileSource(r)) { sqLiteTileSource[0].updateFromTileSourceTemplate(r);
Toast.makeText(activity, activity.getString(R.string.edit_tilesource_successfully, r.getName()), } else {
Toast.LENGTH_SHORT).show(); if (r.getName().length() > 0) {
resultMatcher.publish(r); if (settings.installTileSource(r)) {
Toast.makeText(activity, activity.getString(R.string.edit_tilesource_successfully, r.getName()),
Toast.LENGTH_SHORT).show();
resultMatcher.publish(r);
}
} }
} }
} catch (RuntimeException e) {
Toast.makeText(activity, e.getMessage(), Toast.LENGTH_SHORT).show();
} }
} catch (RuntimeException e) {
Toast.makeText(activity, e.getMessage(), Toast.LENGTH_SHORT).show();
} }
} }
}); });
@ -601,8 +648,8 @@ public class OsmandRasterMapsPlugin extends OsmandPlugin {
bld.show(); bld.show();
} }
private static void updateTileSourceEditView(TileSourceTemplate ts, EditText name, final EditText urlToLoad, final EditText minZoom, private static void updateTileSourceEditView(TileSourceTemplate ts, EditText name, EditText urlToLoad, EditText minZoom,
final EditText maxZoom, EditText expire, final CheckBox elliptic) { EditText maxZoom, EditText expire, CheckBox elliptic) {
minZoom.setText(String.valueOf(ts.getMinimumZoomSupported())); minZoom.setText(String.valueOf(ts.getMinimumZoomSupported()));
maxZoom.setText(String.valueOf(ts.getMaximumZoomSupported())); maxZoom.setText(String.valueOf(ts.getMaximumZoomSupported()));
name.setText(ts.getName()); name.setText(ts.getName());
@ -626,7 +673,6 @@ public class OsmandRasterMapsPlugin extends OsmandPlugin {
OsmandMapTileView mapView = mapActivity.getMapView(); OsmandMapTileView mapView = mapActivity.getMapView();
CommonPreference<String> mapTypePreference; CommonPreference<String> mapTypePreference;
CommonPreference<String> exMapTypePreference; CommonPreference<String> exMapTypePreference;
OsmandSettings.CommonPreference<Integer> mapTransparencyPreference;
//boolean isMapSelected; //boolean isMapSelected;
MapTileLayer layer; MapTileLayer layer;
@ -663,15 +709,16 @@ public class OsmandRasterMapsPlugin extends OsmandPlugin {
} }
} }
private static boolean isNightMode(Activity activity, OsmandApplication app) { private static boolean isNightMode(Context context) {
if (activity == null || app == null) { if (context == null) {
return false; return false;
} }
return activity instanceof MapActivity ? app.getDaynightHelper().isNightModeForMapControls() : !app.getSettings().isLightContent(); OsmandApplication app = (OsmandApplication) context.getApplicationContext();
return context instanceof MapActivity ? app.getDaynightHelper().isNightModeForMapControls() : !app.getSettings().isLightContent();
} }
private static int getThemeRes(Activity activity, OsmandApplication app) { private static int getThemeRes(Context context) {
return isNightMode(activity, app) ? R.style.OsmandDarkTheme : R.style.OsmandLightTheme; return isNightMode(context) ? R.style.OsmandDarkTheme : R.style.OsmandLightTheme;
} }
public enum RasterMapType { public enum RasterMapType {