Example usage for com.amazonaws.services.s3.model ProgressListener ProgressListener

List of usage examples for com.amazonaws.services.s3.model ProgressListener ProgressListener

Introduction

In this page you can find the example usage for com.amazonaws.services.s3.model ProgressListener ProgressListener.

Prototype

ProgressListener

Source Link

Usage

From source file:br.org.fiscal65.amazonaws.AWSPictureUpload.java

License:Open Source License

public AWSPictureUpload(Context context, OnPictureUploadS3PostExecuteListener uploadListener,
        String picturePath, String slugFiscalizacao, String zonaFiscalizacao, Long idFiscalizacao,
        Integer posicaoFoto, Integer podeEnviarRedeDados, Integer sleep) {
    super(context);

    this.uploadListener = uploadListener;

    this.picturePath = picturePath;

    this.slugFiscalizacao = slugFiscalizacao;

    this.zonaFiscalizacao = zonaFiscalizacao;

    this.idFiscalizacao = idFiscalizacao;

    this.posicaoFoto = posicaoFoto;

    this.podeEnviarRedeDados = podeEnviarRedeDados;

    this.sleep = sleep;

    mStatus = Status.IN_PROGRESS;/*from   ww  w .  j  a v a  2  s.  c o m*/

    progressListener = new ProgressListener() {
        @Override
        public void progressChanged(ProgressEvent event) {
            if (event.getEventCode() == ProgressEvent.COMPLETED_EVENT_CODE) {
                boolean hasInternet = CommunicationUtils.verifyConnectivity(getContext());

                if (hasInternet) {
                    boolean isOnWiFi = CommunicationUtils.isWifi(getContext());

                    if (isOnWiFi || (AWSPictureUpload.this.podeEnviarRedeDados != null
                            && AWSPictureUpload.this.podeEnviarRedeDados.equals(1))) {
                        try {
                            ResponseHeaderOverrides override = new ResponseHeaderOverrides();
                            override.setContentType("image/jpeg");

                            GeneratePresignedUrlRequest urlRequest = new GeneratePresignedUrlRequest(
                                    CommunicationConstants.PICTURE_BUCKET_NAME,
                                    AWSUtil.getPrefix(getContext()) + AWSPictureUpload.this.slugFiscalizacao
                                            + "/zona-" + AWSPictureUpload.this.zonaFiscalizacao + "/"
                                            + pictureName);
                            urlRequest.setResponseHeaders(override);

                            URL urlDaFoto = AWSUtil.getS3Client(getContext()).generatePresignedUrl(urlRequest);

                            if (urlDaFoto != null) {
                                String string = urlDaFoto.toString();
                                String[] parts = string.split("\\?");
                                String url = parts[0];

                                S3UploadPictureResult result = new S3UploadPictureResult();
                                result.setUrlDaFoto(url);
                                result.setPosicaoFoto(AWSPictureUpload.this.posicaoFoto);
                                result.setIdFiscalizacao(AWSPictureUpload.this.idFiscalizacao);
                                result.setSlugFiscalizacao(AWSPictureUpload.this.slugFiscalizacao);
                                result.setZonaFiscalizacao(AWSPictureUpload.this.zonaFiscalizacao);

                                mStatus = Status.COMPLETED;
                                AWSPictureUpload.this.uploadListener
                                        .finishedPictureUploadS3ComResultado(result);
                            } else {
                                mStatus = Status.CANCELED;

                                if (mUpload != null) {
                                    mUpload.abort();
                                }

                                AWSPictureUpload.this.uploadListener.finishedPictureUploadS3ComError(
                                        AWSPictureUpload.this.slugFiscalizacao,
                                        AWSPictureUpload.this.zonaFiscalizacao,
                                        AWSPictureUpload.this.idFiscalizacao,
                                        AWSPictureUpload.this.posicaoFoto);
                            }
                        } catch (Exception e) {
                            mStatus = Status.CANCELED;

                            if (mUpload != null) {
                                mUpload.abort();
                            }

                            AWSPictureUpload.this.uploadListener.finishedPictureUploadS3ComError(
                                    AWSPictureUpload.this.slugFiscalizacao,
                                    AWSPictureUpload.this.zonaFiscalizacao,
                                    AWSPictureUpload.this.idFiscalizacao, AWSPictureUpload.this.posicaoFoto);
                        }
                    } else {
                        mStatus = Status.CANCELED;

                        if (mUpload != null) {
                            mUpload.abort();
                        }

                        AWSPictureUpload.this.uploadListener.finishedPictureUploadS3ComError(
                                AWSPictureUpload.this.slugFiscalizacao, AWSPictureUpload.this.zonaFiscalizacao,
                                AWSPictureUpload.this.idFiscalizacao, AWSPictureUpload.this.posicaoFoto);
                    }
                } else {
                    mStatus = Status.CANCELED;

                    if (mUpload != null) {
                        mUpload.abort();
                    }

                    AWSPictureUpload.this.uploadListener.finishedPictureUploadS3ComError(
                            AWSPictureUpload.this.slugFiscalizacao, AWSPictureUpload.this.zonaFiscalizacao,
                            AWSPictureUpload.this.idFiscalizacao, AWSPictureUpload.this.posicaoFoto);
                }
            } else if (event.getEventCode() == ProgressEvent.FAILED_EVENT_CODE) {
                mStatus = Status.CANCELED;

                if (mUpload != null) {
                    mUpload.abort();
                }

                AWSPictureUpload.this.uploadListener.finishedPictureUploadS3ComError(
                        AWSPictureUpload.this.slugFiscalizacao, AWSPictureUpload.this.zonaFiscalizacao,
                        AWSPictureUpload.this.idFiscalizacao, AWSPictureUpload.this.posicaoFoto);
            }
        }
    };
}

