Make NetworkUtils less fat

Breaks dependency cycles
This commit is contained in:
ByteHamster 2022-11-02 23:18:28 +01:00
parent 6c1ec57bc1
commit cd9845ed4c
7 changed files with 114 additions and 93 deletions

View File

@ -8,7 +8,7 @@ import android.text.TextUtils;
import android.util.Log;
import de.danoeh.antennapod.core.ClientConfigurator;
import de.danoeh.antennapod.core.util.NetworkUtils;
import de.danoeh.antennapod.core.util.download.NetworkConnectionChangeHandler;
public class ConnectivityActionReceiver extends BroadcastReceiver {
private static final String TAG = "ConnectivityActionRecvr";
@ -19,7 +19,7 @@ public class ConnectivityActionReceiver extends BroadcastReceiver {
Log.d(TAG, "Received intent");
ClientConfigurator.initialize(context);
NetworkUtils.networkChangedDetected();
NetworkConnectionChangeHandler.networkChangedDetected();
}
}
}

View File

@ -23,6 +23,7 @@ import de.danoeh.antennapod.adapter.CoverLoader;
import de.danoeh.antennapod.adapter.actionbutton.ItemActionButton;
import de.danoeh.antennapod.core.service.download.DownloadRequest;
import de.danoeh.antennapod.core.service.download.DownloadService;
import de.danoeh.antennapod.core.util.download.MediaSizeLoader;
import de.danoeh.antennapod.event.playback.PlaybackPositionEvent;
import de.danoeh.antennapod.core.util.DateFormatter;
import de.danoeh.antennapod.model.feed.FeedItem;
@ -182,7 +183,7 @@ public class EpisodeItemViewHolder extends RecyclerView.ViewHolder {
} else if (NetworkUtils.isEpisodeHeadDownloadAllowed() && !media.checkedOnSizeButUnknown()) {
size.setText("{fa-spinner}");
Iconify.addIcons(size);
NetworkUtils.getFeedMediaSizeObservable(media).subscribe(
MediaSizeLoader.getFeedMediaSizeObservable(media).subscribe(
sizeValue -> {
if (sizeValue > 0) {
size.setText(Formatter.formatShortFileSize(activity, sizeValue));

View File

@ -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);

View File

@ -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);
}
}
}
}

View File

@ -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) {

View File

@ -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());
}
}

View File

@ -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);
}
}
}
}