Skip to content

Commit

Permalink
Merge pull request #344 from ACRA/romansl-master
Browse files Browse the repository at this point in the history
SendWorker now works in the separate process service.
  • Loading branch information
william-ferguson-au committed Jan 4, 2016
2 parents 68b01cf + 001def9 commit cf3cf27
Show file tree
Hide file tree
Showing 21 changed files with 1,051 additions and 893 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ pom.xml.releaseBackup
release.properties
*.iml
*.idea/*
build/
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ android {

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:support-v4:23.1.0'
compile 'com.android.support:support-v4:23.1.1'
}
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<groupId>ch.acra</groupId>
<artifactId>acra</artifactId>
<packaging>aar</packaging>
<version>4.7.1-SNAPSHOT</version>
<version>4.8.0-SNAPSHOT</version>

<name>Application Crash Report for Android</name>

Expand Down Expand Up @@ -76,7 +76,7 @@
<dependency>
<groupId>com.android.support</groupId>
<artifactId>support-v4</artifactId>
<version>23.0.1</version>
<version>23.1.1</version>
<type>aar</type>
</dependency>
<dependency>
Expand Down
14 changes: 14 additions & 0 deletions src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,18 @@
1.0 1
-->
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="23"/>

<application>
<activity
android:name="org.acra.CrashReportDialog"
android:theme="@android:style/Theme.Dialog"
android:launchMode="singleInstance"
android:excludeFromRecents="true"
android:finishOnTaskLaunch="true"/>

<service
android:name="org.acra.sender.SenderService"
android:exported="false"
android:process=":acra" />
</application>
</manifest>
67 changes: 37 additions & 30 deletions src/main/java/org/acra/ACRA.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,16 @@
*/
package org.acra;

import android.os.Build;
import org.acra.annotation.ReportsCrashes;
import org.acra.log.ACRALog;
import org.acra.log.AndroidLogDelegate;

import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Build;
import android.preference.PreferenceManager;
import org.acra.annotation.ReportsCrashes;
import org.acra.log.ACRALog;
import org.acra.log.AndroidLogDelegate;

