This commit is contained in:
2026-04-13 20:19:02 +02:00
commit 31c92aeae9
77 changed files with 4610 additions and 0 deletions
@@ -0,0 +1,5 @@
package eu.konggdev.strikemaps;
public interface Component {
//TODO: Implement base component methods
}
+31
View File
@@ -0,0 +1,31 @@
package eu.konggdev.strikemaps;
import static org.maplibre.android.style.layers.PropertyFactory.lineColor;
import static org.maplibre.android.style.layers.PropertyFactory.lineWidth;
import eu.konggdev.strikemaps.app.AppController;
import android.os.Bundle;
import android.util.Log;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
AppController app;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
app = new AppController(this);
app.init();
}
public void logcat(String tag, String log) {
Log.i(tag, log);
}
public void logcat(String log) {
Log.i("LogcatGeneric", log);
}
}
@@ -0,0 +1,45 @@
package eu.konggdev.strikemaps.app;
import android.content.SharedPreferences;
import androidx.appcompat.app.AppCompatActivity;
import eu.konggdev.strikemaps.MainActivity;
import eu.konggdev.strikemaps.R;
import eu.konggdev.strikemaps.map.MapComponent;
import eu.konggdev.strikemaps.ui.UIComponent;
import static android.content.Context.MODE_PRIVATE;
public class AppController {
private final MainActivity appActivity;
private MapComponent map;
private UIComponent ui;
public AppController(MainActivity refActivity) {
appActivity = refActivity;
}
public void logcat(String log) {
appActivity.logcat(log);
}
public UIComponent getUi() {
if (ui == null) init();
return ui;
}
public MapComponent getMap() {
if (map == null) init();
return map;
}
public SharedPreferences getPrefs() {
return getActivity().getSharedPreferences("user_prefs", MODE_PRIVATE);
}
public AppCompatActivity getActivity() { return appActivity; }
public void init() {
if (getActivity().getSupportActionBar() != null)
getActivity().getSupportActionBar().hide();
if(map == null) map = new MapComponent(this);
if(ui == null) {
ui = new UIComponent(this, map);
ui.swapScreen(R.layout.screen_main); //Initial
}
}
}
@@ -0,0 +1,41 @@
package eu.konggdev.strikemaps.factory;
import android.app.AlertDialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import eu.konggdev.strikemaps.app.AppController;
import eu.konggdev.strikemaps.ui.element.item.PreviewItem;
import org.maplibre.geojson.Feature;
import java.util.List;
import java.util.function.Consumer;
//FIXME: Move Item functions into specific classes for specific types - e.g. StyleItem
public final class AlertDialogFactory {
public static AlertDialog pointSelector(AppController app, List<Feature> features, Consumer<Feature> callback) {
LinearLayout layout = new LinearLayout(app.getActivity());
layout.setOrientation(LinearLayout.VERTICAL);
ScrollView scrollView = new ScrollView(app.getActivity());
scrollView.addView(layout);
AlertDialog dialog = new AlertDialog.Builder(app.getActivity())
.setView(scrollView)
.create();
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#000000")));
for (Feature feature : features) {
View itemView = PreviewItem.fromFeature(feature).makeView(app.getUi(), v -> {
dialog.dismiss();
new android.os.Handler(android.os.Looper.getMainLooper())
.post(() -> callback.accept(feature));
});
layout.addView(itemView);
}
return dialog;
}
}
@@ -0,0 +1,109 @@
package eu.konggdev.strikemaps.helper;
import android.content.res.AssetManager;
import android.os.Environment;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import eu.konggdev.strikemaps.app.AppController;
//FIXME: Ugly
public final class FileHelper {
public static String loadStringFromAssetFile(String filePath, AppController app) {
try (InputStream is = app.getActivity().getAssets().open(filePath)) {
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
return new String(buffer, StandardCharsets.UTF_8);
} catch (IOException ex) {
ex.printStackTrace();
return null;
}
}
public static String loadStringFromUserFile(String filePath) {
File file = new File(filePath);
try (FileInputStream fis = new FileInputStream(file)) {
int size = fis.available();
byte[] buffer = new byte[size];
fis.read(buffer);
return new String(buffer, StandardCharsets.UTF_8);
} catch (IOException ex) {
ex.printStackTrace();
return null;
}
}
public static String[] getAssetFiles(String path, String fileExt, AppController app) {
AssetManager assetManager = app.getActivity().getAssets();
try {
if (path != null && path.startsWith("/")) {
path = path.substring(1);
}
String fullPath = (path == null || path.isEmpty()) ? "" : path;
String[] files = assetManager.list(fullPath);
if (files == null) return new String[0];
if (fileExt == null || fileExt.isEmpty())
return files;
List<String> filtered = new ArrayList<>();
for (String file : files) {
if (file.toLowerCase().endsWith(fileExt.toLowerCase())) {
filtered.add((fullPath.isEmpty() ? "" : fullPath + "/") + file);
}
}
return filtered.toArray(new String[0]);
} catch (IOException e) {
e.printStackTrace();
return new String[0];
}
}
public static InputStream openAssetStream(String path, AppController app) {
try {
return app.getActivity().getAssets().open(path);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static String[] getUserFiles(String path, String fileExt, AppController app) {
String packageName = app.getActivity().getPackageName();
File userDirectory = new File(Environment.getExternalStorageDirectory(), "Android/data/" + packageName + "/" + path);
if (!userDirectory.exists() || !userDirectory.isDirectory())
return new String[0];
File[] files = userDirectory.listFiles(new FilenameFilter() {
@Override
public boolean accept(File dir, String filename) {
if (fileExt == null || fileExt.isEmpty()) {
return true;
}
return filename.toLowerCase().endsWith(fileExt.toLowerCase());
}
});
if (files == null || files.length == 0) {
return new String[0];
}
List<String> fileList = new ArrayList<>();
for (File file : files) {
fileList.add(file.getAbsolutePath());
}
return fileList.toArray(new String[0]);
}
}
@@ -0,0 +1,40 @@
package eu.konggdev.strikemaps.helper;
import android.content.SharedPreferences;
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";
//Defaults
private static final String DEFAULT_MAP_STYLE = "bundled/style/classic.style.json";
private static final String DEFAULT_MAP_RENDERER = "mapLibre";
private static final boolean DEFAULT_PERSIST_LOCATION_ENABLED = true;
private static final boolean DEFAULT_LAST_LOCATION_ENABLED = false;
public static String startupMapStyle(SharedPreferences prefs) {
return prefs.getString(KEY_STARTUP_MAP_STYLE, DEFAULT_MAP_STYLE);
}
public static String mapRenderer(SharedPreferences prefs) {
return prefs.getString(KEY_MAP_RENDERER, DEFAULT_MAP_RENDERER);
}
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();
}
}
@@ -0,0 +1,95 @@
package eu.konggdev.strikemaps.map;
import java.util.*;
import eu.konggdev.strikemaps.Component;
import eu.konggdev.strikemaps.factory.AlertDialogFactory;
import eu.konggdev.strikemaps.helper.UserPrefsHelper;
import eu.konggdev.strikemaps.map.renderer.implementation.VtmRenderer;
import org.maplibre.android.geometry.LatLng;
import org.maplibre.geojson.Feature;
import eu.konggdev.strikemaps.app.AppController;
import eu.konggdev.strikemaps.map.overlay.MapOverlay;
import eu.konggdev.strikemaps.map.renderer.implementation.MapLibreNativeRenderer;
import eu.konggdev.strikemaps.map.renderer.MapRenderer;
import eu.konggdev.strikemaps.ui.fragment.layout.content.main.FragmentLayoutContentMap;
public class MapComponent implements Component {
MapRenderer mapRenderer;
AppController app;
public String style;
public Map<Class<? extends MapOverlay>, MapOverlay> overlays = new HashMap<>();
public MapComponent(AppController ref) {
this.app = ref;
switch(UserPrefsHelper.mapRenderer(app.getPrefs())) {
case "vtm":
this.mapRenderer = new VtmRenderer(app, this);
break;
case "mapLibre":
default: //This shouldn't happen
this.mapRenderer = new MapLibreNativeRenderer(app, this);
break;
};
}
public FragmentLayoutContentMap toFragment() {
return new FragmentLayoutContentMap(mapRenderer.getView());
}
public void setStyle(String style) {
this.style = style;
mapRenderer.reload();
}
public void switchOverlay(MapOverlay overlay) {
if (hasOverlay(overlay)) overlays.remove(overlay.getClass());
else overlays.put(overlay.getClass(), overlay);
update();
}
public boolean hasOverlay(MapOverlay overlay) {
return overlays.containsKey(overlay.getClass());
}
public boolean hasOverlay(Class<? extends MapOverlay> overlay) {
return overlays.containsKey(overlay);
}
public void selectPoint(Feature selection) {
//FIXME: Put back FragmentPointPreviewPopup (private code atm)
}
public void onOverlayUpdate() {
update();
}
public void update() {
if(mapRenderer != null && style != null) mapRenderer.reload();
}
public boolean onMapClick(LatLng point) {
List<Feature> features = mapRenderer.featuresAtPoint(point);
switch (features.size()) {
case 0:
//TODO: Implement point selection for no POI found (MIGHT be done on long click??)
//Maybe collapse UI? (Hide/show UI feature)... could be user configurable
break;
case 1:
selectPoint(features.get(0));
break;
default:
app.getUi().alert(
AlertDialogFactory.pointSelector(app, features, selectedItem -> {
selectPoint(selectedItem);
}));
}
return true;
}
public boolean onMapLongClick(LatLng point) {
//TODO: Likely Nonfeature(?) point selection
return true;
}
}
@@ -0,0 +1,16 @@
package eu.konggdev.strikemaps.map.layer;
import org.maplibre.android.style.layers.Layer;
import org.maplibre.android.style.sources.GeoJsonSource;
import java.util.List;
//TOOD: Make not strictly MapLibre reliant
public class MapLayer {
public GeoJsonSource source;
public Layer layer;
public MapLayer(GeoJsonSource source, Layer layer) {
this.source = source;
this.layer = layer;
}
}
@@ -0,0 +1,8 @@
package eu.konggdev.strikemaps.map.overlay;
import eu.konggdev.strikemaps.map.layer.MapLayer;
/* More or less a data-driven layer factory */
public interface MapOverlay {
public MapLayer makeLayer();
}
@@ -0,0 +1,62 @@
package eu.konggdev.strikemaps.map.overlay.overlay;
import android.graphics.Color;
import android.location.Location;
import android.location.LocationListener;
import androidx.annotation.NonNull;
import eu.konggdev.strikemaps.app.AppController;
import eu.konggdev.strikemaps.map.MapComponent;
import eu.konggdev.strikemaps.map.layer.MapLayer;
import eu.konggdev.strikemaps.map.overlay.MapOverlay;
import eu.konggdev.strikemaps.provider.LocationDataProvider;
import org.maplibre.android.style.layers.CircleLayer;
import org.maplibre.android.style.layers.Property;
import org.maplibre.android.style.sources.GeoJsonSource;
import org.maplibre.geojson.Feature;
import org.maplibre.geojson.FeatureCollection;
import org.maplibre.geojson.Point;
import static org.maplibre.android.style.layers.PropertyFactory.*;
public class LocationOverlay implements MapOverlay, LocationListener {
LocationDataProvider locationDataProvider;
AppController app;
MapComponent map;
public Location currentLocation = null;
public LocationOverlay(AppController app) {
this.app = app;
this.map = app.getMap();
this.locationDataProvider = new LocationDataProvider(app.getActivity(), this);
}
@Override
public MapLayer makeLayer() {
GeoJsonSource source = new GeoJsonSource(
"location",
FeatureCollection.fromFeatures(new Feature[]{}) // empty
);
if (currentLocation != null)
source.setGeoJson(Feature.fromGeometry(Point.fromLngLat(currentLocation.getLongitude(), currentLocation.getLatitude())));
CircleLayer layer = new CircleLayer("location", "location");
layer.setProperties(
circleRadius(5f),
circleColor(Color.parseColor("#1E88E5")),
circleStrokeColor(Color.WHITE),
circleStrokeWidth(1.5f),
circlePitchAlignment(Property.CIRCLE_PITCH_ALIGNMENT_MAP)
);
return new MapLayer(source, layer);
}
@Override
public void onLocationChanged(@NonNull Location location) {
this.currentLocation = location;
map.onOverlayUpdate();
}
}
@@ -0,0 +1,11 @@
package eu.konggdev.strikemaps.map.overlay.overlay;
import eu.konggdev.strikemaps.map.layer.MapLayer;
import eu.konggdev.strikemaps.map.overlay.MapOverlay;
public class PointSelectionOverlay implements MapOverlay {
@Override
public MapLayer makeLayer() {
return null;
}
}
@@ -0,0 +1,19 @@
package eu.konggdev.strikemaps.map.renderer;
import android.view.View;
import android.view.ViewGroup;
import eu.konggdev.strikemaps.map.layer.MapLayer;
import org.maplibre.android.style.layers.Layer;
import org.maplibre.android.geometry.LatLng;
import org.maplibre.geojson.Feature;
import java.util.List;
public interface MapRenderer {
void reload();
View getView();
List<Feature> featuresAtPoint(LatLng point);
}
@@ -0,0 +1,78 @@
package eu.konggdev.strikemaps.map.renderer.implementation;
import android.view.View;
import androidx.annotation.NonNull;
import eu.konggdev.strikemaps.helper.FileHelper;
import eu.konggdev.strikemaps.helper.UserPrefsHelper;
import eu.konggdev.strikemaps.map.overlay.MapOverlay;
import eu.konggdev.strikemaps.map.layer.MapLayer;
import eu.konggdev.strikemaps.map.renderer.MapRenderer;
import org.maplibre.android.MapLibre;
import org.maplibre.android.geometry.LatLng;
import org.maplibre.android.maps.MapLibreMap;
import org.maplibre.android.maps.MapView;
import org.maplibre.android.maps.OnMapReadyCallback;
import org.maplibre.android.maps.Style;
import org.maplibre.geojson.Feature;
import java.util.List;
import eu.konggdev.strikemaps.app.AppController;
import eu.konggdev.strikemaps.map.MapComponent;
public class MapLibreNativeRenderer implements MapRenderer, OnMapReadyCallback {
AppController app;
MapComponent controller;
MapLibreMap map;
final MapView mapView;
public MapLibreNativeRenderer(AppController app, MapComponent controller) {
this.app = app;
this.controller = controller;
MapLibre.getInstance(app.getActivity());
this.mapView = new MapView(app.getActivity());
mapView.onCreate(null);
mapView.getMapAsync(this);
}
void passLayer(MapLayer layer) {
map.getStyle().addSource(layer.source);
map.getStyle().addLayer(layer.layer);
}
@Override
public void reload() {
map.setStyle(new Style.Builder().fromJson(controller.style), style -> {
for(MapOverlay overlay : controller.overlays.values()) {
passLayer(overlay.makeLayer());
}
});
}
@Override
public View getView() {
return mapView;
}
@Override
public List<Feature> featuresAtPoint(LatLng point) {
return map.queryRenderedFeatures(map.getProjection().toScreenLocation(point));
}
@Override
public void onMapReady(@NonNull MapLibreMap maplibreMap) {
this.map = maplibreMap;
controller.style = FileHelper.loadStringFromAssetFile(UserPrefsHelper.startupMapStyle(app.getPrefs()), app);
//I have my own implementation of attribution that credits MapLibre among others, it's not as bad as it looks :)
map.getUiSettings().setLogoEnabled(false);
map.getUiSettings().setAttributionEnabled(false);
map.addOnMapClickListener(point -> controller.onMapClick(point));
map.addOnMapLongClickListener(point -> controller.onMapLongClick(point));
this.reload();
}
}
@@ -0,0 +1,50 @@
package eu.konggdev.strikemaps.map.renderer.implementation;
import android.view.View;
import eu.konggdev.strikemaps.app.AppController;
import eu.konggdev.strikemaps.map.MapComponent;
import eu.konggdev.strikemaps.map.renderer.MapRenderer;
import okhttp3.OkHttpClient;
import org.maplibre.android.geometry.LatLng;
import org.maplibre.geojson.Feature;
import org.oscim.android.MapView;
import org.oscim.map.Map;
import org.oscim.tiling.source.OkHttpEngine;
import org.oscim.tiling.source.oscimap4.OSciMap4TileSource;
import java.util.Collections;
import java.util.List;
public class VtmRenderer implements MapRenderer {
AppController app;
MapComponent controller;
Map map;
final MapView mapView;
public VtmRenderer(AppController app, MapComponent controller) {
this.app = app;
this.controller = controller;
this.mapView = new MapView(app.getActivity());
this.map = mapView.map();
}
@Override
public void reload() {
//TODO
OkHttpClient.Builder builder = new OkHttpClient.Builder();
OSciMap4TileSource tileSource = OSciMap4TileSource.builder().httpFactory(new OkHttpEngine.OkHttpFactory(builder)).build();
map.setBaseMap(tileSource);
}
@Override
public View getView() {
return mapView;
}
@Override
public List<Feature> featuresAtPoint(LatLng point) {
return Collections.emptyList();
}
}
@@ -0,0 +1,12 @@
package eu.konggdev.strikemaps.map.source;
public class MapSource {
public final String url;
public final String type;
public final String schema;
public MapSource(String url, String type, String schema) {
this.url = url;
this.type = type;
this.schema = schema;
}
}
@@ -0,0 +1,5 @@
package eu.konggdev.strikemaps.provider;
public class HttpDataProvider implements Provider {
//TODO
}
@@ -0,0 +1,43 @@
package eu.konggdev.strikemaps.provider;
import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import java.util.List;
public class LocationDataProvider implements Provider {
private LocationManager locationManager;
private List<String> locationManagerProviders = List.of(LocationManager.GPS_PROVIDER, LocationManager.NETWORK_PROVIDER);
public LocationDataProvider(AppCompatActivity activity, LocationListener listener) {
locationManager = (LocationManager) activity.getSystemService(Context.LOCATION_SERVICE);
//TODO: Move permission request to UI
if(ContextCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
Location initLocation = null;
for (String provider : locationManagerProviders) {
if(locationManager.isProviderEnabled(provider)) {
if (initLocation == null) {
initLocation = locationManager.getLastKnownLocation(provider);
if (initLocation != null)
listener.onLocationChanged(initLocation);
}
locationManager.requestLocationUpdates(
provider,
1000,
1,
listener
);
}
}
}
}
}
@@ -0,0 +1,5 @@
package eu.konggdev.strikemaps.provider;
public interface Provider {
}
@@ -0,0 +1,77 @@
package eu.konggdev.strikemaps.ui;
import android.app.AlertDialog;
import android.view.View;
import androidx.annotation.NonNull;
import com.google.common.collect.BiMap;
import eu.konggdev.strikemaps.Component;
import eu.konggdev.strikemaps.R;
import eu.konggdev.strikemaps.app.AppController;
import eu.konggdev.strikemaps.map.MapComponent;
import eu.konggdev.strikemaps.ui.element.UIRegion;
import eu.konggdev.strikemaps.ui.fragment.layout.FragmentLayoutControls;
import eu.konggdev.strikemaps.ui.fragment.layout.content.main.FragmentLayoutContentSettings;
import eu.konggdev.strikemaps.ui.screen.Screen;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Consumer;
public class UIComponent implements Component {
@NonNull AppController app;
private Map<Integer, Screen> screens;
private Integer currentScreen;
public UIComponent(AppController app, MapComponent map) {
this.app = app;
this.screens = Map.of(
//Main screen
R.layout.screen_main, new Screen(
//App reference
app,
//Map view
map.toFragment(), //FragmentLayoutContentMap
//Main screen init regions definition
Map.of(R.id.bottomUi, new UIRegion(new FragmentLayoutControls(app, R.id.bottomUi), R.id.bottomUi)), //TODO: Probably stop referencing layout 3(!) times everytime
//Layout
R.layout.screen_main //TODO: Define this for the Screen without duplicating the reference
),
//Settings screen
R.layout.screen_settings, new Screen(
app,
//Settings
new FragmentLayoutContentSettings(),
/* No regions defined in settings
Entire screen is just the main view */
new HashMap<>(),
//Layout
R.layout.screen_settings
)
);
}
public void swapScreen(Integer screen) {
currentScreen = screen;
getCurrentScreen().attachAll();
}
public Screen getCurrentScreen() {
return getScreen(currentScreen);
}
public Screen getScreen(Integer screen) {
return screens.get(screen);
}
public void alert(AlertDialog dialog) {
dialog.show();
}
public <T> void alert(AlertDialog dialog, Consumer<T> callback) {
dialog.show();
}
public View inflateUi(int layout) {
return app.getActivity().getLayoutInflater().inflate(layout, null);
}
}
@@ -0,0 +1,44 @@
package eu.konggdev.strikemaps.ui.element;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import eu.konggdev.strikemaps.ui.fragment.layout.Layout;
import java.util.ArrayDeque;
public class UIRegion {
private final ArrayDeque<Fragment> previousFragments = new ArrayDeque<>();
private Fragment stockFragment;
private Fragment currentFragment;
public Integer layoutId;
public UIRegion(@NonNull Fragment initFragment, Integer refLayoutId) {
this.currentFragment = initFragment;
this.stockFragment = initFragment;
this.layoutId = refLayoutId;
}
public Fragment getFragment() {
return currentFragment;
}
public void setFragment(Fragment fragment) {
previousFragments.add(currentFragment);
currentFragment = fragment;
}
public void overwriteStockFragment(Fragment fragment) {
stockFragment = fragment;
}
public void back() {
if (!previousFragments.isEmpty()) {
currentFragment = previousFragments.pop();
} else {
currentFragment = stockFragment;
}
}
}
@@ -0,0 +1,77 @@
package eu.konggdev.strikemaps.ui.element.item;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import androidx.annotation.NonNull;
import eu.konggdev.strikemaps.R;
import eu.konggdev.strikemaps.app.AppController;
import eu.konggdev.strikemaps.helper.FileHelper;
import eu.konggdev.strikemaps.map.MapComponent;
import eu.konggdev.strikemaps.ui.UIComponent;
import org.json.JSONObject;
import java.io.InputStream;
public class GenericItem implements UIItem {
@NonNull public String name;
public Bitmap image;
public Runnable onClick;
boolean hasImage;
public GenericItem(String refName) {
this.name = refName;
hasImage = false;
}
public GenericItem(String refName, Runnable onClick) {
this.name = refName;
this.onClick = onClick;
hasImage = false;
}
public GenericItem(String refName, Bitmap refImage) {
this.name = refName;
this.image = refImage;
hasImage = true;
}
public GenericItem(String refName, Bitmap refImage, Runnable onClick) {
this.name = refName;
this.image = refImage;
this.onClick = onClick;
hasImage = true;
}
//FIXME: Ugly glue static constructor
public final static GenericItem fromStyle(String style, AppController app, MapComponent map) {
try {
JSONObject styleJson = new JSONObject(style);
String name = "Unknown"; //Fallback name
if (styleJson.has("name")) name = styleJson.getString("name");
if (styleJson.has("icon")) {
switch(styleJson.getString("icon").split("//")[0]) {
//TODO: https
case "assets:":
Bitmap icon = BitmapFactory.decodeStream(FileHelper.openAssetStream("bundled/icon/" + styleJson.getString("icon").split("//")[1], app));
return new GenericItem(name, icon, () -> map.setStyle(style));
default:
app.logcat("Unimplemented icon source requested in style: " + name);
return new GenericItem(name, () -> map.setStyle(style));
}
}
return new GenericItem(name, () -> map.setStyle(style));
} catch (Exception e) {
e.printStackTrace();
return new GenericItem("Exception!", () -> map.setStyle(style));
}
}
public View makeView(UIComponent spawner) {
View v = spawner.inflateUi(R.layout.item_generic);
//FIXME: These shouldn't be casted like that!
((TextView) v.findViewById(R.id.name)).setText(name);
if(image != null) ((ImageButton) v.findViewById(R.id.image)).setImageBitmap(image);
if(onClick != null) v.findViewById(R.id.image).setOnClickListener(click(onClick));
return v;
}
}
@@ -0,0 +1,41 @@
package eu.konggdev.strikemaps.ui.element.item;
import android.graphics.Bitmap;
import android.view.View;
import android.widget.TextView;
import eu.konggdev.strikemaps.ui.UIComponent;
import org.maplibre.geojson.Feature;
import eu.konggdev.strikemaps.R;
public class PreviewItem implements UIItem {
public String name;
public String type;
public Bitmap image;
boolean hasImage;
public PreviewItem(String refName, String refType) {
this.name = refName;
this.type = refType;
hasImage = false;
}
public PreviewItem(String refName, String refType, Bitmap refImage) {
this.name = refName;
this.type = refType;
this.image = refImage;
hasImage = true;
}
public static PreviewItem fromFeature(Feature feature) {
return new PreviewItem(feature.getStringProperty("name"), feature.getStringProperty("class"));
}
public View makeView(UIComponent spawner) {
View view = spawner.inflateUi(R.layout.item_preview);
((TextView) view.findViewById(R.id.choiceName)).setText(name);
((TextView) view.findViewById(R.id.type)).setText(type);
return view;
}
public View makeView(UIComponent spawner, View.OnClickListener onClick) {
View view = makeView(spawner);
view.setOnClickListener(onClick);
return view;
}
}
@@ -0,0 +1,16 @@
package eu.konggdev.strikemaps.ui.element.item;
import android.view.View;
import eu.konggdev.strikemaps.ui.UIComponent;
public interface UIItem {
abstract View makeView(UIComponent spawner);
default View.OnClickListener click(Runnable action) {
return v -> action.run();
}
default View.OnLongClickListener longClick(Runnable action) { return v -> { action.run(); return true; };}
}
@@ -0,0 +1,90 @@
package eu.konggdev.strikemaps.ui.fragment;
import android.view.MotionEvent;
import android.view.View;
import androidx.fragment.app.Fragment;
import eu.konggdev.strikemaps.ui.element.UIRegion;
public interface ContainerFragment {
abstract public Integer getRegion();
abstract public Fragment toFragment();
//Helper methods (ugly)
//FIXME
default void setupButton(View view, int button, View.OnClickListener onClick) {
view.findViewById(button)
.setOnClickListener(onClick);
}
default void setupButton(View view, int button, View.OnClickListener onClick, View.OnLongClickListener onLongClick) {
View buttonView = view.findViewById(button);
buttonView.setOnClickListener(onClick);
buttonView.setOnLongClickListener(onLongClick);
}
default void setupButton(View view, int button, View.OnLongClickListener onLongClick) {
view.findViewById(button)
.setOnLongClickListener(onLongClick);
}
default View.OnClickListener click(Runnable action) {
return v -> action.run();
}
default View.OnLongClickListener longClick(Runnable action) { return v -> { action.run(); return true; };}
//TODO: Make animation less wonky
default void setupDragHandle(View dragHandle, View layout, Runnable closeAction) {
final float[] dY = new float[1];
dragHandle.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getActionMasked()) {
case MotionEvent.ACTION_DOWN:
dY[0] = event.getRawY() - layout.getY();
return true;
case MotionEvent.ACTION_MOVE:
float newY = event.getRawY() - dY[0];
if (newY >= 0) {
layout.setY(newY);
}
return true;
case MotionEvent.ACTION_UP:
if (layout.getY() > layout.getHeight() / 4) {
layout.animate()
.scaleX(0f)
.scaleY(0f)
.alpha(0f)
.setDuration(300)
.withEndAction(new Runnable() {
@Override
public void run() {
layout.setVisibility(View.GONE);
closeAction.run();
layout.setScaleX(1f);
layout.setScaleY(1f);
layout.setAlpha(1f);
layout.setY(0f);
}
})
.start();
} else {
layout.animate()
.translationY(0f)
.setDuration(200)
.start();
}
return true;
default:
return false;
}
}
});
}
}
@@ -0,0 +1,7 @@
package eu.konggdev.strikemaps.ui.fragment;
import androidx.fragment.app.Fragment;
public class FragmentEmptyPlaceholder extends Fragment {
//:)
}
@@ -0,0 +1,120 @@
package eu.konggdev.strikemaps.ui.fragment.layout;
import android.Manifest;
import android.app.AlertDialog;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import android.widget.TextView;
import android.widget.Toast;
import eu.konggdev.strikemaps.R;
import eu.konggdev.strikemaps.app.AppController;
import eu.konggdev.strikemaps.helper.UserPrefsHelper;
import eu.konggdev.strikemaps.map.overlay.overlay.LocationOverlay;
import eu.konggdev.strikemaps.ui.fragment.popup.FragmentMapChangePopup;
public class FragmentLayoutControls extends Fragment implements Layout {
AppController app;
View rootView;
private final Integer region;
// Action definitions
//*//
public void notImplemented() { //Should never be called in release
Toast.makeText(requireContext(), "Not implemented yet\nWait for release", Toast.LENGTH_SHORT).show();
}
public void toggleLocationService() {
if (ContextCompat.checkSelfPermission(requireContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
} else {
app.getMap().switchOverlay(new LocationOverlay(app));
setupView();
}
}
public void zoomToLocation() {
if(!app.getMap().hasOverlay(LocationOverlay.class)) {
Toast.makeText(requireContext(), "Hold to enable location", Toast.LENGTH_SHORT).show();
return;
}
}
public void attributtionDialog() {
AlertDialog dialog = new AlertDialog.Builder(app.getActivity())
.setTitle(app.getActivity().getString(R.string.attribution_title))
.setMessage(app.getActivity().getString(R.string.shipped_attribution))
.setPositiveButton("OK", null).show();
}
//*//
public FragmentLayoutControls(AppController app, Integer region) {
super(R.layout.fragment_controls);
this.app = app;
this.region = region;
}
@Override
public Integer getRegion() {
return region;
}
@Override
public Fragment toFragment() {
return this;
}
@Override
public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
this.rootView = view;
/* Restores location enabled status from user prefs,
TODO: Should be moved out of UI code in the future */
if(UserPrefsHelper.persistLocationEnabled(app.getPrefs()) && UserPrefsHelper.lastLocationEnabled(app.getPrefs()) && !app.getMap().hasOverlay(LocationOverlay.class))
toggleLocationService();
this.setupView();
}
public void setupView() {
if (rootView == null) return;
setupButton(rootView, R.id.layersButton, click(() -> app.getUi().getCurrentScreen().open(new FragmentMapChangePopup(app, R.id.bottomUi))));
setupButton(rootView, R.id.attributionButton, click(this::attributtionDialog));
setupButton(rootView, R.id.locationButton, click(this::zoomToLocation), longClick(this::toggleLocationService));
//TODO
setupButton(rootView, R.id.placesButton, click(this::notImplemented));
setupButton(rootView, R.id.placesButton, click(this::notImplemented));
setupButton(rootView, R.id.routeButton, click(this::notImplemented));
setupButton(rootView, R.id.modeButton, click(this::notImplemented));
TextView locationServiceStatusIndicator = rootView.findViewById(R.id.locationServiceStatusIndicator);
if (app.getMap().hasOverlay(LocationOverlay.class)) {
locationServiceStatusIndicator.setBackgroundColor(Color.parseColor("#00FF00")); //green
} else {
locationServiceStatusIndicator.setBackgroundColor(Color.parseColor("#FB0303")); //red
}
if(UserPrefsHelper.persistLocationEnabled(app.getPrefs()))
UserPrefsHelper.lastLocationEnabled(app.getPrefs(), app.getMap().hasOverlay(LocationOverlay.class));
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case 1:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) toggleLocationService();
else Toast.makeText(requireContext(), "You need to grant location permission", Toast.LENGTH_SHORT).show();
}
}
}
@@ -0,0 +1,8 @@
package eu.konggdev.strikemaps.ui.fragment.layout;
import eu.konggdev.strikemaps.ui.fragment.ContainerFragment;
public interface Layout extends ContainerFragment {
}
@@ -0,0 +1,6 @@
package eu.konggdev.strikemaps.ui.fragment.layout.content;
import eu.konggdev.strikemaps.ui.fragment.layout.Layout;
public interface ContentLayout extends Layout {
}
@@ -0,0 +1,33 @@
package eu.konggdev.strikemaps.ui.fragment.layout.content.main;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import eu.konggdev.strikemaps.ui.element.UIRegion;
import eu.konggdev.strikemaps.R;
public class FragmentLayoutContentMap extends Fragment implements MainContentLayout {
View mapView;
public FragmentLayoutContentMap(View refMapView) {
super(R.layout.fragment_map);
this.mapView = refMapView;
}
@Override
public Fragment toFragment() {
return this;
}
@Override
public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
LinearLayout layout = (LinearLayout) view;
layout.addView(mapView);
}
}
@@ -0,0 +1,10 @@
package eu.konggdev.strikemaps.ui.fragment.layout.content.main;
import androidx.fragment.app.Fragment;
public class FragmentLayoutContentSettings implements MainContentLayout {
@Override
public Fragment toFragment() {
return null;
}
}
@@ -0,0 +1,10 @@
package eu.konggdev.strikemaps.ui.fragment.layout.content.main;
import eu.konggdev.strikemaps.R;
import eu.konggdev.strikemaps.ui.fragment.layout.content.ContentLayout;
public interface MainContentLayout extends ContentLayout {
default Integer getRegion() {
return R.id.mainContentView;
}
}
@@ -0,0 +1,61 @@
package eu.konggdev.strikemaps.ui.fragment.popup;
import android.os.Bundle;
import android.view.View;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import android.widget.LinearLayout;
import eu.konggdev.strikemaps.R;
import eu.konggdev.strikemaps.app.AppController;
import eu.konggdev.strikemaps.helper.FileHelper;
import eu.konggdev.strikemaps.factory.AlertDialogFactory;
import eu.konggdev.strikemaps.map.MapComponent;
import eu.konggdev.strikemaps.ui.UIComponent;
import eu.konggdev.strikemaps.ui.element.item.GenericItem;
import org.apache.commons.lang3.ArrayUtils;
import java.util.ArrayList;
import java.util.List;
public class FragmentMapChangePopup extends Fragment implements Popup {
@NonNull AppController app;
@NonNull MapComponent map;
@NonNull UIComponent ui;
private final Integer region;
public FragmentMapChangePopup(AppController app, Integer region) {
super(R.layout.popup_map_change);
this.app = app;
this.map = app.getMap();
this.ui = app.getUi();
this.region = region;
}
@Override
public Integer getRegion() {
return region;
}
@Override
public Fragment toFragment() {
return this;
}
@Override
public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
//FIXME
setupButton(view, R.id.closeButton, click(() -> ui.getCurrentScreen().closePopup()));
setupDragHandle(view, view, () -> ui.getCurrentScreen().closePopup());
String[] stylePaths = ArrayUtils.addAll(FileHelper.getAssetFiles("bundled/style", ".style.json", app), FileHelper.getUserFiles("style", ".style.json", app));
List<View> views = new ArrayList<>();
LinearLayout stylesLayout = view.findViewById(R.id.stylesLayout);
for(String i : stylePaths) {
if(i.startsWith("/storage")) stylesLayout.addView(GenericItem.fromStyle(FileHelper.loadStringFromUserFile(i), app, map).makeView(ui));
else stylesLayout.addView(GenericItem.fromStyle(FileHelper.loadStringFromAssetFile(i, app), app, map).makeView(ui));
}
}
}
@@ -0,0 +1,8 @@
package eu.konggdev.strikemaps.ui.fragment.popup;
import androidx.fragment.app.Fragment;
import eu.konggdev.strikemaps.ui.fragment.ContainerFragment;
public interface Popup extends ContainerFragment {
}
@@ -0,0 +1,77 @@
package eu.konggdev.strikemaps.ui.screen;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import java.util.List;
import java.util.Map;
import eu.konggdev.strikemaps.R;
import eu.konggdev.strikemaps.app.AppController;
import eu.konggdev.strikemaps.ui.fragment.ContainerFragment;
import eu.konggdev.strikemaps.ui.fragment.layout.content.main.MainContentLayout;
import eu.konggdev.strikemaps.ui.fragment.popup.Popup;
import eu.konggdev.strikemaps.ui.element.UIRegion;
public class Screen {
@NonNull AppController app;
public Screen(AppController app, MainContentLayout mainContent, Map<Integer, UIRegion> regions, Integer layout) {
this.app = app;
this.layout = layout;
this.mainContent = mainContent;
this.uiRegions = regions;
}
private final Integer layout;
private MainContentLayout mainContent;
Map<Integer, UIRegion> uiRegions;
public Integer popup;
public void open(ContainerFragment fragment) {
if(fragment instanceof Popup && popup != null) return;
if(fragment instanceof Popup)
popup = fragment.getRegion();
setFragment(uiRegions.get(fragment.getRegion()), fragment.toFragment());
}
public void closePopup() {
if(popup != null) {
UIRegion popupRegion = uiRegions.get(popup);
popupRegion.back();
/* If newFragment is still a popup, assign the current popup value to the new fragment
otherwise, set the current popup value to null */
if(popupRegion.getFragment() instanceof Popup) {
popup = popupRegion.layoutId;
} else {
popup = null;
}
setFragment(popupRegion, popupRegion.getFragment());
}
}
public void setFragment(UIRegion region, Fragment fragment) {
if (region == null) return;
region.setFragment(fragment);
fragmentTransaction(region.layoutId, fragment);
}
public void fragmentTransaction(int layoutId, Fragment fragment) {
app.getActivity().getSupportFragmentManager()
.beginTransaction()
.replace(layoutId, fragment)
.commit();
}
public void attachAll() {
app.getActivity().setContentView(layout);
fragmentTransaction(R.id.mainContentView, mainContent.toFragment());
for (UIRegion region : uiRegions.values()) {
setFragment(region, region.getFragment());
}
}
}