Skip to content

Commit

Permalink
fixed some serious bugs with audio playing and downloading. also fixe…
Browse files Browse the repository at this point in the history
…d download between two suras or two juz\'s to fix #233.
  • Loading branch information
ahmedre committed Dec 31, 2012
1 parent 4c22c5e commit 94d68b1
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,7 @@
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

Expand Down Expand Up @@ -468,7 +465,7 @@ private boolean downloadRange(String urlString, String destination,
if (isGapless){
if (i == endSura && endAyah == 0){ continue; }
String destDir = destination + File.separator;
String url = String.format(urlString, i);
String url = String.format(Locale.US, urlString, i);
Log.d(TAG, "gapless asking to download " + url + " to " + destDir);
result = downloadFileWrapper(url, destDir, null, details);
if (!result){ return false; }
Expand All @@ -481,7 +478,7 @@ private boolean downloadRange(String urlString, String destination,
new File(destDir).mkdirs();

for (int j = firstAyah; j <= lastAyah; j++){
String url = String.format(urlString, i, j);
String url = String.format(Locale.US, urlString, i, j);
String destFile = j + extension;
result = downloadFileWrapper(url, destDir, destFile, details);
if (!result){ return false; }
Expand All @@ -498,7 +495,7 @@ private boolean downloadRange(String urlString, String destination,
File basmallah = new File(destDir, "1" + extension);
if (!basmallah.exists()){
Log.d(TAG, "basmallah doesn't exist, downloading...");
String url = String.format(urlString, 1, 1);
String url = String.format(Locale.US, urlString, 1, 1);
String destFile = 1 + extension;
result = downloadFileWrapper(url, destDir, destFile, details);
if (!result){ return false; }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package com.quran.labs.androidquran.service.util;

import java.io.Serializable;

import android.content.Context;
import android.util.Log;

import com.quran.labs.androidquran.common.QuranAyah;
import com.quran.labs.androidquran.data.QuranInfo;

import java.io.Serializable;
import java.util.Locale;

public class AudioRequest implements Serializable {

private static final long serialVersionUID = 1L;
Expand Down Expand Up @@ -128,9 +128,9 @@ public String getUrl(){
}

if (isGapless()){
Log.d(TAG, "isGapless, url: " +
String.format(mBaseUrl, mCurrentSura));
return String.format(mBaseUrl, mCurrentSura);
String url = String.format(Locale.US, mBaseUrl, mCurrentSura);
Log.d(TAG, "isGapless, url: " + url);
return url;
}

int sura = mCurrentSura;
Expand All @@ -150,7 +150,7 @@ public String getUrl(){
}
}

return String.format(mBaseUrl, sura, ayah);
return String.format(Locale.US, mBaseUrl, sura, ayah);
}

public boolean haveSuraAyah(int sura, int ayah){
Expand Down
44 changes: 35 additions & 9 deletions app/src/main/java/com/quran/labs/androidquran/util/AudioUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.quran.labs.androidquran.service.util.DownloadAudioRequest;

import java.io.File;
import java.util.Locale;

public class AudioUtils {
private static final String TAG = "AudioUtils";
Expand Down Expand Up @@ -117,10 +118,32 @@ public static String getGaplessDatabaseUrl(

public static QuranAyah getLastAyahToPlay(QuranAyah startAyah,
int page, int mode){
int pageLastSura = 114;
int pageLastAyah = 6;
if (page > 604 || page < 0){ return null; }
if (page < 604){
int nextPageSura = QuranInfo.PAGE_SURA_START[page];
int nextPageAyah = QuranInfo.PAGE_AYAH_START[page];

pageLastSura = nextPageSura;
pageLastAyah = nextPageAyah - 1;
if (pageLastAyah < 1){
pageLastSura--;
if (pageLastSura < 1){ pageLastSura = 1; }
pageLastAyah = QuranInfo.getNumAyahs(pageLastSura);
}
}

if (mode == LookAheadAmount.SURA){
int sura = startAyah.getSura();
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 QuranAyah(sura, lastAyah);
}
else if (mode == LookAheadAmount.JUZ){
Expand All @@ -130,19 +153,22 @@ else if (mode == LookAheadAmount.JUZ){
}
else if (juz >= 1 && juz < 30){
int[] endJuz = QuranInfo.QUARTERS[juz * 8];
if (pageLastSura > endJuz[0]){
// ex between jathiya and a7qaf
endJuz = QuranInfo.QUARTERS[(juz+1) * 8];
}
else if (pageLastSura == endJuz[0] &&
pageLastAyah > endJuz[1]){
// ex surat al anfal
endJuz = QuranInfo.QUARTERS[(juz+1) * 8];
}

return new QuranAyah(endJuz[0], endJuz[1]);
}
}

// page mode (fallback also from errors above)
if (page > 604 || page < 1){ return null; }
else if (page == 604){ return new QuranAyah(114, 6); }
else {
// get sura and ayah for the next page
int sura = QuranInfo.PAGE_SURA_START[page];
int ayah = QuranInfo.PAGE_AYAH_START[page];
return new QuranAyah(sura, ayah-1);
}
return new QuranAyah(pageLastSura, pageLastAyah);
}

public static boolean shouldDownloadBasmallah(Context context,
Expand Down Expand Up @@ -230,7 +256,7 @@ public static boolean haveAllFiles(DownloadAudioRequest request){
if (isGapless){
if (i == endSura && endAyah == 0){ continue; }
String p = request.getBaseUrl();
String fileName = String.format(p, i);
String fileName = String.format(Locale.US, p, i);
Log.d(TAG, "gapless, checking if we have " + fileName);
f = new File(fileName);
if (!f.exists()){ return false; }
Expand Down

0 comments on commit 94d68b1

Please sign in to comment.