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 public key
 41    public static final int SIGNATURE_SUCCESS_CERTIFIED = 1;
 42    // no public key was found for this signature verification
 43    public static final int SIGNATURE_UNKNOWN_PUB_KEY = 2;
 44    // successfully verified signature, but with uncertified public key
 45    public static final int SIGNATURE_SUCCESS_UNCERTIFIED = 3;
 46
 47    int status;
 48    boolean signatureOnly;
 49    String userId;
 50    long keyId;
 51
 52    public int getStatus() {
 53        return status;
 54    }
 55
 56    public void setStatus(int status) {
 57        this.status = status;
 58    }
 59
 60    public boolean isSignatureOnly() {
 61        return signatureOnly;
 62    }
 63
 64    public void setSignatureOnly(boolean signatureOnly) {
 65        this.signatureOnly = signatureOnly;
 66    }
 67
 68    public String getUserId() {
 69        return userId;
 70    }
 71
 72    public void setUserId(String userId) {
 73        this.userId = userId;
 74    }
 75
 76    public long getKeyId() {
 77        return keyId;
 78    }
 79
 80    public void setKeyId(long keyId) {
 81        this.keyId = keyId;
 82    }
 83
 84    public OpenPgpSignatureResult() {
 85
 86    }
 87
 88    public OpenPgpSignatureResult(int signatureStatus, String signatureUserId,
 89                                  boolean signatureOnly, long keyId) {
 90        this.status = signatureStatus;
 91        this.signatureOnly = signatureOnly;
 92        this.userId = signatureUserId;
 93        this.keyId = keyId;
 94    }
 95
 96    public OpenPgpSignatureResult(OpenPgpSignatureResult b) {
 97        this.status = b.status;
 98        this.userId = b.userId;
 99        this.signatureOnly = b.signatureOnly;
100        this.keyId = b.keyId;
101    }
102
103    public int describeContents() {
104        return 0;
105    }
106
107    public void writeToParcel(Parcel dest, int flags) {
108        /**
109         * NOTE: When adding fields in the process of updating this API, make sure to bump
110         * {@link #PARCELABLE_VERSION}.
111         */
112        dest.writeInt(PARCELABLE_VERSION);
113        // Inject a placeholder that will store the parcel size from this point on
114        // (not including the size itself).
115        int sizePosition = dest.dataPosition();
116        dest.writeInt(0);
117        int startPosition = dest.dataPosition();
118        // version 1
119        dest.writeInt(status);
120        dest.writeByte((byte) (signatureOnly ? 1 : 0));
121        dest.writeString(userId);
122        dest.writeLong(keyId);
123        // Go back and write the size
124        int parcelableSize = dest.dataPosition() - startPosition;
125        dest.setDataPosition(sizePosition);
126        dest.writeInt(parcelableSize);
127        dest.setDataPosition(startPosition + parcelableSize);
128    }
129
130    public static final Creator<OpenPgpSignatureResult> CREATOR = new Creator<OpenPgpSignatureResult>() {
131        public OpenPgpSignatureResult createFromParcel(final Parcel source) {
132            int parcelableVersion = source.readInt();
133            int parcelableSize = source.readInt();
134            int startPosition = source.dataPosition();
135
136            OpenPgpSignatureResult vr = new OpenPgpSignatureResult();
137            vr.status = source.readInt();
138            vr.signatureOnly = source.readByte() == 1;
139            vr.userId = source.readString();
140            vr.keyId = source.readLong();
141
142            // skip over all fields added in future versions of this parcel
143            source.setDataPosition(startPosition + parcelableSize);
144
145            return vr;
146        }
147
148        public OpenPgpSignatureResult[] newArray(final int size) {
149            return new OpenPgpSignatureResult[size];
150        }
151    };
152
153    @Override
154    public String toString() {
155        String out = new String();
156        out += "\nstatus: " + status;
157        out += "\nuserId: " + userId;
158        out += "\nsignatureOnly: " + signatureOnly;
159        out += "\nkeyId: " + OpenPgpUtils.convertKeyIdToHex(keyId);
160        return out;
161    }
162
163}