Skip to content

Commit

Permalink
perf(utils): hold the result of isDownloaderProcess
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacksgong committed Aug 20, 2016
1 parent 930fe6f commit 74ef14e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ public static FileDownloadProperties getImpl() {
private final static String TRUE_STRING = "true";
private final static String FALSE_STRING = "false";

// init properties, normally consume <= 2ms
private FileDownloadProperties() {
if (FileDownloadHelper.getAppContext() == null) {
throw new IllegalStateException("Please invoke the FileDownloader#init in " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,31 +256,44 @@ public static String getStack(final StackTraceElement[] stackTrace, final boolea
return t.toString();
}

private static Boolean IS_DOWNLOADER_PROCESS;

public static boolean isDownloaderProcess(final Context context) {
if (FileDownloadProperties.getImpl().PROCESS_NON_SEPARATE) {
return true;
if (IS_DOWNLOADER_PROCESS != null) {
return IS_DOWNLOADER_PROCESS;
}

int pid = android.os.Process.myPid();
final ActivityManager activityManager = (ActivityManager) context.
getSystemService(Context.ACTIVITY_SERVICE);
boolean result = false;
do {
if (FileDownloadProperties.getImpl().PROCESS_NON_SEPARATE) {
result = true;
break;
}

final List<ActivityManager.RunningAppProcessInfo> runningAppProcessInfoList =
activityManager.getRunningAppProcesses();
int pid = android.os.Process.myPid();
final ActivityManager activityManager = (ActivityManager) context.
getSystemService(Context.ACTIVITY_SERVICE);

if (null == runningAppProcessInfoList || runningAppProcessInfoList.isEmpty()) {
FileDownloadLog.w(FileDownloadUtils.class, "The running app process info list from" +
" ActivityManager is null or empty, maybe current App is not running.");
return false;
}
final List<ActivityManager.RunningAppProcessInfo> runningAppProcessInfoList =
activityManager.getRunningAppProcesses();

for (ActivityManager.RunningAppProcessInfo runningAppProcessInfo : runningAppProcessInfoList) {
if (runningAppProcessInfo.pid == pid) {
return runningAppProcessInfo.processName.endsWith(":filedownloader");
if (null == runningAppProcessInfoList || runningAppProcessInfoList.isEmpty()) {
FileDownloadLog.w(FileDownloadUtils.class, "The running app process info list from" +
" ActivityManager is null or empty, maybe current App is not running.");
return false;
}
}

return false;
for (ActivityManager.RunningAppProcessInfo runningAppProcessInfo : runningAppProcessInfoList) {
if (runningAppProcessInfo.pid == pid) {
result = runningAppProcessInfo.processName.endsWith(":filedownloader");
break;
}
}

} while (false);

IS_DOWNLOADER_PROCESS = result;
return IS_DOWNLOADER_PROCESS;
}

public static String[] convertHeaderString(final String nameAndValuesString) {
Expand Down

0 comments on commit 74ef14e

Please sign in to comment.