package org.bbs.apklauncher;
import java.util.ArrayList;
import java.util.List;

import org.bbs.apklauncher.emb.IntentHelper;
import org.bbs.apklauncher.emb.LoadedApk;
import org.bbs.apklauncher.emb.auto_gen.Stub_Activity;
import org.bbs.apkparser.ApkManifestParser;
import org.bbs.apkparser.ApkManifestParser.PackageInfoX;
import org.bbs.apkparser.ApkManifestParser.PackageInfoX.ActivityInfoX;
import org.bbs.felixonandroid.R;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import dalvik.system.DexClassLoader;

public class ApkLuncherActivity extends Activity {
	private static final String TAG = ApkLuncherActivity.class.getSimpleName();
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.activity_apk_launcher);
		
		InstalledAPks apks = InstalledAPks.getInstance();
		ListAdapter adapter = new ArrayAdapter<PackageInfoX.ActivityInfoX>(this, android.R.layout.simple_list_item_1, parseLauncher(apks.getAllApks())){
			@Override
			public View getView(int position, View convertView, ViewGroup parent) {
				View v =  super.getView(position, convertView, parent);
				
				v.setTag(getItem(position));
				v.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						PackageInfoX.ActivityInfoX a = (ActivityInfoX) v.getTag();
						
						Log.d(TAG, "onClick. activity: " + a);

						ClassLoader cl = new DexClassLoader(a.applicationInfo.publicSourceDir, getDir("tmp", 0).getPath(), null, getClassLoader());
						String superClassName = LoadedApk.getActivitySuperClassName(cl, a.name);
						Intent launcher = new Intent();

		                // inject and replace with our component.
						String comClassName = superClassName.replace("Target", "Stub");
						ComponentName com= new ComponentName(getPackageName(), comClassName);
						launcher.setComponent(com);
						launcher.putExtra(Stub_Activity.EXTRA_COMPONENT_CLASS_NAME, a.name);
//						putExtra(a, launcher);
						
						launcher.putExtra(IntentHelper.EXTRA_INJECT, false);
						startActivity(launcher);
					}
				});
				return v ;
			}
		};
		((ListView)findViewById(R.id.apk_container)).setAdapter(adapter);;
	}	

	private List<PackageInfoX.ActivityInfoX> parseLauncher(List<PackageInfoX> ms) {
		List<PackageInfoX.ActivityInfoX> launchers = new ArrayList<ApkManifestParser.PackageInfoX.ActivityInfoX>();
		for (PackageInfoX m : ms) {
			
			if (m.activities != null) {
				for (ActivityInfo a : m.activities) {
					PackageInfoX.ActivityInfoX aX = (ActivityInfoX) a;
					if (aX.mIntents != null) {
						for (PackageInfoX.IntentInfoX i : aX.mIntents) {
							if (i.hasAction(IntentHelper.ACTION_MAIN) && i.hasCategory(IntentHelper.CATEGORY_LAUNCHER)) {
								launchers.add(aX);
								break;
							}
						}
					}
				}
			}
		}
		return launchers;
	}

	public static  void putExtra(PackageInfoX.ActivityInfoX a,
			IntentHelper launcher) {
		launcher.putExtra(Stub_Activity.EXTRA_COMPONENT_CLASS_NAME, new ComponentName(a.packageName, a.name));
	}
	
}

@@ -710,16 +710,106 @@
 wGroup;%0A
+import android.widget.AdapterView;%0Aimport android.widget.AdapterView.OnItemClickListener;%0A
 import a
@@ -2638,16 +2638,1018 @@
 pter);;%0A
+%09%09((ListView)findViewById(R.id.apk_container)).setOnItemClickListener(new OnItemClickListener() %7B%0A%0A%09%09%09@Override%0A%09%09%09public void onItemClick(AdapterView%3C?%3E parent, View view,%0A%09%09%09%09%09int position, long id) %7B%0A%0A%09%09%09%09PackageInfoX.ActivityInfoX a = (ActivityInfoX) view.getTag();%0A%09%09%09%09%0A%09%09%09%09Log.d(TAG, %22onClick. activity: %22 + a);%0A%0A%09%09%09%09ClassLoader cl = new DexClassLoader(a.applicationInfo.publicSourceDir, getDir(%22tmp%22, 0).getPath(), null, getClassLoader());%0A%09%09%09%09String superClassName = LoadedApk.getActivitySuperClassName(cl, a.name);%0A%09%09%09%09Intent launcher = new Intent();%0A%0A                // inject and replace with our component.%0A%09%09%09%09String comClassName = superClassName.replace(%22Target%22, %22Stub%22);%0A%09%09%09%09ComponentName com= new ComponentName(getPackageName(), comClassName);%0A%09%09%09%09launcher.setComponent(com);%0A%09%09%09%09launcher.putExtra(Stub_Activity.EXTRA_COMPONENT_CLASS_NAME, a.name);%0A//%09%09%09%09putExtra(a, launcher);%0A%09%09%09%09%0A%09%09%09%09launcher.putExtra(IntentHelper.EXTRA_INJECT, false);%0A%09%09%09%09startActivity(launcher);%0A%09%09%09%09%0A%09%09%09%7D%0A%09%09%7D);%0A
 %09%7D%09%0A%0A%09pr

add onItemClickListener.