From source file:com.android.demo.notepad3.DownloadModel.java

License:Open Source License

public DownloadModel(Context context, String key, TransferManager manager) {
    super(context, Uri.parse(key), manager);
    mKey = key;/*from   w w w. j  a  v  a  2  s.  co m*/
    mStatus = Status.IN_PROGRESS;
    mListener = new ProgressListener() {
        @Override
        public void progressChanged(ProgressEvent event) {
            if (event.getEventCode() == ProgressEvent.COMPLETED_EVENT_CODE) {

                Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
                mediaScanIntent.setData(mUri);
                getContext().sendBroadcast(mediaScanIntent);

                mStatus = Status.COMPLETED;

            }
        }
    };
}

From source file:com.android.demo.notepad3.UploadModel.java

License:Open Source License

public UploadModel(Context context, Uri uri, TransferManager manager) {
    super(context, uri, manager);
    mStatus = Status.IN_PROGRESS;//from   www  . j ava2  s.  c  o m
    mExtension = MimeTypeMap.getSingleton().getExtensionFromMimeType(context.getContentResolver().getType(uri));
    ;
    mListener = new ProgressListener() {
        @Override
        public void progressChanged(ProgressEvent event) {
            if (event.getEventCode() == ProgressEvent.COMPLETED_EVENT_CODE) {
                mStatus = Status.COMPLETED;
                if (mFile != null) {
                    mFile.delete();
                }
            }
        }
    };
}

From source file:com.dss.sframework.tools.amazon.models.UploadModel.java

License:Open Source License

public UploadModel(Context context, TransferManager manager, Enums.MEDIA_TYPE mediaType, File file,
        String extension, final String callerFragmentTag) {
    //        super(context, uri, manager);
    super(context, manager, file);

    try {/*  w ww.j av a2  s  .co  m*/
        mCallbacks = (UploadFileCallbacks) context;
    } catch (ClassCastException e) {
        throw new ClassCastException(context.toString() + " must implement UploadFileCallbacks");
    }

    mStatus = Status.IN_PROGRESS;
    mExtension = extension;
    mFile = file;
    mMediaType = mediaType;
    //        mExtension = MimeTypeMap.getSingleton().getExtensionFromMimeType(
    //                context.getContentResolver().getType(uri));
    mCallerFragmentTag = callerFragmentTag;

    mListener = new ProgressListener() {
        @Override
        public void progressChanged(ProgressEvent event) {
            if (event.getEventCode() == ProgressEvent.COMPLETED_EVENT_CODE) {
                mStatus = Status.COMPLETED;
                if (mFile != null) {
                    mFile.delete();
                }

                mCallbacks.onSuccessListenerUploadFile(mMediaType, mediaURL, mCallerFragmentTag);
            }
        }
    };
}

