diff options
4 files changed, 80 insertions, 24 deletions
diff --git a/app/src/main/java/de/danoeh/antennapod/receiver/ConnectivityActionReceiver.java b/app/src/main/java/de/danoeh/antennapod/receiver/ConnectivityActionReceiver.java index 2ea15005a..020f4374b 100644 --- a/app/src/main/java/de/danoeh/antennapod/receiver/ConnectivityActionReceiver.java +++ b/app/src/main/java/de/danoeh/antennapod/receiver/ConnectivityActionReceiver.java @@ -4,37 +4,23 @@ import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.net.ConnectivityManager; -import android.net.NetworkInfo; import android.text.TextUtils; import android.util.Log; + import de.danoeh.antennapod.core.ClientConfig; -import de.danoeh.antennapod.core.storage.DBTasks; -import de.danoeh.antennapod.core.storage.DownloadRequester; import de.danoeh.antennapod.core.util.NetworkUtils; public class ConnectivityActionReceiver extends BroadcastReceiver { - private static final String TAG = "ConnectivityActionRecvr"; + private static final String TAG = "ConnectivityActionRecvr"; - @Override - public void onReceive(final Context context, Intent intent) { - if (TextUtils.equals(intent.getAction(), ConnectivityManager.CONNECTIVITY_ACTION)) { - Log.d(TAG, "Received intent"); + @Override + public void onReceive(final Context context, Intent intent) { + if (TextUtils.equals(intent.getAction(), ConnectivityManager.CONNECTIVITY_ACTION)) { + Log.d(TAG, "Received intent"); ClientConfig.initialize(context); - 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 - ConnectivityManager cm = (ConnectivityManager) context - .getSystemService(Context.CONNECTIVITY_SERVICE); - NetworkInfo ni = cm.getActiveNetworkInfo(); - if (ni == null || ni.getType() != ConnectivityManager.TYPE_WIFI) { - Log.i(TAG, "Device is no longer connected to Wi-Fi. Cancelling ongoing downloads"); - DownloadRequester.getInstance().cancelAllDownloads(context); - } - } - } - } + NetworkUtils.networkChangedDetected(); + } + } } 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 |