Initialize NetworkUtils, set context once

Had some issues with the new NetworkAllowanceInterceptor. When I gave
it a constructor that would hold the context (to use it with
NetworkUtils), the whole Glide image loading process would not work.
This commit is contained in:
Martin Fietz 2015-08-02 17:48:08 +02:00
parent 65d470043b
commit ab116ee6be
9 changed files with 27 additions and 38 deletions

View File

@ -8,6 +8,7 @@ import de.danoeh.antennapod.core.feed.EventDistributor;
import de.danoeh.antennapod.core.preferences.PlaybackPreferences;
import de.danoeh.antennapod.core.preferences.UserPreferences;
import de.danoeh.antennapod.core.service.FeedMediaSizeService;
import de.danoeh.antennapod.core.util.NetworkUtils;
import de.danoeh.antennapod.spa.SPAUtil;
/** Main application class. */
@ -41,6 +42,7 @@ public class PodcastApp extends Application {
UpdateManager.init(this);
UserPreferences.init(this);
PlaybackPreferences.init(this);
NetworkUtils.init(this);
EventDistributor.getInstance();
SPAUtil.sendSPAppsQueryFeedsIntent(this);

View File

@ -12,11 +12,8 @@ import de.danoeh.antennapod.R;
import de.danoeh.antennapod.core.dialog.DownloadRequestErrorDialogCreator;
import de.danoeh.antennapod.core.feed.FeedItem;
import de.danoeh.antennapod.core.feed.FeedMedia;
import de.danoeh.antennapod.core.preferences.UserPreferences;
import de.danoeh.antennapod.core.service.playback.PlaybackService;
import de.danoeh.antennapod.core.gpoddernet.model.GpodnetEpisodeAction;
import de.danoeh.antennapod.core.preferences.GpodnetPreferences;
import de.danoeh.antennapod.core.storage.DBReader;
import de.danoeh.antennapod.core.storage.DBTasks;
import de.danoeh.antennapod.core.storage.DBWriter;
@ -61,7 +58,7 @@ public class DefaultActionButtonCallback implements ActionButtonCallback {
boolean isDownloading = DownloadRequester.getInstance().isDownloadingFile(media);
if (!isDownloading && !media.isDownloaded()) {
LongList queueIds = DBReader.getQueueIDList(context);
if (NetworkUtils.isDownloadAllowed(context) || userAllowedMobileDownloads()) {
if (NetworkUtils.isDownloadAllowed() || userAllowedMobileDownloads()) {
try {
DBTasks.downloadFeedItems(context, item);
Toast.makeText(context, R.string.status_downloading_label, Toast.LENGTH_SHORT).show();

View File

@ -9,7 +9,6 @@ import android.util.Log;
import org.apache.commons.lang3.StringUtils;
import de.danoeh.antennapod.core.BuildConfig;
import de.danoeh.antennapod.core.storage.DBTasks;
import de.danoeh.antennapod.core.storage.DownloadRequester;
import de.danoeh.antennapod.core.util.NetworkUtils;
@ -20,13 +19,10 @@ public class ConnectivityActionReceiver extends BroadcastReceiver {
@Override
public void onReceive(final Context context, Intent intent) {
if (StringUtils.equals(intent.getAction(), ConnectivityManager.CONNECTIVITY_ACTION)) {
if (BuildConfig.DEBUG)
Log.d(TAG, "Received intent");
Log.d(TAG, "Received intent");
if (NetworkUtils.autodownloadNetworkAvailable(context)) {
if (BuildConfig.DEBUG)
Log.d(TAG,
"auto-dl network available, starting auto-download");
if (NetworkUtils.autodownloadNetworkAvailable()) {
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
@ -34,12 +30,9 @@ public class ConnectivityActionReceiver extends BroadcastReceiver {
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ni = cm.getActiveNetworkInfo();
if (ni == null || ni.getType() != ConnectivityManager.TYPE_WIFI) {
if (BuildConfig.DEBUG)
Log.i(TAG,
"Device is no longer connected to Wi-Fi. Cancelling ongoing downloads");
Log.i(TAG, "Device is no longer connected to Wi-Fi. Cancelling ongoing downloads");
DownloadRequester.getInstance().cancelAllDownloads(context);
}
}
}
}

View File

@ -90,7 +90,7 @@ public class FlattrClickWorker extends AsyncTask<Void, Integer, FlattrClickWorke
return ExitCode.NO_TOKEN;
}
if (!NetworkUtils.networkAvailable(context)) {
if (!NetworkUtils.networkAvailable()) {
return ExitCode.NO_NETWORK;
}

View File

@ -19,7 +19,7 @@ public class FeedUpdateReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "Received intent");
if (NetworkUtils.isDownloadAllowed(context)) {
if (NetworkUtils.isDownloadAllowed()) {
DBTasks.refreshAllFeeds(context, null);
} else {
Log.d(TAG, "Blocking automatic update: no wifi available / no mobile updates allowed");

View File

@ -28,13 +28,13 @@ public class FeedMediaSizeService extends IntentService {
@Override
protected void onHandleIntent(Intent intent) {
Log.d(TAG, "onHandleIntent()");
if(false == NetworkUtils.isDownloadAllowed(this)) {
if(false == NetworkUtils.isDownloadAllowed()) {
return;
}
List<FeedMedia> list = DBReader.getFeedMediaUnknownSize(this);
for (FeedMedia media : list) {
Log.d(TAG, "Getting size currently " + media.getSize() + " for " + media.getDownload_url());
if(false == NetworkUtils.isDownloadAllowed(this)) {
if(false == NetworkUtils.isDownloadAllowed()) {
return;
}
long size = Integer.MIN_VALUE;

View File

@ -108,7 +108,7 @@ public class GpodnetSyncService extends Service {
private synchronized void sync() {
if (GpodnetPreferences.loggedIn() == false || NetworkUtils.networkAvailable(this) == false) {
if (GpodnetPreferences.loggedIn() == false || NetworkUtils.networkAvailable() == false) {
stopSelf();
return;
}

View File

@ -40,7 +40,7 @@ public class APDownloadAlgorithm implements AutomaticDownloadAlgorithm {
public void run() {
// true if we should auto download based on network status
boolean networkShouldAutoDl = NetworkUtils.autodownloadNetworkAvailable(context)
boolean networkShouldAutoDl = NetworkUtils.autodownloadNetworkAvailable()
&& UserPreferences.isEnableAutodownload();
// true if we should auto download based on power status

View File

@ -10,14 +10,16 @@ import android.util.Log;
import java.util.Arrays;
import java.util.List;
import de.danoeh.antennapod.core.BuildConfig;
import de.danoeh.antennapod.core.preferences.UserPreferences;
public class NetworkUtils {
private static final String TAG = "NetworkUtils";
private NetworkUtils() {
private static final String TAG = NetworkUtils.class.getSimpleName();
private static Context context;
public static void init(Context context) {
NetworkUtils.context = context;
}
/**
@ -26,18 +28,16 @@ public class NetworkUtils {
* network that is on the 'selected networks' list of the Wi-Fi filter for
* automatic downloads and false otherwise.
* */
public static boolean autodownloadNetworkAvailable(Context context) {
public static boolean autodownloadNetworkAvailable() {
ConnectivityManager cm = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = cm.getActiveNetworkInfo();
if (networkInfo != null) {
if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
if (BuildConfig.DEBUG)
Log.d(TAG, "Device is connected to Wi-Fi");
Log.d(TAG, "Device is connected to Wi-Fi");
if (networkInfo.isConnected()) {
if (!UserPreferences.isEnableAutodownloadWifiFilter()) {
if (BuildConfig.DEBUG)
Log.d(TAG, "Auto-dl filter is disabled");
Log.d(TAG, "Auto-dl filter is disabled");
return true;
} else {
WifiManager wm = (WifiManager) context
@ -48,31 +48,28 @@ public class NetworkUtils {
.getAutodownloadSelectedNetworks());
if (selectedNetworks.contains(Integer.toString(wifiInfo
.getNetworkId()))) {
if (BuildConfig.DEBUG)
Log.d(TAG,
"Current network is on the selected networks list");
Log.d(TAG, "Current network is on the selected networks list");
return true;
}
}
}
}
}
if (BuildConfig.DEBUG)
Log.d(TAG, "Network for auto-dl is not available");
Log.d(TAG, "Network for auto-dl is not available");
return false;
}
public static boolean networkAvailable(Context context) {
public static boolean networkAvailable() {
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = cm.getActiveNetworkInfo();
return info != null && info.isConnected();
}
public static boolean isDownloadAllowed(Context context) {
return UserPreferences.isAllowMobileUpdate() || NetworkUtils.connectedToWifi(context);
public static boolean isDownloadAllowed() {
return UserPreferences.isAllowMobileUpdate() || NetworkUtils.connectedToWifi();
}
public static boolean connectedToWifi(Context context) {
public static boolean connectedToWifi() {
ConnectivityManager connManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mWifi = connManager