From source file:com.example.jinux.mydemo.s3.Uploader.java

License:Apache License

/**
 * Initiate a multipart file upload to Amazon S3
 * /*from   w w  w.j a  va 2s.  com*/
 * @return the URL of a successfully uploaded file
 */
public String start() {

    // initialize
    List<PartETag> partETags = new ArrayList<PartETag>();
    final long contentLength = file.length();
    long filePosition = 0;
    int startPartNumber = 1;

    userInterrupted = false;
    userAborted = false;
    bytesUploaded = 0;

    // check if we can resume an incomplete download
    String uploadId = getCachedUploadId();
    Utils.log("start uploading");
    if (uploadId != null) {
        // we can resume the download
        Log.i(TAG, "resuming upload for " + uploadId);

        // get the cached etags
        List<PartETag> cachedEtags = getCachedPartEtags();
        partETags.addAll(cachedEtags);

        // calculate the start position for resume
        startPartNumber = cachedEtags.size() + 1;
        filePosition = (startPartNumber - 1) * partSize;
        bytesUploaded = filePosition;

        Log.i(TAG, "resuming at part " + startPartNumber + " position " + filePosition);

    } else {
        // initiate a new multi part upload
        Log.i(TAG, "initiating new upload");

        Utils.log("the bucket = " + s3bucketName);
        InitiateMultipartUploadRequest initRequest = new InitiateMultipartUploadRequest(s3bucketName, s3key);
        configureInitiateRequest(initRequest);
        initRequest.getRequestClientOptions()
                .appendUserAgent("TransferService_multipart/" + VersionInfoUtils.getVersion());
        InitiateMultipartUploadResult initResponse = s3Client.initiateMultipartUpload(initRequest);
        uploadId = initResponse.getUploadId();

    }

    final AbortMultipartUploadRequest abortRequest = new AbortMultipartUploadRequest(s3bucketName, s3key,
            uploadId);

    for (int k = startPartNumber; filePosition < contentLength; k++) {

        long thisPartSize = Math.min(partSize, (contentLength - filePosition));

        Log.i(TAG, "starting file part " + k + " with size " + thisPartSize);

        UploadPartRequest uploadRequest = new UploadPartRequest().withBucketName(s3bucketName).withKey(s3key)
                .withUploadId(uploadId).withPartNumber(k).withFileOffset(filePosition).withFile(file)
                .withPartSize(thisPartSize);

        ProgressListener s3progressListener = new ProgressListener() {
            public void progressChanged(ProgressEvent progressEvent) {

                // bail out if user cancelled
                // TODO calling shutdown too brute force?
                if (userInterrupted) {
                    s3Client.shutdown();
                    throw new UploadIterruptedException("User interrupted");
                } else if (userAborted) {
                    // aborted requests cannot be resumed, so clear any cached etags
                    clearProgressCache();
                    s3Client.abortMultipartUpload(abortRequest);
                    s3Client.shutdown();
                }

                bytesUploaded += progressEvent.getBytesTransfered();

                //Log.d(TAG, "bytesUploaded=" + bytesUploaded);

                // broadcast progress
                float fpercent = ((bytesUploaded * 100) / contentLength);
                int percent = Math.round(fpercent);
                if (progressListener != null) {
                    progressListener.progressChanged(progressEvent, bytesUploaded, percent);
                }

            }
        };

        uploadRequest.setProgressListener(s3progressListener);

        UploadPartResult result = s3Client.uploadPart(uploadRequest);

        partETags.add(result.getPartETag());

        // cache the part progress for this upload
        if (k == 1) {
            initProgressCache(uploadId);
        }
        // store part etag
        cachePartEtag(result);

        filePosition += thisPartSize;
    }

    CompleteMultipartUploadRequest compRequest = new CompleteMultipartUploadRequest(s3bucketName, s3key,
            uploadId, partETags);

    CompleteMultipartUploadResult result = s3Client.completeMultipartUpload(compRequest);
    bytesUploaded = 0;

    Log.i(TAG, "upload complete for " + uploadId);

    clearProgressCache();

    return result.getLocation();

}

