package com.esafirm.imagepicker.features.camera;

import android.content.Context;
import android.content.Intent;
import android.media.MediaScannerConnection;
import android.net.Uri;
import android.provider.MediaStore;

import com.esafirm.imagepicker.features.ImagePickerConfigFactory;
import com.esafirm.imagepicker.features.common.BaseConfig;
import com.esafirm.imagepicker.helper.ImagePickerUtils;
import com.esafirm.imagepicker.helper.IpLogger;
import com.esafirm.imagepicker.model.ImageFactory;

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

import androidx.core.content.FileProvider;

public class DefaultCameraModule implements CameraModule, Serializable {

    private String currentImagePath;

    public Intent getCameraIntent(Context context) {
        return getCameraIntent(context, ImagePickerConfigFactory.createDefault(context));
    }

    @Override
    public Intent getCameraIntent(Context context, BaseConfig config) {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        File imageFile = ImagePickerUtils.createImageFile(config.getImageDirectory(), context);
        if (imageFile != null) {
            Context appContext = context.getApplicationContext();
            String providerName = String.format(Locale.ENGLISH, "%s%s", appContext.getPackageName(), ".imagepicker.provider");
            Uri uri = FileProvider.getUriForFile(appContext, providerName, imageFile);
            currentImagePath = "file:" + imageFile.getAbsolutePath();
            intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);

            ImagePickerUtils.grantAppPermission(context, intent, uri);

            return intent;
        }
        return null;
    }

    @Override
    public void getImage(final Context context, Intent intent, final OnImageReadyListener imageReadyListener) {
        if (imageReadyListener == null) {
            throw new IllegalStateException("OnImageReadyListener must not be null");
        }

        if (currentImagePath == null) {
            IpLogger.getInstance().w("currentImagePath null. " +
                    "This happen if you haven't call #getCameraIntent() or the activity is being recreated");
            imageReadyListener.onImageReady(null);
            return;
        }

        final Uri imageUri = Uri.parse(currentImagePath);
        if (imageUri != null) {
            MediaScannerConnection.scanFile(context.getApplicationContext(),
                    new String[]{imageUri.getPath()}, null, (path, uri) -> {

                        IpLogger.getInstance().d("File " + path + " was scanned successfully: " + uri);

                        if (path == null) {
                            IpLogger.getInstance().d("This should not happen, go back to Immediate implemenation");
                            path = currentImagePath;
                        }

                        imageReadyListener.onImageReady(ImageFactory.singleListFromPath(path));
                        ImagePickerUtils.revokeAppPermission(context, imageUri);
                    });
        }
    }

    @Override
    public void removeImage() {
        if (currentImagePath != null) {
            File file = new File(currentImagePath);
            if (file.exists()) {
                file.delete();
            }
        }
    }
}

@@ -43,16 +43,94 @@
 amera;%0A%0A
+import android.content.ContentResolver;%0Aimport android.content.ContentValues;%0A
 import a
@@ -253,16 +253,41 @@
 et.Uri;%0A
+import android.os.Build;%0A
 import a
@@ -1229,16 +1229,625 @@
 ntext);%0A
+%0A        if(Build.VERSION.SDK_INT %3E= Build.VERSION_CODES.Q)%7B%0A            ContentValues values = new ContentValues();%0A            values.put(MediaStore.Images.Media.DISPLAY_NAME, imageFile.getName());%0A            values.put(MediaStore.Images.Media.MIME_TYPE, %22image/jpeg%22);%0A            ContentResolver resolver = context.getContentResolver();%0A            Uri collection = MediaStore.Images.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY);%0A            Uri uri = resolver.insert(collection,values);%0A            intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);%0A            return intent;%0A        %7Delse%7B%0A    
         
@@ -1863,32 +1863,36 @@
 File != null) %7B%0A
+    
             Cont
@@ -1953,16 +1953,20 @@
         
+  
+  
 String p
@@ -2068,24 +2068,28 @@
 provider%22);%0A
+    
             
@@ -2175,16 +2175,20 @@
         
+    
 currentI
@@ -2241,32 +2241,36 @@
 ();%0A            
+ 
+   
 intent.putExtra(
@@ -2293,32 +2293,36 @@
 _OUTPUT, uri);%0A%0A
+    
             Imag
@@ -2381,32 +2381,36 @@
 );%0A%0A            
+   
+ 
 return intent;%0A 
@@ -2408,34 +2408,43 @@
 intent;%0A        
-%7D%0A
+    %7D%0A%0A    
         return n
@@ -2448,16 +2448,26 @@
 n null;%0A
+        %7D%0A
     %7D%0A%0A 

fix for https://github.com/esafirm/android-image-picker/issues/280 (#290)