diff --git a/app/src/main/java/com/michatec/radio/collection/CollectionAdapter.kt b/app/src/main/java/com/michatec/radio/collection/CollectionAdapter.kt index c3e8380..8fa5a45 100644 --- a/app/src/main/java/com/michatec/radio/collection/CollectionAdapter.kt +++ b/app/src/main/java/com/michatec/radio/collection/CollectionAdapter.kt @@ -1,5 +1,7 @@ package com.michatec.radio.collection +import android.R.id.content +import android.app.Activity import android.content.Context import android.content.SharedPreferences import android.text.Editable @@ -10,6 +12,7 @@ import android.view.View import android.view.ViewGroup import android.view.inputmethod.EditorInfo import android.widget.TextView +import com.google.android.material.snackbar.Snackbar import androidx.appcompat.app.AppCompatActivity import androidx.core.net.toUri import androidx.core.view.isGone @@ -31,6 +34,7 @@ import com.michatec.radio.helpers.ImageHelper import com.michatec.radio.helpers.NetworkHelper import com.michatec.radio.helpers.PreferencesHelper import com.michatec.radio.helpers.ShortcutHelper +import com.michatec.radio.extensions.java.StationHealthChecker import com.michatec.radio.helpers.UiHelper import com.michatec.radio.helpers.UpdateHelper import kotlinx.coroutines.CoroutineScope @@ -468,6 +472,23 @@ class CollectionAdapter( if (expandedStationPosition == stationViewHolder.bindingAdapterPosition) return@setOnClickListener collectionAdapterListener.onPlayButtonTapped(station.uuid) } + binding.saveButton.setOnLongClickListener { + if (reorderStationUuid.isNotEmpty()) return@setOnLongClickListener true + val rootView = (context as? Activity)?.findViewById(content) ?: binding.root + Snackbar.make(rootView, context.getString(R.string.toastmessage_checking_stream_health, station.name), Snackbar.LENGTH_LONG).show() + StationHealthChecker.checkStationHealth(station) { checkedStation, result -> + (context as? Activity)?.runOnUiThread { + val statusText = if (result.online()) { + context.getString(R.string.health_status_online, result.responseCode(), result.latencyMs()) + } else { + context.getString(R.string.health_status_offline, result.errorMessage() ?: "unknown error") + } + val msg = context.getString(R.string.toastmessage_health_result, checkedStation.name, statusText) + Snackbar.make(rootView, msg, Snackbar.LENGTH_LONG).show() + } + } + true + } binding.playbackButton.setOnClickListener { collectionAdapterListener.onPlayButtonTapped(station.uuid) } diff --git a/app/src/main/java/com/michatec/radio/extensions/java/StationHealthChecker.java b/app/src/main/java/com/michatec/radio/extensions/java/StationHealthChecker.java new file mode 100644 index 0000000..7e9c2e7 --- /dev/null +++ b/app/src/main/java/com/michatec/radio/extensions/java/StationHealthChecker.java @@ -0,0 +1,86 @@ +package com.michatec.radio.extensions.java; + +import android.util.Log; +import com.michatec.radio.Keys; +import com.michatec.radio.core.Station; +import java.net.HttpURLConnection; +import java.net.URL; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; + +/** + * StationHealthChecker.class + */ +public class StationHealthChecker { + + private static final String TAG = StationHealthChecker.class.getSimpleName(); + private static final ExecutorService executorService = Executors.newCachedThreadPool(); + + public record HealthResult(boolean online, int responseCode, long latencyMs, String contentType, + String errorMessage) { + } + + public interface OnStationHealthCheckedListener { + void onHealthChecked(Station station, HealthResult result); + } + + /** + * Checks the health of a station asynchronously using an ExecutorService. + */ + public static void checkStationHealth(final Station station, final OnStationHealthCheckedListener listener) { + if (station == null || station.getStreamUri().isEmpty()) { + if (listener != null) { + listener.onHealthChecked(station, new HealthResult(false, -1, 0, "", "Invalid stream URI")); + } + return; + } + + executorService.execute(() -> { + long startTime = System.currentTimeMillis(); + boolean online; + int responseCode; + String contentType = Keys.MIME_TYPE_UNSUPPORTED; + String errorMessage = null; + + HttpURLConnection connection = null; + try { + URL url = new URL(station.getStreamUri()); + connection = (HttpURLConnection) url.openConnection(); + connection.setRequestProperty("User-Agent", Keys.INSTANCE.getUSER_AGENT()); + connection.setConnectTimeout(5000); + connection.setReadTimeout(5000); + connection.setRequestMethod("GET"); + + responseCode = connection.getResponseCode(); + long latency = System.currentTimeMillis() - startTime; + + if (responseCode >= 200 && responseCode < 400) { + online = true; + String rawContentType = connection.getContentType(); + if (rawContentType != null && !rawContentType.isEmpty()) { + contentType = rawContentType.split(";")[0].trim(); + } + } else { + online = false; + errorMessage = "HTTP Error: " + responseCode; + } + + final HealthResult result = new HealthResult(online, responseCode, latency, contentType, errorMessage); + if (listener != null) { + listener.onHealthChecked(station, result); + } + } catch (Exception e) { + long latency = System.currentTimeMillis() - startTime; + Log.e(TAG, "Error checking station health: " + e.getMessage(), e); + final HealthResult result = new HealthResult(false, -1, latency, Keys.MIME_TYPE_UNSUPPORTED, e.getMessage()); + if (listener != null) { + listener.onHealthChecked(station, result); + } + } finally { + if (connection != null) { + connection.disconnect(); + } + } + }); + } +} diff --git a/app/src/main/res/values-da/strings.xml b/app/src/main/res/values-da/strings.xml index 9ec319b..5f8e108 100644 --- a/app/src/main/res/values-da/strings.xml +++ b/app/src/main/res/values-da/strings.xml @@ -251,4 +251,10 @@ Deaktiver baggrundsoptimeringer Baggrundsoptimeringer aktiveret! Baggrundsoptimeringer deaktiveret. + + + Tjekker stream-tilstand for %1$s… + Online (%1$d) - %2$dms + Offline (%1$s) + Tilstand [%1$s]: %2$s diff --git a/app/src/main/res/values-de/strings.xml b/app/src/main/res/values-de/strings.xml index 0fc61b2..221b71e 100644 --- a/app/src/main/res/values-de/strings.xml +++ b/app/src/main/res/values-de/strings.xml @@ -253,4 +253,10 @@ Hintergrund-Optimierungen deaktivieren Hintergrund-Optimierungen aktiviert! Hintergrund-Optimierungen deaktiviert. + + + Überprüfe Stream-Status für %1$s… + Online (%1$d) - %2$dms + Offline (%1$s) + Status [%1$s]: %2$s diff --git a/app/src/main/res/values-el/strings.xml b/app/src/main/res/values-el/strings.xml index a2cd492..0f8ad67 100644 --- a/app/src/main/res/values-el/strings.xml +++ b/app/src/main/res/values-el/strings.xml @@ -254,4 +254,10 @@ Απενεργοποίηση βελτιστοποιήσεων παρασκηνίου Οι βελτιστοποιήσεις παρασκηνίου ενεργοποιήθηκαν! Οι βελτιστοποιήσεις παρασκηνίου απενεργοποιήθηκαν. + + + Έλεγχος κατάστασης ροής για %1$s… + Online (%1$d) - %2$dms + Offline (%1$s) + Κατάσταση [%1$s]: %2$s diff --git a/app/src/main/res/values-fr/strings.xml b/app/src/main/res/values-fr/strings.xml index f46c0d0..8b9f22c 100644 --- a/app/src/main/res/values-fr/strings.xml +++ b/app/src/main/res/values-fr/strings.xml @@ -252,4 +252,10 @@ Désactiver les optimisations en arrière-plan Optimisations en arrière-plan activées ! Optimisations en arrière-plan désactivées. + + + Vérification de l\'état du flux pour %1$s… + En ligne (%1$d) - %2$dms + Hors ligne (%1$s) + État [%1$s] : %2$s diff --git a/app/src/main/res/values-ja/strings.xml b/app/src/main/res/values-ja/strings.xml index 1cefeaa..b46eab5 100644 --- a/app/src/main/res/values-ja/strings.xml +++ b/app/src/main/res/values-ja/strings.xml @@ -252,4 +252,10 @@ バックグラウンド最適化を無効化 バックグラウンド最適化が有効になりました! バックグラウンド最適化が無効になりました。 + + + %1$s のストリーム状態を確認中… + オンライン (%1$d) - %2$dms + オフライン (%1$s) + 状態 [%1$s]: %2$s diff --git a/app/src/main/res/values-nl/strings.xml b/app/src/main/res/values-nl/strings.xml index c9745e8..3e42174 100644 --- a/app/src/main/res/values-nl/strings.xml +++ b/app/src/main/res/values-nl/strings.xml @@ -253,4 +253,10 @@ Achtergrondoptimalisaties uitschakelen Achtergrondoptimalisaties ingeschakeld! Achtergrondoptimalisaties uitgeschakeld. + + + Streamstatus controleren voor %1$s… + Online (%1$d) - %2$dms + Offline (%1$s) + Status [%1$s]: %2$s diff --git a/app/src/main/res/values-pl/strings.xml b/app/src/main/res/values-pl/strings.xml index 04cb686..79daa11 100644 --- a/app/src/main/res/values-pl/strings.xml +++ b/app/src/main/res/values-pl/strings.xml @@ -252,4 +252,10 @@ Wyłącz optymalizacje w tle Włączono optymalizacje w tle! Wyłączono optymalizacje w tle. + + + Sprawdzanie stanu strumienia dla %1$s… + Online (%1$d) - %2$dms + Offline (%1$s) + Stan [%1$s]: %2$s diff --git a/app/src/main/res/values-ru/strings.xml b/app/src/main/res/values-ru/strings.xml index d7d100e..b646323 100644 --- a/app/src/main/res/values-ru/strings.xml +++ b/app/src/main/res/values-ru/strings.xml @@ -251,4 +251,10 @@ Отключить фоновые оптимизации Фоновые оптимизации включены! Фоновые оптимизации отключены. + + + Проверка состояния потока для %1$s… + Онлайн (%1$d) - %2$dмс + Офлайн (%1$s) + Статус [%1$s]: %2$s diff --git a/app/src/main/res/values-uk/strings.xml b/app/src/main/res/values-uk/strings.xml index 3b428a0..9d5991a 100644 --- a/app/src/main/res/values-uk/strings.xml +++ b/app/src/main/res/values-uk/strings.xml @@ -251,4 +251,10 @@ Вимкнути фонові оптимізації Фонові оптимізації увімкнено! Фонові оптимізації вимкнено. + + + Перевірка стану потоку для %1$s… + Онлайн (%1$d) - %2$dмс + Офлайн (%1$s) + Статус [%1$s]: %2$s diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index f0b4f76..355e8bd 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -287,4 +287,10 @@ Disable Background Optimizations Background optimizations enabled! Background optimizations disabled. + + + Checking stream health for %1$s… + Online (%1$d) - %2$dms + Offline (%1$s) + Health [%1$s]: %2$s diff --git a/app/src/main/res/xml/provider_paths.xml b/app/src/main/res/xml/provider_paths.xml index 7218213..f531014 100644 --- a/app/src/main/res/xml/provider_paths.xml +++ b/app/src/main/res/xml/provider_paths.xml @@ -1,9 +1,12 @@ - + + \ No newline at end of file