Search in sources :

Example 1 with ProgressIntent

use of com.quran.labs.androidquran.service.util.QuranDownloadNotifier.ProgressIntent in project quran_android by quran.

the class QuranDownloadService method handleOnStartCommand.

private void handleOnStartCommand(Intent intent, int startId) {
    if (intent != null) {
        if (ACTION_CANCEL_DOWNLOADS.equals(intent.getAction())) {
            mServiceHandler.removeCallbacksAndMessages(null);
            mIsDownloadCanceled = true;
            sendNoOpMessage(startId);
        } else if (ACTION_RECONNECT.equals(intent.getAction())) {
            int type = intent.getIntExtra(EXTRA_DOWNLOAD_TYPE, DOWNLOAD_TYPE_UNDEF);
            Intent currentLast = mLastSentIntent;
            int lastType = currentLast == null ? -1 : currentLast.getIntExtra(EXTRA_DOWNLOAD_TYPE, DOWNLOAD_TYPE_UNDEF);
            if (type == lastType) {
                if (currentLast != null) {
                    mBroadcastManager.sendBroadcast(currentLast);
                }
            } else if (mServiceHandler.hasMessages(type)) {
                Intent progressIntent = new Intent(ProgressIntent.INTENT_NAME);
                progressIntent.putExtra(ProgressIntent.DOWNLOAD_TYPE, type);
                progressIntent.putExtra(ProgressIntent.STATE, ProgressIntent.STATE_DOWNLOADING);
                mBroadcastManager.sendBroadcast(progressIntent);
            }
            sendNoOpMessage(startId);
        } else {
            // if we are currently downloading, resend the last broadcast
            // and don't queue anything
            String download = intent.getStringExtra(EXTRA_DOWNLOAD_KEY);
            Intent currentLast = mLastSentIntent;
            String currentDownload = currentLast == null ? null : currentLast.getStringExtra(ProgressIntent.DOWNLOAD_KEY);
            if (download != null && currentDownload != null && download.equals(currentDownload)) {
                Timber.d("resending last broadcast...");
                mBroadcastManager.sendBroadcast(currentLast);
                String state = currentLast.getStringExtra(ProgressIntent.STATE);
                if (!ProgressIntent.STATE_SUCCESS.equals(state) && !ProgressIntent.STATE_ERROR.equals(state)) {
                    // re-queue fatal errors and success cases again just in case
                    // of a race condition in which we miss the error pref and
                    // miss the success/failure notification and this re-play
                    sendNoOpMessage(startId);
                    Timber.d("leaving...");
                    return;
                }
            }
            int what = intent.getIntExtra(EXTRA_DOWNLOAD_TYPE, DOWNLOAD_TYPE_UNDEF);
            // put the message in the queue
            Message msg = mServiceHandler.obtainMessage();
            msg.arg1 = startId;
            msg.obj = intent;
            msg.what = what;
            mServiceHandler.sendMessage(msg);
        }
    }
}
Also used : Message(android.os.Message) Intent(android.content.Intent) ProgressIntent(com.quran.labs.androidquran.service.util.QuranDownloadNotifier.ProgressIntent)

Aggregations

Intent (android.content.Intent)1 Message (android.os.Message)1 ProgressIntent (com.quran.labs.androidquran.service.util.QuranDownloadNotifier.ProgressIntent)1