From source file:com.example.ytlin.gpsandupload.models.DownloadModel.java

License:Open Source License

public DownloadModel(Context context, String key, TransferManager manager) {
    super(context, Uri.parse(key), manager);
    mKey = key;/*from  w w  w.  j av a  2 s  .  c  o m*/
    mStatus = Status.IN_PROGRESS;
    mListener = new ProgressListener() {
        @Override
        public void progressChanged(ProgressEvent event) {
            if (event.getEventCode() == ProgressEvent.COMPLETED_EVENT_CODE) {

                Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);

                mediaScanIntent.setData(mUri);
                getContext().sendBroadcast(mediaScanIntent);

                mStatus = Status.COMPLETED;
            }

        }

    };
}

From source file:com.fylm.aws.models.UploadModel.java

License:Open Source License

public UploadModel(Context context, Uri uri, TransferManager manager, Double lat, Double log) {
    super(context, uri, manager);
    mStatus = Status.IN_PROGRESS;/*from   w  w  w  .j av  a  2s  .co  m*/
    Log.i("Inside upload model definition", "Lat: " + lat);
    latitude = lat;
    longitude = log;
    mExtension = MimeTypeMap.getSingleton().getExtensionFromMimeType(context.getContentResolver().getType(uri));
    ;
    mListener = new ProgressListener() {
        @Override
        public void progressChanged(ProgressEvent event) {
            if (event.getEventCode() == ProgressEvent.COMPLETED_EVENT_CODE) {
                mStatus = Status.COMPLETED;
                if (mFile != null) {
                    mFile.delete();
                }
            }
        }
    };
}

From source file:com.proofpoint.event.collector.combiner.S3StorageSystem.java

License:Apache License

@Override
public StoredObject putObject(final URI location, File source) {
    try {//from w ww.j  av  a2 s. c  o m
        log.info("starting upload: %s", location);
        final AtomicLong totalTransferred = new AtomicLong();
        Upload upload = s3TransferManager.upload(getS3Bucket(location), getS3ObjectKey(location), source);
        upload.addProgressListener(new ProgressListener() {
            @Override
            public void progressChanged(ProgressEvent progressEvent) {
                // NOTE: This may be invoked by multiple threads.
                long transferred = totalTransferred.addAndGet(progressEvent.getBytesTransferred());
                log.debug("upload progress: %s: transferred=%d code=%d", location, transferred,
                        progressEvent.getEventCode());
            }
        });
        UploadResult uploadResult = upload.waitForUploadResult();
        ObjectMetadata metadata = s3Service.getObjectMetadata(getS3Bucket(location), getS3ObjectKey(location));
        if (!uploadResult.getETag().equals(metadata.getETag())) {
            // this might happen in rare cases due to S3's eventual consistency
            throw new IllegalStateException("uploaded etag is different from retrieved object etag");
        }
        log.info("completed upload: %s (size=%d bytes)", location, totalTransferred.get());
        return updateStoredObject(location, metadata);
    } catch (Exception e) {
        throw Throwables.propagate(e);
    }
}

From source file:com.readystatesoftware.simpl3r.Uploader.java

License:Apache License

/**
 * Initiate a multipart file upload to Amazon S3
 * // w  w  w  .  jav a2  s.co m
 * @return the URL of a successfully uploaded file
 */
