/* This program 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. This program 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 . */ /* Copyright 2010 Joe Robinson */ package com.lc8n.blauploader; import java.io.IOException; import java.io.InputStream; import android.os.Bundle; import android.os.Handler; import android.os.Message; public class ProgressInputStream extends InputStream { /* Key to retrieve progress value from message bundle passed to handler */ public static final String PROGRESS_UPDATE = "progress_update"; private static final int TEN_KILOBYTES = 1024 * 10; private InputStream inputStream; private Handler handler; private long progress; private long lastUpdate; private boolean closed; public ProgressInputStream(InputStream inputStream, Handler handler) { this.inputStream = inputStream; this.handler = handler; this.progress = 0; this.lastUpdate = 0; this.closed = false; } @Override public int read() throws IOException { int count = inputStream.read(); return incrementCounterAndUpdateDisplay(count); } @Override public int read(byte[] b, int off, int len) throws IOException { int count = inputStream.read(b, off, len); return incrementCounterAndUpdateDisplay(count); } @Override public void close() throws IOException { super.close(); if (closed) throw new IOException("already closed"); closed = true; } private int incrementCounterAndUpdateDisplay(int count) { if (count > 0) progress += count; lastUpdate = maybeUpdateDisplay(progress, lastUpdate); return count; } private long maybeUpdateDisplay(long progress, long lastUpdate) { if (progress - lastUpdate > TEN_KILOBYTES) { lastUpdate = progress; sendLong(PROGRESS_UPDATE, progress); } return lastUpdate; } public void sendLong(String key, long value) { Bundle data = new Bundle(); data.putLong(key, value); Message message = Message.obtain(); message.setData(data); handler.sendMessage(message); } }