ProcessInfo.java 4.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * Copyright (c) 2012-2013 NetEase, Inc. and other contributors
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
K
kevinkong 已提交
17 18 19
package com.netease.qa.emmagee.utils;

import java.util.ArrayList;
20
import java.util.Collections;
K
kevinkong 已提交
21 22 23 24
import java.util.List;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;
25
import android.app.ActivityManager.RunningTaskInfo;
K
kevinkong 已提交
26 27 28
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
29
import android.os.Debug;
A
andrewleo 已提交
30
import android.util.Log;
K
kevinkong 已提交
31

32 33
/**
 * get information of processes
A
andrewleo 已提交
34 35
 * 
 * @author andrewleo
36
 */
K
kevinkong 已提交
37 38
public class ProcessInfo {

A
andrewleo 已提交
39
	private static final String LOG_TAG = "Emmagee-" + ProcessInfo.class.getSimpleName();
40

41
	private static final String PACKAGE_NAME = "com.netease.qa.emmagee";
42

K
kevinkong 已提交
43
	/**
44
	 * get information of all running processes,including package name ,process
A
andrewleo2013 已提交
45
	 * name ,icon ,pid and uid.
A
andrewleo 已提交
46
	 * 
47 48 49
	 * @param context
	 *            context of activity
	 * @return running processes list
K
kevinkong 已提交
50 51
	 */
	public List<Programe> getRunningProcess(Context context) {
A
andrewleo 已提交
52
		Log.i(LOG_TAG, "get running processes");
K
kevinkong 已提交
53

A
andrewleo 已提交
54
		ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
K
kevinkong 已提交
55 56 57 58 59 60
		List<RunningAppProcessInfo> run = am.getRunningAppProcesses();
		PackageManager pm = context.getPackageManager();
		List<Programe> progressList = new ArrayList<Programe>();

		for (ApplicationInfo appinfo : getPackagesInfo(context)) {
			Programe programe = new Programe();
A
andrewleo 已提交
61
			if (((appinfo.flags & ApplicationInfo.FLAG_SYSTEM) > 0) || ((appinfo.processName != null) && (appinfo.processName.equals(PACKAGE_NAME)))) {
K
kevinkong 已提交
62 63 64
				continue;
			}
			for (RunningAppProcessInfo runningProcess : run) {
A
andrewleo 已提交
65
				if ((runningProcess.processName != null) && runningProcess.processName.equals(appinfo.processName)) {
K
kevinkong 已提交
66 67 68 69 70 71 72
					programe.setPid(runningProcess.pid);
					programe.setUid(runningProcess.uid);
					break;
				}
			}
			programe.setPackageName(appinfo.processName);
			programe.setProcessName(appinfo.loadLabel(pm).toString());
A
andrewleo 已提交
73
			programe.setIcon(appinfo.loadIcon(pm));
K
kevinkong 已提交
74 75
			progressList.add(programe);
		}
76
		Collections.sort(progressList);
K
kevinkong 已提交
77 78 79 80
		return progressList;
	}

	/**
A
andrewleo2013 已提交
81
	 * get information of all applications.
A
andrewleo 已提交
82
	 * 
83 84
	 * @param context
	 *            context of activity
A
andrewleo 已提交
85
	 * @return packages information of all applications
K
kevinkong 已提交
86 87 88
	 */
	private List<ApplicationInfo> getPackagesInfo(Context context) {
		PackageManager pm = context.getApplicationContext().getPackageManager();
A
andrewleo 已提交
89
		List<ApplicationInfo> appList = pm.getInstalledApplications(PackageManager.GET_UNINSTALLED_PACKAGES);
K
kevinkong 已提交
90 91
		return appList;
	}
92

93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
	/**
	 * get pid by package name
	 * 
	 * @param context
	 *            context of activity
	 * @param packageName
	 *            package name of monitoring app
	 * @return pid
	 */
	public Programe getProgrameByPackageName(Context context, String packageName) {
		List<Programe> processList = getRunningProcess(context);
		for (Programe programe : processList) {
			if ((programe.getPackageName() != null) && (programe.getPackageName().equals(packageName))) {
				return programe;
			}
		}
		return null;
	}

112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
	/**
	 * get top activity name
	 * 
	 * @param context
	 *            context of activity
	 * @return top activity name
	 */
	public static String getTopActivity(Context context) {
		ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
		List<RunningTaskInfo> runningTaskInfos = manager.getRunningTasks(1);
		if (runningTaskInfos != null)
			return (runningTaskInfos.get(0).topActivity).toString();
		else
			return null;
	}
K
kevinkong 已提交
127
}