package guard.api.sensitive.ui.sapiguard.hookedapis;

import java.util.ArrayList;
import java.util.List;

import guard.api.sensitive.ui.sapiguard.utils.PrivacyManager;
import guard.api.sensitive.ui.sapiguard.XHook;
import guard.api.sensitive.ui.sapiguard.utils.XParam;

public class XPackageManager extends XHook {
	private Methods mMethod;
	private String mClassName;
	private static final String cClassName = "android.app.ApplicationPackageManager";

	private XPackageManager(Methods method, String restrictionName) {
		super(restrictionName, method.name().replace("Srv_", ""), method.name());
		mMethod = method;
		mClassName = "com.android.server.pm.PackageManagerService";
	}

	private XPackageManager(Methods method, String restrictionName, String className) {
		super(restrictionName, method.name(), null);
		mMethod = method;
		mClassName = className;
	}

	public String getClassName() {
		return mClassName;
	}

	// @formatter:off

	// public List<ApplicationInfo> getInstalledApplications(int flags)
	// public List<PackageInfo> getInstalledPackages(int flags)
	// public String[] getPackagesForUid(int uid)
	// public List<PackageInfo> getPackagesHoldingPermissions(String[] permissions, int flags)
	// public List<PackageInfo> getPreferredPackages(int flags)
	// public List<ResolveInfo> queryBroadcastReceivers(Intent intent, int flags)
	// public List<ProviderInfo> queryContentProviders(String processName, int uid, int flags)
	// public List<ResolveInfo> queryIntentActivities(Intent intent, int flags)
	// public List<ResolveInfo> queryIntentActivityOptions(ComponentName caller, Intent[] specifics, Intent intent, int flags)
	// public List<ResolveInfo> queryIntentContentProviders(Intent intent, int flags)
	// public List<ResolveInfo> queryIntentServices(Intent intent, int flags)
	// frameworks/base/core/java/android/app/ApplicationPackageManager.java
	// http://developer.android.com/reference/android/content/pm/PackageManager.html

	// public int checkPermission(String permName, String pkgName)
	// public int checkUidPermission(String permName, int uid)

	// public java.lang.String[] getPackagesForUid(int uid)
	// public java.util.List<android.content.pm.ResolveInfo> queryIntentActivities(android.content.Intent intent, java.lang.String resolvedType, int flags, int userId)
	// public java.util.List<android.content.pm.ResolveInfo> queryIntentActivityOptions(android.content.ComponentName caller, android.content.Intent[] specifics, java.lang.String[] specificTypes, android.content.Intent intent, java.lang.String resolvedType, int flags, int userId)
	// public java.util.List<android.content.pm.ResolveInfo> queryIntentReceivers(android.content.Intent intent, java.lang.String resolvedType, int flags, int userId)
	// public java.util.List<android.content.pm.ResolveInfo> queryIntentServices(android.content.Intent intent, java.lang.String resolvedType, int flags, int userId)
	// public java.util.List<android.content.pm.ResolveInfo> queryIntentContentProviders(android.content.Intent intent, java.lang.String resolvedType, int flags, int userId)
	// public java.util.List<android.content.pm.ApplicationInfo> getPersistentApplications(int flags)
	// public java.util.List<android.content.pm.ProviderInfo> queryContentProviders(java.lang.String processName, int uid, int flags)
	// public java.util.List<android.content.pm.PackageInfo> getPreferredPackages(int flags)
	// public android.content.pm.ParceledListSlice getInstalledPackages(int flags, int userId)
	// public android.content.pm.ParceledListSlice getPackagesHoldingPermissions(java.lang.String[] permissions, int flags, int userId)
	// public android.content.pm.ParceledListSlice getInstalledApplications(int flags, int userId)

	// public PackageInfo getPackageInfo(String packageName, int flags, int userId)
	// public ApplicationInfo getApplicationInfo(String packageName, int flags, int userId)

	// http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/5.0.0_r1/com/android/server/pm/PackageManagerService.java

	// @formatter:on

	// @formatter:off
	private enum Methods {
		getInstalledApplications, getInstalledPackages,
		getPackagesForUid,
		getPackagesHoldingPermissions,
		getPreferredActivities, getPreferredPackages,
		queryBroadcastReceivers, queryContentProviders,
		queryIntentActivities, queryIntentActivityOptions,
		queryIntentContentProviders, queryIntentServices,

		checkPermission, checkUidPermission,

		Srv_getPackageInfo, Srv_getApplicationInfo,
		Srv_getInstalledApplications, Srv_getInstalledPackages,
		Srv_getPackagesForUid,
		Srv_getPackagesHoldingPermissions,
		Srv_getPersistentApplications,
		Srv_getPreferredPackages,
		Srv_queryContentProviders,
		Srv_queryIntentActivities, Srv_queryIntentActivityOptions,
		Srv_queryIntentContentProviders,
		Srv_queryIntentReceivers,
		Srv_queryIntentServices
	};
	// @formatter:on

