FileDocCategorySizeDatePackage
ProgressCallbackEntity.javaAPI DocAndroid 1.5 API2807Wed May 06 22:42:46 BST 2009com.android.mms.transaction

ProgressCallbackEntity

public class ProgressCallbackEntity extends ByteArrayEntity

Fields Summary
private static final int
DEFAULT_PIECE_SIZE
public static final String
PROGRESS_STATUS_ACTION
public static final int
PROGRESS_START
public static final int
PROGRESS_ABORT
public static final int
PROGRESS_COMPLETE
private final android.content.Context
mContext
private final byte[]
mContent
private final long
mToken
Constructors Summary
public ProgressCallbackEntity(android.content.Context context, long token, byte[] b)


           
        super(b);

        mContext = context;
        mContent = b;
        mToken = token;
    
Methods Summary
private voidbroadcastProgressIfNeeded(int progress)

        if (mToken > 0) {
            Intent intent = new Intent(PROGRESS_STATUS_ACTION);
            intent.putExtra("progress", progress);
            intent.putExtra("token", mToken);
            mContext.sendBroadcast(intent);
        }
    
public voidwriteTo(java.io.OutputStream outstream)

        if (outstream == null) {
            throw new IllegalArgumentException("Output stream may not be null");
        }

        boolean completed = false;
        try {
            broadcastProgressIfNeeded(PROGRESS_START);

            int pos = 0, totalLen = mContent.length;
            while (pos < totalLen) {
                int len = totalLen - pos;
                if (len > DEFAULT_PIECE_SIZE) {
                    len = DEFAULT_PIECE_SIZE;
                }
                outstream.write(mContent, pos, len);
                outstream.flush();

                pos += len;

                broadcastProgressIfNeeded(100 * pos / totalLen);
            }

            broadcastProgressIfNeeded(PROGRESS_COMPLETE);
            completed = true;
        } finally {
            if (!completed) {
                broadcastProgressIfNeeded(PROGRESS_ABORT);
            }
        }