<commit_before>package se.embargo.core.concurrent;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;

public abstract class ProgressTask<Params, Progress, Result> extends AsyncTask<Params, Progress, Result> {
	private static final String TAG = "ProgressTask";
	private final ProgressDialog _dialog;
	
	public ProgressTask(Context context, int titleId, int messageid) {
        try {
        	_dialog = new ProgressDialog(context);
            _dialog.setTitle(titleId);
            _dialog.setMessage(context.getResources().getString(messageid));
            _dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            _dialog.setIndeterminate(true);
            _dialog.setCancelable(false);
        }
        catch (RuntimeException e) {
        	// This may occur if the task is created from a non-UI thread
        	Log.w(TAG, "Failed to create progress dialog", e);
        	throw e;
        }
	}

    protected void onPreExecute() {
		try {
	   		_dialog.show();
		}
		catch (RuntimeException e) {
			Log.w(TAG, "Failed to show dialog", e);
		}
    }
    
    protected void onPostExecute(Result result) {
    	if (_dialog.isShowing()) {
    		try {
    			_dialog.dismiss();
    		}
    		catch (RuntimeException e) {
    			Log.w(TAG, "Failed to close dialog", e);
    		}
    	}
    }
    
    protected void onCancelled(Result result) {
    	if (_dialog.isShowing()) {
    		try {
    			_dialog.dismiss();
    		}
    		catch (RuntimeException e) {
    			Log.w(TAG, "Failed to close dialog", e);
    		}
    	}
    }
}<commit_msg>Added cancel and progress support to progress task<commit_after>package se.embargo.core.concurrent;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.AsyncTask;
import android.util.Log;

public abstract class ProgressTask<Params, Progress, Result> extends AsyncTask<Params, Progress, Result> implements DialogInterface.OnCancelListener {
	private static final String TAG = "ProgressTask";
	private final ProgressDialog _dialog;
	
	public ProgressTask(Context context, int titleId, int messageid) {
        try {
        	_dialog = new ProgressDialog(context);
            _dialog.setTitle(titleId);
            _dialog.setMessage(context.getResources().getString(messageid));
            _dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            _dialog.setIndeterminate(true);
            _dialog.setCancelable(false);
            _dialog.setOnCancelListener(this);
        }
        catch (RuntimeException e) {
        	// This may occur if the task is created from a non-UI thread
        	Log.w(TAG, "Failed to create progress dialog", e);
        	throw e;
        }
	}
	
	protected Context getContext() {
		return _dialog.getContext();
	}
	
	public void setMaxProgress(int maxprogress) {
        _dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        _dialog.setIndeterminate(false);
        _dialog.setMax(maxprogress);
	}
	
	public void setCancelable(boolean cancelable) {
		_dialog.setCancelable(cancelable);
	}

	/**
	 * Update the current progress.
	 * @remark	Must be called on the UI thread.
	 * @param 	progress	Progress between 0 and maxprogress
	 */
    protected void setProgress(int progress) {
    	_dialog.setProgress(progress);
    }

    protected void onPreExecute() {
		try {
	   		_dialog.show();
		}
		catch (RuntimeException e) {
			Log.w(TAG, "Failed to show dialog", e);
		}
    }
    
    protected void onPostExecute(Result result) {
    	if (_dialog.isShowing()) {
    		try {
    			_dialog.dismiss();
    		}
    		catch (RuntimeException e) {
    			Log.w(TAG, "Failed to close dialog", e);
    		}
    	}
    }
    
    protected void onCancelled(Result result) {
    	Log.i(TAG, "Task was cancelled: " + this);

    	if (_dialog.isShowing()) {
    		try {
    			_dialog.dismiss();
    		}
    		catch (RuntimeException e) {
    			Log.w(TAG, "Failed to close dialog", e);
    		}
    	}
    }
    
    @Override
    public void onCancel(DialogInterface dialog) {
    	cancel(true);
    }
}
{'fraction_non_alphanumeric': 0.10028929604628736, 'fraction_numerical': 0.00024108003857280618, 'mean_word_length': 2.6651943462897525, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '6881624', 'n_tokens_mistral': 1302, 'n_tokens_neox': 1246, 'n_words': 300}