package com.gh.gamecenter.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import com.gh.common.util.DataUtils;
import com.gh.common.util.Utils;
import com.gh.gamecenter.eventbus.EBPackage;

import de.greenrobot.event.EventBus;

/**
 * 监听应用安装和卸载的广播
 * @author LGT 黄壮华
 *
 */
public class InstallAndUninstallReceiver extends BroadcastReceiver {

	@Override
	public void onReceive(Context context, Intent intent) {
		

		// 接收安装广播
		if (intent.getAction().equals(Intent.ACTION_PACKAGE_ADDED)) {
			String packageName = intent.getDataString();
			packageName = packageName.substring(packageName.indexOf(":") + 1);
			Utils.log("安装了:" + packageName + "包名的程序");
			EventBus.getDefault().post(new EBPackage("安装", packageName));
		}

		// 接收卸载广播
		if (intent.getAction().equals(Intent.ACTION_PACKAGE_REMOVED)) {
			String packageName = intent.getDataString();
			packageName = packageName.substring(packageName.indexOf(":") + 1);
			Utils.log("卸载了:" + packageName + "包名的程序");
			EventBus.getDefault().post(new EBPackage("卸载", packageName));
		}

		// 接收替换广播
		if (intent.getAction().equals(Intent.ACTION_PACKAGE_REPLACED)) {
			String packageName = intent.getData().getSchemeSpecificPart();
			packageName = packageName.substring(packageName.indexOf(":") + 1);
			Utils.log("替换了:" + packageName + "包名的程序");
			if (packageName.equals(context.getPackageName())) {
				DataUtils.onEvent(context, "软件更新", "更新完成");
			}
			EventBus.getDefault().post(new EBPackage("替换", packageName));
		}
	}

}
