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;
|
2023-03-12 16:34:38 +01: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;
|
2023-03-19 18:24:35 +01:00
|
|
|
import com.cappielloantonio.play.subsonic.models.Child;
|
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;
|
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
|
|
|
|
2023-03-10 15:21:02 +01:00
|
|
|
public static Uri getStreamUri(String id) {
|
|
|
|
|
Map<String, String> params = App.getSubsonicClientInstance(false).getParams();
|
2020-12-08 11:12:44 +01:00
|
|
|
|
2022-02-05 23:30:12 +01:00
|
|
|
StringBuilder uri = new StringBuilder();
|
|
|
|
|
|
2023-03-10 15:21:02 +01:00
|
|
|
uri.append(App.getSubsonicClientInstance(false).getUrl());
|
2022-02-05 23:30:12 +01:00
|
|
|
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"));
|
2021-09-12 12:14:36 +02:00
|
|
|
|
2023-03-10 15:21:02 +01:00
|
|
|
uri.append("&maxBitRate=").append(getBitratePreference());
|
|
|
|
|
uri.append("&format=").append(getTranscodingFormatPreference());
|
2023-03-10 12:17:25 +01:00
|
|
|
uri.append("&id=").append(id);
|
2021-09-12 12:14:36 +02:00
|
|
|
|
2023-03-12 16:34:38 +01:00
|
|
|
Log.d(TAG, "getStreamUri: " + uri);
|
|
|
|
|
|
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) {
|
2023-03-10 15:21:02 +01:00
|
|
|
Map<String, String> params = App.getSubsonicClientInstance(false).getParams();
|
2020-12-08 11:12:44 +01:00
|
|
|
|
2022-02-05 23:30:12 +01:00
|
|
|
StringBuilder uri = new StringBuilder();
|
|
|
|
|
|
2023-03-10 15:21:02 +01:00
|
|
|
uri.append(App.getSubsonicClientInstance(false).getUrl());
|
2023-03-12 16:34:38 +01:00
|
|
|
uri.append("download");
|
2022-02-05 23:30:12 +01:00
|
|
|
|
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"));
|
2021-09-07 18:06:00 +02:00
|
|
|
|
2023-03-10 12:17:25 +01:00
|
|
|
uri.append("&id=").append(id);
|
2022-01-01 21:55:15 +01:00
|
|
|
|
2023-03-12 16:34:38 +01:00
|
|
|
Log.d(TAG, "getDownloadUri: " + uri);
|
|
|
|
|
|
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("&", "'")
|
2023-03-11 18:12:46 +01:00
|
|
|
.replaceAll("<a\\s+([^>]+)>((?:.(?!</a>))*.)</a>", "");
|
2021-08-11 16:08:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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-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());
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-10 15:21:02 +01:00
|
|
|
public static String getBitratePreference() {
|
|
|
|
|
Network network = getConnectivityManager().getActiveNetwork();
|
|
|
|
|
NetworkCapabilities networkCapabilities = getConnectivityManager().getNetworkCapabilities(network);
|
|
|
|
|
String audioTranscodeFormat = getTranscodingFormatPreference();
|
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)) {
|
2023-03-06 21:59:10 +01:00
|
|
|
return Preferences.getMaxBitrateWifi();
|
2022-12-29 13:46:11 +01:00
|
|
|
} else if (networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) {
|
2023-03-06 21:59:10 +01:00
|
|
|
return Preferences.getMaxBitrateMobile();
|
2022-12-29 13:46:11 +01:00
|
|
|
} else {
|
2023-03-06 21:59:10 +01:00
|
|
|
return Preferences.getMaxBitrateWifi();
|
2021-09-12 12:00:27 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-10 15:21:02 +01:00
|
|
|
public static String getTranscodingFormatPreference() {
|
|
|
|
|
Network network = getConnectivityManager().getActiveNetwork();
|
|
|
|
|
NetworkCapabilities networkCapabilities = getConnectivityManager().getNetworkCapabilities(network);
|
2022-12-29 13:46:11 +01:00
|
|
|
|
|
|
|
|
if (network == null || networkCapabilities == null) return "raw";
|
|
|
|
|
|
|
|
|
|
if (networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) {
|
2023-03-06 21:59:10 +01:00
|
|
|
return Preferences.getAudioTranscodeFormatWifi();
|
2022-12-29 13:46:11 +01:00
|
|
|
} else if (networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) {
|
2023-03-06 21:59:10 +01:00
|
|
|
return Preferences.getAudioTranscodeFormatMobile();
|
2022-12-29 13:46:11 +01:00
|
|
|
} else {
|
2023-03-06 21:59:10 +01:00
|
|
|
return Preferences.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
|
|
|
|
2023-03-19 18:24:35 +01:00
|
|
|
public static List<Child> limitPlayableMedia(List<Child> toLimit, int position) {
|
|
|
|
|
if (!toLimit.isEmpty() && toLimit.size() > Constants.PLAYABLE_MEDIA_LIMIT) {
|
|
|
|
|
int from = position < Constants.PRE_PLAYABLE_MEDIA ? 0 : position - Constants.PRE_PLAYABLE_MEDIA;
|
|
|
|
|
int to = Math.min(from + Constants.PLAYABLE_MEDIA_LIMIT, toLimit.size());
|
|
|
|
|
|
|
|
|
|
return toLimit.subList(from, to);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return toLimit;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static int getPlayableMediaPosition(int initialPosition) {
|
|
|
|
|
return initialPosition > Constants.PLAYABLE_MEDIA_LIMIT ? Constants.PRE_PLAYABLE_MEDIA : initialPosition;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-10 15:21:02 +01:00
|
|
|
private static ConnectivityManager getConnectivityManager() {
|
|
|
|
|
return (ConnectivityManager) App.getContext().getSystemService(Context.CONNECTIVITY_SERVICE);
|
2022-03-25 22:30:42 +01:00
|
|
|
}
|
2020-12-08 11:12:44 +01:00
|
|
|
}
|