package com.plugin.core;

import android.app.Application;
import android.app.Instrumentation;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Handler;

import com.plugin.util.LogUtil;
import com.plugin.util.RefInvoker;

public class PluginApplication extends Application {

	private static Object activityThread;
	private String mProcessName;

	public String getProcessName() {
		return mProcessName;
	}

	public static Object getActivityThread() {
		return activityThread;
	}

	@Override
	public void onCreate() {
		super.onCreate();
		injectInstrumentation();
	}

	/**
	 * 注入Instrumentation主要是为了支持Activity
	 */
	private void injectInstrumentation() {
		LogUtil.d("injectInstrumentation");

		// 从ThreadLocal中取出来的
		activityThread = RefInvoker.invokeStaticMethod("android.app.ActivityThread", "currentActivityThread",
				(Class[]) null, (Object[]) null);

		mProcessName = (String) RefInvoker.invokeMethod(activityThread, "android.app.ActivityThread", "getProcessName",
				(Class[]) null, (Object[]) null);

		// 给Instrumentation添加一层代理，用来实现隐藏api的调用
		Instrumentation originalInstrumentation = (Instrumentation) RefInvoker.getFieldObject(activityThread,
				"android.app.ActivityThread", "mInstrumentation");
		RefInvoker.setFieldObject(activityThread, "android.app.ActivityThread", "mInstrumentation",
				new PluginInstrumentionWrapper(originalInstrumentation));

		// getHandler
		Handler handler = (Handler) RefInvoker.getStaticFieldObject("android.app.ActivityThread", "sMainThreadHandler");

		// 给handler添加一个callback
		RefInvoker.setFieldObject(handler, Handler.class.getName(), "mCallback", new PluginAppTrace(handler));

	}

	@Override
	public void sendBroadcast(Intent intent) {
		LogUtil.d("sendBroadcast", intent.toUri(0));
		intent = PluginIntentResolver.resolveReceiver(intent);
		super.sendBroadcast(intent);
	}

	@Override
	public ComponentName startService(Intent service) {
		LogUtil.d("startService", service.toUri(0));
		PluginIntentResolver.resolveService(service);
		return super.startService(service);
	}

	@Override
	public boolean stopService(Intent name) {
		LogUtil.d("stopService", name.toUri(0));
		if (PluginIntentResolver.resolveStopService(name)) {
			super.startService(name);
			return false;
		} else {
			return super.stopService(name);
		}
	}

	/**
	 * startActivity有很多重载的方法，如有必要，可以相应的进行重写
	 */
	@Override
	public void startActivity(Intent intent) {
		LogUtil.d("startActivity", intent.toUri(0));
		PluginIntentResolver.resloveActivity(intent);
		intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		super.startActivity(intent);
	}

}

@@ -1445,16 +1445,196 @@
 Handler)
+ RefInvoker.invokeMethod(activityThread, %22android.app.ActivityThread%22, %22getHandler%22, (Class%5B%5D)null, (Object%5B%5D)null);%0A%09%09//%E4%B8%8B%E9%9D%A2%E7%9A%84%E6%96%B9%E6%B3%95%E5%86%8Dapi16%E5%8F%8A%E4%B8%80%E4%B8%8B%E4%BC%9A%E5%A4%B1%E8%B4%A5%EF%BC%8C%E6%88%90%E5%91%98%E5%8F%98%E9%87%8F%E5%90%8D%E7%A7%B0%E9%94%99%E8%AF%AF%E3%80%82%0A%09%09//Handler handler = (Handler)
  RefInvo

修复Api16及以下Receiver唤起失败的bug