Commit 88a86a5a authored by Robert Stone's avatar Robert Stone
Browse files

Use SecureRandom instead of Random

parent 64c210e0
......@@ -47,6 +47,7 @@ import java.io.File
import java.io.FileNotFoundException
import java.io.FileOutputStream
import java.nio.ByteBuffer
import java.security.SecureRandom
import java.text.DecimalFormat
import java.text.SimpleDateFormat
import java.util.*
......@@ -253,7 +254,7 @@ class ArtworkProvider : ContentProvider() {
/* Shuffle All */
val audioCount = ctx.getFromMl { audioCount }
/* Show cover art from the whole library */
val offset = Random().nextInt((audioCount - MediaSessionBrowser.MAX_COVER_ART_ITEMS).coerceAtLeast(1))
val offset = SecureRandom().nextInt((audioCount - MediaSessionBrowser.MAX_COVER_ART_ITEMS).coerceAtLeast(1))
val list = ctx.getFromMl { getPagedAudio(Medialibrary.SORT_ALPHA, false, false, MediaSessionBrowser.MAX_COVER_ART_ITEMS, offset) }
return@runBlocking getHomeImage(ctx, SHUFFLE_ALL, list)
}
......
......@@ -25,6 +25,7 @@ import org.videolan.vlc.gui.helpers.MediaComparators
import org.videolan.vlc.media.MediaSessionBrowser
import org.videolan.vlc.util.VoiceSearchParams
import org.videolan.vlc.util.awaitMedialibraryStarted
import java.security.SecureRandom
import java.util.*
import kotlin.math.min
......@@ -136,7 +137,7 @@ internal class MediaSessionCallback(private val playbackService: PlaybackService
val tracks = context.getFromMl { audio }
if (tracks.isNotEmpty() && isActive) {
tracks.sortWith(MediaComparators.ANDROID_AUTO)
loadMedia(tracks.toList(), Random().nextInt(min(tracks.size, MEDIALIBRARY_PAGE_SIZE)))
loadMedia(tracks.toList(), SecureRandom().nextInt(min(tracks.size, MEDIALIBRARY_PAGE_SIZE)))
if (!playbackService.isShuffling) playbackService.shuffle()
} else {
playbackService.displayPlaybackError(R.string.search_no_result)
......
......@@ -46,6 +46,7 @@ import org.videolan.vlc.util.Permissions
import org.videolan.vlc.util.generateResolutionClass
import org.videolan.vlc.util.isSchemeStreaming
import java.io.File
import java.security.SecureRandom
import java.util.*
import kotlin.collections.ArrayList
import kotlin.math.min
......@@ -199,7 +200,7 @@ object MediaUtils {
}
}
}?.takeIf { it.isNotEmpty() }?.let { list ->
service.load(list, if (shuffle) Random().nextInt(count) else position)
service.load(list, if (shuffle) SecureRandom().nextInt(count) else position)
if (shuffle && !service.isShuffling) service.shuffle()
}
}
......@@ -210,7 +211,7 @@ object MediaUtils {
SuspendDialogCallback(context) { service ->
val count = withContext(Dispatchers.IO) { provider.getTotalCount() }
fun play(list: List<MediaWrapper>) {
service.load(list, if (shuffle) Random().nextInt(min(count, MEDIALIBRARY_PAGE_SIZE)) else position)
service.load(list, if (shuffle) SecureRandom().nextInt(min(count, MEDIALIBRARY_PAGE_SIZE)) else position)
if (shuffle && !service.isShuffling) service.shuffle()
}
when (count) {
......@@ -237,7 +238,7 @@ object MediaUtils {
SuspendDialogCallback(context) { service ->
val count = withContext(Dispatchers.IO) { provider.getTotalCount() }
fun play(list: List<MediaWrapper>) {
service.load(list, if (shuffle) Random().nextInt(min(count, MEDIALIBRARY_PAGE_SIZE)) else position)
service.load(list, if (shuffle) SecureRandom().nextInt(min(count, MEDIALIBRARY_PAGE_SIZE)) else position)
if (shuffle && !service.isShuffling) service.shuffle()
}
when (count) {
......@@ -268,7 +269,7 @@ object MediaUtils {
SuspendDialogCallback(context) { service ->
val count = withContext(Dispatchers.IO) { provider.getTotalCount() }
fun play(list: List<MediaWrapper>) {
service.load(list, if (shuffle) Random().nextInt(min(count, MEDIALIBRARY_PAGE_SIZE)) else position)
service.load(list, if (shuffle) SecureRandom().nextInt(min(count, MEDIALIBRARY_PAGE_SIZE)) else position)
if (shuffle && !service.isShuffling) service.shuffle()
}
when (count) {
......
......@@ -29,6 +29,7 @@ import org.videolan.vlc.R
import org.videolan.vlc.gui.video.VideoPlayerActivity
import org.videolan.vlc.util.*
import org.videolan.vlc.util.FileUtils
import java.security.SecureRandom
import java.util.*
import kotlin.math.max
......@@ -64,7 +65,7 @@ class PlaylistManager(val service: PlaybackService) : MediaWrapperList.EventList
var isHardware = false
private var parsed = false
var savedTime = 0L
private var random = Random(System.currentTimeMillis())
private var random = SecureRandom()
private var newMedia = false
@Volatile
private var expanding = false
......@@ -605,10 +606,8 @@ class PlaylistManager(val service: PlaybackService) : MediaWrapperList.EventList
return
} else {
previous.clear()
random = Random(System.currentTimeMillis())
}
}
random = Random(System.currentTimeMillis())
// Find a new index not in previous.
do {
nextIndex = random.nextInt(size)
......
......@@ -48,6 +48,7 @@ import org.videolan.vlc.R
import java.io.File
import java.net.URI
import java.net.URISyntaxException
import java.security.SecureRandom
import java.util.*
fun String.validateLocation(): Boolean {
......@@ -117,7 +118,7 @@ fun MediaWrapper?.isBrowserMedia() = this != null && (isMedia() || type == Media
fun Context.getAppSystemService(name: String) = applicationContext.getSystemService(name)!!
fun Long.random() = (Random().nextFloat() * this).toLong()
fun Long.random() = (SecureRandom().nextFloat() * this).toLong()
suspend fun Context.awaitMedialibraryStarted() = getFromMl { isStarted }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment