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

Switch to json #523

Merged
merged 10 commits into from
Dec 21, 2016
Merged
Show file tree
Hide file tree
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
15 changes: 2 additions & 13 deletions src/main/java/org/acra/ACRA.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,9 @@
import org.acra.config.ACRAConfiguration;
import org.acra.config.ACRAConfigurationException;
import org.acra.config.ConfigurationBuilder;
import org.acra.legacy.ReportMigrator;
import org.acra.legacy.LegacyFileHandler;
import org.acra.log.ACRALog;
import org.acra.log.AndroidLogDelegate;
import org.acra.prefs.PrefUtils;
import org.acra.prefs.SharedPreferencesFactory;
import org.acra.util.ApplicationStartupProcessor;
import org.acra.util.IOUtils;
Expand Down Expand Up @@ -107,8 +106,6 @@ private ACRA(){}
*/
public static final String PREF_LAST_VERSION_NR = "acra.lastVersionNr";

private static final String PREF__LEGACY_ALREADY_CONVERTED_TO_4_8_0 = "acra.legacyAlreadyConvertedTo4.8.0";

private static Application mApplication;
@Nullable
private static ACRAConfiguration configProxy;
Expand Down Expand Up @@ -236,15 +233,7 @@ public static void init(@NonNull Application app, @NonNull ACRAConfiguration con

final SharedPreferences prefs = new SharedPreferencesFactory(mApplication, configProxy).create();

// Check prefs to see if we have converted from legacy (pre 4.8.0) ACRA
if (!prefs.getBoolean(PREF__LEGACY_ALREADY_CONVERTED_TO_4_8_0, false)) {
// If not then move reports to approved/unapproved folders and mark as converted.
new ReportMigrator(app).migrate();

// Mark as converted.
final SharedPreferences.Editor editor = prefs.edit().putBoolean(PREF__LEGACY_ALREADY_CONVERTED_TO_4_8_0, true);
PrefUtils.save(editor);
}
new LegacyFileHandler(app, prefs).updateToCurrentVersionIfNecessary();

// Initialize ErrorReporter with all required data
final boolean enableAcra = supportedAndroidVersion && !shouldDisableACRA(prefs);
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/org/acra/ACRAConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import android.support.annotation.StringRes;

import org.acra.dialog.CrashReportDialog;
import org.acra.model.Element;
import org.acra.model.StringElement;

import static org.acra.ReportField.*;

Expand Down Expand Up @@ -56,7 +58,6 @@ private ACRAConstants(){}
* comment.
*/
public static final String EXTRA_REPORT_FILE = "REPORT_FILE";
public static final String EXTRA_REPORT_FILE_NAME = "REPORT_FILE_NAME";

/**
* Used in the intent starting CrashReportDialog to provide the Exception that caused the crash.
Expand Down Expand Up @@ -162,5 +163,5 @@ private ACRAConstants(){}

public static final String DEFAULT_CERTIFICATE_TYPE = "X.509";

public static final String NOT_AVAILABLE = "N/A";
public static final Element NOT_AVAILABLE = new StringElement("N/A");
}
17 changes: 9 additions & 8 deletions src/main/java/org/acra/ErrorReporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,16 @@
import android.support.annotation.Nullable;

import org.acra.annotation.ReportsCrashes;
import org.acra.builder.*;
import org.acra.builder.LastActivityManager;
import org.acra.builder.NoOpReportPrimer;
import org.acra.builder.ReportBuilder;
import org.acra.builder.ReportExecutor;
import org.acra.builder.ReportPrimer;
import org.acra.collector.ConfigurationCollector;
import org.acra.collector.CrashReportDataFactory;
import org.acra.util.ApplicationStartupProcessor;
import org.acra.config.ACRAConfiguration;
import org.acra.model.Element;
import org.acra.util.ApplicationStartupProcessor;
import org.acra.util.ProcessFinisher;

import java.lang.Thread.UncaughtExceptionHandler;
Expand Down Expand Up @@ -97,11 +102,11 @@ public void initializeExceptionHandler(ErrorReporter reporter) {

// Store the initial Configuration state.
// This is expensive to gather, so only do so if we plan to report it.
final String initialConfiguration;
final Element initialConfiguration;
if (config.getReportFields().contains(ReportField.INITIAL_CONFIGURATION)) {
initialConfiguration = ConfigurationCollector.collectConfiguration(this.context);
} else {
initialConfiguration = null;
initialConfiguration = ACRAConstants.NOT_AVAILABLE;
}

// Sets the application start date.
Expand Down Expand Up @@ -146,10 +151,6 @@ public void addCustomData(@NonNull String key, String value) {
* latest value is kept for each key (no history of the values is sent in
* the report).
* </p>
* <p>
* The key/value pairs will be stored in the GoogleDoc spreadsheet in the
* "custom" column, as a text containing a 'key = value' pair on each line.
* </p>
*
* @param key A key for your custom data.
* @param value The value associated to your key.
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/org/acra/collector/Collector.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import org.acra.ReportField;
import org.acra.builder.ReportBuilder;
import org.acra.model.Element;

import java.util.Set;

Expand Down Expand Up @@ -69,8 +70,8 @@ boolean shouldCollect(Set<ReportField> crashReportFields, ReportField collect, R
*
* @param reportField the ReportField to collect
* @param reportBuilder the current reportBuilder
* @return String of what was collected
* @return Element of what was collected
*/
@NonNull
abstract String collect(ReportField reportField, ReportBuilder reportBuilder);
abstract Element collect(ReportField reportField, ReportBuilder reportBuilder);
}
52 changes: 29 additions & 23 deletions src/main/java/org/acra/collector/ConfigurationCollector.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,12 @@
import android.util.SparseArray;