/**
* Use this class to initialize the crash reporting feature using
Expand All @@ -40,12 +38,14 @@
*/
public class ACRA {

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

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

public static ACRALog log = new AndroidLogDelegate();

private static final String ACRA_PRIVATE_PROCESS_NAME= ":acra";

/**
* The key of the application default SharedPreference where you can put a
* 'true' Boolean value to disable ACRA.
Expand Down Expand Up @@ -114,8 +114,7 @@ public class ACRA {
public static void init(Application app) {
final ReportsCrashes reportsCrashes = app.getClass().getAnnotation(ReportsCrashes.class);
if (reportsCrashes == null) {
log.e(LOG_TAG,
"ACRA#init called but no ReportsCrashes annotation on Application " + app.getPackageName());
log.e(LOG_TAG, "ACRA#init called but no ReportsCrashes annotation on Application " + app.getPackageName());
return;
}
init(app, new ACRAConfiguration(reportsCrashes));
Expand Down Expand Up @@ -154,6 +153,11 @@ public static void init(Application app, ACRAConfiguration config) {
*/
public static void init(Application app, ACRAConfiguration config, boolean checkReportsOnApplicationStart){

final boolean senderServiceProcess = isACRASenderServiceProcess(app);
if (senderServiceProcess) {
log.i(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");
}

boolean supportedAndroidVersion = (Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO);
if (!supportedAndroidVersion){
log.w(LOG_TAG, "ACRA 4.7.0+ requires Froyo or greater. ACRA is disabled and will NOT catch crashes or send messages.");
Expand All @@ -179,10 +183,7 @@ public static void init(Application app, ACRAConfiguration config, boolean check
// Initialize ErrorReporter with all required data
final boolean enableAcra = supportedAndroidVersion && !shouldDisableACRA(prefs);
log.d(LOG_TAG, "ACRA is " + (enableAcra ? "enabled" : "disabled") + " for " + mApplication.getPackageName() + ", initializing...");
final ErrorReporter errorReporter = new ErrorReporter(mApplication, prefs, enableAcra, supportedAndroidVersion);

// Append ReportSenders.
errorReporter.setDefaultReportSenders();
final ErrorReporter errorReporter = new ErrorReporter(mApplication, prefs, enableAcra, supportedAndroidVersion, !senderServiceProcess);

errorReporterSingleton = errorReporter;

Expand Down Expand Up @@ -215,6 +216,26 @@ public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, Strin
prefs.registerOnSharedPreferenceChangeListener(mPrefListener);
}

/**
* @return true if the current process is the process running the SenderService.
*/
private static boolean isACRASenderServiceProcess(Application app) {
final String processName = getCurrentProcessName(app);
log.e(LOG_TAG, "ACRA processName='" + processName + "'");
return (processName != null) && processName.endsWith(ACRA_PRIVATE_PROCESS_NAME);
}

private static String getCurrentProcessName(Application app) {
final int processId = android.os.Process.myPid();
final ActivityManager manager = (ActivityManager) app.getSystemService(Context.ACTIVITY_SERVICE);
for (final ActivityManager.RunningAppProcessInfo processInfo : manager.getRunningAppProcesses()){
if(processInfo.pid == processId){
return processInfo.processName;
}
}
return null;
}

/**
* @return the current instance of ErrorReporter.
* @throws IllegalStateException
Expand Down Expand Up @@ -335,20 +356,6 @@ public static ACRAConfiguration getNewDefaultConfig(Application app) {

private static ACRAConfiguration configProxy;

/**
* Returns true if the application is debuggable.
*
* @return true if the application is debuggable.
*/
static boolean isDebuggable() {
PackageManager pm = mApplication.getPackageManager();
try {
return ((pm.getApplicationInfo(mApplication.getPackageName(), 0).flags & ApplicationInfo.FLAG_DEBUGGABLE) > 0);
} catch (NameNotFoundException e) {
return false;
}
}

static Application getApplication() {
return mApplication;
}
Expand Down
40 changes: 27 additions & 13 deletions src/main/java/org/acra/ACRAConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@
package org.acra;

import org.acra.annotation.ReportsCrashes;
import org.acra.sender.DefaultReportSenderFactory;
import org.acra.sender.HttpSender;
import org.acra.sender.HttpSender.Method;
import org.acra.sender.HttpSender.Type;
import org.acra.sender.ReportSenderFactory;

import java.lang.annotation.Annotation;
import java.security.KeyStore;
Expand Down Expand Up @@ -87,6 +89,8 @@ public class ACRAConfiguration implements ReportsCrashes {
private Type mReportType = null;
private Map<String, String> mHttpHeaders;
private KeyStore mKeyStore;
private Class<? extends ReportSenderFactory>[] reportSenderFactoryClasses;


/**
* Set custom HTTP headers to be sent by the provided {@link HttpSender}.
Expand All @@ -96,6 +100,7 @@ public class ACRAConfiguration implements ReportsCrashes {
* A map associating HTTP header names to their values.
* @return The updated ACRA configuration
*/
@SuppressWarnings( "unused" )
public ACRAConfiguration setHttpHeaders(Map<String, String> headers) {
this.mHttpHeaders = headers;
return this;
Expand Down Expand Up @@ -220,13 +225,9 @@ public ACRAConfiguration setForceCloseDialogAfterToast(Boolean forceCloseDialogA
}

/**
* Modify the formUri of your backend server receiving reports. You need to
* call {@link ErrorReporter#setDefaultReportSenders()} after modifying this
* value if you were not using a formUri before (a mailTo or formKey
* instead).
* Modify the formUri of your backend server receiving reports.
*
* @param formUri
* the formUri to set
* @param formUri formUri to set.
* @return The updated ACRA configuration
*/
@SuppressWarnings( "unused" )
Expand Down Expand Up @@ -280,13 +281,9 @@ public ACRAConfiguration setLogcatArguments(String[] logcatArguments) {
}

/**
* Modify the mailTo of the mail account receiving reports. You need to call
* {@link ErrorReporter#setDefaultReportSenders()} after modifying this
* value if you were not using a formKey before (a formKey or formUri
* instead).
* Modify the mailTo of the mail account receiving reports.
*
* @param mailTo
* the mailTo to set
* @param mailTo mailTo to set.
* @return The updated ACRA configuration
*/
@SuppressWarnings( "unused" )
Expand Down Expand Up @@ -1236,6 +1233,24 @@ public Type reportType() {
return Type.FORM;
}

public void setReportSenderFactoryClasses(Class<? extends ReportSenderFactory>[] reportSenderFactoryClasses) {
this.reportSenderFactoryClasses = reportSenderFactoryClasses;
}

@Override
public Class<? extends ReportSenderFactory>[] reportSenderFactoryClasses() {
if (reportSenderFactoryClasses != null) {
return reportSenderFactoryClasses;
}

if (mReportsCrashes != null) {
return mReportsCrashes.reportSenderFactoryClasses();
}

//noinspection unchecked
return new Class[] {DefaultReportSenderFactory.class};
}

public KeyStore keyStore() {
if (mKeyStore != null) {
return mKeyStore;
Expand All @@ -1247,5 +1262,4 @@ public KeyStore keyStore() {
public static boolean isNull(String aString) {
return aString == null || ACRAConstants.NULL_VALUE.equals(aString);
}

}
17 changes: 5 additions & 12 deletions src/main/java/org/acra/ACRAConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,17 @@ public final class ACRAConstants {
* Suffix to be added to report files when they have been approved by the
* user in NOTIFICATION mode
*/
static final String APPROVED_SUFFIX = "-approved";
public static final String APPROVED_SUFFIX = "-approved";
/**
* This key is used to store the silent state of a report sent by
* handleSilentException().
*/
static final String SILENT_SUFFIX = "-" + IS_SILENT;
public static final String SILENT_SUFFIX = "-" + IS_SILENT;
/**
* This is the number of previously stored reports that we send in
* {@link SendWorker#checkAndSendReports(android.content.Context, boolean)}.
* The number of reports is limited to avoid ANR on application start.
* This is the maximum number of previously stored reports that we send
* in one batch to avoid overloading the network.
*/
static final int MAX_SEND_REPORTS = 5;
public static final int MAX_SEND_REPORTS = 5;

/**
* Used in the intent starting CrashReportDialog to provide the name of the
Expand Down Expand Up @@ -82,8 +81,6 @@ public final class ACRAConstants {

public static final boolean DEFAULT_FORCE_CLOSE_DIALOG_AFTER_TOAST = false;

public static final int DEFAULT_MAX_NUMBER_OF_REQUEST_RETRIES = 3;

public static final int DEFAULT_SOCKET_TIMEOUT = 8000;

public static final int DEFAULT_CONNECTION_TIMEOUT = 5000;
Expand Down Expand Up @@ -124,10 +121,6 @@ public final class ACRAConstants {

public static final int DEFAULT_APPLICATION_LOGFILE_LINES = DEFAULT_LOGCAT_LINES;

public static final boolean DEFAULT_DISABLE_SSL_CERT_VALIDATION = false;

public static final String DEFAULT_HTTP_SOCKET_FACTORY_FACTORY_CLASS = "org.acra.util.DefaultHttpsSocketFactoryFactory";

/**
* Default list of {@link ReportField}s to be sent in email reports. You can
* set your own list with
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/acra/BaseCrashReportDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.os.Bundle;
import android.widget.Toast;
import org.acra.collector.CrashReportData;
import org.acra.common.CrashReportPersister;
import org.acra.util.ToastSender;

import java.io.IOException;
Expand Down Expand Up @@ -78,7 +79,6 @@ protected void sendCrash(String comment, String userEmail) {
}

// Start the report sending task
ACRA.log.v(LOG_TAG, "About to start SenderWorker from CrashReportDialog");
ACRA.getErrorReporter().startSendingReports(false, true);

// Optional Toast to thank the user
Expand Down
Loading

0 comments on commit cf3cf27

Please sign in to comment.