Class MessageHandler

java.lang.Object
com.castlabs.sdk.downloader.MessageHandler
All Implemented Interfaces:
Handler.Callback

public class MessageHandler extends Object implements Handler.Callback
The download message handler broadcasts downloader related messages:

  • ACTION_DOWNLOAD_PROGRESS
  • ACTION_DOWNLOAD_PATH_UPDATE
  • The following sample shows how to subscribe and receive the downloader messages:
    
     IntentFilter filter = new IntentFilter();
     filter.addCategory(MessageHandler.INTENT_DOWNLOAD_CATEGORY);
     filter.addAction(MessageHandler.ACTION_DOWNLOAD_ERROR);
     filter.addAction(MessageHandler.ACTION_DOWNLOAD_STOPPED);
     filter.addAction(MessageHandler.ACTION_DOWNLOAD_CREATED);
     filter.addAction(MessageHandler.ACTION_DOWNLOAD_STARTED);
     filter.addAction(MessageHandler.ACTION_DOWNLOAD_DELETED);
     filter.addAction(MessageHandler.ACTION_DOWNLOAD_COMPLETED);
     filter.addAction(MessageHandler.ACTION_DOWNLOAD_NO_PENDING);
     filter.addAction(MessageHandler.ACTION_DOWNLOAD_STORAGE_OK);
     filter.addAction(MessageHandler.ACTION_DOWNLOAD_STORAGE_LOW);
     filter.addAction(MessageHandler.ACTION_DOWNLOAD_PROGRESS);
     filter.addAction(MessageHandler.ACTION_DOWNLOAD_PATH_UPDATE);
    
     LocalBroadcastManager.getInstance(context).registerReceiver(new BroadcastReceiver{
            public void onReceive(Context context, Intent intent) {
                    String downloadId = intent.getStringExtra(MessageHandler.INTENT_DOWNLOAD_ID);
                    Log.d(TAG, "Message: " + intent.getAction() + ", download Id: " + downloadId);
                    switch (intent.getAction()) {
                            case MessageHandler.ACTION_DOWNLOAD_STOPPED:
                                    break;
                            case MessageHandler.ACTION_DOWNLOAD_CREATED:
                                    break;
                            case MessageHandler.ACTION_DOWNLOAD_STARTED:
                                    break;
                            case MessageHandler.ACTION_DOWNLOAD_DELETED:
                                    break;
                            case MessageHandler.ACTION_DOWNLOAD_COMPLETED:
                                    break;
                            case MessageHandler.ACTION_DOWNLOAD_NO_PENDING:
                                    break;
                            case MessageHandler.ACTION_DOWNLOAD_ERROR:
                                    break;
                            case MessageHandler.ACTION_DOWNLOAD_STORAGE_LOW:
                                    break;
                            case MessageHandler.ACTION_DOWNLOAD_STORAGE_OK:
                                    break;
                            case MessageHandler.ACTION_DOWNLOAD_PROGRESS:
                                    break;
                            case MessageHandler.ACTION_DOWNLOAD_PATH_UPDATE:
                                    break;
                            default:
                                    break;
                    }
            }
     }, filter);
     

    Error handling

    The intent bundle that is send with broadcast messages always contains the INTENT_DOWNLOAD_ID parameter, which is the download ID String. In addition, all ACTION_DOWNLOAD_ERROR messages contain the INTENT_DOWNLOAD_ERROR String, which is a string representation that contains the message from the exception that occurred during the download. Because all intent parameters need to be parcelable, we can unfortunately not embed the original Exception.

    Since version 4.1.5, error message also contain additional data. INTENT_DOWNLOAD_ERROR_TYPE contains the error type, which is one of the ERROR_* constants of this class. Some of the error types might contains additional information. You can check for the http status code under the INTENT_DOWNLOAD_ERROR_HTTP_STATUS key. The URL that caused the error can be checked with INTENT_DOWNLOAD_ERROR_HTTP_URL. Both of these values are optional and we might not be able to report them for all errors.

    General connectivity issues are reported as ERROR_TYPE_CONNECTION_ERROR when the underlying socket connection is interrupted. That can happen for instance when the device looses its connectivity.

    DRM errors are reported as ERROR_TYPE_DRM_ERROR if there was a problem while fetching the offline license. The underlying DRM error code can be queried through the INTENT_DOWNLOAD_ERROR_DRM key, which will contain one of the values defined in the constants starting with ERROR_* in DrmTodayException.