<commit_before>/**
  {{serviceName}}
  {{projectName}}

  Created by Generator
  Copyright (c) 2012-2014 Mobivery. All rights reserved.
  Version: {{version}}
*/
  package {{{packagename}}}.tasks.{{{message.serviceNameLower}}};

  import {{{packagename}}}.R;
  import {{{packagename}}}.model.dto.*; 
  import {{{packagename}}}.logic.*;

  import com.mobivery.utils.ResponseInfo;
  import com.mobivery.logic.TaskListener;

  import java.lang.ref.WeakReference;
  import java.lang.reflect.Method;

  import android.app.Activity;
  import android.app.Dialog;
  import android.graphics.drawable.ColorDrawable;
  import android.os.AsyncTask;
  import android.view.Window;
  import android.widget.TextView;

/**
 * Task de {{{message.method}}}
 *
 */
public class {{{message.methodUpperCase}}}Task extends AsyncTask<{{{message.request.name}}},Integer, {{{message.response.name}}}> implements com.mobivery.logic.ServiceExceptionListener {

	private TaskListener<{{{message.request.name}}},{{{message.response.name}}}> listener;

	private WeakReference<Activity> activityReference;
	private int loadingView;
	private int progressText;
	private int loadingText;
	private Dialog progressDialog;

    /**
     * Creates default Task for method {{{message.method}}}
     * @param activity parent Activity
     */
    public {{{message.methodUpperCase}}}Task(Activity activity) {
    	this(activity,-1,-1,-1);
    }

    /**
     * Creates default Task for method {{{message.method}}}
     */
    public {{{message.methodUpperCase}}}Task() {
    	this(null,-1,-1,-1);
    }
    /**
     * Creates Task for method {{{message.method}}}
     * @param loadingView View resource to show while task is running
     * @param progressText Resource id for loading dialog message (TextView id)
     * @param loadingText Resource id for loading dialog message (String id)
     */
    public {{{message.methodUpperCase}}}Task(int loadingView,int progressText, int loadingText) {
    	this(null,loadingView,progressText,loadingText);
    }
	/**
     * Creates Task for method {{{message.method}}}
     * @param activity parent Activity to get Resources from
     * @param loadingView View resource to show while task is running
     * @param progressText Resource id for loading dialog message (TextView id)
     * @param loadingText Resource id for loading dialog message (String id)
     */
	public {{{message.methodUpperCase}}}Task(Activity activity,int loadingView,int progressText, int loadingText) {
		if (activity != null) {
			this.activityReference = new WeakReference<Activity>(activity);		
		} 
		this.loadingView=loadingView;
		this.progressText=progressText;
		this.loadingText=loadingText;
	}
	/**
	 * Executes the async task with request
	 * @param request Server request DTO
	 */
	public void execute({{{message.request.name}}} request) {
		if(loadingView!=-1) {
			Activity activity = (activityReference!=null)? activityReference.get() : null;
			if(activity!=null){
				activity.runOnUiThread(new Thread(new Runnable(){
					public void run() {
						initSpinner();
					}
				}));
			}
		}
		super.execute(request);
	}
	/**
	 * Start spinner for long operations
	 */
	private void initSpinner() {
		Activity activity = (activityReference!=null)? activityReference.get() : null;
		if (activity != null) {
			progressDialog = new Dialog(activity,android.R.style.Theme_Light_NoTitleBar);
			progressDialog.setCancelable(false);
			progressDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
			progressDialog.getWindow().setBackgroundDrawable(new ColorDrawable(0));
			progressDialog.setContentView(loadingView);
			TextView loadingTextView = (TextView)progressDialog.findViewById(progressText);
			if (loadingTextView != null){
				loadingTextView.setText(this.loadingText);
			}
			progressDialog.show();
		}
	}
    /**
     * End spinner
     */
    private void endSpinner() {
    	Activity activity = (activityReference!=null)? activityReference.get() : null;
    	if (activity != null) {
    		if (progressDialog != null) {
    			try {
    				progressDialog.dismiss();
    			} catch(java.lang.IllegalArgumentException ex) {
        			// Ignore: View not attached to window manager
    			}
    		}
    	}
    }
	/**
     * Update async task dialog message
     * @param progress Message to update
     */
	public void updateProgress(final String progress) {
		Activity activity = (activityReference!=null)? activityReference.get() : null;
		if (activity != null) {
			activity.runOnUiThread(new Runnable() {
				public void run() {
					if(progressDialog!=null) {
						TextView loadingTextView = (TextView)progressDialog.findViewById(progressText);
						if(loadingTextView != null){
							loadingTextView.setText({{{message.methodUpperCase}}}Task.this.loadingText);
						}
					}
				}
			});
		}
	}

