Search in sources :

Example 11 with ComplexElement

use of org.acra.model.ComplexElement in project acra by ACRA.

the class SharedPreferencesCollector method collect.

/**
     * Collects all key/value pairs in SharedPreferences.
     * The application default SharedPreferences are always
     * collected, and the developer can provide additional SharedPreferences
     * names in the {@link ReportsCrashes#additionalSharedPreferences()}
     * configuration item.
     *
     * @return the collected key/value pairs.
     */
@NonNull
private Element collect() throws JSONException {
    final ComplexElement result = new ComplexElement();
    // Include the default SharedPreferences
    final Map<String, SharedPreferences> sharedPrefs = new TreeMap<String, SharedPreferences>();
    sharedPrefs.put("default", PreferenceManager.getDefaultSharedPreferences(context));
    // Add in any additional SharedPreferences
    for (final String sharedPrefId : config.additionalSharedPreferences()) {
        sharedPrefs.put(sharedPrefId, context.getSharedPreferences(sharedPrefId, Context.MODE_PRIVATE));
    }
    // Iterate over all included preference files and add the preferences from each.
    for (Map.Entry<String, SharedPreferences> entry : sharedPrefs.entrySet()) {
        final String sharedPrefId = entry.getKey();
        final SharedPreferences prefs = entry.getValue();
        final Map<String, ?> prefEntries = prefs.getAll();
        // Show that we have no preferences saved for that preference file.
        if (prefEntries.isEmpty()) {
            result.put(sharedPrefId, "empty");
        } else {
            for (Iterator<String> iterator = prefEntries.keySet().iterator(); iterator.hasNext(); ) {
                if (filteredKey(iterator.next())) {
                    iterator.remove();
                }
            }
            result.put(sharedPrefId, new JSONObject(prefEntries));
        }
    }
    return result;
}
Also used : JSONObject(org.json.JSONObject) SharedPreferences(android.content.SharedPreferences) TreeMap(java.util.TreeMap) ComplexElement(org.acra.model.ComplexElement) TreeMap(java.util.TreeMap) Map(java.util.Map) NonNull(android.support.annotation.NonNull)

Example 12 with ComplexElement

use of org.acra.model.ComplexElement in project acra by ACRA.

the class JsonUtils method toCrashReportData.

public static CrashReportData toCrashReportData(JSONObject json) {
    CrashReportData data = new CrashReportData();
    for (Iterator<String> iterator = json.keys(); iterator.hasNext(); ) {
        String key = iterator.next();
        try {
            ReportField field = ReportField.valueOf(key);
            Object value = json.get(key);
            if (value instanceof JSONObject) {
                data.put(field, new ComplexElement((JSONObject) value));
            } else if (value instanceof Number) {
                data.putNumber(field, (Number) value);
            } else if (value instanceof Boolean) {
                data.putBoolean(field, (Boolean) value);
            } else {
                data.putString(field, value.toString());
            }
        } catch (IllegalArgumentException e) {
            Log.w(LOG_TAG, "Unknown report key " + key, e);
        } catch (JSONException e) {
            Log.w(LOG_TAG, "Unable to read report field " + key, e);
        }
    }
    return data;
}
Also used : CrashReportData(org.acra.collector.CrashReportData) ReportField(org.acra.ReportField) JSONObject(org.json.JSONObject) JSONException(org.json.JSONException) JSONObject(org.json.JSONObject) ComplexElement(org.acra.model.ComplexElement)

Aggregations

ComplexElement (org.acra.model.ComplexElement)12 NonNull (android.support.annotation.NonNull)11 ReportField (org.acra.ReportField)6 JSONException (org.json.JSONException)5 Field (java.lang.reflect.Field)4 JSONObject (org.json.JSONObject)4 CrashReportData (org.acra.collector.CrashReportData)2 TargetApi (android.annotation.TargetApi)1 SharedPreferences (android.content.SharedPreferences)1 FeatureInfo (android.content.pm.FeatureInfo)1 PackageManager (android.content.pm.PackageManager)1 MediaCodecInfo (android.media.MediaCodecInfo)1 MediaCodecList (android.media.MediaCodecList)1 Build (android.os.Build)1 DropBoxManager (android.os.DropBoxManager)1 SparseArray (android.util.SparseArray)1 BufferedReader (java.io.BufferedReader)1 ArrayList (java.util.ArrayList)1 Calendar (java.util.Calendar)1 Map (java.util.Map)1