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;
|
2022-12-29 13:46:11 +01:00
|
|
|
import android.net.Network;
|
|
|
|
|
import android.net.NetworkCapabilities;
|
2021-12-29 10:20:03 +01:00
|
|
|
import android.net.Uri;
|
2021-08-01 19:08:40 +02:00
|
|
|
import android.text.Html;
|
|
|
|
|
|
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
|
|
|
|
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;
|
2022-01-11 16:25:11 +01:00
|
|
|
import java.util.stream.Collectors;
|
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
|
|
|
|
2022-02-05 18:41:35 +01:00
|
|
|
public static Uri getStreamUri(Context context, String id) {
|
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
|
|
|
|
2022-02-05 23:30:12 +01:00
|
|
|
StringBuilder uri = new StringBuilder();
|
|
|
|
|
|
|
|
|
|
uri.append(App.getSubsonicClientInstance(App.getInstance(), false).getUrl());
|
|
|
|
|
uri.append("stream");
|
|
|
|
|
|
2022-12-29 13:46:11 +01:00
|
|
|
if (params.containsKey("u") && params.get("u") != null)
|
|
|
|
|
uri.append("?u=").append(params.get("u"));
|
|
|
|
|
if (params.containsKey("p") && params.get("p") != null)
|
|
|
|
|
uri.append("&p=").append(params.get("p"));
|
|
|
|
|
if (params.containsKey("s") && params.get("s") != null)
|
|
|
|
|
uri.append("&s=").append(params.get("s"));
|
|
|
|
|
if (params.containsKey("t") && params.get("t") != null)
|
|
|
|
|
uri.append("&t=").append(params.get("t"));
|
|
|
|
|
if (params.containsKey("v") && params.get("v") != null)
|
|
|
|
|
uri.append("&v=").append(params.get("v"));
|
|
|
|
|
if (params.containsKey("c") && params.get("c") != null)
|
|
|
|
|
uri.append("&c=").append(params.get("c"));
|
2022-02-05 23:30:12 +01:00
|
|
|
uri.append("&id=").append(id);
|
2022-01-02 11:54:34 +01:00
|
|
|
|
2022-03-25 22:30:42 +01:00
|
|
|
if (getConnectivityManager(context).getActiveNetworkInfo() != null) {
|
2022-02-05 23:30:12 +01:00
|
|
|
uri.append("&maxBitRate=")
|
2022-03-25 22:30:42 +01:00
|
|
|
.append(getBitratePreference(context))
|
2022-03-29 21:07:43 +02:00
|
|
|
.append("&format=")
|
|
|
|
|
.append(getTranscodingFormatPreference(context));
|
2022-01-02 11:54:34 +01:00
|
|
|
}
|
2021-09-12 12:14:36 +02:00
|
|
|
|
2023-02-18 11:23:53 +01:00
|
|
|
// Log.d(TAG, "getStreamUri(): " + uri);
|
2021-09-12 12:14:36 +02:00
|
|
|
|
2022-02-05 23:30:12 +01:00
|
|
|
return Uri.parse(uri.toString());
|
2021-09-07 18:06:00 +02:00
|
|
|
}
|
|
|
|
|
|
2022-02-05 18:41:35 +01:00
|
|
|
public static Uri getDownloadUri(String id) {
|
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
|
|
|
|
2022-02-05 23:30:12 +01:00
|
|
|
StringBuilder uri = new StringBuilder();
|
|
|
|
|
|
|
|
|
|
uri.append(App.getSubsonicClientInstance(App.getInstance(), false).getUrl());
|
|
|
|
|
uri.append("stream");
|
|
|
|
|
|
2022-12-29 13:46:11 +01:00
|
|
|
if (params.containsKey("u") && params.get("u") != null)
|
|
|
|
|
uri.append("?u=").append(params.get("u"));
|
|
|
|
|
if (params.containsKey("p") && params.get("p") != null)
|
|
|
|
|
uri.append("&p=").append(params.get("p"));
|
|
|
|
|
if (params.containsKey("s") && params.get("s") != null)
|
|
|
|
|
uri.append("&s=").append(params.get("s"));
|
|
|
|
|
if (params.containsKey("t") && params.get("t") != null)
|
|
|
|
|
uri.append("&t=").append(params.get("t"));
|
|
|
|
|
if (params.containsKey("v") && params.get("v") != null)
|
|
|
|
|
uri.append("&v=").append(params.get("v"));
|
|
|
|
|
if (params.containsKey("c") && params.get("c") != null)
|
|
|
|
|
uri.append("&c=").append(params.get("c"));
|
2022-02-05 23:30:12 +01:00
|
|
|
uri.append("&id=").append(id);
|
2021-09-07 18:06:00 +02:00
|
|
|
|
2023-02-18 11:23:53 +01:00
|
|
|
// Log.d(TAG, "getDownloadUri(): " + uri);
|
2022-01-01 21:55:15 +01:00
|
|
|
|
2022-02-05 23:30:12 +01:00
|
|
|
return Uri.parse(uri.toString());
|
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
|
|
|
|
2022-02-05 18:41:35 +01:00
|
|
|
public static String getReadablePodcastDurationString(long duration) {
|
|
|
|
|
long minutes = duration / 60;
|
|
|
|
|
|
|
|
|
|
if (minutes < 60) {
|
|
|
|
|
return String.format(Locale.getDefault(), "%01d min", minutes);
|
|
|
|
|
} else {
|
|
|
|
|
long hours = minutes / 60;
|
|
|
|
|
minutes = minutes % 60;
|
|
|
|
|
return String.format(Locale.getDefault(), "%d h %02d min", hours, minutes);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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 "";
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-28 14:29:09 +01:00
|
|
|
public static String getReadableLyrics(String string) {
|
|
|
|
|
if (string != null) {
|
|
|
|
|
return string
|
|
|
|
|
.replaceAll(""", "\"")
|
|
|
|
|
.replaceAll("'", "'")
|
|
|
|
|
.replaceAll("&", "'")
|
|
|
|
|
.replaceAll("
", "\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-30 16:47:09 +02:00
|
|
|
public static String normalizedArtistName(String string) {
|
|
|
|
|
if (string != null) {
|
2022-12-29 13:46:11 +01: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;
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-11 16:25:11 +01:00
|
|
|
public static String passwordHexEncoding(String plainPassword) {
|
|
|
|
|
return "enc:" + plainPassword.chars().mapToObj(Integer::toHexString).collect(Collectors.joining());
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
2022-03-25 22:30:42 +01:00
|
|
|
public static String getBitratePreference(Context context) {
|
2022-12-29 13:46:11 +01:00
|
|
|
Network network = getConnectivityManager(context).getActiveNetwork();
|
|
|
|
|
NetworkCapabilities networkCapabilities = getConnectivityManager(context).getNetworkCapabilities(network);
|
2022-03-25 22:30:42 +01:00
|
|
|
String audioTranscodeFormat = getTranscodingFormatPreference(context);
|
2021-09-13 17:32:39 +02:00
|
|
|
|
2022-12-29 13:46:11 +01:00
|
|
|
if (audioTranscodeFormat.equals("raw") || network == null || networkCapabilities == null)
|
|
|
|
|
return "0";
|
2021-09-13 17:32:39 +02:00
|
|
|
|
2022-12-29 13:46:11 +01:00
|
|
|
if (networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) {
|
|
|
|
|
return PreferenceUtil.getInstance(context).getMaxBitrateWifi();
|
|
|
|
|
} else if (networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) {
|
|
|
|
|
return PreferenceUtil.getInstance(context).getMaxBitrateMobile();
|
|
|
|
|
} else {
|
|
|
|
|
return PreferenceUtil.getInstance(context).getMaxBitrateWifi();
|
2021-09-12 12:00:27 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-25 22:30:42 +01:00
|
|
|
public static String getTranscodingFormatPreference(Context context) {
|
2022-12-29 13:46:11 +01:00
|
|
|
Network network = getConnectivityManager(context).getActiveNetwork();
|
|
|
|
|
NetworkCapabilities networkCapabilities = getConnectivityManager(context).getNetworkCapabilities(network);
|
|
|
|
|
|
|
|
|
|
if (network == null || networkCapabilities == null) return "raw";
|
|
|
|
|
|
|
|
|
|
if (networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) {
|
|
|
|
|
return PreferenceUtil.getInstance(context).getAudioTranscodeFormatWifi();
|
|
|
|
|
} else if (networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) {
|
|
|
|
|
return PreferenceUtil.getInstance(context).getAudioTranscodeFormatMobile();
|
|
|
|
|
} else {
|
|
|
|
|
return PreferenceUtil.getInstance(context).getAudioTranscodeFormatWifi();
|
2021-09-12 12:14:36 +02:00
|
|
|
}
|
2021-09-12 12:00:27 +02:00
|
|
|
}
|
2022-03-25 22:30:42 +01:00
|
|
|
|
|
|
|
|
private static ConnectivityManager getConnectivityManager(Context context) {
|
|
|
|
|
return (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
|
|
|
|
}
|
2020-12-08 11:12:44 +01:00
|
|
|
}
|