OpenPgpSignatureResult.java

  1/*
  2 * Copyright (C) 2014 Dominik Schürmann <dominik@dominikschuermann.de>
  3 *
  4 * Licensed under the Apache License, Version 2.0 (the "License");
  5 * you may not use this file except in compliance with the License.
  6 * You may obtain a copy of the License at
  7 *
  8 *      http://www.apache.org/licenses/LICENSE-2.0
  9 *
 10 * Unless required by applicable law or agreed to in writing, software
 11 * distributed under the License is distributed on an "AS IS" BASIS,
 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13 * See the License for the specific language governing permissions and
 14 * limitations under the License.
 15 */
 16
 17package org.openintents.openpgp;
 18
 19import android.os.Parcel;
 20import android.os.Parcelable;
 21
 22import org.openintents.openpgp.util.OpenPgpUtils;
 23
 24import java.util.Locale;
 25
 26/**
 27 * Parcelable versioning has been copied from Dashclock Widget
 28 * https://code.google.com/p/dashclock/source/browse/api/src/main/java/com/google/android/apps/dashclock/api/ExtensionData.java
 29 */
 30public class OpenPgpSignatureResult implements Parcelable {
 31    /**
 32     * Since there might be a case where new versions of the client using the library getting
 33     * old versions of the protocol (and thus old versions of this class), we need a versioning
 34     * system for the parcels sent between the clients and the providers.
 35     */
 36    public static final int PARCELABLE_VERSION = 1;
 37
 38    // generic error on signature verification
 39    public static final int SIGNATURE_ERROR = 0;
 40    // successfully verified signature, with certified key
 41    public static final int SIGNATURE_SUCCESS_CERTIFIED = 1;
 42    // no key was found for this signature verification
 43    public static final int SIGNATURE_KEY_MISSING = 2;
 44    // successfully verified signature, but with uncertified key
 45    public static final int SIGNATURE_SUCCESS_UNCERTIFIED = 3;
 46    // key has been revoked
 47    public static final int SIGNATURE_KEY_REVOKED = 4;
 48    // key is expired
 49    public static final int SIGNATURE_KEY_EXPIRED = 5;
 50
 51    int status;
 52    boolean signatureOnly;
 53    String userId;
 54    long keyId;
 55
 56    public int getStatus() {
 57        return status;
 58    }
 59
 60    public void setStatus(int status) {
 61        this.status = status;
 62    }
 63
 64    public boolean isSignatureOnly() {
 65        return signatureOnly;
 66    }
 67
 68    public void setSignatureOnly(boolean signatureOnly) {
 69        this.signatureOnly = signatureOnly;
 70    }
 71
 72    public String getUserId() {
 73        return userId;
 74    }
 75
 76    public void setUserId(String userId) {
 77        this.userId = userId;
 78    }
 79
 80    public long getKeyId() {
 81        return keyId;
 82    }
 83
 84    public void setKeyId(long keyId) {
 85        this.keyId = keyId;
 86    }
 87
 88    public OpenPgpSignatureResult() {
 89
 90    }
 91
 92    public OpenPgpSignatureResult(int signatureStatus, String signatureUserId,
 93                                  boolean signatureOnly, long keyId) {
 94        this.status = signatureStatus;
 95        this.signatureOnly = signatureOnly;
 96        this.userId = signatureUserId;
 97        this.keyId = keyId;
 98    }
 99
100    public OpenPgpSignatureResult(OpenPgpSignatureResult b) {
101        this.status = b.status;
102        this.userId = b.userId;
103        this.signatureOnly = b.signatureOnly;
104        this.keyId = b.keyId;
105    }
106
107    public int describeContents() {
108        return 0;
109    }
110
111    public void writeToParcel(Parcel dest, int flags) {
112        /**
113         * NOTE: When adding fields in the process of updating this API, make sure to bump
114         * {@link #PARCELABLE_VERSION}.
115         */
116        dest.writeInt(PARCELABLE_VERSION);
117        // Inject a placeholder that will store the parcel size from this point on
118        // (not including the size itself).
119        int sizePosition = dest.dataPosition();
120        dest.writeInt(0);
121        int startPosition = dest.dataPosition();
122        // version 1
123        dest.writeInt(status);
124        dest.writeByte((byte) (signatureOnly ? 1 : 0));
125        dest.writeString(userId);
126        dest.writeLong(keyId);
127        // Go back and write the size
128        int parcelableSize = dest.dataPosition() - startPosition;
129        dest.setDataPosition(sizePosition);
130        dest.writeInt(parcelableSize);
131        dest.setDataPosition(startPosition + parcelableSize);
132    }
133
134    public static final Creator<OpenPgpSignatureResult> CREATOR = new Creator<OpenPgpSignatureResult>() {
135        public OpenPgpSignatureResult createFromParcel(final Parcel source) {
136            int parcelableVersion = source.readInt();
137            int parcelableSize = source.readInt();
138            int startPosition = source.dataPosition();
139
140            OpenPgpSignatureResult vr = new OpenPgpSignatureResult();
141            vr.status = source.readInt();
142            vr.signatureOnly = source.readByte() == 1;
143            vr.userId = source.readString();
144            vr.keyId = source.readLong();
145
146            // skip over all fields added in future versions of this parcel
147            source.setDataPosition(startPosition + parcelableSize);
148
149            return vr;
150        }
151
152        public OpenPgpSignatureResult[] newArray(final int size) {
153            return new OpenPgpSignatureResult[size];
154        }
155    };
156
157    @Override
158    public String toString() {
159        String out = new String();
160        out += "\nstatus: " + status;
161        out += "\nuserId: " + userId;
162        out += "\nsignatureOnly: " + signatureOnly;
163        out += "\nkeyId: " + OpenPgpUtils.convertKeyIdToHex(keyId);
164        return out;
165    }
166
167}