aboutsummaryrefslogtreecommitdiffstats
path: root/openpgp-ktx/src/main/java/org/openintents/openpgp/OpenPgpError.kt
blob: fb1c96023dc9c3d4634903f8cf511166889c855c (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
/*
 * Copyright © 2014-2021 The Android Password Store Authors. All Rights Reserved.
 * SPDX-License-Identifier: GPL-3.0-only
 */
@file:JvmName("OpenPgpError")

package org.openintents.openpgp

import android.os.Parcel
import android.os.Parcelable
import android.os.Parcelable.Creator

public class OpenPgpError() : Parcelable {

  public var errorId: Int = 0
  public var message: String? = null

  private constructor(parcel: Parcel) : this() {
    errorId = parcel.readInt()
    message = parcel.readString()
  }

  internal constructor(errorId: Int, message: String?) : this() {
    this.errorId = errorId
    this.message = message
  }

  internal constructor(b: OpenPgpError) : this() {
    errorId = b.errorId
    message = b.message
  }

  override fun describeContents(): Int {
    return 0
  }

  override fun writeToParcel(dest: Parcel, flags: Int) {
    /**
     * NOTE: When adding fields in the process of updating this API, make sure to bump
     * [PARCELABLE_VERSION].
     */
    dest.writeInt(PARCELABLE_VERSION)
    // Inject a placeholder that will store the parcel size from this point on
    // (not including the size itself).
    val sizePosition = dest.dataPosition()
    dest.writeInt(0)
    val startPosition = dest.dataPosition()
    // version 1
    dest.writeInt(errorId)
    dest.writeString(message)
    // Go back and write the size
    val parcelableSize = dest.dataPosition() - startPosition
    dest.setDataPosition(sizePosition)
    dest.writeInt(parcelableSize)
    dest.setDataPosition(startPosition + parcelableSize)
  }

  public companion object CREATOR : Creator<OpenPgpError> {

    /**
     * Since there might be a case where new versions of the client using the library getting old
     * versions of the protocol (and thus old versions of this class), we need a versioning system
     * for the parcels sent between the clients and the providers.
     */
    private const val PARCELABLE_VERSION = 1

    // possible values for errorId
    public const val CLIENT_SIDE_ERROR: Int = -1
    public const val GENERIC_ERROR: Int = 0
    public const val INCOMPATIBLE_API_VERSIONS: Int = 1
    public const val NO_OR_WRONG_PASSPHRASE: Int = 2
    public const val NO_USER_IDS: Int = 3
    public const val OPPORTUNISTIC_MISSING_KEYS: Int = 4

    override fun createFromParcel(source: Parcel): OpenPgpError? {
      source.readInt() // parcelableVersion
      val parcelableSize = source.readInt()
      val startPosition = source.dataPosition()
      val error = OpenPgpError()
      error.errorId = source.readInt()
      error.message = source.readString()
      // skip over all fields added in future versions of this parcel
      source.setDataPosition(startPosition + parcelableSize)
      return error
    }

    override fun newArray(size: Int): Array<OpenPgpError?> {
      return arrayOfNulls(size)
    }
  }
}