<commit_before>package org.commcare.tasks;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.os.Build;
import android.view.View;
import android.view.animation.DecelerateInterpolator;
import android.widget.ProgressBar;

import org.commcare.activities.SyncCapableCommCareActivity;
import org.commcare.dalvik.R;

/**
 * Created by amstone326 on 11/16/16.
 */
public class FormSubmissionProgressBarListener implements DataSubmissionListener {

    private static final long MIN_PROGRESS_BAR_DURATION_MS = 2000;

    private long sizeOfCurrentItem;
    private long startTime;
    private ProgressBar submissionProgressBar;
    private SyncCapableCommCareActivity containingActivity;

    public FormSubmissionProgressBarListener(SyncCapableCommCareActivity activityContainingProgressBar) {
        this.containingActivity = activityContainingProgressBar;
    }

    @Override
    public void beginSubmissionProcess(int totalItems) {
        startTime = System.currentTimeMillis();
        containingActivity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                submissionProgressBar =
                        (ProgressBar)containingActivity.findViewById(R.id.submission_progress_bar);
                submissionProgressBar.setVisibility(View.VISIBLE);
            }
        });
    }

    @Override
    public void startSubmission(int itemNumber, long sizeOfItem) {
        sizeOfCurrentItem = sizeOfItem;
        containingActivity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                submissionProgressBar.setMax(100);
                submissionProgressBar.setProgress(0);
            }
        });
    }

    @Override
    public void notifyProgress(int itemNumber, final long progress) {
        containingActivity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                submissionProgressBar.setProgress(getProgressToReport(progress));
            }
        });
    }

    @Override
    public void endSubmissionProcess(final boolean success) {
        containingActivity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (success && submissionProgressBar.getProgress() != 100) {
                    finishAnimatingProgressBar();
                } else {
                    submissionProgressBar.setVisibility(View.GONE);
                }
            }
        });
    }

    private int getProgressToReport(long progressForCurrentItem) {
        final int actualProgressPercent =
                (int)Math.floor((progressForCurrentItem * 1.0 / sizeOfCurrentItem) * 100);

        long timeElapsed = System.currentTimeMillis() - startTime;
        final int maxAllowedProgressByTime =
                (int)Math.floor((timeElapsed * 1.0 / MIN_PROGRESS_BAR_DURATION_MS) * 100);

        return Math.min(actualProgressPercent, maxAllowedProgressByTime);
    }

    private void finishAnimatingProgressBar() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            ObjectAnimator animation = ObjectAnimator.ofInt(submissionProgressBar, "progress",
                    submissionProgressBar.getProgress(), 100);
            long timeRemaining =
                    MIN_PROGRESS_BAR_DURATION_MS - (System.currentTimeMillis() - startTime);
            if (timeRemaining < 0) {
                timeRemaining = 500;
            }
            animation.setDuration(timeRemaining);
            animation.setInterpolator(new DecelerateInterpolator());
            animation.addListener(new Animator.AnimatorListener() {
                @Override
                public void onAnimationStart(Animator animation) {

                }

                @Override
                public void onAnimationEnd(Animator animation) {
                    containingActivity.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            submissionProgressBar.setVisibility(View.GONE);
                        }
                    });
                }

                @Override
                public void onAnimationCancel(Animator animation) {

                }

                @Override
                public void onAnimationRepeat(Animator animation) {

                }
            });
            animation.start();
        } else {
            submissionProgressBar.setProgress(100);
            submissionProgressBar.setVisibility(View.GONE);
        }
    }

}
<commit_msg>Don't allow progress to decrease when submission tries again (to preserve smoothness), and make multiple submissions in one go use the same progress bar, divided up evenly<commit_after>package org.commcare.tasks;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.os.Build;
import android.view.View;
import android.view.animation.DecelerateInterpolator;
import android.widget.ProgressBar;

import org.commcare.activities.SyncCapableCommCareActivity;
import org.commcare.dalvik.R;

/**
 * Created by amstone326 on 11/16/16.
 */
public class FormSubmissionProgressBarListener implements DataSubmissionListener {

