ProcessInfo.java 3.2 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 25 26 27
import java.util.List;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
A
andrewleo 已提交
28
import android.util.Log;
K
kevinkong 已提交
29

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

37
	private static final String LOG_TAG = "Emmagee-"
K
kevinkong 已提交
38
			+ ProcessInfo.class.getSimpleName();
39

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

K
kevinkong 已提交
42
	/**
43
	 * get information of all running processes,including package name ,process
A
andrewleo2013 已提交
44
	 * name ,icon ,pid and uid.
A
andrewleo 已提交
45
	 * 
46 47 48
	 * @param context
	 *            context of activity
	 * @return running processes list
K
kevinkong 已提交
49 50
	 */
	public List<Programe> getRunningProcess(Context context) {
A
andrewleo 已提交
51
		Log.i(LOG_TAG, "get running processes");
K
kevinkong 已提交
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79

		ActivityManager am = (ActivityManager) context
				.getSystemService(Context.ACTIVITY_SERVICE);
		List<RunningAppProcessInfo> run = am.getRunningAppProcesses();
		PackageManager pm = context.getPackageManager();
		List<Programe> progressList = new ArrayList<Programe>();
		boolean launchTag;

		for (ApplicationInfo appinfo : getPackagesInfo(context)) {
			launchTag = false;
			Programe programe = new Programe();
			if (((appinfo.flags & ApplicationInfo.FLAG_SYSTEM) > 0)
					|| ((appinfo.processName != null) && (appinfo.processName
							.equals(PACKAGE_NAME)))) {
				continue;
			}
			for (RunningAppProcessInfo runningProcess : run) {
				if ((runningProcess.processName != null)
						&& runningProcess.processName
								.equals(appinfo.processName)) {
					launchTag = true;
					programe.setPid(runningProcess.pid);
					programe.setUid(runningProcess.uid);
					break;
				}
			}
			programe.setPackageName(appinfo.processName);
			programe.setProcessName(appinfo.loadLabel(pm).toString());
A
andrewleo2013 已提交
80
			if (launchTag) {
K
kevinkong 已提交
81 82 83 84
				programe.setIcon(appinfo.loadIcon(pm));
			}
			progressList.add(programe);
		}
85
		Collections.sort(progressList);
K
kevinkong 已提交
86 87 88 89
		return progressList;
	}

	/**
A
andrewleo2013 已提交
90
	 * get information of all applications.
A
andrewleo 已提交
91
	 * 
92 93
	 * @param context
	 *            context of activity
A
andrewleo 已提交
94
	 * @return packages information of all applications
K
kevinkong 已提交
95 96 97 98 99 100 101 102
	 */
	private List<ApplicationInfo> getPackagesInfo(Context context) {
		PackageManager pm = context.getApplicationContext().getPackageManager();
		List<ApplicationInfo> appList = pm
				.getInstalledApplications(PackageManager.GET_UNINSTALLED_PACKAGES);
		return appList;
	}
}