aboutsummaryrefslogtreecommitdiffstats
path: root/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/pgp/OperationResultParcel.java
blob: 497c3dd711eb32df9ec10ee055a2a6de686b1bc4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package org.sufficientlysecure.keychain.pgp;

import android.R;
import android.os.Parcel;
import android.os.Parcelable;

import java.util.ArrayList;

/** Represent the result of an operation.
 *
 * This class holds a result and the log of an operation. It can be subclassed
 * to include typed additional information specific to the operation. To keep
 * the class structure (somewhat) simple, this class contains an exhaustive
 * list (ie, enum) of all possible log types, which should in all cases be tied
 * to string resource ids.
 *
 */
public class OperationResultParcel implements Parcelable {
    /** Holds the overall result. A value of 0 is considered a success, all
     * other values may represent failure or varying degrees of success. */
    final int mResult;

    /// A list of log entries tied to the operation result.
    final ArrayList<LogEntryParcel> mLog;

    public OperationResultParcel(int result, ArrayList<LogEntryParcel> log) {
        mResult = result;
        mLog = log;
    }

    public OperationResultParcel(Parcel source) {
        mResult = source.readInt();
        mLog = source.createTypedArrayList(LogEntryParcel.CREATOR);
    }

    public boolean isSuccessful() {
        return mResult == 0;
    }

    /** One entry in the log. */
    public static class LogEntryParcel implements Parcelable {
        final LogType mType;
        final LogLevel mLevel;
        final String[] mParameters;

        public LogEntryParcel(LogType type, LogLevel level, String[] parameters) {
            mType = type;
            mLevel = level;
            mParameters = parameters;
        }

        public LogEntryParcel(Parcel source) {
            mType = LogType.values()[source.readInt()];
            mLevel = LogLevel.values()[source.readInt()];
            mParameters = source.createStringArray();
        }

        @Override
        public int describeContents() {
            return 0;
        }

        @Override
        public void writeToParcel(Parcel dest, int flags) {
            dest.writeInt(mType.ordinal());
            dest.writeInt(mLevel.ordinal());
            dest.writeStringArray(mParameters);
        }

        public static final Creator<LogEntryParcel> CREATOR = new Creator<LogEntryParcel>() {
            public LogEntryParcel createFromParcel(final Parcel source) {
                return new LogEntryParcel(source);
            }

            public LogEntryParcel[] newArray(final int size) {
                return new LogEntryParcel[size];
            }
        };

    }

    public static enum LogType {
        // TODO add actual log entry types here
        MSG_IMPORT_OK (R.string.copy),
        MSG_IMPORT_FAILED (R.string.cancel);

        private int mMsgId;
        LogType(int msgId) {
            mMsgId = msgId;
        }
    }

    /** Enumeration of possible log levels. */
    public static enum LogLevel {
        DEBUG,
        INFO,
        WARN,
        /** If any ERROR log entry is included in the result, the overall operation should have failed. */
        ERROR,
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(mResult);
        dest.writeTypedList(mLog);
    }

    public static final Creator<OperationResultParcel> CREATOR = new Creator<OperationResultParcel>() {
        public OperationResultParcel createFromParcel(final Parcel source) {
            return new OperationResultParcel(source);
        }

        public OperationResultParcel[] newArray(final int size) {
            return new OperationResultParcel[size];
        }
    };

}