Skip to content

Commit

Permalink
Added: support 5.1.1: use top command to get pid
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewleo committed Aug 25, 2015
1 parent 669de8a commit 93d3594
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 27 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.3.0
2.4
17 changes: 5 additions & 12 deletions src/com/netease/qa/emmagee/activity/MainPageActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -205,17 +205,10 @@ private void waitForAppStart(String packageName) {
boolean isProcessStarted = false;
long startTime = System.currentTimeMillis();
while (System.currentTimeMillis() < startTime + TIMEOUT) {
processList = processInfo.getRunningProcess(getBaseContext());
for (Programe programe : processList) {
if ((programe.getPackageName() != null) && (programe.getPackageName().equals(packageName))) {
pid = programe.getPid();
Log.d(LOG_TAG, "pid:" + pid);
uid = programe.getUid();
if (pid != 0) {
isProcessStarted = true;
break;
}
}
pid = processInfo.getPidByPackageName(getBaseContext(), packageName);
if (pid != 0) {
isProcessStarted = true;
break;
}
if (isProcessStarted) {
break;
Expand Down Expand Up @@ -265,7 +258,7 @@ private class ListAdapter extends BaseAdapter {
int lastCheckedPosition = -1;

public ListAdapter() {
programes = processInfo.getRunningProcess(getBaseContext());
programes = processInfo.getAllPackages(getBaseContext());
}

@Override
Expand Down
118 changes: 104 additions & 14 deletions src/com/netease/qa/emmagee/utils/ProcessInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
*/
package com.netease.qa.emmagee.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
Expand All @@ -26,7 +29,7 @@
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Debug;
import android.os.Build;
import android.util.Log;

/**
Expand All @@ -36,9 +39,11 @@
*/
public class ProcessInfo {

private static final String LOG_TAG = "Emmagee-" + ProcessInfo.class.getSimpleName();
private static final String LOG_TAG = "Emmagee-"
+ ProcessInfo.class.getSimpleName();

private static final String PACKAGE_NAME = "com.netease.qa.emmagee";
private static final int ANDROID_M = 22;

/**
* get information of all running processes,including package name ,process
Expand All @@ -50,19 +55,23 @@ public class ProcessInfo {
*/
public List<Programe> getRunningProcess(Context context) {
Log.i(LOG_TAG, "get running processes");

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

ActivityManager am = (ActivityManager) context
.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningAppProcessInfo> run = am.getRunningAppProcesses();
for (ApplicationInfo appinfo : getPackagesInfo(context)) {
Programe programe = new Programe();
if (((appinfo.flags & ApplicationInfo.FLAG_SYSTEM) > 0) || ((appinfo.processName != null) && (appinfo.processName.equals(PACKAGE_NAME)))) {
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)) {
if ((runningProcess.processName != null)
&& runningProcess.processName
.equals(appinfo.processName)) {
programe.setPid(runningProcess.pid);
programe.setUid(runningProcess.uid);
break;
Expand All @@ -77,6 +86,76 @@ public List<Programe> getRunningProcess(Context context) {
return progressList;
}

/**
* get pid by package name
*
* @param context
* context of activity
* @return pid
*/
public int getPidByPackageName(Context context, String packageName) {
Log.i(LOG_TAG, "start getLaunchedPid");
ActivityManager am = (ActivityManager) context
.getSystemService(Context.ACTIVITY_SERVICE);
if (Build.VERSION.SDK_INT < ANDROID_M) {
List<RunningAppProcessInfo> run = am.getRunningAppProcesses();
for (RunningAppProcessInfo runningProcess : run) {
if ((runningProcess.processName != null)
&& runningProcess.processName.equals(packageName)) {
return runningProcess.pid;
}
}
} else {
Log.i(LOG_TAG, "use top command to get pid");
try {
Process p = Runtime.getRuntime().exec("top -m 100 -n 1");
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(
p.getInputStream()));
String line = "";
while ((line = bufferedReader.readLine()) != null) {
if (line.contains(packageName)) {
line = line.trim();
String[] splitLine = line.split("\\s+");
if (packageName.equals(splitLine[splitLine.length - 1])) {
return Integer.parseInt(splitLine[0]);
}
}
}
} catch (IOException e1) {
e1.printStackTrace();
}
}
return 0;
}

/**
* get information of all installed packages
*
* @param context
* context of activity
* @return all installed packages
*/
public List<Programe> getAllPackages(Context context) {
Log.i(LOG_TAG, "getAllPackages");
List<Programe> progressList = new ArrayList<Programe>();
PackageManager pm = context.getPackageManager();

for (ApplicationInfo appinfo : getPackagesInfo(context)) {
Programe programe = new Programe();
if (((appinfo.flags & ApplicationInfo.FLAG_SYSTEM) > 0)
|| ((appinfo.processName != null) && (appinfo.processName
.equals(PACKAGE_NAME)))) {
continue;
}
programe.setPackageName(appinfo.processName);
programe.setProcessName(appinfo.loadLabel(pm).toString());
programe.setIcon(appinfo.loadIcon(pm));
progressList.add(programe);
}
Collections.sort(progressList);
return progressList;
}

/**
* get information of all applications.
*
Expand All @@ -86,7 +165,8 @@ public List<Programe> getRunningProcess(Context context) {
*/
private List<ApplicationInfo> getPackagesInfo(Context context) {
PackageManager pm = context.getApplicationContext().getPackageManager();
List<ApplicationInfo> appList = pm.getInstalledApplications(PackageManager.GET_UNINSTALLED_PACKAGES);
List<ApplicationInfo> appList = pm
.getInstalledApplications(PackageManager.GET_UNINSTALLED_PACKAGES);
return appList;
}

Expand All @@ -100,11 +180,20 @@ private List<ApplicationInfo> getPackagesInfo(Context context) {
* @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;
if (Build.VERSION.SDK_INT < ANDROID_M) {
List<Programe> processList = getRunningProcess(context);
for (Programe programe : processList) {
if ((programe.getPackageName() != null)
&& (programe.getPackageName().equals(packageName))) {
return programe;
}
}
} else {
Programe programe = new Programe();
int pid = getPidByPackageName(context, packageName);
programe.setPid(pid);
programe.setUid(0);
return programe;
}
return null;
}
Expand All @@ -117,7 +206,8 @@ public Programe getProgrameByPackageName(Context context, String packageName) {
* @return top activity name
*/
public static String getTopActivity(Context context) {
ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
ActivityManager manager = (ActivityManager) context
.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> runningTaskInfos = manager.getRunningTasks(1);
if (runningTaskInfos != null)
return (runningTaskInfos.get(0).topActivity).toString();
Expand Down

0 comments on commit 93d3594

Please sign in to comment.