	@Override
	public final {{{message.response.name}}} doInBackground({{{message.request.name}}}... params) {
		// Get Request DTO
		final {{{message.request.name}}} request=params[0];
		{{{message.response.name}}} response=null;
		try {
			{{#preMethod}}
			{{{preMethod}}}(request);
			{{/preMethod}}
			// Call Service
			final ResponseInfo responseInfo = new ResponseInfo();
			final {{{message.response.name}}} tempResponse = execOnBackground(request,responseInfo);
			response = tempResponse;

			Activity activity = (activityReference!=null)? activityReference.get() : null;
			if(activity!=null && listener!=null && !isCancelled()){
				activity.runOnUiThread(new Runnable(){
					public void run() {
						listener.onServiceResponse(request,tempResponse,responseInfo);
					}
				});
			}
			if(activity==null && listener!=null && !isCancelled()) {
				listener.onServiceResponse(request,tempResponse,responseInfo);
			}
			{{#onTask}}
			if (activity==null) {
				throw new com.mobivery.logic.ServiceException(new Exception("Activity to go back to already removed from memory"));
			} else {
				activity.runOnUiThread(new Runnable(){
					public void run() {
						{{{onTask}}}({{{message.methodUpperCase}}}Task.this,{{{message.methodUpperCase}}}Task.this.activityReference.get());
					}
				});
			}
			{{/onTask}}
		} catch (final com.mobivery.logic.ServiceException ex) {
			if(!isCancelled()){
			// If error, call onServiceError on UI Thread
				if (activity!=null && listener!=null) {
					activity.runOnUiThread(new Runnable(){
						public void run(){
							endSpinner();
							listener.onServiceError(request,ex);
						}
					});
				}
				if(activity==null && listener!=null) {
					endSpinner();
					listener.onServiceError(request,ex);
				}
				return null;
			}
		} catch (InterruptedException e) { 
			return null;
		}
		if (loadingView!=-1) {
			if(activity!=null){
				activity.runOnUiThread(new Runnable(){
					public void run(){
						endSpinner();
					}
				});
			}
		}
		return response;<commit_msg>Removed the responses handled to the user when the activity is not set if the TaskResponse is set<commit_after>/**
  {{serviceName}}
  {{projectName}}

  Created by Generator
  Copyright (c) 2012-2014 Mobivery. All rights reserved.
  Version: {{version}}
*/
  package {{{packagename}}}.tasks.{{{message.serviceNameLower}}};

  import {{{packagename}}}.R;
  import {{{packagename}}}.model.dto.*; 
  import {{{packagename}}}.logic.*;

  import com.mobivery.utils.ResponseInfo;
  import com.mobivery.logic.TaskListener;

  import java.lang.ref.WeakReference;
  import java.lang.reflect.Method;

  import android.app.Activity;
  import android.app.Dialog;
  import android.graphics.drawable.ColorDrawable;
  import android.os.AsyncTask;
  import android.view.Window;
  import android.widget.TextView;

/**
 * Task de {{{message.method}}}
 *
 */
public class {{{message.methodUpperCase}}}Task extends AsyncTask<{{{message.request.name}}},Integer, {{{message.response.name}}}> implements com.mobivery.logic.ServiceExceptionListener {

	private TaskListener<{{{message.request.name}}},{{{message.response.name}}}> listener;

	private WeakReference<Activity> activityReference;
	private int loadingView;
	private int progressText;
	private int loadingText;
	private Dialog progressDialog;

    /**
     * Creates default Task for method {{{message.method}}}
     * @param activity parent Activity
     */
    public {{{message.methodUpperCase}}}Task(Activity activity) {
    	this(activity,-1,-1,-1);
    }

    /**
     * Creates default Task for method {{{message.method}}}
     */
    public {{{message.methodUpperCase}}}Task() {
    	this(null,-1,-1,-1);
    }
    /**
     * Creates Task for method {{{message.method}}}
     * @param loadingView View resource to show while task is running
     * @param progressText Resource id for loading dialog message (TextView id)
     * @param loadingText Resource id for loading dialog message (String id)
     */
    public {{{message.methodUpperCase}}}Task(int loadingView,int progressText, int loadingText) {
    	this(null,loadingView,progressText,loadingText);
    }
	/**
     * Creates Task for method {{{message.method}}}
     * @param activity parent Activity to get Resources from
     * @param loadingView View resource to show while task is running
     * @param progressText Resource id for loading dialog message (TextView id)
     * @param loadingText Resource id for loading dialog message (String id)
     */
	public {{{message.methodUpperCase}}}Task(Activity activity,int loadingView,int progressText, int loadingText) {
		if (activity != null) {
			this.activityReference = new WeakReference<Activity>(activity);		
		} 
		this.loadingView=loadingView;
		this.progressText=progressText;
		this.loadingText=loadingText;
	}
	/**
	 * Executes the async task with request
	 * @param request Server request DTO
	 */
	public void execute({{{message.request.name}}} request) {
		if(loadingView!=-1) {
			Activity activity = (activityReference!=null)? activityReference.get() : null;
			if(activity!=null){
				activity.runOnUiThread(new Thread(new Runnable(){
					public void run() {
						initSpinner();
					}
				}));
			}
		}
		super.execute(request);
	}
	/**
	 * Start spinner for long operations
	 */
	private void initSpinner() {
		Activity activity = (activityReference!=null)? activityReference.get() : null;
		if (activity != null) {
			progressDialog = new Dialog(activity,android.R.style.Theme_Light_NoTitleBar);
			progressDialog.setCancelable(false);
			progressDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
			progressDialog.getWindow().setBackgroundDrawable(new ColorDrawable(0));
			progressDialog.setContentView(loadingView);
			TextView loadingTextView = (TextView)progressDialog.findViewById(progressText);
			if (loadingTextView != null){
				loadingTextView.setText(this.loadingText);
			}
			progressDialog.show();
		}
	}
    /**
     * End spinner
     */
    private void endSpinner() {
    	Activity activity = (activityReference!=null)? activityReference.get() : null;
    	if (activity != null) {
    		if (progressDialog != null) {
    			try {
    				progressDialog.dismiss();
    			} catch(java.lang.IllegalArgumentException ex) {
        			// Ignore: View not attached to window manager
    			}
    		}
    	}
    }
	/**
     * Update async task dialog message
     * @param progress Message to update
     */
	public void updateProgress(final String progress) {
		Activity activity = (activityReference!=null)? activityReference.get() : null;
		if (activity != null) {
			activity.runOnUiThread(new Runnable() {
				public void run() {
					if(progressDialog!=null) {
						TextView loadingTextView = (TextView)progressDialog.findViewById(progressText);
						if(loadingTextView != null){
							loadingTextView.setText({{{message.methodUpperCase}}}Task.this.loadingText);
						}
					}
				}
			});
		}
	}

	@Override
	public final {{{message.response.name}}} doInBackground({{{message.request.name}}}... params) {
		// Get Request DTO
		final {{{message.request.name}}} request=params[0];
		{{{message.response.name}}} response=null;
		try {
			{{#preMethod}}
			{{{preMethod}}}(request);
			{{/preMethod}}
			// Call Service
			final ResponseInfo responseInfo = new ResponseInfo();
			final {{{message.response.name}}} tempResponse = execOnBackground(request,responseInfo);
			response = tempResponse;

			Activity activity = (activityReference!=null)? activityReference.get() : null;
			if(activity!=null && listener!=null && !isCancelled()){
				activity.runOnUiThread(new Runnable(){
					public void run() {
						listener.onServiceResponse(request,tempResponse,responseInfo);
					}
				});
			}
			if(activity==null && listener!=null && !isCancelled()) {
				listener.onServiceResponse(request,tempResponse,responseInfo);
			}
			{{#onTask}}
			if (activity==null) {
				throw new com.mobivery.logic.ServiceException(new Exception("Activity to go back to already removed from memory"));
			} else {
				activity.runOnUiThread(new Runnable(){
					public void run() {
						{{{onTask}}}({{{message.methodUpperCase}}}Task.this,{{{message.methodUpperCase}}}Task.this.activityReference.get());
					}
				});
			}
			{{/onTask}}
		} catch (final com.mobivery.logic.ServiceException ex) {
			if(!isCancelled()) {
				Activity activity = (activityReference!=null)? activityReference.get() : null;
				// If error, call onServiceError on UI Thread
				if (activity!=null && listener!=null) {
					activity.runOnUiThread(new Runnable(){
						public void run(){
							endSpinner();
							listener.onServiceError(request,ex);
						}
					});
				}
				if(activity==null && listener!=null) {
					endSpinner();
					listener.onServiceError(request,ex);
				}
				return null;
			}
		} catch (InterruptedException e) { 
			return null;
		}
		if (loadingView!=-1) {
			Activity activity = (activityReference!=null)? activityReference.get() : null;
			if(activity!=null){
				activity.runOnUiThread(new Runnable(){
					public void run(){
						endSpinner();
					}
				});
			}
		}
		return response;
{'fraction_non_alphanumeric': 0.13616471769854366, 'fraction_numerical': 0.0025826816844823877, 'mean_word_length': 3.733446519524618, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6977376', 'n_tokens_mistral': 4624, 'n_tokens_neox': 3972, 'n_words': 898}