package org.appcelerator.titanium.view;

import org.appcelerator.kroll.common.TiMessenger;
import org.appcelerator.titanium.proxy.TiViewProxy;
import org.appcelerator.titanium.view.TiCompositeLayout;
import org.appcelerator.titanium.view.TiUIView;

import android.app.Activity;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

public abstract class TiUIFragment extends TiUIView implements Handler.Callback {
	private static int viewId = 1000;

	private Fragment fragment;
	
	private Handler handler;

	public TiUIFragment(TiViewProxy proxy, Activity activity) {
		super(proxy);

		TiCompositeLayout container = new TiCompositeLayout(activity, proxy);
		container.setId(viewId++);
		setNativeView(container);

		FragmentManager manager = ((FragmentActivity) activity).getSupportFragmentManager();
		FragmentTransaction transaction = manager.beginTransaction();
		fragment = createFragment();
		transaction.add(container.getId(), fragment);
		transaction.commit();
		
		//initialize handler
		handler = new Handler(TiMessenger.getMainMessenger().getLooper(), this);
		//send a msg to skip a cycle to make sure the fragment's view is created and initialized
		handler.obtainMessage().sendToTarget();

	}

	public Fragment getFragment() {
		return fragment;
	}

	public boolean handleMessage(Message msg) {
		//we know here that the view is available, so we can process properties
		onViewCreated();
		return true;
	}
	
	protected abstract void onViewCreated();
	protected abstract Fragment createFragment();
}
@@ -139,113 +139,8 @@
 oxy;
-%0Aimport org.appcelerator.titanium.view.TiCompositeLayout;%0Aimport org.appcelerator.titanium.view.TiUIView;
 %0A%0Aim
@@ -405,16 +405,49 @@
 saction;
+%0Aimport android.view.MotionEvent;
 %0A%0Apublic
@@ -519,17 +519,17 @@
 Callback
- 
+%0A
 %7B%0A%09priva
@@ -586,17 +586,16 @@
 agment;%0A
-%09
 %0A%09privat
@@ -672,17 +672,18 @@
 ctivity)
- 
+%0A%09
 %7B%0A%09%09supe
@@ -763,16 +763,168 @@
 , proxy)
+%0A%09%09%7B%0A%09%09%09@Override%0A%09%09%09public boolean dispatchTouchEvent(MotionEvent ev)%0A%09%09%09%7B%0A%09%09%09%09return interceptTouchEvent(ev) %7C%7C super.dispatchTouchEvent(ev);%0A%09%09%09%7D%0A%09%09%7D
 ;%0A%09%09cont
@@ -1233,15 +1233,14 @@
 ();%0A
-%09%09
 %0A%09%09//
+ 
 init
@@ -1333,16 +1333,17 @@
 s);%0A%09%09//
+ 
 send a m
@@ -1463,17 +1463,16 @@
 rget();%0A
-%0A
 %09%7D%0A%0A%09pub
@@ -1497,17 +1497,18 @@
 agment()
- 
+%0A%09
 %7B%0A%09%09retu
@@ -1570,15 +1570,17 @@
 msg)
- 
+%0A%09
 %7B%0A%09%09//
+ 
 we k
@@ -1682,17 +1682,94 @@
 rue;%0A%09%7D%0A
-%09
+%0A%09protected boolean interceptTouchEvent(MotionEvent ev)%0A%09%7B%0A%09%09return false;%0A%09%7D%0A
 %0A%09protec
@@ -1803,16 +1803,17 @@
 ated();%0A
+%0A
 %09protect
@@ -1852,8 +1852,9 @@
 ent();%0A%7D
+%0A

add interceptTouchEvent() so we can intercept the touch event from the map.