com.madgag.agit.MessagingProgressMonitor.java Source code

Java tutorial

Introduction

Here is the source code for com.madgag.agit.MessagingProgressMonitor.java

Source

/*
 * Copyright (c) 2011 Roberto Tyley
 *
 * This file is part of 'Agit' - an Android Git client.
 *
 * Agit is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Agit is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.madgag.agit;

import com.google.inject.Inject;
import org.eclipse.jgit.lib.ProgressMonitor;

import android.util.Log;

public class MessagingProgressMonitor implements ProgressMonitor, CancellationSignaller {

    public static final String TAG = "MessagingProgressMonitor";

    private boolean output;

    private long taskBeganAt;

    private String msg;

    private int lastWorked;

    private int totalWork;

    private boolean cancelled = false;

    public Progress currentProgress;

    private final ProgressListener<Progress> progressListener;

    public Progress getCurrentProgress() {
        return currentProgress;
    }

    @Inject
    public MessagingProgressMonitor(ProgressListener<Progress> progressListener) {
        this.progressListener = progressListener;
    }

    public void setCancelled() {
        cancelled = true;
    }

    public boolean isCancelled() {
        return cancelled;
    }

    public void beginTask(final String title, final int total) {
        Log.d(TAG, "started " + title + " total=" + total);

        endTask();
        msg = title;
        lastWorked = 0;
        totalWork = total;
    }

    public void endTask() {
        if (output) {
            if (totalWork != UNKNOWN)
                display(totalWork);
            System.err.println();
        }
        output = false;
        msg = null;
    }

    public void start(int arg0) {
    }

    public void update(int completed) {
        final int cmp = lastWorked + completed;
        //Log.d(TAG, "cmp "+cmp);

        if (totalWork == UNKNOWN) {
            display(cmp);
        } else {
            if ((cmp * 10 / totalWork) != (lastWorked * 10) / totalWork) {
                display(cmp);
            }
        }
        lastWorked = cmp;
        output = true;
    }

    private void display(int cmp) {
        currentProgress = new Progress(msg, totalWork, cmp);
        progressListener.publish(currentProgress);
    }

}