Managing File Uploads

The Medable Cortex Android SDK features an upload operations manager, that takes care of the following tasks:

  • Keep an up to date list of ongoing upload operations.
  • Keep an up to date list of recently successfully completed operations.
  • Keep an up to date list of recently failed operations.
  • Keep a progress value (percentage expressed in the 0..1 range) for each operation.
  • Emit notifications for every change in the manager:
    • Upload started.
    • Upload operation progress changed.
    • Upload completed or failed.

Notifications

When you're going to upload a file, you can subscribe to file uploads notifications in order to know what is happening, and when the job is complete.

NotificationCenter.defaultCenter().registerObserver(this, kMDNotificationUploadOperationStarted, new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                AssetUploader.UploadOperation fileUpload = (AssetUploader.UploadOperation) intent.getSerializableExtra(Constants.kObject);
            }
        });

        NotificationCenter.defaultCenter().registerObserver(this, kMDNotificationUploadOperationProgress, new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                float progress = intent.getBundleExtra(Constants.kObject).getFloat(kProgress);
                String fileName = intent.getBundleExtra(Constants.kObject).getString(kFilename);
             }
        });

        NotificationCenter.defaultCenter().registerObserver(this, kMDNotificationUploadOperationEndedSuccessfully, new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                AssetUploader.UploadOperation fileUpload = (AssetUploader.UploadOperation) intent.getSerializableExtra(Constants.kObject);
            }
        });
NotificationCenter.defaultCenter().registerObserver(this, kMDNotificationUploadOperationStarted, object : BroadcastReceiver() {
            override fun onReceive(context: Context, intent: Intent) {
                val fileUpload = intent.getSerializableExtra(Constants.kObject) as AssetUploader.UploadOperation
            }
        })

        NotificationCenter.defaultCenter().registerObserver(this, kMDNotificationUploadOperationProgress, object : BroadcastReceiver() {
            override fun onReceive(context: Context, intent: Intent) {
                val progress = intent.getBundleExtra(Constants.kObject).getFloat(kProgress)
                val fileName = intent.getBundleExtra(Constants.kObject).getString(kFilename)
            }
        })

        NotificationCenter.defaultCenter().registerObserver(this, kMDNotificationUploadOperationEndedSuccessfully, object : BroadcastReceiver() {
            override fun onReceive(context: Context, intent: Intent) {
                val fileUpload = intent.getSerializableExtra(Constants.kObject) as AssetUploader.UploadOperation
            }
        })

Within the notification, depending on the event, two different objects could be sent:

  • A Bundle object is delivered inside the Notification.object property if it is notifying about upload progress. You can access progress and file information to get the upload info.

  • A UploadOperations object is delivered inside the Notification.object property if it is notifying about the following events:

    • A new UploadOperation was added to the queue (started).
    • A UploadOperation succeeded.
    • A UploadOperation failed.
    • A UploadOperation was removed from completed/failed.

State

The state of all upload operations can be queried from the UploadOperations class by checking the the following class methods:

AssetUploader.sharedInstance().completedUploads();

AssetUploader.sharedInstance().ongoingUploads();

AssetUploader.sharedInstance().failedUploads();

An ongoing operation is an upload task that is still uploading, i.e. it hasn't failed and it hasn't completed the upload yet.

Recently completed operations are short lived. After completion, the operation objects will stay in this list for at least 10 seconds. After this time, they'll be removed from the list.

Recently failed operations live a little longer. After failing, the operation objects will stay in this list for at least 1 minute. Again, after this time, they are be removed from the list.

Each operation is an instance of class UploadOperation. Here are some interesting features they provide:

  • Progress: Use the progressNumber attribute to determine the progress percentage (measured in the 0..1 range) of the upload operation.
  • File Name: If you are uploading several things at once, the fileName property contains the name of the file being uploaded.

Retry a Failed Operation

Any operation that has failed can be retried, it need not be in the failed operations set, but beware that the upload tokens used will eventually expire.

Here is code to retry an operation currently in the failed set:

AssetUploader.UploadOperation uploadOperation = AssetUploader.sharedInstance().failedUploads().get(x);
        AssetUploader.sharedInstance().retryUpload(uploadOperation);