Fix issues with Random() in emulator

This commit is contained in:
Philipp Heckel 2022-06-19 14:45:01 -04:00
parent 0c123d58f6
commit 2324352a50
4 changed files with 14 additions and 7 deletions

View file

@ -105,7 +105,7 @@ class DetailActivity : AppCompatActivity(), ActionMode.Callback, NotificationFra
if (subscription == null) { if (subscription == null) {
val instant = baseUrl != appBaseUrl val instant = baseUrl != appBaseUrl
subscription = Subscription( subscription = Subscription(
id = Random.nextLong(), id = randomSubscriptionId(),
baseUrl = baseUrl, baseUrl = baseUrl,
topic = topic, topic = topic,
instant = instant, instant = instant,

View file

@ -43,6 +43,7 @@ import io.heckel.ntfy.work.PollWorker
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay import kotlinx.coroutines.delay
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import java.security.SecureRandom
import java.util.* import java.util.*
import java.util.concurrent.TimeUnit import java.util.concurrent.TimeUnit
import kotlin.random.Random import kotlin.random.Random
@ -426,7 +427,7 @@ class MainActivity : AppCompatActivity(), ActionMode.Callback, AddFragment.Subsc
// Add subscription to database // Add subscription to database
val subscription = Subscription( val subscription = Subscription(
id = Random.nextLong(), id = randomSubscriptionId(),
baseUrl = baseUrl, baseUrl = baseUrl,
topic = topic, topic = topic,
instant = instant, instant = instant,
@ -495,6 +496,7 @@ class MainActivity : AppCompatActivity(), ActionMode.Callback, AddFragment.Subsc
var errorMessage = "" // First error var errorMessage = "" // First error
var newNotificationsCount = 0 var newNotificationsCount = 0
repository.getSubscriptions().forEach { subscription -> repository.getSubscriptions().forEach { subscription ->
Log.d(TAG, "subscription: ${subscription}")
try { try {
val user = repository.getUser(subscription.baseUrl) // May be null val user = repository.getUser(subscription.baseUrl) // May be null
val notifications = api.poll(subscription.id, subscription.baseUrl, subscription.topic, user, subscription.lastNotificationId) val notifications = api.poll(subscription.id, subscription.baseUrl, subscription.topic, user, subscription.lastNotificationId)

View file

@ -7,10 +7,7 @@ import io.heckel.ntfy.app.Application
import io.heckel.ntfy.db.Repository import io.heckel.ntfy.db.Repository
import io.heckel.ntfy.db.Subscription import io.heckel.ntfy.db.Subscription
import io.heckel.ntfy.service.SubscriberServiceManager import io.heckel.ntfy.service.SubscriberServiceManager
import io.heckel.ntfy.util.Log import io.heckel.ntfy.util.*
import io.heckel.ntfy.util.randomString
import io.heckel.ntfy.util.shortUrl
import io.heckel.ntfy.util.topicUrlUp
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay import kotlinx.coroutines.delay
@ -71,7 +68,7 @@ class BroadcastReceiver : android.content.BroadcastReceiver() {
val topic = UP_PREFIX + randomString(TOPIC_RANDOM_ID_LENGTH) val topic = UP_PREFIX + randomString(TOPIC_RANDOM_ID_LENGTH)
val endpoint = topicUrlUp(baseUrl, topic) val endpoint = topicUrlUp(baseUrl, topic)
val subscription = Subscription( val subscription = Subscription(
id = Random.nextLong(), id = randomSubscriptionId(),
baseUrl = baseUrl, baseUrl = baseUrl,
topic = topic, topic = topic,
instant = true, // No Firebase, always instant! instant = true, // No Firebase, always instant!

View file

@ -43,6 +43,7 @@ import java.text.DateFormat
import java.text.StringCharacterIterator import java.text.StringCharacterIterator
import java.util.* import java.util.*
import kotlin.math.abs import kotlin.math.abs
import kotlin.math.absoluteValue
fun topicUrl(baseUrl: String, topic: String) = "${baseUrl}/${topic}" fun topicUrl(baseUrl: String, topic: String) = "${baseUrl}/${topic}"
fun topicUrlUp(baseUrl: String, topic: String) = "${baseUrl}/${topic}?up=1" // UnifiedPush 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("") 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 // Allows letting multiple variables at once, see https://stackoverflow.com/a/35522422/1440785
inline fun <T1: Any, T2: Any, R: Any> safeLet(p1: T1?, p2: T2?, block: (T1, T2)->R?): R? { inline fun <T1: Any, T2: Any, R: Any> safeLet(p1: T1?, p2: T2?, block: (T1, T2)->R?): R? {
return if (p1 != null && p2 != null) block(p1, p2) else null return if (p1 != null && p2 != null) block(p1, p2) else null