package retryable.asyncTask;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;

import java.util.concurrent.ExecutionException;

public abstract class RetryableAsyncTask<Params, Progress, Result> {
  private final Context context;
  private AsyncTask task;

  public RetryableAsyncTask(Context context) {
    this.context = context;
  }

  public final AsyncTask<Params, Progress, Result> execute(final Params... params) {
    if (isNotConnected()) {
      showDialog(context.getResources().getString(R.string.no_internet_connection), params);

      task = new NullableAsyncTask();
    } else {
      task = new ConcreteAsyncTask(this);
    }

    return task.execute(params);
  }

  public final Result get() throws InterruptedException, ExecutionException {
    return (Result) task.get();
  }

  protected void onPreExecute() {};

  protected abstract Result doInBackground(final Params... params);
  protected abstract void onPostExecute(final Result result);

  private void showDialog(String message, final Params... params) {
    AlertDialog dialog = new AlertDialog.Builder(context)
        .setMessage(message)
        .setPositiveButton(R.string.retry, new DialogInterface.OnClickListener() {
          @Override
          public void onClick(DialogInterface dialog, int which) {
            RetryableAsyncTask.this.execute(params);
            dialog.dismiss();
          }
        })
        .create();

    dialog.show();
  }

  private boolean isNotConnected() {
    return !isConnected();
  }

  private boolean isConnected() {
    return getActiveNetworkInfo() != null &&
        getActiveNetworkInfo().isConnectedOrConnecting();
  }

  private NetworkInfo getActiveNetworkInfo() {
    return getConnectivityManager().getActiveNetworkInfo();
  }

  private ConnectivityManager getConnectivityManager() {
    return (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
  }
}

@@ -131,80 +131,8 @@
 ce;%0A
-import android.net.ConnectivityManager;%0Aimport android.net.NetworkInfo;%0A
 impo
@@ -206,16 +206,74 @@
 ption;%0A%0A
+import retryable.asyncTask.checkers.ConnectivityChecker;%0A%0A
 public a
@@ -366,16 +366,74 @@
 ontext;%0A
+  private final ConnectivityChecker connectivityChecker;%0A%0A
   privat
@@ -526,16 +526,76 @@
 ontext;%0A
+    connectivityChecker = new ConnectivityChecker(context);%0A
   %7D%0A%0A  p
@@ -688,25 +688,97 @@
 if (
-isNotConnected())
+connectivityChecker.isConnected()) %7B%0A      task = new ConcreteAsyncTask(this);%0A    %7D else
  %7B%0A 
@@ -917,63 +917,8 @@
    %7D
- else %7B%0A      task = new ConcreteAsyncTask(this);%0A    %7D
 %0A%0A  
@@ -1713,481 +1713,7 @@
 %0A  %7D
-%0A%0A  private boolean isNotConnected() %7B%0A    return !isConnected();%0A  %7D%0A%0A  private boolean isConnected() %7B%0A    return getActiveNetworkInfo() != null &&%0A        getActiveNetworkInfo().isConnectedOrConnecting();%0A  %7D%0A%0A  private NetworkInfo getActiveNetworkInfo() %7B%0A    return getConnectivityManager().getActiveNetworkInfo();%0A  %7D%0A%0A  private ConnectivityManager getConnectivityManager() %7B%0A    return (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);%0A  %7D
 %0A%7D%0A

Extract logic to ConnectivityChecker