package com.stanfy.utils;

import java.io.Serializable;

import android.content.Context;
import android.net.ConnectivityManager;
import android.os.RemoteException;
import android.util.Log;

import com.stanfy.DebugFlags;
import com.stanfy.serverapi.request.RequestDescription;
import com.stanfy.serverapi.response.ResponseData;

/**
 * Requests performer for the server side.
 * @author Roman Mazur (Stanfy - http://www.stanfy.com)
 */
public class ServiceRequestPerformer extends RequestPerformer  {

  /** Logging tag. */
  private static final String TAG = "ServiceRequestPerformer";

  /** Debug flag. */
  private static final boolean DEBUG = DebugFlags.DEBUG_SERVICES;

  /** Connectivity manager. */
  private final ConnectivityManager connectivityManager;

  public ServiceRequestPerformer(final Context a, final RequestCallback callback) {
    super(a, callback);
    callback.performer = this;
    connectivityManager = (ConnectivityManager)a.getSystemService(Context.CONNECTIVITY_SERVICE);
  }

  @Override
  public void performRequest(final RequestDescription description) {
    if (connectivityManager.getBackgroundDataSetting()) {
      bind();
      registerListener();
      super.performRequest(description);
    }
  }

  @Override
  protected void doRequest(final RequestDescription description) {
    try {
      if (DEBUG) { Log.d(TAG, "Perform " + description.getOperationCode()); }
      serviceObject.performRequest(description);
    } catch (final RemoteException e) {
      Log.e(TAG, "Cannot perform operation " + description.getOperationCode(), e);
    }
  }

  /**
   * Provides default behavior.
   * @author Roman Mazur - Stanfy (http://www.stanfy.com)
   */
  public abstract static class RequestCallback extends com.stanfy.serverapi.request.RequestCallback<Serializable> {
    /** Performer instance. */
    ServiceRequestPerformer performer;

    @Override
    public boolean isModelInterest() { return false;  }

    @Override
    public final void reportError(final int token, final int operation, final ResponseData responseData) {
      if (!filterOperation(token, operation)) { return; }
      if (DEBUG) {
        Log.d(TAG, "Problems with operation " + operation + ". Error code: "
            + responseData.getErrorCode() + ". Message: " + responseData.getMessage());
      }
      performer.unbind();
      onError(token, operation, responseData);
    }
    @Override
    public final void reportSuccess(final int token, final int operation, final ResponseData responseData, final Serializable model) {
      if (!filterOperation(token, operation)) { return; }
      performer.unbind();
      onSuccess(token, operation, responseData);
    }
    @Override
    public void reportSuccessUnknownModelType(final int token, final int operation, final ResponseData responseData, final Serializable model) {
      reportSuccess(token, operation, responseData, model);
    }

    protected boolean filterOperation(final int token, final int op) { return false; }

    protected void onError(final int token, final int operation, final ResponseData responseData) { /* empty */ }

    protected void onSuccess(final int token, final int operation, final ResponseData responseData) { /* empty */ }

  }

}

@@ -91,49 +91,8 @@
 t;%0D%0A
-import android.net.ConnectivityManager;%0D%0A
 impo
@@ -656,100 +656,8 @@
 %0D%0A%0D%0A
-  /** Connectivity manager. */%0D%0A  private final ConnectivityManager connectivityManager;%0D%0A%0D%0A
   pu
@@ -798,106 +798,8 @@
 s;%0D%0A
-    connectivityManager = (ConnectivityManager)a.getSystemService(Context.CONNECTIVITY_SERVICE);%0D%0A
   %7D%0D
@@ -888,69 +888,8 @@
  %7B%0D%0A
-    if (connectivityManager.getBackgroundDataSetting()) %7B%0D%0A  
     
@@ -901,18 +901,16 @@
 );%0D%0A    
- 
- 
 register
@@ -918,26 +918,24 @@
 istener();%0D%0A
-  
     super.pe
@@ -958,31 +958,24 @@
 cription);%0D%0A
-    %7D%0D%0A
   %7D%0D%0A%0D%0A  @Ov

remove background data checks from the service request performer