blob: 98d6448e77eabf605750c8b568220c8685518f18 [file] [log] [blame]
Daniel Brightb663fcb2020-01-17 14:11:40 -08001/*
2 * Copyright (C) 2020 The Android Open Source Project
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 android.telephony;
18
19import android.annotation.IntDef;
20import android.annotation.NonNull;
21import android.annotation.Nullable;
Daniel Brightb663fcb2020-01-17 14:11:40 -080022import android.os.Parcel;
23import android.os.Parcelable;
24
25import com.android.internal.telephony.PhoneConstants;
26
27import java.util.Objects;
28
29/**
30 * Holds the result from a pin attempt.
31 *
32 * @hide
33 */
Daniel Brightb663fcb2020-01-17 14:11:40 -080034public final class PinResult implements Parcelable {
35 /** @hide */
36 @IntDef({
37 PIN_RESULT_TYPE_SUCCESS,
38 PIN_RESULT_TYPE_INCORRECT,
39 PIN_RESULT_TYPE_FAILURE,
40 })
41 public @interface PinResultType {}
42
43 /**
44 * Indicates that the pin attempt was a success.
45 */
46 public static final int PIN_RESULT_TYPE_SUCCESS = PhoneConstants.PIN_RESULT_SUCCESS;
47
48 /**
49 * Indicates that the pin attempt was incorrect.
50 */
51 public static final int PIN_RESULT_TYPE_INCORRECT = PhoneConstants.PIN_PASSWORD_INCORRECT;
52
53 /**
54 * Indicates that the pin attempt was a failure.
55 */
56 public static final int PIN_RESULT_TYPE_FAILURE = PhoneConstants.PIN_GENERAL_FAILURE;
57
58 private static final PinResult sFailedResult =
59 new PinResult(PinResult.PIN_RESULT_TYPE_FAILURE, -1);
60
61 private final @PinResultType int mType;
62
63 private final int mAttemptsRemaining;
64
65 /**
66 * Returns either success, incorrect or failure.
67 *
easoncylee8f582432020-01-20 10:55:34 +080068 * @see #PIN_RESULT_TYPE_SUCCESS
69 * @see #PIN_RESULT_TYPE_INCORRECT
70 * @see #PIN_RESULT_TYPE_FAILURE
Daniel Brightb663fcb2020-01-17 14:11:40 -080071 * @return The result type of the pin attempt.
72 */
73 public @PinResultType int getType() {
74 return mType;
75 }
76
77 /**
78 * The number of pin attempts remaining.
79 *
80 * @return Number of attempts remaining.
81 */
82 public int getAttemptsRemaining() {
83 return mAttemptsRemaining;
84 }
85
86 @NonNull
87 public static PinResult getDefaultFailedResult() {
88 return sFailedResult;
89 }
90
91 /**
92 * PinResult constructor
93 *
94 * @param type The type of pin result.
easoncylee8f582432020-01-20 10:55:34 +080095 * @see #PIN_RESULT_TYPE_SUCCESS
96 * @see #PIN_RESULT_TYPE_INCORRECT
97 * @see #PIN_RESULT_TYPE_FAILURE
Daniel Brightb663fcb2020-01-17 14:11:40 -080098 * @param attemptsRemaining Number of pin attempts remaining.
99 */
100 public PinResult(@PinResultType int type, int attemptsRemaining) {
101 mType = type;
102 mAttemptsRemaining = attemptsRemaining;
103 }
104
105 /**
106 * Construct a PinResult object from the given parcel.
107 *
108 * @hide
109 */
110 private PinResult(Parcel in) {
111 mType = in.readInt();
112 mAttemptsRemaining = in.readInt();
113 }
114
115 /**
116 * String representation of the Pin Result.
117 */
118 @NonNull
119 @Override
120 public String toString() {
121 return "type: " + getType() + ", attempts remaining: " + getAttemptsRemaining();
122 }
123
124 /**
125 * Required to be Parcelable
126 */
127 @Override
128 public int describeContents() {
129 return 0;
130 }
131
132 /**
133 * Required to be Parcelable
134 */
135 @Override
136 public void writeToParcel(@NonNull Parcel out, int flags) {
137 out.writeInt(mType);
138 out.writeInt(mAttemptsRemaining);
139 }
140
141 /** Required to be Parcelable */
142 public static final @NonNull Parcelable.Creator<PinResult> CREATOR = new Creator<PinResult>() {
143 public PinResult createFromParcel(Parcel in) {
144 return new PinResult(in);
145 }
146 public PinResult[] newArray(int size) {
147 return new PinResult[size];
148 }
149 };
150
151 @Override
152 public int hashCode() {
153 return Objects.hash(mAttemptsRemaining, mType);
154 }
155
156 @Override
157 public boolean equals(@Nullable Object obj) {
158 if (this == obj) {
159 return true;
160 }
161 if (obj == null) {
162 return false;
163 }
164 if (getClass() != obj.getClass()) {
165 return false;
166 }
167 PinResult other = (PinResult) obj;
168 return (mType == other.mType
169 && mAttemptsRemaining == other.mAttemptsRemaining);
170 }
171}