From 2324352a501683c8186d713b0742fcbf6fc69872 Mon Sep 17 00:00:00 2001 From: Philipp Heckel Date: Sun, 19 Jun 2022 14:45:01 -0400 Subject: [PATCH] Fix issues with Random() in emulator --- app/src/main/java/io/heckel/ntfy/ui/DetailActivity.kt | 2 +- app/src/main/java/io/heckel/ntfy/ui/MainActivity.kt | 4 +++- app/src/main/java/io/heckel/ntfy/up/BroadcastReceiver.kt | 7 ++----- app/src/main/java/io/heckel/ntfy/util/Util.kt | 8 ++++++++ 4 files changed, 14 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/io/heckel/ntfy/ui/DetailActivity.kt b/app/src/main/java/io/heckel/ntfy/ui/DetailActivity.kt index 4dc290c..cfa4553 100644 --- a/app/src/main/java/io/heckel/ntfy/ui/DetailActivity.kt +++ b/app/src/main/java/io/heckel/ntfy/ui/DetailActivity.kt @@ -105,7 +105,7 @@ class DetailActivity : AppCompatActivity(), ActionMode.Callback, NotificationFra if (subscription == null) { val instant = baseUrl != appBaseUrl subscription = Subscription( - id = Random.nextLong(), + id = randomSubscriptionId(), baseUrl = baseUrl, topic = topic, instant = instant, diff --git a/app/src/main/java/io/heckel/ntfy/ui/MainActivity.kt b/app/src/main/java/io/heckel/ntfy/ui/MainActivity.kt index 1dedf98..5321a85 100644 --- a/app/src/main/java/io/heckel/ntfy/ui/MainActivity.kt +++ b/app/src/main/java/io/heckel/ntfy/ui/MainActivity.kt @@ -43,6 +43,7 @@ import io.heckel.ntfy.work.PollWorker import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.delay import kotlinx.coroutines.launch +import java.security.SecureRandom import java.util.* import java.util.concurrent.TimeUnit import kotlin.random.Random @@ -426,7 +427,7 @@ class MainActivity : AppCompatActivity(), ActionMode.Callback, AddFragment.Subsc // Add subscription to database val subscription = Subscription( - id = Random.nextLong(), + id = randomSubscriptionId(), baseUrl = baseUrl, topic = topic, instant = instant, @@ -495,6 +496,7 @@ class MainActivity : AppCompatActivity(), ActionMode.Callback, AddFragment.Subsc var errorMessage = "" // First error var newNotificationsCount = 0 repository.getSubscriptions().forEach { subscription -> + Log.d(TAG, "subscription: ${subscription}") try { val user = repository.getUser(subscription.baseUrl) // May be null val notifications = api.poll(subscription.id, subscription.baseUrl, subscription.topic, user, subscription.lastNotificationId) diff --git a/app/src/main/java/io/heckel/ntfy/up/BroadcastReceiver.kt b/app/src/main/java/io/heckel/ntfy/up/BroadcastReceiver.kt index 66f6315..6906399 100644 --- a/app/src/main/java/io/heckel/ntfy/up/BroadcastReceiver.kt +++ b/app/src/main/java/io/heckel/ntfy/up/BroadcastReceiver.kt @@ -7,10 +7,7 @@ import io.heckel.ntfy.app.Application import io.heckel.ntfy.db.Repository import io.heckel.ntfy.db.Subscription import io.heckel.ntfy.service.SubscriberServiceManager -import io.heckel.ntfy.util.Log -import io.heckel.ntfy.util.randomString -import io.heckel.ntfy.util.shortUrl -import io.heckel.ntfy.util.topicUrlUp +import io.heckel.ntfy.util.* import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.delay @@ -71,7 +68,7 @@ class BroadcastReceiver : android.content.BroadcastReceiver() { val topic = UP_PREFIX + randomString(TOPIC_RANDOM_ID_LENGTH) val endpoint = topicUrlUp(baseUrl, topic) val subscription = Subscription( - id = Random.nextLong(), + id = randomSubscriptionId(), baseUrl = baseUrl, topic = topic, instant = true, // No Firebase, always instant! diff --git a/app/src/main/java/io/heckel/ntfy/util/Util.kt b/app/src/main/java/io/heckel/ntfy/util/Util.kt index 6676942..aa23f55 100644 --- a/app/src/main/java/io/heckel/ntfy/util/Util.kt +++ b/app/src/main/java/io/heckel/ntfy/util/Util.kt @@ -43,6 +43,7 @@ import java.text.DateFormat import java.text.StringCharacterIterator import java.util.* import kotlin.math.abs +import kotlin.math.absoluteValue fun topicUrl(baseUrl: String, topic: String) = "${baseUrl}/${topic}" fun topicUrlUp(baseUrl: String, topic: String) = "${baseUrl}/${topic}?up=1" // UnifiedPush @@ -276,6 +277,13 @@ fun randomString(len: Int): String { return (1..len).map { chars[random.nextInt(chars.size)] }.joinToString("") } +// Generates a random, positive subscription ID between 0-10M. This ensures that it doesn't have issues +// when exported to JSON. It uses SecureRandom, because Random causes issues in the emulator (generating the +// same value again and again), sometimes. +fun randomSubscriptionId(): Long { + return SecureRandom().nextLong().absoluteValue % 100_000_000 +} + // Allows letting multiple variables at once, see https://stackoverflow.com/a/35522422/1440785 inline fun safeLet(p1: T1?, p2: T2?, block: (T1, T2)->R?): R? { return if (p1 != null && p2 != null) block(p1, p2) else null