// Path: libK3b/src/main/java/de/k3b/io/FileNameUtil.java
// public class FileNameUtil {
// 
//     /**
//      * converts baseName to a valid filename by replacing illegal chars.
//      * If it has no file-extension then defaultExtension is added
//      *
//      * @param defaultExtension the new extension, excluding the dot. null means no extension.
//      */
//     public static String createFileName(String baseName, String defaultExtension) {
//         StringBuilder result = new StringBuilder(baseName);
// 
//         // remove trailing "."
//         int len = result.length();
//         while ((len > 0) && (result.charAt(len - 1) == '.')) {
//             result.deleteCharAt(len - 1);
//             len--;
//         }
// 
//         // remove leading "."
//         while ((len > 0) && (result.charAt(0) == '.')) {
//             result.deleteCharAt(0);
//             len--;
//         }
// 
//         // add extension if there is none
//         if ((defaultExtension != null) && (result.indexOf(".") < 0)) {
//             result.append(".").append(defaultExtension);
//         }
// 
//         // replace illegal chars with "_"
//         replace(result, "_", "/", "\\", ":", " ", "?", "*", "&", "%", ">", "<", "|", "'", "\"", "__");
//         return result.toString();
//     }
// 
//     /**
//      * converts baseName to a valid filename by replacing illegal chars.
//      */
//     public static String createFileNameWitoutExtension(String fileNameCandidate) {
//         if (fileNameCandidate != null) {
//             // remove illegal chars or file extension
//             return FileNameUtil.createFileName(FileUtils.replaceExtension(fileNameCandidate, ""), null);
//         }
//         return null;
//     }
// 
//     public static String getWithoutWildcard(String path) {
//         if ((path == null) || (path.length() == 0)) return null;
//         if (path.endsWith("%")) {
//             // remove sql wildcard at end of name
//             return path.substring(0, path.length() - 1);
//         }
//         return path;
//     }
// 
//     /**
//      * converts filePath to a valid path by removing potential sql-wildcards and android specigic "//"
//      */
//     public static String fixPath(String filePath) {
//         String path = FileNameUtil.getWithoutWildcard(FileUtils.fixPath(filePath));
//         return path;
//     }
// 
//     /** replaces all occurences of illegalValues in result by replacement */
//     private static void replace(StringBuilder result, String replacement, String ... illegalValues) {
//         for (String illegalValue : illegalValues) {
//             int found = result.indexOf(illegalValue);
//             while (found >= 0) {
//                 result.replace(found, found+illegalValue.length(), replacement);
//                 found = result.indexOf(illegalValue);
//             }
//         }
//     }
// 
// 
//     /**
//      * so that files are comparable
//      */
//     public static String getCanonicalPath(File zipRelPath) {
//         File canonicalFile = FileUtils.tryGetCanonicalFile(zipRelPath);
//         if (canonicalFile != null) {
//             return FileUtils.fixPath(canonicalFile.getAbsolutePath());
//         }
//         return null;
//     }
// 
//     /**
//      * @return either srcFile without leading relativeToPath or null if srcFile is not relative to relativeToPath
//      */
//     public static String makePathRelative(String relativeToPath, File srcFile) {
//         String result = null;
//         if (!StringUtils.isNullOrEmpty(relativeToPath)) {
//             String srcPath = getCanonicalPath(srcFile);
//             boolean match = srcPath.toLowerCase().startsWith(relativeToPath);
//             if (match) {
//                 result = srcPath.substring(relativeToPath.length() + 1);
//             }
//         }
//         return result;
//     }
// }


