package com.esafirm.imagepicker.helper;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.os.Environment;
import androidx.annotation.Nullable;

import com.esafirm.imagepicker.features.ImagePickerSavePath;
import com.esafirm.imagepicker.model.Image;

import java.io.File;
import java.io.IOException;
import java.net.URLConnection;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

public class ImagePickerUtils {

    public static boolean isStringEmpty(@Nullable String str) {
        return str == null || str.length() == 0;
    }

    public static File createImageFile(ImagePickerSavePath savePath) {
        // External sdcard location
        final String path = savePath.getPath();
        File mediaStorageDir = savePath.isFullPath()
                ? new File(path)
                : new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), path);

        // Create the storage directory if it does not exist
        if (!mediaStorageDir.exists()) {
            if (!mediaStorageDir.mkdirs()) {
                IpLogger.getInstance().d("Oops! Failed create " + path);
                return null;
            }
        }

        // Create a media file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date());
        String imageFileName = "IMG_" + timeStamp;

        File imageFile = null;
        try {
            imageFile = File.createTempFile(imageFileName, ".jpg", mediaStorageDir);
        } catch (IOException e) {
            IpLogger.getInstance().d("Oops! Failed create " + imageFileName + " file");
        }
        return imageFile;
    }

    public static String getNameFromFilePath(String path) {
        if (path.contains(File.separator)) {
            return path.substring(path.lastIndexOf(File.separator) + 1);
        }
        return path;
    }

    public static void grantAppPermission(Context context, Intent intent, Uri fileUri) {
        List<ResolveInfo> resolvedIntentActivities = context.getPackageManager()
                .queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);

        for (ResolveInfo resolvedIntentInfo : resolvedIntentActivities) {
            String packageName = resolvedIntentInfo.activityInfo.packageName;
            context.grantUriPermission(packageName, fileUri,
                    Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
        }
    }

    public static void revokeAppPermission(Context context, Uri fileUri) {
        context.revokeUriPermission(fileUri,
                Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
    }

    public static boolean isGifFormat(Image image) {
        String extension = image.getPath().substring(image.getPath().lastIndexOf(".") + 1, image.getPath().length());
        return extension.equalsIgnoreCase("gif");
    }

    public static boolean isVideoFormat(Image image) {
        String mimeType = URLConnection.guessContentTypeFromName(image.getPath());
        return mimeType != null && mimeType.startsWith("video");
    }
}

@@ -251,29 +251,58 @@
 roid
-x.annotation.Nullable
+.text.TextUtils;%0Aimport android.webkit.MimeTypeMap
 ;%0A%0Ai
@@ -593,16 +593,54 @@
 ocale;%0A%0A
+import androidx.annotation.Nullable;%0A%0A
 public c
@@ -3013,72 +3013,21 @@
 n = 
-image.getPath().substring(image.getPath().lastIndexOf(%22.%22) + 1, 
+getExtension(
 imag
@@ -3037,25 +3037,16 @@
 etPath()
-.length()
 );%0A     
@@ -3171,140 +3171,704 @@
 ing 
-mimeType = URLConnection.guessContentTypeFromName(image.getPath());%0A        return mimeType != null && mimeType.startsWith(%22video%22);
+extension = getExtension(image.getPath());%0A        String mimeType = TextUtils.isEmpty(extension)%0A                ? URLConnection.guessContentTypeFromName(image.getPath())%0A                : MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);%0A        return mimeType != null && mimeType.startsWith(%22video%22);%0A%0A    %7D%0A%0A    private static String getExtension(String path) %7B%0A        String extension = MimeTypeMap.getFileExtensionFromUrl(path);%0A        if (!TextUtils.isEmpty(extension)) %7B%0A            return extension;%0A        %7D%0A        if (path.contains(%22.%22)) %7B%0A            return path.substring(path.lastIndexOf(%22.%22) + 1, path.length());%0A        %7D else %7B%0A            return %22%22;%0A        %7D
 %0A   

Fix crash on extracting mime type (#182)