OpenPgpMetadata.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
 22/**
 23 * Parcelable versioning has been copied from Dashclock Widget
 24 * https://code.google.com/p/dashclock/source/browse/api/src/main/java/com/google/android/apps/dashclock/api/ExtensionData.java
 25 */
 26public class OpenPgpMetadata implements Parcelable {
 27    /**
 28     * Since there might be a case where new versions of the client using the library getting
 29     * old versions of the protocol (and thus old versions of this class), we need a versioning
 30     * system for the parcels sent between the clients and the providers.
 31     */
 32    public static final int PARCELABLE_VERSION = 1;
 33
 34    String filename;
 35    String mimeType;
 36    long modificationTime;
 37    long originalSize;
 38
 39    public String getFilename() {
 40        return filename;
 41    }
 42
 43    public String getMimeType() {
 44        return mimeType;
 45    }
 46
 47    public long getModificationTime() {
 48        return modificationTime;
 49    }
 50
 51    public long getOriginalSize() {
 52        return originalSize;
 53    }
 54
 55    public OpenPgpMetadata() {
 56    }
 57
 58    public OpenPgpMetadata(String filename, String mimeType, long modificationTime,
 59                           long originalSize) {
 60        this.filename = filename;
 61        this.mimeType = mimeType;
 62        this.modificationTime = modificationTime;
 63        this.originalSize = originalSize;
 64    }
 65
 66    public OpenPgpMetadata(OpenPgpMetadata b) {
 67        this.filename = b.filename;
 68        this.mimeType = b.mimeType;
 69        this.modificationTime = b.modificationTime;
 70        this.originalSize = b.originalSize;
 71    }
 72
 73    public int describeContents() {
 74        return 0;
 75    }
 76
 77    public void writeToParcel(Parcel dest, int flags) {
 78        /**
 79         * NOTE: When adding fields in the process of updating this API, make sure to bump
 80         * {@link #PARCELABLE_VERSION}.
 81         */
 82        dest.writeInt(PARCELABLE_VERSION);
 83        // Inject a placeholder that will store the parcel size from this point on
 84        // (not including the size itself).
 85        int sizePosition = dest.dataPosition();
 86        dest.writeInt(0);
 87        int startPosition = dest.dataPosition();
 88        // version 1
 89        dest.writeString(filename);
 90        dest.writeString(mimeType);
 91        dest.writeLong(modificationTime);
 92        dest.writeLong(originalSize);
 93        // Go back and write the size
 94        int parcelableSize = dest.dataPosition() - startPosition;
 95        dest.setDataPosition(sizePosition);
 96        dest.writeInt(parcelableSize);
 97        dest.setDataPosition(startPosition + parcelableSize);
 98    }
 99
100    public static final Creator<OpenPgpMetadata> CREATOR = new Creator<OpenPgpMetadata>() {
101        public OpenPgpMetadata createFromParcel(final Parcel source) {
102            int parcelableVersion = source.readInt();
103            int parcelableSize = source.readInt();
104            int startPosition = source.dataPosition();
105
106            OpenPgpMetadata vr = new OpenPgpMetadata();
107            vr.filename = source.readString();
108            vr.mimeType = source.readString();
109            vr.modificationTime = source.readLong();
110            vr.originalSize = source.readLong();
111
112            // skip over all fields added in future versions of this parcel
113            source.setDataPosition(startPosition + parcelableSize);
114
115            return vr;
116        }
117
118        public OpenPgpMetadata[] newArray(final int size) {
119            return new OpenPgpMetadata[size];
120        }
121    };
122
123    @Override
124    public String toString() {
125        String out = "\nfilename: " + filename;
126        out += "\nmimeType: " + mimeType;
127        out += "\nmodificationTime: " + modificationTime;
128        out += "\noriginalSize: " + originalSize;
129        return out;
130    }
131
132}