Search in sources :

Example 1 with Response

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;
}
Also used : Response(com.quran.labs.androidquran.common.Response) NonNull(android.support.annotation.NonNull)

Example 2 with 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() {
        }
    }));
}
Also used : Response(com.quran.labs.androidquran.common.Response) Bitmap(android.graphics.Bitmap) DisposableObserver(io.reactivex.observers.DisposableObserver)

Example 3 with Response

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);
}
Also used : Response(com.quran.labs.androidquran.common.Response) Bitmap(android.graphics.Bitmap) BitmapFactory(android.graphics.BitmapFactory) NonNull(android.support.annotation.NonNull)

Example 4 with Response

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);
}
Also used : Response(com.quran.labs.androidquran.common.Response) Call(okhttp3.Call) Bitmap(android.graphics.Bitmap) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Request(okhttp3.Request) IOException(java.io.IOException) NonNull(android.support.annotation.NonNull)

Example 5 with Response

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;
}
Also used : Response(com.quran.labs.androidquran.common.Response)

Aggregations

Response (com.quran.labs.androidquran.common.Response)5 Bitmap (android.graphics.Bitmap)3 NonNull (android.support.annotation.NonNull)3 BitmapFactory (android.graphics.BitmapFactory)1 DisposableObserver (io.reactivex.observers.DisposableObserver)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Call (okhttp3.Call)1 Request (okhttp3.Request)1