Search in sources :

Example 1 with UploadProgressListener

use of com.readystatesoftware.simpl3r.Uploader.UploadProgressListener in project android-simpl3r by jgilfelt.

the class UploadService method onHandleIntent.

@Override
protected void onHandleIntent(Intent intent) {
    String filePath = intent.getStringExtra(ARG_FILE_PATH);
    File fileToUpload = new File(filePath);
    final String s3ObjectKey = md5(filePath);
    String s3BucketName = getString(R.string.s3_bucket);
    final String msg = "Uploading " + s3ObjectKey + "...";
    // create a new uploader for this file
    uploader = new Uploader(this, s3Client, s3BucketName, s3ObjectKey, fileToUpload);
    // listen for progress updates and broadcast/notify them appropriately
    uploader.setProgressListener(new UploadProgressListener() {

        @Override
        public void progressChanged(ProgressEvent progressEvent, long bytesUploaded, int percentUploaded) {
            Notification notification = buildNotification(msg, percentUploaded);
            nm.notify(NOTIFY_ID_UPLOAD, notification);
            broadcastState(s3ObjectKey, percentUploaded, msg);
        }
    });
    // broadcast/notify that our upload is starting
    Notification notification = buildNotification(msg, 0);
    nm.notify(NOTIFY_ID_UPLOAD, notification);
    broadcastState(s3ObjectKey, 0, msg);
    try {
        // initiate the upload
        String s3Location = uploader.start();
        broadcastState(s3ObjectKey, -1, "File successfully uploaded to " + s3Location);
    } catch (UploadIterruptedException uie) {
        broadcastState(s3ObjectKey, -1, "User interrupted");
    } catch (Exception e) {
        e.printStackTrace();
        broadcastState(s3ObjectKey, -1, "Error: " + e.getMessage());
    }
}
Also used : UploadProgressListener(com.readystatesoftware.simpl3r.Uploader.UploadProgressListener) UploadIterruptedException(com.readystatesoftware.simpl3r.UploadIterruptedException) ProgressEvent(com.amazonaws.services.s3.model.ProgressEvent) File(java.io.File) Uploader(com.readystatesoftware.simpl3r.Uploader) Notification(android.app.Notification) UploadIterruptedException(com.readystatesoftware.simpl3r.UploadIterruptedException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Aggregations

Notification (android.app.Notification)1 ProgressEvent (com.amazonaws.services.s3.model.ProgressEvent)1 UploadIterruptedException (com.readystatesoftware.simpl3r.UploadIterruptedException)1 Uploader (com.readystatesoftware.simpl3r.Uploader)1 UploadProgressListener (com.readystatesoftware.simpl3r.Uploader.UploadProgressListener)1 File (java.io.File)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1