From 972b0cf5ff61fd7eabfb8960fa474cb80dcde80a Mon Sep 17 00:00:00 2001 From: Iván Ávalos Date: Sun, 16 Jan 2022 00:00:20 -0600 Subject: Default map changed to OpenStreetMap.fr (because .org one doesn't work), and now each map URL has its own cache dir --- androidApp/build.gradle.kts | 3 +- .../TrackerMap/android/map/MapFragment.kt | 36 ++++++++++++++-------- .../TrackerMap/android/map/UnitMapFragment.kt | 2 +- .../TrackerMap/android/units/UnitsViewModel.kt | 10 ++++-- androidApp/src/main/res/values/strings.xml | 3 +- 5 files changed, 34 insertions(+), 20 deletions(-) diff --git a/androidApp/build.gradle.kts b/androidApp/build.gradle.kts index 07fe740..cd63ac0 100644 --- a/androidApp/build.gradle.kts +++ b/androidApp/build.gradle.kts @@ -39,7 +39,7 @@ val googleImplementation by configurations dependencies { implementation(project(":shared")) - implementation("com.google.android.material:material:1.4.0") + implementation("com.google.android.material:material:1.5.0") implementation("androidx.appcompat:appcompat:1.4.0") implementation("androidx.constraintlayout:constraintlayout:2.1.2") implementation("com.squareup.okhttp3:okhttp:4.9.1") @@ -56,6 +56,7 @@ dependencies { implementation("com.github.zerobranch:SwipeLayout:1.3.1") implementation("com.github.addisonElliott:SegmentedButton:3.1.9") implementation("mil.nga.sf:sf-wkt:1.0.1") + implementation("com.soywiz.korlibs.krypto:krypto:2.4.12") implementation(group = "", name = "WhirlyGlobeMaply", ext = "aar") googleImplementation(platform("com.google.firebase:firebase-bom:29.0.3")) diff --git a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/map/MapFragment.kt b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/map/MapFragment.kt index 60c8a47..e4fb8ea 100644 --- a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/map/MapFragment.kt +++ b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/map/MapFragment.kt @@ -11,6 +11,7 @@ import androidx.core.content.ContextCompat import androidx.core.content.res.ResourcesCompat import androidx.core.graphics.drawable.toBitmap import com.mousebird.maply.* +import com.soywiz.krypto.md5 import mil.nga.sf.Polygon import mil.nga.sf.util.SFException import mil.nga.sf.wkt.GeometryReader @@ -56,17 +57,10 @@ class MapFragment : GlobeMapFragment() { override fun controlHasStarted() { Log.d("MapFragment", "controlHasStarted") - val cacheDirName = "stamen_watercolor6" - val cacheDir = File(activity!!.cacheDir, cacheDirName) - cacheDir.mkdir() - if (tileInfo == null) { - tileInfo = RemoteTileInfoNew( - getString(R.string.maps_streets_tile_url), - 0, - 21 - ) - (tileInfo as RemoteTileInfoNew).cacheDir = cacheDir + val mapUrl = getString(R.string.maps_streets_tile_url) + val tmpInfo = RemoteTileInfoNew(mapUrl, 0, 21) + tileInfo = makeTileInfo(getString(R.string.maps_streets_tile_url), tmpInfo) } val params = SamplingParams() @@ -318,9 +312,25 @@ class MapFragment : GlobeMapFragment() { } } - fun updateTileInfo(tileInfo: TileInfoNew) { - this.tileInfo = tileInfo - loader?.changeTileInfo(tileInfo) + private fun makeTileInfo(url: String, tileInfo: TileInfoNew): TileInfoNew? { + return context?.let { + val cacheDirName = url.toByteArray(Charsets.UTF_8).md5().hex + val cacheDirMap = File(it.cacheDir, cacheDirName) + cacheDirMap.mkdir() + Log.d("MapFragment", "Cache dir for $url = ${cacheDirMap.absolutePath}") + + (tileInfo as? RemoteTileInfoNew)?.cacheDir = cacheDirMap + tileInfo + } + } + + fun updateTileInfo(url: String, tileInfo: TileInfoNew) { + context?.let { + this.tileInfo = makeTileInfo(url, tileInfo) + this.tileInfo?.let { + loader?.changeTileInfo(it) + } + } } private fun getIcon(markerType: MarkerType): Bitmap { diff --git a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/map/UnitMapFragment.kt b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/map/UnitMapFragment.kt index 09f41a8..c17e6a4 100644 --- a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/map/UnitMapFragment.kt +++ b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/map/UnitMapFragment.kt @@ -96,7 +96,7 @@ class UnitMapFragment(private val unitsViewModel: UnitsViewModel) : UnitFragment unitsViewModel.mapTileInfo.observe(viewLifecycleOwner) { tileInfo -> Log.d("UnitMapFragment", "Loading layer!") - unitsMapFragment.updateTileInfo(tileInfo) + unitsMapFragment.updateTileInfo(tileInfo.first, tileInfo.second) } unitsViewModel.geofences.observe(viewLifecycleOwner) { geofences -> diff --git a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/units/UnitsViewModel.kt b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/units/UnitsViewModel.kt index 08d35a2..94e22fa 100644 --- a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/units/UnitsViewModel.kt +++ b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/units/UnitsViewModel.kt @@ -4,6 +4,10 @@ import android.util.Log import androidx.lifecycle.* import com.mousebird.maply.RemoteTileInfoNew import com.mousebird.maply.TileInfoNew +import com.soywiz.krypto.Hash +import com.soywiz.krypto.Hasher +import com.soywiz.krypto.HasherFactory +import com.soywiz.krypto.md5 import kotlinx.coroutines.DelicateCoroutinesApi import kotlinx.coroutines.flow.collect import kotlinx.coroutines.launch @@ -30,14 +34,14 @@ class UnitsViewModel( private var _unitsDisplayMode = MutableLiveData(UnitsDisplayMode.MAP) private var _units = MutableLiveData>() private var _selectedUnit = MutableLiveData() - private var _mapTileInfo = MutableLiveData() + private var _mapTileInfo = MutableLiveData>() private var _geofences = MutableLiveData>() val searchQuery: LiveData get() = _searchQuery val unitsDisplayMode: LiveData get() = _unitsDisplayMode val units: LiveData> get() = _units val selectedUnit: LiveData get() = _selectedUnit - val mapTileInfo: LiveData get() = _mapTileInfo + val mapTileInfo: LiveData> get() = _mapTileInfo val geofences: LiveData> get() = _geofences init { @@ -88,7 +92,7 @@ class UnitsViewModel( fun setMapLayer(url: String, minZoom: Int = 0, maxZoom: Int = 21) { val tileInfo = RemoteTileInfoNew(url, minZoom, maxZoom) - _mapTileInfo.postValue(tileInfo) + _mapTileInfo.postValue(url to tileInfo) } fun toggleDisplayMode() { diff --git a/androidApp/src/main/res/values/strings.xml b/androidApp/src/main/res/values/strings.xml index 20f5b21..025a7b9 100644 --- a/androidApp/src/main/res/values/strings.xml +++ b/androidApp/src/main/res/values/strings.xml @@ -11,7 +11,7 @@ - https://mt0.google.com/vt/lyrs=m&hl=en&x={x}&y={y}&z={z}&s=Ga + https://a.tile.openstreetmap.fr/osmfr/{z}/{x}/{y}.png https://mt0.google.com/vt/lyrs=y&hl=en&x={x}&y={y}&z={z}&s=Ga @@ -41,7 +41,6 @@ Streets Satellite - Hybrid Account About -- cgit v1.2.3