Search in sources :

Example 1 with ComplexElement

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

the class DropBoxCollector method collect.

/**
     * Read latest messages contained in the DropBox for system related tags and
     * optional developer-set tags.
     *
     * @return An Element listing messages retrieved.
     */
@NonNull
@Override
Element collect(ReportField reportField, ReportBuilder reportBuilder) {
    try {
        final DropBoxManager dropbox = (DropBoxManager) context.getSystemService(Context.DROPBOX_SERVICE);
        final Calendar calendar = Calendar.getInstance();
        calendar.roll(Calendar.MINUTE, -config.dropboxCollectionMinutes());
        final long time = calendar.getTimeInMillis();
        dateFormat.format(calendar.getTime());
        final List<String> tags = new ArrayList<String>();
        if (config.includeDropBoxSystemTags()) {
            tags.addAll(Arrays.asList(SYSTEM_TAGS));
        }
        final Set<String> additionalTags = config.additionalDropBoxTags();
        if (!additionalTags.isEmpty()) {
            tags.addAll(additionalTags);
        }
        if (tags.isEmpty()) {
            return ACRAConstants.NOT_AVAILABLE;
        }
        final ComplexElement dropboxContent = new ComplexElement();
        for (String tag : tags) {
            final StringBuilder builder = new StringBuilder();
            DropBoxManager.Entry entry = dropbox.getNextEntry(tag, time);
            if (entry == null) {
                builder.append("Nothing.").append('\n');
                continue;
            }
            while (entry != null) {
                final long msec = entry.getTimeMillis();
                calendar.setTimeInMillis(msec);
                builder.append('@').append(dateFormat.format(calendar.getTime())).append('\n');
                final String text = entry.getText(500);
                if (text != null) {
                    builder.append("Text: ").append(text).append('\n');
                } else {
                    builder.append("Not Text!").append('\n');
                }
                entry.close();
                entry = dropbox.getNextEntry(tag, msec);
            }
            dropboxContent.put(tag, builder.toString());
        }
        return dropboxContent;
    } catch (Exception e) {
        if (ACRA.DEV_LOGGING)
            ACRA.log.d(LOG_TAG, "DropBoxManager not available.");
    }
    return ACRAConstants.NOT_AVAILABLE;
}
Also used : DropBoxManager(android.os.DropBoxManager) Calendar(java.util.Calendar) ArrayList(java.util.ArrayList) ComplexElement(org.acra.model.ComplexElement) NonNull(android.support.annotation.NonNull)

Example 2 with ComplexElement

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

the class MediaCodecListCollector method collectMediaCodecList.

/**
     * Builds an Element describing the list of available codecs on the device
     * with their capabilities (supported Color Formats, Codec Profiles et
     * Levels).
     *
     * @return The media codecs information
     */
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@NonNull
private Element collectMediaCodecList() throws JSONException {
    prepare();
    final MediaCodecInfo[] infos;
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
        //noinspection deprecation
        final int codecCount = MediaCodecList.getCodecCount();
        infos = new MediaCodecInfo[codecCount];
        for (int codecIdx = 0; codecIdx < codecCount; codecIdx++) {
            //noinspection deprecation
            infos[codecIdx] = MediaCodecList.getCodecInfoAt(codecIdx);
        }
    } else {
        infos = new MediaCodecList(MediaCodecList.ALL_CODECS).getCodecInfos();
    }
    final ComplexElement result = new ComplexElement();
    for (int i = 0; i < infos.length; i++) {
        final MediaCodecInfo codecInfo = infos[i];
        JSONObject codec = new JSONObject();
        final String[] supportedTypes = codecInfo.getSupportedTypes();
        codec.put("name", codecInfo.getName()).put("isEncoder", codecInfo.isEncoder());
        JSONObject supportedTypesJson = new JSONObject();
        for (String type : supportedTypes) {
            supportedTypesJson.put(type, collectCapabilitiesForType(codecInfo, type));
        }
        codec.put("supportedTypes", supportedTypesJson);
        result.put(String.valueOf(i), codec);
    }
    return result;
}
Also used : JSONObject(org.json.JSONObject) MediaCodecInfo(android.media.MediaCodecInfo) MediaCodecList(android.media.MediaCodecList) ComplexElement(org.acra.model.ComplexElement) NonNull(android.support.annotation.NonNull) TargetApi(android.annotation.TargetApi)

Example 3 with ComplexElement

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

the class ReflectionCollector method collect.

