diff options
author | ByteHamster <info@bytehamster.com> | 2022-11-02 23:18:28 +0100 |
---|---|---|
committer | ByteHamster <info@bytehamster.com> | 2022-11-03 22:49:33 +0100 |
commit | cd9845ed4c8867c16f30bbcc059573609971b667 (patch) | |
tree | dbc48d5bc1aad2e052711549906944731fba4cee /core/src/main | |
parent | 6c1ec57bc13c859b478affbc9d4e44217cd7d92c (diff) | |
download | AntennaPod-cd9845ed4c8867c16f30bbcc059573609971b667.zip |
Make NetworkUtils less fat
Breaks dependency cycles
Diffstat (limited to 'core/src/main')
5 files changed, 110 insertions, 90 deletions
diff --git a/core/src/main/java/de/danoeh/antennapod/core/ClientConfigurator.java b/core/src/main/java/de/danoeh/antennapod/core/ClientConfigurator.java index ff4469c57..4a8759a6b 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/ClientConfigurator.java +++ b/core/src/main/java/de/danoeh/antennapod/core/ClientConfigurator.java @@ -7,6 +7,7 @@ import de.danoeh.antennapod.core.preferences.UsageStatistics; import de.danoeh.antennapod.core.preferences.UserPreferences; import de.danoeh.antennapod.core.service.download.AntennapodHttpClient; import de.danoeh.antennapod.core.util.NetworkUtils; +import de.danoeh.antennapod.core.util.download.NetworkConnectionChangeHandler; import de.danoeh.antennapod.core.util.gui.NotificationUtils; import de.danoeh.antennapod.net.ssl.SslProviderInstaller; import de.danoeh.antennapod.storage.database.PodDBAdapter; @@ -26,6 +27,7 @@ public class ClientConfigurator { PlaybackPreferences.init(context); SslProviderInstaller.install(context); NetworkUtils.init(context); + NetworkConnectionChangeHandler.init(context); AntennapodHttpClient.setCacheDirectory(new File(context.getCacheDir(), "okhttp")); AntennapodHttpClient.setProxyConfig(UserPreferences.getProxyConfig()); SleepTimerPreferences.init(context); diff --git a/core/src/main/java/de/danoeh/antennapod/core/util/NetworkUtils.java b/core/src/main/java/de/danoeh/antennapod/core/util/NetworkUtils.java index 85be3c787..c5524d7b5 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/util/NetworkUtils.java +++ b/core/src/main/java/de/danoeh/antennapod/core/util/NetworkUtils.java @@ -7,37 +7,18 @@ import android.net.NetworkCapabilities; import android.net.NetworkInfo; import android.net.wifi.WifiManager; import android.os.Build; -import android.text.TextUtils; -import android.util.Log; - -import java.io.File; -import java.io.IOException; import java.util.Arrays; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; -import de.danoeh.antennapod.core.service.download.DownloadService; -import de.danoeh.antennapod.core.storage.DBTasks; -import de.danoeh.antennapod.model.feed.FeedMedia; import de.danoeh.antennapod.core.preferences.UserPreferences; -import de.danoeh.antennapod.core.service.download.AntennapodHttpClient; -import de.danoeh.antennapod.core.storage.DBWriter; -import io.reactivex.Single; -import io.reactivex.SingleOnSubscribe; -import io.reactivex.android.schedulers.AndroidSchedulers; -import io.reactivex.schedulers.Schedulers; -import okhttp3.OkHttpClient; -import okhttp3.Request; -import okhttp3.Response; public class NetworkUtils { private static final String REGEX_PATTERN_IP_ADDRESS = "([0-9]{1,3}[\\.]){3}[0-9]{1,3}"; private NetworkUtils(){} - private static final String TAG = NetworkUtils.class.getSimpleName(); - private static Context context; public static void init(Context context) { @@ -160,73 +141,4 @@ public class NetworkUtils { } return false; } - - public static Single<Long> getFeedMediaSizeObservable(FeedMedia media) { - return Single.create((SingleOnSubscribe<Long>) emitter -> { - if (!NetworkUtils.isEpisodeHeadDownloadAllowed()) { - emitter.onSuccess(0L); - return; - } - long size = Integer.MIN_VALUE; - if (media.isDownloaded()) { - File mediaFile = new File(media.getLocalMediaUrl()); - if (mediaFile.exists()) { - size = mediaFile.length(); - } - } else if (!media.checkedOnSizeButUnknown()) { - // only query the network if we haven't already checked - - String url = media.getDownload_url(); - if(TextUtils.isEmpty(url)) { - emitter.onSuccess(0L); - return; - } - - OkHttpClient client = AntennapodHttpClient.getHttpClient(); - Request.Builder httpReq = new Request.Builder() - .url(url) - .header("Accept-Encoding", "identity") - .head(); - try { - Response response = client.newCall(httpReq.build()).execute(); - if (response.isSuccessful()) { - String contentLength = response.header("Content-Length"); - try { - size = Integer.parseInt(contentLength); - } catch (NumberFormatException e) { - Log.e(TAG, Log.getStackTraceString(e)); - } - } - } catch (IOException e) { - emitter.onSuccess(0L); - Log.e(TAG, Log.getStackTraceString(e)); - return; // better luck next time - } - } - Log.d(TAG, "new size: " + size); - if (size <= 0) { - // they didn't tell us the size, but we don't want to keep querying on it - media.setCheckedOnSizeButUnknown(); - } else { - media.setSize(size); - } - emitter.onSuccess(size); - DBWriter.setFeedMedia(media); - }) - .subscribeOn(Schedulers.io()) - .observeOn(AndroidSchedulers.mainThread()); - } - - public static void networkChangedDetected() { - if (NetworkUtils.isAutoDownloadAllowed()) { - Log.d(TAG, "auto-dl network available, starting auto-download"); - DBTasks.autodownloadUndownloadedItems(context); - } else { // if new network is Wi-Fi, finish ongoing downloads, - // otherwise cancel all downloads - if (NetworkUtils.isNetworkRestricted()) { - Log.i(TAG, "Device is no longer connected to Wi-Fi. Cancelling ongoing downloads"); - DownloadService.cancelAll(context); - } - } - } } diff --git a/core/src/main/java/de/danoeh/antennapod/core/util/download/ConnectionStateMonitor.java b/core/src/main/java/de/danoeh/antennapod/core/util/download/ConnectionStateMonitor.java index 5c543bf5a..e88752e4a 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/util/download/ConnectionStateMonitor.java +++ b/core/src/main/java/de/danoeh/antennapod/core/util/download/ConnectionStateMonitor.java @@ -8,7 +8,6 @@ import android.os.Build; import android.util.Log; import androidx.annotation.RequiresApi; -import de.danoeh.antennapod.core.util.NetworkUtils; @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) public class ConnectionStateMonitor @@ -27,7 +26,7 @@ public class ConnectionStateMonitor @Override public void onNetworkActive() { Log.d(TAG, "ConnectionStateMonitor::onNetworkActive network connection changed"); - NetworkUtils.networkChangedDetected(); + NetworkConnectionChangeHandler.networkChangedDetected(); } public void enable(Context context) { diff --git a/core/src/main/java/de/danoeh/antennapod/core/util/download/MediaSizeLoader.java b/core/src/main/java/de/danoeh/antennapod/core/util/download/MediaSizeLoader.java new file mode 100644 index 000000000..67ec35327 --- /dev/null +++ b/core/src/main/java/de/danoeh/antennapod/core/util/download/MediaSizeLoader.java @@ -0,0 +1,78 @@ +package de.danoeh.antennapod.core.util.download; + +import android.text.TextUtils; +import de.danoeh.antennapod.core.service.download.AntennapodHttpClient; +import de.danoeh.antennapod.core.storage.DBWriter; +import de.danoeh.antennapod.core.util.NetworkUtils; +import de.danoeh.antennapod.model.feed.FeedMedia; +import io.reactivex.Single; +import io.reactivex.SingleOnSubscribe; +import io.reactivex.android.schedulers.AndroidSchedulers; +import io.reactivex.schedulers.Schedulers; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import android.util.Log; +import okhttp3.Response; + +import java.io.File; +import java.io.IOException; + +public abstract class MediaSizeLoader { + private static final String TAG = "MediaSizeLoader"; + + public static Single<Long> getFeedMediaSizeObservable(FeedMedia media) { + return Single.create((SingleOnSubscribe<Long>) emitter -> { + if (!NetworkUtils.isEpisodeHeadDownloadAllowed()) { + emitter.onSuccess(0L); + return; + } + long size = Integer.MIN_VALUE; + if (media.isDownloaded()) { + File mediaFile = new File(media.getLocalMediaUrl()); + if (mediaFile.exists()) { + size = mediaFile.length(); + } + } else if (!media.checkedOnSizeButUnknown()) { + // only query the network if we haven't already checked + + String url = media.getDownload_url(); + if (TextUtils.isEmpty(url)) { + emitter.onSuccess(0L); + return; + } + + OkHttpClient client = AntennapodHttpClient.getHttpClient(); + Request.Builder httpReq = new Request.Builder() + .url(url) + .header("Accept-Encoding", "identity") + .head(); + try { + Response response = client.newCall(httpReq.build()).execute(); + if (response.isSuccessful()) { + String contentLength = response.header("Content-Length"); + try { + size = Integer.parseInt(contentLength); + } catch (NumberFormatException e) { + Log.e(TAG, Log.getStackTraceString(e)); + } + } + } catch (IOException e) { + emitter.onSuccess(0L); + Log.e(TAG, Log.getStackTraceString(e)); + return; // better luck next time + } + } + Log.d(TAG, "new size: " + size); + if (size <= 0) { + // they didn't tell us the size, but we don't want to keep querying on it + media.setCheckedOnSizeButUnknown(); + } else { + media.setSize(size); + } + emitter.onSuccess(size); + DBWriter.setFeedMedia(media); + }) + .subscribeOn(Schedulers.io()) + .observeOn(AndroidSchedulers.mainThread()); + } +} diff --git a/core/src/main/java/de/danoeh/antennapod/core/util/download/NetworkConnectionChangeHandler.java b/core/src/main/java/de/danoeh/antennapod/core/util/download/NetworkConnectionChangeHandler.java new file mode 100644 index 000000000..38acf7c7c --- /dev/null +++ b/core/src/main/java/de/danoeh/antennapod/core/util/download/NetworkConnectionChangeHandler.java @@ -0,0 +1,29 @@ +package de.danoeh.antennapod.core.util.download; + +import android.content.Context; +import com.google.android.exoplayer2.util.Log; +import de.danoeh.antennapod.core.service.download.DownloadService; +import de.danoeh.antennapod.core.storage.DBTasks; +import de.danoeh.antennapod.core.util.NetworkUtils; + +public abstract class NetworkConnectionChangeHandler { + private static final String TAG = "NetworkConnectionChangeHandler"; + private static Context context; + + public static void init(Context context) { + NetworkConnectionChangeHandler.context = context; + } + + public static void networkChangedDetected() { + if (NetworkUtils.isAutoDownloadAllowed()) { + Log.d(TAG, "auto-dl network available, starting auto-download"); + DBTasks.autodownloadUndownloadedItems(context); + } else { // if new network is Wi-Fi, finish ongoing downloads, + // otherwise cancel all downloads + if (NetworkUtils.isNetworkRestricted()) { + Log.i(TAG, "Device is no longer connected to Wi-Fi. Cancelling ongoing downloads"); + DownloadService.cancelAll(context); + } + } + } +} |