	public static List<XHook> getInstances(String className, boolean server) {
		List<XHook> listHook = new ArrayList<XHook>();
		if (!cClassName.equals(className)) {
			if (className == null)
				className = cClassName;

			if (server) {
				listHook.add(new XPackageManager(Methods.Srv_getPackageInfo, PrivacyManager.cSystem));
				listHook.add(new XPackageManager(Methods.Srv_getApplicationInfo, PrivacyManager.cSystem));
				listHook.add(new XPackageManager(Methods.Srv_getInstalledApplications, PrivacyManager.cSystem));
				listHook.add(new XPackageManager(Methods.Srv_getInstalledPackages, PrivacyManager.cSystem));
				listHook.add(new XPackageManager(Methods.Srv_getPackagesForUid, PrivacyManager.cSystem));
				listHook.add(new XPackageManager(Methods.Srv_getPackagesHoldingPermissions, PrivacyManager.cSystem));
				listHook.add(new XPackageManager(Methods.Srv_getPersistentApplications, PrivacyManager.cSystem));
				listHook.add(new XPackageManager(Methods.Srv_getPreferredPackages, PrivacyManager.cSystem));
				listHook.add(new XPackageManager(Methods.Srv_queryContentProviders, PrivacyManager.cSystem));
				listHook.add(new XPackageManager(Methods.Srv_queryIntentActivities, PrivacyManager.cSystem));
				listHook.add(new XPackageManager(Methods.Srv_queryIntentActivityOptions, PrivacyManager.cSystem));
				listHook.add(new XPackageManager(Methods.Srv_queryIntentContentProviders, PrivacyManager.cSystem));
				listHook.add(new XPackageManager(Methods.Srv_queryIntentReceivers, PrivacyManager.cSystem));
				listHook.add(new XPackageManager(Methods.Srv_queryIntentServices, PrivacyManager.cSystem));

				listHook.add(new XPackageManager(Methods.checkPermission, PrivacyManager.cSystem));
				listHook.add(new XPackageManager(Methods.checkUidPermission, PrivacyManager.cSystem));
			} else {
				listHook.add(new XPackageManager(Methods.getInstalledApplications, PrivacyManager.cSystem, className));
				listHook.add(new XPackageManager(Methods.getInstalledPackages, PrivacyManager.cSystem, className));
				listHook.add(new XPackageManager(Methods.getPackagesForUid, PrivacyManager.cSystem, className));
				listHook.add(new XPackageManager(Methods.getPackagesHoldingPermissions, PrivacyManager.cSystem,
						className));
				listHook.add(new XPackageManager(Methods.getPreferredActivities, PrivacyManager.cSystem, className));
				listHook.add(new XPackageManager(Methods.getPreferredPackages, PrivacyManager.cSystem, className));
				listHook.add(new XPackageManager(Methods.queryBroadcastReceivers, PrivacyManager.cSystem, className));
				listHook.add(new XPackageManager(Methods.queryContentProviders, PrivacyManager.cSystem, className));
				listHook.add(new XPackageManager(Methods.queryIntentActivities, PrivacyManager.cSystem, className));
				listHook.add(new XPackageManager(Methods.queryIntentActivityOptions, PrivacyManager.cSystem, className));
				listHook.add(new XPackageManager(Methods.queryIntentContentProviders, PrivacyManager.cSystem, className));
				listHook.add(new XPackageManager(Methods.queryIntentServices, PrivacyManager.cSystem, className));
			}
		}
		return listHook;
	}

	@Override
	protected void before(XParam param) throws Throwable {
		if (mMethod == Methods.getPreferredActivities){}

	}

	@Override
	@SuppressWarnings("unchecked")
	protected void after(XParam param) throws Throwable {
		switch (mMethod) {
		case Srv_getPackageInfo:
		case Srv_getApplicationInfo:


		case Srv_getInstalledApplications:


		case Srv_getInstalledPackages:
		case Srv_getPackagesHoldingPermissions:


		case getPackagesForUid:
		case Srv_getPackagesForUid:


		case Srv_getPersistentApplications:

		case Srv_getPreferredPackages:


		case Srv_queryIntentActivities:
		case Srv_queryIntentActivityOptions:
		case Srv_queryIntentContentProviders:
		case Srv_queryIntentReceivers:
		case Srv_queryIntentServices:


		case getInstalledApplications:


		case getPreferredActivities:
			break;

		case getInstalledPackages:
		case getPackagesHoldingPermissions:
		case getPreferredPackages:

		case queryBroadcastReceivers:
		case queryIntentActivities:
		case queryIntentActivityOptions:
		case queryIntentContentProviders:
		case queryIntentServices:


		case queryContentProviders:
		case Srv_queryContentProviders:


		case checkPermission:

		case checkUidPermission:

		}
	}




}
