package com.novoda.downloadmanager;

import android.annotation.TargetApi;
import android.content.ContentResolver;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkCapabilities;
import android.net.NetworkRequest;
import android.os.Build;

import com.novoda.downloadmanager.lib.DownloadManager;
import com.novoda.notils.logger.simple.Log;

/**
 * A client can specify whether the downloads are allowed to proceed by implementing
 * {@link com.novoda.downloadmanager.lib.DownloadClientReadyChecker} on your Application class
 *
 */
public class DownloadManagerBuilder {

    private static final int ANDROID_N = 24;

    private final Context context;

    private boolean verboseLogging;

    DownloadManagerBuilder(Context context) {
        this.context = context;
    }

    public static DownloadManagerBuilder from(Context context) {
        return new DownloadManagerBuilder(context.getApplicationContext());
    }

    public DownloadManagerBuilder withVerboseLogging() {
        this.verboseLogging = true;
        return this;
    }

    public DownloadManager build() {
        ContentResolver contentResolver = context.getContentResolver();
        DownloadManager downloadManager = new DownloadManager(context, contentResolver, verboseLogging);
        updateOnNetworkChanges(downloadManager);
        return downloadManager;
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    private void updateOnNetworkChanges(final DownloadManager downloadManager) {
        if (deviceDoesNotSupportConnectivityChangesBroadcast()) {
            ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkRequest networkRequest = new NetworkRequest.Builder().addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET).build();
            connectivityManager.registerNetworkCallback(networkRequest, new ConnectivityManager.NetworkCallback() {
                @Override
                public void onAvailable(Network network) {
                    Log.v("network is active now from inside the download manager");
                    downloadManager.forceStart();
                }
            });
        }
    }

    private boolean deviceDoesNotSupportConnectivityChangesBroadcast() {
        return Build.VERSION.SDK_INT >= ANDROID_N;
    }
}

@@ -1553,38 +1553,32 @@
   if (device
-DoesNot
 Support
+s
 Connectivity
@@ -1595,32 +1595,59 @@
 adcast()) %7B%0A    
+        return;%0A        %7D%0A%0A
         Connecti
@@ -1758,20 +1758,16 @@
 RVICE);%0A
-    
         
@@ -1903,20 +1903,16 @@
         
-    
 connecti
@@ -2019,20 +2019,16 @@
         
- 
-   
 @Overrid
@@ -2029,20 +2029,16 @@
 verride%0A
-    
         
@@ -2080,28 +2080,24 @@
  network) %7B%0A
-    
             
@@ -2181,20 +2181,16 @@
         
-    
 download
@@ -2227,25 +2227,18 @@
     
-    %7D%0A   
+%7D%0A
         
  %7D);
@@ -2237,23 +2237,12 @@
     
- 
 %7D);%0A
-        %7D%0A
     
@@ -2274,22 +2274,16 @@
 vice
-DoesNot
 Support
+s
 Conn
@@ -2352,10 +2352,9 @@
 INT 
-%3E=
+%3C
  AND

Return fast