@NonNull
@Override
Element collect(ReportField reportField, ReportBuilder reportBuilder) {
    ComplexElement result = new ComplexElement();
    try {
        switch(reportField) {
            case BUILD:
                collectConstants(Build.class, result);
                JSONObject version = new JSONObject();
                collectConstants(Build.VERSION.class, version);
                result.put("VERSION", version);
                break;
            case BUILD_CONFIG:
                try {
                    collectConstants(getBuildConfigClass(), result);
                } catch (ClassNotFoundException e) {
                //already logged in getBuildConfigClass
                }
                break;
            case ENVIRONMENT:
                collectStaticGettersResults(Environment.class, result);
                break;
            default:
                //will not happen if used correctly
                throw new IllegalArgumentException();
        }
    } catch (JSONException e) {
        ACRA.log.w("Couldn't collect constants", e);
        return ACRAConstants.NOT_AVAILABLE;
    }
    return result;
}
Also used : JSONObject(org.json.JSONObject) Build(android.os.Build) JSONException(org.json.JSONException) ComplexElement(org.acra.model.ComplexElement) NonNull(android.support.annotation.NonNull)

Example 4 with ComplexElement

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

the class ThreadCollector method collect.

/**
     * collects some data identifying the crashed thread
     *
     * @return the information including the id, name and priority of the thread.
     */
