diff options
author | Tony Tam <149837+tonytamsf@users.noreply.github.com> | 2021-12-28 11:10:10 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-12-28 20:10:10 +0100 |
commit | 15fe51747695c153f74267827edb18885d200ae3 (patch) | |
tree | 64428995abd3d893dbdfd7fd3f3fc00e558476c5 /core/src | |
parent | 7768aea11dc9551add8e4f21a2fac5ac51874245 (diff) | |
download | AntennaPod-15fe51747695c153f74267827edb18885d200ae3.zip |
Detect network changes with Android > Lollipop (#5609)
Diffstat (limited to 'core/src')
3 files changed, 71 insertions, 1 deletions
diff --git a/core/src/main/java/de/danoeh/antennapod/core/service/download/DownloadService.java b/core/src/main/java/de/danoeh/antennapod/core/service/download/DownloadService.java index 2da0c2db4..6cf6ce107 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/service/download/DownloadService.java +++ b/core/src/main/java/de/danoeh/antennapod/core/service/download/DownloadService.java @@ -8,6 +8,7 @@ import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.Binder; +import android.os.Build; import android.os.Handler; import android.os.IBinder; import android.os.Looper; @@ -39,6 +40,7 @@ import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; import de.danoeh.antennapod.core.event.DownloadEvent; +import de.danoeh.antennapod.core.util.download.ConnectionStateMonitor; import de.danoeh.antennapod.event.FeedItemEvent; import de.danoeh.antennapod.model.feed.Feed; import de.danoeh.antennapod.model.feed.FeedItem; @@ -120,7 +122,7 @@ public class DownloadService extends Service { private static final int SCHED_EX_POOL_SIZE = 1; private final ScheduledThreadPoolExecutor schedExecutor; private static DownloaderFactory downloaderFactory = new DefaultDownloaderFactory(); - + private ConnectionStateMonitor connectionMonitor; private final IBinder mBinder = new LocalBinder(); private class LocalBinder extends Binder { @@ -191,6 +193,11 @@ public class DownloadService extends Service { cancelDownloadReceiverFilter.addAction(ACTION_CANCEL_DOWNLOAD); registerReceiver(cancelDownloadReceiver, cancelDownloadReceiverFilter); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + connectionMonitor = new ConnectionStateMonitor(); + connectionMonitor.enable(getApplicationContext()); + } + downloadCompletionThread.start(); } @@ -225,6 +232,9 @@ public class DownloadService extends Service { downloadPostFuture.cancel(true); } unregisterReceiver(cancelDownloadReceiver); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + connectionMonitor.disable(getApplicationContext()); + } // start auto download in case anything new has shown up DBTasks.autodownloadUndownloadedItems(getApplicationContext()); 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 63b5db1f9..46c1e7f4d 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 @@ -18,6 +18,8 @@ import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; +import de.danoeh.antennapod.core.storage.DBTasks; +import de.danoeh.antennapod.core.storage.DownloadRequester; import de.danoeh.antennapod.model.feed.FeedMedia; import de.danoeh.antennapod.core.preferences.UserPreferences; import de.danoeh.antennapod.core.service.download.AntennapodHttpClient; @@ -227,4 +229,16 @@ public class NetworkUtils { .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"); + DownloadRequester.getInstance().cancelAllDownloads(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 new file mode 100644 index 000000000..5c543bf5a --- /dev/null +++ b/core/src/main/java/de/danoeh/antennapod/core/util/download/ConnectionStateMonitor.java @@ -0,0 +1,46 @@ +package de.danoeh.antennapod.core.util.download; + +import android.content.Context; +import android.net.ConnectivityManager; +import android.net.NetworkCapabilities; +import android.net.NetworkRequest; +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 + extends ConnectivityManager.NetworkCallback + implements ConnectivityManager.OnNetworkActiveListener { + private static final String TAG = "ConnectionStateMonitor"; + final NetworkRequest networkRequest; + + public ConnectionStateMonitor() { + networkRequest = new NetworkRequest.Builder() + .addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR) + .addTransportType(NetworkCapabilities.TRANSPORT_WIFI) + .build(); + } + + @Override + public void onNetworkActive() { + Log.d(TAG, "ConnectionStateMonitor::onNetworkActive network connection changed"); + NetworkUtils.networkChangedDetected(); + } + + public void enable(Context context) { + ConnectivityManager connectivityManager = + (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); + connectivityManager.registerNetworkCallback(networkRequest, this); + connectivityManager.addDefaultNetworkActiveListener(this); + } + + public void disable(Context context) { + ConnectivityManager connectivityManager = + (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); + connectivityManager.unregisterNetworkCallback(this); + connectivityManager.removeDefaultNetworkActiveListener(this); + } +}
\ No newline at end of file |