added a function that generates pseudo-random numbers that change every x hours

This commit is contained in:
CappielloAntonio 2021-04-20 12:36:58 +02:00
parent a7fd7688ab
commit 40e9a6f778
4 changed files with 40 additions and 0 deletions

View file

@ -1,5 +1,6 @@
package com.cappielloantonio.play.util;
import android.content.Context;
import android.util.Log;
import com.cappielloantonio.play.App;
@ -10,7 +11,10 @@ import com.cappielloantonio.play.model.Song;
import org.jellyfin.apiclient.interaction.ApiClient;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Random;
public class MusicUtil {
public static String getSongFileUri(Song song) {
@ -86,4 +90,23 @@ public class MusicUtil {
return R.drawable.default_album_art;
}
}
public static List<Integer> getRandomSongNumber(Context context, int numberOfNumbers, int refreshAfterXHours) {
List<Integer> list = new ArrayList<>();
for (int i = 0; i < numberOfNumbers; i++)
{
list.add(getRandomNumber(0, PreferenceUtil.getInstance(context).getSongNumber(), getMidnightTimestamp(System.currentTimeMillis() / 1000, refreshAfterXHours) + i));
}
return list;
}
private static long getMidnightTimestamp(long timestamp, int hours) {
return timestamp - timestamp % (hours * 60 * 60); // 24 * 60 * 60 sec in one day
}
private static int getRandomNumber(int min, int max, long seed) {
return new Random(seed).nextInt((max - min) + 1) + min;
}
}

View file

@ -34,6 +34,8 @@ public class PreferenceUtil {
public static final String MAXIMUM_BITRATE = "maximum_bitrate";
public static final String AUDIO_DUCKING = "audio_ducking";
public static final String SONG_NUMBER = "SONG_NUMBER";
private static PreferenceUtil sInstance;
private final SharedPreferences mPreferences;
@ -175,4 +177,14 @@ public class PreferenceUtil {
public final boolean getAudioDucking() {
return mPreferences.getBoolean(AUDIO_DUCKING, true);
}
public int getSongNumber() {
return mPreferences.getInt(SONG_NUMBER, 0);
}
public void setSongNumber(int number) {
final SharedPreferences.Editor editor = mPreferences.edit();
editor.putInt(SONG_NUMBER, number);
editor.apply();
}
}