@NonNull
@Override
Element collect(ReportField reportField, ReportBuilder reportBuilder) {
    Thread t = reportBuilder.getUncaughtExceptionThread();
    final ComplexElement result = new ComplexElement();
    if (t != null) {
        try {
            result.put("id", t.getId());
            result.put("name", t.getName());
            result.put("priority", t.getPriority());
            if (t.getThreadGroup() != null) {
                result.put("groupName", t.getThreadGroup().getName());
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    } else {
        return ACRAConstants.NOT_AVAILABLE;
    }
    return result;
}
Also used : JSONException(org.json.JSONException) ComplexElement(org.acra.model.ComplexElement) NonNull(android.support.annotation.NonNull)

Example 5 with ComplexElement

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

the class ReportConverter method legacyLoad.

/**
     * Loads properties from the specified InputStream. The properties are of
     * the form <code>key=value</code>, one property per line. It may be not
     * encode as 'ISO-8859-1'.The {@code Properties} file is interpreted
     * according to the following rules:
     * <ul>
     * <li>Empty lines are ignored.</li>
     * <li>Lines starting with either a "#" or a "!" are comment lines and are
     * ignored.</li>
     * <li>A backslash at the end of the line escapes the following newline
     * character ("\r", "\n", "\r\n"). If there's a whitespace after the
     * backslash it will just escape that whitespace instead of concatenating
     * the lines. This does not apply to comment lines.</li>
     * <li>A property line consists of the key, the space between the key and
     * the value, and the value. The key goes up to the first whitespace, "=" or
     * ":" that is not escaped. The space between the key and the value contains
     * either one whitespace, one "=" or one ":" and any number of additional
     * whitespaces before and after that character. The value starts with the
     * first character after the space between the key and the value.</li>
     * <li>Following escape sequences are recognized: "\ ", "\\", "\r", "\n",
     * "\!", "\#", "\t", "\b", "\f", and "&#92;uXXXX" (unicode character).</li>
     * </ul>
     *
     * @param reader Reader from which to read the properties of this CrashReportData.
     * @return CrashReportData read from the supplied Reader.
     * @throws java.io.IOException if the properties could not be read.
     * @since 1.6
     */
@NonNull
private synchronized CrashReportData legacyLoad(@NonNull Reader reader) throws IOException {
    int mode = NONE, unicode = 0, count = 0;
    char nextChar;
    char[] buf = new char[40];
    int offset = 0, keyLength = -1, intVal;
    boolean firstChar = true;
    final CrashReportData crashData = new CrashReportData();
    final BufferedReader br = new BufferedReader(reader, ACRAConstants.DEFAULT_BUFFER_SIZE_IN_BYTES);
    try {
        while (true) {
            intVal = br.read();
            if (intVal == -1) {
                break;
            }
            nextChar = (char) intVal;
            if (offset == buf.length) {
                final char[] newBuf = new char[buf.length * 2];
                System.arraycopy(buf, 0, newBuf, 0, offset);
                buf = newBuf;
            }
            if (mode == UNICODE) {
                final int digit = Character.digit(nextChar, 16);
                if (digit >= 0) {
                    unicode = (unicode << 4) + digit;
                    if (++count < 4) {
                        continue;
                    }
                } else if (count <= 4) {
                    // luni.09=Invalid Unicode sequence: illegal character
                    throw new IllegalArgumentException("luni.09");
                }
                mode = NONE;
                buf[offset++] = (char) unicode;
                if (nextChar != '\n' && nextChar != '…') {
                    continue;
                }
            }
            if (mode == SLASH) {
                mode = NONE;
                switch(nextChar) {
                    case '\r':
                        // Look for a following \n
                        mode = CONTINUE;
                        continue;
                    case '…':
                    case '\n':
                        // Ignore whitespace on the next line
                        mode = IGNORE;
                        continue;
                    case 'b':
                        nextChar = '\b';
                        break;
                    case 'f':
                        nextChar = '\f';
                        break;
                    case 'n':
                        nextChar = '\n';
                        break;
                    case 'r':
                        nextChar = '\r';
                        break;
                    case 't':
                        nextChar = '\t';
                        break;
                    case 'u':
                        mode = UNICODE;
                        unicode = count = 0;
                        continue;
                }
            } else {
                switch(nextChar) {
                    case '#':
                    case '!':
                        if (firstChar) {
                            while (true) {
                                intVal = br.read();
                                if (intVal == -1) {
                                    break;
                                }
                                // & 0xff
                                nextChar = (char) intVal;
                                // required
                                if (nextChar == '\r' || nextChar == '\n' || nextChar == '…') {
                                    break;
                                }
                            }
                            continue;
                        }
                        break;
                    case '\n':
                        if (mode == CONTINUE) {
                            // Part of a \r\n sequence
                            // Ignore whitespace on the next line
                            mode = IGNORE;
                            continue;
                        }
                    // fall into the next case
                    case '…':
                    case '\r':
                        mode = NONE;
                        firstChar = true;
                        if (offset > 0 || (offset == 0 && keyLength == 0)) {
                            if (keyLength == -1) {
                                keyLength = offset;
                            }
                            final String temp = new String(buf, 0, offset);
                            final String elementString = temp.substring(keyLength);
                            Element element;
                            try {
                                element = new ComplexElement(elementString);
                            } catch (JSONException e1) {
                                try {
                                    element = new NumberElement(Double.valueOf(elementString));
                                } catch (NumberFormatException e2) {
                                    if (elementString.equals("true")) {
                                        element = new BooleanElement(true);
                                    } else if (elementString.equals("false")) {
                                        element = new BooleanElement(false);
                                    } else {
                                        element = new StringElement(elementString);
                                    }
                                }
                            }
                            crashData.put(Enum.valueOf(ReportField.class, temp.substring(0, keyLength)), element);
                        }
                        keyLength = -1;
                        offset = 0;
                        continue;
                    case '\\':
                        if (mode == KEY_DONE) {
                            keyLength = offset;
                        }
                        mode = SLASH;
                        continue;
                    case ':':
                    case '=':
                        if (keyLength == -1) {
                            // if parsing the key
                            mode = NONE;
                            keyLength = offset;
                            continue;
                        }
                        break;
                }
                if (Character.isWhitespace(nextChar)) {
                    if (mode == CONTINUE) {
                        mode = IGNORE;
                    }
                    // if key length == 0 or value length == 0
                    if (offset == 0 || offset == keyLength || mode == IGNORE) {
                        continue;
                    }
                    if (keyLength == -1) {
                        // if parsing the key
                        mode = KEY_DONE;
                        continue;
                    }
                }
                if (mode == IGNORE || mode == CONTINUE) {
                    mode = NONE;
                }
            }
            firstChar = false;
            if (mode == KEY_DONE) {
                keyLength = offset;
                mode = NONE;
            }
            buf[offset++] = nextChar;
        }
        if (mode == UNICODE && count <= 4) {
            // luni.08=Invalid Unicode sequence: expected format \\uxxxx
            throw new IllegalArgumentException("luni.08");
        }
        if (keyLength == -1 && offset > 0) {
            keyLength = offset;
        }
        if (keyLength >= 0) {
            final String temp = new String(buf, 0, offset);
            final ReportField key = Enum.valueOf(ReportField.class, temp.substring(0, keyLength));
            String value = temp.substring(keyLength);
            if (mode == SLASH) {
                value += "";
            }
            Element element;
            try {
                element = new ComplexElement(value);
            } catch (JSONException e1) {
                try {
                    element = new NumberElement(Double.valueOf(value));
                } catch (NumberFormatException e2) {
                    if (value.equals("true")) {
                        element = new BooleanElement(true);
                    } else if (value.equals("false")) {
                        element = new BooleanElement(false);
                    } else {
                        element = new StringElement(value);
                    }
                }
            }
            crashData.put(key, element);
        }
        IOUtils.safeClose(reader);
        return crashData;
    } finally {
        IOUtils.safeClose(br);
    }
}
Also used : StringElement(org.acra.model.StringElement) Element(org.acra.model.Element) StringElement(org.acra.model.StringElement) ComplexElement(org.acra.model.ComplexElement) BooleanElement(org.acra.model.BooleanElement) NumberElement(org.acra.model.NumberElement) NumberElement(org.acra.model.NumberElement) JSONException(org.json.JSONException) BooleanElement(org.acra.model.BooleanElement) CrashReportData(org.acra.collector.CrashReportData) ReportField(org.acra.ReportField) BufferedReader(java.io.BufferedReader) ComplexElement(org.acra.model.ComplexElement) NonNull(android.support.annotation.NonNull)

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