Skip to content

Commit

Permalink
Merge pull request #427 from F43nd1r/processname
Browse files Browse the repository at this point in the history
More robust implementation for getCurrentProcessName
  • Loading branch information
william-ferguson-au committed Apr 10, 2016
2 parents 5932c05 + 2f97364 commit 73cf61d
Showing 1 changed file with 14 additions and 18 deletions.
32 changes: 14 additions & 18 deletions src/main/java/org/acra/ACRA.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@
*/
package org.acra;

import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Build;
Expand All @@ -34,8 +32,10 @@
import org.acra.prefs.PrefUtils;
import org.acra.prefs.SharedPreferencesFactory;
import org.acra.util.ApplicationStartupProcessor;
import org.acra.util.IOUtils;

import java.util.List;
import java.io.FileInputStream;
import java.io.IOException;

/**
* Use this class to initialize the crash reporting feature using
Expand All @@ -50,7 +50,7 @@
public final class ACRA {
private ACRA(){}

public static final boolean DEV_LOGGING = false; // Should be false for release.
public static /*non-final*/ boolean DEV_LOGGING = false; // Should be false for release.

public static final String LOG_TAG = ACRA.class.getSimpleName();

Expand Down Expand Up @@ -172,7 +172,7 @@ public static void init(@NonNull Application app, @NonNull ACRAConfiguration con
*/
public static void init(@NonNull Application app, @NonNull ACRAConfiguration config, boolean checkReportsOnApplicationStart){

final boolean senderServiceProcess = isACRASenderServiceProcess(app);
final boolean senderServiceProcess = isACRASenderServiceProcess();
if (senderServiceProcess) {
if (ACRA.DEV_LOGGING) log.d(LOG_TAG, "Not initialising ACRA to listen for uncaught Exceptions as this is the SendWorker process and we only send reports, we don't capture them to avoid infinite loops");
}
Expand Down Expand Up @@ -269,25 +269,21 @@ public static boolean isInitialised() {
* @return true if the current process is the process running the SenderService.
* NB this assumes that your SenderService is configured to used the default ':acra' process.
*/
public static boolean isACRASenderServiceProcess(@NonNull Application app) {
final String processName = getCurrentProcessName(app);
public static boolean isACRASenderServiceProcess() {
final String processName = getCurrentProcessName();
if (ACRA.DEV_LOGGING) log.d(LOG_TAG, "ACRA processName='" + processName + "'");
return (processName != null) && processName.equals(ACRA_PRIVATE_PROCESS_NAME);
//processName sometimes (or always?) starts with the package name, so we use endsWith instead of equals
return (processName != null) && processName.endsWith(ACRA_PRIVATE_PROCESS_NAME);
}

@Nullable
private static String getCurrentProcessName(@NonNull Application app) {
private static String getCurrentProcessName() {
final int processId = android.os.Process.myPid();
final ActivityManager manager = (ActivityManager) app.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> processInfos = manager.getRunningAppProcesses();
if (processInfos != null) {
for (final ActivityManager.RunningAppProcessInfo processInfo : processInfos) {
if (processInfo.pid == processId) {
return processInfo.processName;
}
}
try {
return IOUtils.streamToString(new FileInputStream("/proc/"+processId+"/cmdline")).trim();
} catch (IOException e) {
return null;
}
return null;
}

/**
Expand Down

0 comments on commit 73cf61d

Please sign in to comment.