/**
 * Appcelerator Titanium Mobile
 * Copyright (c) 2009-2010 by Appcelerator, Inc. All Rights Reserved.
 * Licensed under the terms of the Apache Public License
 * Please see the LICENSE included with this distribution for details.
 */

package org.appcelerator.titanium.io;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.appcelerator.titanium.TiApplication;

import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;

public class TitaniumBlob extends TiBaseFile
{
	protected String url;
	protected String name;
	protected String path;

	public TitaniumBlob(String url) {
		super(TYPE_BLOB);
		this.url = url;
		if (url != null) {
			init();
		}
	}

	protected void init() {
		String[] projection = {
			MediaStore.Images.ImageColumns.DISPLAY_NAME,
			MediaStore.Images.ImageColumns.DATA
		};
		Cursor c = null;

		if (url.startsWith("content://com.android.providers.media.documents")) {
			try {
				c = TiApplication.getInstance().getContentResolver().query(Uri.parse(url), null, null, null, null);
				c.moveToFirst();
				String id = c.getString(0);
				id = id.substring(id.lastIndexOf(":") + 1);
				c.close();

				c = TiApplication.getInstance().getContentResolver().query(
					android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
					projection, MediaStore.Images.Media._ID + " = ? ", new String[]{id}, null);

				if (c.moveToNext()) {
					name = c.getString(0);
					path = c.getString(1);
				}
			} finally {
				if (c != null) {
					c.close();
				}
			}
		} else {
			try {
				c = TiApplication.getInstance().getContentResolver().query(Uri.parse(url), projection, null, null, null);

				if (c.moveToNext()) {
					name = c.getString(0);
					path = c.getString(1);
				}
			} finally {
				if (c != null) {
					c.close();
				}
			}
		}
	}

	public void setUrl(String url) {
		this.url = url;
		if (url != null) {
			init();
		}
	}

	@Override
	public String nativePath() {
		return url;
	}

	public String toURL() {
		return url;
	}

	@Override
	public String name() {
		return name;
	}

	public File getFile() {
		return new File(path);
	}

	public String getContentType() {
		return TiApplication.getInstance().getContentResolver().getType(Uri.parse(url));
	}

	public InputStream getInputStream()
		throws IOException
	{
		return TiApplication.getInstance().getContentResolver().openInputStream(Uri.parse(url));
	}

	@Override
	public OutputStream getOutputStream() throws IOException {
		return null;
	}

	@Override
	public File getNativeFile() {
		return new File(path);
	}

	public String getNativePath() {
		return path;
	}
}

@@ -427,16 +427,52 @@
 ation;%0A%0A
+import android.content.ContentUris;%0A
 import a
@@ -519,16 +519,59 @@
 et.Uri;%0A
+import android.provider.DocumentsContract;%0A
 import a
@@ -1505,32 +1505,555 @@
 %5B%5D%7Bid%7D, null);%0A%0A
+%09%09%09%09if (c.moveToNext()) %7B%0A%09%09%09%09%09name = c.getString(0);%0A%09%09%09%09%09path = c.getString(1);%0A%09%09%09%09%7D%0A%09%09%09%7D finally %7B%0A%09%09%09%09if (c != null) %7B%0A%09%09%09%09%09c.close();%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%09%09%7D  else if (url.startsWith(%22content://com.android.providers.downloads.documents%22)) %7B%0A%09%09%09try %7B%0A%09%09%09%09String id = DocumentsContract.getDocumentId(Uri.parse(url));%0A%09%09%09%09Uri uri = ContentUris.withAppendedId(Uri.parse(%22content://downloads/public_downloads%22), Long.valueOf(id));%0A%09%09%09%09c = TiApplication.getInstance().getContentResolver().query(uri, projection, null, null, null);%0A%0A
 %09%09%09%09if (c.moveTo

Add support for com.android.providers.downloads.documents in TiBlob