Reason.java

 1package eu.siacs.conversations.xmpp.jingle.stanzas;
 2
 3import androidx.annotation.NonNull;
 4
 5import com.google.common.base.CaseFormat;
 6import com.google.common.base.Throwables;
 7
 8import eu.siacs.conversations.crypto.axolotl.AxolotlService;
 9import eu.siacs.conversations.crypto.axolotl.CryptoFailedException;
10import eu.siacs.conversations.xmpp.jingle.RtpContentMap;
11
12public enum Reason {
13    ALTERNATIVE_SESSION,
14    BUSY,
15    CANCEL,
16    CONNECTIVITY_ERROR,
17    DECLINE,
18    EXPIRED,
19    FAILED_APPLICATION,
20    FAILED_TRANSPORT,
21    GENERAL_ERROR,
22    GONE,
23    INCOMPATIBLE_PARAMETERS,
24    MEDIA_ERROR,
25    SECURITY_ERROR,
26    SUCCESS,
27    TIMEOUT,
28    UNSUPPORTED_APPLICATIONS,
29    UNSUPPORTED_TRANSPORTS,
30    UNKNOWN;
31
32    public static Reason of(final String value) {
33        try {
34            return Reason.valueOf(CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_UNDERSCORE, value));
35        } catch (Exception e) {
36            return UNKNOWN;
37        }
38    }
39
40    @Override
41    @NonNull
42    public String toString() {
43        return CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.LOWER_HYPHEN, super.toString());
44    }
45
46    public static Reason of(final RuntimeException e) {
47        if (e instanceof SecurityException) {
48            return SECURITY_ERROR;
49        } else if (e instanceof RtpContentMap.UnsupportedTransportException) {
50            return UNSUPPORTED_TRANSPORTS;
51        } else if (e instanceof RtpContentMap.UnsupportedApplicationException) {
52            return UNSUPPORTED_APPLICATIONS;
53        } else {
54            return FAILED_APPLICATION;
55        }
56    }
57
58    public static Reason ofThrowable(final Throwable throwable) {
59        final Throwable root = Throwables.getRootCause(throwable);
60        if (root instanceof RuntimeException) {
61            return of((RuntimeException) root);
62        }
63        if (root instanceof CryptoFailedException) {
64            return SECURITY_ERROR;
65        }
66        return FAILED_APPLICATION;
67    }
68}