<commit_before>package org.commcare.android.tasks.templates;

import android.os.AsyncTask;

import java.util.ArrayList;

/**
 * AsyncTask that is registered, enabling the task to be cancelled if the
 * session ends. Useful since the session ending might close resources that the
 * task depends on to proceed.
 *
 * @author <NAME> (<EMAIL>)
 */

public abstract class ManagedAsyncTask<A, B, C> extends AsyncTask<A, B, C> {

    /**
     * List of running/to-be-run tasks. Tasks remove themselves automatically
     * upon completion.
     */
    private static final ArrayList<ManagedAsyncTask<?, ?, ?>> livingTasks =
            new ArrayList<ManagedAsyncTask<?, ?, ?>>();

    /**
     * Create task and add it to list of managed tasks.
     */
    public ManagedAsyncTask() {
        synchronized(livingTasks) {
            livingTasks.add(this);
        }
    }

    /**
     * Call cancel on all tasks and then wipe the living task list.
     */
    public static void cancelTasks() {
        synchronized(livingTasks) {
            for (AsyncTask task : livingTasks) {
                task.cancel(true);
            }
            livingTasks.clear();
        }
    }

    /**
     * On execution completion remove task from managed task list.
     */
    @Override
    protected void onPostExecute(C result) {
        super.onPostExecute(result);

        synchronized(livingTasks) {
            livingTasks.remove(this);
        }
    }

    /**<commit_msg>Move task registration from contsr to onPreExecute<commit_after>package org.commcare.android.tasks.templates;

import android.os.AsyncTask;

import java.util.ArrayList;

/**
 * AsyncTask that is registered, enabling the task to be cancelled if the
 * session ends. Useful since the session ending might close resources that the
 * task depends on to proceed.
 *
 * @author <NAME> (<EMAIL>)
 */

public abstract class ManagedAsyncTask<A, B, C> extends AsyncTask<A, B, C> {

    /**
     * List of running tasks. Tasks add/remove themselves automatically upon
     * start, cancellation, and completion.
     */
    private static final ArrayList<ManagedAsyncTask<?, ?, ?>> livingTasks =
            new ArrayList<ManagedAsyncTask<?, ?, ?>>();

    /**
     * Call cancel on all tasks and then wipe the living task list.
     */
    public static void cancelTasks() {
        synchronized(livingTasks) {
            for (AsyncTask task : livingTasks) {
                task.cancel(true);
            }
            livingTasks.clear();
        }
    }

    /**
     * Before executing add the task to list of managed tasks.
     */
    @Override
    protected void onPreExecute() {
        synchronized(livingTasks) {
            livingTasks.add(this);
        }
    }

    /**
     * On execution completion remove task from managed task list.
     */
    @Override
    protected void onPostExecute(C result) {
        super.onPostExecute(result);

        synchronized(livingTasks) {
            livingTasks.remove(this);
        }
    }

    /**
{'fraction_non_alphanumeric': 0.10069790628115653, 'fraction_numerical': 0.0, 'mean_word_length': 2.2860262008733625, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, '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': '1218635', 'n_tokens_mistral': 814, 'n_tokens_neox': 800, 'n_words': 249}