package com.example.pluginmain;

import java.io.IOException;
import java.io.InputStream;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map.Entry;

import android.app.Activity;
import android.app.Application;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
import android.widget.LinearLayout.LayoutParams;

import com.plugin.core.PluginDescriptor;
import com.plugin.core.PluginLoader;
import com.plugin.core.ui.PluginDispatcher;
import com.plugin.util.ApkReader;
import com.plugin.util.RefInvoker;

import dalvik.system.DexClassLoader;

public class PluginListActivity extends Activity {

	private ViewGroup mList;
	private Button install;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_activity);

		// 这行代码应当在Application的onCreate中执行。
		PluginLoader.initLoader(getApplication());

		registerReceiver(pluginChange, new IntentFilter(PluginLoader.ACTION_PLUGIN_CHANGED));

		install = (Button) findViewById(R.id.install);
		install.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				Toast.makeText(PluginListActivity.this, "开始安装", Toast.LENGTH_LONG).show();
				try {
					InputStream assestInput = getAssets().open("PluginTest-debug.apk");
					String sdcardDest = Environment.getExternalStorageDirectory().getAbsolutePath()
							+ "/PluginTest-debug.apk";
					if (ApkReader.copyFile(assestInput, sdcardDest)) {
						PluginLoader.installPlugin(sdcardDest);
					}
				} catch (IOException e) {
					e.printStackTrace();
					Toast.makeText(PluginListActivity.this, "安装失败", Toast.LENGTH_LONG).show();
				}
			}
		});

		mList = (ViewGroup) findViewById(R.id.list);

		Button open = (Button) findViewById(R.id.open);
		open.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				PluginDispatcher.startRealActivity(PluginListActivity.this, "test5");
			}
		});
		
		listAll(mList);
	}
	
	private void listAll(ViewGroup root) {
		root.removeAllViews();

		// 列出所有已经安装的插件
		Hashtable<String, PluginDescriptor> plugins = PluginLoader.listAll();
		Iterator<Entry<String, PluginDescriptor>> itr = plugins.entrySet().iterator();
		while (itr.hasNext()) {
			final Entry<String, PluginDescriptor> entry = itr.next();
			Button btton = new Button(this);
			btton.setText("插件id：" + entry.getKey() + "\n插件介绍：" + entry.getValue().getDescription() + "\n安装路径："
					+ entry.getValue().getInstalledPath() + "\n点击查看详情>>");
			LayoutParams layoutParam = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
			layoutParam.topMargin = 10;
			layoutParam.bottomMargin = 10;
			root.addView(btton, layoutParam);

			btton.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {
					Intent intent = new Intent(PluginListActivity.this, PluginDetailActivity.class);
					intent.putExtra("plugin_id", entry.getKey());
					startActivity(intent);
				}
			});
		}
	}

	@Override
	protected void onResume() {
		super.onResume();
		install.performClick();
	}
	
	private final BroadcastReceiver pluginChange = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			Toast.makeText(PluginListActivity.this, "插件" + intent.getStringExtra(PluginLoader.EXTRA_TYPE) + "成功",
					Toast.LENGTH_LONG).show();
			listAll(mList);
		};
	};

	@Override
	protected void onDestroy() {
		super.onDestroy();
		unregisterReceiver(pluginChange);
	};

}

@@ -959,19 +959,50 @@
 stall;%0D%0A
+%09boolean isInstalled = false;%0D%0A
 %09%0D%0A
-
 %09@Overri
@@ -1476,24 +1476,76 @@
 (View v) %7B%0D%0A
+%09%09%09%09if (!isInstalled) %7B%0D%0A%09%09%09%09%09isInstalled = true;%0D%0A%09
 %09%09%09%09Toast.ma
@@ -1608,24 +1608,25 @@
 G).show();%0D%0A
+%09
 %09%09%09%09try %7B%0D%0A%09
@@ -1629,16 +1629,17 @@
 %7B%0D%0A%09%09%09%09%09
+%09
 InputStr
@@ -1695,24 +1695,25 @@
 bug.apk%22);%0D%0A
+%09
 %09%09%09%09%09String 
@@ -1786,16 +1786,17 @@
 Path()%0D%0A
+%09
 %09%09%09%09%09%09%09+
@@ -1823,24 +1823,25 @@
 apk%22;%0D%0A%09%09%09%09%09
+%09
 if (ApkReade
@@ -1876,24 +1876,25 @@
 rdDest)) %7B%0D%0A
+%09
 %09%09%09%09%09%09Plugin
@@ -1933,19 +1933,21 @@
 ;%0D%0A%09%09%09%09%09
+%09
 %7D%0D%0A
+%09
 %09%09%09%09%7D ca
@@ -1974,16 +1974,17 @@
 %7B%0D%0A%09%09%09%09%09
+%09
 e.printS
@@ -1993,24 +1993,25 @@
 ckTrace();%0D%0A
+%09
 %09%09%09%09%09Toast.m
@@ -2075,24 +2075,32 @@
 G).show();%0D%0A
+%09%09%09%09%09%7D%0D%0A
 %09%09%09%09%7D%0D%0A%09%09%09%7D%0D
@@ -3542,24 +3542,48 @@
 nResume();%0D%0A
+%09%09if (!isInstalled) %7B%0D%0A%09
 %09%09install.pe
@@ -3593,24 +3593,29 @@
 rmClick();%0D%0A
+%09%09%7D%0D%0A
 %09%7D%0D%0A%09%0D%0A%09priv

update pluginMain