Detect network changes with Android > Lollipop (#5609)
This commit is contained in:
parent
7768aea11d
commit
15fe517476
|
@ -4,37 +4,23 @@ import android.content.BroadcastReceiver;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.net.ConnectivityManager;
|
import android.net.ConnectivityManager;
|
||||||
import android.net.NetworkInfo;
|
|
||||||
import android.text.TextUtils;
|
import android.text.TextUtils;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
|
|
||||||
import de.danoeh.antennapod.core.ClientConfig;
|
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;
|
import de.danoeh.antennapod.core.util.NetworkUtils;
|
||||||
|
|
||||||
public class ConnectivityActionReceiver extends BroadcastReceiver {
|
public class ConnectivityActionReceiver extends BroadcastReceiver {
|
||||||
private static final String TAG = "ConnectivityActionRecvr";
|
private static final String TAG = "ConnectivityActionRecvr";
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onReceive(final Context context, Intent intent) {
|
public void onReceive(final Context context, Intent intent) {
|
||||||
if (TextUtils.equals(intent.getAction(), ConnectivityManager.CONNECTIVITY_ACTION)) {
|
if (TextUtils.equals(intent.getAction(), ConnectivityManager.CONNECTIVITY_ACTION)) {
|
||||||
Log.d(TAG, "Received intent");
|
Log.d(TAG, "Received intent");
|
||||||
|
|
||||||
ClientConfig.initialize(context);
|
ClientConfig.initialize(context);
|
||||||
if (NetworkUtils.isAutoDownloadAllowed()) {
|
NetworkUtils.networkChangedDetected();
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,6 +8,7 @@ import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.content.IntentFilter;
|
import android.content.IntentFilter;
|
||||||
import android.os.Binder;
|
import android.os.Binder;
|
||||||
|
import android.os.Build;
|
||||||
import android.os.Handler;
|
import android.os.Handler;
|
||||||
import android.os.IBinder;
|
import android.os.IBinder;
|
||||||
import android.os.Looper;
|
import android.os.Looper;
|
||||||
|
@ -39,6 +40,7 @@ import java.util.concurrent.TimeUnit;
|
||||||
import java.util.concurrent.atomic.AtomicInteger;
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
|
||||||
import de.danoeh.antennapod.core.event.DownloadEvent;
|
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.event.FeedItemEvent;
|
||||||
import de.danoeh.antennapod.model.feed.Feed;
|
import de.danoeh.antennapod.model.feed.Feed;
|
||||||
import de.danoeh.antennapod.model.feed.FeedItem;
|
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 static final int SCHED_EX_POOL_SIZE = 1;
|
||||||
private final ScheduledThreadPoolExecutor schedExecutor;
|
private final ScheduledThreadPoolExecutor schedExecutor;
|
||||||
private static DownloaderFactory downloaderFactory = new DefaultDownloaderFactory();
|
private static DownloaderFactory downloaderFactory = new DefaultDownloaderFactory();
|
||||||
|
private ConnectionStateMonitor connectionMonitor;
|
||||||
private final IBinder mBinder = new LocalBinder();
|
private final IBinder mBinder = new LocalBinder();
|
||||||
|
|
||||||
private class LocalBinder extends Binder {
|
private class LocalBinder extends Binder {
|
||||||
|
@ -191,6 +193,11 @@ public class DownloadService extends Service {
|
||||||
cancelDownloadReceiverFilter.addAction(ACTION_CANCEL_DOWNLOAD);
|
cancelDownloadReceiverFilter.addAction(ACTION_CANCEL_DOWNLOAD);
|
||||||
registerReceiver(cancelDownloadReceiver, cancelDownloadReceiverFilter);
|
registerReceiver(cancelDownloadReceiver, cancelDownloadReceiverFilter);
|
||||||
|
|
||||||
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
||||||
|
connectionMonitor = new ConnectionStateMonitor();
|
||||||
|
connectionMonitor.enable(getApplicationContext());
|
||||||
|
}
|
||||||
|
|
||||||
downloadCompletionThread.start();
|
downloadCompletionThread.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -225,6 +232,9 @@ public class DownloadService extends Service {
|
||||||
downloadPostFuture.cancel(true);
|
downloadPostFuture.cancel(true);
|
||||||
}
|
}
|
||||||
unregisterReceiver(cancelDownloadReceiver);
|
unregisterReceiver(cancelDownloadReceiver);
|
||||||
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
||||||
|
connectionMonitor.disable(getApplicationContext());
|
||||||
|
}
|
||||||
|
|
||||||
// start auto download in case anything new has shown up
|
// start auto download in case anything new has shown up
|
||||||
DBTasks.autodownloadUndownloadedItems(getApplicationContext());
|
DBTasks.autodownloadUndownloadedItems(getApplicationContext());
|
||||||
|
|
|
@ -18,6 +18,8 @@ import java.util.List;
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
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.model.feed.FeedMedia;
|
||||||
import de.danoeh.antennapod.core.preferences.UserPreferences;
|
import de.danoeh.antennapod.core.preferences.UserPreferences;
|
||||||
import de.danoeh.antennapod.core.service.download.AntennapodHttpClient;
|
import de.danoeh.antennapod.core.service.download.AntennapodHttpClient;
|
||||||
|
@ -227,4 +229,16 @@ public class NetworkUtils {
|
||||||
.observeOn(AndroidSchedulers.mainThread());
|
.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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue