Search in sources :

Example 6 with SuraAyah

use of com.quran.labs.androidquran.data.SuraAyah in project quran_android by quran.

the class AyahPlaybackFragment method refreshView.

@Override
protected void refreshView() {
    final Context context = getActivity();
    if (context instanceof PagerActivity && start != null && end != null) {
        final AudioRequest lastRequest = ((PagerActivity) context).getLastAudioRequest();
        final SuraAyah start;
        final SuraAyah ending;
        if (lastRequest != null) {
            start = lastRequest.getRangeStart();
            ending = lastRequest.getRangeEnd();
            verseRepeatCount = lastRequest.getRepeatInfo().getRepeatCount();
            rangeRepeatCount = lastRequest.getRangeRepeatCount();
            shouldEnforce = lastRequest.shouldEnforceBounds();
            decidedStart = start;
            decidedEnd = ending;
            applyButton.setText(R.string.play_apply);
        } else {
            start = this.start;
            if (this.start.equals(end)) {
                final int startPage = quranInfo.getPageFromSuraAyah(start.sura, start.ayah);
                final int[] pageBounds = quranInfo.getPageBounds(startPage);
                ending = new SuraAyah(pageBounds[2], pageBounds[3]);
                shouldEnforce = false;
            } else {
                ending = end;
                shouldEnforce = true;
            }
            rangeRepeatCount = 0;
            verseRepeatCount = 0;
            decidedStart = null;
            decidedEnd = null;
            applyButton.setText(R.string.play_apply_and_play);
        }
        final int maxAyat = quranInfo.getNumAyahs(start.sura);
        if (maxAyat == -1) {
            return;
        }
        updateAyahSpinner(startAyahSpinner, startAyahAdapter, maxAyat, start.ayah);
        final int endAyat = (ending.sura == start.sura) ? maxAyat : quranInfo.getNumAyahs(ending.sura);
        updateAyahSpinner(endingAyahSpinner, endingAyahAdapter, endAyat, ending.ayah);
        startSuraSpinner.setSelection(start.sura - 1);
        endingSuraSpinner.setSelection(ending.sura - 1);
        repeatRangeSpinner.setSelection(repeatToPosition(rangeRepeatCount));
        repeatVerseSpinner.setSelection(repeatToPosition(verseRepeatCount));
        restrictToRange.setChecked(shouldEnforce);
    }
}
Also used : Context(android.content.Context) AudioRequest(com.quran.labs.androidquran.service.util.AudioRequest) SuraAyah(com.quran.labs.androidquran.data.SuraAyah) PagerActivity(com.quran.labs.androidquran.ui.PagerActivity)

Example 7 with SuraAyah

use of com.quran.labs.androidquran.data.SuraAyah in project quran_android by quran.

the class BaseTranslationPresenter method ensureProperTranslations.

/**
 * Ensures that the list of translations is valid
 * In this case, valid means that the number of verses that we have translations for is either
 * the same as the verse range, or that it's 0 (i.e. due to an error querying the database). If
 * the list has a non-zero length that is less than what the verseRange says, it adds empty
 * entries for those.
 *
 * @param verseRange the range of verses we're trying to get
 * @param texts      the data we got back from the database
 * @return a list of QuranText with a length of either 0 or the verse range
 */
@NonNull
List<QuranText> ensureProperTranslations(@NonNull VerseRange verseRange, @NonNull List<QuranText> texts) {
    int expectedVerses = verseRange.versesInRange;
    int textSize = texts.size();
    if (textSize == 0 || textSize == expectedVerses) {
        return texts;
    }
    // missing some entries for some ayat - this is a work around for bad data in some databases
    // ex. ibn katheer is missing 3 records, 1 in each of suras 5, 17, and 87.
    SuraAyah start = new SuraAyah(verseRange.startSura, verseRange.startAyah);
    SuraAyah end = new SuraAyah(verseRange.endingSura, verseRange.endingAyah);
    SuraAyahIterator iterator = new SuraAyahIterator(quranInfo, start, end);
    int i = 0;
    while (iterator.next()) {
        QuranText item = texts.size() > i ? texts.get(i) : null;
        if (item == null || item.sura != iterator.getSura() || item.ayah != iterator.getAyah()) {
            texts.add(i, new QuranText(iterator.getSura(), iterator.getAyah(), ""));
        }
        i++;
    }
    return texts;
}
Also used : SuraAyahIterator(com.quran.labs.androidquran.data.SuraAyahIterator) SuraAyah(com.quran.labs.androidquran.data.SuraAyah) QuranText(com.quran.labs.androidquran.common.QuranText) NonNull(android.support.annotation.NonNull)

Example 8 with SuraAyah

use of com.quran.labs.androidquran.data.SuraAyah in project quran_android by quran.

the class AudioService method updateAudioPlayPosition.

private void updateAudioPlayPosition() {
    Timber.d("updateAudioPlayPosition");
    if (audioRequest == null) {
        return;
    }
    if (player != null || gaplessSuraData == null) {
        int sura = audioRequest.getCurrentSura();
        int ayah = audioRequest.getCurrentAyah();
        int updatedAyah = ayah;
        int maxAyahs = quranInfo.getNumAyahs(sura);
        if (sura != gaplessSura) {
            return;
        }
        setState(PlaybackStateCompat.STATE_PLAYING);
        int pos = player.getCurrentPosition();
        Integer ayahTime = gaplessSuraData.get(ayah);
        Timber.d("updateAudioPlayPosition: %d:%d, currently at %d vs expected at %d", sura, ayah, pos, ayahTime);
        if (ayahTime > pos) {
            int iterAyah = ayah;
            while (--iterAyah > 0) {
                ayahTime = gaplessSuraData.get(iterAyah);
                if (ayahTime <= pos) {
                    updatedAyah = iterAyah;
                    break;
                } else {
                    updatedAyah--;
                }
            }
        } else {
            int iterAyah = ayah;
            while (++iterAyah <= maxAyahs) {
                ayahTime = gaplessSuraData.get(iterAyah);
                if (ayahTime > pos) {
                    updatedAyah = iterAyah - 1;
                    break;
                } else {
                    updatedAyah++;
                }
            }
        }
        Timber.d("updateAudioPlayPosition: %d:%d, decided ayah should be: %d", sura, ayah, updatedAyah);
        if (updatedAyah != ayah) {
            ayahTime = gaplessSuraData.get(ayah);
            if (Math.abs(pos - ayahTime) < 150) {
                // shouldn't change ayahs if the delta is just 150ms...
                handler.sendEmptyMessageDelayed(MSG_UPDATE_AUDIO_POS, 150);
                return;
            }
            SuraAyah nextAyah = audioRequest.setCurrentAyah(quranInfo, sura, updatedAyah);
            if (nextAyah == null) {
                processStopRequest();
                return;
            } else if (nextAyah.sura != sura || nextAyah.ayah != updatedAyah) {
                // remove any messages currently in the queue
                handler.removeCallbacksAndMessages(null);
                // if the ayah hasn't changed, we're repeating the ayah,
                // otherwise, we're repeating a range. this variable is
                // what determines whether or not we replay the basmallah.
                final boolean ayahRepeat = (ayah == nextAyah.ayah && sura == nextAyah.sura);
                if (ayahRepeat) {
                    // jump back to the ayah we should repeat and play it
                    pos = getSeekPosition(true);
                    player.seekTo(pos);
                } else {
                    // we're repeating into a different sura
                    final boolean flag = sura != audioRequest.getCurrentSura();
                    playAudio(flag);
                }
                return;
            }
            // moved on to next ayah
            updateNotification();
        } else {
            // if we have end of sura info and we bypassed end of sura
            // line, switch the sura.
            ayahTime = gaplessSuraData.get(999);
            if (ayahTime > 0 && pos >= ayahTime) {
                SuraAyah repeat = audioRequest.setCurrentAyah(quranInfo, sura + 1, 1);
                if (repeat != null && repeat.sura == sura) {
                    // remove any messages currently in the queue
                    handler.removeCallbacksAndMessages(null);
                    // jump back to the ayah we should repeat and play it
                    pos = getSeekPosition(false);
                    player.seekTo(pos);
                } else {
                    playAudio(true);
                }
                return;
            }
        }
        notifyAyahChanged();
        if (maxAyahs >= (updatedAyah + 1)) {
            Integer t = gaplessSuraData.get(updatedAyah + 1);
            t = t - player.getCurrentPosition();
            Timber.d("updateAudioPlayPosition postingDelayed after: %d", t);
            if (t < 100) {
                t = 100;
            } else if (t > 10000) {
                t = 10000;
            }
            handler.sendEmptyMessageDelayed(MSG_UPDATE_AUDIO_POS, t);
        }
    // if we're on the last ayah, don't do anything - let the file
    // complete on its own to avoid getCurrentPosition() bugs.
    }
}
Also used : SuraAyah(com.quran.labs.androidquran.data.SuraAyah)

Example 9 with SuraAyah

use of com.quran.labs.androidquran.data.SuraAyah in project quran_android by quran.

the class AudioUtils method getLastAyahToPlay.

