Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New implementation for getCurrentProcessName #427

Merged
merged 2 commits into from
Apr 10, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WHy change this from equals to endsWidth? equals is much more precise.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed that when run on an 6.0 device, the process name is prepended with the package name. (I haven't tested on other versions, this might just be normal for the new getCurrentProcessName.)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, just add an inline comment to that effect so that we know why when looking at it later.

}

@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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we also need to keep the original solution as backup in case they are using Exposed (as per the note on the StackOverflow post).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've tested it with Xposed. No changes. As stated in the comments on the solution, this problem is outdated.

}
return null;
}

/**
Expand Down