Search in sources :

Example 1 with NotificationDetails

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

the class QuranDownloadService method onHandleIntent.

private void onHandleIntent(Intent intent) {
    if (ACTION_DOWNLOAD_URL.equals(intent.getAction())) {
        String url = intent.getStringExtra(EXTRA_URL);
        String key = intent.getStringExtra(EXTRA_DOWNLOAD_KEY);
        int type = intent.getIntExtra(EXTRA_DOWNLOAD_TYPE, 0);
        String notificationTitle = intent.getStringExtra(EXTRA_NOTIFICATION_NAME);
        NotificationDetails details = new NotificationDetails(notificationTitle, key, type);
        // check if already downloaded, and if so, send broadcast
        boolean isZipFile = url.endsWith(".zip");
        if (isZipFile && mSuccessfulZippedDownloads.containsKey(url)) {
            mLastSentIntent = mNotifier.broadcastDownloadSuccessful(details);
            return;
        } else if (mRecentlyFailedDownloads.containsKey(url)) {
            // if recently failed and we want to repeat the last error...
            if (intent.getBooleanExtra(EXTRA_REPEAT_LAST_ERROR, false)) {
                Intent failedIntent = mRecentlyFailedDownloads.get(url);
                if (failedIntent != null) {
                    // re-broadcast and leave - just in case of race condition
                    mBroadcastManager.sendBroadcast(failedIntent);
                    return;
                }
            } else // otherwise, remove the fact it was an error and retry
            {
                mRecentlyFailedDownloads.remove(url);
            }
        }
        mNotifier.resetNotifications();
        // get the start/end ayah info if it's a ranged download
        SuraAyah startAyah = intent.getParcelableExtra(EXTRA_START_VERSE);
        SuraAyah endAyah = intent.getParcelableExtra(EXTRA_END_VERSE);
        boolean isGapless = intent.getBooleanExtra(EXTRA_IS_GAPLESS, false);
        String outputFile = intent.getStringExtra(EXTRA_OUTPUT_FILE_NAME);
        if (outputFile == null) {
            outputFile = getFilenameFromUrl(url);
        }
        String destination = intent.getStringExtra(EXTRA_DESTINATION);
        mLastSentIntent = null;
        if (destination == null) {
            return;
        }
        boolean result;
        if (startAyah != null && endAyah != null) {
            result = downloadRange(url, destination, startAyah, endAyah, isGapless, details);
        } else {
            result = download(url, destination, outputFile, details);
        }
        if (result && isZipFile) {
            mSuccessfulZippedDownloads.put(url, true);
        } else if (!result) {
            mRecentlyFailedDownloads.put(url, mLastSentIntent);
        }
        mLastSentIntent = null;
    }
}
Also used : NotificationDetails(com.quran.labs.androidquran.service.util.QuranDownloadNotifier.NotificationDetails) SuraAyah(com.quran.labs.androidquran.data.SuraAyah) Intent(android.content.Intent) ProgressIntent(com.quran.labs.androidquran.service.util.QuranDownloadNotifier.ProgressIntent)

Aggregations

Intent (android.content.Intent)1 SuraAyah (com.quran.labs.androidquran.data.SuraAyah)1 NotificationDetails (com.quran.labs.androidquran.service.util.QuranDownloadNotifier.NotificationDetails)1 ProgressIntent (com.quran.labs.androidquran.service.util.QuranDownloadNotifier.ProgressIntent)1