package com.vaguehope.onosendai.images;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import android.database.Cursor;
import android.database.MatrixCursor;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.ParcelFileDescriptor;
import android.provider.OpenableColumns;
import android.support.v4.content.FileProvider;
import android.webkit.MimeTypeMap;

import com.vaguehope.onosendai.util.LogWrapper;

public class CachedImageFileProvider extends FileProvider {

	private static final LogWrapper LOG = new LogWrapper("CIFP");

	public CachedImageFileProvider () {}

	public static List<File> addFileExtensions (final List<File> input) {
		final List<File> ret = new ArrayList<File>();
		for (final File file : input) {
			ret.add(new File(String.format("%s.%s", file.getAbsolutePath(), identifyFileExtension(file))));
		}
		return ret;
	}

	public static String identifyFileExtension (final File file) {
		final BitmapFactory.Options options = new BitmapFactory.Options();
		options.inJustDecodeBounds = true;
		BitmapFactory.decodeFile(file.getAbsolutePath(), options);
		return MimeTypeMap.getSingleton().getExtensionFromMimeType(options.outMimeType);
	}

	public static Uri removeExtension (final Uri uri) {
		return uri.buildUpon().path(removeExtension(uri.getPath())).build();
	}

	private static String removeExtension (final String path) {
		final int dot = path.lastIndexOf(".");
		final int slash = path.lastIndexOf("/");
		if (dot > slash && dot < path.length() - 1) return path.substring(0, dot);
		return path;
	}

	private static String getExtension (final String path) {
		final int dot = path.lastIndexOf(".");
		final int slash = path.lastIndexOf("/");
		if (dot > slash && dot < path.length() - 1) return path.substring(dot + 1);
		return path;
	}

	@Override
	public Cursor query (final Uri uri, final String[] projection, final String selection, final String[] selectionArgs, final String sortOrder) {
		LOG.i("query: %s, %s, %s, %s, %s", uri, Arrays.toString(projection), selection, selectionArgs, sortOrder);
		final MatrixCursor inputCursor = (MatrixCursor) super.query(removeExtension(uri), projection, selection, selectionArgs, sortOrder);

		// This mess to clone the single entry cursor and append file extensions to display name.
		final String dotExtension = "." + getExtension(uri.getPath());
		final String[] cols = new String[projection.length];
		final Object[] values = new Object[projection.length];
		inputCursor.moveToFirst();
		int i = 0;
		for (int colIndex = 0; colIndex < inputCursor.getColumnCount(); colIndex++) {
			final String colName = inputCursor.getColumnName(colIndex);
			if (OpenableColumns.DISPLAY_NAME.equals(colName)) {
				cols[i] = OpenableColumns.DISPLAY_NAME;
				values[i] = inputCursor.getString(colIndex) + dotExtension;
				i++;
			}
			else if (OpenableColumns.SIZE.equals(colName)) {
				cols[i] = OpenableColumns.SIZE;
				values[i] = inputCursor.getLong(colIndex);
				i++;
			}
		}
		LOG.i("result: %s, %s", Arrays.toString(cols), Arrays.toString(values));
		final MatrixCursor updatedCursor = new MatrixCursor(cols, 1);
		updatedCursor.addRow(values);
		return updatedCursor;
	}

	@Override
	public String getType (final Uri uri) {
		return super.getType(removeExtension(uri));
	}

	@Override
	public ParcelFileDescriptor openFile (final Uri uri, final String mode) throws FileNotFoundException {
		LOG.i("openFile: %s", uri);
		return super.openFile(removeExtension(uri), mode);
	}

}

@@ -339,24 +339,73 @@
 Descriptor;%0A
+import android.provider.MediaStore.MediaColumns;%0A
 import andro
@@ -425,32 +425,32 @@
 penableColumns;%0A
-
 import android.s
@@ -1064,24 +1064,181 @@
 ile file) %7B%0A
+%09%09return MimeTypeMap.getSingleton().getExtensionFromMimeType(identifyFileMimeType(file));%0A%09%7D%0A%0A%09public static String identifyFileMimeType (final File file) %7B%0A
 %09%09final Bitm
@@ -1405,60 +1405,8 @@
 urn 
-MimeTypeMap.getSingleton().getExtensionFromMimeType(
 opti
@@ -1420,17 +1420,16 @@
 MimeType
-)
 ;%0A%09%7D%0A%0A%09p
@@ -2755,77 +2755,41 @@
 or (
+f
 in
-t colIndex = 0; colIndex %3C inputCursor.getColumnCount(); colIndex++
+al String colName : projection
 ) %7B%0A
@@ -2797,30 +2797,28 @@
 %09%09final 
-Str
 in
-g
+t
  col
-Name
+Index
  = input
@@ -2837,31 +2837,48 @@
 lumn
-Name(colIndex);%0A%09%09%09if (
+Index(colName);%0A%09%09%09if (colIndex %3E= 0 && 
 Open
@@ -3055,16 +3055,33 @@
 lse if (
+colIndex %3E= 0 && 
 Openable
@@ -3209,16 +3209,152 @@
 +;%0A%09%09%09%7D%0A
+%09%09%09else if (MediaColumns.MIME_TYPE.equals(colName)) %7B%0A%09%09%09%09cols%5Bi%5D = MediaColumns.MIME_TYPE;%0A%09%09%09%09values%5Bi%5D = getType(uri);%0A%09%09%09%09i++;%0A%09%09%09%7D%0A
 %09%09%7D%0A%09%09LO
@@ -3606,50 +3606,183 @@
 %7B%0A%09%09
-return super.getType(removeExtension(uri))
+// Cheat as file does not have to exist for super class to identify by extension.%0A%09%09final String type = super.getType(uri);%0A%09%09LOG.i(%22getType(%25s)=%25s%22, uri, type);%0A%09%09return type
 ;%0A%09%7D

Fix identifying mime-type. [issue #122]