Skip to content

Commit

Permalink
Fix explore top places layers
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-osm committed Feb 28, 2025
1 parent 19f4f6a commit 461482e
Show file tree
Hide file tree
Showing 13 changed files with 417 additions and 175 deletions.
4 changes: 2 additions & 2 deletions OsmAnd-api/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ apply plugin: 'com.android.library'
apply plugin: 'ivy-publish'

android {
compileSdk 34
buildToolsVersion = "34.0.0"
compileSdk 35
buildToolsVersion = "35.0.0"

defaultConfig {
minSdkVersion 24
Expand Down
5 changes: 4 additions & 1 deletion OsmAnd-shared/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ kotlin {
val commonLoggingVersion = "1.2"
val coroutinesVersion = "1.8.1"
val statelyVersion = "2.1.0"
val coilVersion = "3.1.0"

sourceSets {
commonMain.dependencies {
Expand All @@ -62,6 +63,7 @@ kotlin {
implementation("com.squareup.okio:okio:$okioVersion")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutinesVersion")
implementation("co.touchlab:stately-concurrent-collections:$statelyVersion")
implementation("io.coil-kt.coil3:coil-core:$coilVersion")
}
jvmMain.dependencies {
//implementation(kotlin("stdlib-jdk8"))
Expand All @@ -73,6 +75,7 @@ kotlin {
implementation("androidx.sqlite:sqlite:$sqliteVersion")
implementation("androidx.sqlite:sqlite-framework:$sqliteVersion")
implementation("net.sf.kxml:kxml2:$kxml2Version")
implementation("io.coil-kt.coil3:coil-network-okhttp:$coilVersion")
}
iosMain.dependencies {
implementation("co.touchlab:sqliter-driver:$sqliterVersion")
Expand All @@ -86,7 +89,7 @@ kotlin {

android {
namespace = "net.osmand.shared"
compileSdk = 34
compileSdk = 35
compileOptions {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package net.osmand.shared.util

import android.content.Context
import coil3.Bitmap
import coil3.ImageLoader
import coil3.disk.DiskCache
import coil3.disk.directory
import coil3.memory.MemoryCache
import coil3.request.Disposable
import coil3.request.ImageRequest
import coil3.request.allowHardware
import coil3.toBitmap

class NetworkImageLoader(private val context: Context, useDiskCache: Boolean = false) {

companion object {
private const val DISK_CACHE_SIZE = 1024 * 1024 * 100L // 100MB
private const val DISK_IMAGES_CACHE_DIR = "net_images_cache"
}

private var imageLoader: ImageLoader = ImageLoader.Builder(context)
.allowHardware(false)
.memoryCache {
MemoryCache.Builder()
.maxSizePercent(context, 0.25)
.build()
}
.apply {
if (useDiskCache) {
diskCache {
DiskCache.Builder()
.directory(context.cacheDir.resolve(DISK_IMAGES_CACHE_DIR))
.maxSizeBytes(DISK_CACHE_SIZE)
.build()
}
}
}
.build()

fun loadImage(
url: String, callback: ImageLoaderCallback, handlePlaceholder: Boolean = false
): LoadingImage {
val request = ImageRequest.Builder(context)
.data(url)
.target(
onStart = { placeholder ->
callback.onStart(placeholder?.takeIf { handlePlaceholder }?.toBitmap())
},
onSuccess = { result ->
callback.onSuccess(result.toBitmap())
},
onError = { _ ->
callback.onError()
})
.build()

return LoadingImage(url, imageLoader.enqueue(request))
}
}

interface ImageLoaderCallback {

fun onStart(bitmap: Bitmap?)

fun onSuccess(bitmap: Bitmap)

fun onError()
}

class LoadingImage(val url: String, private val disposable: Disposable) {

fun cancel(): Boolean {
disposable.dispose()
return true
}
}
7 changes: 5 additions & 2 deletions OsmAnd/build-common.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ tasks.register('printc') {
}

android {
compileSdk 34
buildToolsVersion = "34.0.0"
compileSdk 35
buildToolsVersion = "35.0.0"
// compileNdkVersion "android-ndk-r17b"
namespace = "net.osmand.plus"
defaultConfig {
Expand Down Expand Up @@ -404,6 +404,9 @@ dependencies {
// turn off for now
//implementation 'com.atilika.kuromoji:kuromoji-ipadic:0.9.0'
implementation 'com.squareup.picasso:picasso:2.71828'
//implementation("io.coil-kt.coil3:coil-core:3.1.0")
//implementation("io.coil-kt.coil3:coil-network-okhttp:3.1.0")

implementation 'me.zhanghai.android.materialprogressbar:library:1.4.2'
implementation "net.osmand:antpluginlib:3.8.0@aar"
// JS core
Expand Down
5 changes: 5 additions & 0 deletions OsmAnd/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,11 @@ android {
println(osmandTask.getName() + " merge resources")
osmandTask.dependsOn(collectExternalResources)
}
def generateResources= "generate${taskName}Resources"
tasks.named(generateResources).configure { osmandTask ->
println(osmandTask.getName() + " merge resources")
osmandTask.dependsOn(collectExternalResources)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package net.osmand.plus.exploreplaces;

import androidx.annotation.NonNull;

import net.osmand.data.Amenity;
import net.osmand.data.LatLon;
import net.osmand.data.ExploreTopPlacePoint;
Expand All @@ -17,7 +19,7 @@ public interface ExplorePlacesProvider {

@NotNull List<ExploreTopPlacePoint> getDataCollection(QuadRect mapRect, int limit);

void showPointInContextMenu(@NotNull MapActivity it, @NotNull ExploreTopPlacePoint item);
void showPointInContextMenu(@NotNull MapActivity mapActivity, @NotNull ExploreTopPlacePoint item);

void addListener(ExplorePlacesListener listener);

Expand All @@ -30,6 +32,8 @@ public interface ExplorePlacesProvider {
// data version is increased once new data is downloaded
int getDataVersion();

boolean isLoadingRect(@NonNull QuadRect rect);

interface ExplorePlacesListener {
// once new data is downloaded data version is increased
void onNewExplorePlacesDownloaded();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

// TODO use gzip in loading
Expand All @@ -41,18 +43,18 @@
// Extra: display new categories from web
public class ExplorePlacesProviderJava implements ExplorePlacesProvider {

private static final int DEFAULT_LIMIT_POINTS = 200;
public static final int DEFAULT_LIMIT_POINTS = 200;
private static final int NEARBY_MIN_RADIUS = 50;


private static final int MAX_TILES_PER_QUAD_RECT = 12;
private static final double LOAD_ALL_TINY_RECT = 0.5;

private OsmandApplication app;
private final OsmandApplication app;
private volatile int startedTasks = 0;
private volatile int finishedTasks = 0;

private final Set<String> loadingTiles = new HashSet<>(); // Track tiles being loaded
private final Map<String, QuadRect> loadingTiles = new HashMap<>(); // Track tiles being loaded

public ExplorePlacesProviderJava(OsmandApplication app) {
this.app = app;
Expand Down Expand Up @@ -98,11 +100,13 @@ private String getLang() {
return preferredLang;
}

@NonNull
public List<ExploreTopPlacePoint> getDataCollection(QuadRect rect) {
return getDataCollection(rect, DEFAULT_LIMIT_POINTS);
}

public List<ExploreTopPlacePoint> getDataCollection(QuadRect rect, int limit) {
@NonNull
public List<ExploreTopPlacePoint> getDataCollection(QuadRect rect, int limit) {
if (rect == null) {
return Collections.emptyList();
}
Expand Down Expand Up @@ -170,17 +174,22 @@ public List<ExploreTopPlacePoint> getDataCollection(QuadRect rect, int limit) {

@SuppressLint("DefaultLocale")
private void loadTile(int zoom, int tileX, int tileY, String queryLang, PlacesDatabaseHelper dbHelper) {
double left;
double right;
double top;
double bottom;

synchronized (loadingTiles) {
String tileKey = zoom + "_" + tileX + "_" + tileY;
if (loadingTiles.contains(tileKey)) {
if (loadingTiles.containsKey(tileKey)) {
return;
}
loadingTiles.add(tileKey);
left = MapUtils.getLongitudeFromTile(zoom, tileX);
right = MapUtils.getLongitudeFromTile(zoom, tileX + 1);
top = MapUtils.getLatitudeFromTile(zoom, tileY);
bottom = MapUtils.getLatitudeFromTile(zoom, tileY + 1);
loadingTiles.put(tileKey, new QuadRect(left, top, right, bottom));
}
double left = MapUtils.getLongitudeFromTile(zoom, tileX);
double right = MapUtils.getLongitudeFromTile(zoom, tileX + 1);
double top = MapUtils.getLatitudeFromTile(zoom, tileY);
double bottom = MapUtils.getLatitudeFromTile(zoom, tileY + 1);

KQuadRect tileRect = new KQuadRect(left, top, right, bottom);
// Increment the task counter
Expand All @@ -204,11 +213,9 @@ public void onFinish(@NonNull List<? extends OsmandApiFeatureData> result) {
finishedTasks++; // Increment the finished task counter
notifyListeners(startedTasks != finishedTasks);
}
if (result != null) {
// Store the data in the database for the current tile
dbHelper.insertPlaces(zoom, ftileX, ftileY, queryLang, result);
}
// Remove the tile from the loading set
// Store the data in the database for the current tile
dbHelper.insertPlaces(zoom, ftileX, ftileY, queryLang, result);
// Remove the tile from the loading set
String tileKey = zoom + "_" + ftileX + "_" + ftileY;
synchronized (loadingTiles) {
loadingTiles.remove(tileKey);
Expand All @@ -217,7 +224,7 @@ public void onFinish(@NonNull List<? extends OsmandApiFeatureData> result) {
}).execute();
}

public void showPointInContextMenu(MapActivity mapActivity, ExploreTopPlacePoint point) {
public void showPointInContextMenu(@NonNull MapActivity mapActivity, @NonNull ExploreTopPlacePoint point) {
double latitude = point.getLatitude();
double longitude = point.getLongitude();
app.getSettings().setMapLocationToShow(
Expand Down Expand Up @@ -267,4 +274,15 @@ public int getDataVersion() {
// data version is increased once new data is downloaded
return finishedTasks;
}

public boolean isLoadingRect(@NonNull QuadRect rect) {
synchronized (loadingTiles) {
for (QuadRect loadingRect : loadingTiles.values()) {
if (loadingRect.contains(rect)) {
return true;
}
}
return false;
}
}
}
Loading

0 comments on commit 461482e

Please sign in to comment.