public String start() {

    // initialize
    List<PartETag> partETags = new ArrayList<PartETag>();
    final long contentLength = file.length();
    long filePosition = 0;
    int startPartNumber = 1;

    userInterrupted = false;
    userAborted = false;
    bytesUploaded = 0;

    // check if we can resume an incomplete download
    String uploadId = getCachedUploadId();

    if (uploadId != null) {
        // we can resume the download
        Log.i(TAG, "resuming upload for " + uploadId);

        // get the cached etags
        List<PartETag> cachedEtags = getCachedPartEtags();
        partETags.addAll(cachedEtags);

        // calculate the start position for resume
        startPartNumber = cachedEtags.size() + 1;
        filePosition = (startPartNumber - 1) * partSize;
        bytesUploaded = filePosition;

        Log.i(TAG, "resuming at part " + startPartNumber + " position " + filePosition);

    } else {
        // initiate a new multi part upload
        Log.i(TAG, "initiating new upload");

        InitiateMultipartUploadRequest initRequest = new InitiateMultipartUploadRequest(s3bucketName, s3key);
        configureInitiateRequest(initRequest);
        InitiateMultipartUploadResult initResponse = s3Client.initiateMultipartUpload(initRequest);
        uploadId = initResponse.getUploadId();

    }

    final AbortMultipartUploadRequest abortRequest = new AbortMultipartUploadRequest(s3bucketName, s3key,
            uploadId);

    for (int k = startPartNumber; filePosition < contentLength; k++) {

        long thisPartSize = Math.min(partSize, (contentLength - filePosition));

        Log.i(TAG, "starting file part " + k + " with size " + thisPartSize);

        UploadPartRequest uploadRequest = new UploadPartRequest().withBucketName(s3bucketName).withKey(s3key)
                .withUploadId(uploadId).withPartNumber(k).withFileOffset(filePosition).withFile(file)
                .withPartSize(thisPartSize);

        ProgressListener s3progressListener = new ProgressListener() {
            public void progressChanged(ProgressEvent progressEvent) {

                // bail out if user cancelled
                // TODO calling shutdown too brute force?
                if (userInterrupted) {
                    s3Client.shutdown();
                    throw new UploadIterruptedException("User interrupted");
                } else if (userAborted) {
                    // aborted requests cannot be resumed, so clear any cached etags
                    clearProgressCache();
                    s3Client.abortMultipartUpload(abortRequest);
                    s3Client.shutdown();
                }

                bytesUploaded += progressEvent.getBytesTransfered();

                //Log.d(TAG, "bytesUploaded=" + bytesUploaded);

                // broadcast progress
                float fpercent = ((bytesUploaded * 100) / contentLength);
                int percent = Math.round(fpercent);
                if (progressListener != null) {
                    progressListener.progressChanged(progressEvent, bytesUploaded, percent);
                }

            }
        };

        uploadRequest.setProgressListener(s3progressListener);

        UploadPartResult result = s3Client.uploadPart(uploadRequest);

        partETags.add(result.getPartETag());

        // cache the part progress for this upload
        if (k == 1) {
            initProgressCache(uploadId);
        }
        // store part etag
        cachePartEtag(result);

        filePosition += thisPartSize;
    }

    CompleteMultipartUploadRequest compRequest = new CompleteMultipartUploadRequest(s3bucketName, s3key,
            uploadId, partETags);

    CompleteMultipartUploadResult result = s3Client.completeMultipartUpload(compRequest);
    bytesUploaded = 0;

    Log.i(TAG, "upload complete for " + uploadId);

    clearProgressCache();

    return result.getLocation();

}

From source file:com.taxi.UploadModel.java

License:Open Source License

public UploadModel(Context context, Uri uri, TransferManager manager) {
    super(context, uri, manager);
    my_context = context;//from  w  ww .ja  va  2  s. c  o  m
    mStatus = Status.IN_PROGRESS;
    mExtension = MimeTypeMap.getSingleton().getExtensionFromMimeType(context.getContentResolver().getType(uri));
    ;
    mListener = new ProgressListener() {
        @Override
        public void progressChanged(ProgressEvent event) {
            if (event.getEventCode() == ProgressEvent.COMPLETED_EVENT_CODE) {
                mStatus = Status.COMPLETED;
                if (mFile != null) {
                    mFile.delete();
                }
            }
        }
    };
}