public SuraAyah getLastAyahToPlay(SuraAyah startAyah, int page, int mode, boolean isDualPages) {
    if (isDualPages && mode == LookAheadAmount.PAGE && (page % 2 == 1)) {
        // if we download page by page and we are currently in tablet mode
        // and playing from the right page, get the left page as well.
        page++;
    }
    int pageLastSura = 114;
    int pageLastAyah = 6;
    // page < 0 - intentional, because nextPageAyah looks up the ayah on the next page
    if (page > totalPages || page < 0) {
        return null;
    }
    if (page < totalPages) {
        int nextPage = page + 1;
        int nextPageSura = quranInfo.safelyGetSuraOnPage(nextPage);
        // using [page+1] as an index because we literally want the next page
        int nextPageAyah = quranInfo.getFirstAyahOnPage(nextPage);
        pageLastSura = nextPageSura;
        pageLastAyah = nextPageAyah - 1;
        if (pageLastAyah < 1) {
            pageLastSura--;
            pageLastAyah = quranInfo.getNumAyahs(pageLastSura);
        }
    }
    if (mode == LookAheadAmount.SURA) {
        int sura = startAyah.sura;
        int lastAyah = quranInfo.getNumAyahs(sura);
        if (lastAyah == -1) {
            return null;
        }
        // if we start playback between two suras, download both suras
        if (pageLastSura > sura) {
            sura = pageLastSura;
            lastAyah = quranInfo.getNumAyahs(sura);
        }
        return new SuraAyah(sura, lastAyah);
    } else if (mode == LookAheadAmount.JUZ) {
        int juz = quranInfo.getJuzFromPage(page);
        if (juz == 30) {
            return new SuraAyah(114, 6);
        } else if (juz >= 1 && juz < 30) {
            int[] endJuz = quranInfo.getQuarterByIndex(juz * 8);
            if (pageLastSura > endJuz[0]) {
                // ex between jathiya and a7qaf
                endJuz = quranInfo.getQuarterByIndex((juz + 1) * 8);
            } else if (pageLastSura == endJuz[0] && pageLastAyah > endJuz[1]) {
                // ex surat al anfal
                endJuz = quranInfo.getQuarterByIndex((juz + 1) * 8);
            }
            return new SuraAyah(endJuz[0], endJuz[1]);
        }
    }
    // page mode (fallback also from errors above)
    return new SuraAyah(pageLastSura, pageLastAyah);
}
Also used : SuraAyah(com.quran.labs.androidquran.data.SuraAyah)

Example 10 with SuraAyah

use of com.quran.labs.androidquran.data.SuraAyah in project quran_android by quran.

the class AudioUtils method doesRequireBasmallah.

private boolean doesRequireBasmallah(AudioRequest request) {
    SuraAyah minAyah = request.getMinAyah();
    int startSura = minAyah.sura;
    int startAyah = minAyah.ayah;
    SuraAyah maxAyah = request.getMaxAyah();
    int endSura = maxAyah.sura;
    int endAyah = maxAyah.ayah;
    Timber.d("seeing if need basmalla...");
    for (int i = startSura; i <= endSura; i++) {
        int lastAyah = quranInfo.getNumAyahs(i);
        if (i == endSura) {
            lastAyah = endAyah;
        }
        int firstAyah = 1;
        if (i == startSura) {
            firstAyah = startAyah;
        }
        for (int j = firstAyah; j < lastAyah; j++) {
            if (j == 1 && i != 1 && i != 9) {
                Timber.d("need basmalla for %d:%d", i, j);
                return true;
            }
        }
    }
    return false;
}
Also used : SuraAyah(com.quran.labs.androidquran.data.SuraAyah)

Aggregations

SuraAyah (com.quran.labs.androidquran.data.SuraAyah)20 Intent (android.content.Intent)3 Context (android.content.Context)2 AudioRequest (com.quran.labs.androidquran.service.util.AudioRequest)2 DownloadAudioRequest (com.quran.labs.androidquran.service.util.DownloadAudioRequest)2 PagerActivity (com.quran.labs.androidquran.ui.PagerActivity)2 NonNull (android.support.annotation.NonNull)1 Nullable (android.support.annotation.Nullable)1 MadaniDataSource (com.quran.data.pageinfo.common.MadaniDataSource)1 PageProvider (com.quran.data.source.PageProvider)1 QuranText (com.quran.labs.androidquran.common.QuranText)1 QuranInfo (com.quran.labs.androidquran.data.QuranInfo)1 SuraAyahIterator (com.quran.labs.androidquran.data.SuraAyahIterator)1 NotificationDetails (com.quran.labs.androidquran.service.util.QuranDownloadNotifier.NotificationDetails)1 ProgressIntent (com.quran.labs.androidquran.service.util.QuranDownloadNotifier.ProgressIntent)1 StreamingAudioRequest (com.quran.labs.androidquran.service.util.StreamingAudioRequest)1 QuranPage (com.quran.labs.androidquran.ui.helpers.QuranPage)1 DisposableSingleObserver (io.reactivex.observers.DisposableSingleObserver)1 File (java.io.File)1 Test (org.junit.Test)1