use of com.quran.labs.androidquran.common.Response in project quran_android by quran.
the class QuranDisplayHelper method getQuranPage.
@NonNull
static Response getQuranPage(OkHttpClient okHttpClient, Context context, String widthParam, int page, QuranFileUtils quranFileUtils) {
Response response;
String filename = quranFileUtils.getPageFileName(page);
response = quranFileUtils.getImageFromSD(context, widthParam, filename);
if (!response.isSuccessful()) {
// the user to mount their sdcard and try again.
if (response.getErrorCode() != Response.ERROR_SD_CARD_NOT_FOUND) {
Timber.d("failed to get %d with name %s from sd...", page, filename);
response = quranFileUtils.getImageFromWeb(okHttpClient, context, filename);
}
}
return response;
}
use of com.quran.labs.androidquran.common.Response in project quran_android by quran.
the class QuranPagePresenter method downloadImages.
public void downloadImages() {
screen.hidePageDownloadError();
compositeDisposable.add(quranPageWorker.loadPages(pages).observeOn(AndroidSchedulers.mainThread()).subscribeWith(new DisposableObserver<Response>() {
@Override
public void onNext(Response response) {
if (screen != null) {
Bitmap bitmap = response.getBitmap();
if (bitmap != null) {
didDownloadImages = true;
screen.setPageBitmap(response.getPageNumber(), bitmap);
} else {
didDownloadImages = false;
final int errorCode = response.getErrorCode();
final int errorRes;
switch(errorCode) {
case Response.ERROR_SD_CARD_NOT_FOUND:
errorRes = R.string.sdcard_error;
break;
case Response.ERROR_DOWNLOADING_ERROR:
errorRes = R.string.download_error_network;
break;
default:
errorRes = R.string.download_error_general;
}
screen.setPageDownloadError(errorRes);
}
}
}
@Override
public void onError(Throwable e) {
}
@Override
public void onComplete() {
}
}));
}
use of com.quran.labs.androidquran.common.Response in project quran_android by quran.
the class QuranFileUtils method getImageFromSD.
@NonNull
public Response getImageFromSD(Context context, String widthParam, String filename) {
String location;
if (widthParam != null) {
location = getQuranImagesDirectory(context, widthParam);
} else {
location = getQuranImagesDirectory(context);
}
if (location == null) {
return new Response(Response.ERROR_SD_CARD_NOT_FOUND);
}
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ALPHA_8;
final Bitmap bitmap = BitmapFactory.decodeFile(location + File.separator + filename, options);
return bitmap == null ? new Response(Response.ERROR_FILE_NOT_FOUND) : new Response(bitmap);
}
use of com.quran.labs.androidquran.common.Response in project quran_android by quran.
the class QuranFileUtils method getImageFromWeb.
@NonNull
private Response getImageFromWeb(OkHttpClient okHttpClient, Context context, String filename, boolean isRetry) {
String urlString = IMG_BASE_URL + "width" + quranScreenInfo.getWidthParam() + File.separator + filename;
Timber.d("want to download: %s", urlString);
final Request request = new Request.Builder().url(urlString).build();
final Call call = okHttpClient.newCall(request);
InputStream stream = null;
try {
final okhttp3.Response response = call.execute();
if (response.isSuccessful()) {
stream = response.body().byteStream();
final Bitmap bitmap = decodeBitmapStream(stream);
if (bitmap != null) {
String path = getQuranImagesDirectory(context);
int warning = Response.WARN_SD_CARD_NOT_FOUND;
if (path != null && makeQuranDirectory(context)) {
path += File.separator + filename;
warning = tryToSaveBitmap(bitmap, path) ? 0 : Response.WARN_COULD_NOT_SAVE_FILE;
}
return new Response(bitmap, warning);
}
}
} catch (IOException ioe) {
Timber.e(ioe, "exception downloading file");
} finally {
CloseableExtensionKt.closeQuietly(stream);
}
return isRetry ? new Response(Response.ERROR_DOWNLOADING_ERROR) : getImageFromWeb(okHttpClient, context, filename, true);
}
use of com.quran.labs.androidquran.common.Response in project quran_android by quran.
the class QuranPageWorker method downloadImage.
private Response downloadImage(int pageNumber) {
Response response = null;
OutOfMemoryError oom = null;
try {
response = QuranDisplayHelper.getQuranPage(okHttpClient, appContext, imageWidth, pageNumber, quranFileUtils);
} catch (OutOfMemoryError me) {
Crashlytics.log(Log.WARN, TAG, "out of memory exception loading page " + pageNumber + ", " + imageWidth);
oom = me;
}
if (response == null || (response.getBitmap() == null && response.getErrorCode() != Response.ERROR_SD_CARD_NOT_FOUND)) {
if (quranScreenInfo.isDualPageMode()) {
Crashlytics.log(Log.WARN, TAG, "tablet got bitmap null, trying alternate width...");
String param = quranScreenInfo.getWidthParam();
if (param.equals(imageWidth)) {
param = quranScreenInfo.getTabletWidthParam();
}
response = QuranDisplayHelper.getQuranPage(okHttpClient, appContext, param, pageNumber, quranFileUtils);
if (response.getBitmap() == null) {
Crashlytics.log(Log.WARN, TAG, "bitmap still null, giving up... [" + response.getErrorCode() + "]");
}
}
Crashlytics.log(Log.WARN, TAG, "got response back as null... [" + (response == null ? "" : response.getErrorCode()));
}
if ((response == null || response.getBitmap() == null) && oom != null) {
throw oom;
}
response.setPageData(pageNumber);
return response;
}
Aggregations