package no.nordicsemi.android.dfu;

import android.os.SystemClock;

/* JADX INFO: loaded from: classes2.dex */
public class DfuProgressInfo {
    private int bytesReceived;
    private int bytesSent;
    private int currentPart;
    private int imageSizeInBytes;
    private int initialBytesSent;
    private int lastBytesSent;
    private long lastProgressTime;
    private final ProgressListener mListener;
    private int maxObjectSizeInBytes;
    private int progress;
    private long timeStart;
    private int totalParts;

    public interface ProgressListener {
        void updateProgressNotification();
    }

    public DfuProgressInfo(ProgressListener progressListener) {
        this.mListener = progressListener;
    }

    public void addBytesSent(int i2) {
        setBytesSent(this.bytesSent + i2);
    }

    public int getAvailableObjectSizeIsBytes() {
        int i2 = this.imageSizeInBytes;
        int i3 = this.bytesSent;
        int i4 = this.maxObjectSizeInBytes;
        return Math.min(i2 - i3, i4 - (i3 % i4));
    }

    public float getAverageSpeed() {
        if (SystemClock.elapsedRealtime() - this.timeStart != 0) {
            return (this.bytesSent - this.initialBytesSent) / (r0 - r2);
        }
        return 0.0f;
    }

    public int getBytesReceived() {
        return this.bytesReceived;
    }

    public int getBytesSent() {
        return this.bytesSent;
    }

    public int getCurrentPart() {
        return this.currentPart;
    }

    public int getImageSizeInBytes() {
        return this.imageSizeInBytes;
    }

    public int getProgress() {
        return this.progress;
    }

    public float getSpeed() {
        long jElapsedRealtime = SystemClock.elapsedRealtime();
        float f2 = jElapsedRealtime - this.timeStart != 0 ? (this.bytesSent - this.lastBytesSent) / (jElapsedRealtime - this.lastProgressTime) : 0.0f;
        this.lastProgressTime = jElapsedRealtime;
        this.lastBytesSent = this.bytesSent;
        return f2;
    }

    public int getTotalParts() {
        return this.totalParts;
    }

    public void init(int i2, int i3, int i4) {
        this.imageSizeInBytes = i2;
        this.maxObjectSizeInBytes = Integer.MAX_VALUE;
        this.currentPart = i3;
        this.totalParts = i4;
    }

    public boolean isComplete() {
        return this.bytesSent == this.imageSizeInBytes;
    }

    public boolean isLastPart() {
        return this.currentPart == this.totalParts;
    }

    public boolean isObjectComplete() {
        return this.bytesSent % this.maxObjectSizeInBytes == 0;
    }

    public void setBytesReceived(int i2) {
        this.bytesReceived = i2;
    }

    public void setBytesSent(int i2) {
        if (this.timeStart == 0) {
            this.timeStart = SystemClock.elapsedRealtime();
            this.initialBytesSent = i2;
        }
        this.bytesSent = i2;
        this.progress = (int) ((i2 * 100.0f) / this.imageSizeInBytes);
        this.mListener.updateProgressNotification();
    }

    public void setMaxObjectSizeInBytes(int i2) {
        this.maxObjectSizeInBytes = i2;
    }

    public void setProgress(int i2) {
        this.progress = i2;
        this.mListener.updateProgressNotification();
    }

    public DfuProgressInfo setTotalPart(int i2) {
        this.totalParts = i2;
        return this;
    }
}
