package com.gh.common.util;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.app.ActivityManager.RunningTaskInfo;
import android.content.ComponentName;
import android.content.Context;

import java.util.List;

public class RunningUtils {

	/**
	 * 判断当前应用程序处于前台还是后台
	 */
	public static boolean isApplicationBroughtToBackground(final Context context) {
		ActivityManager am = (ActivityManager) context
				.getSystemService(Context.ACTIVITY_SERVICE);
		List<RunningTaskInfo> tasks = am.getRunningTasks(1);
		if (!tasks.isEmpty()) {
			ComponentName topActivity = tasks.get(0).topActivity;
			if (!topActivity.getPackageName().equals(context.getPackageName())) {
				return true;
			}
		}
		return false;

	}

	/**
	 * 判断当前应用程序处于前台还是后台
	 */
	public static boolean isBackground(Context context) {
		ActivityManager activityManager = (ActivityManager) context
				.getSystemService(Context.ACTIVITY_SERVICE);
		List<RunningAppProcessInfo> appProcesses = activityManager
				.getRunningAppProcesses();
		for (RunningAppProcessInfo appProcess : appProcesses) {
			if (appProcess.processName.equals(context.getPackageName())) {
				if (appProcess.importance == RunningAppProcessInfo.IMPORTANCE_BACKGROUND) {
					return true;
				} else {
					return false;
				}
			}
		}
		return false;
	}

	/**
	 * 判断当前topactivity是否与传入的相同
	 */
	public static boolean isEqualsTop(Context context, String activityName) {
		ActivityManager activityManager = (ActivityManager) context
				.getSystemService(Context.ACTIVITY_SERVICE);
		List<RunningTaskInfo> infos = activityManager.getRunningTasks(100);
		for (RunningTaskInfo info : infos) {
			if (info.topActivity.getClassName().equals(activityName)) {
				return true;
			}
		}
		return false;
	}
	
	/**
	 * 判断当前baseActivity是否与传入的相同
	 */
	public static boolean isEqualsBase(Context context, String activityName) {
		ActivityManager activityManager = (ActivityManager) context
				.getSystemService(Context.ACTIVITY_SERVICE);
		List<RunningTaskInfo> infos = activityManager.getRunningTasks(100);
		for (RunningTaskInfo info : infos) {
			if (info.baseActivity.getClassName().equals(activityName)) {
				return true;
			}
		}
		return false;
	}
	
	/**
	 * 判断应用是否正在运行
	 */
	public static boolean isRunning(Context context) {
		ActivityManager activityManager = (ActivityManager) context
				.getSystemService(Context.ACTIVITY_SERVICE);
		List<RunningTaskInfo> infos = activityManager.getRunningTasks(100);
		for (RunningTaskInfo info : infos) {
			if (info.topActivity.getPackageName().equals(
					context.getPackageName())) {
				return true;
			}
		}
		return false;
	}
	
	/**
	 * 获取当前topactivity
	 */
	public static String getTopActivity(Context context) {
		ActivityManager activityManager = (ActivityManager) context
				.getSystemService(Context.ACTIVITY_SERVICE);
		List<RunningTaskInfo> infos = activityManager.getRunningTasks(100);
		for (RunningTaskInfo info : infos) {
			if (info.topActivity.getPackageName().equals(
					context.getPackageName())) {
				return info.topActivity.getClassName();
			}
		}
		return null;
	}

}
