/**
 * 
 */
package ti.modules.titanium.android;

import org.appcelerator.kroll.KrollDict;
import org.appcelerator.kroll.KrollProxy;
import org.appcelerator.kroll.annotations.Kroll;
import org.appcelerator.titanium.TiContext;
import org.appcelerator.titanium.util.Log;
import org.appcelerator.titanium.util.TiConfig;
import org.appcelerator.titanium.util.TiConvert;

import android.content.Intent;
import android.net.Uri;

@Kroll.proxy(creatableInModule=AndroidModule.class)
public class IntentProxy extends KrollProxy 
{
	private static final String LCAT = "TiIntent";
	private static boolean DBG = TiConfig.LOGD;
	
	protected Intent intent;
	
	public IntentProxy(TiContext tiContext) 
	{
		super(tiContext);
	}
	
	public void handleCreationDict(KrollDict dict) {
		intent = new Intent();
		
		// See which set of options we have to work with.
		String action = dict.getString("action");
		String data = dict.getString("data");
		String classname = dict.getString("className");
		String type = dict.getString("type");

		if (action != null) {
			if (DBG) {
				Log.d(LCAT, "Setting action: " + action);
			}
			intent.setAction(action);
		}
		
		if (data != null) {
			if (DBG) {
				Log.d(LCAT, "Setting data uri: " + data);
			}
			intent.setData(Uri.parse(data));
		}
		
		if (classname != null) {
			try {
				Class<?> c = getClass().getClassLoader().loadClass(classname);
				intent.setClass(getTiContext().getActivity().getApplicationContext(), c);
			} catch (ClassNotFoundException e) {
				Log.e(LCAT, "Unable to locate class for name: " + classname);
				throw new IllegalStateException("Missing class for name: " + classname, e);
			}
		}
		
		if (type != null) {
			if (DBG) {
				Log.d(LCAT, "Setting type: " + type);
				intent.setType(type);
			} 
		} else {
			if (action != null && action.equals(Intent.ACTION_SEND)) {
				if (DBG) {
					Log.d(LCAT, "Intent type not set, defaulting to text/plain because action is a SEND action");
				}
				intent.setType("text/plain");
			}
		}
	}	
	
	@Kroll.method
	public void putExtra(String key, Object value) 
	{
		if (value instanceof String) {
			intent.putExtra(key, (String) value);
		} else if (value instanceof Boolean) {
			intent.putExtra(key, (Boolean) value);
		} else if (value instanceof Double) {
			intent.putExtra(key, (Double) value);
		} else if (value instanceof Integer) {
			intent.putExtra(key, (Integer) value);
		} else {
			Log.w(LCAT, "Warning unimplemented put conversion for " + value.getClass().getCanonicalName() + " trying String");
			intent.putExtra(key, TiConvert.toString(value));
		}
	}
	
	protected Intent getIntent() { 
		return intent;
	}
}

@@ -968,24 +968,78 @@
 lassName%22);%0A
+%09%09String packageName = dict.getString(%22packageName%22);%0A
 %09%09String typ
@@ -1332,29 +1332,390 @@
 if (
-classname != null) %7B%0A
+packageName != null) %7B%0A%09%09%09if (DBG) %7B%0A%09%09%09%09Log.d(LCAT, %22Setting package: %22 + packageName);%0A%09%09%09%7D%0A%09%09%09intent.setPackage(packageName);%0A%09%09%7D%0A%09%09%0A%09%09if (classname != null) %7B%0A%09%09%09if (packageName != null) %7B%0A%09%09%09%09if (DBG) %7B%0A%09%09%09%09%09Log.d(LCAT, %22Both className and packageName set, using intent.setClassName(packageName, className%22);%0A%09%09%09%09%7D%0A%09%09%09%09intent.setClassName(packageName, classname);%0A%09%09%09%7D else %7B%0A%09
 %09%09%09t
@@ -1723,16 +1723,17 @@
 y %7B%0A%09%09%09%09
+%09
 Class%3C?%3E
@@ -1783,24 +1783,25 @@
 classname);%0A
+%09
 %09%09%09%09intent.s
@@ -1866,16 +1866,17 @@
 (), c);%0A
+%09
 %09%09%09%7D cat
@@ -1907,24 +1907,25 @@
 on e) %7B%0A%09%09%09%09
+%09
 Log.e(LCAT, 
@@ -1970,24 +1970,25 @@
 classname);%0A
+%09
 %09%09%09%09throw ne
@@ -2051,24 +2051,30 @@
 ssname, e);%0A
+%09%09%09%09%7D%0A
 %09%09%09%7D%0A%09%09%7D%0A%09%09%0A

Support packageName argument