2020-12-08 11:12:44 +01:00
|
|
|
package com.cappielloantonio.play.util;
|
|
|
|
|
|
2021-09-07 18:06:00 +02:00
|
|
|
import android.content.Context;
|
2021-09-12 12:00:27 +02:00
|
|
|
import android.net.ConnectivityManager;
|
2021-08-01 19:08:40 +02:00
|
|
|
import android.text.Html;
|
2021-09-07 18:06:00 +02:00
|
|
|
import android.util.Log;
|
2021-08-01 19:08:40 +02:00
|
|
|
|
2020-12-08 11:12:44 +01:00
|
|
|
import com.cappielloantonio.play.App;
|
2021-04-14 14:06:35 +02:00
|
|
|
import com.cappielloantonio.play.R;
|
|
|
|
|
import com.cappielloantonio.play.glide.CustomGlideRequest;
|
2020-12-08 11:12:44 +01:00
|
|
|
import com.cappielloantonio.play.model.Song;
|
2021-04-26 19:17:42 +02:00
|
|
|
import com.google.android.exoplayer2.MediaItem;
|
2020-12-08 11:12:44 +01:00
|
|
|
|
2021-08-07 15:09:20 +02:00
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.List;
|
2021-04-12 12:43:34 +02:00
|
|
|
import java.util.Locale;
|
2021-07-27 14:53:03 +02:00
|
|
|
import java.util.Map;
|
2021-08-11 13:05:10 +02:00
|
|
|
import java.util.regex.Pattern;
|
2021-04-12 12:43:34 +02:00
|
|
|
|
2020-12-08 11:12:44 +01:00
|
|
|
public class MusicUtil {
|
2021-07-27 14:53:03 +02:00
|
|
|
private static final String TAG = "MusicUtil";
|
2020-12-08 11:12:44 +01:00
|
|
|
|
2021-09-07 18:06:00 +02:00
|
|
|
public static String getSongStreamUri(Context context, Song song) {
|
2021-09-12 12:14:36 +02:00
|
|
|
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
|
|
|
|
|
2021-09-07 18:06:00 +02:00
|
|
|
Map<String, String> params = App.getSubsonicClientInstance(App.getInstance(), false).getParams();
|
2020-12-08 11:12:44 +01:00
|
|
|
|
2021-09-12 12:14:36 +02:00
|
|
|
String uri = App.getSubsonicClientInstance(App.getInstance(), false).getUrl() +
|
2021-09-07 18:06:00 +02:00
|
|
|
"stream" +
|
|
|
|
|
"?u=" + params.get("u") +
|
|
|
|
|
"&s=" + params.get("s") +
|
|
|
|
|
"&t=" + params.get("t") +
|
|
|
|
|
"&v=" + params.get("v") +
|
|
|
|
|
"&c=" + params.get("c") +
|
|
|
|
|
"&id=" + song.getId() +
|
2021-09-12 12:14:36 +02:00
|
|
|
"&maxBitRate=" + getBitratePreference(context, connectivityManager.getActiveNetworkInfo().getType()) +
|
|
|
|
|
"&format=" + getTranscodingFormatPreference(context, connectivityManager.getActiveNetworkInfo().getType());
|
|
|
|
|
|
|
|
|
|
Log.d(TAG, "getSongStreamUri(): " + uri);
|
|
|
|
|
|
|
|
|
|
return uri;
|
2021-09-07 18:06:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static MediaItem getSongDownloadItem(Song song) {
|
2021-07-27 14:53:03 +02:00
|
|
|
Map<String, String> params = App.getSubsonicClientInstance(App.getInstance(), false).getParams();
|
2020-12-08 11:12:44 +01:00
|
|
|
|
2021-09-07 18:06:00 +02:00
|
|
|
String uri = App.getSubsonicClientInstance(App.getInstance(), false).getUrl() +
|
|
|
|
|
"stream" +
|
2021-07-27 14:53:03 +02:00
|
|
|
"?u=" + params.get("u") +
|
|
|
|
|
"&s=" + params.get("s") +
|
|
|
|
|
"&t=" + params.get("t") +
|
|
|
|
|
"&v=" + params.get("v") +
|
|
|
|
|
"&c=" + params.get("c") +
|
|
|
|
|
"&id=" + song.getId();
|
2021-09-07 18:06:00 +02:00
|
|
|
|
|
|
|
|
return MediaItem.fromUri(uri);
|
2020-12-08 11:12:44 +01:00
|
|
|
}
|
2021-04-12 12:43:34 +02:00
|
|
|
|
2021-07-28 18:53:42 +02:00
|
|
|
public static String getReadableDurationString(long duration, boolean millis) {
|
2021-09-04 17:01:39 +02:00
|
|
|
long minutes;
|
|
|
|
|
long seconds;
|
2021-07-28 18:53:42 +02:00
|
|
|
|
2021-07-30 17:34:15 +02:00
|
|
|
if (millis) {
|
2021-07-28 18:53:42 +02:00
|
|
|
minutes = (duration / 1000) / 60;
|
|
|
|
|
seconds = (duration / 1000) % 60;
|
2021-07-30 17:34:15 +02:00
|
|
|
} else {
|
2021-07-28 18:53:42 +02:00
|
|
|
minutes = duration / 60;
|
|
|
|
|
seconds = duration % 60;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-12 12:43:34 +02:00
|
|
|
if (minutes < 60) {
|
|
|
|
|
return String.format(Locale.getDefault(), "%01d:%02d", minutes, seconds);
|
|
|
|
|
} else {
|
|
|
|
|
long hours = minutes / 60;
|
|
|
|
|
minutes = minutes % 60;
|
|
|
|
|
return String.format(Locale.getDefault(), "%d:%02d:%02d", hours, minutes, seconds);
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-04-14 14:06:35 +02:00
|
|
|
|
2021-08-07 15:09:20 +02:00
|
|
|
public static String getReadableString(String string) {
|
|
|
|
|
if (string != null) {
|
|
|
|
|
return Html.fromHtml(string, Html.FROM_HTML_MODE_COMPACT).toString();
|
2021-08-01 19:08:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-11 16:08:53 +02:00
|
|
|
public static String forceReadableString(String string) {
|
|
|
|
|
if (string != null) {
|
|
|
|
|
return getReadableString(string)
|
|
|
|
|
.replaceAll(""", "\"")
|
|
|
|
|
.replaceAll("'", "'")
|
2021-08-13 15:33:34 +02:00
|
|
|
.replaceAll("&", "'")
|
2021-08-11 16:08:53 +02:00
|
|
|
.replaceAll("<a[\\s]+([^>]+)>((?:.(?!</a>))*.)</a>", "");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-30 16:47:09 +02:00
|
|
|
public static String normalizedArtistName(String string) {
|
|
|
|
|
if (string != null) {
|
2021-09-04 16:14:10 +02:00
|
|
|
if (string.toLowerCase().contains(" feat.")) return Pattern.compile(" feat.", Pattern.CASE_INSENSITIVE).split(string)[0].trim();
|
|
|
|
|
else if (string.toLowerCase().contains(" featuring")) return Pattern.compile(" featuring", Pattern.CASE_INSENSITIVE).split(string)[0].trim();
|
2021-08-30 16:47:09 +02:00
|
|
|
else return string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-07 15:09:20 +02:00
|
|
|
public static List<String> getReadableStrings(List<String> strings) {
|
|
|
|
|
List<String> readableStrings = new ArrayList<>();
|
|
|
|
|
|
|
|
|
|
if (strings.size() > 0) {
|
|
|
|
|
for (String string : strings) {
|
|
|
|
|
if (string != null) {
|
|
|
|
|
readableStrings.add(Html.fromHtml(string, Html.FROM_HTML_MODE_COMPACT).toString());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return readableStrings;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-14 14:06:35 +02:00
|
|
|
public static int getDefaultPicPerCategory(String category) {
|
2021-08-14 17:52:46 +02:00
|
|
|
switch (category) {
|
|
|
|
|
case CustomGlideRequest.SONG_PIC:
|
|
|
|
|
return R.drawable.default_album_art;
|
|
|
|
|
case CustomGlideRequest.ALBUM_PIC:
|
|
|
|
|
return R.drawable.default_album_art;
|
|
|
|
|
case CustomGlideRequest.ARTIST_PIC:
|
|
|
|
|
return R.drawable.default_album_art;
|
|
|
|
|
case CustomGlideRequest.PLAYLIST_PIC:
|
|
|
|
|
return R.drawable.default_album_art;
|
|
|
|
|
default:
|
|
|
|
|
return R.drawable.default_album_art;
|
2021-04-14 14:06:35 +02:00
|
|
|
}
|
|
|
|
|
}
|
2021-09-12 12:00:27 +02:00
|
|
|
|
2021-09-12 12:14:36 +02:00
|
|
|
private static String getBitratePreference(Context context, int connectionType) {
|
2021-09-13 17:32:39 +02:00
|
|
|
String audioTranscodeFormat = getTranscodingFormatPreference(context, connectionType);
|
|
|
|
|
|
|
|
|
|
if(audioTranscodeFormat.equals("0")) return "0";
|
|
|
|
|
|
2021-09-12 12:14:36 +02:00
|
|
|
switch (connectionType) {
|
2021-09-12 12:00:27 +02:00
|
|
|
case ConnectivityManager.TYPE_WIFI:
|
|
|
|
|
return PreferenceUtil.getInstance(context).getMaxBitrateWifi();
|
|
|
|
|
case ConnectivityManager.TYPE_MOBILE:
|
|
|
|
|
return PreferenceUtil.getInstance(context).getMaxBitrateMobile();
|
|
|
|
|
default:
|
|
|
|
|
return PreferenceUtil.getInstance(context).getMaxBitrateWifi();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-12 12:14:36 +02:00
|
|
|
private static String getTranscodingFormatPreference(Context context, int connectionType) {
|
|
|
|
|
switch (connectionType) {
|
|
|
|
|
case ConnectivityManager.TYPE_WIFI:
|
|
|
|
|
return PreferenceUtil.getInstance(context).getAudioTranscodeFormatWifi();
|
|
|
|
|
case ConnectivityManager.TYPE_MOBILE:
|
|
|
|
|
return PreferenceUtil.getInstance(context).getAudioTranscodeFormatMobile();
|
|
|
|
|
default:
|
|
|
|
|
return PreferenceUtil.getInstance(context).getAudioTranscodeFormatWifi();
|
|
|
|
|
}
|
2021-09-12 12:00:27 +02:00
|
|
|
}
|
2020-12-08 11:12:44 +01:00
|
|
|
}
|