import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;
import android.webkit.MimeTypeMap;
import de.k3b.io.FileNameUtil;

    /*
    Input: URI -- something like content://com.example.app.provider/table2/dataset1
    Output: PATH -- something like /sdcard/DCIM/123242-image.jpg
    */
    public static String convertMediaUriToPath(Context context, Uri uri) {
        return getString(context, uri, MediaStore.Images.Media.DATA);
    }

    /*
    Input: URI -- something like content://com.example.app.provider/table2/dataset1
    Output: 0 or date
    */
    public static long getDateModified(Context context, Uri uri) {
        return getLong(context, uri, MediaStore.Images.Media.DATE_MODIFIED);
    }

    /*
    Input: URI -- something like content://com.example.app.provider/table2/dataset1
    Output: null or filename (with or without path)
    */
    public static String getFileName(Context context, Uri uri, String mimeType) {
        String baseName = getString(context, uri, MediaStore.Images.Media.DISPLAY_NAME);
        if ((baseName == null) || (baseName.length() == 0)) {
            baseName = uri.getLastPathSegment();

            // "content://com.mediatek.calendarimporter/1282" becomes "calendarimporter_1282"
        }
        String defaultFileExtension = (mimeType != null) ? MimeTypeMap.getSingleton().getExtensionFromMimeType(mimeType) : null;

// Path: libK3b/src/main/java/de/k3b/io/FileNameUtil.java
// public class FileNameUtil {
// 
//     /**
//      * converts baseName to a valid filename by replacing illegal chars.
//      * If it has no file-extension then defaultExtension is added
//      *
//      * @param defaultExtension the new extension, excluding the dot. null means no extension.
//      */
//     public static String createFileName(String baseName, String defaultExtension) {
//         StringBuilder result = new StringBuilder(baseName);
// 
//         // remove trailing "."
//         int len = result.length();
//         while ((len > 0) && (result.charAt(len - 1) == '.')) {
//             result.deleteCharAt(len - 1);
//             len--;
//         }
// 
//         // remove leading "."
//         while ((len > 0) && (result.charAt(0) == '.')) {
//             result.deleteCharAt(0);
//             len--;
//         }
// 
//         // add extension if there is none
//         if ((defaultExtension != null) && (result.indexOf(".") < 0)) {
//             result.append(".").append(defaultExtension);
//         }
// 
//         // replace illegal chars with "_"
//         replace(result, "_", "/", "\\", ":", " ", "?", "*", "&", "%", ">", "<", "|", "'", "\"", "__");
//         return result.toString();
//     }
// 
//     /**
//      * converts baseName to a valid filename by replacing illegal chars.
//      */
//     public static String createFileNameWitoutExtension(String fileNameCandidate) {
//         if (fileNameCandidate != null) {
//             // remove illegal chars or file extension
//             return FileNameUtil.createFileName(FileUtils.replaceExtension(fileNameCandidate, ""), null);
//         }
//         return null;
//     }
// 
//     public static String getWithoutWildcard(String path) {
//         if ((path == null) || (path.length() == 0)) return null;
//         if (path.endsWith("%")) {
//             // remove sql wildcard at end of name
//             return path.substring(0, path.length() - 1);
//         }
//         return path;
//     }
// 
//     /**
//      * converts filePath to a valid path by removing potential sql-wildcards and android specigic "//"
//      */
//     public static String fixPath(String filePath) {
//         String path = FileNameUtil.getWithoutWildcard(FileUtils.fixPath(filePath));
//         return path;
//     }
// 
//     /** replaces all occurences of illegalValues in result by replacement */
//     private static void replace(StringBuilder result, String replacement, String ... illegalValues) {
//         for (String illegalValue : illegalValues) {
//             int found = result.indexOf(illegalValue);
//             while (found >= 0) {
//                 result.replace(found, found+illegalValue.length(), replacement);
//                 found = result.indexOf(illegalValue);
//             }
//         }
//     }
// 
// 
//     /**
//      * so that files are comparable
//      */
//     public static String getCanonicalPath(File zipRelPath) {
//         File canonicalFile = FileUtils.tryGetCanonicalFile(zipRelPath);
//         if (canonicalFile != null) {
//             return FileUtils.fixPath(canonicalFile.getAbsolutePath());
//         }
//         return null;
//     }
// 
//     /**
//      * @return either srcFile without leading relativeToPath or null if srcFile is not relative to relativeToPath
//      */
//     public static String makePathRelative(String relativeToPath, File srcFile) {
//         String result = null;
//         if (!StringUtils.isNullOrEmpty(relativeToPath)) {
//             String srcPath = getCanonicalPath(srcFile);
//             boolean match = srcPath.toLowerCase().startsWith(relativeToPath);
//             if (match) {
//                 result = srcPath.substring(relativeToPath.length() + 1);
//             }
//         }
//         return result;
//     }
// }


// Path: libK3bAndroidZip/src/main/java/de/k3b/android/MediaUtil.java
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;
import android.webkit.MimeTypeMap;
import de.k3b.io.FileNameUtil;


    /*
    Input: URI -- something like content://com.example.app.provider/table2/dataset1
    Output: PATH -- something like /sdcard/DCIM/123242-image.jpg
    */
    public static String convertMediaUriToPath(Context context, Uri uri) {
        return getString(context, uri, MediaStore.Images.Media.DATA);
    }

    /*
    Input: URI -- something like content://com.example.app.provider/table2/dataset1
    Output: 0 or date
    */
    public static long getDateModified(Context context, Uri uri) {
        return getLong(context, uri, MediaStore.Images.Media.DATE_MODIFIED);
    }

    /*
    Input: URI -- something like content://com.example.app.provider/table2/dataset1
    Output: null or filename (with or without path)
    */
    public static String getFileName(Context context, Uri uri, String mimeType) {
        String baseName = getString(context, uri, MediaStore.Images.Media.DISPLAY_NAME);
        if ((baseName == null) || (baseName.length() == 0)) {
            baseName = uri.getLastPathSegment();

            // "content://com.mediatek.calendarimporter/1282" becomes "calendarimporter_1282"
        }
        String defaultFileExtension = (mimeType != null) ? MimeTypeMap.getSingleton().getExtensionFromMimeType(mimeType) : null;

        return FileNameUtil.createFileName(baseName, defaultFileExtension);