package eu.konggdev.strikemaps.helper; import android.content.SharedPreferences; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; import eu.konggdev.strikemaps.app.ComponentHolderActivity; import eu.konggdev.strikemaps.map.source.MapSource; import eu.konggdev.strikemaps.map.source.tiles.SourceTiles; import eu.konggdev.strikemaps.map.style.MapStyle; import eu.konggdev.strikemaps.map.style.management.StyleManagementMetadata; import eu.konggdev.strikemaps.map.style.options.StyleOptions; import eu.konggdev.strikemaps.util.file.FileTools; import org.apache.commons.codec.digest.DigestUtils; import java.lang.reflect.Type; import java.util.HashMap; import java.util.Map; public final class UserPrefsHelper { private UserPrefsHelper() {} // prevent instantiation //Keys private static final String KEY_STARTUP_MAP_STYLE = "startupMapStyle"; private static final String KEY_MAP_RENDERER = "mapRenderer"; private static final String KEY_PERSIST_LOCATION_ENABLED = "persistLocationEnabled"; private static final String KEY_LAST_LOCATION_ENABLED = "lastLocationEnabled"; private static final String KEY_STYLES = "styles"; private static final String KEY_SOURCES = "sources"; //Defaults private static final Integer DEFAULT_MAP_STYLE = 0; private static final Integer DEFAULT_MAP_RENDERER = 0; private static final boolean DEFAULT_PERSIST_LOCATION_ENABLED = true; private static final boolean DEFAULT_LAST_LOCATION_ENABLED = false; public static Map DEFAULT_STYLES(ComponentHolderActivity app) { Map styles = new HashMap<>(); String[] styleAssets = FileTools.getAssetFiles("bundled/style", ".style.json", app); for (int i = 0; i < styleAssets.length; i++) { String styleContents = FileTools.loadStringFromAssetFile(styleAssets[i], app); styles.put( i, new MapStyle( styleContents, new StyleOptions(), new StyleManagementMetadata( //Set the style to automatically update from assets false, true, true, "assets://" + styleAssets[i], DigestUtils.sha256Hex(styleContents)) )); } return styles; } //Built-in Styles private static final Map DEFAULT_SOURCES = Map.of( 0, new MapSource( "Strike Maps Planet", new SourceTiles("https://tiles.strikemaps.eu/planet"), "vector", "smts" ), 1, new MapSource( "ArcGIS Imagery", new SourceTiles(new String[]{"https://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}"}), "raster", "raster" ) ); //Built-in Sources public static Integer startupMapStyle(SharedPreferences prefs) { return prefs.getInt(KEY_STARTUP_MAP_STYLE, DEFAULT_MAP_STYLE); } public static boolean startupMapStyle(SharedPreferences prefs, Integer updated) { return prefs.edit().putInt(KEY_STARTUP_MAP_STYLE, updated).commit(); } public static Integer mapRenderer(SharedPreferences prefs) { return prefs.getInt(KEY_MAP_RENDERER, DEFAULT_MAP_RENDERER); } public static boolean mapRenderer(SharedPreferences prefs, Integer updated) { return prefs.edit().putInt(KEY_MAP_RENDERER, updated).commit(); } public static boolean persistLocationEnabled(SharedPreferences prefs) { return prefs.getBoolean(KEY_PERSIST_LOCATION_ENABLED, DEFAULT_PERSIST_LOCATION_ENABLED); } public static boolean lastLocationEnabled(SharedPreferences prefs) { return prefs.getBoolean(KEY_LAST_LOCATION_ENABLED, DEFAULT_LAST_LOCATION_ENABLED); } public static boolean lastLocationEnabled(SharedPreferences prefs, boolean status) { return prefs.edit().putBoolean(KEY_LAST_LOCATION_ENABLED, status).commit(); } public static Map styles(SharedPreferences prefs, ComponentHolderActivity app) { String json = prefs.getString(KEY_STYLES, null); if (json == null) return DEFAULT_STYLES(app); Type type = new TypeToken>() {}.getType(); Map stored = new Gson().fromJson(json, type); Map result = new HashMap<>(); for (var entry : stored.entrySet()) result.put(entry.getKey(), entry.getValue().restore()); return result; } public static boolean styles(SharedPreferences prefs, Map updated) { Map stored = new HashMap<>(); for (var entry : updated.entrySet()) stored.put(entry.getKey(), entry.getValue().makeStoredRepresentation()); return prefs.edit() .putString(KEY_STYLES, new Gson().toJson(stored)) .commit(); } public static Map sources(SharedPreferences prefs) { String json = prefs.getString(KEY_SOURCES, null); if (json == null) return DEFAULT_SOURCES; Type type = new TypeToken>() {}.getType(); return new Gson().fromJson(json, type); } public static boolean sources(SharedPreferences prefs, Map updated) { return prefs.edit() .putString(KEY_SOURCES, new Gson().toJson(updated)) .commit(); } }