import org.acra.ACRA;
import org.acra.ACRAConstants;
import org.acra.ReportField;
import org.acra.builder.ReportBuilder;
import org.acra.model.ComplexElement;
import org.acra.model.Element;
import org.json.JSONException;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
Expand All @@ -35,11 +39,11 @@
* Inspects a {@link Configuration} object through reflection API in order to
* generate a human readable String with values replaced with their constants
* names. The {@link Configuration#toString()} method was not enough as values
* like 0, 1, 2 or 3 don't look readable to me. Using reflection API allows to
* like 0, 1, 2 or 3 aren't readable. Using reflection API allows to
* retrieve hidden fields and can make us hope to be compatible with all Android
* API levels, even those which are not published yet.
*
* @author Kevin Gaudin
* @author Kevin Gaudin & F43nd1r
*/
public final class ConfigurationCollector extends Collector {

Expand All @@ -59,17 +63,17 @@ public final class ConfigurationCollector extends Collector {
private static final String PREFIX_HARDKEYBOARDHIDDEN = "HARDKEYBOARDHIDDEN_";

private final Context context;
private final String initialConfiguration;
private final Element initialConfiguration;

public ConfigurationCollector(Context context, String initialConfiguration) {
public ConfigurationCollector(@NonNull Context context, @NonNull Element initialConfiguration) {
super(ReportField.INITIAL_CONFIGURATION, ReportField.CRASH_CONFIGURATION);
this.context = context;
this.initialConfiguration = initialConfiguration;
}

@NonNull
@Override
String collect(ReportField reportField, ReportBuilder reportBuilder) {
Element collect(ReportField reportField, ReportBuilder reportBuilder) {
switch (reportField) {
case INITIAL_CONFIGURATION:
return initialConfiguration;
Expand All @@ -81,36 +85,38 @@ String collect(ReportField reportField, ReportBuilder reportBuilder) {
}

/**
* Use this method to generate a human readable String listing all values
* Creates an Element listing all values human readable
* from the provided Configuration instance.
*
* @param conf The Configuration to be described.
* @return A String describing all the fields of the given Configuration,
* @return An Element describing all the fields of the given Configuration,
* with values replaced by constant names.
*/
@NonNull
private static String configToString(@NonNull Configuration conf) {
final StringBuilder result = new StringBuilder();
private static Element configToElement(@NonNull Configuration conf) {
final ComplexElement result = new ComplexElement();
Map<String, SparseArray<String>> valueArrays = getValueArrays();
for (final Field f : conf.getClass().getFields()) {
try {
if (!Modifier.isStatic(f.getModifiers())) {
final String fieldName = f.getName();
result.append(fieldName).append('=');
if (f.getType().equals(int.class)) {
result.append(getFieldValueName(valueArrays, conf, f));
} else if (f.get(conf) != null) {
result.append(f.get(conf).toString());
try {
if (f.getType().equals(int.class)) {
result.put(fieldName, getFieldValueName(valueArrays, conf, f));
} else if (f.get(conf) != null) {
result.put(fieldName, f.get(conf));
}
} catch (JSONException e) {
ACRA.log.w(LOG_TAG, "Could not collect configuration field " + fieldName, e);
}
result.append('\n');
}
} catch (@NonNull IllegalArgumentException e) {
ACRA.log.e(LOG_TAG, "Error while inspecting device configuration: ", e);
} catch (@NonNull IllegalAccessException e) {
ACRA.log.e(LOG_TAG, "Error while inspecting device configuration: ", e);
}
}
return result.toString();
return result;
}

private static Map<String, SparseArray<String>> getValueArrays() {
Expand Down Expand Up @@ -181,10 +187,10 @@ private static Map<String, SparseArray<String>> getValueArrays() {
* constant name.
* @throws IllegalAccessException if the supplied field is inaccessible.
*/
private static String getFieldValueName(Map<String, SparseArray<String>> valueArrays, @NonNull Configuration conf, @NonNull Field f) throws IllegalAccessException {
private static Object getFieldValueName(Map<String, SparseArray<String>> valueArrays, @NonNull Configuration conf, @NonNull Field f) throws IllegalAccessException {
final String fieldName = f.getName();
if (fieldName.equals(FIELD_MCC) || fieldName.equals(FIELD_MNC)) {
return Integer.toString(f.getInt(conf));
return f.getInt(conf);
} else if (fieldName.equals(FIELD_UIMODE)) {
return activeFlags(valueArrays.get(PREFIX_UI_MODE), f.getInt(conf));
} else if (fieldName.equals(FIELD_SCREENLAYOUT)) {
Expand All @@ -193,13 +199,13 @@ private static String getFieldValueName(Map<String, SparseArray<String>> valueAr
final SparseArray<String> values = valueArrays.get(fieldName.toUpperCase() + '_');
if (values == null) {
// Unknown field, return the raw int as String
return Integer.toString(f.getInt(conf));
return f.getInt(conf);
}

final String value = values.get(f.getInt(conf));
if (value == null) {
// Unknown value, return the raw int as String
return Integer.toString(f.getInt(conf));
return f.getInt(conf);
}
return value;
}
Expand Down Expand Up @@ -243,12 +249,12 @@ private static String activeFlags(@NonNull SparseArray<String> valueNames, int b
* @return A String representation of the current configuration for the application.
*/
@NonNull
public static String collectConfiguration(@NonNull Context context) {
public static Element collectConfiguration(@NonNull Context context) {
try {
return configToString(context.getResources().getConfiguration());
return configToElement(context.getResources().getConfiguration());
} catch (RuntimeException e) {
ACRA.log.w(LOG_TAG, "Couldn't retrieve CrashConfiguration for : " + context.getPackageName(), e);
return "Couldn't retrieve crash config";
return ACRAConstants.NOT_AVAILABLE;
}
}
}
Loading