package de.danoeh.antennapod.core.util;

import android.content.Context;
import android.util.Log;

import org.awaitility.core.ConditionTimeoutException;

import java.util.concurrent.TimeUnit;

import de.danoeh.antennapod.core.storage.DBTasks;

import static org.awaitility.Awaitility.with;

public class FeedUpdateUtils {
    private static final String TAG = "FeedUpdateUtils";

    private FeedUpdateUtils() {}

    public static void startAutoUpdate(Context context, Runnable callback) {
        try {
            with().pollInterval(1, TimeUnit.SECONDS)
                    .await()
                    .atMost(10, TimeUnit.SECONDS)
                    .until(() -> NetworkUtils.networkAvailable() && NetworkUtils.isDownloadAllowed());
            DBTasks.refreshAllFeeds(context, null, callback);
        } catch (ConditionTimeoutException ignore) {
            Log.d(TAG, "Blocking automatic update: no wifi available / no mobile updates allowed");
        }
    }

}

@@ -496,14 +496,221 @@
     
-try %7B%0A
+// the network check is blocking for possibly a long time: so run the logic%0A        // in a separate thread to prevent the code blocking the callers%0A        final Runnable runnable = () -%3E %7B%0A            try %7B%0A    
     
@@ -778,16 +778,20 @@
         
+    
 .await()
@@ -811,16 +811,20 @@
         
+  
+  
 .atMost(
@@ -837,32 +837,36 @@
 meUnit.SECONDS)%0A
+    
                 
@@ -964,16 +964,20 @@
         
+   
+ 
 DBTasks.
@@ -1026,16 +1026,20 @@
         
+    
 %7D catch 
@@ -1071,24 +1071,28 @@
 n ignore) %7B%0A
+    
             
@@ -1187,17 +1187,70 @@
         
-%7D
+    %7D%0A        %7D;%0A        new Thread(runnable).start();
 %0A    %7D%0A%0A

fix #2956 - prevent ANR during some automatic feed update