1 /*
2  * Copyright (C) 2023 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.app;
18 
19 import android.annotation.DurationMillisLong;
20 import android.annotation.IntDef;
21 import android.annotation.NonNull;
22 import android.annotation.SystemApi;
23 import android.os.Parcel;
24 import android.os.Parcelable;
25 
26 import java.lang.annotation.Retention;
27 import java.lang.annotation.RetentionPolicy;
28 /**
29  * Result of lock screen credentials verification.
30  *
31  * @hide
32  */
33 @SystemApi
34 public final class RemoteLockscreenValidationResult implements Parcelable {
35 
36     /**
37      * The guess was correct
38      */
39     public static final int RESULT_GUESS_VALID = 1;
40 
41     /**
42      * Remote device provided incorrect credentials.
43      */
44     public static final int RESULT_GUESS_INVALID = 2;
45 
46     /**
47      * The operation was canceled because the API is locked out due to too many attempts. It
48      * usually happens after 5 failed attempts and API may be called again after a short
49      * delay specified by {@code getTimeoutMillis}.
50      */
51     public static final int RESULT_LOCKOUT = 3;
52 
53     /**
54      * There were too many invalid guesses.
55      */
56     public static final int RESULT_NO_REMAINING_ATTEMPTS = 4;
57 
58     /**
59      * New lockscreen validation session is required to verify guess.
60      */
61     public static final int RESULT_SESSION_EXPIRED = 5;
62 
63     @IntDef({RESULT_GUESS_VALID,
64             RESULT_GUESS_INVALID,
65             RESULT_LOCKOUT,
66             RESULT_NO_REMAINING_ATTEMPTS,
67             RESULT_SESSION_EXPIRED})
68     @Retention(RetentionPolicy.SOURCE)
69     @interface ResultCode {}
70 
71     private int mResultCode;
72     private long mTimeoutMillis;
73 
74     public static final @NonNull Parcelable.Creator<RemoteLockscreenValidationResult> CREATOR =
75             new Parcelable.Creator<RemoteLockscreenValidationResult>() {
76         @Override
77         public RemoteLockscreenValidationResult createFromParcel(Parcel source) {
78             return new RemoteLockscreenValidationResult(source);
79         }
80 
81         @Override
82         public RemoteLockscreenValidationResult[] newArray(int size) {
83             return new RemoteLockscreenValidationResult[size];
84         }
85     };
86 
87     /**
88      * Builder for {@code RemoteLockscreenValidationResult}
89      */
90     public static final class Builder {
91         private RemoteLockscreenValidationResult mInstance = new RemoteLockscreenValidationResult();
92 
93         /**
94          * Sets the result code.
95          */
setResultCode(@esultCode int resultCode)96         public @NonNull Builder setResultCode(@ResultCode int resultCode) {
97             mInstance.mResultCode = resultCode;
98             return this;
99         }
100 
101         /**
102          * Sets timeout for {@code RESULT_LOCKOUT}.
103          * Default value is {@code 0}.
104          */
setTimeoutMillis(@urationMillisLong long timeoutMillis)105         public @NonNull Builder setTimeoutMillis(@DurationMillisLong long timeoutMillis) {
106             mInstance.mTimeoutMillis = timeoutMillis;
107             return this;
108         }
109 
110         /**
111          * Creates {@code RemoteLockscreenValidationResult}.
112          *
113          * @throws IllegalStateException if result code was not set.
114          */
build()115         public @NonNull RemoteLockscreenValidationResult build() {
116             if (mInstance.mResultCode == 0) {
117                 throw new IllegalStateException("Result code must be set");
118             }
119             return mInstance;
120         }
121     }
122 
123     /**
124      * Gets the result code.
125      */
getResultCode()126     public @ResultCode int getResultCode() {
127         return mResultCode;
128     }
129 
130     /**
131      * Delay before next attempt to verify credentials.
132      *
133      * Default value is {@code 0}.
134      */
getTimeoutMillis()135     public @DurationMillisLong long getTimeoutMillis() {
136         return mTimeoutMillis;
137     }
138 
139 
140     @Override
writeToParcel(@onNull Parcel out, int flags)141     public void writeToParcel(@NonNull Parcel out, int flags) {
142         out.writeInt(mResultCode);
143         out.writeLong(mTimeoutMillis);
144     }
145 
RemoteLockscreenValidationResult()146     private RemoteLockscreenValidationResult() {
147     }
148 
RemoteLockscreenValidationResult(Parcel in)149     private RemoteLockscreenValidationResult(Parcel in) {
150         mResultCode = in.readInt();
151         mTimeoutMillis = in.readLong();
152     }
153 
154     @Override
describeContents()155     public int describeContents() {
156         return 0;
157     }
158 }
159