// Path: app/src/main/java/net/fabiszewski/ulogger/ImageHelper.java
// static void clearImageCache(@NonNull Context context) {
//     File dir = context.getCacheDir();
//     clearImages(dir);
// }
// 
// Path: app/src/main/java/net/fabiszewski/ulogger/ImageHelper.java
// static void getPersistablePermission(@NonNull Context context, @NonNull Uri uri) {
//     try {
//         context.getContentResolver().takePersistableUriPermission(uri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
//     } catch (SecurityException e) {
//         if (Logger.DEBUG) { Log.d(TAG, "[getPersistablePermission failed for " + uri + "]"); }
//     }
// }
// 
// Path: app/src/main/java/net/fabiszewski/ulogger/ImageHelper.java
// static Bitmap getResampledBitmap(@NonNull Context context, @NonNull Uri uri, int dstWidth) throws IOException {
//     ContentResolver cr = context.getContentResolver();
//     BitmapFactory.Options options = new BitmapFactory.Options();
//     options.inJustDecodeBounds = true;
//     try (InputStream is = cr.openInputStream(uri)) {
//         BitmapFactory.decodeStream(is, null, options);
//     }
//     int srcWidth = Math.max(options.outWidth, options.outHeight);
//     int scale = srcWidth / dstWidth;
//     if (Logger.DEBUG) { Log.d(TAG, "[resampleIfNeeded scale: " + scale + "]"); }
//     options = new BitmapFactory.Options();
//     Bitmap bitmap = null;
//     boolean retry = false;
//     do {
//         try {
//             if (scale > 1) {
//                 options.inScaled = true;
//                 options.inSampleSize = 1;
//                 options.inDensity = srcWidth;
//                 options.inTargetDensity =  dstWidth * options.inSampleSize;
//             }
//             try (InputStream is = cr.openInputStream(uri)) {
//                 bitmap = BitmapFactory.decodeStream(is, null, options);
//             }
//         } catch (OutOfMemoryError e) {
//             if (Logger.DEBUG) { Log.d(TAG, "[resampleIfNeeded OutOfMemoryError]"); }
//             if (retry) {
//                 throw new IOException("Out of memory");
//             } else if (scale > 1) {
//                 retry = true;
//                 options.inSampleSize = scale;
//                 if (Logger.DEBUG) { Log.d(TAG, "[resampleIfNeeded try sampling]"); }
//             }
//         }
//     } while (retry);
// 
//     if (bitmap == null) {
//         throw new IOException("Failed to decode image");
//     }
// 
//     bitmap = fixImageOrientation(context, uri, bitmap);
//     return bitmap;
// }
// 
// Path: app/src/main/java/net/fabiszewski/ulogger/ImageHelper.java
// static Bitmap getThumbnail(@NonNull Context context, @NonNull Uri uri) throws IOException {
//     int sizePx = getThumbnailSize(context);
//     Bitmap bitmap;
//     ContentResolver cr = context.getContentResolver();
//     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
//         bitmap = cr.loadThumbnail(uri, new Size(sizePx, sizePx), null);
//     } else {
//         try (InputStream is = cr.openInputStream(uri)) {
//             bitmap = BitmapFactory.decodeStream(is, null, null);
//         }
// 
//         bitmap = ThumbnailUtils.extractThumbnail(bitmap, sizePx, sizePx);
//     }
//     bitmap = fixImageOrientation(context, uri, bitmap);
//     return bitmap;
// }
// 
// Path: app/src/main/java/net/fabiszewski/ulogger/ImageHelper.java
// static Uri saveToCache(@NonNull Context context, @NonNull Bitmap bitmap) throws IOException {
//     String filename = getUniqueName() + EXT_JPG;
//     File outFile = new File(context.getCacheDir(), filename);
//     try (FileOutputStream os = new FileOutputStream(outFile)) {
//         bitmap.compress(Bitmap.CompressFormat.JPEG, 90, os);
//     }
//     return Uri.fromFile(outFile);
// }