    private static final long MIN_PROGRESS_BAR_DURATION_PER_ITEM = 1000;
    private static final long MAX_TOTAL_PROGRESS_BAR_DURATION = 5000;

    private int totalItems;
    private int maxProgress;
    private long sizeOfCurrentItem;
    private long startTime;
    private ProgressBar submissionProgressBar;
    private SyncCapableCommCareActivity containingActivity;

    public FormSubmissionProgressBarListener(SyncCapableCommCareActivity activityContainingProgressBar) {
        this.containingActivity = activityContainingProgressBar;
    }

    @Override
    public void beginSubmissionProcess(int totalItems) {
        this.totalItems = totalItems;
        // Give each item 100 units of progress to use
        this.maxProgress = totalItems * 100;
        this.startTime = System.currentTimeMillis();
        this.containingActivity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                submissionProgressBar =
                        (ProgressBar)containingActivity.findViewById(R.id.submission_progress_bar);
                submissionProgressBar.setVisibility(View.VISIBLE);
                submissionProgressBar.setMax(maxProgress);
                submissionProgressBar.setProgress(0);
            }
        });
    }


    @Override
    public void startSubmission(int itemNumber, long sizeOfItem) {
        sizeOfCurrentItem = sizeOfItem;
    }

    @Override
    public void notifyProgress(final int itemNumber, final long progress) {
        containingActivity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                int nextProgress = getProgressToReport(itemNumber, progress);
                if (nextProgress > submissionProgressBar.getProgress()) {
                    submissionProgressBar.setProgress(nextProgress);
                }
            }
        });
    }

    private int getProgressToReport(int itemNumber, long progressForCurrentItem) {
        int progressPercentForPriorItems = 100 * itemNumber;
        int progressPercentForCurrentItem =
                (int)Math.floor((progressForCurrentItem * 1.0 / sizeOfCurrentItem) * 100);
        int actualProgressPercent = progressPercentForPriorItems + progressPercentForCurrentItem;

        long timeElapsed = System.currentTimeMillis() - startTime;
        final int maxAllowedProgressByTime =
                (int)Math.floor((timeElapsed * 1.0 / getIdealDuration()) * maxProgress);

        return Math.min(actualProgressPercent, maxAllowedProgressByTime);
    }

    @Override
    public void endSubmissionProcess(final boolean success) {
        containingActivity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (success && submissionProgressBar.getProgress() < maxProgress) {
                    finishAnimatingProgressBar();
                } else {
                    submissionProgressBar.setVisibility(View.GONE);
                }
            }
        });
    }

    private void finishAnimatingProgressBar() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            ObjectAnimator animation = ObjectAnimator.ofInt(submissionProgressBar, "progress",
                    submissionProgressBar.getProgress(), maxProgress);
            animation.setDuration(getFinishAnimationDuration());
            animation.setInterpolator(new DecelerateInterpolator());
            animation.addListener(new Animator.AnimatorListener() {
                @Override
                public void onAnimationStart(Animator animation) {
                }

                @Override
                public void onAnimationEnd(Animator animation) {
                    containingActivity.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            submissionProgressBar.setVisibility(View.GONE);
                        }
                    });
                }

                @Override
                public void onAnimationCancel(Animator animation) {
                }

                @Override
                public void onAnimationRepeat(Animator animation) {
                }
            });
            animation.start();
        } else {
            submissionProgressBar.setProgress(maxProgress);
            submissionProgressBar.setVisibility(View.GONE);
        }
    }

    private int getFinishAnimationDuration() {
        int progressRemaining = maxProgress - submissionProgressBar.getProgress();
        double proportionRemaining = progressRemaining * 1.0 / maxProgress;
        return (int)Math.floor(getIdealDuration() * proportionRemaining);
    }

    private long getIdealDuration() {
        return Math.min(MIN_PROGRESS_BAR_DURATION_PER_ITEM * totalItems, MAX_TOTAL_PROGRESS_BAR_DURATION);
    }

}

{'fraction_non_alphanumeric': 0.07233201581027668, 'fraction_numerical': 0.007509881422924901, 'mean_word_length': 2.2314814814814814, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, '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': '6720225', 'n_tokens_mistral': 2584, 'n_tokens_neox': 2409, 'n_words': 477}