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.xmpp.jingle.RtpContentMap;
10
11public enum Reason {
12    ALTERNATIVE_SESSION,
13    BUSY,
14    CANCEL,
15    CONNECTIVITY_ERROR,
16    DECLINE,
17    EXPIRED,
18    FAILED_APPLICATION,
19    FAILED_TRANSPORT,
20    GENERAL_ERROR,
21    GONE,
22    INCOMPATIBLE_PARAMETERS,
23    MEDIA_ERROR,
24    SECURITY_ERROR,
25    SUCCESS,
26    TIMEOUT,
27    UNSUPPORTED_APPLICATIONS,
28    UNSUPPORTED_TRANSPORTS,
29    UNKNOWN;
30
31    public static Reason of(final String value) {
32        try {
33            return Reason.valueOf(CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_UNDERSCORE, value));
34        } catch (Exception e) {
35            return UNKNOWN;
36        }
37    }
38
39    @Override
40    @NonNull
41    public String toString() {
42        return CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.LOWER_HYPHEN, super.toString());
43    }
44
45    public static Reason of(final RuntimeException e) {
46        if (e instanceof SecurityException) {
47            return SECURITY_ERROR;
48        } else if (e instanceof RtpContentMap.UnsupportedTransportException) {
49            return UNSUPPORTED_TRANSPORTS;
50        } else if (e instanceof RtpContentMap.UnsupportedApplicationException) {
51            return UNSUPPORTED_APPLICATIONS;
52        } else {
53            return FAILED_APPLICATION;
54        }
55    }
56
57    public static Reason ofThrowable(final Throwable throwable) {
58        final Throwable root = Throwables.getRootCause(throwable);
59        if (root instanceof RuntimeException) {
60            return of((RuntimeException) root);
61        }
62        return FAILED_APPLICATION;
63    }
64}