import android.app.Activity;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.UiThread;
import androidx.annotation.WorkerThread;
import androidx.preference.PreferenceManager;
import java.io.IOException;
import java.lang.ref.WeakReference;
import static net.fabiszewski.ulogger.ImageHelper.clearImageCache;
import static net.fabiszewski.ulogger.ImageHelper.getPersistablePermission;
import static net.fabiszewski.ulogger.ImageHelper.getResampledBitmap;
import static net.fabiszewski.ulogger.ImageHelper.getThumbnail;
import static net.fabiszewski.ulogger.ImageHelper.saveToCache;
    }

    @UiThread
    private void onPostExecute(@Nullable ImageTaskResult result) {
        ImageTaskCallback callback = weakCallback.get();
        if (callback != null && callback.getActivity() != null) {
            if (result == null) {
                callback.onImageTaskFailure(errorMessage);
            } else {
                callback.onImageTaskCompleted(result.savedUri, result.thumbnail);
            }
        }
    }

    @Nullable
    private Activity getActivity() {
        ImageTaskCallback callback = weakCallback.get();
        if (callback != null) {
            return callback.getActivity();
        }
        return null;
    }

    /**
     * Try to clean image cache
     * @param result Task result
     */
    private void cleanUp(ImageTaskResult result) {
        Activity activity = getActivity();
        if (result != null && activity != null) {
// Path: app/src/main/java/net/fabiszewski/ulogger/ImageHelper.java
// static void clearImageCache(@NonNull Context context) {
//     File dir = context.getCacheDir();
//     clearImages(dir);
// }
// 
// Path: app/src/main/java/net/fabiszewski/ulogger/ImageHelper.java
// static void getPersistablePermission(@NonNull Context context, @NonNull Uri uri) {
//     try {
//         context.getContentResolver().takePersistableUriPermission(uri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
//     } catch (SecurityException e) {
//         if (Logger.DEBUG) { Log.d(TAG, "[getPersistablePermission failed for " + uri + "]"); }
//     }
// }
// 
// Path: app/src/main/java/net/fabiszewski/ulogger/ImageHelper.java
// static Bitmap getResampledBitmap(@NonNull Context context, @NonNull Uri uri, int dstWidth) throws IOException {
//     ContentResolver cr = context.getContentResolver();
//     BitmapFactory.Options options = new BitmapFactory.Options();
//     options.inJustDecodeBounds = true;
//     try (InputStream is = cr.openInputStream(uri)) {
//         BitmapFactory.decodeStream(is, null, options);
//     }
//     int srcWidth = Math.max(options.outWidth, options.outHeight);
//     int scale = srcWidth / dstWidth;
//     if (Logger.DEBUG) { Log.d(TAG, "[resampleIfNeeded scale: " + scale + "]"); }
//     options = new BitmapFactory.Options();
//     Bitmap bitmap = null;
//     boolean retry = false;
//     do {
//         try {
//             if (scale > 1) {
//                 options.inScaled = true;
//                 options.inSampleSize = 1;
//                 options.inDensity = srcWidth;
//                 options.inTargetDensity =  dstWidth * options.inSampleSize;
//             }
//             try (InputStream is = cr.openInputStream(uri)) {
//                 bitmap = BitmapFactory.decodeStream(is, null, options);
//             }
//         } catch (OutOfMemoryError e) {
//             if (Logger.DEBUG) { Log.d(TAG, "[resampleIfNeeded OutOfMemoryError]"); }
//             if (retry) {
//                 throw new IOException("Out of memory");
//             } else if (scale > 1) {
//                 retry = true;
//                 options.inSampleSize = scale;
//                 if (Logger.DEBUG) { Log.d(TAG, "[resampleIfNeeded try sampling]"); }
//             }
//         }
//     } while (retry);
// 
//     if (bitmap == null) {
//         throw new IOException("Failed to decode image");
//     }
// 
//     bitmap = fixImageOrientation(context, uri, bitmap);
//     return bitmap;
// }
// 
// Path: app/src/main/java/net/fabiszewski/ulogger/ImageHelper.java
// static Bitmap getThumbnail(@NonNull Context context, @NonNull Uri uri) throws IOException {
//     int sizePx = getThumbnailSize(context);
//     Bitmap bitmap;
//     ContentResolver cr = context.getContentResolver();
//     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
//         bitmap = cr.loadThumbnail(uri, new Size(sizePx, sizePx), null);
//     } else {
//         try (InputStream is = cr.openInputStream(uri)) {
//             bitmap = BitmapFactory.decodeStream(is, null, null);
//         }
// 
//         bitmap = ThumbnailUtils.extractThumbnail(bitmap, sizePx, sizePx);
//     }
//     bitmap = fixImageOrientation(context, uri, bitmap);
//     return bitmap;
// }
// 
// Path: app/src/main/java/net/fabiszewski/ulogger/ImageHelper.java
// static Uri saveToCache(@NonNull Context context, @NonNull Bitmap bitmap) throws IOException {
//     String filename = getUniqueName() + EXT_JPG;
//     File outFile = new File(context.getCacheDir(), filename);
//     try (FileOutputStream os = new FileOutputStream(outFile)) {
//         bitmap.compress(Bitmap.CompressFormat.JPEG, 90, os);
//     }
//     return Uri.fromFile(outFile);
// }


// Path: app/src/main/java/net/fabiszewski/ulogger/ImageTask.java
import android.app.Activity;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.UiThread;
import androidx.annotation.WorkerThread;
import androidx.preference.PreferenceManager;
import java.io.IOException;
import java.lang.ref.WeakReference;
import static net.fabiszewski.ulogger.ImageHelper.clearImageCache;
import static net.fabiszewski.ulogger.ImageHelper.getPersistablePermission;
import static net.fabiszewski.ulogger.ImageHelper.getResampledBitmap;
import static net.fabiszewski.ulogger.ImageHelper.getThumbnail;
import static net.fabiszewski.ulogger.ImageHelper.saveToCache;

    }

    @UiThread
    private void onPostExecute(@Nullable ImageTaskResult result) {
        ImageTaskCallback callback = weakCallback.get();
        if (callback != null && callback.getActivity() != null) {
            if (result == null) {
                callback.onImageTaskFailure(errorMessage);
            } else {
                callback.onImageTaskCompleted(result.savedUri, result.thumbnail);
            }
        }
    }

    @Nullable
    private Activity getActivity() {
        ImageTaskCallback callback = weakCallback.get();
        if (callback != null) {
            return callback.getActivity();
        }
        return null;
    }

    /**
     * Try to clean image cache
     * @param result Task result
     */
    private void cleanUp(ImageTaskResult result) {
        Activity activity = getActivity();
        if (result != null && activity != null) {
            clearImageCache(activity.getApplicationContext());