<commit_before>package org.commcare.activities;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;

import org.commcare.CommCareApplication;
import org.commcare.android.database.user.models.FormRecord;
import org.commcare.dalvik.R;
import org.commcare.engine.resource.installers.SingleAppInstallation;
import org.commcare.interfaces.WithUIController;
import org.commcare.models.database.SqlStorage;
import org.commcare.network.DataPullRequester;
import org.commcare.network.LocalReferencePullResponseFactory;
import org.commcare.network.mocks.LocalFilePullResponseFactory;
import org.commcare.preferences.HiddenPreferences;
import org.commcare.preferences.ServerUrls;
import org.commcare.suite.model.OfflineUserRestore;
import org.commcare.tasks.DataPullTask;
import org.commcare.tasks.FormSubmissionProgressBarListener;
import org.commcare.tasks.ProcessAndSendTask;
import org.commcare.tasks.PullTaskResultReceiver;
import org.commcare.tasks.ResultAndError;
import org.commcare.utils.FormUploadResult;
import org.commcare.utils.StorageUtils;
import org.javarosa.core.model.User;
import org.javarosa.core.reference.InvalidReferenceException;
import org.javarosa.core.reference.ReferenceManager;
import org.javarosa.core.services.locale.Localization;

import java.io.File;
import java.io.IOException;

/**
 * Processes and submits forms and syncs data with server
 */
public class FormAndDataSyncer {

    public FormAndDataSyncer() {
    }

    /**
     * @return Were forms sent to the server by this method invocation?
     */
    public boolean checkAndStartUnsentFormsTask(SyncCapableCommCareActivity activity,
                                                final boolean syncAfterwards,
                                                boolean userTriggered) {
        SqlStorage<FormRecord> storage = CommCareApplication.instance().getUserStorage(FormRecord.class);
        FormRecord[] records = StorageUtils.getUnsentRecordsForCurrentApp(storage);

        // We only want to update the last upload sync time when it's a blocking sync
        if (syncAfterwards) {
            HiddenPreferences.updateLastUploadSyncAttemptTime();
        }

        if (records.length > 0) {
            processAndSendForms(activity, records, syncAfterwards, userTriggered);
            return true;
        } else {
            return false;
        }
    }

    @SuppressLint("NewApi")
    private void processAndSendForms(final SyncCapableCommCareActivity activity,
                                    FormRecord[] records,
                                    final boolean syncAfterwards,
                                    final boolean userTriggered) {

        ProcessAndSendTask<SyncCapableCommCareActivity> processAndSendTask =
                new ProcessAndSendTask<SyncCapableCommCareActivity>(activity, getFormPostURL(activity), syncAfterwards) {

                    @Override
                    protected void deliverResult(SyncCapableCommCareActivity receiver, FormUploadResult result) {
                        if (CommCareApplication.instance().isConsumerApp()) {
                            // if this is a consumer app we don't want to show anything in the UI about
                            // sending forms, or do a sync afterward
                            return;
                        }

                        if (result == FormUploadResult.PROGRESS_LOGGED_OUT) {
                            receiver.finish();
                            return;
                        }

                        if (receiver instanceof WithUIController) {
                            ((WithUIController)receiver).getUIController().refreshView();
                        }

                        switch (result) {
                            case FULL_SUCCESS:
                                receiver.handleFormSendResult(getLabelForFormsSent(), true);

                                if (syncAfterwards) {
                                    syncDataForLoggedInUser(receiver, true, userTriggered);
                                }<commit_msg>Private -> Protected<commit_after>package org.commcare.activities;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;

import org.commcare.CommCareApplication;
import org.commcare.android.database.user.models.FormRecord;
import org.commcare.dalvik.R;
import org.commcare.engine.resource.installers.SingleAppInstallation;
import org.commcare.interfaces.WithUIController;
import org.commcare.models.database.SqlStorage;
import org.commcare.network.DataPullRequester;
import org.commcare.network.LocalReferencePullResponseFactory;
import org.commcare.network.mocks.LocalFilePullResponseFactory;
import org.commcare.preferences.HiddenPreferences;
import org.commcare.preferences.ServerUrls;
import org.commcare.suite.model.OfflineUserRestore;
import org.commcare.tasks.DataPullTask;
import org.commcare.tasks.FormSubmissionProgressBarListener;
import org.commcare.tasks.ProcessAndSendTask;
import org.commcare.tasks.PullTaskResultReceiver;
import org.commcare.tasks.ResultAndError;
import org.commcare.utils.FormUploadResult;
import org.commcare.utils.StorageUtils;
import org.javarosa.core.model.User;
import org.javarosa.core.reference.InvalidReferenceException;
import org.javarosa.core.reference.ReferenceManager;
import org.javarosa.core.services.locale.Localization;

import java.io.File;
import java.io.IOException;

/**
 * Processes and submits forms and syncs data with server
 */
public class FormAndDataSyncer {

    public FormAndDataSyncer() {
    }

    /**
     * @return Were forms sent to the server by this method invocation?
     */
    public boolean checkAndStartUnsentFormsTask(SyncCapableCommCareActivity activity,
                                                final boolean syncAfterwards,
                                                boolean userTriggered) {
        SqlStorage<FormRecord> storage = CommCareApplication.instance().getUserStorage(FormRecord.class);
        FormRecord[] records = StorageUtils.getUnsentRecordsForCurrentApp(storage);

        // We only want to update the last upload sync time when it's a blocking sync
        if (syncAfterwards) {
            HiddenPreferences.updateLastUploadSyncAttemptTime();
        }

        if (records.length > 0) {
            processAndSendForms(activity, records, syncAfterwards, userTriggered);
            return true;
        } else {
            return false;
        }
    }

    @SuppressLint("NewApi")
    protected void processAndSendForms(final SyncCapableCommCareActivity activity,
                                    FormRecord[] records,
                                    final boolean syncAfterwards,
                                    final boolean userTriggered) {

        ProcessAndSendTask<SyncCapableCommCareActivity> processAndSendTask =
                new ProcessAndSendTask<SyncCapableCommCareActivity>(activity, getFormPostURL(activity), syncAfterwards) {

                    @Override
                    protected void deliverResult(SyncCapableCommCareActivity receiver, FormUploadResult result) {
                        if (CommCareApplication.instance().isConsumerApp()) {
                            // if this is a consumer app we don't want to show anything in the UI about
                            // sending forms, or do a sync afterward
                            return;
                        }

                        if (result == FormUploadResult.PROGRESS_LOGGED_OUT) {
                            receiver.finish();
                            return;
                        }

                        if (receiver instanceof WithUIController) {
                            ((WithUIController)receiver).getUIController().refreshView();
                        }

                        switch (result) {
                            case FULL_SUCCESS:
                                receiver.handleFormSendResult(getLabelForFormsSent(), true);

                                if (syncAfterwards) {
                                    syncDataForLoggedInUser(receiver, true, userTriggered);
                                }
{'fraction_non_alphanumeric': 0.07029450975639316, 'fraction_numerical': 0.00024239486122894195, 'mean_word_length': 2.205905205905206, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 18, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5212937', 'n_tokens_mistral': 2012, 'n_tokens_neox': 1942, 'n_words': 405}