1 /*
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 
17 package android.telephony;
18 
19 import android.annotation.IntDef;
20 import android.annotation.NonNull;
21 import android.annotation.Nullable;
22 import android.annotation.SystemApi;
23 import android.os.Parcel;
24 import android.os.Parcelable;
25 
26 import com.android.internal.telephony.PhoneConstants;
27 
28 import java.lang.annotation.Retention;
29 import java.lang.annotation.RetentionPolicy;
30 import java.util.Objects;
31 
32 /**
33  * Holds the result from a PIN attempt.
34  *
35  * @see TelephonyManager#supplyIccLockPin
36  * @see TelephonyManager#supplyIccLockPuk
37  * @see TelephonyManager#setIccLockEnabled
38  * @see TelephonyManager#changeIccLockPin
39  *
40  * @hide
41  */
42 @SystemApi
43 public final class PinResult implements Parcelable {
44     /** @hide */
45     @IntDef({
46             PIN_RESULT_TYPE_SUCCESS,
47             PIN_RESULT_TYPE_INCORRECT,
48             PIN_RESULT_TYPE_FAILURE,
49             PIN_RESULT_TYPE_ABORTED,
50     })
51     @Retention(RetentionPolicy.SOURCE)
52     public @interface PinResultType {}
53 
54     /**
55      * Indicates that the pin attempt was a success.
56      */
57     public static final int PIN_RESULT_TYPE_SUCCESS = PhoneConstants.PIN_RESULT_SUCCESS;
58 
59     /**
60      * Indicates that the pin attempt was incorrect.
61      */
62     public static final int PIN_RESULT_TYPE_INCORRECT = PhoneConstants.PIN_PASSWORD_INCORRECT;
63 
64     /**
65      * Indicates that the pin attempt was a failure.
66      */
67     public static final int PIN_RESULT_TYPE_FAILURE = PhoneConstants.PIN_GENERAL_FAILURE;
68 
69     /**
70      * Indicates that the pin attempt was aborted.
71      */
72     public static final int PIN_RESULT_TYPE_ABORTED = PhoneConstants.PIN_OPERATION_ABORTED;
73 
74     private static final PinResult sFailedResult =
75             new PinResult(PinResult.PIN_RESULT_TYPE_FAILURE, -1);
76 
77     private final @PinResultType int mResult;
78 
79     private final int mAttemptsRemaining;
80 
81     /**
82      * Returns the result of the PIN attempt.
83      *
84      * @return The result of the PIN attempt.
85      */
getResult()86     public @PinResultType int getResult() {
87         return mResult;
88     }
89 
90     /**
91      * Returns the number of PIN attempts remaining.
92      * This will be set when {@link #getResult} is {@link #PIN_RESULT_TYPE_INCORRECT}.
93      * Indicates the number of attempts at entering the PIN before the SIM will be locked and
94      * require a PUK unlock to be performed.
95      *
96      * @return Number of attempts remaining.
97      */
getAttemptsRemaining()98     public int getAttemptsRemaining() {
99         return mAttemptsRemaining;
100     }
101 
102     /**
103      * Used to indicate a failed PIN attempt result.
104      *
105      * @return default PinResult for failure.
106      *
107      * @hide
108      */
109     @NonNull
getDefaultFailedResult()110     public static PinResult getDefaultFailedResult() {
111         return sFailedResult;
112     }
113 
114     /**
115      * PinResult constructor.
116      *
117      * @param result The pin result value.
118      * @see #PIN_RESULT_TYPE_SUCCESS
119      * @see #PIN_RESULT_TYPE_INCORRECT
120      * @see #PIN_RESULT_TYPE_FAILURE
121      * @see #PIN_RESULT_TYPE_ABORTED
122      * @param attemptsRemaining Number of pin attempts remaining.
123      *
124      * @hide
125      */
PinResult(@inResultType int result, int attemptsRemaining)126     public PinResult(@PinResultType int result, int attemptsRemaining) {
127         mResult = result;
128         mAttemptsRemaining = attemptsRemaining;
129     }
130 
131     /**
132      * Construct a PinResult object from the given parcel.
133      *
134      * @hide
135      */
PinResult(Parcel in)136     private PinResult(Parcel in) {
137         mResult = in.readInt();
138         mAttemptsRemaining = in.readInt();
139     }
140 
141     /**
142      * String representation of the Pin Result.
143      */
144     @NonNull
145     @Override
toString()146     public String toString() {
147         return "result: " + getResult() + ", attempts remaining: " + getAttemptsRemaining();
148     }
149 
150     /**
151      * Describe the contents of this object.
152      */
153     @Override
describeContents()154     public int describeContents() {
155         return 0;
156     }
157 
158     /**
159      * Write this object to a Parcel.
160      */
161     @Override
writeToParcel(@onNull Parcel out, int flags)162     public void writeToParcel(@NonNull Parcel out, int flags) {
163         out.writeInt(mResult);
164         out.writeInt(mAttemptsRemaining);
165     }
166 
167     /**
168      * Parcel creator class.
169      */
170     public static final @NonNull Parcelable.Creator<PinResult> CREATOR = new Creator<PinResult>() {
171         public PinResult createFromParcel(Parcel in) {
172             return new PinResult(in);
173         }
174         public PinResult[] newArray(int size) {
175             return new PinResult[size];
176         }
177     };
178 
179     @Override
hashCode()180     public int hashCode() {
181         return Objects.hash(mAttemptsRemaining, mResult);
182     }
183 
184     @Override
equals(@ullable Object obj)185     public boolean equals(@Nullable Object obj) {
186         if (this == obj) {
187             return true;
188         }
189         if (obj == null) {
190             return false;
191         }
192         if (getClass() != obj.getClass()) {
193             return false;
194         }
195         PinResult other = (PinResult) obj;
196         return (mResult == other.mResult
197                 && mAttemptsRemaining == other.mAttemptsRemaining);
198     }
199 }
200